# https://wolfy.tistory.com/243
import torch
import torch.nn as nn
def conv_start():
return nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3), # padding size를 왜 4로 했지? 계산하면 3이 맞는것 같은데...
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
def bottleneck_block(in_dim, mid_dim, out_dim, down=False):
layers = []
if down:
layers.append(nn.Conv2d(in_dim, mid_dim, kernel_size=1, stride=2, padding=0))
else:
layers.append(nn.Conv2d(in_dim, mid_dim, kernel_size=1, stride=1, padding=1))
layers.extend([
nn.BatchNorm2d(mid_dim),
nn.ReLU(inplace=True),
nn.Conv2d(mid_dim, mid_dim, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(mid_dim),
nn.ReLU(inplace=True),
nn.Conv2d(mid_dim, out_dim, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(out_dim),
])
return nn.Sequential(*layers)
class Bottleneck(nn.Module):
def __init__(self, in_dim, mid_dim, out_dim, down: bool = False, starting: bool = False) -> None:
super(Bottleneck, self).__init__()
if starting:
down = False
self.block = bottleneck_block(in_dim, mid_dim, out_dim, down=down)
self.relu = nn.ReLU(inplace=True)
if down:
conn_layer = nn.Conv2d(in_dim, out_dim, kernel_size=1, stride=2, padding=0),
else:
conn_layer = nn.Conv2d(in_dim, out_dim, kernel_size=1, stride=1, padding=0),
self.change_dim = nn.Sequential(conn_layer, nn.BatchNorm2d(out_dim)) # 더해주기 위해 차원 맞춰주기
def forward(self, x):
identity = self.change_dim(x)
x = self.block(x)
x += identity
x = self.relu(x)
return x
def make_layer(in_dim, mid_dim, out_dim, repeats, starting=False):
layers = []
layers.append(Bottleneck(in_dim, mid_dim, out_dim, down=True, starting=starting))
for _ in range(1, repeats):
layers.append(Bottleneck(out_dim, mid_dim, out_dim, down=False))
return nn.Sequential(*layers)
class ResNet(nn.Module):
def __init__(self, repeats: list = [3, 4, 6, 3], num_classes=1000):
super(ResNet, self).__init__()
'Deep Learning' 카테고리의 다른 글
앞으로 봐야 할것들 (0) | 2021.06.18 |
---|---|
환영합니다! (0) | 2021.06.11 |