Esempio n. 1
0
# set grad_enabled setting
torch.set_grad_enabled(True)
for param in model.features.parameters():
    param.requires_grad = False

# set loss function and optimizer
criterion = nn.NLLLoss()
optimizer = optim.SGD(model.classifier.parameters(), lr=args["learning_rate"])

# tramsfer the model and train
model = model.to(device)
model = model_functions.train_model(model, args["epochs"], device, optimizer,
                                    criterion, train_loader, valid_loader)

# test the model with training dataset
model_functions.test_model(model, device, optimizer, criterion, test_loader)

# make the checpoint dictionary
checkpoint = {
    'epochs': args["epochs"],
    'arch': args["arch"],
    'classifier_dict': classifier_dict,
    'cumulative_epochs': args["epochs"],
    'state_dict': model.state_dict(),
    'class_to_idx': train_data.class_to_idx,
    'optimizer_state_dict': optimizer.state_dict()
}

# save the checkpoint in the file
torch.save(checkpoint, args["save_path"])
Esempio n. 2
0
             model = train_model(model=model,
                                 target_size=target_size,
                                 batch_size=args.batch_size,
                                 list_of_categories=categories,
                                 train_epoch=args.epochs,
                                 steps_per_epoch=args.steps_per_epoch,
                                 validation_split=validation_split,
                                 train_folder=train_folder,
                                 checkpoint_path=checkpoint_path,
                                 tensorboard_path=tensorboard_path,
                                 as_gray=as_gray)
         # else just eval
         test_model(model=model,
                    target_size=target_size,
                    batch_size=args.batch_size,
                    list_of_categories=categories,
                    as_gray=as_gray,
                    test_folder=test_folder,
                    result_folder=result_folder,
                    image_type=image_type)
 else:
     if not args.eval:  # Training
         model = train_model(model=model,
                             target_size=target_size,
                             batch_size=args.batch_size,
                             list_of_categories=categories,
                             train_epoch=args.epochs,
                             steps_per_epoch=args.steps_per_epoch,
                             validation_split=validation_split,
                             train_folder=train_folder,
                             checkpoint_path=checkpoint_path,
                             tensorboard_path=tensorboard_path,
Esempio n. 3
0
                                    val_folder=val_folder,
                                    checkpoint_path=checkpoint_path,
                                    tensorboard_path=tensorboard_path,
                                    as_gray=as_gray,
                                    data_gen_args=data_gen_args,
                                    early_stop_number=args.early_stop,
                                    image_type=image_type)
                print("Done training")

            model.load_weights(str(checkpoint_path))
            # else just eval
            test_model(model=model,
                       target_size=target_size,
                       batch_size=args.batch_size,
                       list_of_categories=categories,
                       as_gray=as_gray,
                       test_folder=test_folder,
                       result_folder=result_folder,
                       image_type=image_type,
                       save_images=True,
                       max_images_saved=0)
    else:
        print(tf.device)
        if not args.eval:  # Training
            model = train_model(model=model,
                                target_size=target_size,
                                batch_size=args.batch_size,
                                list_of_categories=categories,
                                train_epoch=args.epochs,
                                steps_per_epoch=args.steps_per_epoch,
                                validation_split=validation_split,
                                train_folder=train_folder,