def adversarial_example(torch_dev_dataset, parameters, ckpt): print("Start part 3") # Question 3 - Adversarial example data_loader = DataLoader(dataset=torch_dev_dataset, batch_size=1) adversarial_model = SimpleModel() adversarial_model.load(path=ckpt) adversarial = Adversarial(adversarial_model, data_loader, parameters['adversarial_epsilons'], parameters['path_plots_adversarial']) adversarial.__attack__() adversarial.__plot_attack__() adversarial.__plot_examples__() print("End part 3")
def full_process(torch_train_dataset, torch_dev_dataset, train_loader, dev_loader, counters_train, counters_dev, parameters): # Load model pre_trained = SimpleModel() pre_trained.load(path=parameters['pretrained_path']) # Evaluate given model and train it evaluation(pre_trained, dev_loader, parameters, counters_dev, "pretrain_model", True) trainer = training_loop(pre_trained, train_loader, parameters, "pretrain_model") # Load the best state of the model we've trained and evaluate trained = SimpleModel() trained.load(path=trainer.ckpt) evaluation(trained, dev_loader, parameters, counters_dev, "trained_model", True) # Model improvements - get mislabeled images and fix them print("Improving given model:") print("Fix train dataset") torch_train_dataset_fixed = dataset_fix(torch_train_dataset, trainer, parameters, counters_dev, False, "train") train_loader = create_data_loader(torch_train_dataset_fixed, counters_train, parameters, True) print( "==============================================================================" ) print("Fix dev dataset") torch_dev_dataset_fixed = dataset_fix(torch_dev_dataset, trainer, parameters, counters_dev, False, "dev") dev_loader = create_data_loader(torch_dev_dataset_fixed, counters_train, parameters, True) print( "==============================================================================" ) counters_train, counters_dev = inspect_dataset(torch_train_dataset_fixed, torch_dev_dataset) # Test given model after fixing datasets print("Test given model after fixing datasets") test_model = SimpleModel() test_model.load(path=parameters['pretrained_path']) trainer = training_loop(test_model, train_loader, parameters, "train_improved_model_train") ckpt = trainer.ckpt eval_model = SimpleModel() eval_model.load(path=ckpt) evaluation(eval_model, dev_loader, parameters, counters_dev, "improved_model_eval", True) print("End part 1") print( "==============================================================================" ) playing_with_learning_rate(train_loader, parameters) adversarial_example(torch_dev_dataset, parameters, ckpt) return ckpt
def dataset_fix(dataset, trainer, parameters, counters_dev, is_evaluating, name): mislabeled_train_loader = DataLoader(dataset=dataset) improved_train_model = SimpleModel() improved_train_model.load(path=trainer.ckpt) evaluator = evaluate(improved_train_model, mislabeled_train_loader, parameters, counters_dev, "improved_model_eval", is_evaluating) if name == "train": output = parameters['fixed_dataset'] else: output = parameters['fixed_dataset_dev'] fix_dataset(evaluator, dataset, output) torch_dataset_fixed = get_dataset_as_torch_dataset(path=output) return torch_dataset_fixed
for item in tf.global_variables(): tf.summary.histogram(item.name, item) merged_summary = tf.summary.merge_all() train_writer = tf.summary.FileWriter('./adaptaion_summary/train', tf.get_default_graph()) test_writer = tf.summary.FileWriter('./adaptaion_summary/test') with tf.Session() as sess: sess.run(tf.global_variables_initializer()) train_task_np, val_task_np, train_labels_np, val_labels_np = sess.run( (train_task, val_task, train_labels, val_labels)) model.load(sess, model_address) for it in range(15): output, loss, summ = sess.run( (model.out, model.loss, merged_summary), feed_dict={ model.is_training: False, train_task: val_task_np, train_labels: val_labels_np, }) test_writer.add_summary(summ, global_step=it) _, summ = sess.run( (model.op, merged_summary), feed_dict={ model.is_training: True, train_task: train_task_np,
def train_and_eval(dev_loader, train_loader, counters, parameters, name, is_evaluating, path): model = SimpleModel() model.load(path) evaluate(model, dev_loader, counters, parameters, name, is_evaluating) return training_loop(model, train_loader, parameters, name)