def main(dirname, dirname2): x_train, y_train, x_val, y_val = load_data(dirname) x_test, y_test = load_data_testing(dirname2) # print('\nTRAINING DATA X-') # print(x_train.shape) # print('\nTRAINING DATA Y-') # print(y_train.shape) # print('\nVALIDATION DATA X-') # print(x_val.shape) # print('\nVALIDATION DATA Y-') # print(y_val.shape) # print('\n') # print('\TEST DATA X-') # print(x_test.shape) # print('\nTEST DATA Y-') # print(y_test.shape) # print('\n') model = build_model(y_train.shape[1]) print('Training stage') print('==============') model.fit(x_train, y_train, epochs=200, batch_size=16, validation_data=(x_val, y_val)) score, acc = model.evaluate(x_test, y_test, batch_size=16, verbose=0) print('Test performance: accuracy={0}, loss={1}'.format(acc, score)) model.save('model.h5') model.summary()
def main(dirname): x_train, y_train, x_test, y_test = load_data(dirname) print(y_train.shape) model = build_model(y_train.shape[1]) print(model.summary()) print('Training stage') print('==============') history = model.fit(x_train, y_train, epochs=250, batch_size=16, validation_data=(x_test, y_test)) score, acc = model.evaluate(x_test, y_test, batch_size=16, verbose=0) print('Test performance: accuracy={0}, loss={1}'.format(acc, score)) model.save('model.h5')
def main(dirname): x_train, y_train, x_test, y_test = load_data(dirname) num_val_samples = (x_train.shape[0]) // 5 model = build_model(y_train.shape[1]) print('Training stage') print('==============') history = model.fit(x_train, y_train, epochs=100, batch_size=32, validation_data=(x_test, y_test)) score, acc = model.evaluate(x_test, y_test, batch_size=16, verbose=0) print('Test performance: accuracy={0}, loss={1}'.format(acc, score)) model.save('HPRmodelv4.h5')
def main(): # Get Command Line Arguments args = ParseCommandLine() #Print data directory print("Data directory: ", args.data_directory) #Print device used use_gpu = torch.cuda.is_available() and args.gpu if use_gpu: print("Training on GPU.") else: print("Training on CPU.") #Print out architecture and hyperparameters print("Architecture: {}".format(args.arch)) print("Learning rate: {}".format(args.learning_rate)) print("Hidden units: {}".format(args.hidden_units)) print("Epochs: {}".format(args.epochs)) #Print our dave_dir option if args.save_dir: print("Checkpoint save directory: {}".format(args.save_dir)) #-------------------------------------------------------------------- # Get data loaders train_loader, valid_loader, test_loader, class_to_idx = train_utils.load_data( args.data_directory) #-------------------------------------------------------------------- # Build the model model = train_utils.build_model(args.arch, args.hidden_units) criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr=args.learning_rate) model.class_to_idx = class_to_idx #-------------------------------------------------------------------- #Train the model train_utils.train_model(model, args.epochs, args.learning_rate, use_gpu, criterion, optimizer, train_loader, valid_loader) #-------------------------------------------------------------------- #Validation on the test set test_loss, accuracy = train_utils.validate_model(model, criterion, test_loader) print("Validation on the test set") print(f"Test accuracy: {accuracy:.2f}%") #-------------------------------------------------------------------- # Save the checkpoint if input_args.save_dir: save_checkpoint(args.arch, args.learning_rate, args.hidden_units, args.epochs, model, optimizer, args.save_directory)
import argparse import train_utils parser = argparse.ArgumentParser( description='This script helps in training the model', ) parser.add_argument('--data_directory', dest='data_directory', action='store', default='./flowers') parser.add_argument('--model_name', dest='model_name', action='store', default='vgg16') parser.add_argument('--save_dir', dest='save_dir', action='store', default='checkpoint.pth') parser.add_argument('--learning_rate', dest='learning_rate', action='store', default=0.001, type=float) parser.add_argument('--hidden_input', dest='hidden_input', action='store', default=1024, type=int) parser.add_argument('--epochs', dest='epochs', action='store', default=5, type=int) parser.add_argument('--gpu', dest="mode", action="store", default="gpu") args = parser.parse_args() # fetch dataloaders train_data, train_dataloader, test_dataloader, validate_dataloader = train_utils.load_data(args.data_directory) # setup the classifier, criterion, optimizer model model, optimizer, criterion = train_utils.create_model( args.model_name, args.hidden_input, args.learning_rate, args.mode) # train model train_utils.train_model(model, optimizer, criterion, train_dataloader, validate_dataloader, args.epochs, args.mode) # save the model as checkpoint train_utils.save_checkpoint(model, args, optimizer, train_data)
def main(): test_loaders=False inputs = argparse.Argumentinputs(description='Train a new network on a data set') inputs.add_argument('data_dir', type=str, \ ) inputs.add_argument('--save_dir', type=str, \ help='Directory to save checkpoints') inputs.add_argument('--arch', type=str, \ ) inputs.add_argument('--learning_rate', type=float, \ ) inputs.add_argument('--hidden_units', type=int, \ ) inputs.add_argument('--epochs', type=int, \ ) inputs.add_argument('--gpu', action='store_true', \ ) inputs.add_argument('--save_every', type=int, \ ) args, _ = inputs.parse_known_args() data_dir = args.data_dir save_dir = './' if args.save_dir: save_dir = args.save_dir arch = 'densenet121' if args.arch: arch = args.arch learning_rate = 0.01 if args.learning_rate: learning_rate = args.learning_rate hidden_units = 200 if args.hidden_units: hidden_units = args.hidden_units epochs = 7 if args.epochs: epochs = args.epochs save_every = 50 if args.save_every: save_every = args.save_every cuda = False if args.gpu: if torch.cuda.is_available(): cuda = True else: print("Warning! GPU flag was set however no GPU is available in \ the machine") trainloader, validloader, testloader = load_data(data_dir) # Test loaders if test_loaders: images, labels = next(iter(trainloader)) imshow(images[2]) plt.show() images, labels = next(iter(validloader)) imshow(images[2]) plt.show() images, labels = next(iter(testloader)) imshow(images[2]) plt.show() train_model(trainloader, validloader, arch=arch, hidden_units=hidden_units,\ learning_rate=learning_rate, cuda=cuda, epochs=epochs, save_dir=save_dir, \ save_every=save_every)