-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpo_distinct.py
204 lines (170 loc) · 6.67 KB
/
hpo_distinct.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
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.models as models
import torchvision.transforms as transforms
from torch.utils.data import Dataset
import time
import os
import pandas as pd
from PIL import ImageFile, Image
ImageFile.LOAD_TRUNCATED_IMAGES = True #disable image truncated error
import argparse
class GTSRBDataset(Dataset):
def __init__(self, annotations_file, img_dir, transform=None):
self.img_labels = pd.read_csv(annotations_file)
self.img_dir = img_dir
self.transform = transform
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_labels.loc[idx, 'Path'])
image =Image.open(img_path)
label = self.img_labels. loc[idx, 'ClassId']
if self.transform:
image = self.transform(image)
return image, label
def test(model, test_loader, criterion, device):
print("Testing Model on Whole Testing Dataset")
model.eval()
running_loss = 0
running_corrects = 0
for inputs, labels in test_loader:
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data).item()
total_loss = running_loss / len(test_loader.dataset)
total_acc = running_corrects / len(test_loader.dataset)
print(f"Test set: Average loss: {total_loss}, Accuracy: {100 * total_acc}")
def train(model, train_loader, validation_loader, criterion, optimizer, device, num_epochs):
epochs = num_epochs
best_loss = 1e6
image_dataset = {'train': train_loader, 'valid': validation_loader}
loss_counter = 0
for epoch in range(epochs):
for phase in ['train', 'valid']:
print(f"Epoch {epoch}, Phase {phase}")
if phase == 'train':
model.train()
else:
model.eval()
running_loss = 0.0
running_corrects = 0
running_samples = 0
for step, (inputs, labels) in enumerate(image_dataset[phase]):
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
if phase == 'train':
optimizer.zero_grad()
loss.backward()
optimizer.step()
_, preds = torch.max(outputs, 1)
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data).item()
running_samples += len(inputs)
if running_samples % 2000 == 0:
accuracy = running_corrects / running_samples
print("Train set: [{}/{} ({:.0f}%)] Loss: {:.2f} Accuracy: {}/{} ({:.2f}%) Time: {}".format(
running_samples,
len(image_dataset[phase].dataset),
100.0 * (running_samples / len(image_dataset[phase].dataset)),
loss.item(),
running_corrects,
running_samples,
100.0 * accuracy,
time.asctime()
)
)
epoch_loss = running_loss / running_samples
epoch_acc = running_corrects / running_samples
if phase == 'valid':
if epoch_loss < best_loss:
best_loss = epoch_loss
else:
loss_counter += 1
if loss_counter == 1:
break
return model
def net():
model = models.resnet50(pretrained=True)
for param in model.parameters():
param.requires_grad = False
num_features=model.fc.in_features
model.fc = nn.Sequential(
nn.Linear(num_features, 128),
nn.ReLU(inplace=True),
nn.Linear(128, 43))
return model
def create_data_loaders(data, labels_file, batch_size):
transform = transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(),
])
data = GTSRBDataset(annotations_file = labels_file, img_dir=data, transform = transform)
data_loader = torch.utils.data.DataLoader(data, batch_size=batch_size,
shuffle=True)
return data_loader
def model_fn(model_dir):
model = net(os.environ["NUM_CLASSES"])
with open(os.path.join(model_dir, "model.pth"), "rb") as f:
model.load_state_dict(torch.load(f))
return model
def main(args):
os.environ["SM_CHANNEL_TRAIN"]
os.environ["SM_CHANNEL_VALID"]
os.environ["SM_CHANNEL_TEST"]
# Create loaders
train_loader = create_data_loaders(os.environ["SM_CHANNEL_TRAIN"], os.environ["SM_CHANNEL_TRAIN"] + "/gt_43.csv", args.batch_size)
validation_loader = create_data_loaders(os.environ["SM_CHANNEL_VALID"], os.environ["SM_CHANNEL_VALID"] + "/gt_43.csv", args.batch_size)
test_loader = create_data_loaders(os.environ["SM_CHANNEL_TEST"], os.environ["SM_CHANNEL_TEST"] + "/gt_43.csv", args.batch_size)
'''
Initialize a model by calling the net function
'''
model=net()
model = model.to(device)
'''
Create loss and optimizer
'''
loss_criterion = nn.CrossEntropyLoss()
optimizer = optim.Adadelta(model.fc.parameters(), lr=args.lr)
'''
Call the train function to start training model
'''
model=train(model, train_loader, validation_loader, loss_criterion, optimizer, device, num_epochs = args.num_epochs)
'''
Test the model to see its accuracy
'''
test(model, test_loader, loss_criterion, device)
'''
Save the trained model
'''
torch.save(model.state_dict(), os.path.join(args.model_dir, "model.pth"))
if __name__=='__main__':
parser=argparse.ArgumentParser()
'''
Specify all the hyperparameters
'''
parser.add_argument(
"--lr", type = float, metavar="LR", help="learning rate (default: 0.01)"
)
parser.add_argument(
"--batch-size", type = int, metavar="N",
help="input batch size for training"
)
parser.add_argument(
"--num-epochs", type = int, metavar="E",
help="Number of epochs"
)
parser.add_argument("--model_dir", type = str, default=os.environ["SM_MODEL_DIR"])
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print(f"Running on Device {device}")
args=parser.parse_args()
main(args)