def main(): start_time = time() in_arg = get_input_args() check_command_line_arguments(in_arg) results = get_pet_labels(in_arg.dir) check_creating_pet_image_labels(results) classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): """ Runs the different modules together. """ # Retrieves Command Line Arguments from user as input from the user running # the program from a terminal window in_arg = get_input_args() # Loads the dataset into different variables train_data, validation_data, test_data, train_loader, validation_loader, test_loader, cat_to_name = data_loader(in_arg.save_dir) # Generates deep learning model model = model_loader(in_arg.arch, in_arg.hidden_unit) # GPU if in_arg.gpu == "True": device = "cuda" else: device = "cpu" # Trains model model, criterion = model_train(model, train_loader, validation_loader, in_arg.learning_rate, in_arg.epochs) # Tests models against a validation dataset model_test(model, criterion, test_loader, validation_loader) # Saves the trained model model_checkpoint(model, train_data)
def main(): # Measures total program runtime by collecting start time start_time = time() # Function that retrieves 3 Command Line Arugments from user's input in_arg = get_input_args() # Function that checks command line arguments using in_arg - # Remove the # from in front of the function call after you have # coded get_input_args to check your code check_command_line_arguments(in_arg) # Creates a dictionary that contains the results - called results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results # Remove the # from in front of the function call after you have # coded get_pet_labels to check your code check_creating_pet_image_labels(results) # Creates Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary - results # Remove the # from in front of the function call after you have # coded classify_images to check your code check_classifying_images(results) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results # Remove the # from in front of the function call after you have # coded adjust_results4_isadog to check your code check_classifying_labels_as_dogs(results) # Calculates results of run and puts statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary - results_stats # Remove the # from in front of the function call after you have # coded calculates_results_stats to check your code check_calculating_results(results, results_stats) # Prints summary results, incorrect classifications of dogs # and breeds if requested print_results(results, results_stats, in_arg.arch, True, True) # Measure total program runtime by collecting end time end_time = time() # Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print("\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int((tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): # TODO 0: Measures total program runtime by collecting start time start_time = time() sleep(10) # This function returns # the collection of these command line arguments from the function call as # the variable in_arg in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # This function creates the results dictionary that contains the results, # this dictionary is returned from the function call as the variable results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) # Creates Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # TODO 5: Define calculates_results_stats function within the file calculates_results_stats.py # This function creates the results statistics dictionary that contains a # summary of the results statistics (this includes counts & percentages). This # dictionary is returned from the function call as the variable results_stats # Calculates results of run and puts statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) # TODO 0: Measure total program runtime by collecting end time end_time = time() # TODO 0: Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): start_time = time() in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = end_time - start_time #calculate difference between end time and start time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): """ Runs the different modules together """ # Retrieves Comman Line Arguments from user as input from the user running # the program from a terminal window in_arg = get_input_args() # GPU - Allows user to use the GPU to calculate the predictions if in_arg.gpu == "True": device = "cuda" else: device = "cpu" # Load json cat_to_name = json_load(in_arg.json) # Load model model = model_load(in_arg.checkpoint) # Prediction probabilities, classes = class_prediction(in_arg.save_dir, model, in_arg.top_k) # Convert class IDs to flower labels flower_labels = [] prediction_dictionary = {} for key in classes: flower_labels.append(cat_to_name[key]) # Display class names for flower, flower_probabilitiy in zip(flower_labels, probabilities): prediction_dictionary[flower] = round(flower_probabilitiy, 2) print(prediction_dictionary)
def check_flower(image_path): start_time = time() input_args = get_input_args() cat_to_name = cat_to_names() topk = input_args.top_k """gets model and class_to_idx from checkpoint:""" model, model.class_to_idx = load_checkpoint() """gets predicted name, max probability, topk probabilities and topk classes for test image:""" pred_name, pred_prob, top_probabilities, top_classes = predict(model, image_path) """Printing results:""" print(" Pred_Name: {:20} Probability: {:0.3f}\n Top {} probabilities: {}\n Top {} classes: {}".format(pred_name, pred_prob, topk, top_probabilities, topk, top_classes)) for i in range(1,topk): print(" #{}: {:20} probability: {:0.5f}".format(i+1, [cat_to_name[cl] for cl in top_classes][i], top_probabilities.tolist()[0][i])) """Computes overall runtime and prints it in hh:mm:ss format:""" end_time = time() tot_time = end_time - start_time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(round((tot_time%3600)%60)) ) return pred_name, pred_prob, top_classes[0]
def main(): start_time = time() in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # this dictionary is returned from the function call as the variable results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): # start the timer and parse any args from the user start_time = time() in_arg = get_input_args() check_command_line_arguments(in_arg) # infer the correct answers from the image file names results = get_pet_labels(in_arg.dir) check_creating_pet_image_labels(results) # predict the result for each image, for the given architecture classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) # aggregate the results to dog not-dog level adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) # calculate accuracy and some other aggregate statistics results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) # print the final results for this architecture and stop the timer print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = (end_time - start_time) print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): start_time = time() in_arg = get_input_args() check_command_line_arguments(in_arg)
def main(): # Read arguments from command line in_args = get_input_args() train_dir = in_args.dir + '/train' valid_dir = in_args.dir + '/valid' test_dir = in_args.dir + '/test' trainloaders, testloaders, validloaders = process_data(train_dir, test_dir, valid_dir) model = pretrained_model(in_args.arch) for name, child in model.named_children(): print(name) #model = getattr(models, in_args.arch)(pretrained=True) # https://knowledge.udacity.com/questions/262667 for param in model.parameters(): param.requires_grad = False model = set_classifier(model, in_args.hidden_units) for name, child in model.named_children(): print(name) model.classifier.requires_grad = True #params_to_update = [] # for name, param in model.named_parameters(): # if param.requires_grad == True: # params_to_update.append(param) #print(params_to_update) #criterion = nn.NLLLoss() # optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=in_args.lr) #for param in model.parameters(): # param.requires_grad = False params_to_update = [] for name, param in model.named_parameters(): if param.requires_grad == True: params_to_update.append(param) criterion = nn.NLLLoss() optimizer = optim.Adam(params_to_update, lr = in_args.lr) #optimizer = optim.Adam(model.classifier.parameters(), lr=in_args.lr) # trmodel = train_model(in_args.epochs, trainloaders, validloaders, in_args.gpu, model, optimizer, criterion) #valid_model(trmodel, testloaders, in_args.gpu) #save_checkpoint(in_args.arch, in_args.epochs, model, optimizer, in_args.save_dir) print('Completed!')
def main(): # Measures total program runtime by collecting start time start_time = time() # Retrieve the 3 Command Line Arugments from user as input from # the user running the program from a terminal window. Returns # the collection of these command line arguments from the function # call as the variable in_arg in_arg = get_input_args() # Checks command line arguments using in_arg check_command_line_arguments(in_arg) # Create the results dictionary that contains the results, this dictionary # is returned from the function call results = get_pet_labels(in_arg.dir) # Check Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) # Create Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Check Results Dictionary using results. check_classifying_images(results) # Adjust the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify images as real animal (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Check the Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # Create the results statistics dictionary that contains a # summary of the results statistics (this includes counts & percentages). # Calculates results of run and add statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Check the Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # Print summary results, incorrect classifications of animals (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) # Measure total program runtime by collecting end time end_time = time() # Compute overall runtime in seconds & prints it in hh:mm:ss format # calculate difference between end time and start time tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def main(): cmd_arg = get_input_args() model = load_checkpoint(cmd_arg.cp_dir, cmd_arg.learn_rate) device = device_sel(cmd_arg.predict_gpu) cat_to_name = load_mapping('cat_to_name.json') np_image = process_image(cmd_arg.img_path) top_p, labels = predict(model, np_image, cmd_arg.top_classes, cat_to_name) print(top_p, labels)
def main(): # Measures total program runtime by collecting start time start_time = time() # This function retrieves 3 Command Line Arguments as input from # the user running the program from a terminal window. in_arg = get_input_args() check_command_line_arguments(in_arg) # Define get_pet_labels function within the file get_pet_labels.py results = get_pet_labels(in_arg.dir) check_creating_pet_image_labels(results) # Creates Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) # Creates the results statistics dictionary that contains counts & percentages. results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() # Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(round( (tot_time % 3600) % 60))) final_results = """ *** ----------------------------------Project Results -------------------------------------------- *** Given our results, the "best" model architecture is VGG. It out-performed both of the other architectures when considering both objectives 1 and 2. ResNet did classify dog breeds better than AlexNet, but only VGG and AlexNet were able to classify "dogs" and "not-a-dog"with 100% accuracy. The model VGG was the one that was able to classify "dogs" and "not-a-dog" with 100% accuracy and had the best performance regarding breed classification with 93.3% accuracy. If short runtime is of essence, one could argue that ResNet is preferrable to VGG. The resulting loss in accuracy in the dog breed classification is only 3.6% (VGG: 93.3% and ResNet: 89.7%). There is also some loss in accuracy predicting not-a-dog images of 9.1% (VGG: 100% and ResNet: 90.9%). *** ---------------------------------------------------------------------------------------------- *** """ print(final_results)
def main(): in_arg = get_input_args() filename_list = listdir("pet_images/") results_dic = dict() for idx in range(0, len(filename_list), 1): if filename_list[idx] not in results_dic: results_dic[filename_list[idx]] = get_pet_label(filename_list[idx]) classify_images(in_arg.dir, results_dic, in_arg.arch) adjust_results4_isadog(results_dic, in_arg.dogfile) results_stats_dic = calculates_results_stats(results_dic) # print_dict(results_dic_output) print_results(results_dic, results_stats_dic, in_arg.arch, True, True)
def main(): in_arg = get_input_args() image_path = in_arg.dir #image_path = "flowers/test/100/image_07896.jpg" # Load the model with the loading function file_name = 'modelcheckpoint.pth' #saved_model model = load_model(file_name) probs, classes = predict(image_path, model) print(probs) print(classes)
def main(): in_arg = get_input_args() data = in_arg.data suff = in_arg.suff dir = in_arg.dir print('Data to load: ', data) print('Suffix to add: ', suff) print('Folder to save the file(s): ', dir) unlockpikepdf(data=in_arg.data, suff=in_arg.suff, dirtosave=in_arg.dir)
def main(): # start the timer and parse any args from the user start_time = time() in_arg = get_input_args() check_command_line_arguments(in_arg) # infer the correct answers from the image file names results = get_pet_labels(in_arg.dir) check_creating_pet_image_labels(results) # predict the result for each image, for the given architecture classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) # aggregate the results to dog not-dog level adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) # calculate accuracy and some other aggregate statistics results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) # print the final results for this architecture and stop the timer print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = (end_time - start_time) print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60))) import pickle with open("results_stats.pickle", "wb") as f: pickle.dump(results_stats, f) print() print() print("*******************************") print() print() print("{} {} {} {} {}".format( "Architecture", "Not a Dog", "Dog", "Breed", "Label")) print("{}{}{:.2f} {:.2f} {:.2f} {:.2f}".format( in_arg.arch, " " * (19 - len(in_arg.arch)), results_stats['pct_correct_notdogs'], results_stats['pct_correct_dogs'], results_stats['pct_correct_breed'], results_stats['pct_match'])) print() print() print("*******************************")
def main(): # Measures total program runtime by collecting start time start_time = time() # Use get_input_args() to retrieve 3 Command Line Arugments from user as input from # the user running the program from a terminal window. Returns # the collection of these command line arguments from the function call as # the variable in_arg in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # Initialize the results dictionary that contains the results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) # Create Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) # Adjust the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # Calculate results of run and puts statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # Print summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) # Measure total program runtime by collecting end time end_time = time() # Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print("\nTotal elasped time: " + formatted_time(tot_time))
def main(): # log start time start_time = time() # get input in_arg = get_input_args() # check input check_command_line_arguments(in_arg) # get pet labels results = get_pet_labels(in_arg.dir) # check pet labels check_creating_pet_image_labels(results) # classify images classify_images(in_arg.dir, results, in_arg.arch) # check image classification check_classifying_images(results) # adjust results for dogs adjust_results4_isadog(results, in_arg.dogfile) # check adjusted results for dogs check_classifying_labels_as_dogs(results) # get results stats results_stats = calculates_results_stats(results) # check results stats check_calculating_results(results, results_stats) # TODO 6: Define print_results function within the file print_results.py # Once the print_results function has been defined replace 'None' # in the function call with in_arg.arch Once you have done the # replacements your function call should look like this: # print_results(results, results_stats, in_arg.arch, True, True) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = end_time - start_time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(int((tot_time%3600)%60)) )
def main(): start_time = time() in_arg = get_input_args() check_command_line_arguments(in_arg) results = get_pet_labels(in_arg.dir) check_creating_pet_image_labels(results) classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) print_results(results, results_stats, in_arg.arch, True, True)
def main(): # Measures total program runtime by collecting start time start_time = time() # Creates & retrieves Command Line Arugments in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # Creates Pet Image Labels by creating a dictionary answers_dic = get_pet_labels(in_arg.dir) # Function that checks Pet Images Dictionary- answers_dic check_creating_pet_image_labels(answers_dic) # Creates Classifier Labels with classifier function, Compares Labels, # and creates a results dictionary result_dic = classify_images(in_arg.dir, answers_dic, in_arg.arch) # Function that checks Results Dictionary - result_dic check_classifying_images(result_dic) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(result_dic, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment- result_dic check_classifying_labels_as_dogs(result_dic) # Calculates results of run and puts statistics in results_stats_dic results_stats_dic = calculates_results_stats(result_dic) # Function that checks Results Stats Dictionary - results_stats_dic check_calculating_results(result_dic, results_stats_dic) # Prints summary results, incorrect classifications of dogs # and breeds if requested print_results(result_dic, results_stats_dic, in_arg.arch, True, True) # Measure total program runtime by collecting end time end_time = time() # Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def data_loading(): ''' Formats and loads image directories. ''' in_arg = get_input_args() data_dir = in_arg.dir train_dir = data_dir + '/train' valid_dir = data_dir + '/valid' test_dir = data_dir + '/test' # Transforms for the training, validation, and testing sets train_transforms = transforms.Compose([ transforms.RandomRotation(45), transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) test_transforms = transforms.Compose([ transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) validation_transforms = transforms.Compose([ transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) #Load datasets with ImageFolder train_data = datasets.ImageFolder(train_dir, transform=train_transforms) test_data = datasets.ImageFolder(test_dir, transform=test_transforms) validation_data = datasets.ImageFolder(valid_dir, transform=validation_transforms) #Define the dataloaders trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True) testloader = torch.utils.data.DataLoader(test_data, batch_size=64) validationloader = torch.utils.data.DataLoader(validation_data, batch_size=64) return trainloader, testloader, validationloader, train_data, test_transforms
def main(): start_time = time() # start_time from time module (0) #1 in_arg = get_input_args() #read command line args, using argparse mod(1) check_command_line_arguments(in_arg) #prints command line arguments (1) #2 results = get_pet_labels( in_arg.dir ) #creates dictionary with key: file_name & value: [pet_label] check_creating_pet_image_labels(results) # prints 10 of key value pairs #3 classify_images(in_arg.dir, results, in_arg.arch) check_classifying_images(results) adjust_results4_isadog(results, in_arg.dogfile) check_classifying_labels_as_dogs(results) results_stats = calculates_results_stats(results) check_calculating_results(results, results_stats) print_results(results, results_stats, in_arg.arch, True, True) #python check_images.py --dir pet_images/ --arch resnet --dogfile dognames.txt --> 6 seconds, but pct_correct_dogs is wrong #python check_images.py --dir pet_images/ --arch alexnet --dogfile dognames.txt --> 3 seconds #python check_images.py --dir pet_images/ --arch vgg --dogfile dognames.txt --> 37 seconds end_time = time() tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60))) table1 = PrettyTable() table1.field_names = [ "# Total Images", "# Dog Images", "# Not-a-Dog Images" ] table1.add_row([40, 30, 10]) print(table1) print("\n\n\n") table2 = PrettyTable() table2.field_names = [ "CNN Model Architecture: ", "% Not-a-Dog Correct", "% Dogs Corrects", "% Breeds Correct", "% Match Labels", "Runtime (seconds)" ] table2.add_row(["ResNet", "90%", "100%", "90%", "82.5%", 6]) table2.add_row(["AlexNet", "100%", "100%", "80%", "75%", 3]) table2.add_row(["VGG", "100%", "100%", "93.3%", "87.5%", 35]) print(table2) print( "The model VGG was the one that was able to classify 'dogs' and 'not-a-dog' with 100% accuracy and had the best performance regarding breed classification with 93.3% accuracy. The Model AlexNet was the most efficient with the fastest runtime at only 3 seconds but still images 100% accuracy for identifying dogs correctly" )
def prediction(): ''' Function takes in image as command line argument and will print out it's predicted class (or classes) using a trained deep learning model. ''' in_arg = get_input_args() device = torch.device("cuda" if torch.cuda.is_available() and in_arg.gpu == "gpu" else "cpu") print("Loading model...\n") model, optimizer, criterion = create_model(device) load_checkpoint(model, optimizer) trainloader, testloader, validationloader, train_data, test_transforms = data_loading() probabilities, classes = predict(model, train_data, device) print("\nTop {} prediction/s for input image are as follows:\n".format(in_arg.top_k)) print_outcome(probabilities, classes, model, train_data, device)
def main(): # Record the start time of main function start_time = time() # Get input arguments in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # Creates the results dictionary that contains the results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) # Creates Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # Calculates results of run and puts statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) # Record the end time of main function end_time = time() # Calculate the total runtime in seconds & prints it in hh:mm:ss format tot_time = end_time - start_time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(int((tot_time%3600)%60)) )
def main(): in_arg = get_input_args() first_filename_list = listdir("pet_images/") filename_list = [] for idx in range(0, len(first_filename_list), 1): if not first_filename_list[idx].startswith('.'): filename_list.append(first_filename_list[idx]) results_dic = dict() for idx in range(0, len(filename_list), 1): if filename_list[idx] not in results_dic: results_dic[filename_list[idx]] = get_pet_label(filename_list[idx]) classify_images(in_arg.dir, results_dic, in_arg.arch) print_dict(results_dic)
def main(): cmd_arg = get_input_args() load_mapping('cat_to_name.json') image_datasets, dataloaders = image_load() device = device_sel(cmd_arg.gpu) model, criterion, optimizer = architecture(cmd_arg.cnn, cmd_arg.hidden_layer, cmd_arg.gpu, cmd_arg.learn_rate) train_eval(device, model, cmd_arg.number_epochs, optimizer, criterion, cmd_arg.learn_rate, dataloaders['train'], dataloaders['valid']) test_loop(model, device, dataloaders['test']) save_cpt(model, image_datasets['test'], cmd_arg.cnn, cmd_arg.hidden_layer, cmd_arg.learn_rate, cmd_arg.number_epochs, optimizer, cmd_arg.cp_dir)
def main(): start_time = time() # This function retrieves 3 Command Line Arugments from user as input from # the user running the program from a terminal window. in_arg = get_input_args() #Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) #Creates the results dictionary that contains the results results = get_pet_labels(in_arg.dir) #Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) #Creates Classifier Labels with classifier function, Compares Labels, #and adds these results to the results dictionary classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) #Adjusts the results dictionary to determine if classifier correctly classified images as 'a dog' or 'not a dog'. adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # Calculates results of run and puts statistics in the Results Statistics # Dictionary results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg.arch, True, True) end_time = time() tot_time = end_time - start_time print( "\n** Total Elapsed Runtime:", str(int((tot_time / 3600))) + ":" + str(int( (tot_time % 3600) / 60)) + ":" + str(int((tot_time % 3600) % 60)))
def create_model(device): ''' Creates a classifier network and appends to selected architecture. ''' in_arg = get_input_args() arch = in_arg.arch # Allows for a choice of 3 model architectures if in_arg.arch == 'resnet50': model = models.resnet50(pretrained=True) elif in_arg.arch == 'alexnet': model = models.alexnet(pretrained=True) elif in_arg.arch == 'densenet121': model = models.densenet121(pretrained=True) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Freezing parameters for model so as to not # backpropagate through them for param in model.parameters(): param.requires_grad = False in_features = {'resnet50': 2048, 'alexnet': 9216, 'densenet121': 1024} my_classifier = nn.Sequential( OrderedDict([('fc1', nn.Linear(in_features[in_arg.arch], in_arg.hidden_units)), ('relu', nn.ReLU()), ('dropout', nn.Dropout(0.2)), ('fc2', nn.Linear(in_arg.hidden_units, 102)), ('output', nn.LogSoftmax(dim=1))])) # Replaces classifier in each architecture with my_classifier if in_arg.arch == 'resnet50': model.fc = my_classifier elif in_arg.arch == 'alexnet': model.classifier = my_classifier elif in_arg.arch == 'densenet121': model.classifier = my_classifier # Error function to use criterion = nn.NLLLoss() #Get optimizer ready for backprop against classifier parameters only optimizer = optim.Adam(my_classifier.parameters(), in_arg.lr) model.to(device) my_classifier.to(device) return model, optimizer, criterion
def main(): # TODO 0: Measures total program runtime by collecting start time start_time = time() # TODO 1: Define get_input_args function within the file get_input_args.py # This function retrieves 3 Command Line Arugments from user as input from # the user running the program from a terminal window. This function returns # the collection of these command line arguments from the function call as # the variable in_arg in_arg = get_input_args() # Function that checks command line arguments using in_arg check_command_line_arguments(in_arg) # TODO 2: Define get_pet_labels function within the file get_pet_labels.py # Once the get_pet_labels function has been defined replace 'None' # in the function call with in_arg.dir Once you have done the replacements # your function call should look like this: # get_pet_labels(in_arg.dir) # This function creates the results dictionary that contains the results, # this dictionary is returned from the function call as the variable results results = get_pet_labels(in_arg.dir) # Function that checks Pet Images in the results Dictionary using results check_creating_pet_image_labels(results) # TODO 3: Define classify_images function within the file classiy_images.py # Once the classify_images function has been defined replace first 'None' # in the function call with in_arg.dir and replace the last 'None' in the # function call with in_arg.arch Once you have done the replacements your # function call should look like this: # classify_images(in_arg.dir, results, in_arg.arch) # Creates Classifier Labels with classifier function, Compares Labels, # and adds these results to the results dictionary - results classify_images(in_arg.dir, results, in_arg.arch) # Function that checks Results Dictionary using results check_classifying_images(results) # TODO 4: Define adjust_results4_isadog function within the file adjust_results4_isadog.py # Once the adjust_results4_isadog function has been defined replace 'None' # in the function call with in_arg.dogfile Once you have done the # replacements your function call should look like this: # adjust_results4_isadog(results, in_arg.dogfile) # Adjusts the results dictionary to determine if classifier correctly # classified images as 'a dog' or 'not a dog'. This demonstrates if # model can correctly classify dog images as dogs (regardless of breed) adjust_results4_isadog(results, in_arg.dogfile) # Function that checks Results Dictionary for is-a-dog adjustment using results check_classifying_labels_as_dogs(results) # TODO 5: Define calculates_results_stats function within the file calculates_results_stats.py # This function creates the results statistics dictionary that contains a # summary of the results statistics (this includes counts & percentages). This # dictionary is returned from the function call as the variable results_stats # Calculates results of run and puts statistics in the Results Statistics # Dictionary - called results_stats results_stats = calculates_results_stats(results) # Function that checks Results Statistics Dictionary using results_stats check_calculating_results(results, results_stats) # TODO 6: Define print_results function within the file print_results.py # Once the print_results function has been defined replace 'None' # in the function call with in_arg.arch Once you have done the # replacements your function call should look like this: # print_results(results, results_stats, in_arg.arch, True, True) # Prints summary results, incorrect classifications of dogs (if requested) # and incorrectly classified breeds (if requested) print_results(results, results_stats, in_arg, True, True) # TODO 0: Measure total program runtime by collecting end time end_time = time() # TODO 0: Computes overall runtime in seconds & prints it in hh:mm:ss format tot_time = start_time + end_time#calculate difference between end time and start time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(int((tot_time%3600)%60)) )