{"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 numpy as np\nimport pandas as pd\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:18.133397Z","iopub.execute_input":"2021-09-09T13:18:18.133813Z","iopub.status.idle":"2021-09-09T13:18:18.142895Z","shell.execute_reply.started":"2021-09-09T13:18:18.133704Z","shell.execute_reply":"2021-09-09T13:18:18.142130Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyze the data structure","metadata":{}},{"cell_type":"code","source":"PATH = '/kaggle/input'\ndata_folder = os.listdir(PATH)[0]\nmain_path = os.path.join(PATH, data_folder)\nprint(f'the data is in: {main_path}')","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:18.672137Z","iopub.execute_input":"2021-09-09T13:18:18.672524Z","iopub.status.idle":"2021-09-09T13:18:18.679028Z","shell.execute_reply.started":"2021-09-09T13:18:18.672486Z","shell.execute_reply":"2021-09-09T13:18:18.677874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'The data structure is: {os.listdir(main_path)}')","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:18.765143Z","iopub.execute_input":"2021-09-09T13:18:18.765470Z","iopub.status.idle":"2021-09-09T13:18:18.771902Z","shell.execute_reply.started":"2021-09-09T13:18:18.765441Z","shell.execute_reply":"2021-09-09T13:18:18.770766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Business understanding","metadata":{}},{"cell_type":"markdown","source":"Histopathologic Cancer Detection\n\nWe have a histopathology by light microscopy: It is a sample of dough extracted from an organ (biopsy) with an ematoxylin eosin stain, this staining shows us the cytoplasm, nucleus and the plasma membrane (It only shows the morphology of the cell). For the cell to be normal the image should be like:\n1. The cell must have a polarity (an order in orientation)\n2. Homogeneous cell differentiation: It must have a specialized order, for example, ranging from large to small cells.\n3. That it is not dysmorphic (same size between equal cells)\n4. Neoplasia: Do not exceed the basement membrane (basement membrane discontinuity is cancer)","metadata":{}},{"cell_type":"markdown","source":"# Data Understanding","metadata":{}},{"cell_type":"code","source":"# See the shape of all images:\n\n\ndef get_size(folder):\n    sizes = []\n    # Get from main directory all sub-directories\n    for folder_path in os.listdir(folder):\n        # Skip\n\n        if (folder_path == 'train_labels.csv' or folder_path == 'sample_submission.csv'):\n            #print(f'{folder_path}')\n            continue\n        # See Train and Test sub-directories\n        count = 0\n        for filename in os.listdir(os.path.join(folder, folder_path)):\n            if count == 100:\n                break\n            count += 1\n            # take image\n            img = Image.open(os.path.join(folder, folder_path, filename))\n            # Get image with \n            #print(img.size)\n            # Stores data like: (width, height)\n            sizes.append(img.size)\n        \n        print(f'{folder_path}')\n        print(f'the max width is: {max(sizes[0])}, and the min width is: {min(sizes[0])}')\n        print(f'the max height is: {max(sizes[1])}, and the min height is: {min(sizes[1])}')\n        print(f'the mean width is: {np.mean(sizes[0])}, and the mean height is: {np.mean(sizes[1])}')\n\n# Call the function\nget_size(main_path)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:19.107044Z","iopub.execute_input":"2021-09-09T13:18:19.107371Z","iopub.status.idle":"2021-09-09T13:18:33.057853Z","shell.execute_reply.started":"2021-09-09T13:18:19.107344Z","shell.execute_reply":"2021-09-09T13:18:33.056799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Paths\n\nimages_folder_train = os.path.join(main_path , 'train/')\ntrain_path_labels = os.path.join(main_path , 'train_labels.csv')\n\nimages_folder_test = os.path.join(main_path , 'test/')\ntest_path_labels = os.path.join(main_path , 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:33.059182Z","iopub.execute_input":"2021-09-09T13:18:33.059464Z","iopub.status.idle":"2021-09-09T13:18:33.064414Z","shell.execute_reply.started":"2021-09-09T13:18:33.059434Z","shell.execute_reply":"2021-09-09T13:18:33.063450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## See Images","metadata":{}},{"cell_type":"code","source":"# See some files\nprint(f'the number of images are {len(os.listdir(images_folder_train))}')\nos.listdir(images_folder_train)[:10]","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:33.065923Z","iopub.execute_input":"2021-09-09T13:18:33.066206Z","iopub.status.idle":"2021-09-09T13:18:33.447073Z","shell.execute_reply.started":"2021-09-09T13:18:33.066176Z","shell.execute_reply":"2021-09-09T13:18:33.446228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plot_images(axis=(2,2), train = True):\n    \n    if train:\n        img_path = images_folder_train\n        # name of images \n        df = pd.read_csv(train_path_labels)['id']\n        labels = pd.read_csv(train_path_labels)['label']\n    else:\n        img_path = images_folder_test\n        # name of images \n        labels = pd.read_csv(test_path_labels)['label']\n        df = pd.read_csv(test_path_labels)['id']\n        \n    \n        \n    # Grid\n    f, axarr = plt.subplots(axis[0], axis[1], figsize=(30/axis[1], 10))\n    \n    for i in range(0,axis[0]):\n        for j in range (0,axis[1]):\n            \n            # Choose a random image\n            index_img = np.random.randint(len(df))\n            image_name = df.iloc[index_img]\n            image_path = os.path.join(img_path, image_name)\n            \n            image_label = labels.iloc[index_img]\n            \n            if (not train):\n                image_label = 'No label'\n            elif (image_label == 0):\n                image_label = 'No cancer'\n            elif (image_label == 1):\n                image_label = 'Cancer'\n            \n            # Read Image:\n            img = Image.open(image_path+'.tif').convert('RGB')\n            # To numpy\n            img = np.asarray(img)\n            # print(f'image shape {img.shape}, max: {img.max()}, min: {img.min()}')\n            # Plot\n            axarr[i,j].imshow(img)\n            axarr[i,j].title.set_text(image_label)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:33.448396Z","iopub.execute_input":"2021-09-09T13:18:33.448658Z","iopub.status.idle":"2021-09-09T13:18:33.457791Z","shell.execute_reply.started":"2021-09-09T13:18:33.448632Z","shell.execute_reply":"2021-09-09T13:18:33.456636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(axis=(3,3), train = True)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:33.459270Z","iopub.execute_input":"2021-09-09T13:18:33.459541Z","iopub.status.idle":"2021-09-09T13:18:35.279426Z","shell.execute_reply.started":"2021-09-09T13:18:33.459514Z","shell.execute_reply":"2021-09-09T13:18:35.278262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(axis=(3,3), train=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:35.280860Z","iopub.execute_input":"2021-09-09T13:18:35.281405Z","iopub.status.idle":"2021-09-09T13:18:36.458021Z","shell.execute_reply.started":"2021-09-09T13:18:35.281364Z","shell.execute_reply":"2021-09-09T13:18:36.457074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## See Labels","metadata":{}},{"cell_type":"code","source":"# Train Images and labels\ntrain_map = pd.read_csv(train_path_labels)\ntrain_map.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:36.459367Z","iopub.execute_input":"2021-09-09T13:18:36.459966Z","iopub.status.idle":"2021-09-09T13:18:36.702763Z","shell.execute_reply.started":"2021-09-09T13:18:36.459923Z","shell.execute_reply":"2021-09-09T13:18:36.701718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test Images and labels\ntest_map = pd.read_csv(test_path_labels)\ntest_map.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:36.705130Z","iopub.execute_input":"2021-09-09T13:18:36.705435Z","iopub.status.idle":"2021-09-09T13:18:36.769359Z","shell.execute_reply.started":"2021-09-09T13:18:36.705403Z","shell.execute_reply":"2021-09-09T13:18:36.768252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data distribution\ntrain_map.groupby(['label'])['id'].count().plot(kind='bar', stacked=True)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:36.771626Z","iopub.execute_input":"2021-09-09T13:18:36.772064Z","iopub.status.idle":"2021-09-09T13:18:36.931197Z","shell.execute_reply.started":"2021-09-09T13:18:36.772020Z","shell.execute_reply":"2021-09-09T13:18:36.930106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preparation","metadata":{}},{"cell_type":"code","source":"# label datatype to string\ntrain_map['label'] = train_map['label'].astype(str)\ntest_map['label'] = test_map['label'].astype(str)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:36.932465Z","iopub.execute_input":"2021-09-09T13:18:36.932827Z","iopub.status.idle":"2021-09-09T13:18:37.123466Z","shell.execute_reply.started":"2021-09-09T13:18:36.932795Z","shell.execute_reply":"2021-09-09T13:18:37.122531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add .tif\ntrain_map.id = train_map.id.apply(lambda name: name + '.tif')\ntest_map.id = test_map.id.apply(lambda name: name + '.tif')\n\ntrain_map","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:37.124575Z","iopub.execute_input":"2021-09-09T13:18:37.124825Z","iopub.status.idle":"2021-09-09T13:18:37.222283Z","shell.execute_reply.started":"2021-09-09T13:18:37.124800Z","shell.execute_reply":"2021-09-09T13:18:37.221287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# Split Train and test\ntrain, test = train_test_split(train_map, test_size=0.2, random_state=1)\n\n# Split Train and validation\ntrain, validation = train_test_split(train_map, test_size=0.1, random_state=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:37.223870Z","iopub.execute_input":"2021-09-09T13:18:37.224258Z","iopub.status.idle":"2021-09-09T13:18:38.437707Z","shell.execute_reply.started":"2021-09-09T13:18:37.224215Z","shell.execute_reply":"2021-09-09T13:18:38.436627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:38.439006Z","iopub.execute_input":"2021-09-09T13:18:38.439303Z","iopub.status.idle":"2021-09-09T13:18:38.449757Z","shell.execute_reply.started":"2021-09-09T13:18:38.439274Z","shell.execute_reply":"2021-09-09T13:18:38.448597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:38.451215Z","iopub.execute_input":"2021-09-09T13:18:38.451526Z","iopub.status.idle":"2021-09-09T13:18:38.467721Z","shell.execute_reply.started":"2021-09-09T13:18:38.451493Z","shell.execute_reply":"2021-09-09T13:18:38.466610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:38.469209Z","iopub.execute_input":"2021-09-09T13:18:38.469612Z","iopub.status.idle":"2021-09-09T13:18:38.481633Z","shell.execute_reply.started":"2021-09-09T13:18:38.469579Z","shell.execute_reply":"2021-09-09T13:18:38.480588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generator and Data Agumentation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# We can't load all data in memory at once, so we use a DataGenerator\n#Create instance of ImageDataGenerator Class\nimage_gen_train = ImageDataGenerator(\n                    # Rescale\n                    rescale=1./255,\n                    # Rotate 30\n                    rotation_range=30,\n                    # Shift pixel values\n                    width_shift_range=.20,\n                    height_shift_range=.20,\n                    # Flip all image\n                    horizontal_flip=True,\n                    # Random zoom\n                    zoom_range=0.4\n                    )\nimage_gen_test = ImageDataGenerator(rescale=1./255)\nimage_gen_valid = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:38.483253Z","iopub.execute_input":"2021-09-09T13:18:38.483520Z","iopub.status.idle":"2021-09-09T13:18:44.069241Z","shell.execute_reply.started":"2021-09-09T13:18:38.483494Z","shell.execute_reply":"2021-09-09T13:18:44.068367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"width = 96 # width = height\nbatch_size = 32\n\n# Custom datagenerator\ntrain_datagen = image_gen_train.flow_from_dataframe(dataframe=train,\n                                                    directory=images_folder_train,\n                                                    x_col='id',\n                                                    y_col='label',\n                                                    batch_size=batch_size, #16,32,64...\n                                                    seed=1,\n                                                    shuffle=True,\n                                                    class_mode=\"binary\",\n                                                    target_size=(width,width))\n                                                                \ntest_datagen = image_gen_test.flow_from_dataframe(dataframe=test,\n                                                    directory=images_folder_train,\n                                                    x_col='id',\n                                                    y_col='label',\n                                                    batch_size=batch_size, #16,32,64...\n                                                    seed=1,\n                                                    shuffle=False,\n                                                    class_mode=\"binary\",\n                                                    target_size=(width,width))\n\nvalid_datagen = image_gen_valid.flow_from_dataframe(dataframe=validation,\n                                                    directory=images_folder_train,\n                                                    x_col='id',\n                                                    y_col='label',\n                                                    batch_size=batch_size, #16,32,64...\n                                                    seed=1,\n                                                    shuffle=True,\n                                                    class_mode=\"binary\",\n                                                    target_size=(width,width))","metadata":{"execution":{"iopub.status.busy":"2021-09-09T13:18:44.070501Z","iopub.execute_input":"2021-09-09T13:18:44.070761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\ndef plot_images_datagen(axis=(2,2), images=None):\n\n    # Grid\n    f, axarr = plt.subplots(axis[0], axis[1], figsize=(30/axis[1], 10))\n    index = 0\n    for i in range(0,axis[0]):\n        for j in range (0,axis[1]):\n            # Plot\n            axarr[i,j].imshow(images[index])\n            index += 1\n    plt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# See Example of image datagenerator\nexample = image_gen_train.flow_from_dataframe(dataframe=validation,\n                                                    directory=images_folder_train,\n                                                    x_col='id',\n                                                    y_col='label',\n                                                    batch_size=batch_size, #16,32,64...\n                                                    seed=1,\n                                                    shuffle=True,\n                                                    class_mode=\"binary\",\n                                                    target_size=(width,width))\n\nimages, _ = next(example)\nexample_images = images[:9]\nplot_images_datagen(axis=(3,3), images=example_images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"markdown","source":"# Create Model\n* <a href=\"https://arxiv.org/abs/1512.03385\">Resnet 50 </a>","metadata":{}},{"cell_type":"code","source":"# See if GPU is aviable\nimport tensorflow as tf\n\ngpu = len(tf.config.list_physical_devices('GPU'))>0\nprint(\"GPU is\", \"available\" if gpu else \"NOT AVAILABLE\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import applications\n\n# See model\napplications.resnet50.ResNet50(weights= None).summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\n\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.models import Model\n\nouts = 1\n\n\nclass MyModel(tf.keras.Model):\n\n    def __init__(self, n_outputs=outs, pretrained=False, freeze=False, size = width, depth = 3):\n        \n        super(MyModel, self).__init__()\n        \n        \n        if pretrained:\n            self.model_weights = 'imagenet'\n        else:\n            self.model_weights = None\n        \n        # Download the architecture of ResNet50 with ImageNet weights\n        self.resnet = applications.resnet50.ResNet50(include_top=False, weights=self.model_weights, input_shape= (width,width, depth))\n        \n        # Taking the output of the last convolution block in ResNet50\n        self.res_out = self.resnet.output\n        self.res_in = self.resnet.input\n        \n        self.GlobPoll = GlobalAveragePooling2D()\n        \n        # Adding a fully connected layer having 1024 neurons\n        #self.fc1 = Dense(1024, activation='relu')\n        \n        # Sigmoid Out\n        self.out = Dense(outs, activation='softmax')\n        \n        if freeze:\n            # Training only top layers i.e. the layers which we have added in the end\n            self.resnet.trainable = False\n\n    def call(self, inputs):\n\n        x = self.resnet(inputs)\n        x = self.GlobPoll(x)\n        #x = self.fc1(x)\n        x = self.out(x)\n        \n        return x\n\n\nclass MyModel(tf.keras.Model):\n\n    def __init__(self, n_outputs=outs, pretrained=False, freeze=False, size = width, depth = 3):\n        \n        super(MyModel, self).__init__()\n        \n        \n        if pretrained:\n            self.model_weights = 'imagenet'\n        else:\n            self.model_weights = None\n        \n        # Download the architecture of ResNet50 with ImageNet weights\n        self.resnet = applications.resnet50.ResNet50(include_top=False, weights=self.model_weights, input_shape= (width,width, depth))\n        \n        # Taking the output of the last convolution block in ResNet50\n        self.res_out = self.resnet.output\n        self.res_in = self.resnet.input\n        \n        self.GlobPoll = GlobalAveragePooling2D()\n        \n        # Adding a fully connected layer having 1024 neurons\n        #self.fc1 = Dense(1024, activation='relu')\n        \n        # Sigmoid Out\n        self.out = Dense(outs, activation='sigmoid')\n        \n        if freeze:\n            # Training only top layers i.e. the layers which we have added in the end\n            self.resnet.trainable = False\n\n    def call(self, inputs):\n\n        x = self.resnet(inputs)\n        x = self.GlobPoll(x)\n        #x = self.fc1(x)\n        x = self.out(x)\n        \n        return x\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.seed(1)\ntf.random.set_seed(1234)\n\n# With Class\nmodel = MyModel()\n#model.build(input_shape=(None,width, width, 3))\n#model.summary()\n# Model \n#model.load_weights('/kaggle/working/Models/ModelResnet50/Resnet50_tf_batch32_NoPretrained')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train Model","metadata":{}},{"cell_type":"markdown","source":"### Custom Loss unbalanced","metadata":{}},{"cell_type":"code","source":"positive_weights = {}\nnegative_weights = {}\n\npositive_weights['label'] = train.shape[0]/(2*np.count_nonzero(train['label']=='1'))\nnegative_weights['label'] = train.shape[0]/(2*np.count_nonzero(train['label']=='0'))\n\nprint(positive_weights)\nprint('----------------------')\nprint(negative_weights)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# custon Binary Crossentropy\nimport tensorflow.keras.backend as K\n\ndef loss_fn(y_true,y_pred):\n    \n    y_true = tf.cast(y_true, tf.float32)\n    \n    #print(y_true.dtype)\n    #print(y_pred.dtype)\n    loss = 0\n    loss -= (positive_weights['label']*y_true[0]*K.log(y_pred[0]) + negative_weights['label']*(1-y_true[0])*K.log(1-y_pred[0]))\n    #print(loss)\n    return loss","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile with custom loss\nmodel.compile(optimizer = tf.keras.optimizers.Adam(3e-5), loss = loss_fn, metrics = ['categorical_accuracy','accuracy'])\n\n#model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['categorical_accuracy','accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\n# EarlyStopping:\nmonitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, \n        verbose=1, mode='auto', restore_best_weights=True)\n\nSTEP_SIZE_TRAIN = train_datagen.n//train_datagen.batch_size\nSTEP_SIZE_VALID = valid_datagen.n//valid_datagen.batch_size\nSTEP_SIZE_TEST = test_datagen.n//test_datagen.batch_size\n\n\n\n# https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/keras/Model#fit\nmodel.fit(x = train_datagen,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_datagen,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=1,\n                    callbacks=[monitor]\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"markdown","source":"## Test Model","metadata":{}},{"cell_type":"code","source":"# Predict \n#test_datagen.reset()\npred = model.predict_generator(test_datagen,\n                            steps=STEP_SIZE_TEST,\n                            verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('the predictions are: ')\npred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('the predictions are: ')\n# Transform predictions to 0 or 1\nround_pred = np.rint(pred)\nround_pred","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = test_datagen.labels\nprint(f'the actual values are: {y_true[:5]}...')\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score\nfrom sklearn.metrics import f1_score\nfrom sklearn.metrics import accuracy_score\n\ndef get_metrics(y_true=y_true, round_pred=round_pred):\n\n    y_true = y_true[:round_pred.shape[0]]\n    \n    # accuracy: (tp + tn) / (p + n)\n    accuracy = accuracy_score(y_true, round_pred)\n    print('Accuracy: %f' % accuracy)\n    # precision tp / (tp + fp)\n    precision = precision_score(y_true, round_pred)\n    print('Precision: %f' % precision)\n    # recall: tp / (tp + fn)\n    recall = recall_score(y_true, round_pred)\n    print('Recall: %f' % recall)\n    # f1: 2 tp / (2 tp + fp + fn)\n    f1 = f1_score(y_true, round_pred)\n    print('F1 score: %f' % f1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_metrics(y_true, round_pred)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the weights (Class)\nmodel.save_weights('/kaggle/working/Models/ModelResnet50/Resnet50_tf_batch32_NoPretrained')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}