SimCLR/train.py

141 lines
4.8 KiB
Python
Raw Normal View History

2020-02-18 03:05:44 +08:00
import torch
2020-02-20 10:27:27 +08:00
import yaml
2020-02-18 10:17:10 +08:00
print(torch.__version__)
2020-02-18 03:05:44 +08:00
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision import datasets
2020-02-18 10:17:10 +08:00
from torch.utils.tensorboard import SummaryWriter
import torch.nn.functional as F
2020-02-18 03:05:44 +08:00
from model import ResNet18
2020-02-18 03:05:44 +08:00
from utils import GaussianBlur
torch.manual_seed(0)
2020-02-20 10:27:27 +08:00
config = yaml.load(open("config.yaml", "r"), Loader=yaml.FullLoader)
2020-02-20 06:12:07 +08:00
batch_size = config['batch_size']
out_dim = config['out_dim']
s = config['s']
temperature = config['temperature']
use_cosine_similarity = config['use_cosine_similarity']
2020-02-18 03:05:44 +08:00
color_jitter = transforms.ColorJitter(0.8 * s, 0.8 * s, 0.8 * s, 0.2 * s)
data_augment = transforms.Compose([transforms.ToPILImage(),
transforms.RandomResizedCrop(96),
transforms.RandomHorizontalFlip(),
transforms.RandomApply([color_jitter], p=0.8),
transforms.RandomGrayscale(p=0.2),
GaussianBlur(),
transforms.ToTensor()])
train_dataset = datasets.STL10('data', split='train', download=True, transform=transforms.ToTensor())
2020-02-18 03:05:44 +08:00
train_loader = DataLoader(train_dataset, batch_size=batch_size, num_workers=1, drop_last=True, shuffle=True)
# model = Encoder(out_dim=out_dim)
model = ResNet18(out_dim=out_dim)
2020-02-18 03:05:44 +08:00
print(model)
2020-02-18 10:17:10 +08:00
train_gpu = torch.cuda.is_available()
2020-02-18 03:05:44 +08:00
print("Is gpu available:", train_gpu)
2020-02-18 03:05:44 +08:00
# moves the model paramemeters to gpu
if train_gpu:
model.cuda()
2020-02-20 10:27:27 +08:00
criterion = torch.nn.CrossEntropyLoss(reduction='sum')
2020-02-18 03:05:44 +08:00
optimizer = optim.Adam(model.parameters(), 3e-4)
2020-02-18 10:17:10 +08:00
train_writer = SummaryWriter()
if use_cosine_similarity:
similarity_dim1 = torch.nn.CosineSimilarity(dim=1)
similarity_dim2 = torch.nn.CosineSimilarity(dim=2)
2020-02-20 06:12:07 +08:00
# Mask to remove positive examples from the batch of negative samples
negative_mask = torch.ones((batch_size, 2 * batch_size), dtype=bool)
for i in range(batch_size):
negative_mask[i, i] = 0
negative_mask[i, i + batch_size] = 0
2020-02-18 10:17:10 +08:00
n_iter = 0
2020-02-20 06:12:07 +08:00
for e in range(config['epochs']):
2020-02-18 03:05:44 +08:00
for step, (batch_x, _) in enumerate(train_loader):
2020-02-20 06:42:45 +08:00
2020-02-18 03:05:44 +08:00
optimizer.zero_grad()
xis = []
xjs = []
for k in range(len(batch_x)):
xis.append(data_augment(batch_x[k]))
xjs.append(data_augment(batch_x[k]))
xis = torch.stack(xis)
xjs = torch.stack(xjs)
2020-02-20 06:12:07 +08:00
2020-02-18 10:17:10 +08:00
if train_gpu:
xis = xis.cuda()
xjs = xjs.cuda()
2020-02-18 03:05:44 +08:00
2020-02-18 10:17:10 +08:00
ris, zis = model(xis) # [N,C]
train_writer.add_histogram("xi_repr", ris, global_step=n_iter)
train_writer.add_histogram("xi_latent", zis, global_step=n_iter)
2020-02-18 03:05:44 +08:00
2020-02-18 10:17:10 +08:00
rjs, zjs = model(xjs) # [N,C]
train_writer.add_histogram("xj_repr", rjs, global_step=n_iter)
train_writer.add_histogram("xj_latent", zjs, global_step=n_iter)
2020-02-18 03:05:44 +08:00
# normalize projection feature vectors
zis = F.normalize(zis, dim=1)
zjs = F.normalize(zjs, dim=1)
2020-02-20 06:12:07 +08:00
# assert zis.shape == (batch_size, out_dim), "Shape not expected: " + str(zis.shape)
# assert zjs.shape == (batch_size, out_dim), "Shape not expected: " + str(zjs.shape)
2020-02-18 03:05:44 +08:00
# positive pairs
if use_cosine_similarity:
l_pos = similarity_dim1(zis.view(batch_size, out_dim), zjs.view(batch_size, out_dim)).view(batch_size, 1)
else:
l_pos = torch.bmm(zis.view(batch_size, 1, out_dim), zjs.view(batch_size, out_dim, 1)).view(batch_size, 1)
l_pos /= temperature
assert l_pos.shape == (batch_size, 1) # [N,1]
2020-02-20 06:12:07 +08:00
negatives = torch.cat([zjs, zis], dim=0)
loss = 0
for positives in [zis, zjs]:
if use_cosine_similarity:
negatives = negatives.view(1, (2 * batch_size), out_dim)
l_neg = similarity_dim2(positives.view(batch_size, 1, out_dim), negatives)
else:
l_neg = torch.tensordot(positives.view(batch_size, 1, out_dim),
negatives.T.view(1, out_dim, (2 * batch_size)),
dims=2)
labels = torch.zeros(batch_size, dtype=torch.long)
if train_gpu:
labels = labels.cuda()
2020-02-18 03:05:44 +08:00
2020-02-20 10:27:27 +08:00
l_neg = l_neg[negative_mask].view(l_neg.shape[0], -1)
l_neg /= temperature
assert l_neg.shape == (batch_size, 2 * (batch_size - 1)), "Shape of negatives not expected." + str(
l_neg.shape)
2020-02-20 10:27:27 +08:00
logits = torch.cat([l_pos, l_neg], dim=1) # [N,K+1]
loss += criterion(logits, labels)
loss = loss / (2 * batch_size)
2020-02-18 10:17:10 +08:00
train_writer.add_scalar('loss', loss, global_step=n_iter)
2020-02-18 03:05:44 +08:00
loss.backward()
optimizer.step()
2020-02-18 10:17:10 +08:00
n_iter += 1
# print("Step {}, Loss {}".format(step, loss))
2020-02-18 03:05:44 +08:00
torch.save(model.state_dict(), './model/checkpoint.pth')