def save_acc(self, dataloader, x_value, split="train"): """ Calculate the accuracy on the testset given with DataLoader object dataloader Arguments: dataloader - DataLoader object containing the dataset x_value - corresponding x_value to be written to list split - train or test """ accuracy = evaluate(self, dataloader, use_gpu=self.use_gpu) if split == "train": self.list_train_acc.append([x_value, accuracy]) else: self.list_test_acc.append([x_value, accuracy]) return None
tqdm.write("CUDA is available: " + str(use_gpu)) # Model creation and training model = CNN_STN(43, use_gpu=use_gpu) #model.use_gpu=use_gpu logger = Logger() model.databatch = next(iter(dataloader_train))["tensor"].cuda() train(model, dataloader_train, n_epochs=30, checkpoint_name="test", use_gpu=use_gpu, stn=True, dataloader_test=dataloader_test, logger=logger) print("Train accuracy: " + str(evaluate(model, dataloader_train))) print("Test accuracy: " + str(evaluate(model, dataloader_test))) # Plot the results of STN side-by-side tensor = model.databatch.cpu() original = convert_image_np( torchvision.utils.make_grid(tensor.narrow(1, 0, 3)).cpu()) fig = plt.figure(figsize=(8, 5), dpi=100) fig.add_subplot(2, 3, 1) plt.imshow(original) plt.xticks([]), plt.yticks([]) plt.title('Dataset Images', fontsize=9) epoch_list = [6, 9, 12, 18, 29]
Images.append(temp) #Load our pretrained CNN-Model model, logger, _ = import_classifier() # Construct dataset and dataloader with Images evaluationset = Evalset(Images) dataloader = DataLoader(evaluationset, batch_size=32, shuffle=True) use_gpu = torch.cuda.is_available() if not use_gpu: print("Warning! No CUDA available. This is untested!") if use_gpu: model.cuda() # Calculate the accuracy of the pretrained model on the data from the SVM accuracy = evaluate(model, dataloader, use_gpu=use_gpu) # Visualize the STN on sample images of SVM output if use_gpu: model.databatch = next(iter(dataloader))["tensor"].cuda() else: model.databatch = next(iter(dataloader))["tensor"].cuda() visualize_stn(model, filename="./Classifier/Plots/stn_gtsdb_output.pdf") print( "For the GTSDB dataset (full) we obtain a total accuracy of the STN+CNN classifier of : " + str(accuracy))