Created
June 5, 2019 19:01
-
-
Save prigoyal/057a4e3aec30c05446c6970b13f4ec49 to your computer and use it in GitHub Desktop.
res3 Taskonomy taskonomyencoder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import os | |
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
import torchvision.models as models | |
import warnings | |
TASKONOMY_TASKS = "autoencoder class_1000 class_places curvature denoise edge2d edge3d inpainting_whole jigsaw keypoint2d keypoint3d reshade rgb2depth rgb2sfnorm rgb2mist room_layout segment25d segment2d segmentsemantic vanishing_point".split() | |
class TaskonomyEncoder(nn.Module): | |
def __init__(self, normalize_outputs=True, eval_only=True): | |
self.inplanes = 64 | |
super(TaskonomyEncoder, self).__init__() | |
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) | |
self.bn1 = nn.BatchNorm2d(64) | |
self.relu = nn.ReLU(inplace=True) | |
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=0) | |
block = models.resnet.Bottleneck | |
layers = [3,4,6,3] | |
self.layer1 = self._make_layer(block, 64, layers[0], stride=2) | |
self.layer2 = self._make_layer(block, 128, layers[1], stride=2) | |
# self.layer3 = self._make_layer(block, 256, layers[2]) | |
# self.layer4 = self._make_layer(block, 512, layers[3]) | |
self.compress1 = nn.Conv2d(512, 8, kernel_size=3, stride=1, padding=1, bias=False) | |
# self.compress1 = nn.Conv2d(2048, 8, kernel_size=3, stride=1, padding=1, bias=False) | |
# self.compress_bn = nn.BatchNorm2d(8) | |
self.relu1 = nn.ReLU(inplace=True) | |
self.groupnorm = nn.GroupNorm(8, 8, affine=False) | |
self.normalize_outputs = normalize_outputs | |
self.eval_only = eval_only | |
if self.eval_only: | |
self.eval() | |
for p in self.parameters(): | |
p.requires_grad = False | |
def _make_layer(self, block, planes, blocks, stride=1): | |
downsample = None | |
layers = [] | |
if self.inplanes != planes * block.expansion: | |
downsample = nn.Sequential( | |
nn.Conv2d(self.inplanes, planes * block.expansion, | |
kernel_size=1, stride=1, bias=False), | |
nn.BatchNorm2d(planes * block.expansion), | |
) | |
layers.append(block(self.inplanes, planes, downsample=downsample)) | |
self.inplanes = planes * block.expansion | |
for i in range(1, blocks - 1): | |
layers.append(block(self.inplanes, planes)) | |
downsample = None | |
if stride != 1: | |
downsample = nn.Sequential( | |
nn.MaxPool2d( kernel_size=1, stride=stride ), | |
) | |
layers.append(block(self.inplanes, planes, stride, downsample)) | |
return nn.Sequential(*layers) | |
def forward(self, x): | |
x = self.conv1(x) | |
x = self.bn1(x) | |
x = self.relu(x) | |
x = F.pad(x, (0,1,0,1), 'constant', 0) | |
x = self.maxpool(x) | |
x = self.layer1(x) | |
x = self.layer2(x) | |
# x = self.layer3(x) | |
# x = self.layer4(x) | |
x = self.compress1(x) | |
# x = self.compress_bn(x) | |
x = self.relu1(x) | |
if self.normalize_outputs: | |
x = self.groupnorm(x) | |
return x | |
def train(self, val): | |
if val and self.eval_only: | |
warnings.warn("Ignoring 'train()' in TaskonomyEncoder since 'eval_only' was set during initialization.", RuntimeWarning) | |
else: | |
return super().train(val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment