Skip to content

Commit

Permalink
Merge pull request #6 from shashankboosi/deep_learning
Browse files Browse the repository at this point in the history
Deep learning
  • Loading branch information
shashankboosi authored Nov 1, 2019
2 parents 5835d0b + da4544d commit b67f990
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ coverage.xml
# Pycharm stuff:
.idea/
Images/IDRiD/
data/

# Django stuff:
*.log
Expand Down
Binary file added OutputImages/Deep_Learning/Accuracy_Table.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 OutputImages/Deep_Learning/lossvsepochs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# Machine-Vision
This repository consists of the algorithms related to Images processing, segmentation and Deep learning on Images


### Datasets:

1) **Deep Learning**: The dataset for the deep learning code is MNIST image dataset but in the tabular format where every tuple indicates an image.

Elements per row: ( 28 * 28 ) image with a label = 785 elements

`The dataset is highly visible online`
98 changes: 98 additions & 0 deletions src/deep_learning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import torch
import pandas as pd
from sklearn import preprocessing
import matplotlib.pyplot as plt

# Load data(do not change)
data = pd.read_csv("../data/mnist_train.csv")
train_data = data[:2000]
test_data = data[2000:2500]

# ----- Prepare Data ----- #
# step one: preparing your data including data normalization
train_X = train_data.iloc[:, 1:]
train_Y = train_data["label"]
test_X = test_data.iloc[:, 1:]
test_Y = test_data["label"]

min_max_scaler = preprocessing.MinMaxScaler()
train_X_data_norm = min_max_scaler.fit_transform(train_X)
test_X_data_norm = min_max_scaler.fit_transform(test_X)

# step two: transform np array to pytorch tensor
train_X_tensor = torch.tensor(train_X_data_norm, dtype=torch.float32).reshape(-1, 1, 28, 28)
test_X_tensor = torch.tensor(test_X_data_norm, dtype=torch.float32).reshape(-1, 1, 28, 28)
train_Y_tensor = torch.tensor(train_Y)
test_Y_tensor = torch.tensor(test_Y.to_numpy())


# ----- Build CNN Network ----- #
# Define your model here
class mymodel(torch.nn.Module):
def __init__(self):
super(mymodel, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = torch.nn.Dropout2d()
self.fc1 = torch.nn.Linear(320, 50)
self.fc2 = torch.nn.Linear(50, 10)

def forward(self, x):
x = torch.nn.functional.relu(torch.nn.functional.max_pool2d(self.conv1(x), 2))
x = torch.nn.functional.relu(torch.nn.functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = torch.nn.functional.relu(self.fc1(x))
x = torch.nn.functional.dropout(x, training=self.training)
x = self.fc2(x)
return torch.nn.functional.log_softmax(x, dim=1)


# Define our model
model = mymodel()
learning_rate = 0.01
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
criterion = torch.nn.NLLLoss()


# ----- Complete PlotLearningCurve function ----- #
def PlotLearningCurve(epoch, trainingloss, testingloss):
plt.plot(epoch, trainingloss, color='blue')
plt.plot(epoch, testingloss, color='red')
plt.legend(['Train Loss', 'Test Loss'], loc='upper right')
plt.title('Learning Curve')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig('../OutputImages/Deep_Learning/lossvsepochs.png')
plt.show()


# ----- Main Function ----- #
trainingloss = []
testingloss = []
# Define number of iterations
epochs = 100
for epoch in range(1, epochs + 1):
model.train()
# step one : fit your model by using training data and get predict label
output = model(train_X_tensor)
loss = criterion(output, train_Y_tensor)
optimizer.zero_grad()
loss.backward()
optimizer.step()
trainingloss += loss.item(),
# step seven: evaluation your model by using testing data and get the accuracy
correct = 0
with torch.no_grad():
total = 0
model.eval()
output_test = model(test_X_tensor)
loss = criterion(output_test, test_Y_tensor)
testingloss += loss.item(),
if epoch % 10 == 0:
_, predicted = torch.max(output_test, 1)
total += test_Y_tensor.size(0)
correct += (predicted == test_Y_tensor).sum().item()
acc = 100 * correct / total
print('Epoch:', epoch, 'Test Accuracy:', acc)

PlotLearningCurve(range(len(trainingloss)), trainingloss, testingloss)

0 comments on commit b67f990

Please sign in to comment.