forked from michaelwiest/ucsd-cse-253-hw-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem2.py
144 lines (120 loc) · 4.3 KB
/
problem2.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
from caltech import Caltech256
import torch.nn as nn
import torchvision.models as models
import torch.utils.model_zoo as model_zoo
import math
from torch.autograd import Variable
import torch.optim as optim
import numpy as np
import pdb
import torchvision
import torchvision.transforms as transforms
import torch
use_gpu = torch.cuda.is_available()
vgg = models.vgg16(pretrained=True)
for param in vgg.parameters():
# make all parameters untrainiable except last
param.requires_gradient = False
features_in = vgg.classifier._modules['6'].in_features
softmax_model = nn.Sequential(nn.Linear(features_in,256),nn.Softmax())
# vgg.classifier._modules['6'] = softmax_model
vgg.fc = softmax_model
vgg.fc.reguires_gradient = True
if use_gpu:
vgg.cuda()
example_transform = transforms.Compose(
[
transforms.Scale((224,224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
caltech256_train = Caltech256("256_ObjectCategories",
example_transform, train=True)
pdb.set_trace()
train_data = torch.utils.data.DataLoader(
dataset = caltech256_train,
batch_size = 32,
shuffle = True,
num_workers = 4)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(vgg.parameters(), lr=0.001, momentum=0.9)
rl_epoch=[]
rl_epoch_test=[]
acc_test=[]
acc_train_vec=[]
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
total_train = 0.0
correct_train = 0.0
for i, data in enumerate(train_data, 0):
# get the inputs
inputs, labels = data
labels_orig = labels.long()
labels = labels.long()
# wrap them in Variable
if use_gpu:
labels_orig = labels_orig.cuda()
inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
else:
inputs, labels = Variable(inputs), Variable(labels)
# inputs, labels = Variable(inputs.cuda()), Variable(labels.cuda())
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = vgg(inputs)
loss = criterion(outputs, torch.max(labels, 1)[1])
loss.backward()
optimizer.step()
_, predicted_train = torch.max(outputs.data, 1)
total_train += labels_orig.size(0)
correct_train += (predicted_train == labels_orig).sum()
# print statistics
running_loss += loss.data[0]
if i % 2000 == 1999: # print every 2000 mini-batches
print('Completed 2000 Minibatches')
print('Running Loss'+str(running_loss))
print('Accuracy' + str((100 * float(correct_train) / total_train)))
#rl_vec.append(running_loss)
#running_loss = 0.0
acc_train = (100 * float(correct_train) / total_train)
correct = 0.0
total = 0.0
running_loss_test = 0.0
for data_test in test_data:
images, labels_test = data
labels_test = labels_test.long()
if use_gpu:
labels_test_orig = labels_test.cuda()
images, labels_test = Variable(images.cuda()),Variable(labels_test.cuda())
else:
labels_test_orig = labels_test
images, labels_test = Variable(images),Variable(labels_test)
outputs_test = vgg(images)
_, predicted_test = torch.max(outputs_test.data, 1)
loss_test = criterion(outputs_test, torch.max(labels_test, 1)[1])
running_loss_test += loss_test.data[0]
total += labels_test_orig.size(0)
correct += (predicted_test == labels_test_orig).sum()
acc = (100 * float(correct) / total)
print('Accuracy of the network on the test images:' + str(
100 * correct / total))
print('Completed an Epoch')
rl_epoch.append(running_loss / float(total_train))
rl_epoch_test.append(running_loss_test / float(total))
acc_test.append(acc)
acc_train_vec.append(acc_train)
fh = open('test_acc.txt', 'a')
fh.write(str(acc_test[-1]))
fh.close
fh = open('test_loss.txt', 'a')
fh.write(str(rl_epoch_test[-1]))
fh.close
fh = open('train_acc.txt', 'a')
fh.write(str(acc_train_vec[-1]))
fh.close
fh = open('train_loss.txt', 'a')
fh.write(str(rl_epoch[-1]))
fh.close
print('Test Accuracy:' + str(acc_test))
print('Train Loss: ' + str(rl_epoch))