<jupyter_start><jupyter_text>Run model module locally<jupyter_code>arguments = {}
# File arguments.
arguments["train_file_pattern"] = "gs://machine-learning-1234-bucket/gan/data/mnist/train*.tfrecord"
arguments["eval_file_pattern"] = "gs://machine-learning-1234-bucket/gan/data/mnist/test*.tfrecord"
arguments["output_dir"] = "gs://machine-learning-1234-bucket/gan/dcgan/tf2/trained_model"

# Training parameters.
arguments["tf_version"] = 2.2
arguments["use_graph_mode"] = True
arguments["distribution_strategy"] = ""
arguments["write_summaries"] = False
arguments["num_epochs"] = 30
arguments["train_dataset_length"] = 60000
arguments["train_batch_size"] = 32
arguments["log_step_count_steps"] = 100
arguments["save_summary_steps"] = 100
arguments["save_checkpoints_steps"] = 10000
arguments["keep_checkpoint_max"] = 10
arguments["input_fn_autotune"] = False

# Eval parameters.
arguments["eval_batch_size"] = 32
arguments["eval_steps"] = 100

# Image parameters.
arguments["height"] = 28
arguments["width"] = 28
arguments["depth"] = 1

# Generator parameters.
arguments["latent_size"] = 512
arguments["generator_projection_dims"] = [7, 7, 256]
arguments["generator_num_filters"] = [128, 128, arguments["depth"]]
arguments["generator_kernel_sizes"] = [4, 4, 3]
arguments["generator_strides"] = [2, 2, 1]
arguments["generator_leaky_relu_alpha"] = 0.2
arguments["generator_final_activation"] = "tanh"
arguments["generator_l1_regularization_scale"] = 0.
arguments["generator_l2_regularization_scale"] = 0.
arguments["generator_optimizer"] = "Adam"
arguments["generator_learning_rate"] = 0.0002
arguments["generator_adam_beta1"] = 0.5
arguments["generator_adam_beta2"] = 0.999
arguments["generator_adam_epsilon"] = 1e-8
arguments["generator_clip_gradients"] = None
arguments["generator_train_steps"] = 1

# Discriminator hyperparameters.
arguments["discriminator_num_filters"] = [64, 128, 128, 256]
arguments["discriminator_kernel_sizes"] = [3, 3, 3, 3]
arguments["discriminator_strides"] = [1, 2, 2, 2]
arguments["discriminator_dropout_rates"] = [0.3, 0.3, 0.3, 0.3]
arguments["discriminator_leaky_relu_alpha"] = 0.2
arguments["discriminator_l1_regularization_scale"] = 0.
arguments["discriminator_l2_regularization_scale"] = 0.
arguments["discriminator_optimizer"] = "Adam"
arguments["discriminator_learning_rate"] = 0.0002
arguments["discriminator_adam_beta1"] = 0.5
arguments["discriminator_adam_beta2"] = 0.999
arguments["discriminator_adam_epsilon"] = 1e-8
arguments["discriminator_clip_gradients"] = None
arguments["discriminator_train_steps"] = 1
arguments["label_smoothing"] = 0.9

import os

# Import os environment variables for file hyperparameters.
os.environ["TRAIN_FILE_PATTERN"] = arguments["train_file_pattern"]
os.environ["EVAL_FILE_PATTERN"] = arguments["eval_file_pattern"]
os.environ["OUTPUT_DIR"] = arguments["output_dir"]

# Import os environment variables for train hyperparameters.
os.environ["TF_VERSION"] = str(arguments["tf_version"])
os.environ["USE_GRAPH_MODE"] = str(arguments["use_graph_mode"])
os.environ["DISTRIBUTION_STRATEGY"] = arguments["distribution_strategy"]
os.environ["WRITE_SUMMARIES"] = str(arguments["write_summaries"])
os.environ["NUM_EPOCHS"] = str(arguments["num_epochs"])
os.environ["TRAIN_DATASET_LENGTH"] = str(arguments["train_dataset_length"])
os.environ["TRAIN_BATCH_SIZE"] = str(arguments["train_batch_size"])
os.environ["LOG_STEP_COUNT_STEPS"] = str(arguments["log_step_count_steps"])
os.environ["SAVE_SUMMARY_STEPS"] = str(arguments["save_summary_steps"])
os.environ["SAVE_CHECKPOINTS_STEPS"] = str(arguments["save_checkpoints_steps"])
os.environ["KEEP_CHECKPOINT_MAX"] = str(arguments["keep_checkpoint_max"])
os.environ["INPUT_FN_AUTOTUNE"] = str(arguments["input_fn_autotune"])

# Import os environment variables for eval hyperparameters.
os.environ["EVAL_BATCH_SIZE"] = str(arguments["eval_batch_size"])
os.environ["EVAL_STEPS"] = str(arguments["eval_steps"])

# Import os environment variables for image hyperparameters.
os.environ["HEIGHT"] = str(arguments["height"])
os.environ["WIDTH"] = str(arguments["width"])
os.environ["DEPTH"] = str(arguments["depth"])

# Import os environment variables for generator hyperparameters.
os.environ["LATENT_SIZE"] = str(arguments["latent_size"])
os.environ["GENERATOR_PROJECTION_DIMS"] = ",".join(
    [str(x) for x in arguments["generator_projection_dims"]]
)
os.environ["GENERATOR_NUM_FILTERS"] = ",".join(
    [str(x) for x in arguments["generator_num_filters"]]
)
os.environ["GENERATOR_KERNEL_SIZES"] = ",".join(
    [str(x) for x in arguments["generator_kernel_sizes"]]
)
os.environ["GENERATOR_STRIDES"] = ",".join(
    [str(x) for x in arguments["generator_strides"]]
)
os.environ["GENERATOR_LEAKY_RELU_ALPHA"] = str(arguments["generator_leaky_relu_alpha"])
os.environ["GENERATOR_FINAL_ACTIVATION"] = arguments["generator_final_activation"]
os.environ["GENERATOR_L1_REGULARIZATION_SCALE"] = str(arguments["generator_l1_regularization_scale"])
os.environ["GENERATOR_L2_REGULARIZATION_SCALE"] = str(arguments["generator_l2_regularization_scale"])
os.environ["GENERATOR_OPTIMIZER"] = arguments["generator_optimizer"]
os.environ["GENERATOR_LEARNING_RATE"] = str(arguments["generator_learning_rate"])
os.environ["GENERATOR_ADAM_BETA1"] = str(arguments["generator_adam_beta1"])
os.environ["GENERATOR_ADAM_BETA2"] = str(arguments["generator_adam_beta2"])
os.environ["GENERATOR_ADAM_EPSILON"] = str(arguments["generator_adam_epsilon"])
os.environ["GENERATOR_CLIP_GRADIENTS"] = str(arguments["generator_clip_gradients"])
os.environ["GENERATOR_TRAIN_STEPS"] = str(arguments["generator_train_steps"])

# Import os environment variables for discriminator hyperparameters.
os.environ["DISCRIMINATOR_NUM_FILTERS"] = ",".join(
    [str(x) for x in arguments["discriminator_num_filters"]]
)
os.environ["DISCRIMINATOR_KERNEL_SIZES"] = ",".join(
    [str(x) for x in arguments["discriminator_kernel_sizes"]]
)
os.environ["DISCRIMINATOR_STRIDES"] = ",".join(
    [str(x) for x in arguments["discriminator_strides"]]
)
os.environ["DISCRIMINATOR_DROPOUT_RATES"] = ",".join(
    [str(x) for x in arguments["discriminator_dropout_rates"]]
)
os.environ["DISCRIMINATOR_LEAKY_RELU_ALPHA"] = str(arguments["discriminator_leaky_relu_alpha"])
os.environ["DISCRIMINATOR_L1_REGULARIZATION_SCALE"] = str(arguments["discriminator_l1_regularization_scale"])
os.environ["DISCRIMINATOR_L2_REGULARIZATION_SCALE"] = str(arguments["discriminator_l2_regularization_scale"])
os.environ["DISCRIMINATOR_OPTIMIZER"] = arguments["discriminator_optimizer"]
os.environ["DISCRIMINATOR_LEARNING_RATE"] = str(arguments["discriminator_learning_rate"])
os.environ["DISCRIMINATOR_ADAM_BETA1"] = str(arguments["discriminator_adam_beta1"])
os.environ["DISCRIMINATOR_ADAM_BETA2"] = str(arguments["discriminator_adam_beta2"])
os.environ["DISCRIMINATOR_ADAM_EPSILON"] = str(arguments["discriminator_adam_epsilon"])
os.environ["DISCRIMINATOR_CLIP_GRADIENTS"] = str(arguments["discriminator_clip_gradients"])
os.environ["DISCRIMINATOR_TRAIN_STEPS"] = str(arguments["discriminator_train_steps"])
os.environ["LABEL_SMOOTHING"] = str(arguments["label_smoothing"])<jupyter_output><empty_output><jupyter_text>Train DCGAN model<jupyter_code>%%bash
gsutil -m rm -rf ${OUTPUT_DIR}
export PYTHONPATH=$PYTHONPATH:$PWD/dcgan_class_ctl_module
python3 -m trainer.task \
    --train_file_pattern=${TRAIN_FILE_PATTERN} \
    --eval_file_pattern=${EVAL_FILE_PATTERN} \
    --output_dir=${OUTPUT_DIR} \
    --job-dir=./tmp \
    \
    --tf_version=${TF_VERSION} \
    --use_graph_mode=${USE_GRAPH_MODE} \
    --distribution_strategy=${DISTRIBUTION_STRATEGY} \
    --write_summaries=${WRITE_SUMMARIES} \
    --num_epochs=${NUM_EPOCHS} \
    --train_dataset_length=${TRAIN_DATASET_LENGTH} \
    --train_batch_size=${TRAIN_BATCH_SIZE} \
    --log_step_count_steps=${LOG_STEP_COUNT_STEPS} \
    --save_summary_steps=${SAVE_SUMMARY_STEPS} \
    --save_checkpoints_steps=${SAVE_CHECKPOINTS_STEPS} \
    --keep_checkpoint_max=${KEEP_CHECKPOINT_MAX} \
    --input_fn_autotune=${INPUT_FN_AUTOTUNE} \
    \
    --eval_batch_size=${EVAL_BATCH_SIZE} \
    --eval_steps=${EVAL_STEPS} \
    \
    --height=${HEIGHT} \
    --width=${WIDTH} \
    --depth=${DEPTH} \
    \
    --latent_size=${LATENT_SIZE} \
    --generator_projection_dims=${GENERATOR_PROJECTION_DIMS} \
    --generator_num_filters=${GENERATOR_NUM_FILTERS} \
    --generator_kernel_sizes=${GENERATOR_KERNEL_SIZES} \
    --generator_strides=${GENERATOR_STRIDES} \
    --generator_leaky_relu_alpha=${GENERATOR_LEAKY_RELU_ALPHA} \
    --generator_final_activation=${GENERATOR_FINAL_ACTIVATION} \
    --generator_l1_regularization_scale=${GENERATOR_L1_REGULARIZATION_SCALE} \
    --generator_l2_regularization_scale=${GENERATOR_L2_REGULARIZATION_SCALE} \
    --generator_optimizer=${GENERATOR_OPTIMIZER} \
    --generator_learning_rate=${GENERATOR_LEARNING_RATE} \
    --generator_adam_beta1=${GENERATOR_ADAM_BETA1} \
    --generator_adam_beta2=${GENERATOR_ADAM_BETA2} \
    --generator_adam_epsilon=${GENERATOR_ADAM_EPSILON} \
    --generator_clip_gradients=${GENERATOR_CLIP_GRADIENTS} \
    --generator_train_steps=${GENERATOR_TRAIN_STEPS} \
    \
    --discriminator_num_filters=${DISCRIMINATOR_NUM_FILTERS} \
    --discriminator_kernel_sizes=${DISCRIMINATOR_KERNEL_SIZES} \
    --discriminator_strides=${DISCRIMINATOR_STRIDES} \
    --discriminator_dropout_rates=${DISCRIMINATOR_DROPOUT_RATES} \
    --discriminator_leaky_relu_alpha=${DISCRIMINATOR_LEAKY_RELU_ALPHA} \
    --discriminator_l1_regularization_scale=${DISCRIMINATOR_L1_REGULARIZATION_SCALE} \
    --discriminator_l2_regularization_scale=${DISCRIMINATOR_L2_REGULARIZATION_SCALE} \
    --discriminator_optimizer=${DISCRIMINATOR_OPTIMIZER} \
    --discriminator_learning_rate=${DISCRIMINATOR_LEARNING_RATE} \
    --discriminator_adam_beta1=${DISCRIMINATOR_ADAM_BETA1} \
    --discriminator_adam_beta2=${DISCRIMINATOR_ADAM_BETA2} \
    --discriminator_adam_epsilon=${DISCRIMINATOR_ADAM_EPSILON} \
    --discriminator_clip_gradients=${DISCRIMINATOR_CLIP_GRADIENTS} \
    --discriminator_train_steps=${DISCRIMINATOR_TRAIN_STEPS} \
    --label_smoothing=${LABEL_SMOOTHING}<jupyter_output>{'train_file_pattern': 'gs://machine-learning-1234-bucket/gan/data/mnist/train*.tfrecord', 'eval_file_pattern': 'gs://machine-learning-1234-bucket/gan/data/mnist/test*.tfrecord', 'output_dir': 'gs://machine-learning-1234-bucket/gan/dcgan/tf2/trained_model2/', 'tf_version': 2.2, 'use_graph_mode': True, 'distribution_strategy': '', 'write_summaries': False, 'num_epochs': 30, 'train_dataset_length': 60000, 'train_batch_size': 32, 'log_step_count_steps': 100, 'save_summary_steps': 100, 'save_checkpoints_steps': 10000, 'keep_checkpoint_max': 10, 'input_fn_autotune': False, 'eval_batch_size': 32, 'eval_steps': 100, 'height': 28, 'width': 28, 'depth': 1, 'latent_size': 512, 'generator_projection_dims': [7, 7, 256], 'generator_num_filters': [128, 128, 1], 'generator_kernel_sizes': [4, 4, 3], 'generator_strides': [2, 2, 1], 'generator_leaky_relu_alpha': 0.2, 'generator_final_activation': 'tanh', 'generator_l1_regularization_scale': 0.0, 'generator_l2_regularization_scale': 0.0, 'generator_optim[...]<jupyter_text>Prediction<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
!gsutil ls ${OUTPUT_DIR}/export
loaded = tf.saved_model.load(
    export_dir=os.path.join(
        arguments["output_dir"], "export", "20200731081915"
    )
)
print(list(loaded.signatures.keys()))
infer = loaded.signatures["serving_default"]
print(infer.structured_outputs)
Z = tf.random.normal(shape=(10, 512))
predictions = infer(Z)<jupyter_output><empty_output><jupyter_text>Convert image back to the original scale.<jupyter_code>generated_images = np.clip(
    a=tf.cast(
        x=((tf.reshape(
            tensor=predictions["generator_resize_fake_images"],
            shape=[
                -1,
                arguments["height"],
                arguments["width"],
                arguments["depth"]
            ]
        ) + 1.0) * (255. / 2)),
        dtype=tf.int32
    ),
    a_min=0,
    a_max=255
)
print(generated_images.shape)
def plot_images(images):
    """Plots images.

    Args:
        images: np.array, array of images of
            [num_images, image_size, image_size, num_channels].
    """
    num_images = len(images)

    plt.figure(figsize=(20, 20))
    for i in range(num_images):
        image = images[i]
        plt.subplot(1, num_images, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(
            tf.reshape(image, image.shape[:-1]),
            cmap="gray_r"
        )
    plt.show()
plot_images(generated_images)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14558510218574924, 'fraction_numerical': 0.022804387280044188, 'mean_word_length': 6.6257521058965105, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '214818', 'license': 'Apache-2.0', 'n_tokens_mistral': 4681, 'n_tokens_neox': 4464, 'n_words': 541, 'path': 'machine_learning/gan/dcgan/tf2_dcgan/tf2_dcgan_class_ctl_run_module_local.ipynb', 'repo_name': 'ryangillard/artificial_intelligence', 'size': '12673'}