栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Cannot use TimeDistributed with some Model

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Cannot use TimeDistributed with some Model

项目场景:

视频分类项目用到ShuffleNetV2提取每帧图的特征,对于多帧连续图同时提取特征,需要TimeDistributed层包装ShuffleNetV2模型层,此时本人遇到包装问题,问题如下描述。


问题描述
    feature_layer = TimeDistributed(ShuffleNet_V2(input_shape=[24, 32, CHANNEL_NUM], output_num=FEATURE_SIZE))(org_input)
  File "/home/ml/.local/lib/python3.8/site-packages/keras/engine/base_layer_v1.py", line 765, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "/home/ml/.local/lib/python3.8/site-packages/keras/layers/wrappers.py", line 271, in call
    output_shape = self.compute_output_shape(input_shape)
  File "/home/ml/.local/lib/python3.8/site-packages/keras/layers/wrappers.py", line 190, in compute_output_shape
    child_output_shape = self.layer.compute_output_shape(child_input_shape)
  File "/home/ml/.local/lib/python3.8/site-packages/keras/engine/functional.py", line 470, in compute_output_shape
    layer_output_shapes = layer.compute_output_shape(layer_input_shapes)
  File "/home/ml/.local/lib/python3.8/site-packages/keras/engine/base_layer_v1.py", line 559, in compute_output_shape
    raise NotImplementedError
NotImplementedError


原因分析:

TimeDistributed层包装特征提取层ShuffleNet后,无法计算输出的shape


解决方案:

以下两方法都是定义特征提取模型层后,自主计算特征提取模型层的输出shape

方法一: Output_shape excludes the batch dimension

layer = hub.KerasLayer(“https://tfhub.dev/some/model/1”, output_shape=(4, 4))
…

Input_shape includes the batch dimension (can be None)

expected_shape = layer.compute_output_shape(input_shape=(10, 2, 4)).as_list()

方法二: Load KerasLayer

input = …
layer = hub.KerasLayer(“https://tfhub.dev/some/model/1”,)
net = layer(input)
net = tf.keras.Model(input , net)

Add custom compute_output_shape

net.compute_output_shape = lambda x : (x[0],x[1],512)

Create model

model = tf.keras.Sequential([tf.keras.layers.TimeDistributed(net),…])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968594.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号