Keras(Tensorflow后端)错误-在图表中找不到在feed_devices或fetch_devices中指定的Tensor输入_1:0 [英] Keras (Tensorflow backend) Error - Tensor input_1:0, specified in either feed_devices or fetch_devices was not found in the Graph

查看:2440
本文介绍了Keras(Tensorflow后端)错误-在图表中找不到在feed_devices或fetch_devices中指定的Tensor输入_1:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用我之前已经训练过的简单模型进行预测时,出现以下错误:

When trying to predict using a simple model I've previously trained I get the following error:

在feed_devices或fetch_devices中指定的张量输入_1:0

在线:

seatbelt_model.predict(image_arr, verbose=1)

使用代码:

from tensorflow import keras
import tensorflow as tf
import numpy as np

graph = tf.get_default_graph()

seatbelt_model = keras.models.load_model(filepath='./graphs/seatbelt_A_3_81.h5')

class SeatbeltPredictor:
    INPUT_SHAPE = (-1, 120, 160, 1)

    @staticmethod
    def predict_seatbelt(image_arr):
        with graph.as_default():
            image_arr = np.array(image_arr).reshape(SeatbeltPredictor.INPUT_SHAPE)
            predicted_labels = seatbelt_model.predict(image_arr, verbose=1)
            return predicted_labels

模型具有以下形状:

input_layer = keras.layers.Input(shape=(IMAGE_HEIGHT, IMAGE_WIDTH, 1))
conv_0 = keras.layers.Conv2D(filters=32, kernel_size=[5, 5], activation=tf.nn.relu, padding="SAME")(input_layer)
pool_0 = keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="VALID")(conv_0)
conv_1 = keras.layers.Conv2D(filters=32, kernel_size=[5, 5], activation=tf.nn.relu, padding="SAME")(pool_0)
pool_1 = keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding="VALID")(conv_1)
flat_0 = keras.layers.Flatten()(pool_1)
dense_0 = keras.layers.Dense(units=1024, activation=tf.nn.relu)(flat_0)
drop_0 = keras.layers.Dropout(rate=0.4, trainable=True)(dense_0)
dense_1 = keras.layers.Dense(units=2, activation=tf.nn.softmax)(drop_0)

如果运行以下命令,则会得到张量结果:

If I run the following, I get a tensor result:

graph.get_tensor_by_name('input_1:0')
<tf.Tensor 'input_1:0' shape=(?, 120, 160, 1) dtype=float32>

第一层的名称为input_1

The name of the first layer is input_1

image_arr的形状为(1,120,160,1)

image_arr is of shape (1, 120, 160, 1)

Tensorflow 1.12

Tensorflow 1.12

有什么想法吗?

推荐答案

好吧,在经历了许多痛苦和折磨并跳入张量流的肠子后,我发现了以下内容:

OK, after a lot of pain and suffering and diving into the bowels of tensorflow I found the following:

尽管模型具有Session和Graph,但在某些张量流方法中,使用了默认的Session和Graph.为了解决这个问题,我必须明确地说我想同时使用Session和Graph作为默认值:

Although the model has a Session and Graph, in some tensorflow methods, the default Session and Graph are used. To fix this I had to explicity say that I wanted to use both my Session and my Graph as the default:

with session.as_default():
    with session.graph.as_default():

完整代码:

from tensorflow import keras
import tensorflow as tf
import numpy as np
import log

config = tf.ConfigProto(
    device_count={'GPU': 1},
    intra_op_parallelism_threads=1,
    allow_soft_placement=True
)

config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.6

session = tf.Session(config=config)

keras.backend.set_session(session)

seatbelt_model = keras.models.load_model(filepath='./seatbelt.h5')

SEATBEL_INPUT_SHAPE = (-1, 120, 160, 1)

def predict_seatbelt(image_arr):
    try:
        with session.as_default():
            with session.graph.as_default():
                image_arr = np.array(image_arr).reshape(SEATBEL_INPUT_SHAPE)
                predicted_labels = seatbelt_model.predict(image_arr, verbose=1)
                return predicted_labels
    except Exception as ex:
        log.log('Seatbelt Prediction Error', ex, ex.__traceback__.tb_lineno)

这篇关于Keras(Tensorflow后端)错误-在图表中找不到在feed_devices或fetch_devices中指定的Tensor输入_1:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆