def train(n_classes=11, batch_size=16, epochs=100, width=960, height=720, crop_factor_x=2, crop_factor_y=1.25, init_lr=1e-4, median_frequency=.15, zoom_augmentation=.2, dataset_path='datasets/endoscopy', weights_path='weights/endoscopy/model', preprocess='imagenet'): CONFIG = {} CONFIG['n_classes'] = n_classes CONFIG['batch_size'] = batch_size CONFIG['epochs'] = epochs CONFIG['width'] = width CONFIG['height'] = height CONFIG['crop_factor_x'] = crop_factor_x CONFIG['crop_factor_y'] = crop_factor_y CONFIG['width_train'] = int( CONFIG['width'] / CONFIG['crop_factor_x']) # will be cropped from width_test size CONFIG['height_train'] = int( CONFIG['height'] / CONFIG['crop_factor_y']) # will be cropped from height_test size CONFIG['init_lr'] = init_lr CONFIG['median_frequency'] = median_frequency CONFIG['zoom_augmentation'] = zoom_augmentation CONFIG['dataset_path'] = dataset_path CONFIG['weights_path'] = weights_path CONFIG['preprocess'] = preprocess assert CONFIG['width'] * ( 1 - CONFIG['zoom_augmentation']) >= CONFIG['width_train'] assert CONFIG['height'] * ( 1 - CONFIG['zoom_augmentation']) >= CONFIG['height_train'] # GPU to use n_gpu = 0 os.environ["CUDA_VISIBLE_DEVICES"] = str(n_gpu) # Loader loader = Loader.Loader(dataFolderPath=CONFIG['dataset_path'], n_classes=CONFIG['n_classes'], width=CONFIG['width'], height=CONFIG['height'], median_frequency=CONFIG['median_frequency']) print('Dataset loaded...') # build model #model = MiniNetv2.MiniNetv2p(num_classes=CONFIG['n_classes']) model = ResNet50.ResNet50Seg(CONFIG['n_classes'], input_shape=(None, None, 3), weights='imagenet') # optimizer learning_rate = tf.Variable(CONFIG['init_lr']) optimizer = tf.keras.optimizers.Adam(learning_rate) loss_function = tf.keras.losses.CategoricalCrossentropy() # restore if model saved and show number of params restore_state(model, CONFIG['weights_path']) init_model(model, (1, CONFIG['width'], CONFIG['height'], 3)) get_params(model) # Train print('Training...') _train(loader=loader, optimizer=optimizer, loss_function=loss_function, model=model, config=CONFIG, lr=learning_rate, name_best_model=CONFIG['weights_path'], evaluation=True, preprocess_mode=CONFIG['preprocess']) print('Testing model') test_acc, test_miou = get_metrics(loader, model, loader.n_classes, train=False, flip_inference=True, scales=[1, 2, 1.5, 0.5, 0.75], write_images=True, preprocess_mode=CONFIG['preprocess'], time_exect=True) print('Test accuracy: ' + str(test_acc.numpy())) print('Test miou: ' + str(test_miou.numpy()))
model = Segception.Efficient(num_classes=n_classes, weights='imagenet', input_shape=(None, None, channels)) # optimizer learning_rate = tfe.Variable(lr) optimizer = tf.train.AdamOptimizer(learning_rate) # Init models (optional, just for get_params function) init_model(model, input_shape=(batch_size, width, height, channels)) variables_to_restore = model.variables variables_to_save = model.variables variables_to_optimize = model.variables # Init saver. can use also ckpt = tfe.Checkpoint((model=model, optimizer=optimizer,learning_rate=learning_rate, global_step=global_step) saver_model = tfe.Saver(var_list=variables_to_save) restore_model = tfe.Saver(var_list=variables_to_restore) # restore if model saved and show number of params restore_state(restore_model, name_best_model) get_params(model) train(loader=loader, optimizer=optimizer, model=model, epochs=epochs, batch_size=batch_size, augmenter='segmentation', lr=learning_rate, init_lr=lr, saver=saver_model, variables_to_optimize=variables_to_optimize, name_best_model=name_best_model, evaluation=True, aux_loss=False, preprocess_mode=preprocess_mode) # Test best model print('Testing model') test_acc, test_miou = get_metrics(loader, model, loader.n_classes, train=False, flip_inference=True, scales=[0.75, 1, 1.5], write_images=True, preprocess_mode=preprocess_mode, time_exect=False) print('Test accuracy: ' + str(test_acc.numpy())) print('Test miou: ' + str(test_miou))
height=CONFIG['height'], median_frequency=CONFIG['median_frequency']) print('Dataset loaded...') # build model #model = MiniNetv2.MiniNetv2p(num_classes=CONFIG['n_classes']) model = ResNet50.ResNet50Seg(CONFIG['n_classes'], input_shape=(None, None, 3), weights='imagenet') # optimizer learning_rate = tf.Variable(CONFIG['init_lr']) optimizer = tf.keras.optimizers.Adam(learning_rate) loss_function = tf.keras.losses.CategoricalCrossentropy() # restore if model saved and show number of params restore_state(model, args.weights_path) init_model(model, (1, CONFIG['width'], CONFIG['height'], 3)) get_params(model) # Train print('Training...') _train(loader=loader, optimizer=optimizer, loss_function=loss_function, model=model, config=CONFIG, lr=learning_rate, name_best_model=args.weights_path, evaluation=True, preprocess_mode=args.preprocess)