keras.layers.TimeDistributed与hub.KerasLayer NotImplementedError [英] keras.layers.TimeDistributed with hub.KerasLayer NotImplementedError
问题描述
我想将tf.keras.TimeDistributed()层与最新TensorFLow V2版本(tf-nightly-gpu-2.0-preview)中的tf.hub inception_v3 CNN模型一起使用.输出如下所示.似乎tf.keras.TimeDistributed()并未完全实现与tf.hub模型一起使用.不知何故,无法计算输入层的形状.我的问题:是否有解决此问题的方法?
I want to use tf.keras.TimeDistributed() layer with the tf.hub inception_v3 CNN model from the latest TensorFLow V2 version (tf-nightly-gpu-2.0-preview). The output is shown below. It seemst that tf.keras.TimeDistributed() is not fully implemented to work with tf.hub models. Somehow, the shape of the input layer cannot be computed. My question: Is there a workaround this problem?
tf.keras.TimeDistributed可以正常工作.我只想将CNN模型应用于每个时间步骤.
tf.keras.TimeDistributed with regular tf.keras.layer works fine. I just would like to apply the CNN model to each time step.
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers, Model
model_url = "https://tfhub.dev/google/tf2-
preview/inception_v3/feature_vector/3"
preview/inception_v3/feature_vector/3"
feature_layer = hub.KerasLayer(model_url,
input_shape = (299, 299, 3),
output_shape = [2048],
trainable = False)
video = layers.Input(shape = (None, 299, 299, 3))
encoded_frames = layers.TimeDistributed(feature_layer)(video)
model = Model(inputs = video, outputs = encoded_frames)
预期产量
tf.keras模型
Expected output
tf.keras model
文件"/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/base_layer.py",第489行,在compute_output_shape中 引发NotImplementedError NotImplementedError
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 489, in compute_output_shape raise NotImplementedError NotImplementedError
推荐答案
在Tensorflow 2中,可以将自定义层与TimeDistributed
层结合使用.引发错误是因为它无法计算输出形状(请参见
In Tensorflow 2 it is possible to use custom layers in combination with the TimeDistributed
layer. The error is thrown because it can't compute the output shape (see here).
因此,在您的情况下,您应该能够继承KerasLayer
的子类并手动实现compute_output_shape
.
So in your case you should be able to subclass KerasLayer
and implement compute_output_shape
manually.
这篇关于keras.layers.TimeDistributed与hub.KerasLayer NotImplementedError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!