|
import torch |
|
import torch.nn as nn |
|
import torch.nn.functional as F |
|
|
|
import cliport.utils.utils as utils |
|
|
|
from cliport.models.resnet import ConvBlock, IdentityBlock |
|
from torchvision.models import resnet18, resnet34, resnet50 |
|
|
|
class PretrainedResNet18(nn.Module): |
|
def __init__(self, input_shape, output_dim, cfg, device, preprocess): |
|
super(PretrainedResNet18, self).__init__() |
|
self.input_shape = input_shape |
|
self.input_dim = input_shape[-1] |
|
self.output_dim = output_dim |
|
self.cfg = cfg |
|
self.device = device |
|
self.batchnorm = self.cfg['train']['batchnorm'] |
|
self.preprocess = preprocess |
|
self.pretrained_model = resnet18(pretrained=True) |
|
self.pretrained_model.avgpool = nn.Identity() |
|
self.pretrained_model.fc = nn.Identity() |
|
|
|
self.pretrained_model.conv1 = nn.Conv2d(self.input_dim, 64, kernel_size=2, stride=1, padding=3, bias=False) |
|
|
|
for param in self.pretrained_model.parameters(): |
|
param.requires_grad = False |
|
self.pretrained_model.conv1.weight.requires_grad = True |
|
|
|
self._make_layers() |
|
|
|
def _make_layers(self): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.layer7 = nn.Sequential( |
|
ConvBlock(512, [256, 256, 256], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
IdentityBlock(256, [256, 256, 256], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
nn.UpsamplingBilinear2d(scale_factor=2), |
|
) |
|
|
|
self.layer8 = nn.Sequential( |
|
ConvBlock(256, [128, 128, 128], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
IdentityBlock(128, [128, 128, 128], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
nn.UpsamplingBilinear2d(scale_factor=2), |
|
) |
|
|
|
self.layer9 = nn.Sequential( |
|
ConvBlock(128, [64, 64, 64], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
IdentityBlock(64, [64, 64, 64], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
nn.UpsamplingBilinear2d(scale_factor=2), |
|
) |
|
|
|
self.layer10 = nn.Sequential( |
|
ConvBlock(64, [32, 32, 32], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
IdentityBlock(32, [32, 32, 32], kernel_size=3, stride=1, batchnorm=self.batchnorm), |
|
nn.UpsamplingBilinear2d(scale_factor=2), |
|
) |
|
|
|
|
|
self.conv2 = nn.Sequential( |
|
ConvBlock(128, [16, 16, self.output_dim], kernel_size=3, stride=1, |
|
final_relu=False, batchnorm=self.batchnorm), |
|
IdentityBlock(self.output_dim, [16, 16, self.output_dim], kernel_size=3, stride=1, |
|
final_relu=False, batchnorm=self.batchnorm) |
|
) |
|
|
|
def forward(self, x): |
|
x = self.preprocess(x, dist='transporter') |
|
in_shape = x.shape |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = self.pretrained_model.conv1(x) |
|
for name, module in self.pretrained_model._modules.items(): |
|
if name == 'conv1': |
|
continue |
|
x = module(x) |
|
if name == 'layer4': |
|
break |
|
|
|
|
|
|
|
|
|
|
|
x = F.interpolate(x, size=(8, 8), mode='bilinear') |
|
|
|
|
|
im = [] |
|
for layer in [self.layer7, self.layer8, self.conv2]: |
|
im.append(x) |
|
x = layer(x) |
|
|
|
x = F.interpolate(x, size=(in_shape[-2], in_shape[-1]), mode='bilinear') |
|
return x, im |