-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_densenet.py
244 lines (187 loc) · 7.64 KB
/
model_densenet.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
243
244
from __future__ import print_function, division
import sys
sys.path.append('../')
import os
import os.path
os.environ["CUDA_VISIBLE_DEVICES"]="0"
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
#import matplotlib.pyplot as plt
import time
import pandas as pd
from datetime import datetime
import torch.utils.data as data
import glob
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
arch = 'densnet121'
arch_model = models.densenet121
batch_size_num = 32
train_file_name = '../data/train_aug_8k.txt'
test_file_name = '../data/valid.txt'
learning_rate = 0.001
momentum = 0.9
step_size = 3
num_epochs = 12
data_transforms = {
'train': transforms.Compose([
transforms.Resize((224,224)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize( (224,224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
class Read_Dataset():
def __init__(self, file_path,transform=None):
self.data = pd.read_csv(file_path, header = None, sep = ' ')
self.label = self.data.iloc[:, 1].tolist()
self.img_path = self.data.iloc[:, 0].tolist()
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, index):
label = self.label[index]
img_path = self.img_path[index]
image = Image.open(img_path).convert('RGB')
if self.transform is not None:
image = self.transform(image)
return img_path, image, label
train_val_list = ['train' , 'val']
path_to_input_file = {
'train' : train_file_name ,
'val': test_file_name
}
# path_to_checkpoint = 'densenet121_'+str(learning_rate)
path_to_checkpoint = "../models/aug8k_{}_lr_{}".format(arch,learning_rate)
if not os.path.exists(path_to_checkpoint):
print("creating directory for checkpoint...")
os.makedirs(path_to_checkpoint)
image_datasets = {x: Read_Dataset( file_path = path_to_input_file[x], transform = data_transforms[x])
for x in train_val_list}
dataloders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size_num, shuffle=True, num_workers=4)
for x in train_val_list}
print ('Current time and date is: ' + str(datetime.now()) )
print ('train file is: ' + train_file_name )
dataset_sizes = {x: len(image_datasets[x]) for x in train_val_list}
print ('dataset_sizes: ' + str(dataset_sizes) )
use_gpu = torch.cuda.is_available()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("checking gpu")
print(use_gpu)
def train_model(model, criterion, optimizer, scheduler, num_epochs):
since = time.time()
best_model_wts = model.state_dict()
best_acc = 0.0
for epoch in range(num_epochs):
epoch_start = time.time()
print('Epoch {}/{}'.format(epoch, num_epochs - 1))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in train_val_list:
if phase == 'train':
scheduler.step()
model.train(True) # Set model to training mode
else:
model.train(False) # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for i, data in enumerate(dataloders[phase]):
# get the inputs
iamge_path, inputs, labels = data
# wrap them in Variable
# if use_gpu:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
outputs = model(inputs)
_, preds = torch.max(outputs.data, 1)
# print (type(outputs), type(labels))
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.data[0]
running_corrects += torch.sum(preds == labels.data)
epoch_loss = running_loss / dataset_sizes[phase]
ver = torch.__version__
if '0.3' in ver:
epoch_acc = running_corrects / dataset_sizes[phase]
else:
epoch_acc = running_corrects.cpu().numpy() / dataset_sizes[phase]
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
best_model_wts = model.state_dict()
save_checkpoint(epoch,{
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_acc': epoch_acc,
'optimizer' : optimizer.state_dict(),
})
epoch_end = time.time() - epoch_start
print('{}th epoch training completed in {:.0f}m {:.0f}s'.format(epoch,
epoch_end // 60, epoch_end % 60))
print()
time_elapsed = time.time() - since
print('Entire Training completed in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best val Acc: {:4f}'.format(best_acc))
# load best model weights
model.load_state_dict(best_model_wts)
return model
def save_checkpoint(epoch, state, path_to_checkpoint = path_to_checkpoint, filename = 'batch'+ str(batch_size_num) + '_ckpt.pth.tar'):
filepath = os.path.join(path_to_checkpoint, "epoch_" + str(epoch) + "_" + filename)
torch.save(state, filepath)
# number of classes in train dataset
df = pd.read_csv(path_to_input_file['train'], header = None, sep =' ')
df.columns = ['path', 'label']
n_classes = df['label'].nunique()
print ('number of classes in train dataset:' + str(n_classes) )
# number of classes in test dataset
df = pd.read_csv(path_to_input_file['val'], header = None, sep =' ')
df.columns = ['path', 'label']
n_classes_test = df['label'].nunique()
print ('number of classes in test dataset:' + str(n_classes_test) )
print ('using densenet121 model architecture...')
model_ft = arch_model(pretrained=True)
# densenet121(pretrained=True)
num_ftrs = model_ft.classifier.in_features
model_ft.classifier = nn.Linear(num_ftrs, n_classes)
# picking latest checkpoint
modelPath = path_to_checkpoint + '/*'
print (modelPath)
list_of_files = glob.glob(modelPath)
if len(list_of_files) > 0:
model_file = max(list_of_files, key=os.path.getctime)
print("picking model file : "+str(model_file))
checkpoint = torch.load(model_file)
epoch = checkpoint['epoch']
best_acc = checkpoint['best_acc']
model_ft.load_state_dict(checkpoint['state_dict'])
model_ft = model_ft.cuda()
criterion = nn.CrossEntropyLoss()
# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(model_ft.parameters(), lr=learning_rate, momentum=momentum)
# Decay LR by a factor of 0.1 every 6 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=step_size, gamma=0.1)
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,
num_epochs=num_epochs)