-
Notifications
You must be signed in to change notification settings - Fork 14
/
PyTorch.py
242 lines (189 loc) · 8.33 KB
/
PyTorch.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Importing Modules
import os
import random
from tqdm import tqdm
import numpy as np
from PIL import Image
import torch
from torch import nn
from torch import optim
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
from matplotlib import pyplot as plt
# Device Configuration
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Set randomness
seed = 777
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set hyperparameter
epochs= 5
batch_size= 32
img_size= 192
# Dataset
class FlowerDataset(Dataset):
def __init__(self, data_dir, transform):
IMG_FORMAT = ["jpg", "jpeg", "bmp", "png", "tif", "tiff"]
self.filelist = []
self.classes = sorted(os.listdir(data_dir))
for root, _, files in os.walk(data_dir):
if not len(files): continue
files = [os.path.join(root, file) for file in files if file.split(".")[-1] in IMG_FORMAT]
self.filelist += files
# self.filelist = self.filelist[:64]
self.transform = transform
def __len__(self):
# return size of dataset
return len(self.filelist)
def __getitem__(self, idx):
image = Image.open(self.filelist[idx]).convert("RGB")
image = self.transform(image)
label = self.filelist[idx].split('/')[-2]
label = self.classes.index(label)
return image, label
transform = transforms.Compose([
transforms.Resize((img_size, img_size)), transforms.ToTensor()
])
train_dataset = FlowerDataset(os.path.join("../../../data/flower_photos/train"), transform)
val_dataset = FlowerDataset(os.path.join("../../../data/flower_photos/validation"), transform)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
# Defining Model
class ConvBlock(nn.Module):
def __init__(self, input_feature, output_feature, ksize=3, strides=1, padding=1):
super(ConvBlock, self).__init__()
self.block = nn.Sequential(
nn.Conv2d(input_feature, output_feature, ksize, strides, padding),
nn.BatchNorm2d(output_feature),
nn.ReLU6(True)
)
def forward(self, x):
return self.block(x)
class Depthwise_Separable_Block(nn.Module):
def __init__(self, input_feature, output_feature, ksize=3, strides=1, padding=1, alpha=1):
super(Depthwise_Separable_Block, self).__init__()
self.block = nn.Sequential(
nn.Conv2d(input_feature, input_feature, ksize, strides, padding, groups=input_feature),
nn.BatchNorm2d(input_feature),
nn.ReLU6(True),
nn.Conv2d(input_feature, int(output_feature*alpha), 1),
nn.BatchNorm2d(int(output_feature*alpha)),
nn.ReLU6(True)
)
def forward(self, x):
return self.block(x)
class Inverted_Residual_Block(nn.Module):
def __init__(self, input_feature, expansion, output_feature, strides=1, alpha=1):
super(Inverted_Residual_Block, self).__init__()
self.stride = strides
self.intermediate_featrue = int(input_feature*expansion)
self.output_feature = output_feature
self.alpha = alpha
self.block = nn.Sequential(
ConvBlock(input_feature, self.intermediate_featrue, 1, 1, 0),
Depthwise_Separable_Block(self.intermediate_featrue, self.output_feature, 3, strides, 1, self.alpha)
)
def forward(self, x):
output = self.block(x)
if self.stride==1 and self.intermediate_featrue == int(self.output_feature*self.alpha):
return x + output
return output
class MobileNetV2(nn.Sequential):
def __init__(self, input_channel=3, num_classes=1000, alpha=1):
super(MobileNetV2, self).__init__()
self.Stem = ConvBlock(input_channel, 32, 3, 2, 1)
layer_list = []
layer_list.append(Inverted_Residual_Block(32, 1, 16, 1, 1))
layer_list.append(Inverted_Residual_Block(16, 6, 24, 2, 1))
layer_list.append(Inverted_Residual_Block(24, 6, 24, 1, 1))
layer_list.append(Inverted_Residual_Block(24, 6, 32, 2, 1))
layer_list.append(Inverted_Residual_Block(32, 6, 32, 1, 1))
layer_list.append(Inverted_Residual_Block(32, 6, 32, 1, 1))
layer_list.append(Inverted_Residual_Block(32, 6, 64, 2, 1))
layer_list.append(Inverted_Residual_Block(64, 6, 64, 1, 1))
layer_list.append(Inverted_Residual_Block(64, 6, 64, 1, 1))
layer_list.append(Inverted_Residual_Block(64, 6, 64, 1, 1))
layer_list.append(Inverted_Residual_Block(64, 6, 96, 1, 1))
layer_list.append(Inverted_Residual_Block(96, 6, 96, 1, 1))
layer_list.append(Inverted_Residual_Block(96, 6, 96, 1, 1))
layer_list.append(Inverted_Residual_Block(96, 6, 160, 2, 1))
layer_list.append(Inverted_Residual_Block(160, 6, 160, 1, 1))
layer_list.append(Inverted_Residual_Block(160, 6, 160, 1, 1))
layer_list.append(Inverted_Residual_Block(160, 6, 320, 1, 1))
self.Main_Block = nn.Sequential(*layer_list)
self.Exit = ConvBlock(320, 1280, 1, 1, 0)
self.Classifier = nn.Sequential(
nn.AdaptiveAvgPool2d((1,1)),
nn.Flatten(),
nn.Linear(1280, num_classes)
)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight)
nn.init.constant_(m.bias, 0)
def forward(self, x):
x = self.Stem(x)
x = self.Main_Block(x)
x = self.Exit(x)
x = self.Classifier(x)
return x
model = MobileNetV2(input_channel=3, num_classes=5, alpha=1).to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
# Training
for epoch in range(epochs):
model.train()
avg_loss = 0
avg_acc = 0
with tqdm(total=len(train_loader)) as t:
t.set_description(f'[{epoch+1}/{epochs}]')
total = 0
correct = 0
for i, (batch_img, batch_lab) in enumerate(train_loader):
X = batch_img.to(device)
Y = batch_lab.to(device)
y_pred = model.forward(X)
loss = criterion(y_pred, Y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss += loss.item()
_, predicted = torch.max(y_pred.data, 1)
total += Y.size(0)
correct += (predicted == Y).sum().item()
t.set_postfix({"loss": f"{avg_loss/(i+1):05.3f}"})
t.update()
acc = (100 * correct / total)
model.eval()
with tqdm(total=len(val_loader)) as t:
t.set_description(f'[{epoch+1}/{epochs}]')
with torch.no_grad():
val_loss = 0
total = 0
correct = 0
for i, (batch_img, batch_lab) in enumerate(val_loader):
X = batch_img.to(device)
Y = batch_lab.to(device)
y_pred = model(X)
val_loss += criterion(y_pred, Y)
_, predicted = torch.max(y_pred.data, 1)
total += Y.size(0)
correct += (predicted == Y).sum().item()
t.set_postfix({"val_loss": f"{val_loss.item()/(i+1):05.3f}"})
t.update()
val_loss /= len(val_loader)
val_acc = (100 * correct / total)
print(f"Epoch : {epoch+1}, Loss : {(avg_loss/len(train_loader)):.3f}, Acc: {acc:.3f}, Val Loss : {val_loss.item():.3f}, Val Acc : {val_acc:.3f}\n")
print("Training Done !")