尝试将图像数据输入DNNRegressor时发生ValueError

时间:2018-09-30 21:12:43

标签: python tensorflow

我在让DNNRegressor接受图像数据时遇到了一些麻烦。运行代码时出现此错误:

ValueError: Cannot reshape a tensor with 147456 elements to shape [384,442368] (169869312 elements) for 'dnn/input_from_feature_columns/input_layer/image/Reshape' (op: 'Reshape') with input shapes: [384,384,1], [2] and with input tensors computed as partial shapes: input[1] = [384,442368].

这是违规代码的简化版本。

import os
import os.path

import tensorflow as tf

SPLIT_PERCENTAGE = 0.8

# snip snip

# ids is a List of strings
# filenames is a List of filenames of image files on the disk
# labels is a List of int scores

estimator = tf.estimator.DNNRegressor(
    feature_columns=[
        tf.feature_column.numeric_column('image', shape=(384, 384, 3)),
    ],
    hidden_units=[1024, 512, 256],
    model_dir=output_dir,
)

estimator.train(input_fn=lambda: input_fn(False, ids, filenames, labels))

def input_fn(is_training, ids, filenames, labels):
    id_tensor = tf.constant(ids, dtype=tf.string)
    filenames_tensor = tf.constant(filenames, dtype=tf.string)
    labels_tensor = tf.constant(labels, dtype=tf.float32)

    ds = tf.data.Dataset.from_tensor_slices(((id_tensor, filenames_tensor), labels_tensor))
    print(ds)
    ds = ds.take(int(len(labels) * SPLIT_PERCENTAGE)) if is_training else ds.skip(int(len(labels) * SPLIT_PERCENTAGE))
    ds = ds.map(load_image)

    iterator = ds.make_one_shot_iterator()
    features, labels = iterator.get_next()

    return features, labels

def load_image(id_file, score):
    _, filename = id_file
    image_string = tf.read_file(filename)
    image_decoded = tf.image.decode_jpeg(image_string, channels=1)
    image_converted = tf.image.convert_image_dtype(image_decoded, tf.float16)
    image_resized = tf.image.resize_image_with_crop_or_pad(image_converted, 384, 384)

    return {'image': image_resized}, [tf.log(score)]

我怀疑这与我声明要素列的方式有关,但是this example几乎完全相同并且可以工作。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

我去散步了很长时间,买了一个很好的新枕头,喝了啤酒,并意识到在$ echo '172.31.20.98 - - [30/Sep/2018:20:01:01 +0000] "GET ...' | sed 's/\[//; s/\]//' 172.31.20.98 - - 30/Sep/2018:20:01:01 +0000 "GET ... 的一个.batch()呼叫是必要的即使它只是DataSet,也可以使用它。

您了解的越多