Expected more than 1 value per channel when training, got input size torch.Size([1, 64, 1, 1])

最近在测试网络模型,记录一下创建model.py后如何进行简单的检测和查看

BUG:

Expected more than 1 value per channel when training, got input size torch.Size([1, 64, 1, 1])

分析:

如标题 此问题是因为进行简单测试时,自己设置了BN层,进行测试时input_image.shape=[1,3,224,224]

batch_size=1 影响了BN层

解决办法:

①加入model.eval()

②调整batch_size

下面记录一下自己的模型简单测试学习

Method 1:

查看网络结构 网络层

model=de_model()
summary(model, input_size=(3, 224, 224))

from torchsummary import summary

class de_model(nn.Module):
    def __init__(self):
        super(de_model, self).__init__()
        ....
    def forward(self,x):
        pass
   
# 重点
model=de_model()
summary(model, input_size=(3, 224, 224))

Method 2:

直接输出 查看结果

model=de_model()
image=torch.rand((1,3,224,224))
result=model(image)

from torchsummary import summary

class de_model(nn.Module):
    def __init__(self):
        super(de_model, self).__init__()
        ....
    def forward(self,x):
        pass
   
# 重点
model=de_model()
model.eval()
image=torch.rand((1,3,224,224))
result=model(image)

Method 3:

tensor类型转为可视化的图像 这个办法十分重要!!!!!

unloader = transforms.ToPILImage()


image = x.cpu().clone()  # clone the tensor

此处的x是指你想查看并想保存的图像名称!!!!!!!!


image = image.squeeze(0)  # remove the fake batch dimension
image = unloader(image)
image.save('my_example.jpg')

path=r'00001.png'
image=Image.open(path)
image=image.resize((224, 224), Image.ANTIALIAS)
image = np.asarray(image) / 255.0
image = torch.from_numpy(image).float().permute(2, 0, 1).unsqueeze(0)
print(image.shape)
#
model=dehaze_sample()
x=model(image)
print(x.shape)
#
# tensor类型------> 可视化的图像
unloader = transforms.ToPILImage()
image = x.cpu().clone()  # clone the tensor
image = image.squeeze(0)  # remove the fake batch dimension
image = unloader(image)
image.save('my_example.jpg')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值