日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發布時間:2023/12/13 综合教程 36 生活家

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

但又出現錯誤:AttributeError 'NoneType' object has no attribute '_inbound_nodes'

原來是因為:“只要使用Model,就必須保證該函數內全為layer而不能有其他函數,如果有其他函數必須用Lambda封裝為layer?!?/p>

參考:https://zhuanlan.zhihu.com/p/138075621

好吧,再改一下:

from keras.layers import  RepeatVector, Reshape
from keras.layers.merge import concatenate

reshaped_shape = enco_loco.shape[:3].concatenate(enco_glob.shape[1])
    fuse = RepeatVector(enco_loco.shape[1]*enco_loco.shape[2])(enco_glob)
    fuse = Reshape(( reshaped_shape[1], reshaped_shape[2], reshaped_shape[3]))(fuse)
    fuse = concatenate([enco_loco, fuse], axis=3)

注意這里的維度必須是( reshaped_shape[1], reshaped_shape[2], reshaped_shape[3]) 而不是( -1, reshaped_shape[1], reshaped_shape[2], reshaped_shape[3])

不然會出錯

總結

以上是生活随笔為你收集整理的解决keras.backend.reshape中的错误ValueError: Tried to convert 'shape' to a tensor and failed. Error: Cannot convert a partially known TensorShape to a Tensor的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。