{"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":"import pandas as pd \nimport tensorflow as tf \nimport numpy as np \nimport matplotlib.pyplot as plt \nfrom PIL import Image \nImage.MAX_IMAGE_PIXELS = 3000000000 ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T19:53:27.617932Z","iopub.execute_input":"2022-07-28T19:53:27.618317Z","iopub.status.idle":"2022-07-28T19:53:34.257205Z","shell.execute_reply.started":"2022-07-28T19:53:27.618289Z","shell.execute_reply":"2022-07-28T19:53:34.256009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/mayo-clinic-strip-ai/train.csv')\nother_df = pd.read_csv('../input/mayo-clinic-strip-ai/other.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_images = tf.data.Dataset.list_files('../input/mayo-clinic-strip-ai/other/*.tif')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:02:42.351996Z","iopub.execute_input":"2022-07-28T20:02:42.352461Z","iopub.status.idle":"2022-07-28T20:02:42.481049Z","shell.execute_reply.started":"2022-07-28T20:02:42.352425Z","shell.execute_reply":"2022-07-28T20:02:42.480097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_unlabeled(image_path) : \n    img = Image.open(image_path.numpy()) \n    img = img.resize((128  , 128) ) \n    img = np.array(img)\n    img = img  / 255. \n    return img , img \n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:02:42.682117Z","iopub.execute_input":"2022-07-28T20:02:42.682694Z","iopub.status.idle":"2022-07-28T20:02:42.689099Z","shell.execute_reply.started":"2022-07-28T20:02:42.682665Z","shell.execute_reply":"2022-07-28T20:02:42.688244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Augment(tf.keras.layers.Layer) : \n    def __init__(self , seed = 42) : \n        super().__init__()\n        self.flip_inputs = tf.keras.layers.RandomFlip(mode=\"horizontal_and_vertical\", seed=seed)\n        self.flip_labels = tf.keras.layers.RandomFlip(mode=\"horizontal_and_vertical\", seed=seed)\n        self.rotate_inputs = tf.keras.layers.RandomRotation(.2 , seed=seed)\n        self.rotate_labels = tf.keras.layers.RandomRotation(.2 , seed = seed)\n    def call(self , inputs , labels)  :\n        X = self.flip_inputs(inputs)\n        y = self.flip_labels(labels)\n        X = self.rotate_inputs(X)\n        y = self.rotate_labels(y)\n        return X  , y \n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:02:42.819954Z","iopub.execute_input":"2022-07-28T20:02:42.820565Z","iopub.status.idle":"2022-07-28T20:02:42.834789Z","shell.execute_reply.started":"2022-07-28T20:02:42.820496Z","shell.execute_reply":"2022-07-28T20:02:42.83396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_dataset = unlabeled_images.map(lambda X : tf.py_function(preprocess_unlabeled , [X] , [tf.float32 , tf.float32]))","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:02:46.731157Z","iopub.execute_input":"2022-07-28T20:02:46.731757Z","iopub.status.idle":"2022-07-28T20:02:46.84661Z","shell.execute_reply.started":"2022-07-28T20:02:46.731726Z","shell.execute_reply":"2022-07-28T20:02:46.845489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"396 - 315","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:13:07.681401Z","iopub.execute_input":"2022-07-28T20:13:07.681879Z","iopub.status.idle":"2022-07-28T20:13:07.688333Z","shell.execute_reply.started":"2022-07-28T20:13:07.681845Z","shell.execute_reply":"2022-07-28T20:13:07.68756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_train_ds = unlabeled_dataset.take(315)\nunlabeled_val_ds = unlabeled_dataset.skip(315)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:04:36.838155Z","iopub.execute_input":"2022-07-28T20:04:36.839929Z","iopub.status.idle":"2022-07-28T20:04:36.847215Z","shell.execute_reply.started":"2022-07-28T20:04:36.839881Z","shell.execute_reply":"2022-07-28T20:04:36.846207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_train_ds = unlabeled_train_ds.cache().shuffle(10).batch(4).repeat().map(Augment()).prefetch(buffer_size=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:04:38.66456Z","iopub.execute_input":"2022-07-28T20:04:38.664874Z","iopub.status.idle":"2022-07-28T20:04:39.147285Z","shell.execute_reply.started":"2022-07-28T20:04:38.664848Z","shell.execute_reply":"2022-07-28T20:04:39.146109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_val_ds = unlabeled_val_ds.batch(4)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:04:40.32735Z","iopub.execute_input":"2022-07-28T20:04:40.327729Z","iopub.status.idle":"2022-07-28T20:04:40.332422Z","shell.execute_reply.started":"2022-07-28T20:04:40.327703Z","shell.execute_reply":"2022-07-28T20:04:40.331444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for b_images , b_images_ in unlabeled_dataset.take(1) : \n    print(b_images.shape) \n    print(b_images_.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T19:53:42.480291Z","iopub.execute_input":"2022-07-28T19:53:42.480688Z","iopub.status.idle":"2022-07-28T19:57:58.015775Z","shell.execute_reply.started":"2022-07-28T19:53:42.480662Z","shell.execute_reply":"2022-07-28T19:57:58.01126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AutoEncoder \n\nwe will build autoencoder trying to extract pattern from unlabeled images","metadata":{}},{"cell_type":"code","source":"def encoder(inputs) : \n    conv_1 = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),\n                                  activation=\"relu\", padding=\"same\")(inputs)\n    max_pool_1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv_1)\n    conv_2 = tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3),\n                                  activation=\"relu\", padding=\"same\")(max_pool_1)\n    max_pool_2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(conv_2)\n    \n\n    return max_pool_2\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:07:04.149012Z","iopub.execute_input":"2022-07-28T20:07:04.149391Z","iopub.status.idle":"2022-07-28T20:07:04.157705Z","shell.execute_reply.started":"2022-07-28T20:07:04.149359Z","shell.execute_reply":"2022-07-28T20:07:04.156558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bottle_neck(inputs):\n    bottle_neck = tf.keras.layers.Conv2D(filters=256, kernel_size=(3, 3),\n                                      activation=\"relu\", padding=\"same\")(inputs)\n    return bottle_neck","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:08:54.538474Z","iopub.execute_input":"2022-07-28T20:08:54.538937Z","iopub.status.idle":"2022-07-28T20:08:54.544405Z","shell.execute_reply.started":"2022-07-28T20:08:54.538907Z","shell.execute_reply":"2022-07-28T20:08:54.543457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def decoder(inputs):\n  \n    conv_1 = tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3),\n                                      activation=\"relu\", padding=\"same\")(inputs)\n    up_sample_1 = tf.keras.layers.UpSampling2D(size=(2, 2))(conv_1)\n    conv_2 = tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3),\n                                      activation=\"relu\", padding=\"same\")(up_sample_1)\n    up_sample_1 = tf.keras.layers.UpSampling2D(size=(2, 2))(conv_2)\n    conv_3 = tf.keras.layers.Conv2D(filters=3, kernel_size=(3, 3),\n                                      activation=\"sigmoid\", padding=\"same\")(up_sample_1)\n    return conv_3                       ","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:08:55.959604Z","iopub.execute_input":"2022-07-28T20:08:55.960006Z","iopub.status.idle":"2022-07-28T20:08:55.969783Z","shell.execute_reply.started":"2022-07-28T20:08:55.959976Z","shell.execute_reply":"2022-07-28T20:08:55.968246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convolutional_auto_encoder():\n    inputs = tf.keras.layers.Input(shape=(128, 128, 3))\n    encoder_output = encoder(inputs)\n    bottleneck_output = bottle_neck(encoder_output)\n    decoder_output = decoder(bottleneck_output)\n    model = tf.keras.Model(inputs=inputs, outputs=decoder_output)\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:08:57.009474Z","iopub.execute_input":"2022-07-28T20:08:57.009861Z","iopub.status.idle":"2022-07-28T20:08:57.016987Z","shell.execute_reply.started":"2022-07-28T20:08:57.009831Z","shell.execute_reply":"2022-07-28T20:08:57.015825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auto_encoder = convolutional_auto_encoder()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:10:50.06149Z","iopub.execute_input":"2022-07-28T20:10:50.062267Z","iopub.status.idle":"2022-07-28T20:10:50.153017Z","shell.execute_reply.started":"2022-07-28T20:10:50.062237Z","shell.execute_reply":"2022-07-28T20:10:50.151856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auto_encoder.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:09:08.564686Z","iopub.execute_input":"2022-07-28T20:09:08.565778Z","iopub.status.idle":"2022-07-28T20:09:08.574699Z","shell.execute_reply.started":"2022-07-28T20:09:08.565736Z","shell.execute_reply":"2022-07-28T20:09:08.573525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(auto_encoder, show_shapes=True, rankdir='TB')","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:09:34.591078Z","iopub.execute_input":"2022-07-28T20:09:34.591864Z","iopub.status.idle":"2022-07-28T20:09:36.238525Z","shell.execute_reply.started":"2022-07-28T20:09:34.591806Z","shell.execute_reply":"2022-07-28T20:09:36.236981Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"auto_encoder.compile(optimizer=tf.keras.optimizers.Adam(), loss='binary_crossentropy')\n","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:11:06.835889Z","iopub.execute_input":"2022-07-28T20:11:06.836271Z","iopub.status.idle":"2022-07-28T20:11:06.848842Z","shell.execute_reply.started":"2022-07-28T20:11:06.836241Z","shell.execute_reply":"2022-07-28T20:11:06.847742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_steps =  315  // 4\nvalid_steps = 81 // 4\nauto_encoder_hist  = auto_encoder.fit(unlabeled_train_ds , validation_data = unlabeled_val_ds , steps_per_epoch=train_steps, validation_steps=valid_steps,epochs = 10)","metadata":{"execution":{"iopub.status.busy":"2022-07-28T20:14:08.80813Z","iopub.execute_input":"2022-07-28T20:14:08.808525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}