def validation(model, validloader, criterion): device = helper.get_device() test_loss = 0 accuracy = 0 print("\nPERFORMING VALIDATION CHECK") for ii, (images, labels) in enumerate(validloader): images, labels = images.to(device), labels.to(device) output = model.forward(images) test_loss += criterion(output, labels).item() ps = torch.exp(output) equality = (labels.data == ps.max(dim=1)[1]) accuracy += equality.type(torch.FloatTensor).mean() return test_loss, accuracy
def predict(model): if (model == None): model = helper.load_checkpoint() device = helper.get_device() image_tensor, image = helper.get_image_tensor( '\nPlease enter the path of the image you want to analyse\n') image_tensor = image_tensor.to(device) topk = helper.get_int( '\nPlease enter how many to the top predictions you want to see (topk)\n' ) model = model.to(device) print('\nPredicting\n') with torch.no_grad(): output = model.forward(image_tensor) ps = torch.exp(output) topK_ps = torch.topk(ps, topk) probs = topK_ps[0].cpu().numpy().squeeze() sorted_ps_label_keys = topK_ps[1].cpu().numpy().squeeze() classes = [] print('Sorted label keys {}'.format(sorted_ps_label_keys)) try: get_label = lambda x: idx_to_class[str(x)] for i in sorted_ps_label_keys[0:topk]: classes.append(get_label(i)) except NameError: print( '\nCaught Key Error idx_to_class does not exist\nUsing normal keys\n' ) for i in sorted_ps_label_keys[0:topk]: classes.append(i) print('\nFinished predicting\n') helper.view_classify(image, probs, classes) return
def predict(model): if (model == None): model = helper.load_checkpoint() image_path = helper.get_file_path( '\nPlease enter the path of the image you want to analyse\n') topk = helper.get_int( '\nPlease enter how many to the top predictions you want to see (topk)\n' ) device = helper.get_device() model = helper.load_device(model) image_tensor = helper.process_image(image_path).to(device) idx_to_class = helper.get_idx_to_class() print('\nPredicting\n') with torch.no_grad(): output = model.forward(image_tensor) ps = torch.exp(output) topK_ps = torch.topk(ps, topk) probs = topK_ps[0].cpu().numpy().squeeze() sorted_ps_label_keys = topK_ps[1].cpu().numpy().squeeze() get_label = lambda x: idx_to_class[str(x)] classes = [] for i in sorted_ps_label_keys[0:topk]: classes.append(get_label(i)) print('\nFinished predicting\n') return probs, classes
def train_network(model): if (model == None): model = helper.build_or_load_model() optimizer = model.optimizer criterion = model.criterion valid_pass = False valid_loader = None train_loader = None epochs = helper.get_int('Please enter the epochs for training') train_data_dir = helper.get_dir_path( 'Please enter path to the training data from current location') train_loader = helper.get_dataloader(train_data_dir, Constant.TRAIN) valid_pass = helper.get_yn_input('Do you want to do a validation pass?') if (valid_pass): valid_data_dir = helper.get_dir_path( 'Please enter path to the validating data from current location') valid_loader = helper.get_dataloader(valid_data_dir, Constant.VALID) print_every = len(train_loader) steps = 0 print('\nTraining the network\n') device = helper.get_device() model = helper.load_device(model) stepsArr = [] accuracyArr = [] print("\nTrain loader has {} images\n".format(len(train_loader))) # run a pre-set amount of times (epochs) for e in range(epochs): running_loss = 0 accuracy = 0 for ii, (images, labels) in enumerate(train_loader): steps += 1 images, labels = images.to(device), labels.to(device) optimizer.zero_grad() # Forward and backward passes outputs = model.forward(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() ps = torch.exp(outputs) #_, predicted = torch.max(outputs.data, 1) running_loss += loss.item() equality = (labels.data == ps.max(dim=1)[1]) accuracy = equality.type(torch.FloatTensor).mean() accString = "{:.2f}".format(accuracy * 100) print("Step {} Test Loss {:.3f} Running Loss {:.3f} Accuracy {}%". format(steps, loss, running_loss, accString)) stepsArr.append(steps) accuracyArr.append(accString) if (steps % print_every == 0): print("\n\nFinished Epoch: {}/{}.. ".format(e + 1, epochs)) if (valid_pass): print("\nStarting validation pass for epoch: {}/{}.. ". format(e + 1, epochs)) # Make sure network is in eval mode for inference model.eval() # Turn off gradients for validation, saves memory and computations with torch.no_grad(): test_loss, accuracy = helper.validation( model, valid_loader, criterion) print( "\nFinished validating for epoch: {}/{}.. ".format( e + 1, epochs), "Training loss: {:.3f}.. ".format( running_loss / print_every), "Validation loss: {:.3f}.. ".format(test_loss / len(valid_loader)), "Validation accuracy: {:.3f} %".format( accuracy / len(valid_loader) * 100)) running_loss = 0 if (e == epochs): break else: # Make sure training is back on model.train() print("\n\nTraining finished\n")
import os import numpy as np from torch import nn from torch import optim import torch.nn.functional as F from torchvision import datasets, transforms, models flag = False learn_rate = None hidden_layers = [] model = None criterion = None optimizer = None actions = ['predict', 'build', 'train', 'exit'] device = helper.get_device() while (flag != True): action = input( "\nWhat would you like to do? Actions include: \n{}\n".format(actions)) if (action == actions[0]): if (model != None): predict.predict(model) else: predict.predict(None) elif (action == actions[1]): model = helper.build_new_network() elif (action == actions[2]): if (model != None):
os.path.join( os.path.abspath(__file__ + "/../../assets/"), "fonts", name ) ) return ImageFont.truetype(font_path, size) # Maintains current time in milliseconds current_milli_time = lambda: int(str(round(time.time() * 1000))[-3:]) try: device = get_device() font_regular, font_bold = setDisplayStyle() while not internet_connection_found(): generate_arrival_board(device, None, None) time.sleep(5) requested_station = get_station() last_refresh_time_aws = time.time() upcoming_arrivals = [] last_refresh_time_tfl = time.time() - REFRESH_INTERVAL_TFL while True: