From 2067929723d78bd7c1bbfaa7e432205f18ed4593 Mon Sep 17 00:00:00 2001
From: tomrink <rink@ssec.wisc.edu>
Date: Mon, 26 Sep 2022 10:41:39 -0500
Subject: [PATCH] initial commit...

---
 modules/deeplearning/srcnn_l1b_l2.py | 701 +++++++++++++++++++++++++++
 1 file changed, 701 insertions(+)
 create mode 100644 modules/deeplearning/srcnn_l1b_l2.py

diff --git a/modules/deeplearning/srcnn_l1b_l2.py b/modules/deeplearning/srcnn_l1b_l2.py
new file mode 100644
index 00000000..ec572ce7
--- /dev/null
+++ b/modules/deeplearning/srcnn_l1b_l2.py
@@ -0,0 +1,701 @@
+import glob
+import tensorflow as tf
+from util.setup import logdir, modeldir, cachepath, now, ancillary_path
+from util.util import EarlyStop, normalize, denormalize, resample, resample_one, get_grid_values_all
+import os, datetime
+import numpy as np
+import pickle
+import h5py
+
+# L1B M/I-bands: /apollo/cloud/scratch/cwhite/VIIRS_HRES/2019/2019_01_01/
+# CLAVRx: /apollo/cloud/scratch/Satellite_Output/VIIRS_HRES/2019/2019_01_01/
+# /apollo/cloud/scratch/Satellite_Output/andi/NEW/VIIRS_HRES/2019
+
+LOG_DEVICE_PLACEMENT = False
+
+PROC_BATCH_SIZE = 4
+PROC_BATCH_BUFFER_SIZE = 50000
+
+NumClasses = 2
+if NumClasses == 2:
+    NumLogits = 1
+else:
+    NumLogits = NumClasses
+
+BATCH_SIZE = 64
+NUM_EPOCHS = 80
+
+TRACK_MOVING_AVERAGE = False
+EARLY_STOP = True
+
+NOISE_TRAINING = False
+NOISE_STDDEV = 0.10
+DO_AUGMENT = True
+
+# setup scaling parameters dictionary
+mean_std_dct = {}
+mean_std_file = ancillary_path+'mean_std_lo_hi_l2.pkl'
+f = open(mean_std_file, 'rb')
+mean_std_dct_l2 = pickle.load(f)
+f.close()
+
+mean_std_file = ancillary_path+'mean_std_lo_hi_l1b.pkl'
+f = open(mean_std_file, 'rb')
+mean_std_dct_l1b = pickle.load(f)
+f.close()
+
+mean_std_dct.update(mean_std_dct_l1b)
+mean_std_dct.update(mean_std_dct_l2)
+
+params = ['temp_11_0um_nom', 'temp_12_0um_nom', 'cloud_fraction']
+data_params = ['temp_11_0um_nom', 'temp_12_0um_nom']
+label_params = ['cloud_fraction']
+
+
+DO_ZERO_OUT = False
+
+label_idx = 0
+label_param = label_params[label_idx]
+print('data_params: ', data_params)
+print('label_params: ', label_params)
+
+x_134 = np.arange(134)
+y_134 = np.arange(134)
+x_64 = np.arange(64)
+y_64 = np.arange(64)
+x_134_2 = x_134[3:131:2]
+y_134_2 = y_134[3:131:2]
+#x_134_2 = x_134[2:133:2]
+#y_134_2 = y_134[2:133:2]
+t = np.arange(0, 64, 0.5)
+s = np.arange(0, 64, 0.5)
+
+
+def build_residual_conv2d_block(conv, num_filters, block_name, activation=tf.nn.relu, padding='SAME', kernel_initializer='he_uniform', scale=None):
+
+    with tf.name_scope(block_name):
+        skip = tf.keras.layers.Conv2D(num_filters, kernel_size=3, padding=padding, kernel_initializer=kernel_initializer, activation=activation)(conv)
+        skip = tf.keras.layers.Conv2D(num_filters, kernel_size=3, padding=padding, activation=None)(skip)
+        if scale is not None:
+            skip = tf.keras.layers.Lambda(lambda x: x * scale)(skip)
+        conv = conv + skip
+        print(block_name+':', conv.shape)
+
+    return conv
+
+
+class SRCNN:
+    
+    def __init__(self):
+
+        self.train_data = None
+        self.train_label = None
+        self.test_data = None
+        self.test_label = None
+        self.test_data_denorm = None
+        
+        self.train_dataset = None
+        self.inner_train_dataset = None
+        self.test_dataset = None
+        self.eval_dataset = None
+        self.X_img = None
+        self.X_prof = None
+        self.X_u = None
+        self.X_v = None
+        self.X_sfc = None
+        self.inputs = []
+        self.y = None
+        self.handle = None
+        self.inner_handle = None
+        self.in_mem_batch = None
+
+        self.h5f_l1b_trn = None
+        self.h5f_l1b_tst = None
+        self.h5f_l2_trn = None
+        self.h5f_l2_tst = None
+
+        self.logits = None
+
+        self.predict_data = None
+        self.predict_dataset = None
+        self.mean_list = None
+        self.std_list = None
+        
+        self.training_op = None
+        self.correct = None
+        self.accuracy = None
+        self.loss = None
+        self.pred_class = None
+        self.variable_averages = None
+
+        self.global_step = None
+
+        self.writer_train = None
+        self.writer_valid = None
+        self.writer_train_valid_loss = None
+
+        self.OUT_OF_RANGE = False
+
+        self.abi = None
+        self.temp = None
+        self.wv = None
+        self.lbfp = None
+        self.sfc = None
+
+        self.in_mem_data_cache = {}
+        self.in_mem_data_cache_test = {}
+
+        self.model = None
+        self.optimizer = None
+        self.ema = None
+        self.train_loss = None
+        self.train_accuracy = None
+        self.test_loss = None
+        self.test_accuracy = None
+        self.test_auc = None
+        self.test_recall = None
+        self.test_precision = None
+        self.test_confusion_matrix = None
+        self.test_true_pos = None
+        self.test_true_neg = None
+        self.test_false_pos = None
+        self.test_false_neg = None
+
+        self.test_labels = []
+        self.test_preds = []
+        self.test_probs = None
+
+        self.learningRateSchedule = None
+        self.num_data_samples = None
+        self.initial_learning_rate = None
+
+        self.data_dct = None
+        self.train_data_files = None
+        self.train_label_files = None
+        self.test_data_files = None
+        self.test_label_files = None
+
+        self.train_data_nda = None
+        self.train_label_nda = None
+        self.test_data_nda = None
+        self.test_label_nda = None
+
+        self.n_chans = 1
+
+        self.X_img = tf.keras.Input(shape=(None, None, self.n_chans))
+        # self.X_img = tf.keras.Input(shape=(36, 36, self.n_chans))
+        # self.X_img = tf.keras.Input(shape=(34, 34, self.n_chans))
+        # self.X_img = tf.keras.Input(shape=(66, 66, self.n_chans))
+
+        self.inputs.append(self.X_img)
+
+        tf.debugging.set_log_device_placement(LOG_DEVICE_PLACEMENT)
+
+    def get_in_mem_data_batch(self, idxs, is_training):
+        if is_training:
+            files = self.train_data_files
+        else:
+            files = self.test_data_files
+
+        data_s = []
+        label_s = []
+        for k in idxs:
+            f = files[k]
+            nda = np.load(f)
+            data_s.append(nda[0:len(data_params), :, :])
+            label_s.append(nda[2, :, :])
+
+        data = np.concatenate(data_s)
+        label = np.concatenate(label_s)
+
+        add_noise = None
+        noise_scale = None
+        if is_training:
+            add_noise = True
+            noise_scale = 0.005
+
+        data_norm = []
+        for k, param in enumerate(data_params):
+            data = data[:, k, 3:131:2, 3:131:2]
+            data = resample(y_64, x_64, data, s, t)
+            data = normalize(data, param, mean_std_dct, add_noise=add_noise, noise_scale=noise_scale)
+            data_norm.append(data)
+        data = np.stack(data_norm, axis=3)
+        data = data.astype(np.float32)
+
+        # label = label[:, 3:131:2, 3:131:2]
+        label = label[:, 3:131, 3:131]
+        label = np.expand_dims(label, axis=3)
+        if label_param != 'cloud_fraction':
+            label = normalize(label, label_param, mean_std_dct)
+
+        data = data.astype(np.float32)
+        label = label.astype(np.float32)
+
+        if is_training and DO_AUGMENT:
+            data_ud = np.flip(data, axis=1)
+            label_ud = np.flip(label, axis=1)
+
+            data_lr = np.flip(data, axis=2)
+            label_lr = np.flip(label, axis=2)
+
+            data = np.concatenate([data, data_ud, data_lr])
+            label = np.concatenate([label, label_ud, label_lr])
+
+        return data, label
+
+    def get_in_mem_data_batch_train(self, idxs):
+        return self.get_in_mem_data_batch(idxs, True)
+
+    def get_in_mem_data_batch_test(self, idxs):
+        return self.get_in_mem_data_batch(idxs, False)
+
+    def get_in_mem_data_batch_eval(self, idxs):
+        data = []
+        for param in self.train_params:
+            nda = self.data_dct[param]
+            nda = normalize(nda, param, mean_std_dct)
+            data.append(nda)
+        data = np.stack(data)
+        data = data.astype(np.float32)
+        data = np.transpose(data, axes=(1, 2, 0))
+        data = np.expand_dims(data, axis=0)
+
+        return data
+
+    @tf.function(input_signature=[tf.TensorSpec(None, tf.int32)])
+    def data_function(self, indexes):
+        out = tf.numpy_function(self.get_in_mem_data_batch_train, [indexes], [tf.float32, tf.float32])
+        return out
+
+    @tf.function(input_signature=[tf.TensorSpec(None, tf.int32)])
+    def data_function_test(self, indexes):
+        out = tf.numpy_function(self.get_in_mem_data_batch_test, [indexes], [tf.float32, tf.float32])
+        return out
+
+    @tf.function(input_signature=[tf.TensorSpec(None, tf.int32)])
+    def data_function_evaluate(self, indexes):
+        # TODO: modify for user specified altitude
+        out = tf.numpy_function(self.get_in_mem_data_batch_eval, [indexes], [tf.float32])
+        return out
+
+    def get_train_dataset(self, indexes):
+        indexes = list(indexes)
+
+        dataset = tf.data.Dataset.from_tensor_slices(indexes)
+        dataset = dataset.batch(PROC_BATCH_SIZE)
+        dataset = dataset.map(self.data_function, num_parallel_calls=8)
+        dataset = dataset.cache()
+        if DO_AUGMENT:
+            dataset = dataset.shuffle(PROC_BATCH_BUFFER_SIZE)
+        dataset = dataset.prefetch(buffer_size=1)
+        self.train_dataset = dataset
+
+    def get_test_dataset(self, indexes):
+        indexes = list(indexes)
+
+        dataset = tf.data.Dataset.from_tensor_slices(indexes)
+        dataset = dataset.batch(PROC_BATCH_SIZE)
+        dataset = dataset.map(self.data_function_test, num_parallel_calls=8)
+        dataset = dataset.cache()
+        self.test_dataset = dataset
+
+    def get_evaluate_dataset(self, indexes):
+        indexes = list(indexes)
+
+        dataset = tf.data.Dataset.from_tensor_slices(indexes)
+        dataset = dataset.map(self.data_function_evaluate, num_parallel_calls=8)
+        self.eval_dataset = dataset
+
+    def setup_pipeline(self, train_data_files, test_data_files, num_train_samples):
+
+        self.train_data_files = train_data_files
+        self.test_data_files = test_data_files
+
+        trn_idxs = np.arange(len(train_data_files))
+        np.random.shuffle(trn_idxs)
+        tst_idxs = np.arange(len(train_data_files))
+
+        self.get_train_dataset(trn_idxs)
+        self.get_test_dataset(tst_idxs)
+
+        self.num_data_samples = num_train_samples  # approximately
+
+        print('datetime: ', now)
+        print('training and test data: ')
+        print('---------------------------')
+        print('num train samples: ', self.num_data_samples)
+        print('BATCH SIZE: ', BATCH_SIZE)
+        print('num test samples: ', tst_idxs.shape[0])
+        print('setup_pipeline: Done')
+
+    def setup_test_pipeline(self, test_data_files):
+        self.test_data_files = test_data_files
+        tst_idxs = np.arange(len(test_data_files))
+        self.get_test_dataset(tst_idxs)
+        print('setup_test_pipeline: Done')
+
+    def setup_eval_pipeline(self, filename):
+        idxs = [0]
+        self.num_data_samples = idxs.shape[0]
+
+        self.get_evaluate_dataset(idxs)
+
+    def build_srcnn(self, do_drop_out=False, do_batch_norm=False, drop_rate=0.5, factor=2):
+        print('build_cnn')
+        padding = "SAME"
+
+        # activation = tf.nn.relu
+        # activation = tf.nn.elu
+        activation = tf.nn.relu
+        momentum = 0.99
+
+        num_filters = 64
+
+        input_2d = self.inputs[0]
+        print('input: ', input_2d.shape)
+        # conv = tf.keras.layers.Conv2D(num_filters, kernel_size=5, strides=1, padding='VALID', activation=None)(input_2d)
+        conv = input_2d
+        print('input: ', conv.shape)
+
+        # conv = conv_b = tf.keras.layers.Conv2D(num_filters, kernel_size=3, padding=padding)(input_2d)
+        conv = conv_b = tf.keras.layers.Conv2D(num_filters, kernel_size=3, kernel_initializer='he_uniform', activation=activation, padding='SAME')(input_2d)
+        print(conv.shape)
+
+        if NOISE_TRAINING:
+            conv = conv_b = tf.keras.layers.GaussianNoise(stddev=NOISE_STDDEV)(conv)
+
+        scale = 0.2
+
+        conv_b = build_residual_conv2d_block(conv_b, num_filters, 'Residual_Block_1', scale=scale)
+
+        conv_b = build_residual_conv2d_block(conv_b, num_filters, 'Residual_Block_2', scale=scale)
+
+        conv_b = build_residual_conv2d_block(conv_b, num_filters, 'Residual_Block_3', scale=scale)
+
+        conv_b = build_residual_conv2d_block(conv_b, num_filters, 'Residual_Block_4', scale=scale)
+
+        conv_b = build_residual_conv2d_block(conv_b, num_filters, 'Residual_Block_5', scale=scale)
+
+        conv_b = tf.keras.layers.Conv2D(num_filters, kernel_size=3, strides=1, kernel_initializer='he_uniform', padding=padding)(conv_b)
+
+        conv = conv + conv_b
+        print(conv.shape)
+
+        self.logits = tf.keras.layers.Conv2D(1, kernel_size=3, strides=1, padding=padding, name='regression')(conv)
+
+        print(self.logits.shape)
+
+    def build_training(self):
+        # if NumClasses == 2:
+        #     self.loss = tf.keras.losses.BinaryCrossentropy(from_logits=False)  # for two-class only
+        # else:
+        #     self.loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)  # For multi-class
+        self.loss = tf.keras.losses.MeanSquaredError()  # Regression
+
+        # decayed_learning_rate = learning_rate * decay_rate ^ (global_step / decay_steps)
+        initial_learning_rate = 0.002
+        decay_rate = 0.95
+        steps_per_epoch = int(self.num_data_samples/BATCH_SIZE)  # one epoch
+        decay_steps = int(steps_per_epoch / 2)
+        print('initial rate, decay rate, steps/epoch, decay steps: ', initial_learning_rate, decay_rate, steps_per_epoch, decay_steps)
+
+        self.learningRateSchedule = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate, decay_steps, decay_rate)
+
+        optimizer = tf.keras.optimizers.Adam(learning_rate=self.learningRateSchedule)
+
+        if TRACK_MOVING_AVERAGE:
+            # Not really sure this works properly (from tfa)
+            # optimizer = tfa.optimizers.MovingAverage(optimizer)
+            self.ema = tf.train.ExponentialMovingAverage(decay=0.9999)
+
+        self.optimizer = optimizer
+        self.initial_learning_rate = initial_learning_rate
+
+    def build_evaluation(self):
+        self.train_accuracy = tf.keras.metrics.MeanAbsoluteError(name='train_accuracy')
+        self.test_accuracy = tf.keras.metrics.MeanAbsoluteError(name='test_accuracy')
+        self.train_loss = tf.keras.metrics.Mean(name='train_loss')
+        self.test_loss = tf.keras.metrics.Mean(name='test_loss')
+
+    @tf.function
+    def train_step(self, mini_batch):
+        inputs = [mini_batch[0]]
+        labels = mini_batch[1]
+        with tf.GradientTape() as tape:
+            pred = self.model(inputs, training=True)
+            loss = self.loss(labels, pred)
+            total_loss = loss
+            if len(self.model.losses) > 0:
+                reg_loss = tf.math.add_n(self.model.losses)
+                total_loss = loss + reg_loss
+        gradients = tape.gradient(total_loss, self.model.trainable_variables)
+        self.optimizer.apply_gradients(zip(gradients, self.model.trainable_variables))
+        if TRACK_MOVING_AVERAGE:
+            self.ema.apply(self.model.trainable_variables)
+
+        self.train_loss(loss)
+        self.train_accuracy(labels, pred)
+
+        return loss
+
+    @tf.function
+    def test_step(self, mini_batch):
+        inputs = [mini_batch[0]]
+        labels = mini_batch[1]
+        pred = self.model(inputs, training=False)
+        t_loss = self.loss(labels, pred)
+
+        self.test_loss(t_loss)
+        self.test_accuracy(labels, pred)
+
+    def predict(self, mini_batch):
+        inputs = [mini_batch[0]]
+        labels = mini_batch[1]
+        pred = self.model(inputs, training=False)
+        t_loss = self.loss(labels, pred)
+
+        self.test_labels.append(labels)
+        self.test_preds.append(pred.numpy())
+
+        self.test_loss(t_loss)
+        self.test_accuracy(labels, pred)
+
+    def reset_test_metrics(self):
+        self.test_loss.reset_states()
+        self.test_accuracy.reset_states()
+
+    def get_metrics(self):
+        recall = self.test_recall.result()
+        precsn = self.test_precision.result()
+        f1 = 2 * (precsn * recall) / (precsn + recall)
+
+        tn = self.test_true_neg.result()
+        tp = self.test_true_pos.result()
+        fn = self.test_false_neg.result()
+        fp = self.test_false_pos.result()
+
+        mcc = ((tp * tn) - (fp * fn)) / np.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))
+        return f1, mcc
+
+    def do_training(self, ckpt_dir=None):
+
+        if ckpt_dir is None:
+            if not os.path.exists(modeldir):
+                os.mkdir(modeldir)
+            ckpt = tf.train.Checkpoint(step=tf.Variable(1), model=self.model)
+            ckpt_manager = tf.train.CheckpointManager(ckpt, modeldir, max_to_keep=3)
+        else:
+            ckpt = tf.train.Checkpoint(step=tf.Variable(1), model=self.model)
+            ckpt_manager = tf.train.CheckpointManager(ckpt, ckpt_dir, max_to_keep=3)
+
+        self.writer_train = tf.summary.create_file_writer(os.path.join(logdir, 'plot_train'))
+        self.writer_valid = tf.summary.create_file_writer(os.path.join(logdir, 'plot_valid'))
+        self.writer_train_valid_loss = tf.summary.create_file_writer(os.path.join(logdir, 'plot_train_valid_loss'))
+
+        step = 0
+        total_time = 0
+        best_test_loss = np.finfo(dtype=np.float).max
+
+        if EARLY_STOP:
+            es = EarlyStop()
+
+        for epoch in range(NUM_EPOCHS):
+            self.train_loss.reset_states()
+            self.train_accuracy.reset_states()
+
+            t0 = datetime.datetime.now().timestamp()
+
+            proc_batch_cnt = 0
+            n_samples = 0
+
+            for data, label in self.train_dataset:
+                trn_ds = tf.data.Dataset.from_tensor_slices((data, label))
+                trn_ds = trn_ds.batch(BATCH_SIZE)
+                for mini_batch in trn_ds:
+                    if self.learningRateSchedule is not None:
+                        loss = self.train_step(mini_batch)
+
+                    if (step % 100) == 0:
+
+                        with self.writer_train.as_default():
+                            tf.summary.scalar('loss_trn', loss.numpy(), step=step)
+                            tf.summary.scalar('learning_rate', self.optimizer._decayed_lr('float32').numpy(), step=step)
+                            tf.summary.scalar('num_train_steps', step, step=step)
+                            tf.summary.scalar('num_epochs', epoch, step=step)
+
+                        self.reset_test_metrics()
+                        for data_tst, label_tst in self.test_dataset:
+                            tst_ds = tf.data.Dataset.from_tensor_slices((data_tst, label_tst))
+                            tst_ds = tst_ds.batch(BATCH_SIZE)
+                            for mini_batch_test in tst_ds:
+                                self.test_step(mini_batch_test)
+
+                        # if NumClasses == 2:
+                        #     f1, mcc = self.get_metrics()
+
+                        with self.writer_valid.as_default():
+                            tf.summary.scalar('loss_val', self.test_loss.result(), step=step)
+                            tf.summary.scalar('acc_val', self.test_accuracy.result(), step=step)
+
+                        with self.writer_train_valid_loss.as_default():
+                            tf.summary.scalar('loss_trn', loss.numpy(), step=step)
+                            tf.summary.scalar('loss_val', self.test_loss.result(), step=step)
+
+                        print('****** test loss, acc, lr: ', self.test_loss.result().numpy(), self.test_accuracy.result().numpy(),
+                              self.optimizer._decayed_lr('float32').numpy())
+
+                    step += 1
+                    print('train loss: ', loss.numpy())
+
+                proc_batch_cnt += 1
+                n_samples += data.shape[0]
+                print('proc_batch_cnt: ', proc_batch_cnt, n_samples)
+
+            t1 = datetime.datetime.now().timestamp()
+            print('End of Epoch: ', epoch+1, 'elapsed time: ', (t1-t0))
+            total_time += (t1-t0)
+
+            self.reset_test_metrics()
+            for data, label in self.test_dataset:
+                ds = tf.data.Dataset.from_tensor_slices((data, label))
+                ds = ds.batch(BATCH_SIZE)
+                for mini_batch in ds:
+                    self.test_step(mini_batch)
+
+            print('loss, acc: ', self.test_loss.result().numpy(), self.test_accuracy.result().numpy())
+            print('------------------------------------------------------')
+
+            tst_loss = self.test_loss.result().numpy()
+            if tst_loss < best_test_loss:
+                best_test_loss = tst_loss
+                ckpt_manager.save()
+
+            if EARLY_STOP and es.check_stop(tst_loss):
+                break
+
+        print('total time: ', total_time)
+        self.writer_train.close()
+        self.writer_valid.close()
+        self.writer_train_valid_loss.close()
+
+        # f = open(home_dir+'/best_stats_'+now+'.pkl', 'wb')
+        # pickle.dump((best_test_loss, best_test_acc, best_test_recall, best_test_precision, best_test_auc, best_test_f1, best_test_mcc), f)
+        # f.close()
+
+    def build_model(self):
+        self.build_srcnn()
+        self.model = tf.keras.Model(self.inputs, self.logits)
+
+    def restore(self, ckpt_dir):
+
+        ckpt = tf.train.Checkpoint(step=tf.Variable(1), model=self.model)
+        ckpt_manager = tf.train.CheckpointManager(ckpt, ckpt_dir, max_to_keep=3)
+
+        ckpt.restore(ckpt_manager.latest_checkpoint)
+
+        self.reset_test_metrics()
+
+        for data, label in self.test_dataset:
+            ds = tf.data.Dataset.from_tensor_slices((data, label))
+            ds = ds.batch(BATCH_SIZE)
+            for mini_batch_test in ds:
+                self.predict(mini_batch_test)
+
+        print('loss, acc: ', self.test_loss.result().numpy(), self.test_accuracy.result().numpy())
+
+    def do_evaluate(self, nda_lr, param, ckpt_dir):
+
+        ckpt = tf.train.Checkpoint(step=tf.Variable(1), model=self.model)
+        ckpt_manager = tf.train.CheckpointManager(ckpt, ckpt_dir, max_to_keep=3)
+
+        ckpt.restore(ckpt_manager.latest_checkpoint)
+
+        data = normalize(nda_lr, param, mean_std_dct)
+        data = np.expand_dims(data, axis=0)
+        data = np.expand_dims(data, axis=3)
+
+        self.reset_test_metrics()
+
+        pred = self.model([data], training=False)
+        self.test_probs = pred
+        pred = pred.numpy()
+
+        return denormalize(pred, param, mean_std_dct)
+
+    def run(self, directory):
+        train_data_files = glob.glob(directory+'data_train*.npy')
+        valid_data_files = glob.glob(directory+'data_valid*.npy')
+
+        train_data_files.sort()
+        valid_data_files.sort()
+
+        self.setup_pipeline(train_data_files, valid_data_files, 100000)
+        self.build_model()
+        self.build_training()
+        self.build_evaluation()
+        self.do_training()
+
+    def run_restore(self, directory, ckpt_dir):
+        valid_data_files = glob.glob(directory + 'data_valid*.npy')
+        self.num_data_samples = 1000
+        self.setup_test_pipeline(valid_data_files)
+        self.build_model()
+        self.build_training()
+        self.build_evaluation()
+        self.restore(ckpt_dir)
+
+    def run_evaluate(self, nda_lr, param, ckpt_dir):
+        self.num_data_samples = 80000
+        self.build_model()
+        self.build_training()
+        self.build_evaluation()
+        return self.do_evaluate(nda_lr, param, ckpt_dir)
+
+
+def prepare(param_idx=1, filename='/Users/tomrink/data_valid_40.npy'):
+    nda = np.load(filename)
+    #nda = nda[:, param_idx, :, :]
+    nda_lr = nda[:, param_idx, x_134_2, y_134_2]
+    # nda_lr = resample(x_70, y_70, nda, x_70_2, y_70_2)
+    nda_lr = np.expand_dims(nda_lr, axis=3)
+    return nda_lr
+
+
+def run_evaluate_static(in_file, out_file, ckpt_dir):
+    nda = np.load(in_file)
+
+    nda = nda[:, data_idx, 3:131:2, 3:131:2]
+    nda = resample(y_64, x_64, nda, s, t)
+    nda = np.expand_dims(nda, axis=3)
+
+    nn = SRCNN()
+    out_sr = nn.run_evaluate(nda, data_param, ckpt_dir)
+    if out_file is not None:
+        np.save(out_file, out_sr)
+    else:
+        return out_sr
+
+
+def run_evaluate_static_new(in_file, out_file, ckpt_dir):
+    h5f = h5py.File(in_file, 'r')
+    grd = get_grid_values_all(h5f, data_param)
+    leny, lenx = grd.shape
+    x = np.arange(lenx)
+    y = np.arange(leny)
+    x_up = np.arange(0, lenx, 0.5)
+    y_up = np.arange(0, leny, 0.5)
+
+    grd = resample_one(y, x, grd, y_up, x_up)
+
+    nn = SRCNN()
+    out_sr = nn.run_evaluate(grd, data_param, ckpt_dir)
+    if out_file is not None:
+        np.save(out_file, out_sr)
+    else:
+        return out_sr
+
+
+if __name__ == "__main__":
+    nn = SRCNN()
+    nn.run('matchup_filename')
-- 
GitLab