{"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":"markdown","source":"","metadata":{"id":"XJ3r-2jMG94V"}},{"cell_type":"code","source":"!pip install tensorflow_addons\n","metadata":{"id":"GQ18Kd5F3uKe","outputId":"2231906a-e0e2-48c4-a2b2-081c37dec023","execution":{"iopub.status.busy":"2021-08-10T04:58:12.033312Z","iopub.execute_input":"2021-08-10T04:58:12.033875Z","iopub.status.idle":"2021-08-10T04:58:20.458882Z","shell.execute_reply.started":"2021-08-10T04:58:12.033785Z","shell.execute_reply":"2021-08-10T04:58:20.457856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:58:23.485598Z","iopub.execute_input":"2021-08-10T04:58:23.485962Z","iopub.status.idle":"2021-08-10T04:58:31.030755Z","shell.execute_reply.started":"2021-08-10T04:58:23.485926Z","shell.execute_reply":"2021-08-10T04:58:31.029874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom tensorflow.python.distribute.cluster_resolver.tpu.tpu_cluster_resolver import is_running_in_gce  # pylint: disable=unused-import\nfrom tensorflow.python.distribute.cluster_resolver.tpu.tpu_cluster_resolver import TPUClusterResolver\nfrom tensorflow.python.util.tf_export import tf_export\n\n#tf_export('distribute.cluster_resolver.TPUClusterResolver')(TPUClusterResolver)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:58:31.032171Z","iopub.execute_input":"2021-08-10T04:58:31.032543Z","iopub.status.idle":"2021-08-10T04:58:36.779004Z","shell.execute_reply.started":"2021-08-10T04:58:31.032510Z","shell.execute_reply":"2021-08-10T04:58:36.778255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math, re, os\nimport tensorflow as tf\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\n\nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:58:36.780636Z","iopub.execute_input":"2021-08-10T04:58:36.781193Z","iopub.status.idle":"2021-08-10T04:58:37.433242Z","shell.execute_reply.started":"2021-08-10T04:58:36.781158Z","shell.execute_reply":"2021-08-10T04:58:37.432553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:58:43.889998Z","iopub.execute_input":"2021-08-10T04:58:43.890658Z","iopub.status.idle":"2021-08-10T04:58:49.299701Z","shell.execute_reply.started":"2021-08-10T04:58:43.890610Z","shell.execute_reply":"2021-08-10T04:58:49.298743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Configurations","metadata":{"execution":{"iopub.status.busy":"2021-08-09T16:06:14.531105Z","iopub.execute_input":"2021-08-09T16:06:14.531367Z","iopub.status.idle":"2021-08-09T16:06:14.534874Z","shell.execute_reply.started":"2021-08-09T16:06:14.531342Z","shell.execute_reply":"2021-08-09T16:06:14.53411Z"},"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()\nprint('Number of replicas:', strategy.num_replicas_in_sync)\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n   \nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:58:51.693759Z","iopub.execute_input":"2021-08-10T04:58:51.694172Z","iopub.status.idle":"2021-08-10T04:58:58.679276Z","shell.execute_reply.started":"2021-08-10T04:58:51.694142Z","shell.execute_reply":"2021-08-10T04:58:58.678302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa\ncust = {'InstanceNormalization': tfa.layers.InstanceNormalization}\nfrom tensorflow.python.keras.models import load_model\nmodel_AtoB = load_model('../input/recheck-weights/g_model_AtoB_022750.h5', cust)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:59:21.132314Z","iopub.execute_input":"2021-08-10T04:59:21.132685Z","iopub.status.idle":"2021-08-10T04:59:26.302499Z","shell.execute_reply.started":"2021-08-10T04:59:21.132649Z","shell.execute_reply":"2021-08-10T04:59:26.301084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    \n    model_AtoB = load_model('../input/recheck-weights/g_model_AtoB_022750.h5', cust)\n    model_AtoB.compile()\n#     model.compile(\n#         optimizer=tf.keras.optimizers.Adam(),\n#         loss = 'sparse_categorical_crossentropy',\n#         metrics=['sparse_categorical_accuracy']\n#     )\n    model_AtoB.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-10T04:59:37.021196Z","iopub.execute_input":"2021-08-10T04:59:37.021548Z","iopub.status.idle":"2021-08-10T04:59:52.275122Z","shell.execute_reply.started":"2021-08-10T04:59:37.021519Z","shell.execute_reply":"2021-08-10T04:59:52.274076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example of using saved cyclegan models for image translation\nfrom numpy import load\nfrom numpy import expand_dims\nfrom tensorflow.python.keras.models import load_model\nimport tensorflow_addons as tfa\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom matplotlib import pyplot\nimport matplotlib\n\n# load an image to the preferred size\ndef load_image(filename, size=(256,256)):\n    # load and resize the image\n    pixels = load_img(filename, target_size=size)\n    # convert to numpy array\n    pixels = img_to_array(pixels)\n    # transform in a sample\n    pixels = expand_dims(pixels, 0)\n    # scale from [0,255] to [-1,1]\n    pixels = (pixels - 127.5) / 127.5\n    return pixels\n    \n# GCS_PATH = KaggleDatasets().get_gcs_path(\"gan-getting-started\") \nphoto_ds = tf.io.gfile.glob('../input/gan-getting-started/photo_jpg/*.jpg') \nprint('Photo jpg Files:', len(photo_ds)) \n\n\ni = 1\nfor img in photo_ds :\n    # load the image\n    print (img)\n    image_src = load_image(img)\n    # load the model\n    cust = {'InstanceNormalization': tfa.layers.InstanceNormalization}\n    model_AtoB = load_model('../input/latest/g_model_AtoB_035000.h5', cust)\n    # translate image\n    image_tar = model_AtoB.predict(image_src)\n    # scale from [-1,1] to [0,1]\n    image_tar = (image_tar + 1) / 2.0\n    # plot the translated image\n    # plot the translated image\n#     pyplot.imshow(image_tar[0])\n#     pyplot.show()\n    matplotlib.image.imsave(\"../images/\" + str(i) + \".jpg\", image_tar[0])\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2021-08-09T20:43:21.708607Z","iopub.execute_input":"2021-08-09T20:43:21.708990Z","iopub.status.idle":"2021-08-09T20:43:30.073318Z","shell.execute_reply.started":"2021-08-09T20:43:21.708960Z","shell.execute_reply":"2021-08-09T20:43:30.070286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    image_src = load_image(img)\n    # load the model\n    cust = {'InstanceNormalization': tfa.layers.InstanceNormalization}\n    model_AtoB = load_model('../input/recheck-weights/g_model_AtoB_022750.h5', cust)\n    # translate image\n    image_tar = model_AtoB.predict(image_src)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:00:14.648979Z","iopub.execute_input":"2021-08-10T05:00:14.649376Z","iopub.status.idle":"2021-08-10T05:00:14.680117Z","shell.execute_reply.started":"2021-08-10T05:00:14.649345Z","shell.execute_reply":"2021-08-10T05:00:14.678576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math, re, os\nimport tensorflow as tf\nimport numpy as np\nfrom matplotlib import pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\nimport efficientnet.tfkeras as efn\nfrom sklearn.metrics import f1_score, precision_score, recall_score, confusion_matrix\n\nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:00:24.152673Z","iopub.execute_input":"2021-08-10T05:00:24.153091Z","iopub.status.idle":"2021-08-10T05:00:24.450675Z","shell.execute_reply.started":"2021-08-10T05:00:24.153053Z","shell.execute_reply":"2021-08-10T05:00:24.449636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# example of using saved cyclegan models for image translation\nfrom numpy import load\nfrom numpy import expand_dims\nfrom tensorflow.python.keras.models import load_model\nimport tensorflow_addons as tfa\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom matplotlib import pyplot\nimport matplotlib\n\n\n# load an image to the preferred size\ndef load_image(filename, size=(256,256)):\n    # load and resize the image\n    pixels = load_img(filename, target_size=size)\n    # convert to numpy array\n    pixels = img_to_array(pixels)\n    # transform in a sample\n    pixels = expand_dims(pixels, 0)\n    # scale from [0,255] to [-1,1]\n    pixels = (pixels - 127.5) / 127.5\n    return pixels\n    \n# GCS_PATH = KaggleDatasets().get_gcs_path(\"gan-getting-started\") \nphoto_ds = tf.io.gfile.glob('../input/gan-getting-started/photo_jpg/*.jpg') \nprint('Photo jpg Files:', len(photo_ds)) \n\ncust = {'InstanceNormalization': tfa.layers.InstanceNormalization}\nmodel_AtoB = load_model('../input/recheck-weights/g_model_AtoB_022750.h5', cust)\n\ni = 1\nfor img in photo_ds :\n    # load the image\n    print (img)\n    image_src = load_image(img)\n    # load the model\n\n    # translate image\n    image_tar = model_AtoB.predict(image_src)\n    # scale from [-1,1] to [0,1]\n    image_tar = (image_tar + 1) / 2.0\n    # plot the translated image\n    # plot the translated image\n#     pyplot.imshow(image_tar[0])\n#     pyplot.show()\n    matplotlib.image.imsave(\"../images/\" + str(i) + \".jpg\", image_tar[0])\n    i += 1\n#     im = PIL.Image.fromarray(image_tar)\n#     im.save(\"../images/\" + str(i) + \".jpg\")\n\n\n \n# # monet_ds = get_dataset(MONET_FILENAMES, augment=data_augment, take = 30).batch(1) \n# photo_ds = load_dataset(PHOTO_FILENAMES, labeled=True).batch(1)","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:00:55.677104Z","iopub.execute_input":"2021-08-10T05:00:55.677673Z","iopub.status.idle":"2021-08-10T05:28:26.074945Z","shell.execute_reply.started":"2021-08-10T05:00:55.677628Z","shell.execute_reply":"2021-08-10T05:28:26.073999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n! mkdir ../images","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:00:44.134056Z","iopub.execute_input":"2021-08-10T05:00:44.134465Z","iopub.status.idle":"2021-08-10T05:00:44.895338Z","shell.execute_reply.started":"2021-08-10T05:00:44.134421Z","shell.execute_reply":"2021-08-10T05:00:44.894170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:00:47.298413Z","iopub.execute_input":"2021-08-10T05:00:47.298847Z","iopub.status.idle":"2021-08-10T05:00:47.302870Z","shell.execute_reply.started":"2021-08-10T05:00:47.298799Z","shell.execute_reply":"2021-08-10T05:00:47.302132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")","metadata":{"execution":{"iopub.status.busy":"2021-08-10T05:28:48.198595Z","iopub.execute_input":"2021-08-10T05:28:48.198976Z","iopub.status.idle":"2021-08-10T05:28:51.565810Z","shell.execute_reply.started":"2021-08-10T05:28:48.198947Z","shell.execute_reply":"2021-08-10T05:28:51.564869Z"},"trusted":true},"execution_count":null,"outputs":[]}]}