在自定义图像数据集上训练DCGAN的ValueError

时间:2019-06-13 22:38:48

标签: python tensorflow keras

我试图在tensorflow和keras中创建一个DCGAN以与自定义数据集一起使用。我在训练它时遇到问题,我认为这是由数据形状或模型形状引起的。数据的形状为[None,100,100,3]

我已经能够确定代码中有问题的区域,我相信这是与生成器有关的。

我根据此功能定义

def make_generator_model():
    model = tf.keras.Sequential()
    #I believe the issue is with the line below
    model.add(tf.keras.layers.Dense(25*25*256, use_bias=False, input_shape=([32])))
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.LeakyReLU())

    model.add(tf.keras.layers.Reshape((25, 25, 256)))
    assert model.output_shape == (None, 25, 25, 256) # Note: None is the batch size

    model.add(tf.keras.layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
    assert model.output_shape == (None, 25, 25, 128)  
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.LeakyReLU())

    model.add(tf.keras.layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    assert model.output_shape == (None, 50, 50, 64)    
    model.add(tf.keras.layers.BatchNormalization())
    model.add(tf.keras.layers.LeakyReLU())

    model.add(tf.keras.layers.Conv2DTranspose(1, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
    assert model.output_shape == (None, 100, 100, 1)

    return model

失败,并显示错误代码:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-3bfe38106dd7> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', 'train(train_dataset, EPOCHS)')

8 frames
</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-60> in time(self, line, cell, local_ns)

<timed eval> in <module>()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    703           except Exception as e:  # pylint:disable=broad-except
    704             if hasattr(e, "ag_error_metadata"):
--> 705               raise e.ag_error_metadata.to_exception(type(e))
    706             else:
    707               raise

ValueError: in converted code:

    <ipython-input-15-bca9093ebeab>:6 train_step *
        generated_images = generator(noise, training=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:586 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:159 assert_input_compatibility
        ' but received input with shape ' + str(shape))

    ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 32 but received input with shape [32, 100]

0 个答案:

没有答案