Skip to content

Commit

Permalink
update generated files for gan example
Browse files Browse the repository at this point in the history
  • Loading branch information
fchollet committed Jan 4, 2021
1 parent 5fe9695 commit 46e57b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 7 additions & 12 deletions examples/generative/ipynb/dcgan_overriding_train_step.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"colab_type": "text"
},
"source": [
"Create a dataset from our folder:"
"Create a dataset from our folder, and rescale the images to the [0-1] range:"
]
},
{
Expand All @@ -90,6 +90,7 @@
"dataset = keras.preprocessing.image_dataset_from_directory(\n",
" \"celeba_gan\", label_mode=None, image_size=(64, 64), batch_size=32\n",
")\n",
"dataset = dataset.map(lambda x: x / 255.0)\n",
""
]
},
Expand Down Expand Up @@ -147,12 +148,11 @@
" layers.Conv2D(128, kernel_size=4, strides=2, padding=\"same\"),\n",
" layers.LeakyReLU(alpha=0.2),\n",
" layers.Flatten(),\n",
" layers.Dropout(0.5),\n",
" layers.Dropout(0.2),\n",
" layers.Dense(1, activation=\"sigmoid\"),\n",
" ],\n",
" name=\"discriminator\",\n",
")\n",
"\n",
"discriminator.summary()"
]
},
Expand Down Expand Up @@ -192,7 +192,6 @@
" ],\n",
" name=\"generator\",\n",
")\n",
"\n",
"generator.summary()"
]
},
Expand Down Expand Up @@ -234,8 +233,6 @@
" return [self.d_loss_metric, self.g_loss_metric]\n",
"\n",
" def train_step(self, real_images):\n",
" if isinstance(real_images, tuple):\n",
" real_images = real_images[0]\n",
" # Sample random points in the latent space\n",
" batch_size = tf.shape(real_images)[0]\n",
" random_latent_vectors = tf.random.normal(shape=(batch_size, self.latent_dim))\n",
Expand Down Expand Up @@ -276,6 +273,7 @@
" grads = tape.gradient(g_loss, self.generator.trainable_weights)\n",
" self.g_optimizer.apply_gradients(zip(grads, self.generator.trainable_weights))\n",
"\n",
" # Update metrics\n",
" self.d_loss_metric.update_state(d_loss)\n",
" self.g_loss_metric.update_state(g_loss)\n",
" return {\n",
Expand Down Expand Up @@ -340,16 +338,13 @@
"\n",
"gan = GAN(discriminator=discriminator, generator=generator, latent_dim=latent_dim)\n",
"gan.compile(\n",
" d_optimizer=keras.optimizers.Adam(learning_rate=0.0003),\n",
" g_optimizer=keras.optimizers.Adam(learning_rate=0.0003),\n",
" d_optimizer=keras.optimizers.Adam(learning_rate=0.0001),\n",
" g_optimizer=keras.optimizers.Adam(learning_rate=0.0001),\n",
" loss_fn=keras.losses.BinaryCrossentropy(),\n",
")\n",
"\n",
"gan.fit(\n",
" dataset,\n",
" epochs=epochs,\n",
" verbose=2,\n",
" callbacks=[GANMonitor(num_img=3, latent_dim=latent_dim)],\n",
" dataset, epochs=epochs, callbacks=[GANMonitor(num_img=10, latent_dim=latent_dim)]\n",
")"
]
},
Expand Down
30 changes: 10 additions & 20 deletions examples/generative/md/dcgan_overriding_train_step.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ with ZipFile("celeba_gan/data.zip", "r") as zipobj:
zipobj.extractall("celeba_gan")
```

Create a dataset from our folder:
Create a dataset from our folder, and rescale the images to the [0-1] range:


```python
dataset = keras.preprocessing.image_dataset_from_directory(
"celeba_gan", label_mode=None, image_size=(64, 64), batch_size=32
)
dataset = dataset.map(lambda x: x / 255.0)

```

Expand All @@ -70,13 +71,8 @@ for x in dataset:

```

<div class="k-default-codeblock">
```
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

```
</div>
![png](/img/examples/generative/dcgan_overriding_train_step/dcgan_overriding_train_step_8_1.png)
![png](/img/examples/generative/dcgan_overriding_train_step/dcgan_overriding_train_step_8_0.png)


---
Expand All @@ -96,12 +92,11 @@ discriminator = keras.Sequential(
layers.Conv2D(128, kernel_size=4, strides=2, padding="same"),
layers.LeakyReLU(alpha=0.2),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dropout(0.2),
layers.Dense(1, activation="sigmoid"),
],
name="discriminator",
)

discriminator.summary()
```

Expand Down Expand Up @@ -160,7 +155,6 @@ generator = keras.Sequential(
],
name="generator",
)

generator.summary()
```

Expand Down Expand Up @@ -221,8 +215,6 @@ class GAN(keras.Model):
return [self.d_loss_metric, self.g_loss_metric]

def train_step(self, real_images):
if isinstance(real_images, tuple):
real_images = real_images[0]
# Sample random points in the latent space
batch_size = tf.shape(real_images)[0]
random_latent_vectors = tf.random.normal(shape=(batch_size, self.latent_dim))
Expand Down Expand Up @@ -263,6 +255,7 @@ class GAN(keras.Model):
grads = tape.gradient(g_loss, self.generator.trainable_weights)
self.g_optimizer.apply_gradients(zip(grads, self.generator.trainable_weights))

# Update metrics
self.d_loss_metric.update_state(d_loss)
self.g_loss_metric.update_state(g_loss)
return {
Expand Down Expand Up @@ -303,24 +296,21 @@ epochs = 1 # In practice, use ~100 epochs

gan = GAN(discriminator=discriminator, generator=generator, latent_dim=latent_dim)
gan.compile(
d_optimizer=keras.optimizers.Adam(learning_rate=0.0003),
g_optimizer=keras.optimizers.Adam(learning_rate=0.0003),
d_optimizer=keras.optimizers.Adam(learning_rate=0.0001),
g_optimizer=keras.optimizers.Adam(learning_rate=0.0001),
loss_fn=keras.losses.BinaryCrossentropy(),
)

gan.fit(
dataset,
epochs=epochs,
verbose=2,
callbacks=[GANMonitor(num_img=3, latent_dim=latent_dim)],
dataset, epochs=epochs, callbacks=[GANMonitor(num_img=10, latent_dim=latent_dim)]
)
```

<div class="k-default-codeblock">
```
6332/6332 - 593s - d_loss: -1.8418e+08 - g_loss: 17124297728.0000
6332/6332 [==============================] - 605s 96ms/step - d_loss: 0.6113 - g_loss: 1.1976
<tensorflow.python.keras.callbacks.History at 0x7f8442785250>
<tensorflow.python.keras.callbacks.History at 0x7f4eb5d055d0>
```
</div>
Expand Down

0 comments on commit 46e57b3

Please sign in to comment.