{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Imports"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nimport tensorflow.data as td\nimport matplotlib.pyplot as plt\nfrom imblearn.over_sampling import RandomOverSampler","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CSV Exploration\nLet's load up the csv and see what we got"},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')\ndf.sample(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.id_code.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.plot.hist(by='diagnosis')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Simple class labels. Nothing is missing, and there is a slight class imbalance. Now, let's check out the images"},{"metadata":{},"cell_type":"markdown","source":"# Image Exploration\nWe'll define a dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"oversampler = RandomOverSampler()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x,y = oversampler.fit_resample(df.id_code.values.reshape(-1,1),df.diagnosis.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df=pd.DataFrame({\"id_code\":x.flatten(),\"diagnosis\":y})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.plot.hist(by='diagnosis')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imagePaths = df.apply(lambda x: '/kaggle/input/aptos2019-blindness-detection/train_images/'+str(x[0])+'.png',axis=1).values\nclasses = df.iloc[:,1].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imagePaths[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes[:10]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load(path):\n    image = tf.image.decode_png(tf.io.read_file(path),channels=3)\n    return image\nwith tf.Session() as sess:\n    image,label = sess.run(load(imagePaths[0])),classes[0]\nplt.imshow(image)\nplt.title('Diagnosis: '+str(label))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pathDS = td.Dataset.from_tensor_slices(imagePaths)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labelDS = td.Dataset.from_tensor_slices(classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def oneHotter(label):\n    return tf.one_hot(label,5)\n\noneHotLabelDS = labelDS.map(oneHotter,num_parallel_calls=AUTOTUNE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"imageDSIterator = pathDS.map(load,num_parallel_calls=AUTOTUNE).make_one_shot_iterator()\nelem = imageDSIterator.get_next()\nwith tf.Session() as sess:\n    plt.figure(figsize=(40,30))\n    for idx in range(12):\n        image=sess.run(elem)\n        plt.subplot(3,4,idx+1)\n        plt.imshow(image)\n        plt.grid(False)\n        plt.xticks([])\n        plt.yticks([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def transform_perspective(image):\n    def x_y_1():\n        x = tf.random_uniform([], minval=-0.3, maxval=-0.15)\n        y = tf.random_uniform([], minval=-0.3, maxval=-0.15)\n        return x, y\n     \n    def x_y_2():\n        x = tf.random_uniform([], minval=0.15, maxval=0.3)\n        y = tf.random_uniform([], minval=0.15, maxval=0.3)\n        return x, y       \n\n    def trans(image):\n        ran = tf.random_uniform([])\n        x = tf.random_uniform([], minval=-0.3, maxval=0.3)\n        x_com = tf.random_uniform([], minval=1-x-0.1, maxval=1-x+0.1)\n\n        y = tf.random_uniform([], minval=-0.3, maxval=0.3)\n        y_com = tf.random_uniform([], minval=1-y-0.1, maxval=1-y+0.1)\n\n        transforms =  [x_com, x,0,y,y_com,0,0.00,0]\n\n        ran = tf.random_uniform([]) \n        image = tf.cond(ran<0.5, lambda:tf.contrib.image.transform(image,transforms,interpolation='NEAREST', name=None), \n                lambda:tf.contrib.image.transform(image,transforms,interpolation='BILINEAR', name=None))\n        return image\n\n    ran = tf.random_uniform([])\n    image = tf.cond(ran<1, lambda: trans(image), lambda:image)\n\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def loadAndPreProcess(path):\n    image = tf.image.decode_png(tf.io.read_file(path),channels=3)\n    image = tf.image.resize(image,[299,299])\n    image = tf.image.random_brightness(image,0.5)\n    image = tf.image.random_hue(image,0.05)\n    image = tf.image.random_contrast(image,0.75,1.25)\n    image = tf.image.random_saturation(image,0.75,1.25)\n    image = tf.image.random_flip_left_right(image)\n    image = tf.contrib.image.rotate(image,tf.random_uniform(shape=[], minval=-15, maxval=15, dtype=tf.float32))\n    image = transform_perspective(image)\n    image /= 255.\n    image-=0.5\n    image*=2.\n    return image\n    \naugImageDS = pathDS.map(loadAndPreProcess,num_parallel_calls=AUTOTUNE)\naugImageDSIterator = augImageDS.make_one_shot_iterator()\nelem = augImageDSIterator.get_next()\nwith tf.Session() as sess:\n    plt.figure(figsize=(40,30))\n    for idx in range(12):\n        image = sess.run(elem)\n        plt.subplot(3,4,idx+1)\n        plt.imshow(image/2+0.5)\n        plt.grid(False)\n        plt.xticks([])\n        plt.yticks([])\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = td.Dataset.zip((augImageDS,oneHotLabelDS))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batchSize=2\nds = dataset.shuffle(buffer_size=len(imagePaths)//10)\nds = ds.repeat()\nds = ds.batch(batchSize)\nds = ds.prefetch(buffer_size=AUTOTUNE)\nds = ds.make_one_shot_iterator()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(ds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base = tf.keras.applications.InceptionResNetV2(input_shape=(299,299,3),include_top=False,weights='imagenet')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base.trainable=False\nbase.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.keras.Sequential([base,\n                             tf.keras.layers.GlobalAveragePooling2D(),\n                             tf.keras.layers.Dense(100,activation='relu'),\n                             tf.keras.layers.Dense(100,activation='relu'),\n                             tf.keras.layers.Dense(5)])\n\nmodel.compile(optimizer=tf.train.AdamOptimizer(),loss=tf.losses.softmax_cross_entropy,metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nmodel.fit(ds,epochs=20,verbose=1,steps_per_epoch=len(imagePaths)//batchSize)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('EyeClassifier')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}