Skip to content
This repository was archived by the owner on Sep 13, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Accuracy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data_batch_1
Binary file not shown.
Binary file added data_batch_2
Binary file not shown.
Binary file added data_batch_3
Binary file not shown.
Binary file added data_batch_4
Binary file not shown.
Binary file added data_batch_5
Binary file not shown.
Binary file added graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,116 @@

# Import whatever libraries/modules you need

from cProfile import label
from cgi import test
from unittest import result
import matplotlib.pyplot as plt
import numpy as np
import PIL
import pandas as pd
import torch
from torch import nn
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import matplotlib.pyplot as plt

# Your working code here

# Unpack data
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict

# Put data into dataframe
dicts = [];
for i in range(1,6):
dicts.append(unpickle("data_batch_"+str(i)))
dataFrame = pd.DataFrame(columns=['labels','data'])
for i in range(5):
temp = pd.DataFrame([dicts[i][b'labels'],dicts[i][b'data']])
temp = temp.transpose()
temp.columns=['labels','data']
dataFrame = pd.concat([dataFrame,temp], ignore_index = True)

# Change data into tensor form
labelTensor = torch.tensor(dataFrame['labels'].values, dtype = torch.uint8)
dataTensor = torch.tensor(dataFrame['data'].values, dtype = torch.uint8)
dataTensor = torch.div(dataTensor,255)
print(dataTensor)

# Construct neural network
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.conv1 = nn.Conv2d(3, 3, 3)
self.Flatten = nn.Flatten()
self.pool = nn.MaxPool2d(2, 1)
self.conv2 = nn.Conv2d(3, 1, 3)
self.linearReLUStack = nn.Sequential(
nn.Flatten(),
nn.Linear(22*22,512),
nn.ReLU(),
)
def forward(self,x):
logits = torch.reshape(x, (32,32,3))
logits = logits.permute(2,0,1)
logits = self.conv1(logits)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like your using convolutions without pooling in between, as well as not adding enough convolutiopns for the image features to be extracted. Normally, A typical CNN looks like: Cnv2D -> MaxPool -> Conv2D -> Max Pool -> Flatten -> Dense layers with activation -> final softmax or sigmoid layer. I would try (1) adding max pooling (I see it in the class initialization but not the forward() method), and (2) tweaking the CNN parameters as having only 1 dim in the argument (e.g for your second convolution ) will not do much! Here is a pretty helpful article going over CNN architecture https://medium.com/thecyphy/train-cnn-model-with-pytorch-21dafb918f48 with sample code, you don't need to follow this - it just has some helpful tips about how a typical CNN should look like et cetera!

logits = self.conv1(logits)
logits = self.pool(logits)
logits = self.conv1(logits)
logits = self.conv2(logits)
logits = self.pool(logits)
logits = self.linearReLUStack(logits)
logits = torch.squeeze(logits)
return logits
network = NeuralNetwork()
loss_fn = torch.nn.CrossEntropyLoss()
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(network.parameters(), lr = 0.0001, momentum = 0.9)

# Train
trainLoss = []
valLoss = []
for epoch in range(16):
print('Epoch ' + str(epoch))
runningLoss = 0.0
runningValLoss = 0.0
for i in range(40000):
# 40000
optimizer.zero_grad()
outputs = network(dataTensor[i])
# print(outputs.size())
loss = criterion(outputs, labelTensor[i])
loss.backward()
optimizer.step()
runningLoss += loss.item()
if(i%2000 == 1999):
print("Running loss: " + str(runningLoss/(i+1)))
runningLoss = runningLoss/40000
trainLoss.append(runningLoss)
for i in range(10000):
# 10000
valOut = network(dataTensor[i+40000])
loss = criterion(valOut,labelTensor[i+40000])
runningValLoss += loss.item()
runningValLoss = runningValLoss/10000
valLoss.append(runningValLoss)
print(trainLoss)
print(valLoss)

correct = 0
for i in range(10000):
result = network(dataTensor[i+40000])
print(torch.argmax(result).item())
print(labelTensor.data[i+40000].item())
if(torch.argmax(result).item() == labelTensor.data[i+40000].item()):
correct += 1

print('Correct: ' + str(correct) + ' (' + str(correct/100.0) + '%) out of 10000')
plt.plot(trainLoss, label = "Training Loss")
plt.plot(valLoss, label = "Value Loss")
plt.legend()
plt.show()