-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification.py
More file actions
152 lines (119 loc) · 3.91 KB
/
classification.py
File metadata and controls
152 lines (119 loc) · 3.91 KB
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
# Classifies cats and dogs
import os
import torch as T
from torch import nn
from torch import optim
from torch.nn import Conv2d, Linear, Module, ReLU, Softmax, Sigmoid, MaxPool2d, BatchNorm2d
from torchvision.datasets import ImageFolder
from torchvision import transforms
import numpy as np
import PIL.Image as im
from utils import dataset_dir, models_dir, img_load, img_show, stack_show, img_div, img_undiv
class Net(Module):
def __init__(self, im_width, im_height):
super().__init__()
self.im_width = im_width
self.im_height = im_height
self.flatten_size = 32 * 13 * 13
hidden_size = 1024
hidden_size2 = 256
self.conv1 = Conv2d(3, 16, 4)
self.conv2 = Conv2d(16, 24, 5)
self.conv3 = Conv2d(24, 32, 4)
self.norm1 = BatchNorm2d(16)
self.norm2 = BatchNorm2d(24)
self.norm3 = BatchNorm2d(32)
self.fc1 = Linear(self.flatten_size, hidden_size)
self.fc2 = Linear(hidden_size, hidden_size2)
self.fc3 = Linear(hidden_size2, 1)
def forward(self, x):
x = self.norm1(MaxPool2d(2)(ReLU(True)(self.conv1(x))))
x = self.norm2(MaxPool2d(2)(ReLU(True)(self.conv2(x))))
x = self.norm3(MaxPool2d(2)(ReLU(True)(self.conv3(x))))
x = x.view(-1, self.flatten_size)
x = ReLU(True)(self.fc1(x))
x = ReLU(True)(self.fc2(x))
x = Sigmoid()(self.fc3(x))
return x
# Params
im_width = 128
im_height = 128
epochs = 1
batch_size = 100
learning_rate = .0002
train_or_test = 'test'
path = models_dir + '/classification'
# Training device
device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
# Data
train_dataset_path = dataset_dir + '/cats_dogs/train'
test_dataset_path = dataset_dir + '/cats_dogs/test'
# Network
net = Net(im_width, im_height)
net.to(device)
# Load
if os.path.exists(path):
net.load_state_dict(T.load(path))
print('Model loaded')
if train_or_test == 'train':
# Dataset
dataset = ImageFolder(
root=train_dataset_path,
transform=transforms.Compose([transforms.Resize((im_width, im_height)), transforms.ToTensor()])
)
loader = T.utils.data.DataLoader(
dataset,
batch_size=batch_size,
num_workers=0,
shuffle=True
)
# Train
optim = T.optim.Adam(net.parameters(), lr=learning_rate, betas=(.9, .999))
criterion = nn.BCELoss()
for e in range(epochs):
avg_loss = 0
for i, data in enumerate(loader, 0):
# Data
inputs, labels = data
inputs = inputs.to(device)
labels = labels.to(device).to(T.float32).view(-1, 1)
# Zero the parameter gradients
optim.zero_grad()
# Predictions
y = net(inputs)
# Back prop
loss = criterion(y, labels)
loss.backward()
optim.step()
avg_loss += loss.item()
# Stats
print_freq = 10
if i % print_freq == print_freq - 1:
print(f'Epoch {e + 1:2d}, Batch {i + 1:5d}, Loss {avg_loss / print_freq:.3f}')
avg_loss = 0.0
# Save
T.save(net.state_dict(), path)
print('Model trained and saved')
else:
# Dataset
dataset = ImageFolder(
root=test_dataset_path,
transform=transforms.Compose([transforms.Resize((im_width, im_height)), transforms.ToTensor()])
)
loader = T.utils.data.DataLoader(
dataset,
batch_size=batch_size,
num_workers=0,
shuffle=False
)
# Test
ok = 0
with T.no_grad():
for i, (x, y) in enumerate(loader, 0):
x, y = x.to(device), y.to(device)
preds = net(x)
oks = [round(pred.detach().cpu().item()) == label for pred, label in zip(preds, y)]
for o in oks:
if o:
ok += 1
print(f'Accuracy : {ok / len(dataset) * 100:.1f} %')