Tensorflow: Tensor must be from the same graph as Tensor

北城余情 提交于 2019-12-23 04:19:25

问题


First steps in tensorflow, I'm trying to train a DNN model for image classification.

My current code is:

folder_path = Path('cropped_images/cropped')
df['filename'] = df['tag_id'].map(lambda tag: str(folder_path / (tag + '.png')))

def database_input_fn():

    def parse_image(filename, label):
        image_decoded = tf.image.decode_png(tf.read_file(filename), channels=3)
        image_resized = tf.image.resize_images(image_decoded, [64, 64])
        label = label == 'large vehicle'
        return image_resized, label

    filenames = tf.constant(df['filename'])
    labels = tf.constant(df['general_class'])
    dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
    dataset = dataset.map(parse_image)
    dataset = dataset.shuffle()
    dataset = dataset.batch(32)
    dataset = dataset.repeat()
    return dataset

images_fc = tf.feature_column.numeric_column('image', shape=[64, 64, 3])

estimator = tf.estimator.DNNClassifier(feature_columns=[images_fc],
                                     hidden_units=[32, 32, 32, 32])
metrics = estimator.train(lambda : dataset, steps=10000)

Where df is pandas.DataFrame containing the images paths and their corresponding labels. Images are stored on disk in the above folder path.

I'm getting the following error:

ValueError: Tensor("IteratorV2:0", shape=(), dtype=resource) must be from the same graph as Tensor("BatchDatasetV2_4:0", shape=(), dtype=variant).

What am I missing? Why isn't everything being constructed on the same graph?


回答1:


I think

metrics = estimator.train(lambda : dataset, steps=10000)

might be the problem. If you check the arguments for estimator train, the input_fn constructs and returns a dataset, which means a new graph is created for the estimator and the input function. In your case, you have already created that graph outside of this scope. Probably changing your code to something like:

metrics = estimator.train(input_fn=database_input_fn, steps=10000) 

might solve that problem!



来源:https://stackoverflow.com/questions/53583792/tensorflow-tensor-must-be-from-the-same-graph-as-tensor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!