{"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 os\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport matplotlib.pyplot as plt\nimport numpy as np \nimport pandas as pd \n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T12:57:43.611879Z","iopub.execute_input":"2021-07-29T12:57:43.612192Z","iopub.status.idle":"2021-07-29T12:57:44.841586Z","shell.execute_reply.started":"2021-07-29T12:57:43.612161Z","shell.execute_reply":"2021-07-29T12:57:44.840821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE_FOLDER = \"/kaggle/input/prostate-cancer-grade-assessment/\"\ntrain = pd.read_csv(BASE_FOLDER+\"train.csv\")\ntest  = pd.read_csv(BASE_FOLDER+\"test.csv\")\nsub   = pd.read_csv(BASE_FOLDER+\"sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:58:00.040863Z","iopub.execute_input":"2021-07-29T12:58:00.041149Z","iopub.status.idle":"2021-07-29T12:58:00.067445Z","shell.execute_reply.started":"2021-07-29T12:58:00.041119Z","shell.execute_reply":"2021-07-29T12:58:00.066804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calificaciones de gleason\ntrain['gleason_score'].unique()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:59:38.676586Z","iopub.execute_input":"2021-07-29T12:59:38.676866Z","iopub.status.idle":"2021-07-29T12:59:38.689753Z","shell.execute_reply.started":"2021-07-29T12:59:38.676838Z","shell.execute_reply":"2021-07-29T12:59:38.688972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# los 0+0 y negativos son lo mismo\nprint(len(train[train['gleason_score']=='0+0']['isup_grade']))\nprint(len(train[train['gleason_score']=='negative']['isup_grade']))\n\n# hay que modificar eso , los negativos ahora son 0 + 0 \ntrain['gleason_score'] = train['gleason_score'].apply(lambda x: \"0+0\" if x==\"negative\" else x)\n\nprint(len(train[train['gleason_score']=='0+0']['isup_grade']))\nprint(len(train[train['gleason_score']=='negative']['isup_grade']))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:59:44.759849Z","iopub.execute_input":"2021-07-29T12:59:44.760125Z","iopub.status.idle":"2021-07-29T12:59:44.789502Z","shell.execute_reply.started":"2021-07-29T12:59:44.760084Z","shell.execute_reply":"2021-07-29T12:59:44.788618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GLEASON_SCORES = train['gleason_score'].unique()\nclassification = []\nprint(f\"SCR {80:>12}  {20:>12} {'suma':>12} {'igual':>12} {'total':>12}\")\nc_80 , c_20 = 0 ,0\nfor score in GLEASON_SCORES:\n    size = len(train[train['gleason_score'] == score])\n    s_80 ,s_20= round(size*.80), round(size*.20)\n    classification.append((score, int(s_80) , int(s_20)))\n    suma = int(s_80 + s_20)\n    c_80 , c_20 = c_80 + s_80 , c_20 + s_20\n    print(f\"{score:>2} {s_80:>12.2f} {(s_20):>12.2f} {(suma):>12.2f} {suma==size:>12} { size:>12}\")\nprint(f\"c_80:{c_80:>12} {('c_20: '+str(c_20)):>12} {'total: '+str(c_20 + c_80):>12}\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:59:47.906802Z","iopub.execute_input":"2021-07-29T12:59:47.907144Z","iopub.status.idle":"2021-07-29T12:59:47.94558Z","shell.execute_reply.started":"2021-07-29T12:59:47.907103Z","shell.execute_reply":"2021-07-29T12:59:47.944424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(train[train['gleason_score'] == score][0:17])\n\ntrain_csv , test_csv = pd.DataFrame([]), pd.DataFrame([])\n\nfor sizes in classification:\n    score, s_80 , s_20 = sizes\n    train_csv = train_csv.append(train[train['gleason_score'] == score][0:s_80])\n    test_csv  = test_csv.append(train[train['gleason_score'] == score][s_80:])\ntrain_csv = train_csv.reset_index(drop=True)\ntest_csv  = test_csv.reset_index(drop=True)\nprint(train_csv)\nprint(test_csv)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T12:59:52.741538Z","iopub.execute_input":"2021-07-29T12:59:52.741818Z","iopub.status.idle":"2021-07-29T12:59:52.855902Z","shell.execute_reply.started":"2021-07-29T12:59:52.74179Z","shell.execute_reply":"2021-07-29T12:59:52.855133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_demo , train_demo = [] , []\ntest_size , train_size = len(test_csv), len(train_csv) \nfor score in GLEASON_SCORES:\n    test_demo.append((score, len(test_csv[test_csv['gleason_score']==score])/test_size))\n    train_demo.append((score, len(train_csv[train_csv['gleason_score']==score])/train_size))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:00:17.717224Z","iopub.execute_input":"2021-07-29T13:00:17.717563Z","iopub.status.idle":"2021-07-29T13:00:17.760966Z","shell.execute_reply.started":"2021-07-29T13:00:17.717529Z","shell.execute_reply":"2021-07-29T13:00:17.760253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.arange(len(test_demo)) \nwidth = 0.40 \nfig, ax = plt.subplots()\nax.bar(x,[i[1] for i in test_demo],color='b')\nax.bar(x + width , [i[1] for i in train_demo],color='r')\nax.set_xticks(x + width / 2)\nax.set_xticklabels(GLEASON_SCORES)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:00:36.96498Z","iopub.execute_input":"2021-07-29T13:00:36.965243Z","iopub.status.idle":"2021-07-29T13:00:37.141796Z","shell.execute_reply.started":"2021-07-29T13:00:36.965216Z","shell.execute_reply":"2021-07-29T13:00:37.141038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# exportando \n!pwd\ntrain_csv.to_csv (r'train_csv.csv', index = False, header=True)\ntest_csv.to_csv (r'test_csv.csv', index = False, header=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:00.954139Z","iopub.execute_input":"2021-07-29T13:01:00.954427Z","iopub.status.idle":"2021-07-29T13:01:02.037223Z","shell.execute_reply.started":"2021-07-29T13:01:00.954398Z","shell.execute_reply":"2021-07-29T13:01:02.036518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# no hay intersecciones en ambos datasets\nvar = pd.merge(train_csv, test_csv, how='inner', on='image_id')\nprint(var)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:35.063779Z","iopub.execute_input":"2021-07-29T13:01:35.064075Z","iopub.status.idle":"2021-07-29T13:01:35.08849Z","shell.execute_reply.started":"2021-07-29T13:01:35.064042Z","shell.execute_reply":"2021-07-29T13:01:35.087399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parte 2 \nGenerar dataset de cada imagen","metadata":{}},{"cell_type":"code","source":"# aqui mas info de porque openslide https://www.kaggle.com/wouterbulten/getting-started-with-the-panda-dataset\nimport openslide\ndef open_image(id):\n    image = openslide.OpenSlide(BASE_FOLDER+'/train_images/'+ id + '.tiff')\n    #   patch = image.read_region((1780,1950), 0, (256, 256))\n    #isplay(patch)\n    full = image.get_thumbnail((600,400))\n    image.close()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:42.47008Z","iopub.execute_input":"2021-07-29T13:01:42.470389Z","iopub.status.idle":"2021-07-29T13:01:42.541834Z","shell.execute_reply.started":"2021-07-29T13:01:42.47035Z","shell.execute_reply":"2021-07-29T13:01:42.541124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# PANDA 16x128x128 tiles\n\nImplementacion de https://www.kaggle.com/iafoss/panda-16x128x128-tiles","metadata":{}},{"cell_type":"code","source":"TRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\nsz = 128\nN = 16\nseq_len = 16\nside = int(seq_len**.5)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:48.16973Z","iopub.execute_input":"2021-07-29T13:01:48.170017Z","iopub.status.idle":"2021-07-29T13:01:48.174608Z","shell.execute_reply.started":"2021-07-29T13:01:48.169989Z","shell.execute_reply":"2021-07-29T13:01:48.173835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\nsz = 64\nN = 16\nseq_len = 16\nside = int(seq_len**.5)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:50.92068Z","iopub.execute_input":"2021-07-29T13:01:50.920953Z","iopub.status.idle":"2021-07-29T13:01:50.925348Z","shell.execute_reply.started":"2021-07-29T13:01:50.920926Z","shell.execute_reply":"2021-07-29T13:01:50.924662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tile(img, mask):\n    result = []\n    shape = img.shape\n    print(shape)\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    \n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=255)\n    mask = np.pad(mask,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=0)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    mask = mask.reshape(mask.shape[0]//sz,sz,mask.shape[1]//sz,sz,3)\n    mask = mask.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n   \n    if len(img) < N:\n        mask = np.pad(mask,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=0)\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    \n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    img = img[idxs]\n    mask = mask[idxs]\n    return img\n    \"\"\"\n    for i in range(len(img)):\n        result.append({'img':img[i], 'mask':mask[i], 'idx':i})\n    return result\n    \"\"\"\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:54.037421Z","iopub.execute_input":"2021-07-29T13:01:54.03772Z","iopub.status.idle":"2021-07-29T13:01:54.054435Z","shell.execute_reply.started":"2021-07-29T13:01:54.037692Z","shell.execute_reply":"2021-07-29T13:01:54.053753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import array_to_img\n\n#sample = pd.read_csv('sample.csv')\nimages = [\n'07a7ef0ba3bb0d6564a73f4f3e1c2293',\n    '037504061b9fba71ef6e24c48c6df44d',\n    '035b1edd3d1aeeffc77ce5d248a01a53',\n    '059cbf902c5e42972587c8d17d49efed',\n    '06a0cbd8fd6320ef1aa6f19342af2e68',\n    '06eda4a6faca84e84a781fee2d5f47e1',\n    '0a4b7a7499ed55c71033cefb0765e93d',\n    '0838c82917cd9af681df249264d2769c',\n    '046b35ae95374bfb48cdca8d7c83233f',\n    '074c3e01525681a275a42282cd21cbde',\n    '05abe25c883d508ecc15b6e857e59f32',\n    '05f4e9415af9fdabc19109c980daf5ad',\n    '060121a06476ef401d8a21d6567dee6d',\n    '068b0e3be4c35ea983f77accf8351cc8',\n    '08f055372c7b8a7e1df97c6586542ac8'\n]\nslice = 10\n\n\nfor idx1 , x in enumerate(tqdm(images[:slice])):\n    \"\"\"\n    b = preprocess(x)\n    print(b.shape)\n    l = array_to_img(b)\n    #l.save(f\"{x}.png\")\n    ax[idx].imshow(l)\n    \"\"\"\n    img = skimage.io.MultiImage(os.path.join(TRAIN,x+'.tiff'))[-1]\n    mask = skimage.io.MultiImage(os.path.join(MASKS,x+'_mask.tiff'))[-1]\n    tiles = tile(img , mask)\n    f, ax = plt.subplots(1,len(tiles) ,figsize=(20,40))\n\n    for idx , t in enumerate(tiles):\n        img = t\n        #x_tot.append((img/255.0).reshape(-1,3).mean(0))\n        ax[idx].imshow(img)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:01:57.872387Z","iopub.execute_input":"2021-07-29T13:01:57.872677Z","iopub.status.idle":"2021-07-29T13:02:19.690089Z","shell.execute_reply.started":"2021-07-29T13:01:57.872648Z","shell.execute_reply":"2021-07-29T13:02:19.689141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parte 3 \nDesarrollo del modelo\n","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import Model, Sequential\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import categorical_crossentropy\nfrom tensorflow.keras.metrics import categorical_accuracy, top_k_categorical_accuracy\nimport tensorflow.keras.backend as K\nfrom tensorflow.keras.applications.densenet import DenseNet121\nfrom tensorflow.keras.layers import Input, Flatten, Dense, Dropout, GlobalAveragePooling2D, BatchNormalization, Activation","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:09:28.790449Z","iopub.execute_input":"2021-07-29T13:09:28.79073Z","iopub.status.idle":"2021-07-29T13:09:28.797672Z","shell.execute_reply.started":"2021-07-29T13:09:28.790701Z","shell.execute_reply":"2021-07-29T13:09:28.796679Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:09:32.439268Z","iopub.execute_input":"2021-07-29T13:09:32.439553Z","iopub.status.idle":"2021-07-29T13:09:32.449428Z","shell.execute_reply.started":"2021-07-29T13:09:32.439524Z","shell.execute_reply":"2021-07-29T13:09:32.448587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGen(tf.keras.utils.Sequence):\n    def __init__(self,\n                 image_shape,\n                 df,\n                 img_dir,\n                 mask_dir,\n                 MAPEO={},\n                 batch_size=32, \n                 seq_len=16,\n                 N=16,\n                 num_patches=16,\n                 augmentation=None,\n                 is_training=True,\n                 debug=False\n                ):\n        self.sz = image_shape\n        self.batch_size = batch_size\n        self.df = df.sample(frac=1).reset_index(drop=True)\n        self.num_patches = num_patches\n        self.img_dir = img_dir\n        self.mask_dir = mask_dir\n        self.aug = augmentation\n        self.seq_len = seq_len\n        self.is_training = is_training\n        self.N = N\n        self.side = int(seq_len**.5)\n        self.indices = range(df.shape[0])\n        self.debug = debug\n        self.MAPEO = MAPEO\n        \n    def __len__(self):\n        return self.df.shape[0] // self.batch_size\n\n    def __getitem__(self, index):\n        start , end = index*self.batch_size, (index + 1)*self.batch_size \n        img_ids = self.df['image_id'][start:end]\n        batch_images = [self.preprocess(img)/255. for img in img_ids]\n        batch_labels = [self.MAPEO[label] for label in self.df['gleason_score'][start:end]]\n        labels = np.zeros((self.batch_size, len(self.MAPEO)), dtype=np.float32)\n        for idx in range(self.batch_size):\n            labels[idx  , batch_labels[idx]] = 1\n        if self.debug:\n            f , ax  = plt.subplots(self.batch_size,figsize=(40,20))\n            for idx , x in enumerate(batch_images):\n                ax[idx].imshow(array_to_img(x))\n                ax[idx].axis('off')\n                ax[idx].set_title(batch_labels[idx])\n                \n        return np.array(batch_images),labels\n    \n    def labeler(self,label2):    \n        if label2==0:\n            label = tf.convert_to_tensor(np.array([0,0,0,0,0]))\n        elif label2==1:\n            label = tf.convert_to_tensor(np.array([1,0,0,0,0]))\n        elif label2==2:\n            label = tf.convert_to_tensor(np.array([1,1,0,0,0]))\n        elif label2==3:\n            label = tf.convert_to_tensor(np.array([1,1,1,0,0]))\n        elif label2==4:\n            label = tf.convert_to_tensor(np.array([1,1,1,1,0]))\n        else:\n            label = tf.convert_to_tensor(np.array([1,1,1,1,1]))\n        return label\n\n    def tile(self,img, mask=None):\n        result, sz , shape = [], self.sz , img.shape\n\n        pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n        img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)\n\n        img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n        img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n\n        if len(img) < N:\n            img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n        idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n\n        return np.array(img[idxs])\n\n    def glue_imgs(self,imgs_seq):\n        img_glue = np.zeros((sz* side, sz * side, 3), dtype=np.float32)\n        for i, ptch in enumerate(imgs_seq):\n            x = i // side\n            y = i % side\n            img_glue[x * sz : (x + 1) * sz, \n                     y * sz : (y + 1) * sz, :] = ptch\n        return img_glue\n\n    def preprocess(self,id):\n        img = skimage.io.MultiImage(os.path.join(TRAIN,id+'.tiff'))[-1]\n        tiles = self.tile(img)\n        glued = self.glue_imgs(tiles)\n        return glued","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:09:48.600901Z","iopub.execute_input":"2021-07-29T13:09:48.601168Z","iopub.status.idle":"2021-07-29T13:09:48.63279Z","shell.execute_reply.started":"2021-07-29T13:09:48.60114Z","shell.execute_reply":"2021-07-29T13:09:48.632049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Mapeo de gleason scores","metadata":{}},{"cell_type":"code","source":"# Calificaciones de gleason\nMAPEO = {}\nfor idx, score in enumerate(train['gleason_score'].unique()):\n    MAPEO[score] = idx\nprint(MAPEO)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:11:14.304214Z","iopub.execute_input":"2021-07-29T13:11:14.304544Z","iopub.status.idle":"2021-07-29T13:11:14.313531Z","shell.execute_reply.started":"2021-07-29T13:11:14.304511Z","shell.execute_reply":"2021-07-29T13:11:14.312501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = train_csv.sample(frac=1).reset_index(drop=True)\ndf_2 = test_csv.sample(frac=1).reset_index(drop=True)\nTRAIN = '../input/prostate-cancer-grade-assessment/train_images/'\nMASKS = '../input/prostate-cancer-grade-assessment/train_label_masks/'\nOUT_TRAIN = 'train.zip'\nOUT_MASKS = 'masks.zip'\nsz = 64\nN = 16\nseq_len = 16\nBATCH_SIZE = 12\nside = int(seq_len**.5)\n\ntrain_gen = DataGen(\n    image_shape =sz,\n    df=df,\n    img_dir=TRAIN,\n    mask_dir=MASKS,\n    batch_size=BATCH_SIZE, \n    seq_len=seq_len,\n    N=16,\n    debug=False,\n    MAPEO=MAPEO\n)\n\nvalid_gen = DataGen(\n    image_shape =sz,\n    df=df_2,\n    img_dir=TRAIN,\n    mask_dir=MASKS,\n    batch_size=BATCH_SIZE, \n    seq_len=seq_len,\n    N=16,\n    debug=False,\n    MAPEO=MAPEO\n)\na = train_gen[0]\n#print(a[0][1].shape)\n#_ = valid_gen[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:11:17.228868Z","iopub.execute_input":"2021-07-29T13:11:17.229132Z","iopub.status.idle":"2021-07-29T13:11:17.949611Z","shell.execute_reply.started":"2021-07-29T13:11:17.229101Z","shell.execute_reply":"2021-07-29T13:11:17.948618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelo \nDenseNet 121","metadata":{}},{"cell_type":"code","source":"from keras import backend as K\n        \n#ith strategy.scope():\nbase_model = DenseNet121(weights='imagenet', include_top=False, input_shape = (sz*4,sz*4,3))\n\nmodel = Sequential()\nmodel.add(base_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dropout(.5))\nmodel.add(Dense(1024, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(\n    loss='categorical_crossentropy',\n    optimizer=Adam(lr=1e-3),\n    metrics=['categorical_accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:11:55.015013Z","iopub.execute_input":"2021-07-29T13:11:55.015297Z","iopub.status.idle":"2021-07-29T13:12:07.723775Z","shell.execute_reply.started":"2021-07-29T13:11:55.015269Z","shell.execute_reply":"2021-07-29T13:12:07.722864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:12:21.420013Z","iopub.execute_input":"2021-07-29T13:12:21.420386Z","iopub.status.idle":"2021-07-29T13:12:21.479849Z","shell.execute_reply.started":"2021-07-29T13:12:21.420322Z","shell.execute_reply":"2021-07-29T13:12:21.478733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" tf.test.is_gpu_available()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:13:41.901585Z","iopub.execute_input":"2021-07-29T13:13:41.901886Z","iopub.status.idle":"2021-07-29T13:13:41.91611Z","shell.execute_reply.started":"2021-07-29T13:13:41.901855Z","shell.execute_reply":"2021-07-29T13:13:41.91489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 40\nhistory = model.fit(\n    train_gen,\n    validation_data=valid_gen,\n    epochs=EPOCHS,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T13:13:46.0818Z","iopub.execute_input":"2021-07-29T13:13:46.082169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}