def main(): in_args = get_train_args() dataloaders, image_datasets = load_data(in_args) model = create_network(in_args) train_network(in_args, model, dataloaders) save_checkpoint(model, in_args, image_datasets) test_network(model, dataloaders, in_args) return None
def standard_train(args, input_var, network, tang_output): """Train a network normally, output the network, a function to make predictions and training losses""" # Create data X = utils.create_dataset(args.npts) # Create a list of batches (a list of batch idxs splitting X in batches of size batch_size) list_batches = utils.get_list_batches(args.npts, args.batch_size) # create loss function prediction = lasagne.layers.get_output(network) loss = lasagne.objectives.squared_error(prediction, tang_output) loss = loss.mean() # create parameter update expressions params = lasagne.layers.get_all_params(network, trainable=True) updates = lasagne.updates.nesterov_momentum( loss, params, learning_rate=args.learning_rate, momentum=0.9) # compile training function that updates parameters and returns training loss train_fn = theano.function([input_var], loss, updates=updates) # train network list_loss = utils.train_network(train_fn, X, list_batches, args.nb_epoch) # Create a prediction function to evaluate after training predict_fn = utils.get_prediction_fn(input_var, network) return network, predict_fn, list_loss
def standard_train(args, input_var, network, tang_output): """Train a network normally, output the network, a function to make predictions and training losses""" # Create data X = utils.create_dataset(args.npts) # Create a list of batches (a list of batch idxs splitting X in batches of size batch_size) list_batches = utils.get_list_batches(args.npts, args.batch_size) # create loss function prediction = lasagne.layers.get_output(network) loss = lasagne.objectives.squared_error(prediction, tang_output) loss = loss.mean() # create parameter update expressions params = lasagne.layers.get_all_params(network, trainable=True) updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=args.learning_rate, momentum=0.9) # compile training function that updates parameters and returns training loss train_fn = theano.function([input_var], loss, updates=updates) # train network list_loss = utils.train_network(train_fn, X, list_batches, args.nb_epoch) # Create a prediction function to evaluate after training predict_fn = utils.get_prediction_fn(input_var, network) return network, predict_fn, list_loss
# Command Line ardguments ap.add_argument('data_dir', nargs='*', action="store", default="./flowers/") ap.add_argument('--gpu', dest="gpu", action="store", default="gpu") ap.add_argument('--save_dir', dest="save_dir", action="store", default="./checkpoint.pth") ap.add_argument('--learning_rate', dest="learning_rate", action="store", default=0.003) ap.add_argument('--dropout', dest = "dropout", action = "store", default = 0.5) ap.add_argument('--epochs', dest="epochs", action="store", type=int, default=12) ap.add_argument('--arch', dest="arch", action="store", default="vgg16", type = str) pa = ap.parse_args() where = pa.data_dir path = pa.save_dir lr = pa.learning_rate structure = pa.arch dropout = pa.dropout power = pa.gpu epochs = pa.epochs trainloader, validationloader, testloader = utils.load_data(where) model, optimizer, criterion = utils.setup_model(structure,dropout,lr,power) utils.train_network(model, optimizer, criterion, epochs, 12, trainloader, power) utils.save_checkpoint(path,structure,dropout,lr)
def run_mlp_experiment(args, device): """ Runs the experiment with a 3-layers fully connected network. :param args: Namespace from utils.parse_arguments :param device: torch.device :return: np.array of float, np.array of float, np.array of float; train_acc, val_acc, test_acc (in percentage) """ validation_ratio, record_train_acc, record_val_acc, record_test_acc = utils.configure_training_mode( args) train_loader, validation_loader, test_loader = datasets.build_loaders_by_dataset( args.dataset, args.batch_size, validation_ratio=validation_ratio, train_validation_split_seed=0) local_loss_list = utils.get_loss(args) nonlinearity = utils.get_nonlinearity(args) optimizer_local, local_opt_arguments_dict, local_scheduler_arguments_dict, \ optimizer_final, final_opt_arguments_dict, final_scheduler_arguments_dict = \ utils.choose_optimizers_and_parameters(args) conv_sizes = [] do_pooling = [] kernel_sizes = [] fc_layers = [args.mlp_layer_size, args.mlp_layer_size, args.mlp_layer_size] if args.divisive_norm_fc: divisive_norm_list = [ networks.DivisiveNorm(args.divnorm_power, args.grouping_dim, args.grouped_var_delta) for i in range(len(fc_layers)) ] else: divisive_norm_list = None alt_feedback_type = None if args.feedback_alignment: alt_feedback_type = 'feedback_alignment' elif args.sign_symmetry: alt_feedback_type = 'sign_symmetry' net = networks.Network( nonlinearity, local_loss_list, optimizer_local, torch.optim.lr_scheduler.MultiStepLR, conv_sizes, kernel_sizes, do_pooling, fc_layers, 'max', args.dataset, bias=False, local_opt_arguments_dict=local_opt_arguments_dict, local_scheduler_arguments_dict=local_scheduler_arguments_dict, dropout_p=args.dropout_p, batch_norm=args.batch_norm, divisive_norm_list_conv=None, divisive_norm_list_fc=divisive_norm_list, spatial_dropout=args.spatial_dropout, alt_feedback_type=alt_feedback_type) net = net.to(device) print(net) final_loss = nn.CrossEntropyLoss() if args.backprop: final_opt = optimizer_final(net.parameters(), **final_opt_arguments_dict) compute_local_loss = False update_local_loss = False else: final_opt = optimizer_final(net.softmax_layer.parameters(), **final_opt_arguments_dict) compute_local_loss = True update_local_loss = True final_scheduler = torch.optim.lr_scheduler.MultiStepLR( final_opt, **final_scheduler_arguments_dict) train_acc, val_acc, test_acc = utils.train_network( net, device, final_loss, final_opt, final_scheduler, args.n_epochs, train_loader, validation_loader, test_loader, compute_local_loss=compute_local_loss, update_local_loss=update_local_loss, record_train_acc=record_train_acc, record_val_acc=record_val_acc, record_test_acc=record_test_acc, print_results=True, backprop_batch_manhattan=args.backprop_batch_manhattan) return train_acc, val_acc, test_acc
default=0.2) parser.add_argument('--arch', dest="arch", action='store', default='vgg16') parser.add_argument('--learning_rate', dest='lr', action='store', default=0.01) parser.add_argument('--hidden_units', dest='hidden_units', action='store', default=1024) parser.add_argument('--epochs', dest='epochs', action='store', default=1) parser.add_argument('--gpu', dest='gpu', action='store', default='gpu') args = parser.parse_args() data_dir = args.data_dir save_dir = args.save_dir dropout_prob = args.drop_prob arch = args.arch learning_rate = args.lr hidden_units = args.hidden_units gpu = args.gpu epochs = args.epochs trainloader, validloader, testloader = utils.data_load(data_dir) model, criterion, optimizer = utils.create_network(arch, learning_rate, hidden_units, dropout_prob) model = utils.train_network(model, criterion, optimizer, trainloader, validloader, epochs, gpu) utils.save_checkpoint(model, data_dir, save_dir, arch, hidden_units, dropout_prob, learning_rate, epochs)
parser.add_argument('--arch', dest='arch', default='densenet169') parser.add_argument('--hidden_layers', dest='hidden_layers', default='1664, 832, 350, 175, 102') parser.add_argument('--learning_rate', dest='learning_rate', default=0.001) parser.add_argument('--gpu', dest='gpu', default=True) parser.add_argument('--epochs', dest='epochs', default=3) args = parser.parse_args() data_dir = args.dir arch = args.arch hidden_layers = [int(x) for x in args.hidden_layers.split(',')] learning_rate = args.learning_rate gpu = args.gpu epochs = int(args.epochs) trainloader, testloader, validationloader, class_to_idx = utils.load_data( data_dir) model, criterion, optimizer = utils.instantiate_model( arch=arch, hidden_layers=hidden_layers, learning_rate=learning_rate) model.class_to_idx = class_to_idx utils.train_network(model, criterion, optimizer, trainloader, validationloader, gpu, epochs) utils.save_checkpoint(model, optimizer, arch, hidden_layers, epochs, learning_rate) print('All done!')
ap.add_argument('--gpu', dest="gpu", action="store", default="gpu") ap.add_argument('--save_dir', dest="save_dir", action="store", default="./checkpoint.pth") ap.add_argument('--learning_rate', dest="learning_rate", action="store", default=0.001) ap.add_argument('--dropout', dest = "dropout", action = "store", default = 0.5) ap.add_argument('--epochs', dest="epochs", action="store", type=int, default=1) ap.add_argument('--arch', dest="arch", action="store", default="vgg16", type = str) ap.add_argument('--hidden_units', type=int, dest="hidden_units", action="store", default=120) pa = ap.parse_args() where = pa.data_dir path = pa.save_dir lr = pa.learning_rate structure = pa.arch dropout = pa.dropout hidden_layer1 = pa.hidden_units power = pa.gpu epochs = pa.epochs # Load dataset and assets dataloader=utils.load_data(where) # Setup Neural Network Model model,criterion,optimizer,device=utils.nn_setup(structure,hidden_layer1,dropout,lr,power) # Train the Neural Network Model utils.train_network(model,criterion,optimizer,device,dataloader['loaders']['trainingloader'],dataloader['loaders']['validationloader'],epochs) # Save Model Configurations utils.save_checkpoint(path,model,structure,hidden_layer1,dropout,lr,epochs,dataloader['dataset']['training_dataset'],power)
default="vgg16") ap.add_argument('--hidden_layer', dest="hidden_layer", action="store", type=int, default=16725) pa = ap.parse_args() place = pa.data_dir path = pa.save_dir check_steps = pa.check_steps lr = pa.learning_rate dropout = pa.dropout epochs = pa.epochs power = pa.gpu arch = pa.arch hidden_size = pa.hidden_layer train_dataset, validate_dataset, test_dataset, train_dataloader, validate_dataloader, test_dataloader = utils.load_data( place) model, classifier, criterion, optimizer = utils.nn_setup( arch, dropout, hidden_size, lr, power) utils.train_network(model, criterion, optimizer, train_dataloader, validate_dataloader, epochs, check_steps, lr, power) utils.save_checkpoint(model, arch, classifier, train_dataset, path) print("All Set and Done! The Model is trained")