def test_Unet_naive_with_batch_norm(test_images, test_flows, h, w, dataset, sequence_n_frame, clip_idx, batch_size=32, model_idx=20, using_test_data=True): print(test_images.shape, test_flows.shape, np.sum(sequence_n_frame)) assert len(test_images) == len(test_flows) assert len(test_images) == sequence_n_frame[clip_idx] test_images /= 0.5 test_images -= 1. plh_frame_true = tf.placeholder(tf.float32, shape=[None, h, w, 3]) plh_is_training = tf.placeholder(tf.bool) # generator plh_dropout_prob = tf.placeholder_with_default(1.0, shape=()) output_opt, output_appe = Generator(plh_frame_true, plh_is_training, plh_dropout_prob) saver = tf.train.Saver(max_to_keep=20) saved_out_appes = np.zeros(test_images.shape) saved_out_flows = np.zeros(test_flows.shape) with tf.Session() as sess: saved_model_file = './training_saver/%s/model_ckpt_%d.ckpt' % ( dataset['name'], model_idx) saver.restore(sess, saved_model_file) # saved_data_path = './training_saver/%s/output_%s/%d_epoch' % ( dataset['name'], 'test' if using_test_data else 'train', model_idx) if not os.path.exists(saved_data_path): pathlib.Path(saved_data_path).mkdir(parents=True, exist_ok=True) saved_data_file = '%s/output_%d.npz' % (saved_data_path, clip_idx) if os.path.isfile(saved_data_file): print('File existed! Return!') return batch_idx = np.array_split(np.arange(len(test_images)), np.ceil(len(test_images) / batch_size)) # progress = ProgressBar(len(batch_idx), fmt=ProgressBar.FULL) for j in range(len(batch_idx)): progress.current += 1 progress() saved_out_appes[batch_idx[j]], saved_out_flows[batch_idx[j]] = \ sess.run([output_appe, output_opt], feed_dict={plh_frame_true: test_images[batch_idx[j]], plh_is_training: False, plh_dropout_prob: 1.0}) saved_out_appes[ batch_idx[j]] = 0.5 * (saved_out_appes[batch_idx[j]] + 1) progress.done() np.savez_compressed(saved_data_file, image=saved_out_appes, flow=saved_out_flows)
def test_model_naive_with_batch_norm(dataset, test_cubes, cube_row_indices, cube_col_indices, n_row, n_col, sequence_n_frame, clip_idx, model_idx=20, batch_size=256, using_test_data=True): assert len(test_cubes) == len(cube_row_indices) assert len(cube_row_indices) == len(cube_col_indices) print('shape of test cubes = %s' % str(test_cubes.shape)) print(np.sum(sequence_n_frame)) h, w, d = test_cubes.shape[1:4] # score_dir = '%s/scores' % dataset['cube_dir'] if not os.path.exists(score_dir): pathlib.Path(score_dir).mkdir(parents=True, exist_ok=True) saved_data_path = '%s/output_%s/%d_epoch' % ( score_dir, 'test' if using_test_data else 'train', model_idx) if not os.path.exists(saved_data_path): pathlib.Path(saved_data_path).mkdir(parents=True, exist_ok=True) saved_data_file = '%s/output_%d.npz' % (saved_data_path, clip_idx) if os.path.isfile(saved_data_file): print('File existed! Return!') return # test_cubes /= 0.5 test_cubes -= 1. # plh_cube_true = tf.placeholder(tf.float32, shape=[None, h, w, d]) plh_row_idx = tf.placeholder(tf.uint8, shape=[None]) plh_col_idx = tf.placeholder(tf.uint8, shape=[None]) plh_is_training = tf.placeholder(tf.bool) # generator plh_dropout_prob = tf.placeholder_with_default(1.0, shape=()) cube_recon, _, out_row_softmax, _, out_col_softmax = Generator( plh_cube_true, [h, w, d], plh_is_training, plh_dropout_prob, n_row, n_col) # saver = tf.train.Saver(max_to_keep=20) # saved_out_cubes = np.zeros(test_cubes.shape) saved_out_row_softmax = np.zeros([len(test_cubes), n_row]) saved_out_col_softmax = np.zeros([len(test_cubes), n_col]) with tf.Session() as sess: saved_model_file = '%s/models/model_ckpt_%d.ckpt' % ( dataset['cube_dir'], model_idx) saver.restore(sess, saved_model_file) # batch_idx = np.array_split(np.arange(len(test_cubes)), np.ceil(len(test_cubes) / batch_size)) progress = ProgressBar(len(batch_idx), fmt=ProgressBar.FULL) start_pt = time.time() for j in range(len(batch_idx)): progress.current += 1 progress() saved_out_cubes[batch_idx[j]], saved_out_row_softmax[batch_idx[j]], saved_out_col_softmax[batch_idx[j]] = \ sess.run([cube_recon, out_row_softmax, out_col_softmax], feed_dict={plh_cube_true: test_cubes[batch_idx[j]], plh_row_idx: cube_row_indices[batch_idx[j]], plh_col_idx: cube_col_indices[batch_idx[j]], plh_is_training: False, plh_dropout_prob: 1.0}) saved_out_cubes[ batch_idx[j]] = 0.5 * (saved_out_cubes[batch_idx[j]] + 1.) print('batch size: %d => average time per batch: %f' % (batch_size, (time.time() - start_pt) / len(batch_idx))) progress.done() np.savez_compressed(saved_data_file, cube=saved_out_cubes, row=saved_out_row_softmax, col=saved_out_col_softmax)