elia / modeling /backbone /resnet.py
yxchng
add files
a166479
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : resnet.py
@Time : 2022/04/23 14:08:10
@Author : BQH
@Version : 1.0
@Contact : raogx.vip@hotmail.com
@License : (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc : Backbone
'''
# here put the import lib
import torch
import torch.nn as nn
from addict import Dict
import torch.utils.model_zoo as model_zoo
BN_MOMENTUM = 0.1
model_urls = {'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth', }
def conv3x3(in_planes, out_planes, stride=1):
"""3x3 convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
class InvertedResidual(nn.Module):
def __init__(self, in_channels, hidden_dim, out_channels=3):
super(InvertedResidual, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, hidden_dim, kernel_size=1, stride=1, padding=0, bias=True),
nn.BatchNorm2d(hidden_dim, momentum=BN_MOMENTUM),
nn.ReLU6(inplace=True),
# dw
# nn.Conv2d(hidden_dim, hidden_dim, kernel_size=3, stride=1, padding=1, bias=False),
# nn.BatchNorm2d(hidden_dim, momentum=BN_MOMENTUM),
# nn.ReLU(inplace=True),
# pw-linear
nn.Conv2d(hidden_dim, out_channels, kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(out_channels, momentum=BN_MOMENTUM),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.conv(x)
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion, momentum=BN_MOMENTUM)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(nn.Module):
def __init__(self, block, layers):
super(ResNet, self).__init__()
self.inplanes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion, momentum=BN_MOMENTUM))
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, input_x):
out = {}
x = self.conv1(input_x)
x = self.bn1(x)
x = self.relu(x)
feature1 = self.maxpool(x)
feature2 = self.layer1(feature1)
out['res2'] = feature2
feature3 = self.layer2(feature2)
out['res3'] = feature3
feature4 = self.layer3(feature3)
out['res4'] = feature4
feature5 = self.layer4(feature4)
out['res5'] = feature5
return out
def init_weights(self, num_layers=50):
# url = model_urls['resnet{}'.format(num_layers)]
# pretrained_state_dict = model_zoo.load_url(url, model_dir='/home/code/pytorch_model/')
# print('=> loading pretrained model {}'.format(url))
pertained_model = r'/home/code/pytorch_model/resnet50-19c8e357.pth'
pretrained_state_dict = torch.load(pertained_model)
self.load_state_dict(pretrained_state_dict, strict=False)
resnet_spec = {'resnet18': (BasicBlock, [2, 2, 2, 2]),
'resnet34': (BasicBlock, [3, 4, 6, 3]),
'resnet50': (Bottleneck, [3, 4, 6, 3]),
'resnet101': (Bottleneck, [3, 4, 23, 3]),
'resnet152': (Bottleneck, [3, 8, 36, 3])}