-
Notifications
You must be signed in to change notification settings - Fork 15
/
rotation.py
executable file
·147 lines (114 loc) · 5.21 KB
/
rotation.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
import random
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision.datasets import DatasetFolder
from torchvision.models.resnet import resnet50
from utils import (AverageMeter, Logger, Memory, ModelCheckpoint,
NoiseContrastiveEstimator, Progbar, pil_loader)
device = torch.device('cuda:2')
data_dir = '/media/dysk/datasets/isic_challenge_2017/train'
negative_nb = 1000 # number of negative examples in NCE
lr = 0.001
checkpoint_dir = 'rotation_models'
log_filename = 'pretraining_log_rotation'
class RotationLoader(DatasetFolder):
def __init__(self, root_dir):
super(RotationLoader, self).__init__(root_dir, pil_loader, extensions=('jpg'))
self.root_dir = root_dir
self.color_transform = torchvision.transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.4)
self.flips = [torchvision.transforms.RandomHorizontalFlip(), torchvision.transforms.RandomVerticalFlip()]
self.normalize = torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (sample, target) where target is class_index of the target class.
"""
path, _ = self.samples[index]
original = self.loader(path)
image = torchvision.transforms.Resize((300, 300))(original)
image = torchvision.transforms.RandomCrop((224, 224))(image)
rotation = torchvision.transforms.Resize((224, 224))(image)
# augmentation - collor jitter
image = self.color_transform(image)
rotation = self.color_transform(rotation)
# augmentation - flips
image = self.flips[0](image)
image = self.flips[1](image)
# augmentation - rotation
angles = [90, 180, 270]
angle = random.choice(angles)
rotation = torchvision.transforms.functional.rotate(rotation, angle)
# to tensor
image = torchvision.transforms.functional.to_tensor(image)
rotation = torchvision.transforms.functional.to_tensor(rotation)
# normalize
image = self.normalize(image)
rotation = self.normalize(rotation)
return {'original': image, 'rotation': rotation, 'index': index}
dataset = RotationLoader(data_dir)
train_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=32, num_workers=32)
class Network(nn.Module):
def __init__(self):
super(Network, self).__init__()
self.network = resnet50()
self.network = torch.nn.Sequential(*list(self.network.children())[:-1])
self.projection_original_features = nn.Linear(2048, 128)
def forward_once(self, x):
return self.network(x)
def return_reduced_image_features(self, original):
features = self.forward_once(original)
features = features.view(-1, 2048)
features = self.projection_original_features(features)
return features
def forward(self, images=None, rotation=None, mode=0):
'''
mode 0: get 128d feature for image,
mode 1: get 128d feature for image and rotation
'''
if mode == 0:
return self.return_reduced_image_features(images)
if mode == 1:
image_features = self.return_reduced_image_features(images)
rotation_features = self.return_reduced_image_features(rotation)
return image_features, rotation_features
net = Network().to(device)
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9)
memory = Memory(size=len(dataset), weight=0.5, device=device)
memory.initialize(net, train_loader)
checkpoint = ModelCheckpoint(mode='min', directory=checkpoint_dir)
noise_contrastive_estimator = NoiseContrastiveEstimator(device)
logger = Logger(log_filename)
loss_weight = 0.5
for epoch in range(1000):
print('\nEpoch: {}'.format(epoch))
memory.update_weighted_count()
train_loss = AverageMeter('train_loss')
bar = Progbar(len(train_loader), stateful_metrics=['train_loss', 'valid_loss'])
for step, batch in enumerate(train_loader):
# prepare batch
images = batch['original'].to(device)
rotation = batch['rotation'].to(device)
index = batch['index']
representations = memory.return_representations(index).to(device).detach()
# zero grad
optimizer.zero_grad()
#forward, loss, backward, step
output = net(images=images, rotation=rotation, mode=1)
loss_1 = noise_contrastive_estimator(representations, output[1], index, memory, negative_nb=negative_nb)
loss_2 = noise_contrastive_estimator(representations, output[0], index, memory, negative_nb=negative_nb)
loss = loss_weight * loss_1 + (1 - loss_weight) * loss_2
loss.backward()
optimizer.step()
# update representation memory
memory.update(index, output[0].detach().cpu().numpy())
# update metric and bar
train_loss.update(loss.item(), images.shape[0])
bar.update(step, values=[('train_loss', train_loss.return_avg())])
logger.update(epoch, train_loss.return_avg())
# save model if improved
checkpoint.save_model(net, train_loss.return_avg(), epoch)