-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkeep_training.py
76 lines (49 loc) · 1.83 KB
/
keep_training.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
from utils import ConvNet
from utils import CocoCaptions
import utils
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import pickle
### Hyperparameters
NUM_EPOCHS = utils.NUM_EPOCHS
BATCH_SIZE = utils.BATCH_SIZE
LR = utils.LR
NUM_CLASSES = utils.NUM_CLASSES
train_dir = 'resized_train2017/'
ann_file_train = 'annotations/captions_train2017.json'
train_dataset = CocoCaptions(root=train_dir, annFile=ann_file_train, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=BATCH_SIZE,
shuffle=True)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = ConvNet(NUM_CLASSES).to(device)
model.load_state_dict(torch.load('model.ckpt'))
### Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LR)
### Train the model
loss_log = []
total_step = len(train_loader)
for epoch in range(NUM_EPOCHS):
losses_for_epoch = []
for i, (image_id, images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(images)
loss = 0
for j in range(labels.shape[1]):
loss += criterion(outputs, labels[:,j])
losses_for_epoch.append(loss)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{} / {}], Step [{} / {}], Loss: {}'.format(epoch+1, NUM_EPOCHS, i+1, total_step, loss.item()))
loss_log.append(losses_for_epoch)
### Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
with open('loss_log.pkl', 'wb') as f:
pickle.dump(loss_log, f)