我尝试在kaggle上重复一些代码。我使用tensorflow 2.3.1,下面是我的代码:
input_shape = (size, size, 3)
in_lay = tf.keras.layers.Input(shape = input_shape)
in_lay = tf.keras.layers.Input(shape = input_shape)
base_pretrained_model = tf.keras.applications.VGG16(input_shape = input_shape,
include_top = False, weights = 'imagenet')
base_pretrained_model.trainable = False
pt_depth = base_pretrained_model.get_output_shape_at(0)[-1]
pt_features = base_pretrained_model(in_lay)
bn_features = tf.keras.layers.BatchNormalization()(pt_features)
……
我在行“pt_depth = base_pretrained_model.get_output_shape_at(0)-1".”处得到错误错误是:
pt_depth = base_pretrained_model.get_output_shape_at(0)[-1]
File "/home/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 2030, in get_output_shape_at
'output shape')
File "/home/xxxx/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 2603, in _get_node_attribute_at_index
'and thus has no defined ' + attr_name + '.')
RuntimeError: The layer has never been called and thus has no defined output shape.
原因是什么?
谢谢
发布于 2020-10-30 12:00:24
请尝试此方法:
pt_depth = model.layers[0].compute_output_shape(input_shape)
发布于 2021-04-22 09:56:48
我尝试了Andrey的修复,但在代码中的另一个步骤失败了。所以它对我不起作用。
我从这个页面得到了解决方案:https://github.com/tensorflow/tensorflow/issues/44857。在得到outshape之前,我们需要使用输入调用模型,如下所示:
in_lay = Input(t_x.shape[1:])
base_pretrained_model = VGG16(input_shape = t_x.shape[1:], include_top = False, weights = 'imagenet')
base_pretrained_model.trainable = False
pt_features = base_pretrained_model(in_lay)
pt_depth = base_pretrained_model.get_output_shape_at(0)[-1]
然后我的程序就可以继续了。
https://stackoverflow.com/questions/64607234
复制相似问题