From 7923d2894b59511f24703c69f7d57650ae178ed3 Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Wed, 18 Jan 2023 11:02:56 -0600
Subject: [PATCH] snapshot...

---
 modules/deeplearning/srcnn_l1b_l2.py | 43 +++++++++++++++++++---------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/modules/deeplearning/srcnn_l1b_l2.py b/modules/deeplearning/srcnn_l1b_l2.py
index 6a86303e..e72c1f17 100644
--- a/modules/deeplearning/srcnn_l1b_l2.py
+++ b/modules/deeplearning/srcnn_l1b_l2.py
@@ -37,6 +37,7 @@ NOISE_STDDEV = 0.01
 DO_AUGMENT = True
 
 DO_ZERO_OUT = False
+DO_ESPCN = False  # Note: If True, cannot do mixed resolution input fields (Adjust accordingly below)
 
 # setup scaling parameters dictionary
 mean_std_dct = {}
@@ -98,10 +99,11 @@ elif KERNEL_SIZE == 5:
     y_2 = np.arange(68)
 # ----------------------------------------
 # Exp for ESPCN version
-# slc_x_2 = slice(0, 132, 2)
-# slc_y_2 = slice(0, 132, 2)
-# x_128 = slice(2, 130)
-# y_128 = slice(2, 130)
+if DO_ESPCN:
+    slc_x_2 = slice(0, 132, 2)
+    slc_y_2 = slice(0, 132, 2)
+    x_128 = slice(2, 130)
+    y_128 = slice(2, 130)
 
 
 def build_residual_conv2d_block(conv, num_filters, block_name, activation=tf.nn.relu, padding='SAME',
@@ -127,6 +129,13 @@ def build_residual_conv2d_block(conv, num_filters, block_name, activation=tf.nn.
     return conv
 
 
+def upsample(tmp):
+    tmp = tmp[:, slc_y_2, slc_x_2]
+    tmp = resample_2d_linear(x_2, y_2, tmp, t, s)
+    tmp = tmp[:, y_k, x_k]
+    return tmp
+
+
 class SRCNN:
     
     def __init__(self):
@@ -259,10 +268,10 @@ class SRCNN:
             tmp = tmp.copy()
             tmp = np.where(np.isnan(tmp), 0, tmp)
             # tmp = smooth_2d(tmp, sigma=1.0)
-            # Half res upsampled to full res:
-            tmp = tmp[:, slc_y_2, slc_x_2]
-            tmp = resample_2d_linear(x_2, y_2, tmp, t, s)
-            tmp = tmp[:, y_k, x_k]
+            if DO_ESPCN:
+                tmp = tmp[:, slc_y_2, slc_x_2]
+            else:  # Half res upsampled to full res:
+                tmp = upsample(tmp)
             tmp = normalize(tmp, param, mean_std_dct)
             if DO_ADD_NOISE:
                 tmp = add_noise(tmp, noise_scale=NOISE_STDDEV)
@@ -285,9 +294,10 @@ class SRCNN:
         tmp = tmp.copy()
         tmp = np.where(np.isnan(tmp), 0, tmp)
         # tmp = smooth_2d(tmp, sigma=1.0)
-        tmp = tmp[:, slc_y_2, slc_x_2]
-        tmp = resample_2d_linear(x_2, y_2, tmp, t, s)
-        tmp = tmp[:, y_k, x_k]
+        if DO_ESPCN:
+            tmp = tmp[:, slc_y_2, slc_x_2]
+        else:  # Half res upsampled to full res:
+            tmp = upsample(tmp)
         if label_param != 'cloud_probability':
             tmp = normalize(tmp, label_param, mean_std_dct)
             if DO_ADD_NOISE:
@@ -432,9 +442,16 @@ class SRCNN:
 
         conv = conv + conv_b
         print(conv.shape)
-        # This is effectively a Dense layer
-        self.logits = tf.keras.layers.Conv2D(1, kernel_size=1, strides=1, padding=padding, name='regression')(conv)
 
+        if not DO_ESPCN:
+            # This is effectively a Dense layer
+            self.logits = tf.keras.layers.Conv2D(1, kernel_size=1, strides=1, padding=padding, name='regression')(conv)
+        else:
+            conv = tf.keras.layers.Conv2D(num_filters * (factor ** 2), 3, padding=padding, activation=activation)(conv)
+            print(conv.shape)
+            conv = tf.nn.depth_to_space(conv, factor)
+            print(conv.shape)
+            self.logits = tf.keras.layers.Conv2D(IMG_DEPTH, kernel_size=3, strides=1, padding=padding, name='regression')(conv)
         print(self.logits.shape)
 
     def build_training(self):
-- 
GitLab