text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from kaggle_datasets import KaggleDatasets

import matplotlib.pyplot as plt

import numpy as np

import re

import tensorflow as tf

HEIGHT = 256
WIDTH = 256
BATCH_SIZE = 1
CHANNELS = 3
LAMBDA = 10
EPOCHS = 250
GCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')

try:
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
except ValueError:
    tpu = None
if tpu:
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
else:
    strategy = tf.distribute.get_strategy()
REPLICAS = strategy.num_replicas_in_sync
AUTOTUNE = tf.data.AUTOTUNE

MONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))
PHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))
def count_data_items(filenames):
    n = [int(re.compile('-([0-9]*)\\.').search(filename).group(1)) for filename in filenames]
    return np.sum(n)
n_monet_samples = count_data_items(MONET_FILENAMES)
n_photo_samples = count_data_items(PHOTO_FILENAMES)

def decode_image(image):
    image = tf.image.decode_jpeg(image, channels=CHANNELS)
    image = tf.cast(image, tf.float32) / 127.5 - 1
    image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])
    return image

def read_tfrecord(example):
    tfrecord_format = {'image_name': tf.io.FixedLenFeature([], tf.string), 'image': tf.io.FixedLenFeature([], tf.string), 'target': tf.io.FixedLenFeature([], tf.string)}
    example = tf.io.parse_single_example(example, tfrecord_format)
    image = decode_image(example['image'])
    return image

def load_dataset(filenames, labeled=True, ordered=False):
    dataset = tf.data.TFRecordDataset(filenames)
    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)
    return dataset

def display_samples(dataset, row, col):
    dataset_iter = iter(dataset)
    for j in range(row * col):
        example_sample = next(dataset_iter)
        plt.axis('off')

display_samples(load_dataset(MONET_FILENAMES).shuffle(30).batch(1), 4, 4)