Ejemplo n.º 1
0
def model_training(model, dataloader_train, dataloader_valid, gpu_f, learn_r,
                   num_epoches):
    # Define the optimizer to be used in training and number of epoches to run
    optmizer = optim.Adam(model.classifier.parameters(), lr=learn_r)
    # Define the criterion that will be used in training and evaluation
    criterion = nn.NLLLoss()

    # Training of model
    for e in range(num_epoches):

        for images, labels in iter(dataloader_train):
            optmizer.zero_grad()
            images, device = hw_control(images, gpu_f)
            labels, device = hw_control(labels, gpu_f)

            output = model.forward(images)
            loss = criterion(output, labels)
            loss.backward()
            optmizer.step()

        # Turn off gradients for validation, saves memory and computations
        with torch.no_grad():
            Valid_loss, accuracy = evaluation(model, dataloader_valid,
                                              criterion, gpu_f)
            print('Epoch {}/{}: Validation loss= {} \n Accuracy= {}'.format(
                e, num_epoches, Valid_loss, accuracy))

    return model
Ejemplo n.º 2
0
def evaluation(model, data_loader, criterion, gpu_f):
    ''' Test the performence of NN against givien data set either validation of test.
       
        Arguments
        ---------
        model: the model to be tested
        data_loader: data loader for the data set to be used in testing
        criterion: criterion to measure the performence upon
    '''
    model.eval()
    loss = 0
    accuracy = 0

    for image, label in data_loader:

        image, device = hw_control(image, gpu_f)
        label, device = hw_control(label, gpu_f)

        output = model.forward(image)
        loss += criterion(output, label).item()
        ps = torch.exp(output)
        equality = (label.data == ps.max(dim=1)[1])
        accuracy = equality.type(torch.FloatTensor).mean()

    model.train()
    return loss, accuracy
def predict(image_path, model, topk, gpu_f):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    Arguments
        ---------
        image_path: Pathh to image
        model: the pretrained model
        topk: integer, number of class probabilities to be displayed
    '''

    # Prepare image
    Tensor_image = process_image(image_path)
    Tensor_image, device = hw_control(Tensor_image, gpu_f)
    Tensor_image.unsqueeze_(0)
    Tensor_image = Tensor_image.float()

    with torch.no_grad():
        # Put the model in evaluation mode
        model.eval()
        # Forward propagation through the model
        output = model.forward(Tensor_image)
        ps = torch.exp(output)
    print(ps)
    print(ps.max())
    top_k_prob = ps.topk(topk)
    #Make sure the module is back to training mode
    model.train()

    # Return the results as numpy arrays
    probablities = top_k_prob[0][0].cpu().numpy()
    classes = top_k_prob[1][0].cpu().numpy()
    return probablities, classes
Ejemplo n.º 4
0
def main():
    
    # Get the arguments passed to the python script 
    in_arg = get_input_args_train()
    train_data_loader, valid_data_loader = data_preprocessing(in_arg.path)
    model = creat_model(in_arg.arch, in_arg.hidden_units, 25088, 102)
    
    # Send model to gpu
    model_gpu, device = hw_control(model, in_arg.gpu)

    # Train the model    
    model = model_training(model_gpu, train_data_loader, valid_data_loader, in_arg.gpu, in_arg.learning_rate, in_arg.epochs)
    
    # Save CP of model
    save_model_cp(model, in_arg.arch, 25088, 102, in_arg.hidden_units, in_arg.save_dir)
Ejemplo n.º 5
0
def main():

    # Get the arguments passed to the python script
    in_arg = get_input_args_predict()

    # Load check point from pass
    model = load_checkpoint(in_arg.checkpoint)

    # Send model to gpu
    model_gpu, device = hw_control(model, in_arg.gpu)

    # Perform the prediction
    probs, classes = predict(in_arg.path, model, in_arg.top_k, in_arg.gpu)

    with open('cat_to_name.json', 'r') as f:
        cat_to_name = json.load(f)

    Class_names = [cat_to_name[str(class_name)] for class_name in classes]

    print("Most probable cat.:\t", Class_names)
    print("Probalbilities \t", probs)

    return probs, classes