1. 程式人生 > 實用技巧 >解決keras.backend.reshape中的錯誤ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor

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

許多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