首页 > 其他 > 详细

解决keras.backend.reshape中的错误ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor

时间:2020-11-13 23:43:47      阅读:532      评论:0      收藏:0      [点我收藏+]

许多CNN网络都有Fusion layer作为融合层,比如:

技术分享图片

 

 

 

 

 参考:https://arxiv.org/pdf/1712.03400.pdf

相关代码:(https://github.com/baldassarreFe/deep-koalarization/blob/master/src/koalarization/fusion_layer.py)

class FusionLayer(Layer):
    def call(self, inputs, mask=None):
        imgs, embs = inputs
        reshaped_shape = imgs.shape[:3].concatenate(embs.shape[1])
        embs = K.repeat(embs, imgs.shape[1] * imgs.shape[2])
        embs = K.reshape(embs, reshaped_shape)
        return K.concatenate([imgs, embs], axis=3)

当我实际去做的时候, K.reshape 报错:ValueError: Tried to convert ‘shape‘ to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor

 reshaped_shape = enco_loco.shape[:3].concatenate(enco_glob.shape[1])
    fuse = K.repeat(enco_glob, enco_loco.shape[1]*enco_loco.shape[2])
    fuse = K.reshape(fuse, (reshaped_shape))
    fuse = K.concatenate([enco_loco, fuse], axis=3)

相关信息:

enco_loco:(None, 16, 16, 512)
enco_glob:(None, 512)
reshaped_shape:(None, 16, 16, 512)
enco_glob.shape:(None, 512)
fuse.shape:(None, 256, 512)

最后想把fuse从(None, 256, 512) 变成(None, 16, 16, 512) 就出现上述错误。

解决方法:

fuse = K.reshape(fuse, (-1, reshaped_shape[1], reshaped_shape[2], reshaped_shape[3]))

参考:https://github.com/matterport/Mask_RCNN/issues/1070

解决keras.backend.reshape中的错误ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor

原文:https://www.cnblogs.com/mrlonely2018/p/13971791.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!