forked from xungeer29/Remote-Sensing-Image-Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
195 lines (167 loc) · 7.68 KB
/
train.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
# -*- coding:utf-8 -*-
import sys
sys.path.append('/home/gfx/Projects/remote_sensing_image_classification')
import os, argparse, time
import numpy as np
import cv2
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision import transforms, models
from torch.nn.parallel.data_parallel import data_parallel
import torchvision
import torch.backends.cudnn as cudnn
import torch.nn.functional as F
from dataset.dataset import *
from networks.network import *
from networks.lr_schedule import *
from metrics.metric import *
from utils.plot import *
from config import config
def train():
# model
if config.model == 'ResNet18':
backbone = models.resnet18(pretrained=True)
model = ResNet18(backbone, num_classes=config.num_classes)
elif config.model == 'ResNet34':
backbone = models.resnet34(pretrained=True)
model = ResNet34(backbone, num_classes=config.num_classes)
elif config.model == 'ResNet50':
backbone = models.resnet50(pretrained=True)
model = ResNet50(backbone, num_classes=config.num_classes)
elif config.model == 'ResNet101':
backbone = models.resnet101(pretrained=True)
model = ResNet101(backbone, num_classes=config.num_classes)
elif config.model == 'ResNet152':
backbone = models.resnet152(pretrained=True)
model = ResNet152(backbone, num_classes=config.num_classes)
else:
print('ERROR: No model {}!!!'.format(config.model))
print model
# model = torch.nn.DataParallel(model)
model.cuda()
# freeze layers
if config.freeze:
for p in model.backbone.layer1.parameters(): p.requires_grad = False
for p in model.backbone.layer2.parameters(): p.requires_grad = False
for p in model.backbone.layer3.parameters(): p.requires_grad = False
# for p in model.backbone.layer4.parameters(): p.requires_grad = False
# loss
criterion = nn.CrossEntropyLoss().cuda()
# train data
transform = transforms.Compose([transforms.Scale(256),
transforms.RandomSizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(0.05, 0.05, 0.05),
transforms.RandomRotation(10),
transforms.Resize((config.width, config.height)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
dst_train = RSDataset('./data/train.txt', width=config.width,
height=config.height, transform=transform)
dataloader_train = DataLoader(dst_train, shuffle=True, batch_size=config.batch_size, num_workers=config.num_workers)
# validation data
transform = transforms.Compose([transforms.Resize((config.width, config.height)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
dst_valid = RSDataset('./data/valid.txt', width=config.width,
height=config.height, transform=transform)
dataloader_valid = DataLoader(dst_valid, shuffle=False, batch_size=config.batch_size/2, num_workers=config.num_workers)
# log
if not os.path.exists('./log'):
os.makedirs('./log')
log = open('./log/log.txt', 'a')
log.write('-'*30+'\n')
log.write('model:{}\nnum_classes:{}\nnum_epoch:{}\nlearning_rate:{}\nim_width:{}\nim_height:{}\niter_smooth:{}\n'.format(
config.model, config.num_classes, config.num_epochs, config.lr,
config.width, config.height, config.iter_smooth))
# load checkpoint
if config.resume:
model = torch.load(os.path.join('./checkpoints', config.checkpoint))
# train
sum = 0
train_loss_sum = 0
train_top1_sum = 0
max_val_acc = 0
train_draw_acc = []
val_draw_acc = []
for epoch in range(config.num_epochs):
ep_start = time.time()
# adjust lr
# lr = half_lr(config.lr, epoch)
lr = step_lr(epoch)
# optimizer
# optimizer = torch.optim.Adam(model.parameters(), lr=lr, betas=(0.9, 0.999), weight_decay=0.0002)
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()),
lr=lr, betas=(0.9, 0.999), weight_decay=0.0002)
model.train()
top1_sum = 0
for i, (ims, label) in enumerate(dataloader_train):
input = Variable(ims).cuda()
target = Variable(label).cuda().long()
output = model(input)
loss = criterion(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
top1 = accuracy(output.data, target.data, topk=(1,))
train_loss_sum += loss.data.cpu().numpy()
train_top1_sum += top1[0]
sum += 1
top1_sum += top1[0]
if (i+1) % config.iter_smooth == 0:
print('Epoch [%d/%d], Iter [%d/%d], lr: %f, Loss: %.4f, top1: %.4f'
%(epoch+1, config.num_epochs, i+1, len(dst_train)//config.batch_size,
lr, train_loss_sum/sum, train_top1_sum/sum))
log.write('Epoch [%d/%d], Iter [%d/%d], lr: %f, Loss: %.4f, top1: %.4f\n'
%(epoch+1, config.num_epochs, i+1, len(dst_train)//config.batch_size,
lr, train_loss_sum/sum, train_top1_sum/sum))
sum = 0
train_loss_sum = 0
train_top1_sum = 0
train_draw_acc.append(top1_sum/len(dataloader_train))
epoch_time = (time.time() - ep_start) / 60.
if epoch % 1 == 0 and epoch < config.num_epochs:
# eval
val_time_start = time.time()
val_loss, val_top1 = eval(model, dataloader_valid, criterion)
val_draw_acc.append(val_top1)
val_time = (time.time() - val_time_start) / 60.
print('Epoch [%d/%d], Val_Loss: %.4f, Val_top1: %.4f, val_time: %.4f s'
%(epoch+1, config.num_epochs, val_loss, val_top1, val_time*60))
print('epoch time: {}s'.format(epoch_time*60))
if val_top1[0].data > max_val_acc:
max_val_acc = val_top1[0].data
print('Taking snapshot...')
if not os.path.exists('./checkpoints'):
os.makedirs('./checkpoints')
torch.save(model, '{}/{}.pth'.format('checkpoints', config.model))
log.write('Epoch [%d/%d], Val_Loss: %.4f, Val_top1: %.4f, val_time: %.4f s\n'
%(epoch+1, config.num_epochs, val_loss, val_top1, val_time*60))
draw_curve(train_draw_acc, val_draw_acc)
log.write('-'*30+'\n')
log.close()
# validation
def eval(model, dataloader_valid, criterion):
sum = 0
val_loss_sum = 0
val_top1_sum = 0
model.eval()
for ims, label in dataloader_valid:
input_val = Variable(ims).cuda()
target_val = Variable(label).cuda()
output_val = model(input_val)
loss = criterion(output_val, target_val)
top1_val = accuracy(output_val.data, target_val.data, topk=(1,))
sum += 1
val_loss_sum += loss.data.cpu().numpy()
val_top1_sum += top1_val[0]
avg_loss = val_loss_sum / sum
avg_top1 = val_top1_sum / sum
return avg_loss, avg_top1
if __name__ == '__main__':
train()