【问题标题】:AttributeError: The layer "input_4" has multiple inbound nodes, with different output shapesAttributeError:层“input_4”有多个入站节点,具有不同的输出形状
【发布时间】:2020-03-03 18:03:07
【问题描述】:

我得到以下错误。“AttributeError:层“input_4”有多个入站节点,不同 输出形状。因此,该层的输出形状的概念定义不明确”。使用 get_output_shape_at(node_index) 代替”。

该代码无需在 Docker 容器上运行即可完美运行。我有两个 两个 docker 容器上的 docker 容器 我有相同版本的 TensorFlow 版本 2.1.0 和 Keras 2.2.4-tf。但是,在以前的系统上,我运行 windows 机器上的代码 TensorFlow 版本 1.12.0 和 2.1.6-tf 谢谢,非常感谢帮助 我正在使用 this1 教程在 docker 容器中运行 TensorFlow。给出了 Docker 文件的代码 下面。

Docker 文件

FROM tensorflow/tensorflow:latest-py3

RUN pip install -q keras

RUN pip install prettytable

RUN pip install pillow

Python 代码

import numpy as np
from keras.applications.vgg19 import decode_predictions
from tensorflow.python.keras.models import load_model
from prettytable import PrettyTable
import time
from keras import backend as K
from tensorflow import keras
from tensorflow.python import keras
import time

model_2=load_model('model_2.h5',compile=False)
model_2.summary()

predictions1= np.load('predictions_result.npy')
times=[]
def profiler(model):
   layer_input = keras.layers.Input(batch_shape=model.get_layer('input_4').get_input_shape_at(0))
   x = layer_input
   t = PrettyTable( ['Layer', 'Latency (milliseconds)', 'Output (bytes)'] )
   for layer in model.layers:
      x = layer( x )
    # input and output of layer
      result = 1
      output_shape_list = []
      for i in layer.output_shape[1:]:
        result = result * i
        output_shape_list.append( i )

    intermediate_model = keras.Model( layer_input, x )
    start = time.time()
    intermediate_model = intermediate_model.predict(predictions1)
    np.save('predictions_result', intermediate_model)
    end = time.time() - start
    print(end)
    times.append(end)

    def convert_bytes(result):
        for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
            if result < 1024.0:
                return "%3.1f %s" % (result, x)
            result /= 1024.0
        return result
    t.add_row([type( layer ).__name__, round( end*1000, 2 ),  convert_bytes( result*4 )])
 print(t)

profiler(model_2)
print("Total Latency(milliseconds):", round(sum(times*1000),2))
tmp=np.zeros((1,28,28,512))

for i in range(0,1):
  tmp[i,:,:,:]=predictions1[i,:]

predictions2 = model_2.predict(tmp)
label_vgg19 = decode_predictions(predictions2)
print ('label_vgg19 =', label_vgg19)

【问题讨论】:

  • 这是版本不匹配:检查您在 docker 映像外部运行代码的版本和内部版本。它们可能会有所不同,并且它们之间的 api 也会发生变化。
  • 嗨,@Yuri-M-Dias 我在两个 docker 容器上都有两个 docker 容器我​​有相同的版本 TensorFlow 版本 2.1.0 和 Keras 2.2.4-tf。但是,在之前的系统上,我在windows机器TensorFlow版本1.12.0和2.1.6-tf上运行代码

标签: python docker tensorflow keras deep-learning


【解决方案1】:

问题出现在下面一行

`for i in layer.output_shape[1:]:'

由

修复
shape=layer.get_output_at( 0 ).get_shape().as_list()
for i in shape[1:]:
  result = result * i 

【讨论】:

    猜你喜欢
    • 2022-12-22
    • 2020-11-26
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2021-07-20
    相关资源
    最近更新 更多