微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用fastai学习器的张量大小不匹配

如何解决使用fastai学习器的张量大小不匹配

使用MVTec AD数据集构建自动编码器时遇到麻烦。

尤其是,我正在尝试复制基于MNIST的this指南。

代码如下:

m = torchvision.models.resnet34(pretrained = True).cuda()
m = nn.Sequential(*list(m.children())[:-3]).cuda()

t,_ = next(iter(load_train_dataset()))
t.size()
  > torch.Size([1,3,224,224])
m(t).size()
  > torch.Size([1,256,14,14])
code_sz = 32

conv = nn.Conv2d(256,code_sz,kernel_size=(2,2)).cuda()

m.add_module('CodeIn',conv)
m(t).size()
  > torch.Size([1,32,13,13])
class UpSample(nn.Module):
    def __init__(self,feat_in,feat_out,out_shape=None,scale=2):
        super().__init__()
        self.conv = nn.Conv2d(feat_in,kernel_size=(3,3),stride=1,padding=1)
        self.out_shape,self.scale = out_shape,scale
        
    
    def forward(self,x):
        return self.conv(
            nn.functional.interpolate(
                x,size=self.out_shape,scale_factor=self.scale,mode='bilinear',align_corners=True))
def get_upSamp(feat_in,scale=2,act='relu'):
    
    upSamp = UpSample(feat_in,out_shape=out_shape,scale=scale).cuda()
    
    layer = nn.Sequential(upSamp)
    
    if act == 'relu':
        act_f = nn.ReLU(inplace=True).cuda()
        bn = nn.Batchnorm2d(feat_out).cuda()
        layer.add_module('ReLU',act_f)
        layer.add_module('BN',bn)
    elif act == 'sig':
        act_f = nn.Sigmoid()
        layer.add_module('Sigmoid',act_f)
    return layer

def add_layer(m,name,act='relu'):
    upSamp = get_upSamp(feat_in,scale=scale,act=act)
    m.add_module(name,upSamp)
add_layer(m,'CodeOut')
m(t).size()
  > torch.Size([1,26,26])
add_layer(m,128,'Upsample0',out_shape=(56,56),scale=None)
m(t).size()
  > torch.Size([1,56,56])
add_layer(m,64,'Upsample1')
m(t).size()
  > torch.Size([1,112,112])
add_layer(m,'Upsample2',act='sig')
m(t).size()
  > torch.Size([1,224])
output = m(t)
print("out",output.size())
print("img",t.size())
  > out torch.Size([1,224])
    img torch.Size([1,224])
learn = Learner(dls_train,m,loss_func=F.mse_loss)
learn.lr_find(end_lr=10000000)

代码在此处失败,并显示以下输出

/usr/local/lib/python3.6/dist-packages/fastai/learner.py:166: UserWarning: Using a target size (torch.Size([64])) that is different to the input size (torch.Size([64,224])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
  if len(self.yb): self.loss = self.loss_func(self.pred,*self.yb)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-55-ab61ebca7e6a> in <module>()
----> 1 learn.lr_find(end_lr=10000000)

14 frames
/usr/local/lib/python3.6/dist-packages/torch/functional.py in broadcast_tensors(*tensors)
     63         if any(type(t) is not Tensor for t in tensors) and has_torch_function(tensors):
     64             return handle_torch_function(broadcast_tensors,tensors,*tensors)
---> 65     return _VF.broadcast_tensors(tensors)
     66 
     67 

RuntimeError: The size of tensor a (224) must match the size of tensor b (64) at non-singleton dimension 3

我真的不知道最初的警告是什么意思。 我是Python的新手,所以我确定这里缺少明显的东西。我被卡住了,我真的不知道如何解决这个问题。任何帮助将不胜感激!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。