LA Lab
LA Lab
ipynb - Colab
def build_generator():
model = Sequential()
model.add(Dense(256, input_dim=latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(img_shape), activation='tanh'))
model.add(Reshape(img_shape))
model.summary() return
model generator =
build_generator()
model.add(Flatten(input_shape=img_shape))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(256)) model.add(Dense(1,
activation='sigmoid'))
model.summary()
return model
https://colab.research.google.com/drive/1wLkoofVmDXV2y_9zyorrvMDhay4CTsx_#scrollTo=nr3ZG_uF0sEu&printMode=true 1/4
10/10/24, 4:48 PM Untitled14.ipynb - Colab
discriminator = build_discriminator() discriminator.compile(loss='binary_crossentropy',
optimizer=adam, metrics=['accuracy'])
GAN = Sequential()
discriminator.trainable = False
GAN.add(generator)
GAN.add(discriminator)
GAN.compile(loss='binary_crossentropy', optimizer=adam)
GAN.summary()
Model: "sequential_2"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ sequential (Sequential) │ (None, 28, 28, 1) │ 362,000 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ sequential_1 (Sequential) │ (None, 1) │ 533,505 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 895,505 (3.42 MB)
Trainable params: 360,464 (1.38 MB)
Non-trainable params: 535,041 (2.04 MB)
def save_imgs(epoch):
r, c = 5, 5 noise = np.random.normal(0, 1, (r *
c, latent_dim)) gen_imgs = generator.predict(noise)
global save_name save_name += 0.00000001
print("%.8f" % save_name)
# Rescale images 0 - 1
gen_imgs = 0.5 * gen_imgs + 0.5
fig.savefig("generated_images/%.8f.png" % save_name)
print('saved') plt.close()
https://colab.research.google.com/drive/1wLkoofVmDXV2y_9zyorrvMDhay4CTsx_#scrollTo=nr3ZG_uF0sEu&printMode=true 2/4
10/10/24, 4:48 PM Untitled14.ipynb - Colab
# Generate Fake Images noise =
np.random.normal(0, 1, (batch_size, latent_dim))
gen_imgs = generator.predict(noise)
if (epoch % save_interval) == 0:
save_imgs(epoch)
train(950 , batch_size= 64, save_interval= 200)
https://colab.research.google.com/drive/1wLkoofVmDXV2y_9zyorrvMDhay4CTsx_#scrollTo=nr3ZG_uF0sEu&printMode=true 3/4
10/10/24, 4:48 PM Untitled14.ipynb - Colab
/ / p
******* 948 [D loss: 2.605092, acc: 29.91%] [G loss : 2.606087]
2/2 ━━━━━━━━━━━━━━━━━━━━ 0s 9ms/step
******* 949 [D loss: 2.606057, acc: 29.91%] [G loss : 2.607045]
anim_file = 'dcgan.gif'
<ipython-input-8-5d911b6b7554>:11: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of
image = imageio.imread(filename)
<ipython-input-8-5d911b6b7554>:13: DeprecationWarning: Starting with ImageIO v3 the behavior of this function will switch to that of
image = imageio.imread(filename)
https://colab.research.google.com/drive/1wLkoofVmDXV2y_9zyorrvMDhay4CTsx_#scrollTo=nr3ZG_uF0sEu&printMode=true 4/4