在部署pytorch训练好的模型,出现板子上推理出来的结果和在服务器上推理结果相差很大的问题,
在服务器上测试onnx输出可以和pytorch输出对应上
这是模型,附件中为模型参数和推理的输入样本
class encoder_block(nn.Module):
def __init__(self, in_channel, out_channel, stride=1):
super().__init__()
self.stride = stride
# 拆分第一个卷积块
self.conv1 = nn.Conv2d(
in_channels=in_channel,
out_channels=out_channel,
kernel_size=(7, 7),
padding=3,
stride=1,
padding_mode='zeros'
)
self.bn1 = nn.BatchNorm2d(out_channel)
self.relu = nn.ReLU()
# 拆分第二个卷积块
self.conv2 = nn.Conv2d(
in_channels=out_channel,
out_channels=out_channel,
kernel_size=(5, 5),
stride=self.stride,
padding=2,
padding_mode='zeros'
)
self.bn2 = nn.BatchNorm2d(out_channel)
# 下采样层
if stride != 1 or in_channel != out_channel:
self.down_conv = nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride)
self.down_bn = nn.BatchNorm2d(out_channel)
self.has_downsample = True
else:
self.has_downsample = False
def forward(self, batch_x):
identity = batch_x
# 下采样分支
if self.has_downsample:
identity = self.down_conv(identity)
identity = self.down_bn(identity)
# 主分支
batch_x = self.conv1(batch_x)
batch_x = self.bn1(batch_x)
batch_x = self.relu(batch_x)
batch_x = self.conv2(batch_x)
batch_x = self.bn2(batch_x)
out = batch_x + identity
return out
class res_embedding_4_layers(nn.Module):
def __init__(self, n_classes, in_channels=2):
super().__init__()
# 输入层
self.conv_in = nn.Conv2d(
in_channels=in_channels,
out_channels=32,
kernel_size=(7,7),
stride=(8,8)
)
self.bn_in = nn.BatchNorm2d(32)
self.relu_in = nn.ReLU()
self.dropout = nn.Dropout2d(p=0.3)
# Encoder blocks
self.layer1 = encoder_block(32, 64, stride=4)
self.layer2 = encoder_block(64, 128, stride=4)
if in_channels == 1:
self.layer3 = encoder_block(128, 128, stride=2)
else:
self.layer3 = encoder_block(128, 128, stride=4)
# 输出层
self.gap = nn.AvgPool2d((2,2))
self.flatten = nn.Flatten()
self.fc = nn.Linear(128, n_classes)
def forward(self, batch_x):
# 输入层
batch_x = self.conv_in(batch_x)
batch_x = self.bn_in(batch_x)
batch_x = self.relu_in(batch_x)
batch_x = self.dropout(batch_x)
# Encoder blocks
batch_x = self.layer1(batch_x)
batch_x = self.layer2(batch_x)
batch_x = self.layer3(batch_x)
# 输出层
batch_x = self.gap(batch_x)
batch_y = self.flatten(batch_x)
batch_y = self.fc(batch_y)
batch_y = F.normalize(batch_y, 2, dim=1)
return batch_y
在部署pytorch训练好的模型,出现板子上推理出来的结果和在服务器上推理结果相差很大的问题,
在服务器上测试onnx输出可以和pytorch输出对应上
这是模型,附件中为模型参数和推理的输入样本
class encoder_block(nn.Module):
def __init__(self, in_channel, out_channel, stride=1):
super().__init__()
self.stride = stride
# 拆分第一个卷积块
self.conv1 = nn.Conv2d(
in_channels=in_channel,
out_channels=out_channel,
kernel_size=(7, 7),
padding=3,
stride=1,
padding_mode='zeros'
)
self.bn1 = nn.BatchNorm2d(out_channel)
self.relu = nn.ReLU()
# 拆分第二个卷积块
self.conv2 = nn.Conv2d(
in_channels=out_channel,
out_channels=out_channel,
kernel_size=(5, 5),
stride=self.stride,
padding=2,
padding_mode='zeros'
)
self.bn2 = nn.BatchNorm2d(out_channel)
# 下采样层
if stride != 1 or in_channel != out_channel:
self.down_conv = nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride)
self.down_bn = nn.BatchNorm2d(out_channel)
self.has_downsample = True
else:
self.has_downsample = False
def forward(self, batch_x):
identity = batch_x
# 下采样分支
if self.has_downsample:
identity = self.down_conv(identity)
identity = self.down_bn(identity)
# 主分支
batch_x = self.conv1(batch_x)
batch_x = self.bn1(batch_x)
batch_x = self.relu(batch_x)
batch_x = self.conv2(batch_x)
batch_x = self.bn2(batch_x)
out = batch_x + identity
return out
class res_embedding_4_layers(nn.Module):
def __init__(self, n_classes, in_channels=2):
super().__init__()
# 输入层
self.conv_in = nn.Conv2d(
in_channels=in_channels,
out_channels=32,
kernel_size=(7,7),
stride=(8,8)
)
self.bn_in = nn.BatchNorm2d(32)
self.relu_in = nn.ReLU()
self.dropout = nn.Dropout2d(p=0.3)
# Encoder blocks
self.layer1 = encoder_block(32, 64, stride=4)
self.layer2 = encoder_block(64, 128, stride=4)
if in_channels == 1:
self.layer3 = encoder_block(128, 128, stride=2)
else:
self.layer3 = encoder_block(128, 128, stride=4)
# 输出层
self.gap = nn.AvgPool2d((2,2))
self.flatten = nn.Flatten()
self.fc = nn.Linear(128, n_classes)
def forward(self, batch_x):
# 输入层
batch_x = self.conv_in(batch_x)
batch_x = self.bn_in(batch_x)
batch_x = self.relu_in(batch_x)
batch_x = self.dropout(batch_x)
# Encoder blocks
batch_x = self.layer1(batch_x)
batch_x = self.layer2(batch_x)
batch_x = self.layer3(batch_x)
# 输出层
batch_x = self.gap(batch_x)
batch_y = self.flatten(batch_x)
batch_y = self.fc(batch_y)
batch_y = F.normalize(batch_y, 2, dim=1)
return batch_y