-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
148 lines (112 loc) · 5.07 KB
/
main.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
import os
from PIL import Image
import torch
from torch.utils.data import Dataset, DataLoader
from torch import nn
from torchvision import transforms
class CustomImageDataset(Dataset):
def read_data_set(self):
all_img_files = []
all_labels = []
class_names = os.walk(self.data_set_path).__next__()[1]
for index, class_name in enumerate(class_names):
label = index
img_dir = os.path.join(self.data_set_path, class_name)
img_files = os.walk(img_dir).__next__()[2]
for img_file in img_files:
img_file = os.path.join(img_dir, img_file)
img = Image.open(img_file)
if img is not None:
all_img_files.append(img_file)
all_labels.append(label)
return all_img_files, all_labels, len(all_img_files), len(class_names)
def __init__(self, data_set_path, transforms=None):
self.data_set_path = data_set_path
self.image_files_path, self.labels, self.length, self.num_classes = self.read_data_set()
self.transforms = transforms
def __getitem__(self, index):
image = Image.open(self.image_files_path[index])
image = image.convert("RGB")
if self.transforms is not None:
image = self.transforms(image)
return {'image': image, 'label': self.labels[index]}
def __len__(self):
return self.length
class CustomConvNet(nn.Module):
def __init__(self, num_classes):
super(CustomConvNet, self).__init__()
self.layer1 = self.conv_module(3, 16)
self.layer2 = self.conv_module(16, 32)
self.layer3 = self.conv_module(32, 64)
self.layer4 = self.conv_module(64, 128)
self.layer5 = self.conv_module(128, 256)
self.gap = self.global_avg_pool(256, num_classes)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.layer5(out)
out = self.gap(out)
out = out.view(-1, num_classes)
return out
def conv_module(self, in_num, out_num):
return nn.Sequential(
nn.Conv2d(in_num, out_num, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_num),
nn.LeakyReLU(),
nn.MaxPool2d(kernel_size=2, stride=2))
def global_avg_pool(self, in_num, out_num):
return nn.Sequential(
nn.Conv2d(in_num, out_num, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_num),
nn.LeakyReLU(),
nn.AdaptiveAvgPool2d((1, 1)))
hyper_param_epoch = 20
hyper_param_batch = 8
hyper_param_learning_rate = 0.001
transforms_train = transforms.Compose([transforms.Resize((128, 128)),
transforms.RandomRotation(10.),
transforms.ToTensor()])
transforms_test = transforms.Compose([transforms.Resize((128, 128)),
transforms.ToTensor()])
train_data_set = CustomImageDataset(data_set_path="./data/train", transforms=transforms_train)
train_loader = DataLoader(train_data_set, batch_size=hyper_param_batch, shuffle=True)
test_data_set = CustomImageDataset(data_set_path="./data/test", transforms=transforms_test)
test_loader = DataLoader(test_data_set, batch_size=hyper_param_batch, shuffle=True)
if not (train_data_set.num_classes == test_data_set.num_classes):
print("error: Numbers of class in training set and test set are not equal")
exit()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
num_classes = train_data_set.num_classes
custom_model = CustomConvNet(num_classes=num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(custom_model.parameters(), lr=hyper_param_learning_rate)
for e in range(hyper_param_epoch):
for i_batch, item in enumerate(train_loader):
images = item['image'].to(device)
labels = item['label'].to(device)
# Forward pass
outputs = custom_model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i_batch + 1) % hyper_param_batch == 0:
print('Epoch [{}/{}], Loss: {:.4f}'
.format(e + 1, hyper_param_epoch, loss.item()))
# Test the model
custom_model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
correct = 0
total = 0
for item in test_loader:
images = item['image'].to(device)
labels = item['label'].to(device)
outputs = custom_model(images)
_, predicted = torch.max(outputs.data, 1)
total += len(labels)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the {} test images: {} %'.format(total, 100 * correct / total))