目录
介绍对比学习中最流行的两个模型:SimCLR及MOCO。
1.SimCLR
1.1 SimCLR框架主要组成
SimCLR模型框架
从上图可以看出,SimCLR由以下几部分组成:
- 随机数据扩增模块,通过该模块生成positive pair:
和
;
- Base encoder f,文中采用resnet50(在average pooling layer之后作为输出);
- projection head g
- contrastive loss function (NT-Xent loss,the normalized temperature-scaled cross entropy loss))
1.2 算法流程及pytorch实现
算法完整流程如下:
pytorch实现 (来自https://github.com/leftthomas/SimCLR/blob/master/model.py):
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models.resnet import resnet50
class Model(nn.Module):
def __init__(self, feature_dim=128):
super(Model, self).__init__()
self.f = []
for name, module in resnet50().named_children():
if name == 'conv1':
module = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
if not isinstance(module, nn.Linear) and not isinstance(module, nn.MaxPool2d):
self.f.append(module)
# encoder
self.f = nn.Sequential(*self.f)
# projection head
self.g = nn.Sequential(nn.Linear(2048, 512, bias=False), nn.BatchNorm1d(512),
nn.ReLU(inplace=True), nn.Linear(5