{"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!pip uninstall tensorflow-cloud -y\n!pip install tensorflow_datasets==4.0.1\n\nimport re\nimport os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n#from tensorflow import keras\nimport tensorflow_datasets as tfds\nimport tensorflow_addons as tfa\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import class_weight\nimport datetime\n\nimport scipy\nimport gc\n\n#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)\n#except:\n#    strategy = tf.distribute.get_strategy()\n#print('Number of replicas:', strategy.num_replicas_in_sync)\n    \nprint(tf.__version__)\nprint(tfds.__version__)\n\n\n\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\n#import os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2020-12-28T01:13:38.241694Z","iopub.status.busy":"2020-12-28T01:13:38.240974Z","iopub.status.idle":"2020-12-28T01:14:01.133093Z","shell.execute_reply":"2020-12-28T01:14:01.133808Z"},"papermill":{"duration":22.951377,"end_time":"2020-12-28T01:14:01.134036","exception":false,"start_time":"2020-12-28T01:13:38.182659","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load And Split","metadata":{"papermill":{"duration":0.049464,"end_time":"2020-12-28T01:14:01.234228","exception":false,"start_time":"2020-12-28T01:14:01.184764","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Custom variables\nmanual_dir = r'/kaggle/input/retinopathy-btgraham300/tensorflow_datasets'\n\n#AUTOTUNE = tf.data.experimental.AUTOTUNE\n#BATCH_SIZE = 16 * strategy.num_replicas_in_sync\n#IMAGE_SIZE = [300, 300]\n#EPOCHS = 25","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2020-12-28T01:14:01.335951Z","iopub.status.busy":"2020-12-28T01:14:01.335Z","iopub.status.idle":"2020-12-28T01:14:01.338383Z","shell.execute_reply":"2020-12-28T01:14:01.337771Z"},"papermill":{"duration":0.056914,"end_time":"2020-12-28T01:14:01.338503","exception":false,"start_time":"2020-12-28T01:14:01.281589","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Configure dataset\n(ds_train, ds_val, ds_test), ds_info = \\\ntfds.load('diabetic_retinopathy_detection/btgraham-300:3.0.0',\n          split=['train', 'validation', 'test'],\n          download=False,data_dir=manual_dir, with_info=True,\n          shuffle_files=False, as_supervised=False)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:01.481268Z","iopub.status.busy":"2020-12-28T01:14:01.480647Z","iopub.status.idle":"2020-12-28T01:14:04.675857Z","shell.execute_reply":"2020-12-28T01:14:04.675214Z"},"papermill":{"duration":3.25107,"end_time":"2020-12-28T01:14:04.675984","exception":false,"start_time":"2020-12-28T01:14:01.424914","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(ds_info)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:04.779223Z","iopub.status.busy":"2020-12-28T01:14:04.77851Z","iopub.status.idle":"2020-12-28T01:14:04.787649Z","shell.execute_reply":"2020-12-28T01:14:04.788229Z"},"papermill":{"duration":0.062091,"end_time":"2020-12-28T01:14:04.78836","exception":false,"start_time":"2020-12-28T01:14:04.726269","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:04.890732Z","iopub.status.busy":"2020-12-28T01:14:04.890015Z","iopub.status.idle":"2020-12-28T01:14:04.893908Z","shell.execute_reply":"2020-12-28T01:14:04.893383Z"},"papermill":{"duration":0.055488,"end_time":"2020-12-28T01:14:04.894003","exception":false,"start_time":"2020-12-28T01:14:04.838515","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tensorflow.keras.utils import Sequence\n#from imblearn.over_sampling import RandomOverSampler\n#from imblearn.keras import balanced_batch_generator\n\n#class BalancedDataGenerator(Sequence):\n#    \"\"\"ImageDataGenerator + RandomOversampling\"\"\"\n#    def __init__(self, x, y, datagen, batch_size=32):\n#        self.datagen = datagen\n#        self.batch_size = min(batch_size, x.shape[0])\n#        datagen.fit(x)\n#        self.gen, self.steps_per_epoch = balanced_batch_generator(x.reshape(x.shape[0], -1), y, sampler=RandomOverSampler(), batch_size=self.batch_size, keep_sparse=True)\n#        self._shape = (self.steps_per_epoch * batch_size, *x.shape[1:])\n        \n#    def __len__(self):\n#        return self.steps_per_epoch\n\n#    def __getitem__(self, idx):\n#        x_batch, y_batch = self.gen.__next__()\n#        x_batch = x_batch.reshape(-1, *self._shape[1:])\n#        return self.datagen.flow(x_batch, y_batch, batch_size=self.batch_size).next()","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.000678Z","iopub.status.busy":"2020-12-28T01:14:04.999769Z","iopub.status.idle":"2020-12-28T01:14:05.003007Z","shell.execute_reply":"2020-12-28T01:14:05.003472Z"},"papermill":{"duration":0.056539,"end_time":"2020-12-28T01:14:05.003588","exception":false,"start_time":"2020-12-28T01:14:04.947049","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def map_image(tensor):\n#    return tensor['label']","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.10691Z","iopub.status.busy":"2020-12-28T01:14:05.106187Z","iopub.status.idle":"2020-12-28T01:14:05.110716Z","shell.execute_reply":"2020-12-28T01:14:05.110065Z"},"papermill":{"duration":0.056794,"end_time":"2020-12-28T01:14:05.110856","exception":false,"start_time":"2020-12-28T01:14:05.054062","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#[i for i in ds_train.map(map_image)]","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.216362Z","iopub.status.busy":"2020-12-28T01:14:05.215698Z","iopub.status.idle":"2020-12-28T01:14:05.219571Z","shell.execute_reply":"2020-12-28T01:14:05.220051Z"},"papermill":{"duration":0.057973,"end_time":"2020-12-28T01:14:05.220198","exception":false,"start_time":"2020-12-28T01:14:05.162225","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#balanced_gen = BalancedDataGenerator(\n#    [img for img in ds_train.map(lambda tensor:tensor['image'])],\n#    [label for label in ds_train.map(lambda tensor:tensor['label'])], \n#    datagen, batch_size=32)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.326941Z","iopub.status.busy":"2020-12-28T01:14:05.325019Z","iopub.status.idle":"2020-12-28T01:14:05.327817Z","shell.execute_reply":"2020-12-28T01:14:05.328389Z"},"papermill":{"duration":0.058086,"end_time":"2020-12-28T01:14:05.328535","exception":false,"start_time":"2020-12-28T01:14:05.270449","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for i in ds_train.take(1):\n#    print(i)\n#    print(\"Espacio\")\n#    print(i['image'].numpy())","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.437803Z","iopub.status.busy":"2020-12-28T01:14:05.435787Z","iopub.status.idle":"2020-12-28T01:14:05.439032Z","shell.execute_reply":"2020-12-28T01:14:05.43846Z"},"papermill":{"duration":0.059947,"end_time":"2020-12-28T01:14:05.439154","exception":false,"start_time":"2020-12-28T01:14:05.379207","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{"papermill":{"duration":0.050455,"end_time":"2020-12-28T01:14:05.541292","exception":false,"start_time":"2020-12-28T01:14:05.490837","status":"completed"},"tags":[]}},{"cell_type":"code","source":"vis = tfds.visualization.show_examples(ds_train, ds_info)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:05.646889Z","iopub.status.busy":"2020-12-28T01:14:05.645735Z","iopub.status.idle":"2020-12-28T01:14:06.926846Z","shell.execute_reply":"2020-12-28T01:14:06.927442Z"},"papermill":{"duration":1.336358,"end_time":"2020-12-28T01:14:06.927623","exception":false,"start_time":"2020-12-28T01:14:05.591265","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for tensor in ds_train.take(1):\n    image=tensor['image'].numpy()\n    label=tensor['label'].numpy()","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:07.053858Z","iopub.status.busy":"2020-12-28T01:14:07.052917Z","iopub.status.idle":"2020-12-28T01:14:07.172962Z","shell.execute_reply":"2020-12-28T01:14:07.171896Z"},"papermill":{"duration":0.186386,"end_time":"2020-12-28T01:14:07.173083","exception":false,"start_time":"2020-12-28T01:14:06.986697","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Etiqueta: \",label)\nplt.imshow(image);","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:07.298954Z","iopub.status.busy":"2020-12-28T01:14:07.297904Z","iopub.status.idle":"2020-12-28T01:14:07.516475Z","shell.execute_reply":"2020-12-28T01:14:07.515674Z"},"papermill":{"duration":0.283335,"end_time":"2020-12-28T01:14:07.516637","exception":false,"start_time":"2020-12-28T01:14:07.233302","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_list = [tensor['label'] for tensor in ds_train.as_numpy_iterator()]\nunique, counts = np.unique(label_list, return_counts=True)\nplt.bar(unique, counts)\nprint(*zip(unique, counts))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:14:07.650733Z","iopub.status.busy":"2020-12-28T01:14:07.649821Z","iopub.status.idle":"2020-12-28T01:15:04.174394Z","shell.execute_reply":"2020-12-28T01:15:04.173729Z"},"papermill":{"duration":56.593414,"end_time":"2020-12-28T01:15:04.174517","exception":false,"start_time":"2020-12-28T01:14:07.581103","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Class weights","metadata":{"papermill":{"duration":0.061403,"end_time":"2020-12-28T01:15:04.301853","exception":false,"start_time":"2020-12-28T01:15:04.24045","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from sklearn.utils.class_weight import compute_class_weight, compute_sample_weight\n \nclass_weights = compute_class_weight('balanced', np.unique(label_list), label_list)\nsample_weights = compute_sample_weight('balanced', label_list)\n \nclass_weights_dict = dict(enumerate(class_weights))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:04.446502Z","iopub.status.busy":"2020-12-28T01:15:04.445663Z","iopub.status.idle":"2020-12-28T01:15:04.519064Z","shell.execute_reply":"2020-12-28T01:15:04.519662Z"},"papermill":{"duration":0.15425,"end_time":"2020-12-28T01:15:04.519799","exception":false,"start_time":"2020-12-28T01:15:04.365549","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(sample_weights)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:04.65636Z","iopub.status.busy":"2020-12-28T01:15:04.655444Z","iopub.status.idle":"2020-12-28T01:15:04.659087Z","shell.execute_reply":"2020-12-28T01:15:04.65956Z"},"papermill":{"duration":0.074199,"end_time":"2020-12-28T01:15:04.659705","exception":false,"start_time":"2020-12-28T01:15:04.585506","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.bincount(label_list)\nii = np.nonzero(y)[0]\nlist_zip =[*zip(ii,y[ii])]\nlist_zip","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:04.803051Z","iopub.status.busy":"2020-12-28T01:15:04.80101Z","iopub.status.idle":"2020-12-28T01:15:04.807172Z","shell.execute_reply":"2020-12-28T01:15:04.806587Z"},"papermill":{"duration":0.081781,"end_time":"2020-12-28T01:15:04.807273","exception":false,"start_time":"2020-12-28T01:15:04.725492","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:04.95195Z","iopub.status.busy":"2020-12-28T01:15:04.951032Z","iopub.status.idle":"2020-12-28T01:15:04.95459Z","shell.execute_reply":"2020-12-28T01:15:04.95515Z"},"papermill":{"duration":0.075313,"end_time":"2020-12-28T01:15:04.955288","exception":false,"start_time":"2020-12-28T01:15:04.879975","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[y[i]*class_weights[i] for i in range(4)]\n    ","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.09572Z","iopub.status.busy":"2020-12-28T01:15:05.094941Z","iopub.status.idle":"2020-12-28T01:15:05.098342Z","shell.execute_reply":"2020-12-28T01:15:05.09883Z"},"papermill":{"duration":0.07732,"end_time":"2020-12-28T01:15:05.098945","exception":false,"start_time":"2020-12-28T01:15:05.021625","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reject undersampling","metadata":{"papermill":{"duration":0.067888,"end_time":"2020-12-28T01:15:05.232929","exception":false,"start_time":"2020-12-28T01:15:05.165041","status":"completed"},"tags":[]}},{"cell_type":"code","source":"#def count(counts, tensor):\n#  labels = tensor['label']\n\n#  class_1 = labels == 1\n#  class_1 = tf.cast(class_1, tf.int32)\n\n#  class_0 = labels == 0\n#  class_0 = tf.cast(class_0, tf.int32)\n\n#  class_2 = labels == 2\n#  class_2 = tf.cast(class_2, tf.int32)\n    \n#  class_3 = labels == 3\n#  class_3 = tf.cast(class_3, tf.int32)\n    \n#  class_4 = labels == 4\n#  class_4 = tf.cast(class_4, tf.int32)\n\n#  counts['class_0'] += tf.reduce_sum(class_0)\n#  counts['class_1'] += tf.reduce_sum(class_1)\n#  counts['class_2'] += tf.reduce_sum(class_2)\n#  counts['class_3'] += tf.reduce_sum(class_3)\n#  counts['class_4'] += tf.reduce_sum(class_4)  \n\n#  return counts","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.369832Z","iopub.status.busy":"2020-12-28T01:15:05.368975Z","iopub.status.idle":"2020-12-28T01:15:05.372095Z","shell.execute_reply":"2020-12-28T01:15:05.371598Z"},"papermill":{"duration":0.073709,"end_time":"2020-12-28T01:15:05.372214","exception":false,"start_time":"2020-12-28T01:15:05.298505","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#counts = ds_train.take(35126).reduce(\n#    initial_state={'class_0': 0, 'class_1': 0, 'class_2': 0, 'class_3': 0, 'class_4': 0},\n#    reduce_func = count)\n#\n#counts = np.array([counts['class_0'].numpy(),\n#                   counts['class_1'].numpy(),\n#                   counts['class_2'].numpy(),\n#                   counts['class_3'].numpy(),\n#                   counts['class_4'].numpy()]).astype(np.float32)\n\n#fractions = counts/counts.sum()\n#print(*fractions)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.514855Z","iopub.status.busy":"2020-12-28T01:15:05.513893Z","iopub.status.idle":"2020-12-28T01:15:05.517184Z","shell.execute_reply":"2020-12-28T01:15:05.516571Z"},"papermill":{"duration":0.076871,"end_time":"2020-12-28T01:15:05.517347","exception":false,"start_time":"2020-12-28T01:15:05.440476","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#def class_func(tensor): #Aux func for resampling 'cause the data is imbalance\n#    return tensor['label']","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.655318Z","iopub.status.busy":"2020-12-28T01:15:05.654483Z","iopub.status.idle":"2020-12-28T01:15:05.657641Z","shell.execute_reply":"2020-12-28T01:15:05.657161Z"},"papermill":{"duration":0.074042,"end_time":"2020-12-28T01:15:05.65774","exception":false,"start_time":"2020-12-28T01:15:05.583698","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#resampler = tf.data.experimental.rejection_resample(\n#    class_func, target_dist=[0.2,0.2,0.2,0.2,0.2], initial_dist=fractions)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.795146Z","iopub.status.busy":"2020-12-28T01:15:05.794271Z","iopub.status.idle":"2020-12-28T01:15:05.796913Z","shell.execute_reply":"2020-12-28T01:15:05.797543Z"},"papermill":{"duration":0.07394,"end_time":"2020-12-28T01:15:05.797664","exception":false,"start_time":"2020-12-28T01:15:05.723724","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#resample_ds = ds_train.apply(resampler).take(3800)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:05.936984Z","iopub.status.busy":"2020-12-28T01:15:05.936098Z","iopub.status.idle":"2020-12-28T01:15:05.939106Z","shell.execute_reply":"2020-12-28T01:15:05.938558Z"},"papermill":{"duration":0.073232,"end_time":"2020-12-28T01:15:05.939227","exception":false,"start_time":"2020-12-28T01:15:05.865995","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#balanced_ds = resample_ds.map(lambda extra_label, features_and_label: features_and_label)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:06.081449Z","iopub.status.busy":"2020-12-28T01:15:06.080684Z","iopub.status.idle":"2020-12-28T01:15:06.084016Z","shell.execute_reply":"2020-12-28T01:15:06.083463Z"},"papermill":{"duration":0.075273,"end_time":"2020-12-28T01:15:06.084136","exception":false,"start_time":"2020-12-28T01:15:06.008863","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label_list_balanced = [tensor['label'].numpy() for tensor in balanced_ds.take(3800)]\n#unique, counts = np.unique(label_list_balanced, return_counts=True)\n#plt.bar(unique, counts)\n#print(*zip(unique, counts))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:06.225263Z","iopub.status.busy":"2020-12-28T01:15:06.224279Z","iopub.status.idle":"2020-12-28T01:15:06.227487Z","shell.execute_reply":"2020-12-28T01:15:06.226987Z"},"papermill":{"duration":0.076212,"end_time":"2020-12-28T01:15:06.227594","exception":false,"start_time":"2020-12-28T01:15:06.151382","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocesing","metadata":{"papermill":{"duration":0.069094,"end_time":"2020-12-28T01:15:06.36454","exception":false,"start_time":"2020-12-28T01:15:06.295446","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def transform_images(row, size, reescale=True):\n    x_train = tf.image.resize(row['image'], (size, size))\n    if reescale:\n        x_train = x_train  / 255\n    return x_train, tf.one_hot(row['label'], depth=5)\ndef transform_images_complete(row, size):\n    x_train = tf.image.resize(row['image'], (size, size))\n    x_train = x_train  / 255\n    return x_train, tf.one_hot(row['label'], depth=5), row['name']\n\nds_train = ds_train.map(lambda row:transform_images(row, 400))\n#ds_train = resample_ds.map(lambda _, row:transform_images(row, 400))\nds_val = ds_val.map(lambda row:transform_images(row, 400))\n#ds_test_all = ds_test.map(lambda row:transform_images_complete(row, 300))\nds_test = ds_test.map(lambda row:transform_images(row, 400, reescale=True))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:06.516844Z","iopub.status.busy":"2020-12-28T01:15:06.515753Z","iopub.status.idle":"2020-12-28T01:15:06.685828Z","shell.execute_reply":"2020-12-28T01:15:06.685059Z"},"papermill":{"duration":0.250461,"end_time":"2020-12-28T01:15:06.685962","exception":false,"start_time":"2020-12-28T01:15:06.435501","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num classes: \" + str(ds_info.features['label'].num_classes))\nprint(\"Class names: \" + str(ds_info.features['label'].names))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:06.828543Z","iopub.status.busy":"2020-12-28T01:15:06.82772Z","iopub.status.idle":"2020-12-28T01:15:06.833127Z","shell.execute_reply":"2020-12-28T01:15:06.832557Z"},"papermill":{"duration":0.078548,"end_time":"2020-12-28T01:15:06.833241","exception":false,"start_time":"2020-12-28T01:15:06.754693","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_TRAIN_IMAGES = tf.data.experimental.cardinality(ds_train).numpy()\nprint(\"Num training images: \" + str(NUM_TRAIN_IMAGES))\n\nNUM_VAL_IMAGES = tf.data.experimental.cardinality(ds_val).numpy()\nprint(\"Num validating images: \" + str(NUM_VAL_IMAGES))\n\nNUM_TEST_IMAGES = tf.data.experimental.cardinality(ds_test).numpy()\nprint(\"Num testing images: \" + str(NUM_TEST_IMAGES))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:06.979768Z","iopub.status.busy":"2020-12-28T01:15:06.978851Z","iopub.status.idle":"2020-12-28T01:15:06.984856Z","shell.execute_reply":"2020-12-28T01:15:06.984361Z"},"papermill":{"duration":0.081631,"end_time":"2020-12-28T01:15:06.984948","exception":false,"start_time":"2020-12-28T01:15:06.903317","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#ds_train = ds_train.cache()\nds_train = ds_train.shuffle(1000)\nds_train = ds_train.batch(32)\n#ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE)\n\n#ds_val = ds_val.cache()\nds_val = ds_val.shuffle(1000)\nds_val = ds_val.batch(32)\n#ds_val = ds_val.prefetch(tf.data.experimental.AUTOTUNE)\n\n#ds_test = ds_test.cache()\nds_test = ds_test.batch(32)\n#ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:07.131894Z","iopub.status.busy":"2020-12-28T01:15:07.131215Z","iopub.status.idle":"2020-12-28T01:15:07.138829Z","shell.execute_reply":"2020-12-28T01:15:07.138311Z"},"papermill":{"duration":0.083427,"end_time":"2020-12-28T01:15:07.138932","exception":false,"start_time":"2020-12-28T01:15:07.055505","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in ds_train.take(1):\n    print(i)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:07.28565Z","iopub.status.busy":"2020-12-28T01:15:07.284778Z","iopub.status.idle":"2020-12-28T01:15:11.306357Z","shell.execute_reply":"2020-12-28T01:15:11.307254Z"},"papermill":{"duration":4.096326,"end_time":"2020-12-28T01:15:11.307459","exception":false,"start_time":"2020-12-28T01:15:07.211133","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Model","metadata":{"papermill":{"duration":0.0738,"end_time":"2020-12-28T01:15:11.483615","exception":false,"start_time":"2020-12-28T01:15:11.409815","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\n\nfrom tensorflow.keras.layers import Input # Input Layer\nfrom tensorflow.keras.applications import DenseNet121 # Keras Application\nfrom tensorflow.keras.layers import Dense # Dense Layer (Fully connected)\nfrom tensorflow.keras.models import Model # Model Structure\n\n\n\ninput_shape=(400, 400, 3)\n\nimg_input = Input(shape=input_shape)\nbase_model = DenseNet121(include_top=False, \n                         input_tensor=img_input, \n                         input_shape=input_shape, \n                         pooling=\"max\", \n                         weights='imagenet')\nbase_model.trainable = True\nx = base_model.output\npredictions = Dense(5, \n                    activation=\"softmax\", \n                    name=\"predictions\")(x)\nmodel = Model(inputs=img_input, \n              outputs=predictions)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:11.638712Z","iopub.status.busy":"2020-12-28T01:15:11.638014Z","iopub.status.idle":"2020-12-28T01:15:16.756338Z","shell.execute_reply":"2020-12-28T01:15:16.757528Z"},"papermill":{"duration":5.20005,"end_time":"2020-12-28T01:15:16.757713","exception":false,"start_time":"2020-12-28T01:15:11.557663","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001), #by default learning_rate=0.001\n    loss='categorical_crossentropy',\n    metrics=[tf.keras.metrics.CategoricalAccuracy(name=\"cat_acc\"),tf.keras.metrics.AUC(name='auc'),\n            tf.keras.metrics.Recall(name='recall'),tf.keras.metrics.Precision(name='precision')]\n)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:16.96918Z","iopub.status.busy":"2020-12-28T01:15:16.968045Z","iopub.status.idle":"2020-12-28T01:15:17.014733Z","shell.execute_reply":"2020-12-28T01:15:17.015275Z"},"papermill":{"duration":0.135119,"end_time":"2020-12-28T01:15:17.015418","exception":false,"start_time":"2020-12-28T01:15:16.880299","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{"papermill":{"duration":0.072989,"end_time":"2020-12-28T01:15:17.164218","exception":false,"start_time":"2020-12-28T01:15:17.091229","status":"completed"},"tags":[]}},{"cell_type":"code","source":"history = model.fit(\n    ds_train,\n    epochs=20,\n    #steps_per_epoch=NUM_TRAIN_IMAGES/32, los batchs de 32 ya han sido asignados arriba\n    validation_data=ds_val,\n    #validation_steps=NUM_VAL_IMAGES/32,\n    class_weight=class_weights_dict,\n    shuffle=True,\n    callbacks=[\n        #tf.keras.callbacks.EarlyStopping(patience=11, verbose=1),\n        tf.keras.callbacks.ReduceLROnPlateau(patience=4, verbose=1),\n        tf.keras.callbacks.ModelCheckpoint(filepath='bestmodel.h5',\n                                          verbose=1, save_best_only=True)\n    ]\n)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T01:15:17.326549Z","iopub.status.busy":"2020-12-28T01:15:17.32581Z","iopub.status.idle":"2020-12-28T06:00:33.949265Z","shell.execute_reply":"2020-12-28T06:00:33.950034Z"},"papermill":{"duration":17116.709892,"end_time":"2020-12-28T06:00:33.950257","exception":false,"start_time":"2020-12-28T01:15:17.240365","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot model training","metadata":{"papermill":{"duration":8.260378,"end_time":"2020-12-28T06:00:50.738298","exception":false,"start_time":"2020-12-28T06:00:42.47792","status":"completed"},"tags":[]}},{"cell_type":"code","source":"# Se realiza un gráfico de la precisión de entrenamiento y validación\nimport matplotlib.pyplot as plt\nauc = history.history['auc']\nval_auc = history.history['val_auc']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\ncat_acc = history.history['cat_acc']\nval_cat_acc = history.history['val_cat_acc']\n\nepochs = range(len(auc))\nplt.figure(figsize=(18, 4.8))\nplt.subplot(1,3,1)\nplt.plot(epochs, auc, 'r', label='Training auc')\nplt.plot(epochs, val_auc, 'b', label='Validation auc')\nplt.ylim(0, 1)\nplt.title('Training and validation AUC')\nplt.legend(loc=0)\n\nplt.subplot(1,3,2)\nplt.plot(epochs, loss, 'y-.', label='Training loss')\nplt.plot(epochs, val_loss, 'g-.', label='Validation loss')\nplt.title('Training and validation Loss')\nplt.ylim(0, 2)\nplt.legend(loc=0)\n\nplt.subplot(1,3,3)\nplt.plot(epochs, cat_acc, 'c-.', label='Training cat_acc')\nplt.plot(epochs, val_cat_acc, 'g', label='Validation cat_acc')\nplt.title('Training and validation cat_acc')\nplt.ylim(0, 1)\nplt.legend(loc=0)\n\n\n\nplt.show();","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:01:06.645055Z","iopub.status.busy":"2020-12-28T06:01:06.63899Z","iopub.status.idle":"2020-12-28T06:01:07.116722Z","shell.execute_reply":"2020-12-28T06:01:07.11722Z"},"papermill":{"duration":8.775656,"end_time":"2020-12-28T06:01:07.117355","exception":false,"start_time":"2020-12-28T06:00:58.341699","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Only use the best weights for the model.","metadata":{"papermill":{"duration":7.847463,"end_time":"2020-12-28T06:01:22.82921","exception":false,"start_time":"2020-12-28T06:01:14.981747","status":"completed"},"tags":[]}},{"cell_type":"code","source":"best_model = tf.keras.models.load_model('bestmodel.h5')","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:01:39.150837Z","iopub.status.busy":"2020-12-28T06:01:39.149881Z","iopub.status.idle":"2020-12-28T06:01:45.174407Z","shell.execute_reply":"2020-12-28T06:01:45.173574Z"},"papermill":{"duration":14.662983,"end_time":"2020-12-28T06:01:45.174541","exception":false,"start_time":"2020-12-28T06:01:30.511558","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preds and Evaluation","metadata":{"papermill":{"duration":7.774795,"end_time":"2020-12-28T06:02:00.645527","exception":false,"start_time":"2020-12-28T06:01:52.870732","status":"completed"},"tags":[]}},{"cell_type":"code","source":"preds = best_model.predict(ds_test, verbose=1)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:02:16.682466Z","iopub.status.busy":"2020-12-28T06:02:16.681401Z","iopub.status.idle":"2020-12-28T06:05:51.72522Z","shell.execute_reply":"2020-12-28T06:05:51.670507Z"},"papermill":{"duration":223.0453,"end_time":"2020-12-28T06:05:51.725356","exception":false,"start_time":"2020-12-28T06:02:08.680056","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation_model = best_model.evaluate(ds_test, verbose=1)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:06:09.183345Z","iopub.status.busy":"2020-12-28T06:06:09.182411Z","iopub.status.idle":"2020-12-28T06:09:39.281201Z","shell.execute_reply":"2020-12-28T06:09:39.280323Z"},"papermill":{"duration":218.653469,"end_time":"2020-12-28T06:09:39.281321","exception":false,"start_time":"2020-12-28T06:06:00.627852","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(*zip(evaluation_model,['loss','cat_acc','auc','recall','precision']))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:09:58.240944Z","iopub.status.busy":"2020-12-28T06:09:58.24005Z","iopub.status.idle":"2020-12-28T06:09:58.247391Z","shell.execute_reply":"2020-12-28T06:09:58.246614Z"},"papermill":{"duration":9.398755,"end_time":"2020-12-28T06:09:58.247539","exception":false,"start_time":"2020-12-28T06:09:48.848784","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds[1]","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:10:15.93755Z","iopub.status.busy":"2020-12-28T06:10:15.93676Z","iopub.status.idle":"2020-12-28T06:10:15.940209Z","shell.execute_reply":"2020-12-28T06:10:15.940832Z"},"papermill":{"duration":8.964382,"end_time":"2020-12-28T06:10:15.940978","exception":false,"start_time":"2020-12-28T06:10:06.976596","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = [np.argmax(pred) for pred in preds]","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:10:34.53954Z","iopub.status.busy":"2020-12-28T06:10:34.538635Z","iopub.status.idle":"2020-12-28T06:10:34.671601Z","shell.execute_reply":"2020-12-28T06:10:34.671056Z"},"papermill":{"duration":9.156625,"end_time":"2020-12-28T06:10:34.671728","exception":false,"start_time":"2020-12-28T06:10:25.515103","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(preds)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:10:54.260521Z","iopub.status.busy":"2020-12-28T06:10:54.259736Z","iopub.status.idle":"2020-12-28T06:10:54.2628Z","shell.execute_reply":"2020-12-28T06:10:54.263338Z"},"papermill":{"duration":9.772327,"end_time":"2020-12-28T06:10:54.263472","exception":false,"start_time":"2020-12-28T06:10:44.491145","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_test = ds_test.unbatch()","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:11:12.23244Z","iopub.status.busy":"2020-12-28T06:11:12.231508Z","iopub.status.idle":"2020-12-28T06:11:12.24039Z","shell.execute_reply":"2020-12-28T06:11:12.239845Z"},"papermill":{"duration":8.953033,"end_time":"2020-12-28T06:11:12.240494","exception":false,"start_time":"2020-12-28T06:11:03.287461","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actuals = [np.argmax(row[1]) for row in ds_test.as_numpy_iterator()]","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:11:30.377638Z","iopub.status.busy":"2020-12-28T06:11:30.376716Z","iopub.status.idle":"2020-12-28T06:13:55.885683Z","shell.execute_reply":"2020-12-28T06:13:55.884898Z"},"papermill":{"duration":154.736414,"end_time":"2020-12-28T06:13:55.885854","exception":false,"start_time":"2020-12-28T06:11:21.14944","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(actuals)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:14:14.217313Z","iopub.status.busy":"2020-12-28T06:14:14.215532Z","iopub.status.idle":"2020-12-28T06:14:14.219378Z","shell.execute_reply":"2020-12-28T06:14:14.217854Z"},"papermill":{"duration":9.253645,"end_time":"2020-12-28T06:14:14.219481","exception":false,"start_time":"2020-12-28T06:14:04.965836","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"preds:\",preds[:30])\nprint(\"trues:\",actuals[:30])","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:14:31.772974Z","iopub.status.busy":"2020-12-28T06:14:31.772171Z","iopub.status.idle":"2020-12-28T06:14:31.777541Z","shell.execute_reply":"2020-12-28T06:14:31.777026Z"},"papermill":{"duration":8.793393,"end_time":"2020-12-28T06:14:31.777648","exception":false,"start_time":"2020-12-28T06:14:22.984255","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_weights = compute_sample_weight('balanced', actuals)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:14:51.239761Z","iopub.status.busy":"2020-12-28T06:14:51.238891Z","iopub.status.idle":"2020-12-28T06:14:51.26985Z","shell.execute_reply":"2020-12-28T06:14:51.269224Z"},"papermill":{"duration":9.680079,"end_time":"2020-12-28T06:14:51.269977","exception":false,"start_time":"2020-12-28T06:14:41.589898","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True, weightage=\"quadratic\")\n#m = tf.keras.metrics.Accuracy()\nm.update_state(actuals, preds, sample_weight=sample_weights)\nprint('Final result: ', m.result().numpy())","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:15:09.264741Z","iopub.status.busy":"2020-12-28T06:15:09.263747Z","iopub.status.idle":"2020-12-28T06:35:29.392249Z","shell.execute_reply":"2020-12-28T06:35:29.39279Z"},"papermill":{"duration":1229.112279,"end_time":"2020-12-28T06:35:29.392935","exception":false,"start_time":"2020-12-28T06:15:00.280656","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import (mean_squared_error,confusion_matrix, plot_confusion_matrix, f1_score)\nfrom sklearn.metrics import classification_report\n\ntarget_names = ['class 0', 'class 1', 'class 2', 'class 3', 'class 4']\nprint(classification_report(actuals, preds, target_names=target_names))","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:35:49.392436Z","iopub.status.busy":"2020-12-28T06:35:49.390504Z","iopub.status.idle":"2020-12-28T06:35:49.548596Z","shell.execute_reply":"2020-12-28T06:35:49.55734Z"},"papermill":{"duration":9.930679,"end_time":"2020-12-28T06:35:49.557577","exception":false,"start_time":"2020-12-28T06:35:39.626898","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(actuals, preds)","metadata":{"execution":{"iopub.execute_input":"2020-12-28T06:36:08.152598Z","iopub.status.busy":"2020-12-28T06:36:08.15183Z","iopub.status.idle":"2020-12-28T06:36:08.260765Z","shell.execute_reply":"2020-12-28T06:36:08.260049Z"},"papermill":{"duration":9.680681,"end_time":"2020-12-28T06:36:08.260899","exception":false,"start_time":"2020-12-28T06:35:58.580218","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":9.311582,"end_time":"2020-12-28T06:36:26.786524","exception":false,"start_time":"2020-12-28T06:36:17.474942","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}