def raw_test_data(): image_path = full_path("data/orig/test/*.png") num_images = len(glob.glob(image_path)) # glob does not return image files in correct order!!!!! image_files = [full_path("data/orig/test/frame_{}.png".format(i)) for i in range(num_images)] return image_files
def main(_): full_video_file = full_path("video_data/{}.mp4".format(FLAGS.video_file)) vid = skvideo.io.vreader(full_video_file) folder_path = full_path("data/orig") image_folder_path = "{}/{}".format(folder_path, FLAGS.video_file) if os.path.exists(folder_path) == False: os.makedirs(folder_path) if os.path.exists(image_folder_path) == False: os.makedirs(image_folder_path) print("Preparing to process", full_video_file) for i, image in enumerate(vid): file_out_path = "{}/frame_{}.png".format(image_folder_path, i) cv2.imwrite(file_out_path, cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # save frame as JPEG file if i % 100 == 0: print("Done saving image", file_out_path) if FLAGS.frame_count is not None and i >= FLAGS.frame_count: break print("Done processing images.")
def raw_train_data(): image_path = full_path("data/orig/train/*.png") num_images = len(glob.glob(image_path)) # glob does not return image files in correct order!!!!! image_files = [full_path("data/orig/train/frame_{}.png".format(i)) for i in range(num_images)] label_path = "video_data/train.txt" train_labels = read_txt_file(label_path)[0:num_images] return (image_files, train_labels)
def main(_): config = Config(FLAGS.folder, FLAGS.max_epochs, FLAGS.batch_size, FLAGS.min_delta, FLAGS.patience) print("Training model named", config.model_name()) if FLAGS.video_file == 'train': video_folder = 'train_full' elif FLAGS.video_file == 'test': video_folder = 'test' else: raise ValueError("Unexpected video file {}".format(FLAGS.video_file)) folder_path = full_path("data/{}/{}".format(FLAGS.folder, video_folder)) steps, generator = create_mobilenet_full_generator(folder_path, FLAGS.debug) model = load_model(config.model_checkpoint()) print("Compiling model.") model.compile('sgd', 'mse') print("Starting model prediction process.") predictions = model.predict_generator(generator, steps, verbose=1) # b/c the first `num_images` predictions are skipped, add these back based on the first value predictions = np.concatenate((np.ones(FLAGS.num_images-1)*predictions[0][0], predictions.reshape(-1))) if FLAGS.video_file == 'train': _, labels = raw_train_data() labels = labels[:len(predictions)] print("MSE:", np.mean(np.square(predictions - labels))) file = "results_{}.txt".format(FLAGS.video_file) print("Saving predictions to {}".format(file)) np.savetxt(full_path(file), predictions, fmt='%.5f')
def folder_path(self): if self.is_recurrent: return full_path("data/optical_flow_recurrent") else: return full_path("data/optical_flow_{}_augmented_{}".format( self.num_images, self.num_augmentations))
def read_txt_file(file_path): with open(full_path(file_path), "r") as f: return np.array(list(map(float, f)))
def main(_): config = Config(FLAGS.folder, FLAGS.max_epochs, FLAGS.batch_size, FLAGS.min_delta, FLAGS.patience, FLAGS.alpha, FLAGS.is_recurrent) print("Training model named", config.model_name()) folder_path = full_path("data/{}".format(FLAGS.folder)) if FLAGS.is_recurrent: # train, valid, input_shape = create_recurrent_generators(folder_path, FLAGS.batch_size, FLAGS.num_images, FLAGS.debug) train, valid, input_shape = create_orig_recurrent_generator(FLAGS.batch_size, FLAGS.num_images, FLAGS.debug) else: train, valid, input_shape = create_mobilenet_generators(folder_path, FLAGS.batch_size, FLAGS.num_images, FLAGS.debug) train_steps_per_epoch, train_generator = train valid_steps_per_epoch, valid_generator = valid print("Creating model with input", input_shape) if FLAGS.model_file: model = load_model(full_path(FLAGS.model_file)) elif FLAGS.is_recurrent: # model = recurrent_net(input_shape, FLAGS.num_images, FLAGS.alpha) model = recurrent_mobilenet(input_shape, FLAGS.num_images, FLAGS.alpha) else: # model = create_optical_flow_model(input_shape, FLAGS.alpha) # model = MobileNetSlim(input_shape, FLAGS.alpha) # model = create_mobilenet_plus_model(input_shape, FLAGS.num_images, FLAGS.alpha) # model = create_optical_flow_model(input_shape, FLAGS.num_images, FLAGS.alpha) model = create_simple_optical_flow_model(input_shape) if FLAGS.debug: print(model.summary()) callbacks = None else: callbacks = [ ModelCheckpoint(config.model_checkpoint(), verbose=FLAGS.verbose, save_best_only=True), CSVLogger(config.csv_log_file()), EarlyStopping(monitor='val_loss', min_delta=FLAGS.min_delta, patience=FLAGS.patience, verbose=1) ] print("Compiling model.") model.compile( optimizer= Adam(lr=0.0001),#, clipnorm=15.), loss={'speed': 'mean_squared_error'}, metrics=['mean_absolute_error', 'mean_squared_error']) print("Starting model train process.") model.fit_generator(train_generator, train_steps_per_epoch, epochs=FLAGS.max_epochs, verbose=FLAGS.verbose, callbacks=callbacks, validation_data=valid_generator, validation_steps=valid_steps_per_epoch) print("Finished training model.") if isAWS() and FLAGS.debug == False: upload_s3(config.model_checkpoint()) upload_s3(config.csv_log_file()) if isAWS() and FLAGS.stop: stop_instance()