From 2e9c78ae08ffee92d956a8ee09cb03c4bb6bbaf2 Mon Sep 17 00:00:00 2001 From: gong1 <b.gong@fz-juelich.de> Date: Fri, 28 Aug 2020 10:22:39 +0200 Subject: [PATCH] Add new dataset moving mnist for preprocessing, revise the generate to correct the images shape --- .../HPC_scripts/train_movingmnist.sh | 12 ++++----- .../generate_transfer_learning_finetune.py | 25 ++++++++----------- .../video_prediction/datasets/kth_dataset.py | 6 ++--- .../video_prediction/datasets/moving_mnist.py | 8 +++--- .../video_prediction/layers/layer_def.py | 4 +-- .../video_prediction/models/__init__.py | 2 +- .../video_prediction/models/base_model.py | 1 + .../video_prediction/models/savp_model.py | 4 +++ .../models/vanilla_convLSTM_model.py | 2 -- 9 files changed, 32 insertions(+), 32 deletions(-) diff --git a/video_prediction_savp/HPC_scripts/train_movingmnist.sh b/video_prediction_savp/HPC_scripts/train_movingmnist.sh index 9f0ea378..68b107e2 100755 --- a/video_prediction_savp/HPC_scripts/train_movingmnist.sh +++ b/video_prediction_savp/HPC_scripts/train_movingmnist.sh @@ -6,9 +6,9 @@ #SBATCH --cpus-per-task=1 #SBATCH --output=train_era5-out.%j #SBATCH --error=train_era5-err.%j -#SBATCH --time=00:20:00 -#SBATCH --gres=gpu:2 -#SBATCH --partition=develgpus +#SBATCH --time=23:20:00 +#SBATCH --gres=gpu:1 +#SBATCH --partition=gpus #SBATCH --mail-type=ALL #SBATCH --mail-user=b.gong@fz-juelich.de ##jutil env activate -p cjjsc42 @@ -31,14 +31,12 @@ if [ -z ${VIRTUAL_ENV} ]; then fi - - # declare directory-variables which will be modified appropriately during Preprocessing (invoked by mpi_split_data_multi_years.py) source_dir=/p/project/deepacf/deeprain/video_prediction_shared_folder/preprocessedData/moving_mnist -destination_dir=/p/project/deepacf/deeprain/video_prediction_shared_folder/models/moving_mnist +destination_dir=/p/project/deepacf/deeprain/video_prediction_shared_folder/models/moving_mnist_test -# for choosing the model, convLSTM,savp, mcnet,vae +# for choosing the model, convLSTM,savp, mcnet,vae,convLSTM_Loliver model=convLSTM model_hparams=../hparams/era5/${model}/model_hparams.json diff --git a/video_prediction_savp/scripts/generate_transfer_learning_finetune.py b/video_prediction_savp/scripts/generate_transfer_learning_finetune.py index 0e250b47..7b0c462c 100644 --- a/video_prediction_savp/scripts/generate_transfer_learning_finetune.py +++ b/video_prediction_savp/scripts/generate_transfer_learning_finetune.py @@ -202,11 +202,6 @@ def save_to_netcdf_per_sequence(output_dir,input_images_,gen_images_,lons,lats,t ts_len = len(ts) ts_input = ts[:context_frames] ts_forecast = ts[context_frames:] - #print("context_frame:",context_frames) - #print("future_frame",future_length) - #print("length of ts input:",len(ts_input)) - - print("input_images_ shape in netcdf,",input_images_.shape) gen_images_ = np.array(gen_images_) output_file = os.path.join(output_dir,fl_name) @@ -289,18 +284,19 @@ def save_to_netcdf_per_sequence(output_dir,input_images_,gen_images_,lons,lats,t #Temperature: t2 = nc_file.createVariable("/forecast/{}/T2".format(model_name),"f4",("time_forecast","lat","lon"), zlib = True) t2.units = 'K' - t2[:,:,:] = gen_images_[context_frames:,:,:,0] + print ("gen_images_ 20200822:",np.array(gen_images_).shape) + t2[:,:,:] = gen_images_[context_frames-1:,:,:,0] print("NetCDF created") #mean sea level pressure msl = nc_file.createVariable("/forecast/{}/MSL".format(model_name),"f4",("time_forecast","lat","lon"), zlib = True) msl.units = 'Pa' - msl[:,:,:] = gen_images_[context_frames:,:,:,1] + msl[:,:,:] = gen_images_[context_frames-1:,:,:,1] #Geopotential at 500 gph500 = nc_file.createVariable("/forecast/{}/GPH500".format(model_name),"f4",("time_forecast","lat","lon"), zlib = True) gph500.units = 'm' - gph500[:,:,:] = gen_images_[context_frames:,:,:,2] + gph500[:,:,:] = gen_images_[context_frames-1:,:,:,2] print("{} created".format(output_file)) @@ -451,7 +447,8 @@ def main(): #Get prediction values feed_dict = {input_ph: input_results[name] for name, input_ph in input_phs.items()} gen_images = sess.run(model.outputs['gen_images'], feed_dict = feed_dict)#return [batchsize,seq_len,lat,lon,channel] - + assert gen_images.shape[1] = sequence_length-1 #The generate images seq_len should be sequence_len -1, since the last one is not used for comparing with groud truth + print("gen_images 20200822:",np.array(gen_images).shape) #Loop in batch size for i in range(args.batch_size): @@ -459,26 +456,26 @@ def main(): input_images_,t_start = get_one_seq_and_time(input_images,t_starts,i) #generate time stamps for sequences ts = generate_seq_timestamps(t_start,len_seq=sequence_length) - + #Renormalized data for inputs stat_fl = os.path.join(args.input_dir,"pickle/statistics.json") input_images_denorm = denorm_images_all_channels(stat_fl,input_images_,["T2","MSL","gph500"]) - print("input_images_denorm",input_images_denorm[0][0]) + print("input_images_denorm shape",np.array(input_images_denorm).shape) #Renormalized data for inputs gen_images_ = gen_images[i] gen_images_denorm = denorm_images_all_channels(stat_fl,gen_images_,["T2","MSL","gph500"]) - print("gene_images_denorm:",gen_images_denorm[0][0]) + print("gene_images_denorm shape",np.array(gen_images_denorm).shape) #Save input to netCDF file init_date_str = ts[0].strftime("%Y%m%d%H") save_to_netcdf_per_sequence(args.results_dir,input_images_denorm,gen_images_denorm,lons,lats,ts,context_frames,future_length,args.model,fl_name="vfp_{}.nc".format(init_date_str)) #Generate images inputs - plot_seq_imgs(imgs=input_images_denorm[:context_frames-1,:,:,0],lats=lats,lons=lons,ts=ts[:context_frames-1],label="Ground Truth",output_png_dir=args.results_dir) + plot_seq_imgs(imgs=input_images_denorm[context_frames+1:,:,:,0],lats=lats,lons=lons,ts=ts[context_frames+1:],label="Ground Truth",output_png_dir=args.results_dir) #Generate forecast images - plot_seq_imgs(imgs=gen_images_denorm[context_frames:,:,:,0],lats=lats,lons=lons,ts=ts[context_frames:],label="Forecast by Model " + args.model,output_png_dir=args.results_dir) + plot_seq_imgs(imgs=gen_images_denorm[context_frames:,:,:,0],lats=lats,lons=lons,ts=ts[context_frames+1:],label="Forecast by Model " + args.model,output_png_dir=args.results_dir) #TODO: Scaret plot persistence image #implment get_persistence() function diff --git a/video_prediction_savp/video_prediction/datasets/kth_dataset.py b/video_prediction_savp/video_prediction/datasets/kth_dataset.py index 40fb6bf5..e1e11d51 100644 --- a/video_prediction_savp/video_prediction/datasets/kth_dataset.py +++ b/video_prediction_savp/video_prediction/datasets/kth_dataset.py @@ -136,11 +136,11 @@ def read_frames_and_save_tf_records(output_dir, video_dirs, image_size, sequence def main(): parser = argparse.ArgumentParser() - parser.add_argument("--input_dir", type=str, help="directory containing the processed directories " + parser.add_argument("input_dir", type=str, help="directory containing the processed directories " "boxing, handclapping, handwaving, " "jogging, running, walking") - parser.add_argument("--output_dir", type=str) - parser.add_argument("--image_size", type=int) + parser.add_argument("output_dir", type=str) + parser.add_argument("image_size", type=int) args = parser.parse_args() partition_names = ['train', 'val', 'test'] diff --git a/video_prediction_savp/video_prediction/datasets/moving_mnist.py b/video_prediction_savp/video_prediction/datasets/moving_mnist.py index 8966e1af..12a517ac 100644 --- a/video_prediction_savp/video_prediction/datasets/moving_mnist.py +++ b/video_prediction_savp/video_prediction/datasets/moving_mnist.py @@ -207,7 +207,6 @@ def plot_seq_imgs(imgs,output_png_dir,idx,label="Ground Truth"): - def main(): parser = argparse.ArgumentParser() parser.add_argument("input_dir", type=str, help="directory containing the processed directories ""boxing, handclapping, handwaving, ""jogging, running, walking") @@ -221,12 +220,15 @@ def main(): height = data.shape[2] width = data.shape[3] num_samples = data.shape[1] - + max_npz = np.max(data) + min_npz = np.min(data) + print("max_npz,",max_npz) + print("min_npz",min_npz) #Todo need to discuss how to split the data, since we have totally 10000 samples, the origin paper convLSTM used 10000 as training, 2000 as validation and 3000 for testing dat_train = data[:,:6000,:,:] dat_val = data[:,6000:7000,:,:] dat_test = data[:,7000:,:] - plot_seq_imgs(dat_test[10:,0,:,:],output_png_dir="/p/project/deepacf/deeprain/video_prediction_shared_folder/results/moving_mnist/convLSTM",idx=1,label="Ground Truth from npz") + #plot_seq_imgs(dat_test[10:,0,:,:],output_png_dir="/p/project/deepacf/deeprain/video_prediction_shared_folder/results/moving_mnist/convLSTM",idx=1,label="Ground Truth from npz") #save train #read_frames_and_save_tf_records(os.path.join(args.output_dir,"train"),dat_train, seq_length=20, sequences_per_file=40, height=height, width=width) #save val diff --git a/video_prediction_savp/video_prediction/layers/layer_def.py b/video_prediction_savp/video_prediction/layers/layer_def.py index a59643c7..738e139d 100644 --- a/video_prediction_savp/video_prediction/layers/layer_def.py +++ b/video_prediction_savp/video_prediction/layers/layer_def.py @@ -3,8 +3,8 @@ import tensorflow as tf import numpy as np - weight_decay = 0.0005 + def _activation_summary(x): """Helper to create summaries for activations. Creates a summary that provides a histogram of activations. @@ -157,4 +157,4 @@ def bn_layers(inputs,idx,is_training=True,epsilon=1e-3,decay=0.99,reuse=None): def bn_layers_wrapper(inputs, is_training): pass - \ No newline at end of file + diff --git a/video_prediction_savp/video_prediction/models/__init__.py b/video_prediction_savp/video_prediction/models/__init__.py index 6d7323f3..b71769a9 100644 --- a/video_prediction_savp/video_prediction/models/__init__.py +++ b/video_prediction_savp/video_prediction/models/__init__.py @@ -21,7 +21,7 @@ def get_model_class(model): 'vae': 'VanillaVAEVideoPredictionModel', 'convLSTM': 'VanillaConvLstmVideoPredictionModel', 'mcnet': 'McNetVideoPredictionModel', - + 'convLSTM_Loliver': "ConvLstmLoliverVideoPredictionModel" } model_class = model_mappings.get(model, model) model_class = globals().get(model_class) diff --git a/video_prediction_savp/video_prediction/models/base_model.py b/video_prediction_savp/video_prediction/models/base_model.py index 846621d8..df479968 100644 --- a/video_prediction_savp/video_prediction/models/base_model.py +++ b/video_prediction_savp/video_prediction/models/base_model.py @@ -487,6 +487,7 @@ class VideoPredictionModel(BaseVideoPredictionModel): # skip_vars = {" discriminator_encoder/video_sn_fc4/dense/bias"} if self.num_gpus <= 1: # cpu or 1 gpu + print("self.inputs:>20200822",self.inputs) outputs_tuple, losses_tuple, loss_tuple, metrics_tuple = self.tower_fn(self.inputs) self.outputs, self.eval_outputs = outputs_tuple self.d_losses, self.g_losses, g_losses_post = losses_tuple diff --git a/video_prediction_savp/video_prediction/models/savp_model.py b/video_prediction_savp/video_prediction/models/savp_model.py index c510d050..03953386 100644 --- a/video_prediction_savp/video_prediction/models/savp_model.py +++ b/video_prediction_savp/video_prediction/models/savp_model.py @@ -689,6 +689,10 @@ class SAVPCell(tf.nn.rnn_cell.RNNCell): def generator_given_z_fn(inputs, mode, hparams): # all the inputs needs to have the same length for unrolling the rnn print("inputs.items",inputs.items()) + #20200822 bing + inputs ={"images":inputs["images"]} + print("inputs 20200822:",inputs) + #20200822 inputs = {name: tf_utils.maybe_pad_or_slice(input, hparams.sequence_length - 1) for name, input in inputs.items()} cell = SAVPCell(inputs, mode, hparams) diff --git a/video_prediction_savp/video_prediction/models/vanilla_convLSTM_model.py b/video_prediction_savp/video_prediction/models/vanilla_convLSTM_model.py index ee0b0c0b..f64ce663 100644 --- a/video_prediction_savp/video_prediction/models/vanilla_convLSTM_model.py +++ b/video_prediction_savp/video_prediction/models/vanilla_convLSTM_model.py @@ -89,9 +89,7 @@ class VanillaConvLstmVideoPredictionModel(BaseVideoPredictionModel): def convLSTM_cell(inputs, hidden): channels = inputs.get_shape()[-1] conv1 = ld.conv_layer(inputs, 3, 2, 8, "encode_1", activate = "leaky_relu") - conv2 = ld.conv_layer(conv1, 3, 1, 8, "encode_2", activate = "leaky_relu") - conv3 = ld.conv_layer(conv2, 3, 2, 8, "encode_3", activate = "leaky_relu") y_0 = conv3 -- GitLab