r"""Helper function for computing SSIM. SSIM estimates covariances with weighted sums. The default parameters use a biased estimate of the covariance: Suppose `reducer` is a weighted sum, then the mean estimators are \mu_x = \sum_i w_i x_i, \mu_y = \sum_i w_i y_i, where w_i's are t
(x, y, reducer, max_val, compensation=1.0, k1=0.01, k2=0.03)
| 3147 | |
| 3148 | |
| 3149 | def _ssim_helper(x, y, reducer, max_val, compensation=1.0, k1=0.01, k2=0.03): |
| 3150 | r"""Helper function for computing SSIM. |
| 3151 | |
| 3152 | SSIM estimates covariances with weighted sums. The default parameters |
| 3153 | use a biased estimate of the covariance: |
| 3154 | Suppose `reducer` is a weighted sum, then the mean estimators are |
| 3155 | \mu_x = \sum_i w_i x_i, |
| 3156 | \mu_y = \sum_i w_i y_i, |
| 3157 | where w_i's are the weighted-sum weights, and covariance estimator is |
| 3158 | cov_{xy} = \sum_i w_i (x_i - \mu_x) (y_i - \mu_y) |
| 3159 | with assumption \sum_i w_i = 1. This covariance estimator is biased, since |
| 3160 | E[cov_{xy}] = (1 - \sum_i w_i ^ 2) Cov(X, Y). |
| 3161 | For SSIM measure with unbiased covariance estimators, pass as `compensation` |
| 3162 | argument (1 - \sum_i w_i ^ 2). |
| 3163 | |
| 3164 | Arguments: |
| 3165 | x: First set of images. |
| 3166 | y: Second set of images. |
| 3167 | reducer: Function that computes 'local' averages from set of images. For |
| 3168 | non-convolutional version, this is usually tf.reduce_mean(x, [1, 2]), and |
| 3169 | for convolutional version, this is usually tf.nn.avg_pool2d or |
| 3170 | tf.nn.conv2d with weighted-sum kernel. |
| 3171 | max_val: The dynamic range (i.e., the difference between the maximum |
| 3172 | possible allowed value and the minimum allowed value). |
| 3173 | compensation: Compensation factor. See above. |
| 3174 | k1: Default value 0.01 |
| 3175 | k2: Default value 0.03 (SSIM is less sensitivity to K2 for lower values, so |
| 3176 | it would be better if we taken the values in range of 0< K2 <0.4). |
| 3177 | |
| 3178 | Returns: |
| 3179 | A pair containing the luminance measure, and the contrast-structure measure. |
| 3180 | """ |
| 3181 | |
| 3182 | c1 = (k1 * max_val)**2 |
| 3183 | c2 = (k2 * max_val)**2 |
| 3184 | |
| 3185 | # SSIM luminance measure is |
| 3186 | # (2 * mu_x * mu_y + c1) / (mu_x ** 2 + mu_y ** 2 + c1). |
| 3187 | mean0 = reducer(x) |
| 3188 | mean1 = reducer(y) |
| 3189 | num0 = mean0 * mean1 * 2.0 |
| 3190 | den0 = math_ops.square(mean0) + math_ops.square(mean1) |
| 3191 | luminance = (num0 + c1) / (den0 + c1) |
| 3192 | |
| 3193 | # SSIM contrast-structure measure is |
| 3194 | # (2 * cov_{xy} + c2) / (cov_{xx} + cov_{yy} + c2). |
| 3195 | # Note that `reducer` is a weighted sum with weight w_k, \sum_i w_i = 1, then |
| 3196 | # cov_{xy} = \sum_i w_i (x_i - \mu_x) (y_i - \mu_y) |
| 3197 | # = \sum_i w_i x_i y_i - (\sum_i w_i x_i) (\sum_j w_j y_j). |
| 3198 | num1 = reducer(x * y) * 2.0 |
| 3199 | den1 = reducer(math_ops.square(x) + math_ops.square(y)) |
| 3200 | c2 *= compensation |
| 3201 | cs = (num1 - num0 + c2) / (den1 - den0 + c2) |
| 3202 | |
| 3203 | # SSIM score is the product of the luminance and contrast-structure measures. |
| 3204 | return luminance, cs |
| 3205 | |
| 3206 |
no test coverage detected