-
Notifications
You must be signed in to change notification settings - Fork 14
/
PyTorch.py
178 lines (142 loc) · 5.83 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
# 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 'mps' if torch.backends.mps.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= 16
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.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)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
# Defining Model
class build_vgg(nn.Module):
def __init__(self, input_channel= 3, num_classes=1000, num_layer=16):
super(build_vgg, self).__init__()
blocks_dict = {
11: [1, 1, 2, 2, 2],
13: [2, 2, 2, 2, 2],
16: [2, 2, 3, 3, 3],
19: [2, 2, 4, 4, 4]
}
num_channel_list = [64, 128, 256, 512, 512]
assert num_layer in blocks_dict.keys(), "Number of layer must be in %s"%blocks_dict.keys()
layer_list = []
input_features = input_channel
for idx, num_iter in enumerate(blocks_dict[num_layer]):
for _ in range(num_iter):
layer_list.append(nn.Conv2d(input_features, num_channel_list[idx], 3, padding=1))
layer_list.append(nn.ReLU(True))
input_features = num_channel_list[idx]
layer_list.append(nn.MaxPool2d(2, 2))
self.vgg = nn.Sequential(*layer_list)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.classifier = nn.Linear(512, 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.vgg(x)
x = self.avgpool(x)
x = nn.Flatten()(x)
x = self.classifier(x)
return x
model = build_vgg(input_channel=3, num_classes=len(train_dataset.classes), num_layer=13).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 !")