def finetune_model(prog_path,nb_epoch,train_iters,val_iters,training_image_path,training_map_path,weight_path,output_folder,test_images,test_output,shuffle=True): #config settings imgs_train_path = training_image_path maps_train_path = training_map_path #nb_imgs_train = train_iters #imgs_val_path = training_image_path #maps_val_path = training_map_path #nb_imgs_val = val_iters #b_s, shape_r, shape_c, shape_r_gt, shape_c_gt, _= config() #start finetunings model = ml_net_model(img_cols=shape_c, img_rows=shape_r, downsampling_factor_product=10,weight_path=weight_path) datagen = ImageDataGenerator() sgd = SGD(lr=1e-4, decay=0.0005, momentum=0.9, nesterov=True) print("Compile ML-Net Model for finetuning") model.compile(sgd, loss) print("Load weights ML-Net") model.load_weights(weight_path + '/mlnet_salicon_weights.pkl') model = prepare_finetune(model,4,sgd) #prepare model timestamp = time.localtime() timestamp = prog_path + 'checkpoints/' + str(timestamp.tm_mon) + '_' + str(timestamp.tm_mday) + '_' + str(timestamp.tm_hour) + '_' + str(timestamp.tm_min) if not os.path.exists(timestamp): os.makedirs(timestamp) print('Finetuning model for ' + str(nb_epoch) + ' epochs') for ep in range(nb_epoch): ep_loss = 0 num_batches = 0 batch_estimate = int(np.ceil(len(imgs_train_path) / b_s)) if shuffle: rand_order = np.arange(len(imgs_train_path)) np.random.shuffle(rand_order) ar_images = np.asarray(imgs_train_path) ar_maps = np.asarray(maps_train_path) ar_images = ar_images[rand_order] ar_maps = ar_maps[rand_order] it_imgs_train_path = ar_images.tolist() it_maps_train_path = ar_maps.tolist() else: it_imgs_train_path = np.copy(it_imgs_train_path).tolist() it_maps_train_path = np.copy(it_maps_train_path).tolist() for X_train, Y_train in generator(b_s, it_imgs_train_path, it_maps_train_path, shape_r, shape_c, shape_r_gt, shape_c_gt): if X_train.shape[0] == 0: break else: ep_loss += model.train_on_batch(X_train, Y_train) num_batches += 1 sys.stdout.write('\r' + str(num_batches) + '/' + str(batch_estimate)) sys.stdout.flush() print('mean loss across batches', ep_loss / num_batches) model_pointer = timestamp + '/' + str(ep) + '.h5' model.save(model_pointer) #Now make predictions prediction_paths = make_predictions(model,test_images,test_output) return model_pointer, prediction_paths
if f.endswith('.jpg') ] images.sort() counter = 0 while True: yield preprocess_images(images[counter:counter + b_s], shape_r, shape_c) counter = (counter + b_s) % len(images) if __name__ == '__main__': phase = sys.argv[1] model = ml_net_model(img_cols=shape_c, img_rows=shape_r, downsampling_factor_product=10) sgd = SGD(lr=1e-3, decay=0.0005, momentum=0.9, nesterov=True) print("Compile ML-Net Model") model.compile(sgd, loss) if phase == 'train': print("Training ML-Net") model.fit_generator( generator(b_s=b_s), nb_imgs_train, nb_epoch=nb_epoch, validation_data=generator(b_s=b_s, phase_gen='val'), nb_val_samples=nb_imgs_val, callbacks=[ EarlyStopping(patience=5),
def generator_test(b_s, imgs_test_path): images = [imgs_test_path + f for f in os.listdir(imgs_test_path) if f.endswith('.jpg')] images.sort() counter = 0 while True: yield preprocess_images(images[counter:counter + b_s], shape_r, shape_c) counter = (counter + b_s) % len(images) if __name__ == '__main__': phase = sys.argv[1] model = ml_net_model(img_cols=shape_c, img_rows=shape_r, downsampling_factor_product=10) sgd = SGD(lr=1e-3, decay=0.0005, momentum=0.9, nesterov=True) print("Compile ML-Net Model") model.compile(sgd, loss) if phase == 'train': print("Training ML-Net") model.fit_generator(generator(b_s=b_s), nb_imgs_train, nb_epoch=nb_epoch, validation_data=generator(b_s=b_s, phase_gen='val'), nb_val_samples=nb_imgs_val, callbacks=[EarlyStopping(patience=5), ModelCheckpoint('weights.mlnet.{epoch:02d}-{val_loss:.4f}.pkl', save_best_only=True)]) elif phase == "test": # path of output folder output_folder = ''
verbose = True # parse arguments parser = argparse.ArgumentParser() parser.add_argument("--seq") parser.add_argument("--pred_dir") args = parser.parse_args() assert args.seq is not None, 'Please provide a correct dreyeve sequence' assert args.pred_dir is not None, 'Please provide a correct pred_dir' dreyeve_dir = 'Z:/DATA' # local # dreyeve_dir = '/gpfs/work/IscrC_DeepVD/dabati/DREYEVE/data/' # cineca # get the model model = ml_net_model(img_rows=shape_r, img_cols=shape_c) model.compile(optimizer='adam', loss='kld') # do we need this? model.load_weights('weights.mlnet.07-0.0193.pkl') # load weights # set up some directories pred_dir = join(args.pred_dir, '{:02d}'.format(int(args.seq)), 'output') makedirs([pred_dir]) sequence_dir = join(dreyeve_dir, '{:02d}'.format(int(args.seq))) for sample in tqdm(range(15, 7500 - 1)): X = load_dreyeve_sample(sequence_dir=sequence_dir, sample=sample, shape_c=shape_c, shape_r=shape_r) # predict sample
def load_dreyeve_sample(sequence_dir, sample, shape_r, shape_c): filename = join(sequence_dir, 'frames', '{}.jpg'.format(sample)) X = preprocess_images([filename], shape_r, shape_c) return X if __name__ == '__main__': shape_r = 480 shape_c = 640 # get the model model_bdda = ml_net_model(img_rows=shape_r, img_cols=shape_c) model_bdda.compile(optimizer='adam', loss='kld') model_sage = ml_net_model(img_rows=shape_r, img_cols=shape_c) model_sage.compile(optimizer='adam', loss='kld') model_path_bdda = "../pretrained_models/mlnet/weights.mlnet.bdda.pkl" model_path_sage = "../pretrained_models/mlnet/weights.mlnet.sage.pkl" model_bdda.load_weights(model_path_bdda) model_sage.load_weights(model_path_sage) demo_dir = 'demo_images/' X = load_dreyeve_sample(sequence_dir=demo_dir, sample=16, shape_c=shape_c,