def demo_main(char_set, weight, name): _, valid_transform = get_transform() demo_data = DemoDataset('cleaned_data', name, valid_transform) test_loader = DataLoader( dataset=demo_data, batch_size=3, shuffle=False, num_workers=1, pin_memory=True, ) model = ConvNet(1, len(char_set)) if torch.cuda.is_available(): model = model.cuda() print('load weights from {}'.format(weight)) model.load_state_dict(torch.load(weight)) model.eval() def map_indexlist_char(ind_list, char_set): return ''.join([char_set[i] for i in ind_list]) with torch.no_grad(): for batch_idx, (x, imgpath) in enumerate(test_loader): if batch_idx > 0: break x = x.cuda() out = model(x) _, pred_label = torch.max(out, 1) pred_name = map_indexlist_char(pred_label.tolist(), char_set) print('name {} pred name {}'.format(name, pred_name)) def get_concat(im1, im2): dst = Image.new('RGB', (im1.width + im2.width, im1.height)) dst.paste(im1, (0, 0)) dst.paste(im2, (im1.width, 0)) return dst concat_im = None for img in demo_data.images(): im = Image.open(img) if concat_im is None: concat_im = im else: concat_im = get_concat(concat_im, im) #concat_im.show() concat_im.save('demo.jpg')
def perform_experiments(n_runs=10, n_points=1000, n_epochs=200, run_best=False, verbose=False): """ Perform experiments for 5 different neural network architectures and losses. To run all experiments call this function with default params :param n_runs: number of runs for which experiment should be repeated :param n_points: number of training and testing data points used in the experiments :param n_epochs: number of epochs every architecture should be trained on :param run_best: If True only the best architecture (Siamese Network with auxiliary loss) is trained :param verbose: If True, print training and validation loss every epoch :returns: dictionary containing history of training (training, validation loss and accuracy) """ history_mlp_net = [] history_conv_net = [] history_conv_net_aux = [] history_siamese = [] history_siamese_aux = [] for n_run in range(n_runs): data_set = generate_pair_sets(n_points) MAX_VAL = 255.0 TRAIN_INPUT = Variable(data_set[0]) / MAX_VAL TRAIN_TARGET = Variable(data_set[1]) TRAIN_CLASSES = Variable(data_set[2]) TEST_INPUT = Variable(data_set[3]) / MAX_VAL TEST_TARGET = Variable(data_set[4]) TEST_CLASSES = Variable(data_set[5]) if not run_best: ############################################################################## # Creates Multilayer Perceptron Network with ReLU activationss mlp_net = MLPNet(in_features=392, out_features=2, n_layers=3, n_hidden=16) # Set train flag on (for dropouts) mlp_net.train() # Train the model and append the history history_mlp_net.append( train_model(mlp_net, train_input=TRAIN_INPUT.view((n_points, -1)), train_target=TRAIN_TARGET, val_input=TEST_INPUT.view((n_points, -1)), val_target=TEST_TARGET, n_epochs=n_epochs, verbose=verbose)) # Set train flag to False for getting accuracies on validation data mlp_net.eval() acc = get_accuracy(mlp_net, TEST_INPUT.view( (n_points, -1)), TEST_TARGET) * 100.0 print("Run: {}, Mlp_net Test Accuracy: {:.3f} %".format( n_run, acc)) ############################################################################## # Create ConvNet without auxiliary outputs conv_net = ConvNet(n_classes=2, n_layers=3, n_features=16) # Set train flag on (for dropouts) conv_net.train() # Train the model and append the history history_conv_net.append( train_model(conv_net, train_input=TRAIN_INPUT, train_target=TRAIN_TARGET, val_input=TEST_INPUT, val_target=TEST_TARGET, n_epochs=n_epochs, verbose=verbose)) # Set train flag to False for getting accuracies on validation data conv_net.eval() acc = get_accuracy(conv_net, TEST_INPUT, TEST_TARGET) * 100.0 print("Run: {}, ConvNet Test Accuracy: {:.3f} %".format( n_run, acc)) ############################################################################## # Create ConvNet with auxiliary outputs conv_net_aux = ConvNet(n_classes=22, n_layers=3, n_features=16) # Set train flag on (for dropouts) conv_net_aux.train() # Train the model and append the history history_conv_net_aux.append( train_model(conv_net_aux, train_input=TRAIN_INPUT, train_target=TRAIN_TARGET, aux_param=1.0, train_classes=TRAIN_CLASSES, val_input=TEST_INPUT, val_target=TEST_TARGET, val_classes=TEST_CLASSES, n_epochs=n_epochs, verbose=verbose)) # Set train flag to False for getting accuracies on validation data conv_net_aux.eval() acc = get_accuracy(conv_net_aux, TEST_INPUT, TEST_TARGET) * 100.0 print("Run: {}, ConvNet Auxilary Test Accuracy: {:.3f} %".format( n_run, acc)) ############################################################################## # Create Siamese Network without auxiliary outputs conv_net = BlockConvNet() conv_net_siamese = DeepSiameseNet(conv_net) # Set train flag on (for dropouts) conv_net.train() conv_net_siamese.train() # Train the model and append the history history_siamese.append( train_model(conv_net_siamese, train_input=TRAIN_INPUT, train_target=TRAIN_TARGET, val_input=TEST_INPUT, val_target=TEST_TARGET, n_epochs=n_epochs, verbose=verbose)) # Set train flag to False for getting accuracies on validation data conv_net.eval() conv_net_siamese.eval() acc = get_accuracy(conv_net_siamese, TEST_INPUT, TEST_TARGET) * 100.0 print("Run: {}, Siamese Test Accuracy: {:.3f} %".format( n_run, acc)) ############################################################################## # Create Siamese Network with auxiliary outputs conv_net = BlockConvNet() conv_net_siamese_aux = DeepSiameseNet(conv_net) # Set train flag on (for dropouts) conv_net.train() conv_net_siamese_aux.train() # Train the model and append the history history_siamese_aux.append( train_model(conv_net_siamese_aux, train_input=TRAIN_INPUT, train_target=TRAIN_TARGET, train_classes=TRAIN_CLASSES, val_input=TEST_INPUT, val_target=TEST_TARGET, val_classes=TEST_CLASSES, aux_param=3.0, n_epochs=n_epochs, verbose=verbose)) # Set train flag to False for getting accuracies on validation data conv_net.eval() conv_net_siamese_aux.eval() acc = get_accuracy(conv_net_siamese_aux, TEST_INPUT, TEST_TARGET) * 100.0 print("Run: {}, Siamese Auxilary Test Accuracy: {:.3f} %".format( n_run, acc)) ############################################################################## return { 'history_mlp_net': history_mlp_net, 'history_conv_net': history_conv_net, 'history_conv_net_aux': history_conv_net_aux, 'history_siamese': history_siamese, 'history_siamese_aux': history_siamese_aux }