{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.preprocessing.image import ImageDataGenerator\nimport re\nfrom PIL import Image\nimport torch\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nmonet_tfrec = '-60.tfrec'\nfor dirname, xx, filenames in os.walk('/kaggle/input'):\n   for filename in filenames:\n        fName = os.path.join(dirname, filename)\n        if monet_tfrec in fName: print(fName) \n        \n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-12T14:31:00.683881Z","iopub.execute_input":"2021-06-12T14:31:00.68428Z","iopub.status.idle":"2021-06-12T14:31:09.466687Z","shell.execute_reply.started":"2021-06-12T14:31:00.684247Z","shell.execute_reply":"2021-06-12T14:31:09.465343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\n    #A state & compute distribution policy on a list of devices.\n\nprint('Number of replicas:', strategy.num_replicas_in_sync)\nAUTO = tf.data.experimental.AUTOTUNE\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.468151Z","iopub.execute_input":"2021-06-12T14:31:09.46847Z","iopub.status.idle":"2021-06-12T14:31:09.483069Z","shell.execute_reply.started":"2021-06-12T14:31:09.46844Z","shell.execute_reply":"2021-06-12T14:31:09.481924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MONET_FILENAMES = tf.io.gfile.glob(str('/kaggle/input/gan-getting-started/monet_tfrec/*.tfrec'))\nPHOTO_FILENAMES = tf.io.gfile.glob(str('/kaggle/input/gan-getting-started/photo_tfrec/*.tfrec'))\nprint(\"Total MONET files\", len(MONET_FILENAMES), \"\\nTotal PHOTO files\", len(PHOTO_FILENAMES), \"\\n\")\nMONET_FILENAMES","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.486432Z","iopub.execute_input":"2021-06-12T14:31:09.486728Z","iopub.status.idle":"2021-06-12T14:31:09.500772Z","shell.execute_reply.started":"2021-06-12T14:31:09.486698Z","shell.execute_reply":"2021-06-12T14:31:09.499606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)\n\nn_monet_samples = count_data_items(MONET_FILENAMES)\nn_photo_samples = count_data_items(PHOTO_FILENAMES)\n\nBATCH_SIZE =  4\nEPOCHS_NUM = 30\n\nprint(f'Monet TFRecord files: {len(MONET_FILENAMES)}')\nprint(f'Monet image files: {n_monet_samples}')\nprint(f'Photo TFRecord files: {len(PHOTO_FILENAMES)}')\nprint(f'Photo image files: {n_photo_samples}')\nprint(f\"Batch_size: {BATCH_SIZE}\")\nprint(f\"Epochs number: {EPOCHS_NUM}\")","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.503018Z","iopub.execute_input":"2021-06-12T14:31:09.503503Z","iopub.status.idle":"2021-06-12T14:31:09.514231Z","shell.execute_reply.started":"2021-06-12T14:31:09.503446Z","shell.execute_reply":"2021-06-12T14:31:09.512984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_augment(image):\n    print(\"data_augment\\n\")\n    p_rotate = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_spatial = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    p_crop = tf.random.uniform([], 0, 1.0, dtype=tf.float32)\n    if p_crop > .5:\n        image = tf.image.resize(image, [286, 286]) #resizing to 286 x 286 x 3\n        image = tf.image.random_crop(image, size=[256, 256, 3]) # randomly cropping to 256 x 256 x 3\n        if p_crop > .9:\n            image = tf.image.resize(image, [300, 300])\n            image = tf.image.random_crop(image, size=[256, 256, 3])\n    \n    if p_rotate > .9:\n        image = tf.image.rot90(image, k=3) # rotate 270º\n    elif p_rotate > .7:\n        image = tf.image.rot90(image, k=2) # rotate 180º\n    elif p_rotate > .5:\n        image = tf.image.rot90(image, k=1) # rotate 90º\n        \n    ## random mirroring\n    if p_spatial > .6:\n        image = tf.image.random_flip_left_right(image)\n        image = tf.image.random_flip_up_down(image)\n        if p_spatial > .9:\n            image = tf.image.transpose(image)\n    \n    return image\n\ndef load_dataset(filenames):\n    print(\"load_dataset\\n\")\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\n    return dataset\n\ndef get_gan_dataset(monet_files, photo_files, augment=None, repeat=True, shuffle=True, batch_size=1):\n    print(\"get_gan_dataset\\n\")\n    monet_ds = load_dataset(monet_files)\n    photo_ds = load_dataset(photo_files)\n    \n    if augment:\n        monet_ds = monet_ds.map(augment, num_parallel_calls=AUTO)\n        photo_ds = photo_ds.map(augment, num_parallel_calls=AUTO)\n    \n    if repeat:\n        monet_ds = monet_ds.repeat()\n        photo_ds = photo_ds.repeat()\n    \n    if shuffle:\n        monet_ds = monet_ds.shuffle(2048)\n        photo_ds = photo_ds.shuffle(2048)\n        \n    monet_ds = monet_ds.batch(batch_size, drop_remainder=True)\n    photo_ds = photo_ds.batch(batch_size, drop_remainder=True)\n    monet_ds = monet_ds.cache()\n    photo_ds = photo_ds.cache()\n    monet_ds = monet_ds.prefetch(AUTO)\n    photo_ds = photo_ds.prefetch(AUTO)\n    \n    gan_ds = tf.data.Dataset.zip((monet_ds, photo_ds))\n    \n    return gan_ds","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.516094Z","iopub.execute_input":"2021-06-12T14:31:09.516586Z","iopub.status.idle":"2021-06-12T14:31:09.534767Z","shell.execute_reply.started":"2021-06-12T14:31:09.516519Z","shell.execute_reply":"2021-06-12T14:31:09.533627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = [256, 256]\ndef decode_image(image):\n    print(\"decode_image\\n\")\n    #Decode the JPEG-encoded images to uint8 tensors.\n    #channels = 3 for the output an RGB image.\n    #The attr channels indicates the desired number of color channels for the decoded image.\n    image = tf.image.decode_jpeg(image, channels=3)\n    #Cast the tensors to new type.\n    image = (tf.cast(image, tf.float32) / 127.5) - 1\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\n    return image\n\ndef read_tfrecord(example):\n    print(\"read_tfrecord\\n\")\n    tfrecord_format = {\n        \"image_name\": tf.io.FixedLenFeature([], tf.string),\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"target\": tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.536339Z","iopub.execute_input":"2021-06-12T14:31:09.536764Z","iopub.status.idle":"2021-06-12T14:31:09.551337Z","shell.execute_reply.started":"2021-06-12T14:31:09.53672Z","shell.execute_reply":"2021-06-12T14:31:09.550279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"get_gan_dataset calls the following functions and prepares the image data:\\n\")\nfull_dataset = get_gan_dataset(MONET_FILENAMES, PHOTO_FILENAMES, augment=data_augment, repeat=True, shuffle=True, batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:09.552267Z","iopub.execute_input":"2021-06-12T14:31:09.552544Z","iopub.status.idle":"2021-06-12T14:31:10.567265Z","shell.execute_reply.started":"2021-06-12T14:31:09.552518Z","shell.execute_reply":"2021-06-12T14:31:10.566439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_dataset?","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:29:00.700363Z","iopub.execute_input":"2021-06-12T14:29:00.700668Z","iopub.status.idle":"2021-06-12T14:29:00.706394Z","shell.execute_reply.started":"2021-06-12T14:29:00.700639Z","shell.execute_reply":"2021-06-12T14:29:00.705633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_dataset.element_spec","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:11:14.963712Z","iopub.execute_input":"2021-06-12T14:11:14.963983Z","iopub.status.idle":"2021-06-12T14:11:14.975336Z","shell.execute_reply.started":"2021-06-12T14:11:14.963957Z","shell.execute_reply":"2021-06-12T14:11:14.974293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images, labels in full_dataset.take(1):\n  print('images.shape: ', images.shape)\n  print('labels.shape: ', labels.shape)\n\n    ","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:11:14.976767Z","iopub.execute_input":"2021-06-12T14:11:14.977114Z","iopub.status.idle":"2021-06-12T14:11:23.866137Z","shell.execute_reply.started":"2021-06-12T14:11:14.977084Z","shell.execute_reply":"2021-06-12T14:11:23.865067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 0\nfor fn in MONET_FILENAMES:#PHOTO_FILENAMES,MONET_FILENAMES:\n  for record in tf.compat.v1.python_io.tf_record_iterator(fn):\n    c += 1\n  print(c, 'files in', fn)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:24:28.36274Z","iopub.execute_input":"2021-06-12T10:24:28.363447Z","iopub.status.idle":"2021-06-12T10:24:28.383979Z","shell.execute_reply.started":"2021-06-12T10:24:28.363396Z","shell.execute_reply":"2021-06-12T10:24:28.382525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_monet ,example_photo = next(iter(full_dataset))\n# print(example_monet.numpy())\n# iter() contains an iterator\n# The next() gets the first iteration. Running next() again will get the second item of the iterator, and so-on.","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:31:10.910309Z","iopub.execute_input":"2021-06-12T14:31:10.910865Z","iopub.status.idle":"2021-06-12T14:31:18.043409Z","shell.execute_reply.started":"2021-06-12T14:31:10.910816Z","shell.execute_reply":"2021-06-12T14:31:18.042185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"example_monet: \", example_monet.shape, len(example_monet), len(example_monet[0]), len(example_monet[1]), len(example_monet[2]), len(example_monet[3]))\nprint(\"example_photo: \", example_photo.shape, len(example_photo), len(example_photo[0]), len(example_photo[1]), len(example_photo[2]), len(example_photo[3]))","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:11:28.974072Z","iopub.execute_input":"2021-06-12T14:11:28.974408Z","iopub.status.idle":"2021-06-12T14:11:28.990952Z","shell.execute_reply.started":"2021-06-12T14:11:28.97437Z","shell.execute_reply":"2021-06-12T14:11:28.989824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_monet?","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:11:28.9927Z","iopub.execute_input":"2021-06-12T14:11:28.993446Z","iopub.status.idle":"2021-06-12T14:11:29.170773Z","shell.execute_reply.started":"2021-06-12T14:11:28.9934Z","shell.execute_reply":"2021-06-12T14:11:29.169579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_photo?","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:11:54.859246Z","iopub.execute_input":"2021-06-12T14:11:54.859628Z","iopub.status.idle":"2021-06-12T14:11:54.876881Z","shell.execute_reply.started":"2021-06-12T14:11:54.859594Z","shell.execute_reply":"2021-06-12T14:11:54.876064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nn = len(example_monet)\nfor ii in range(nn): \n    plt.subplot(1,nn,ii+1)\n    plt.imshow(example_monet[ii])#plt.imshow(example_monet[ii] * 0.5 + 0.5)\nexample_monet.shape[2]","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:24:33.268797Z","iopub.execute_input":"2021-06-12T10:24:33.269522Z","iopub.status.idle":"2021-06-12T10:24:33.679387Z","shell.execute_reply.started":"2021-06-12T10:24:33.269476Z","shell.execute_reply":"2021-06-12T10:24:33.678392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for jj in range(1000):\n    example_monet ,example_photo = next(iter(full_dataset))\n    nn = len(example_photo)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:26:19.616738Z","iopub.execute_input":"2021-06-12T10:26:19.617187Z","iopub.status.idle":"2021-06-12T10:27:45.442787Z","shell.execute_reply.started":"2021-06-12T10:26:19.617147Z","shell.execute_reply":"2021-06-12T10:27:45.441656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xx = np.array(example_monet[2])#numpy.ndarray\nprint(xx.shape, type(xx))\nplt.imshow(xx)\nplt.savefig('test.png')\n","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:52:09.872136Z","iopub.execute_input":"2021-06-12T14:52:09.872539Z","iopub.status.idle":"2021-06-12T14:52:10.125789Z","shell.execute_reply.started":"2021-06-12T14:52:09.872493Z","shell.execute_reply":"2021-06-12T14:52:10.124643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zz = torch.from_numpy(xx)#torch.Tensor\n# yy = Image.fromarray((xx * 255).astype(np.uint8))#PIL.Image.Image\n# plt.imshow(zz)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-12T14:52:35.414009Z","iopub.execute_input":"2021-06-12T14:52:35.414387Z","iopub.status.idle":"2021-06-12T14:52:35.419495Z","shell.execute_reply.started":"2021-06-12T14:52:35.414356Z","shell.execute_reply":"2021-06-12T14:52:35.417903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfrecord_filenames_queue = [MONET_FILENAMES]\n# tfrecord_filenames_queue : tfrecord filename queue\n#    String queue object from tf.train.string_input_producer()\n#reader = tf.TFRecordReader()\n#_, serialized_example = reader.read(tfrecord_filenames_queue)\nreader = tf.data.TFRecordDataset(tfrecord_filenames_queue)\n\ncount = 0\nfor serialized_example in reader.take(1):# read the first record.\n    count = count+1\n\n# Create a description of the features.\n# features={\n#         'height': tf.io.FixedLenFeature([], tf.int64),\n#         'width': tf.io.FixedLenFeature([], tf.int64),\n#         'image_raw': tf.io.FixedLenFeature([], tf.string),\n#         'mask_raw': tf.io.FixedLenFeature([], tf.string)\n#         }\n\nfeatures={\n        'image_name': tf.io.FixedLenFeature([], tf.string),\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'target': tf.io.FixedLenFeature([], tf.string)\n        }\n            \nfeatures = tf.io.parse_single_example(serialized_example, features)\n# image = tf.io.decode_raw(features['image_raw'], tf.uint8)\n# annotation = tf.io.decode_raw(features['mask_raw'], tf.uint8)\n    \n# height = tf.cast(features['height'], tf.int32)\n# width = tf.cast(features['width'], tf.int32)\n    \n# image_shape = tf.pack([height, width, 3])#module 'tensorflow' has no attribute 'pack'\n# annotation_shape = tf.pack([height, width, 1])#module 'tensorflow' has no attribute 'pack'\n    \n# image = tf.reshape(image, image_shape)\n# notation = tf.reshape(annotation, annotation_shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:24:43.563176Z","iopub.execute_input":"2021-06-12T10:24:43.56349Z","iopub.status.idle":"2021-06-12T10:24:43.588853Z","shell.execute_reply.started":"2021-06-12T10:24:43.563459Z","shell.execute_reply":"2021-06-12T10:24:43.587805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This will do preprocessing and realtime data augmentation:\ndatagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # apply ZCA whitening\n        rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)\n        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)\n        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)\n        horizontal_flip=True,  # randomly flip images\n        vertical_flip=False)  # randomly flip images","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:24:43.590069Z","iopub.execute_input":"2021-06-12T10:24:43.590358Z","iopub.status.idle":"2021-06-12T10:24:43.596514Z","shell.execute_reply.started":"2021-06-12T10:24:43.590322Z","shell.execute_reply":"2021-06-12T10:24:43.595514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen.flow(image, annotation, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2021-06-12T10:24:43.597675Z","iopub.execute_input":"2021-06-12T10:24:43.598049Z","iopub.status.idle":"2021-06-12T10:24:43.64246Z","shell.execute_reply.started":"2021-06-12T10:24:43.598016Z","shell.execute_reply":"2021-06-12T10:24:43.641033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}