ImageGenerator和Model.predict的问题。

问题描述 投票:0回答:1

我需要有3张图片作为CNN的输入,我使用ImageGenerator和flow_from_dataframe进行预处理。

idg = ImageDataGenerator(rescale = 1./255)

A_gen = idg.flow_from_dataframe(df,directory = path,x_col = 'A',y_col = 'class',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = 'binary',seed=1,batch_size=batch_size)
B_gen = idg.flow_from_dataframe(df,directory = path,x_col = 'taste1',y_col = 'class',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = 'binary',seed=1,batch_size=batch_size)
C_gen = idg.flow_from_dataframe(df,directory = path,x_col = 'taste2',y_col = 'class',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = 'binary',seed=1,batch_size=batch_size)

然后,我把所有的3个生成器放在一起,使用。

    def combine(A,B,C):
        while True:
            X1i = A.next()
            X2i = B.next()
            X3i = C.next()
            yield [X1i[0], X2i[0],X3i[0]], X1i[1]

inputgenerator = combine(A_gen,B_gen,C_gen)

我的CNN的开头是这样的 。

def simple_cnn():
    pic_input1 = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))
    pic_input2 = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))
    pic_input3 = Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3))


    cnn1 = BatchNormalization()(pic_input1)
    cnn2 = BatchNormalization()(pic_input2)
    cnn3 = BatchNormalization()(pic_input3)
... (rest is not relevant I guess)

然后,我使用.NET来拟合我的模型。

model.fit(inputgenerator,steps_per_epoch=len(df) / batch_size, epochs=4)

到这里为止,任何东西都能完美地工作。(我知道,我需要使用一个验证集等,但首先我想确保我知道如何处理多个生成器)

但是,当我想进行预测时,与我的testgenerator,是。

idg2 = ImageDataGenerator(rescale = 1./255)

D_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'D',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)
E_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'E',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)
F_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'F',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)
testgenerator = combine_test(D_gen,E_gen,F_gen)
pred = model.predict(testgenerator)

def combine_test(A,B,C):
    while True:
        X1i = A.next()
        X2i = B.next()
        X3i = C.next()
        yield [X1i[0], X2i[0],X3i[0]]

我得到了以下错误。

Traceback (most recent call last):

  File "/home/maeul/Documents/ETHZ/2ndSemester/IntroToMachineLearning/Task4/Task4.py", line 228, in <module>
    pred = model.predict(testgenerator)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 1013, in predict
    use_multiprocessing=use_multiprocessing)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 498, in predict
    workers=workers, use_multiprocessing=use_multiprocessing, **kwargs)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 426, in _model_iteration
    use_multiprocessing=use_multiprocessing)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 706, in _process_inputs
    use_multiprocessing=use_multiprocessing)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/data_adapter.py", line 767, in __init__
    dataset = standardize_function(dataset)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 684, in standardize_function
    return dataset.map(map_fn, num_parallel_calls=dataset_ops.AUTOTUNE)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 1591, in map
    self, map_func, num_parallel_calls, preserve_cardinality=True)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3926, in __init__
    use_legacy_function=use_legacy_function)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3147, in __init__
    self._function = wrapper_fn._get_concrete_function_internal()

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2395, in _get_concrete_function_internal
    *args, **kwargs)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2389, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2703, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py", line 2593, in _create_graph_function
    capture_by_value=self._capture_by_value),

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py", line 978, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3140, in wrapper_fn
    ret = _wrapper_helper(*args)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 3082, in _wrapper_helper
    ret = autograph.tf_convert(func, ag_ctx)(*nested_args)

  File "/home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)

ValueError: in converted code:

    /home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py:677 map_fn
        batch_size=None)
    /home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py:2410 _standardize_tensors
        exception_prefix='input')
    /home/maeul/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py:573 standardize_input_data
        'with shape ' + str(data_shape))

    ValueError: Error when checking input: expected input_10 to have 4 dimensions, but got array with shape (None, None, None)

我想这是与单个生成器的批量大小有关,但我不知道如何 "欺骗 "model.predict通过在生成的每个图像中添加一个微不足道的维度......

先谢谢您的帮助!

image keras generator predict
1个回答
0
投票

我终于找到了答案:由于在生成器中没有标注

D_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'D',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)
E_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'E',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)
F_gen = idg2.flow_from_dataframe(df2,directory = path,x_col = 'F',y_col = 'None',target_size = (IMG_HEIGHT,IMG_WIDTH),
                       class_mode = None,seed=1,batch_size=1)

这意味着生成器生成的是一个列表而不是一个表,所以当我在 combine_test 中写 Xi[0]时,我实际上调用的是列表的第一个元素,而不是整个列表(技术术语可能是错误的,但以我的基本知识,我是怎么得到的),所以我需要用简单的 Xi 来代替 Xi[0]。

要解决这个错误,请修改 "combel_test "函数如下。

def combine_test(A,B,C):
    while True:
        X1i = A.next()
        X2i = B.next()
        X3i = C.next()
        yield [X1i, X2i, X3i]
© www.soinside.com 2019 - 2024. All rights reserved.