在更改GAN网络中第一层的输入时遇到了一些麻烦(脚本在下面)。好像我只能输入尺寸为4的倍数的图像(我不确定为什么)。事实证明这是个问题,因为我想输入9x9的图像。
您有什么建议吗?请注意,我是初学者,我们将为您提供帮助。
脚本:
images_dir = "dcgan_images"
img_rows = 9
img_cols = 9
channels = 1
noise_len = 100
X_train = np.empty((6000, 9, 9), dtype=numpy.uint8)
for i in range(6000):
col_1 = randint(0, 255)
col_2 = randint(0, 255)
col_3 = randint(0, 255)
col_4 = randint(0, 255)
col_5 = randint(0, 255)
col_6 = randint(0, 255)
col_7 = randint(0, 255)
col_8 = randint(0, 255)
col_9 = randint(0, 255)
image = numpy.array([
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
[col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9],
], dtype=numpy.uint8)
X_train[i] = image
def build_discriminator():
'''
Put together a CNN that will return a single confidence output.
returns: the model object
'''
img_shape = (img_rows, img_cols, channels)
model = Sequential()
model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=img_shape, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=3, strides=2, padding="same"))
model.add(ZeroPadding2D(padding=((0, 1), (0, 1))))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(256, kernel_size=3, strides=1, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
return model
def build_generator():
'''
Put together a model that takes in one-dimensional noise and outputs two-dimensional
data representing a black and white image, with -1 for black and 1 for white.
returns: the model object
'''
noise_shape = (noise_len,)
model = Sequential()
model.add(Dense(128 * 1 * 1, activation="relu", input_shape=noise_shape))
model.add(Reshape((1, 1, 128)))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(128, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(1, kernel_size=3, padding="same"))
model.add(Activation("tanh"))
for layer in model.layers:
print (layer.output_shape)
model.summary()
return model
def build_combined():
'''
Puts together a model that combines the discriminator and generator models.
returns: the generator, discriminator, and combined model objects
'''
optimizer = Adam(0.0002, 0.5)
# Build and compile the discriminator
discriminator = build_discriminator()
discriminator.trainable = False
discriminator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
# Build and compile the generator
generator = build_generator()
generator.compile(loss='binary_crossentropy', optimizer=optimizer)
# The generator takes noise as input and generates images
noise = Input(shape=(noise_len,))
img = generator(noise)
# For the combined model we will only train the generator
# discriminator.trainable = False
# The discriminator takes generated images as input and determines validity
valid = discriminator(img)
# The combined model (stacked generator and discriminator) takes
# noise as input => generates images => determines validity
combined = Model(inputs=noise, outputs=valid)
combined.compile(loss='binary_crossentropy', optimizer=optimizer)
return generator, discriminator, combined