Data-efficient GANs with Adaptive Discriminator Augmentation

import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras
from tensorflow.keras import layers

data

num_epochs = 400 # train for 400 epochs for good results

image_size = 64

resolution of Kernel Inception Distance measurement, see related section

kid_image_size = 75

padding = 0.25

dataset_name = “caltech_birds2011”

adaptive discriminator augmentation

max_translation = 0.125

max_rotation = 0.125

max_zoom = 0.25

target_accuracy = 0.85

integration_steps = 1000

architecture

noise_size = 64

depth = 4

width = 128

leaky_relu_slope = 0.2

dropout_rate = 0.4

optimization

batch_size = 128

learning_rate = 2e-4

beta_1 = 0.5 # not using the default value of 0.9 is important

ema = 0.99

def round_to_int(float_value):

return tf.cast(tf.math.round(float_value), dtype=tf.int32)

def preprocess_image(data):

# unnormalize bounding box coordinates

height = tf.cast(tf.shape(data["image"])[0], dtype=tf.float32)

width = tf.cast(tf.shape(data["image"])[1], dtype=tf.float32)

bounding_box = data["bbox"] * tf.stack([height, width, height, width])

# calculate center and length of longer side, add padding

target_center_y = 0.5 * (bounding_box[0] + bounding_box[2])

target_center_x = 0.5 * (bounding_box[1] + bounding_box[3])

target_size = tf.maximum(

    (1.0 + padding) * (bounding_box[2] - bounding_box[0]),

    (1.0 + padding) * (bounding_box[3] - bounding_box[1]),

)

# modify crop size to fit into image

target_height = tf.reduce_min(

    [target_size, 2.0 * target_center_y, 2.0 * (height - target_center_y)]

)

target_width = tf.reduce_min(

    [target_size, 2.0 * target_center_x, 2.0 * (width - target_center_x)]

)

# crop image

image = tf.image.crop_to_bounding_box(

    data["image"],

    offset_height=round_to_int(target_center_y - 0.5 * target_height),

    offset_width=round_to_int(target_center_x - 0.5 * target_width),

    target_height=round_to_int(target_height),

    target_width=round_to_int(target_width),

)

# resize and clip

# for image downsampling, area interpolation is the preferred method

image = tf.image.resize(

    image, size=[image_size, image_size], method=tf.image.ResizeMethod.AREA

)

return tf.clip_by_value(image / 255.0, 0.0, 1.0)

def prepare_dataset(split):

# the validation dataset is shuffled as well, because data order matters

# for the KID calculation

return (

    tfds.load(dataset_name, split=split, shuffle_files=True)

    .map(preprocess_image, num_parallel_calls=tf.data.AUTOTUNE)

    .cache()

    .shuffle(10 * batch_size)

    .batch(batch_size, drop_remainder=True)

    .prefetch(buffer_size=tf.data.AUTOTUNE)

)

train_dataset = prepare_dataset(“train”)

val_dataset = prepare_dataset(“test”)

Hi, I am using this code. I have an issue in loading my own dataset but in code, the TensorFlow dataset is used to tell me how to replace TensorFlow data with to own dataset. I search a lot on the internet but can’t any good soultion.