简体   繁体   English

矩阵大小不兼容:DCGAN 中的 In[0]: [16,1024], In[1]: [16384,1]

[英]Matrix size-incompatible: In[0]: [16,1024], In[1]: [16384,1] in DCGAN

I am trying to build a dcgan nn.我正在尝试构建一个 dcgan nn。

I am receiving:我收到:

InvalidArgumentError: Matrix size-incompatible: In[0]: [16,1024], In[1]: [16384,1]
     [[{{node model_69/dense_50/BiasAdd}}]]

I tried to add a reshape in the discriminator but with no success.我试图在鉴别器中添加一个重塑,但没有成功。

My images have dimensions: (64, 64, 3)我的图像有尺寸: (64, 64, 3)

#Generator
def generator(gen_inputs):
    # 4x4x1024
    inputs = Input(shape=(gen_inputs,))
    x = Dense(4 * 4 * 1024, activation='relu')(inputs)
    x = Reshape((4, 4, 1024))(x)
    x = BatchNormalization()(x)

    # 8x8x512
    x = UpSampling2D()(x)
    x = Conv2D(512, (5, 5), strides=(2, 2), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 16x16x256
    x = UpSampling2D()(x)
    x = Conv2D(256, (5, 5), strides=(2, 2), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 32x32x128
    x = UpSampling2D()(x)
    x = Conv2D(128, (5, 5), strides=(2, 2), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 64x64x3
    x = UpSampling2D()(x)
    out = Conv2D(3, (5, 5), strides=(2, 2), activation='tanh', padding='same')(x)

    return Model(inputs, out)

def discriminator(discr_inputs):

    # 32x32x128
    x = Conv2D(128, (5, 5), strides=(2, 2), padding='same')(discr_inputs)
    x = LeakyReLU()(x)
    x = BatchNormalization()(x)

    # 16x16x256
    x = Conv2D(256, (5, 5), strides=(2, 2), padding='same')(x)
    x = LeakyReLU()(x)
    x = BatchNormalization()(x)

    # 8x8x512
    x = Conv2D(512, (5, 5), strides=(2, 2), padding='same')(x)
    x = LeakyReLU()(x)
    x = BatchNormalization()(x)

    # 4x4x1024
    x = Conv2D(1024, (5, 5), strides=(2, 2), padding='same')(x)
    x = LeakyReLU()(x)
    x = BatchNormalization()(x)

    x = Flatten()(x)
    #x = Reshape((-1,))(x)
    out = Dense(1, activation='sigmoid')(x)

    return Model(discr_inputs, out)


def build_gan(gen_inputs, discr_inputs, optimizer):
    # discriminator
    discr = discriminator(discr_inputs)
    discr.compile(loss='binary_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    # generator
    gen = generator(gen_inputs)
    gen.compile(loss='binary_crossentropy',
                optimizer=optimizer)

    print('discriminator:', discr.summary())
    # setup    
    z = Input(shape=(100,))
    img = gen(z)

    discr.trainable = False
    discr_out = discr(img)

    print('discriminator_out:', discr_out)

    model = Model(z, discr_out)
    model.compile(loss='binary_crossentropy',
                  optimizer=optimizer)

    print('gan modell:', model.summary())
    return model


def train(gen_inputs, discr_inputs, optimizer, epochs, image_path, batch_size, save_interval=50):

    gan = build_gan(gen_inputs, discr_inputs, optimizer)
    X_train = load_imgs()

    # Rescale images from -1 to 1
    X_train = (X_train.astype(np.float32) - 127.5) / 127.5

    half_batch = batch_size // 2

    for epoch in range(epochs):

        # Train Generator
        noise = np.random.normal(0, 1, (batch_size, 100))
        gen_loss = gan.train_on_batch(noise, np.ones((batch_size, 1)))

        # Train Discriminator
        idx = np.random.randint(0, X_train.shape[0], half_batch)
        imgs = X_train[idx]

        # Sample noise 
        noise = np.random.normal(0, 1, (half_batch, 100))
        gen_imgs = gen.predict(noise)

        # Train the discriminator 
        discr_loss_real = discr.train_on_batch(imgs, np.ones((half_batch, 1)))
        discr_loss_fake = discr.train_on_batch(gen_imgs, np.zeros((half_batch, 1)))
        discr_loss = 0.5 * np.add(discr_loss_real, discr_loss_fake)




gen_inputs = 100
discr_inputs = Input(shape=(images.shape[1:])
batch_size = 16

discriminator:鉴别器:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_59 (InputLayer)        (None, 64, 64, 3)         0         
_________________________________________________________________
conv2d_201 (Conv2D)          (None, 32, 32, 128)       9728      
_________________________________________________________________
leaky_re_lu_99 (LeakyReLU)   (None, 32, 32, 128)       0         
_________________________________________________________________
batch_normalization_200 (Bat (None, 32, 32, 128)       512       
_________________________________________________________________
conv2d_202 (Conv2D)          (None, 16, 16, 256)       819456    
_________________________________________________________________
leaky_re_lu_100 (LeakyReLU)  (None, 16, 16, 256)       0         
_________________________________________________________________
batch_normalization_201 (Bat (None, 16, 16, 256)       1024      
_________________________________________________________________
conv2d_203 (Conv2D)          (None, 8, 8, 512)         3277312   
_________________________________________________________________
leaky_re_lu_101 (LeakyReLU)  (None, 8, 8, 512)         0         
_________________________________________________________________
batch_normalization_202 (Bat (None, 8, 8, 512)         2048      
_________________________________________________________________
conv2d_204 (Conv2D)          (None, 4, 4, 1024)        13108224  
_________________________________________________________________
leaky_re_lu_102 (LeakyReLU)  (None, 4, 4, 1024)        0         
_________________________________________________________________
batch_normalization_203 (Bat (None, 4, 4, 1024)        4096      
_________________________________________________________________
flatten_25 (Flatten)         (None, 16384)             0         
_________________________________________________________________
dense_50 (Dense)             (None, 1)                 16385     
=================================================================
Total params: 17,238,785
Trainable params: 17,234,945
Non-trainable params: 3,840


discriminator_out: Tensor("model_69/dense_50/Sigmoid:0", shape=(?, 1), dtype=float32)

gan model:甘型号:

Layer (type)                 Output Shape              Param #   
=================================================================
input_61 (InputLayer)        (None, 100)               0         
_________________________________________________________________
model_70 (Model)             (None, 4, 4, 3)           18876163  
_________________________________________________________________
model_69 (Model)             (None, 1)                 17238785  
=================================================================
Total params: 36,114,948
Trainable params: 18,872,323
Non-trainable params: 17,242,625

The generator's output tensor has shape (None, 4, 4, 3) , which is different from the expected shape (None, 64, 64, 3) .生成器的输出张量的形状为(None, 4, 4, 3) ,与预期的形状(None, 64, 64, 3) This happens due to the use of strided convolutions.这是由于使用了跨步卷积。

The following generator produces images of dimensions 64x64x3:以下生成器生成尺寸为 64x64x3 的图像:

def generator(gen_inputs):
    # 4x4x1024
    inputs = Input(shape=(gen_inputs,))
    x = Dense(4 * 4 * 1024, activation='relu')(inputs)
    x = Reshape((4, 4, 1024))(x)
    x = BatchNormalization()(x)

    # 8x8x512
    x = UpSampling2D()(x)
    x = Conv2D(512, (5, 5), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 16x16x256
    x = UpSampling2D()(x)
    x = Conv2D(256, (5, 5), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 32x32x128
    x = UpSampling2D()(x)
    x = Conv2D(128, (5, 5), activation='relu', padding='same')(x)
    x = BatchNormalization()(x)

    # 64x64x3
    x = UpSampling2D()(x)
    out = Conv2D(3, (5, 5), activation='tanh', padding='same')(x)

    return Model(inputs, out)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 矩阵尺寸不兼容 - Keras Tensorflow - Matrix size-incompatible - Keras Tensorflow Tensorflow:张量上的矩阵大小不兼容错误 - Tensorflow: Matrix size-incompatible error on Tensors Tensorflow keras 矩阵大小与极其简单的模型不兼容 - Tensorflow keras Matrix size-incompatible with extremely simple model InvalidArgumentError: 矩阵大小不兼容: In[0]: [32,21], In[1]: [128,1] - InvalidArgumentError: Matrix size-incompatible: In[0]: [32,21], In[1]: [128,1] 尝试创建GAN:InvalidArgumentError:矩阵大小不兼容 - Trying to create GAN: InvalidArgumentError: Matrix size-incompatible Keras Model.predict 返回错误“矩阵大小不兼容” - Keras Model.predict returns the error 'Matrix size-incompatible' 矩阵大小不兼容:In[0]:[47,1000],In[1]:[4096,256] - Matrix size-incompatible: In[0]: [47,1000], In[1]: [4096,256] Keras Python脚本有时运行正常,有时会因Matrix大小不兼容而失败:在[0]:[10000,1],In [1]:[3,1] - Keras Python script sometimes runs fine, sometimes fails with Matrix size-incompatible: In[0]: [10000,1], In[1]: [3,1] 为什么删除张量中的一维会导致InvalidArgumentError:矩阵大小不兼容? - Why deleting one dimension in tensor causes InvalidArgumentError: Matrix size-incompatible? 如何解决此错误:矩阵大小不兼容:在[0]:[1,786432],在[1]中:[784,512] [[{{node MatMul}}]]> - How to solve this error: Matrix size-incompatible: In[0]: [1,786432], In[1]: [784,512] [[{{node MatMul}}]]>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM