import pandas import numpy as np import matplotlib.pyplot as plt import torch import torch.nn as nn import torch.optim as optim from google.colab import drive drive.mount('/content/gdrive') # Your answer goes here. Please make sure it is not cut off # Your code goes here. Make sure it does not get cut off # You can use the code below to help you get started. You're welcome to modify # the code or remove it entirely: it's just here so that you don't get stuck # reading files import glob path = "/content/gdrive/My Drive/CSC321/data/train/*.jpg" # edit me images = {} for file in glob.glob(path): filename = file.split("/")[-1] # get the name of the .jpg file img = plt.imread(file) # read the image as a numpy array images[filename] = img[:, :, :3] # remove the alpha channel # Run this code, include the image in your PDF submission # plt.figure() # plt.imshow(train_data[4,0,0,:,:,:]) # left shoe of first pair submitted by 5th student # plt.figure() # plt.imshow(train_data[4,0,1,:,:,:]) # right shoe of first pair submitted by 5th student # plt.figure() # plt.imshow(train_data[4,1,1,:,:,:]) # right shoe of second pair submitted by 5th student # Your code goes here # Run this code, include the result with your PDF submission print(train_data.shape) # if this is [N, 3, 2, 224, 224, 3] print(generate_same_pair(train_data).shape) # should be [N*3, 448, 224, 3] plt.imshow(generate_same_pair(train_data)[0]) # should show 2 shoes from the same pair # Your code goes here # Run this code, include the result with your PDF submission print(train_data.shape) # if this is [N, 3, 2, 224, 224, 3] print(generate_different_pair(train_data).shape) # should be [N*3, 448, 224, 3] plt.imshow(generate_different_pair(train_data)[0]) # should show 2 shoes from different pairs # Your answer goes here. Please make sure it is not cut off # Your answer goes here. Please make sure it is not cut off class CNN(nn.Module): def __init__(self, n=4): super(CNN, self).__init__() # TODO: complete this method # TODO: complete this class class CNNChannel(nn.Module): def __init__(self, n=4): super(CNNChannel, self).__init__() # TODO: complete this method # TODO: complete this class # Your answer goes here. Please make sure it is not cut off # Your answer goes here. Please make sure it is not cut off # Your answer goes here. Please make sure it is not cut off def get_accuracy(model, data, batch_size=50): """Compute the model accuracy on the data set. This function returns two separate values: the model accuracy on the positive samples, and the model accuracy on the negative samples. Example Usage: >>> model = CNN() # create untrained model >>> pos_acc, neg_acc= get_accuracy(model, valid_data) >>> false_positive = 1 - pos_acc >>> false_negative = 1 - neg_acc """ model.eval() n = data.shape[0] data_pos = generate_same_pair(data) # should have shape [n * 3, 448, 224, 3] data_neg = generate_different_pair(data) # should have shape [n * 3, 448, 224, 3] pos_correct = 0 for i in range(0, len(data_pos), batch_size): xs = torch.Tensor(data_pos[i:i+batch_size]).transpose(1, 3) zs = model(xs) pred = zs.max(1, keepdim=True)[1] # get the index of the max logit pred = pred.detach().numpy() pos_correct += (pred == 1).sum() neg_correct = 0 for i in range(0, len(data_neg), batch_size): xs = torch.Tensor(data_neg[i:i+batch_size]).transpose(1, 3) zs = model(xs) pred = zs.max(1, keepdim=True)[1] # get the index of the max logit pred = pred.detach().numpy() neg_correct += (pred == 0).sum() return pos_correct / (n * 3), neg_correct / (n * 3) # Write your code here # Write your code here. Remember to include your results so that your TA can # see that your model attains a high training accuracy. (UPDATED March 12) # Include the training curves for the two models. # Include the training curves for the two models. # Write your code here. Make sure to include the test accuracy in your report