求助:为什么相同的代码,在1.x版的tensorflow跑与在2.x版的tensorflow跑的结果不相同?
发布于 1月前 作者 OnceMonkey 来自问答

碰到一个很奇怪的问题。最近在学习GAN方面的问题,我一开始是使用最新版本的tensorflow2.0跑的程序,生成的图片总是崩坏。类似像这样:


训练非常多代后甚至图片最后只生成全部一模一样的方块图了。

而我使用tensorflow1.13.2跑相同的代码,没有改一个字,结果就完全不同了,效果远好于在2.0上跑:



程序采用的是DCGAN,代码如下:

from __future__ import print_function, division

from tensorflow.keras.layers import *
from tensorflow.keras.models import *
from tensorflow.keras.optimizers import *

import matplotlib.pyplot as plt


import numpy as np
import cv2



class DCGAN():
def __init__(self):
self.img_rows = 64
self.img_cols = 64
self.channels = 3
self.img_shape = (self.img_rows, self.img_cols, self.channels)
self.latent_dim = 512
self.sample_path='images'
self.model_path='model'

optimizer = Adam(0.00002, 0.5)

self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])

self.generator = self.build_generator()

self.combined=self.build_GD(self.generator,self.discriminator)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)

def build_generator(self):

model = Sequential()
model.add(Dense(128 * 4 * 4, activation="relu", input_dim=self.latent_dim))
model.add(Reshape((4, 4, 128)))

num_conv=int(np.log2(self.img_shape[0]/4))
for i in range(num_conv):
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=3, padding="same"))
model.add(BatchNormalization(momentum=0.8))
model.add(Activation("relu"))

model.add(Conv2D(self.channels, kernel_size=3, padding="same"))
model.add(Activation("tanh"))

model.summary()
return model

def build_discriminator(self):
model = Sequential()
model.add(Conv2D(32, kernel_size=3, strides=2, input_shape=self.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(BatchNormalization(momentum=0.8))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
model.add(BatchNormalization(momentum=0.8))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=3, strides=1, padding="same"))
model.add(BatchNormalization(momentum=0.8))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))

model.summary()
return model
def build_GD(self,G,D):
z = Input(shape=(self.latent_dim,))
img = G(z)
D.trainable = False
valid = self.discriminator(img)
GD = Model(z, valid)

return GD

def train(self,ds_x,epochs, batch_size=32, sample_interval=500,save_interval=2000):
real = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
for epoch in range(epochs):
idx = np.random.randint(0, ds_x.shape[0], batch_size)
real_images = ds_x[idx]
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
gen_imgs = self.generator.predict(noise)

d_loss_real = self.discriminator.train_on_batch(real_images, real)
d_loss_fake = self.discriminator.train_on_batch(gen_imgs, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)

g_loss = self.combined.train_on_batch(noise, real)

print("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100 * d_loss[1], g_loss))
if epoch % sample_interval == 0:self.sample_images(self.generator,self.sample_path,epoch)
if epoch % save_interval == 0:self.save_generator(self.model_path)

def sample_images(self,G,save_path,epoch=0):
r, c = 5, 5
noise = np.random.normal(0, 1, (r * c, self.latent_dim))
gen_imgs = G.predict(noise)

# Rescale images 0 - 1
gen_imgs = 0.5 * gen_imgs + 0.5

fig, axs = plt.subplots(r, c)
cnt = 0
for i in range(r):
for j in range(c):
gen_imgs[cnt] = cv2.cvtColor(gen_imgs[cnt], cv2.COLOR_BGR2RGB)
axs[i, j].imshow(gen_imgs[cnt])
axs[i, j].axis('off')
cnt += 1
fig.savefig("%s/%d.png" % (save_path,epoch))
plt.close()

def save_generator(self,save_path):
self.generator.save('%s/G.h5'%save_path)
self.discriminator.save('%s/D.h5'%save_path)
self.combined.save('%s/GD.h5'%save_path)



if __name__ == '__main__':
images = np.load(r'E:\Data\anime_girls\numpy\anime_girls_64x64.npy')
print(images.shape)
images = (images.astype(np.float32) - 127.5) / 127.5

dcgan=DCGAN()
dcgan.train(images,epochs=10000*100)



模型放在百度云盘上了:

1回复

晕,问题发表了居然还不能修改了。 补下数据集链接:https://pan.baidu.com/s/1jKHdfjhiX6i5GnQqs7ckgQ 提取码:726w 这个数据集包含3w+张动漫头像,有兴趣的朋友也可以用来生成自己的动漫头像。 联系方式:853349556@qq.com

回到顶部