Skip to content
Snippets Groups Projects
Select Git revision
  • e7a4586d0e4eb6edf94471b3dab3391ca725af12
  • master default protected
  • enxhi_issue460_remove_TOAR-I_access
  • michael_issue459_preprocess_german_stations
  • sh_pollutants
  • develop protected
  • release_v2.4.0
  • michael_issue450_feat_load-ifs-data
  • lukas_issue457_feat_set-config-paths-as-parameter
  • lukas_issue454_feat_use-toar-statistics-api-v2
  • lukas_issue453_refac_advanced-retry-strategy
  • lukas_issue452_bug_update-proj-version
  • lukas_issue449_refac_load-era5-data-from-toar-db
  • lukas_issue451_feat_robust-apriori-estimate-for-short-timeseries
  • lukas_issue448_feat_load-model-from-path
  • lukas_issue447_feat_store-and-load-local-clim-apriori-data
  • lukas_issue445_feat_data-insight-plot-monthly-distribution
  • lukas_issue442_feat_bias-free-evaluation
  • lukas_issue444_feat_choose-interp-method-cams
  • 414-include-crps-analysis-and-other-ens-verif-methods-or-plots
  • lukas_issue384_feat_aqw-data-handler
  • v2.4.0 protected
  • v2.3.0 protected
  • v2.2.0 protected
  • v2.1.0 protected
  • Kleinert_etal_2022_initial_submission
  • v2.0.0 protected
  • v1.5.0 protected
  • v1.4.0 protected
  • v1.3.0 protected
  • v1.2.1 protected
  • v1.2.0 protected
  • v1.1.0 protected
  • IntelliO3-ts-v1.0_R1-submit
  • v1.0.0 protected
  • v0.12.2 protected
  • v0.12.1 protected
  • v0.12.0 protected
  • v0.11.0 protected
  • v0.10.0 protected
  • IntelliO3-ts-v1.0_initial-submit
41 results

model_class.py

Blame
  • metrics.py 1.97 KiB
    import tensorflow as tf
    #import lpips_tf
    import math
    import numpy as np
    try:
        from skimage.measure import compare_ssim as ssim_ski
    except:
        try:
            import skimage.metrics._structural_similarity as ssmi_ski
        except ModuleNotFoundError as err:
            print("Could not get ssmi-function from skimage. Please check installed skimage-package.")
            raise err
    
    
    
    def mse(a, b):
        return tf.reduce_mean(tf.squared_difference(a, b), [-3, -2, -1])
    
    
    def psnr(a, b):
        return tf.image.psnr(a, b, 1.0)
    
    
    def ssim(a, b):
        return tf.image.ssim(a, b, 1.0)
    
    
    def psnr_imgs(img1, img2, pixel_max=1.):
        mse_all = mse(img1, img2)
        if mse_all == 0: return 100
        return 20 * math.log10(pixel_max / math.sqrt(mse_all))
    
    
    def mse_imgs(image1,image2):
        mse = ((image1 - image2)**2).mean(axis=None)
        return mse
    
    # def lpips(input0, input1):
    #     if input0.shape[-1].value == 1:
    #         input0 = tf.tile(input0, [1] * (input0.shape.ndims - 1) + [3])
    #     if input1.shape[-1].value == 1:
    #         input1 = tf.tile(input1, [1] * (input1.shape.ndims - 1) + [3])
    #
    #     distance = lpips_tf.lpips(input0, input1)
    #     return -distance
    
    def ssim_images(image1, image2):
        """
        Reference for calculating ssim
        Numpy impelmeentation for ssim https://cvnote.ddlee.cc/2019/09/12/psnr-ssim-python
        https://scikit-image.org/docs/dev/auto_examples/transform/plot_ssim.html
        :param image1 the reference images
        :param image2 the predicte images
        """
        ssim_pred = ssim_ski(image1, image2,
                          data_range = image2.max() - image2.min())
        return ssim_pred
    
    def acc_imgs(image1,image2,clim):
        """
        Reference for calculating acc
        :param image1 the reference images ?? single image or batch_size images?
        :param image2 the predicte images
        :param clim the climatology images
        """
        img1_ = image1-clim
        img2_ = image2-clim
        cor1 = np.sum(img1_*img2_)  
        cor2 = np.sqrt(np.sum(img1_**2)*np.sum(img2_**2))
        acc = cor1/cor2
        return acc