Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it torchScript Compilable #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
from collections import OrderedDict
from typing import Dict , Optional, Type, Any, Tuple
import math

import torch
import torch.nn as nn
import torch.nn.functional as F


def conv3x3(in_planes, out_planes, stride=1, dilation=1):
def conv3x3(in_planes: int, out_planes: int, stride: int = 1, dilation: int = 1) -> nn.Conv2d:
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation, bias=False)


class Bottleneck(nn.Module):
expansion = 4
expansion: int = 4

def __init__(self, inplanes, planes, stride=1, downsample=None, dilation=1):
def __init__(self, inplanes: int, planes: int, stride: int = 1,
downsample: Optional[nn.Module] = None, dilation: int = 1) -> None:
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, dilation=dilation,
padding=dilation, bias=False)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
dilation=dilation, padding=dilation, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride

def forward(self, x):
def forward(self, x: torch.Tensor) -> torch.Tensor:
residual = x

out = self.conv1(x)
Expand All @@ -51,7 +52,8 @@ def forward(self, x):


class ResNet(nn.Module):
def __init__(self, block, layers=(3, 4, 23, 3)):
def __init__(self, block: Type[nn.Module], layers: Tuple[int, int, int, int] = (3, 4, 23, 3)):

self.inplanes = 64
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(6, 64, kernel_size=7, stride=2, padding=3,
Expand All @@ -72,7 +74,8 @@ def __init__(self, block, layers=(3, 4, 23, 3)):
m.weight.data.fill_(1)
m.bias.data.zero_()

def _make_layer(self, block, planes, blocks, stride=1, dilation=1):
def _make_layer(self, block: Type[nn.Module], planes: int, blocks: int, stride: int = 1, dilation: int = 1) -> nn.Sequential:

downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
Expand All @@ -88,7 +91,7 @@ def _make_layer(self, block, planes, blocks, stride=1, dilation=1):

return nn.Sequential(*layers)

def forward(self, x):
def forward(self, x: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
x_1 = self.conv1(x) # /2
x = self.bn1(x_1)
x = self.relu(x)
Expand All @@ -105,4 +108,3 @@ def forward(self, x):
def resnet50():
model = ResNet(Bottleneck, [3, 4, 6, 3])
return model

Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
import torch
from torch import nn
from torch.nn import functional as F

from segmentation_refinement.models.psp import extractors
from typing import Dict , Optional, Type, Any, Tuple
from . import extractors


class PSPModule(nn.Module):
def __init__(self, features, out_features=1024, sizes=(1, 2, 3, 6)):
def __init__(self, features: int, out_features: int = 1024, sizes: Tuple[int, int, int, int] = (1, 2, 3, 6)):
super().__init__()
self.stages = []
self.stages = nn.ModuleList([self._make_stage(features, size) for size in sizes])
self.bottleneck = nn.Conv2d(features * (len(sizes) + 1), out_features, kernel_size=1)
self.relu = nn.ReLU(inplace=True)
self.stages: nn.ModuleList = nn.ModuleList([self._make_stage(features, size) for size in sizes])
self.bottleneck: nn.Module = nn.Conv2d(features * (len(sizes) + 1), out_features, kernel_size=1)
self.relu: nn.Module = nn.ReLU(inplace=True)

def _make_stage(self, features, size):
def _make_stage(self, features: int, size: int) -> nn.Sequential:
prior = nn.AdaptiveAvgPool2d(output_size=(size, size))
conv = nn.Conv2d(features, features, kernel_size=1, bias=False)
return nn.Sequential(prior, conv)

def forward(self, feats):
def forward(self, feats: torch.Tensor) -> torch.Tensor:
h, w = feats.size(2), feats.size(3)
set_priors = [F.interpolate(input=stage(feats), size=(h, w), mode='bilinear', align_corners=False) for stage in self.stages]
priors = set_priors + [feats]
Expand All @@ -27,7 +26,7 @@ def forward(self, feats):


class PSPUpsample(nn.Module):
def __init__(self, x_channels, in_channels, out_channels):
def __init__(self, x_channels:int, in_channels:int, out_channels:int):
super().__init__()
self.conv = nn.Sequential(
nn.BatchNorm2d(in_channels),
Expand All @@ -49,8 +48,8 @@ def __init__(self, x_channels, in_channels, out_channels):

self.shortcut = nn.Conv2d(x_channels, out_channels, kernel_size=1)

def forward(self, x, up):
x = F.interpolate(input=x, scale_factor=2, mode='bilinear', align_corners=False)
def forward(self, x:torch.Tensor, up:torch.Tensor)->torch.Tensor:
x = F.interpolate(input=x, scale_factor=2.0, mode='bilinear', align_corners=False)

p = self.conv(torch.cat([x, up], 1))
sc = self.shortcut(x)
Expand Down Expand Up @@ -88,7 +87,7 @@ def __init__(self):
self.final_11 = nn.Conv2d(32+3, 32, kernel_size=1)
self.final_21 = nn.Conv2d(32, 1, kernel_size=1)

def forward(self, x, seg, inter_s8=None, inter_s4=None):
def forward(self, x:torch.Tensor , seg:torch.Tensor, inter_s8:Optional[torch.Tensor]=None, inter_s4:Optional[torch.Tensor]=None)-> Dict[str, torch.Tensor]:

images = {}

Expand All @@ -102,7 +101,7 @@ def forward(self, x, seg, inter_s8=None, inter_s4=None):
p = self.psp(f)

inter_s8 = self.final_28(p)
r_inter_s8 = F.interpolate(inter_s8, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_s8 = F.interpolate(inter_s8, scale_factor=8.0, mode='bilinear', align_corners=False)
r_inter_tanh_s8 = torch.tanh(r_inter_s8)

images['pred_28'] = torch.sigmoid(r_inter_s8)
Expand All @@ -119,13 +118,13 @@ def forward(self, x, seg, inter_s8=None, inter_s4=None):
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_2 = self.final_28(p)
r_inter_s8_2 = F.interpolate(inter_s8_2, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_s8_2 = F.interpolate(inter_s8_2, scale_factor=8.0, mode='bilinear', align_corners=False)
r_inter_tanh_s8_2 = torch.tanh(r_inter_s8_2)

p = self.up_1(p, f_2)

inter_s4 = self.final_56(p)
r_inter_s4 = F.interpolate(inter_s4, scale_factor=4, mode='bilinear', align_corners=False)
r_inter_s4 = F.interpolate(inter_s4, scale_factor=4.0, mode='bilinear', align_corners=False)
r_inter_tanh_s4 = torch.tanh(r_inter_s4)

images['pred_28_2'] = torch.sigmoid(r_inter_s8_2)
Expand All @@ -144,11 +143,11 @@ def forward(self, x, seg, inter_s8=None, inter_s4=None):
f, f_1, f_2 = self.feats(p)
p = self.psp(f)
inter_s8_3 = self.final_28(p)
r_inter_s8_3 = F.interpolate(inter_s8_3, scale_factor=8, mode='bilinear', align_corners=False)
r_inter_s8_3 = F.interpolate(inter_s8_3, scale_factor=8.0, mode='bilinear', align_corners=False)

p = self.up_1(p, f_2)
inter_s4_2 = self.final_56(p)
r_inter_s4_2 = F.interpolate(inter_s4_2, scale_factor=4, mode='bilinear', align_corners=False)
r_inter_s4_2 = F.interpolate(inter_s4_2, scale_factor=4.0, mode='bilinear', align_corners=False)
p = self.up_2(p, f_1)
p = self.up_3(p, x)

Expand Down