155 lines
5.4 KiB
Python
155 lines
5.4 KiB
Python
import torch
|
|
from torch import nn
|
|
import torch.nn.functional as F
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
# Define model
|
|
# input:128x256x3
|
|
# output: 64
|
|
class ResidualBlock(nn.Module):
|
|
def __init__(self, channels):
|
|
super(ResidualBlock, self).__init__()
|
|
self.channels = channels
|
|
self.conv1 = nn.Conv2d(channels, channels, kernel_size=(3, 3), padding=1)
|
|
self.conv2 = nn.Conv2d(channels, channels*2, kernel_size=(3, 3), padding=1)
|
|
self.conv3 = nn.Conv2d(channels, channels*2, kernel_size=(1, 1), padding=0)
|
|
|
|
# 两个卷积层就使用一次残差网络。
|
|
def forward(self, x):
|
|
y = F.leaky_relu(self.conv1(x))
|
|
x = self.conv3(x)
|
|
y = self.conv2(y)
|
|
return F.leaky_relu(x + y) # 先求和再激活。
|
|
|
|
class ReIDNet(nn.Module):
|
|
def __init__(self):
|
|
super(ReIDNet, self).__init__()
|
|
self.conv1 = nn.Conv2d(3, 16, kernel_size=(3, 3), padding=1)
|
|
# self.maxpool1 = nn.MaxPool2d(2) # 32x64x16
|
|
self.bn1 = nn.BatchNorm2d(16)
|
|
self.dropout1 = nn.Dropout(p=0.3)
|
|
self.conv2 = nn.Conv2d(16, 32, kernel_size=(3, 3), padding=1)
|
|
self.maxpool2 = nn.MaxPool2d(2) # 16x32x32
|
|
self.bn2 = nn.BatchNorm2d(32)
|
|
self.rblock1 = ResidualBlock(32)
|
|
# self.conv3 = nn.Conv2d(32, 32, kernel_size=(3, 3), padding=1)
|
|
self.dropout2 = nn.Dropout(p=0.5)
|
|
self.conv4 = nn.Conv2d(64, 64, kernel_size=(3, 3), padding=1)
|
|
self.maxpool3 = nn.MaxPool2d(2) # 8x16x64
|
|
self.bn3 = nn.BatchNorm2d(64)
|
|
self.rblock2 = ResidualBlock(64)
|
|
# self.conv5 = nn.Conv2d(64, 128, kernel_size=(3, 3), padding=1) # 8x16x64
|
|
self.maxpool4 = nn.MaxPool2d(2) # 8x16x32
|
|
self.bn4 = nn.BatchNorm2d(128)
|
|
self.conv6 = nn.Conv2d(128,256,kernel_size=(3, 3), padding=1) #8x16x64
|
|
self.flat = nn.Flatten()
|
|
self.linear = nn.Linear(8*16*256,23) # 64D-feature ID
|
|
|
|
self.sigmoid = nn.Sigmoid()
|
|
self.lrelu = nn.LeakyReLU()
|
|
|
|
def forward(self, x:torch.Tensor):
|
|
x = self.conv1(x)
|
|
x = self.lrelu(x)
|
|
# x = self.maxpool1(x)
|
|
x = self.bn1(x)
|
|
x = self.dropout1(x)
|
|
x = self.conv2(x)
|
|
x = self.lrelu(x)
|
|
x = self.maxpool2(x)
|
|
x = self.bn2(x)
|
|
x = self.rblock1(x)
|
|
# x = self.conv3(x)
|
|
# x = self.lrelu(x)
|
|
x = self.dropout2(x)
|
|
x = self.conv4(x)
|
|
x = self.lrelu(x)
|
|
x = self.maxpool3(x)
|
|
x = self.bn3(x)
|
|
x = self.rblock2(x)
|
|
# x = self.conv5(x)
|
|
# x = self.lrelu(x)
|
|
x = self.maxpool4(x)
|
|
x = self.bn4(x)
|
|
x = self.conv6(x)
|
|
x = self.sigmoid(x)
|
|
x = self.flat(x)
|
|
x = self.linear(x)
|
|
|
|
return x
|
|
|
|
#########ResNet18
|
|
class RestNetBasicBlock(nn.Module):
|
|
def __init__(self, in_channels, out_channels, stride):
|
|
super(RestNetBasicBlock, self).__init__()
|
|
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
|
|
self.bn1 = nn.BatchNorm2d(out_channels)
|
|
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
|
|
self.bn2 = nn.BatchNorm2d(out_channels)
|
|
|
|
def forward(self, x):
|
|
output = self.conv1(x)
|
|
output = F.relu(self.bn1(output))
|
|
output = self.conv2(output)
|
|
output = self.bn2(output)
|
|
return F.leaky_relu(x + output)
|
|
|
|
|
|
class RestNetDownBlock(nn.Module):
|
|
def __init__(self, in_channels, out_channels, stride):
|
|
super(RestNetDownBlock, self).__init__()
|
|
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride[0], padding=1)
|
|
self.bn1 = nn.BatchNorm2d(out_channels)
|
|
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride[1], padding=1)
|
|
self.bn2 = nn.BatchNorm2d(out_channels)
|
|
self.extra = nn.Sequential(
|
|
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride[0], padding=0),
|
|
nn.BatchNorm2d(out_channels)
|
|
)
|
|
|
|
def forward(self, x):
|
|
extra_x = self.extra(x)
|
|
output = self.conv1(x)
|
|
out = F.relu(self.bn1(output))
|
|
|
|
out = self.conv2(out)
|
|
out = self.bn2(out)
|
|
return F.leaky_relu(extra_x + out)
|
|
|
|
|
|
class RestNet18(nn.Module):
|
|
def __init__(self):
|
|
super(RestNet18, self).__init__()
|
|
self.conv1 = nn.Conv2d(3, 32, kernel_size=(3, 3), padding=1)
|
|
self.bn1 = nn.BatchNorm2d(32)
|
|
self.maxpool = nn.MaxPool2d(2)
|
|
|
|
self.layer1 = nn.Sequential(RestNetBasicBlock(32, 32, 1),
|
|
RestNetBasicBlock(32, 32, 1))
|
|
|
|
self.layer2 = nn.Sequential(RestNetDownBlock(32, 64, [2, 1]),
|
|
RestNetBasicBlock(64, 64, 1))
|
|
|
|
self.layer3 = nn.Sequential(RestNetDownBlock(64, 128, [2, 1]),
|
|
RestNetBasicBlock(128, 128, 1))
|
|
|
|
self.layer4 = nn.Sequential(RestNetDownBlock(128, 256, [2, 1]),
|
|
RestNetBasicBlock(256, 256, 1))
|
|
|
|
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
|
|
|
|
self.fc = nn.Linear(32768, 23)
|
|
|
|
def forward(self, x):
|
|
x = self.conv1(x)
|
|
x = self.layer1(x)
|
|
x = self.layer2(x)
|
|
x = self.layer3(x)
|
|
x = self.layer4(x)
|
|
# x = self.avgpool(x)
|
|
x = x.reshape(x.shape[0], -1)
|
|
# print(x.size())
|
|
x = self.fc(x)
|
|
return x
|