-
Notifications
You must be signed in to change notification settings - Fork 1
/
resnet.py
129 lines (105 loc) · 4.38 KB
/
resnet.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
from read_dataset import *
train_dataloader, test_dataloader = prepare_data(batch_size=16)
n_epochs = 15
print_every = 10
valid_loss_min = np.Inf
val_loss = []
val_acc = []
train_loss = []
train_acc = []
total_step = len(train_dataloader)
use_cuda = torch.cuda.is_available()
device = torch.device('cuda:0' if use_cuda else 'cpu')
net = models.wide_resnet50_2(pretrained=True)
net = net.cuda() if use_cuda else net
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.0005, momentum=0.9)
num_ftrs = net.fc.in_features
net.fc = nn.Linear(num_ftrs, 256)
net.fc = net.fc.cuda() if use_cuda else net.fc
def train():
global valid_loss_min
for epoch in range(1, n_epochs+1):
running_loss = 0.0
correct = 0
total=0
print(f'Epoch {epoch}\n')
for batch_idx, (data_, target_) in enumerate(train_dataloader):
data_, target_ = data_.to(device), target_.to(device)
optimizer.zero_grad()
outputs = net(data_)
target_ = target_.reshape(-1)
loss = criterion(outputs, target_)
loss.backward()
optimizer.step()
running_loss += loss.item()
_,pred = torch.max(outputs, dim=1)
correct += torch.sum(pred==target_).item()
total += target_.size(0)
if (batch_idx) % 20 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch, n_epochs, batch_idx, total_step, loss.item()))
train_acc.append(100 * correct / total)
train_loss.append(running_loss/total_step)
print(f'\ntrain-loss: {np.mean(train_loss):.4f}, train-acc: {(100 * correct/total):.4f}')
batch_loss = 0
total_t = 0
correct_t = 0
with torch.no_grad():
net.eval()
for data_t, target_t in (test_dataloader):
data_t, target_t = data_t.to(device), target_t.to(device)
outputs_t = net(data_t)
target_t = target_t.reshape(-1)
loss_t = criterion(outputs_t, target_t)
batch_loss += loss_t.item()
_,pred_t = torch.max(outputs_t, dim=1)
correct_t += torch.sum(pred_t==target_t).item()
total_t += target_t.size(0)
val_acc.append(100 * correct_t/total_t)
val_loss.append(batch_loss/len(test_dataloader))
network_learned = batch_loss < valid_loss_min
print(f'validation loss: {np.mean(val_loss):.4f}, validation acc: {(100 * correct_t/total_t):.4f}\n')
if network_learned:
valid_loss_min = batch_loss
torch.save(net.state_dict(), f'image_classifier_test-acc-{val_acc[-1]}.pt')
print('Improvement-Detected, save-model')
net.train()
def pytorch_predict_images(model, test_loader, device):
'''
Make prediction from a pytorch model
'''
# set model to evaluate model
model.eval()
y_true = torch.tensor([], dtype=torch.long, device=device)
all_outputs = torch.tensor([], device=device)
with torch.no_grad():
for data in test_loader:
inputs = [i.to(device) for i in data[:-1]]
labels = data[-1].to(device)
outputs = model(*inputs)
y_true = torch.cat((y_true, labels), 0)
all_outputs = torch.cat((all_outputs, outputs), 0)
y_true = y_true.cpu().numpy()
_, y_pred = torch.max(all_outputs, 1)
y_pred = y_pred.cpu().numpy()
y_pred_prob = F.softmax(all_outputs, dim=1).cpu().numpy()
return y_true, y_pred, y_pred_prob
def load_resnet(checkpoint='./wide_resnet.pt'):
net = models.wide_resnet50_2(pretrained=True)
net = net.cuda() if use_cuda else net
num_ftrs = net.fc.in_features
net.fc = nn.Linear(num_ftrs, 256)
net.fc = net.fc.cuda() if use_cuda else net.fc
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(), lr=0.0005)
if use_cuda:
trained_model = torch.load(checkpoint)
else:
trained_model = torch.load(checkpoint, map_location=torch.device('cpu'))
net.load_state_dict(trained_model)
_, test_dataloader_im = prepare_data(batch_size=16)
true_im, pred_im, prob_im = pytorch_predict_images(net, test_dataloader_im, device)
return true_im, pred_im, prob_im
if __name__ == "__main__":
train()