-
Notifications
You must be signed in to change notification settings - Fork 4
/
CNN_Classification_Training.py
157 lines (128 loc) · 5.15 KB
/
CNN_Classification_Training.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms,datasets, models
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt
import copy
import time
use_gpu = torch.cuda.is_available()
if use_gpu:
pinMem = True
else:
pinMem = False
trainDir = 'train_5class'
valDir = 'test_5class'
apply_transform = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor()])
# Training dataloader
train_dataset = datasets.ImageFolder(trainDir,transform=apply_transform)
trainLoader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True,num_workers=4, pin_memory=pinMem)
# Test dataloader
test_dataset = datasets.ImageFolder(valDir,transform=apply_transform)
testLoader = torch.utils.data.DataLoader(test_dataset, batch_size=128, shuffle=False,num_workers=4, pin_memory=pinMem)
# Size of train and test datasets
print('No. of samples in train set: '+str(len(trainLoader.dataset)))
print('No. of samples in test set: '+str(len(testLoader.dataset)))
net = models.resnet18(pretrained=True)
print(net)
#params
totalParams = 0
for params in net.parameters():
print(params.size())
totalParams += np.sum(np.prod(params.size()))
print('Total number of parameters: '+str(totalParams))
net.fc = nn.Linear(512,101)
iterations = 10
trainLoss = []
trainAcc = []
testLoss = []
testAcc = []
start = time.time()
for epoch in range(iterations):
epochStart = time.time()
runningLoss = 0.0
avgTotalLoss = 0.0
running_correct = 0
net.train(True) # For training
batchNum = 1
for data in trainLoader:
inputs,labels = data
# Wrap them in Variable
if use_gpu:
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
running_correct += (predicted.cpu() == labels.data.cpu()).sum()
else:
inputs, labels = Variable(inputs), Variable(labels)
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
running_correct += (predicted == labels.data).sum()
# Initialize gradients to zero
optimizer.zero_grad()
# Compute loss/error
loss = criterion(F.log_softmax(outputs), labels)
# Backpropagate loss and compute gradients
loss.backward()
# Update the network parameters
optimizer.step()
# Accumulate loss per batch
runningLoss += loss.item()
batchNum += 1
avgTrainAcc = running_correct/float(len(trainLoader.dataset))
avgTrainLoss = runningLoss/float(len(trainLoader.dataset))
trainAcc.append(avgTrainAcc)
trainLoss.append(avgTrainLoss)
# Evaluating performance on test set for each epoch
net.train(False) # For testing [Affects batch-norm and dropout layers (if any)]
running_correct = 0
for data in testLoader:
inputs,labels = data
# Wrap them in Variable
if use_gpu:
inputs, labels= Variable(inputs.cuda()), Variable(labels.cuda())
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
running_correct += (predicted.cpu() == labels.data.cpu()).sum()
else:
inputs, labels = Variable(inputs), Variable(labels)
# Model 1
outputs = net(inputs)
_, predicted = torch.max(outputs.data, 1)
running_correct += (predicted == labels.data).sum()
loss = criterion(F.log_softmax(outputs), labels)
runningLoss += loss.item()
avgTestLoss = runningLoss/float(len(testLoader.dataset))
avgTestAcc = running_correct/float(len(testLoader.dataset))
testAcc.append(avgTestAcc)
testLoss.append(avgTestLoss)
# Plotting training loss vs Epochs
fig1 = plt.figure(1)
plt.plot(range(epoch+1),trainLoss,'r-',label='train')
plt.plot(range(epoch+1),testLoss,'g-',label='test')
if epoch==0:
plt.legend(loc='upper left')
plt.xlabel('Epochs')
plt.ylabel('Loss')
# Plotting testing accuracy vs Epochs
fig2 = plt.figure(2)
plt.plot(range(epoch+1),trainAcc,'r-',label='train')
plt.plot(range(epoch+1),testAcc,'g-',label='test')
if epoch==0:
plt.legend(loc='upper left')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
epochEnd = time.time()-epochStart
print('Iteration: {:.0f} /{:.0f}; Training Loss: {:.6f} ; Training Acc: {:.3f}'\
.format(epoch + 1,iterations,avgTrainLoss,avgTrainAcc*100))
print('Iteration: {:.0f} /{:.0f}; Testing Loss: {:.6f} ; Testing Acc: {:.3f}'\
.format(epoch + 1,iterations,avgTestLoss,avgTestAcc*100))
print('Time consumed: {:.0f}m {:.0f}s'.format(epochEnd//60,epochEnd%60))
end = time.time()-start
print('Training completed in {:.0f}m {:.0f}s'.format(end//60,end%60))
torch.save(net.state_dict(), 'resnet18Pre_fcOnly5class_ucf101_10adam_1e-4_b128.pt')