{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-01T21:20:46.500903Z","iopub.execute_input":"2023-02-01T21:20:46.501253Z","iopub.status.idle":"2023-02-01T21:20:46.50683Z","shell.execute_reply.started":"2023-02-01T21:20:46.501223Z","shell.execute_reply":"2023-02-01T21:20:46.505618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's download the data ","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:46.521381Z","iopub.execute_input":"2023-02-01T21:20:46.52164Z","iopub.status.idle":"2023-02-01T21:20:46.681677Z","shell.execute_reply.started":"2023-02-01T21:20:46.521615Z","shell.execute_reply":"2023-02-01T21:20:46.680708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploring the data","metadata":{}},{"cell_type":"code","source":"print(f'Length of train dataframe: {len(train_df)}\\n')\nprint(f'Number of NaN values:\\n{train_df.isna().sum()}\\n')","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:46.683946Z","iopub.execute_input":"2023-02-01T21:20:46.68493Z","iopub.status.idle":"2023-02-01T21:20:46.70146Z","shell.execute_reply.started":"2023-02-01T21:20:46.684894Z","shell.execute_reply":"2023-02-01T21:20:46.70043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> 37 missing value on Age, and more than  25000 value on BIRAFS and density ","metadata":{}},{"cell_type":"code","source":"#->For simple model fitting and future predictions I'm gonna use only patient scans.\n#let's consider one particular patient\npatient_id = train_df[train_df.cancer == 1].iloc[0].patient_id\n\none_patient_df = train_df[train_df.patient_id == patient_id]\none_patient_df","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:46.703196Z","iopub.execute_input":"2023-02-01T21:20:46.703561Z","iopub.status.idle":"2023-02-01T21:20:46.727725Z","shell.execute_reply.started":"2023-02-01T21:20:46.703527Z","shell.execute_reply":"2023-02-01T21:20:46.726583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install dicomsdl","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:46.730498Z","iopub.execute_input":"2023-02-01T21:20:46.731125Z","iopub.status.idle":"2023-02-01T21:20:59.69643Z","shell.execute_reply.started":"2023-02-01T21:20:46.731091Z","shell.execute_reply":"2023-02-01T21:20:59.695283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_dir = '/kaggle/input/rsna-breast-cancer-detection/{}_images/{}/{}.dcm'\ntrain = 'train'\ntest = 'test'","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:59.697979Z","iopub.execute_input":"2023-02-01T21:20:59.698587Z","iopub.status.idle":"2023-02-01T21:20:59.704713Z","shell.execute_reply.started":"2023-02-01T21:20:59.698547Z","shell.execute_reply":"2023-02-01T21:20:59.703703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport dicomsdl\n\nn_rows = len(one_patient_df)\n\nplt.figure(figsize=(5 * n_rows, 5))\nfor i in range(n_rows):\n    row = one_patient_df.iloc[i]\n    \n    plt.subplot(1, n_rows, i + 1)\n    \n    img_arr = dicomsdl.open(images_dir.format(train, row.patient_id, row.image_id)).pixelData()\n    plt.imshow(img_arr, cmap = plt.cm.bone)\n    plt.text(200, 300, row['view'], fontsize = 14, bbox={'facecolor': 'white', 'pad' : 5})\n    plt.text(200, 700, row['cancer'], fontsize = 14, bbox={'facecolor': 'white', 'pad' : 5})","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:20:59.706352Z","iopub.execute_input":"2023-02-01T21:20:59.706686Z","iopub.status.idle":"2023-02-01T21:21:08.258465Z","shell.execute_reply.started":"2023-02-01T21:20:59.706652Z","shell.execute_reply":"2023-02-01T21:21:08.257634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\n\nplt.figure(figsize=(5, 8))\nsns.countplot(data = train_df, x=\"laterality\", hue=\"cancer\", dodge = False)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:08.259457Z","iopub.execute_input":"2023-02-01T21:21:08.259887Z","iopub.status.idle":"2023-02-01T21:21:09.022675Z","shell.execute_reply.started":"2023-02-01T21:21:08.259839Z","shell.execute_reply":"2023-02-01T21:21:09.021737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> number of left and right breast pictures is nearly the same","metadata":{}},{"cell_type":"code","source":"sns.displot(data=train_df, x='age', kde=True)\nplt.title(\"Distribution of Age\")","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:09.024324Z","iopub.execute_input":"2023-02-01T21:21:09.024657Z","iopub.status.idle":"2023-02-01T21:21:09.621702Z","shell.execute_reply.started":"2023-02-01T21:21:09.024622Z","shell.execute_reply":"2023-02-01T21:21:09.620714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Distribution and count of the age column\nm = {\n    '20-30': 0,\n    '30-40': 0,\n    '40-50': 0,\n    '50-60': 0,\n    '60-70': 0,\n    '70-80': 0,\n    '80-90': 0\n}\nfor index, row in train_df.iterrows():\n    if row['age']>=20 and row['age']<30: m['20-30'] += 1 \n    elif row['age']>=30 and row['age']<40: m['30-40'] += 1\n    elif row['age']>=40 and row['age']<50: m['40-50'] += 1\n    elif row['age']>=50 and row['age']<60: m['50-60'] += 1\n    elif row['age']>=60 and row['age']<70: m['60-70'] += 1\n    elif row['age']>=70 and row['age']<80: m['70-80'] += 1\n    else: m['80-90'] += 1\nfig, ax = plt.subplots(figsize=(12,10))\nax = sns.barplot(x=list(m.keys()), y=list(m.values()))\nax.bar_label(ax.containers[0])\nplt.title(\"Patient Count of Different Age Groups\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:09.626072Z","iopub.execute_input":"2023-02-01T21:21:09.626341Z","iopub.status.idle":"2023-02-01T21:21:14.11574Z","shell.execute_reply.started":"2023-02-01T21:21:09.626316Z","shell.execute_reply":"2023-02-01T21:21:14.114739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> From the above chart and plot it can be noticed that people from the age group of 50 to 70 have more chances of occurence of breast canncer. The distribution looks like a bell curve. Distribution of age almost like a normal distribution.","metadata":{}},{"cell_type":"markdown","source":"# Frequency of cancer and normal patients","metadata":{}},{"cell_type":"code","source":"# Let's visualize how many patient in the dataset have cancer and how many don't\ntemp = train_df.groupby('patient_id')['cancer'].max().to_frame()\ntemp.reset_index(inplace=True)\ntemp = temp['cancer'].value_counts().to_frame()\ntemp.reset_index(inplace=True)\ntemp.columns = ['cancer', 'patient_count']\nfig,ax = plt.subplots(figsize=(12, 8))\nax = sns.barplot(data=temp, x=temp.columns[0], y=temp.columns[1])\nax.bar_label(ax.containers[0])\nplt.title('Frequency of normal patients and breast cancer patients')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:14.117824Z","iopub.execute_input":"2023-02-01T21:21:14.118483Z","iopub.status.idle":"2023-02-01T21:21:14.315701Z","shell.execute_reply.started":"2023-02-01T21:21:14.118444Z","shell.execute_reply":"2023-02-01T21:21:14.31472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Among all the patients 486 patients were affected by cancer.","metadata":{}},{"cell_type":"markdown","source":"**Realtionship between age and cancer**","metadata":{}},{"cell_type":"code","source":"# Now let's identified cancer patients and relationship with age\ntemp = train_df.groupby('patient_id')[['age', 'cancer']].max()\nm = {\n    \"30-40\": [0, 0],\n    \"40-50\": [0, 0],\n    \"50-60\": [0, 0],\n    \"60-70\": [0, 0],\n    \"70-80\": [0, 0],\n    \"80-90\": [0, 0]\n}\nfor index, row in temp.iterrows():\n    age = row['age']\n    if age>=30 and age<40: \n        m['30-40'][0] += 1\n        if row['cancer']==1: m['30-40'][1] += 1\n    elif age>=40 and age<50: \n        m['40-50'][0] += 1\n        if row['cancer']==1: m['40-50'][1] += 1\n    elif age>=50 and age<60: \n        m['50-60'][0] += 1\n        if row['cancer']==1: m['50-60'][1] += 1\n    elif age>=60 and age<70: \n        m['60-70'][0] += 1\n        if row['cancer']==1: m['60-70'][1] += 1\n    elif age>=70 and age<80: \n        m['70-80'][0] += 1\n        if row['cancer']==1: m['70-80'][1] += 1\n    else: \n        m['80-90'][0] += 1\n        if row['cancer']==1: m['80-90'][1] += 1\ntemp = {}\nfor key, val in m.items(): temp[key] = val[1] / val[0]\nfig, ax = plt.subplots(figsize=(12,10))\nax = sns.barplot(x=list(temp.keys()), y=list(temp.values()))\nplt.title(\"Percentage of cancer patient of different age groups\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:14.317873Z","iopub.execute_input":"2023-02-01T21:21:14.31854Z","iopub.status.idle":"2023-02-01T21:21:15.088748Z","shell.execute_reply.started":"2023-02-01T21:21:14.318493Z","shell.execute_reply":"2023-02-01T21:21:15.08772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It is clear that, age is an important factor which influence that a patient can have cancer or not. With increasing age chances of breast cancer increases in woman.","metadata":{}},{"cell_type":"markdown","source":"# Biopsy\n","metadata":{}},{"cell_type":"markdown","source":"**Images labelled with biopsy**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(12,8))\nax = sns.countplot(x=train_df['biopsy'])\nax.bar_label(ax.containers[0])\nplt.title(\"Images with biopsy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:15.090315Z","iopub.execute_input":"2023-02-01T21:21:15.090653Z","iopub.status.idle":"2023-02-01T21:21:15.282741Z","shell.execute_reply.started":"2023-02-01T21:21:15.090618Z","shell.execute_reply":"2023-02-01T21:21:15.281726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Patients who treated with biopsy\ntemp = train_df[['patient_id', 'biopsy']]\ntemp = temp.groupby('patient_id')['biopsy'].max().to_frame()\nfig, ax = plt.subplots(figsize=(12,8))\nax = sns.countplot(x=temp['biopsy'])\nax.bar_label(ax.containers[0])\nplt.title(\"Images with biopsy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:15.284822Z","iopub.execute_input":"2023-02-01T21:21:15.285473Z","iopub.status.idle":"2023-02-01T21:21:15.475854Z","shell.execute_reply.started":"2023-02-01T21:21:15.285435Z","shell.execute_reply":"2023-02-01T21:21:15.474965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Relationship between cancer and biopsy**","metadata":{}},{"cell_type":"code","source":"temp = train_df[['cancer', 'biopsy']]\nfig, ax = plt.subplots(figsize=(12,6))\nsns.countplot(data=temp, x = 'biopsy', hue = 'cancer', ax=ax)\nax.bar_label(ax.containers[0])\nplt.title(\"Relation between cancer and biopsy\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:15.47726Z","iopub.execute_input":"2023-02-01T21:21:15.47759Z","iopub.status.idle":"2023-02-01T21:21:15.687574Z","shell.execute_reply.started":"2023-02-01T21:21:15.477563Z","shell.execute_reply":"2023-02-01T21:21:15.686872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Biopsy is never performed on some patient who don't have cancer and what is the reason for this? A biopsy is a procedure in which a small sample of tissue is removed from the body and examined under a microscope to determine if cancer cells are present. Biopsies are typically performed on patients who have abnormal findings on imaging tests, such as mammography or ultrasound, or who have symptoms that may be indicative of cancer, such as a lump or mass in the breast. The decision to perform a biopsy is made by a doctor based on the individual patient's medical history, symptoms, and imaging test results. Biopsies are an important tool in the diagnosis of cancer, and can help doctors determine the type and extent of the disease, as well as the best course of treatment.","metadata":{}},{"cell_type":"markdown","source":"**Correlation between all the variables**","metadata":{}},{"cell_type":"code","source":"temp = train_df.drop(columns=['patient_id', 'image_id', 'site_id', 'machine_id'])\nfig, ax = plt.subplots(figsize=(20, 12))\ndataplot = sns.heatmap(temp.corr(method='spearman'), cmap=\"YlGnBu\", annot=True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:15.688766Z","iopub.execute_input":"2023-02-01T21:21:15.691542Z","iopub.status.idle":"2023-02-01T21:21:16.423968Z","shell.execute_reply.started":"2023-02-01T21:21:15.691514Z","shell.execute_reply":"2023-02-01T21:21:16.422933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# pre-processing the dataset","metadata":{}},{"cell_type":"markdown","source":"**Split the data into train and test**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n#train_df= train_df.sample(1000)\ntrain_df= train_df.sample(500)\n\nX_train, X_val = train_test_split(train_df, test_size = 0.4, random_state = 42)\nlen(X_train), len(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:16.428318Z","iopub.execute_input":"2023-02-01T21:21:16.430619Z","iopub.status.idle":"2023-02-01T21:21:16.560878Z","shell.execute_reply.started":"2023-02-01T21:21:16.430583Z","shell.execute_reply":"2023-02-01T21:21:16.559845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Generation**","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom enum import Enum, auto\n\nclass Mode(Enum):\n    TRAIN = auto()\n    TEST = auto()\n    \nimg_height = 300\nimg_width = 250\nimg_shape = (img_height, img_width, 1)\n\nclass ImageDataGen(tf.keras.utils.Sequence):\n    \n    def __init__(self,\n                 df,\n                 batch_size,\n                 mode = Mode.TRAIN):\n\n        self.df = df\n        self.batch_size = batch_size\n        self.mode = mode\n        self.mode_str = train if mode == Mode.TRAIN else test\n        \n        self.len = len(df)\n        \n    def __getitem__(self, index):\n        \n        start, end = index * self.batch_size, (index + 1) * self.batch_size\n        \n        X = np.zeros((self.batch_size, ) + img_shape)\n        y = np.zeros((self.batch_size, 1))\n        \n        for i , pos in enumerate(range(start, end)):\n            if pos >= self.len: break\n                     \n            row = self.df.iloc[pos]\n            patient_id = row.patient_id\n            img_id = row.image_id\n            \n            file_name = images_dir.format(self.mode_str, patient_id, img_id)\n            \n            img = dicomsdl.open(file_name)\n            img_arr = img.pixelData()\n            \n            # standartize all scans ( Hyperparameter tuning )\n            img_arr = (img_arr - img_arr.min()) / (img_arr.max() - img_arr.min())\n            \n            if img.PhotometricInterpretation == \"MONOCHROME1\":\n                img_arr = 1 - img_arr\n\n            img_arr = np.expand_dims(img_arr, axis = -1)\n            img_arr = tf.image.resize(img_arr, img_shape[:-1], method = 'nearest').numpy()\n                 \n            X[i,...] = img_arr\n                \n            \n            if self.mode == Mode.TRAIN:\n                y[i] = row.cancer\n                \n        return (X, y) if self.mode == Mode.TRAIN else X\n                \n    \n    def __len__(self):\n        return self.len // self.batch_size + bool(self.len % self.batch_size)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:16.562917Z","iopub.execute_input":"2023-02-01T21:21:16.563561Z","iopub.status.idle":"2023-02-01T21:21:21.87114Z","shell.execute_reply.started":"2023-02-01T21:21:16.563525Z","shell.execute_reply":"2023-02-01T21:21:21.870098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = ImageDataGen(X_train, 50)\nval_gen = ImageDataGen(X_val, 50)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:21.87252Z","iopub.execute_input":"2023-02-01T21:21:21.873261Z","iopub.status.idle":"2023-02-01T21:21:21.885639Z","shell.execute_reply.started":"2023-02-01T21:21:21.873219Z","shell.execute_reply":"2023-02-01T21:21:21.884527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import tensorflow.keras as K\n\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.layers import (Conv2D, \n                                     MaxPooling2D, \n                                     BatchNormalization, \n                                     Dense, \n                                     Dropout,\n                                     GlobalMaxPooling2D)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:21.888274Z","iopub.execute_input":"2023-02-01T21:21:21.888537Z","iopub.status.idle":"2023-02-01T21:21:21.899869Z","shell.execute_reply.started":"2023-02-01T21:21:21.888512Z","shell.execute_reply":"2023-02-01T21:21:21.89901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# build a simple model\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, 5, activation = \"relu\", input_shape = img_shape))\nmodel.add(Conv2D(64, 5, activation = \"relu\"))\nmodel.add(Conv2D(64, 5, activation = \"relu\"))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(64, 5, activation = \"relu\"))\nmodel.add(Conv2D(128, 5, activation = \"relu\"))\nmodel.add(Conv2D(128, 5, activation = \"relu\"))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(128, 5, activation = \"relu\"))\nmodel.add(Conv2D(256, 5, activation = \"relu\"))\nmodel.add(Conv2D(256, 5, activation = \"relu\"))\nmodel.add(GlobalMaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(64, activation = 'relu'))\nmodel.add(Dropout(0.3))\nmodel.add(Dense(1, activation = 'sigmoid'))\n\nrecall_thresholds = [0.4, 0.5, 0.6, 0.8]\nmodel.compile(optimizer = Adam(learning_rate = 5e-5), \n              loss = 'binary_crossentropy', \n              metrics = [tf.keras.metrics.BinaryAccuracy(threshold = 0.5), \n                         tf.keras.metrics.Recall(thresholds = recall_thresholds),\n                         tf.keras.metrics.Precision(thresholds = recall_thresholds)])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:21.901152Z","iopub.execute_input":"2023-02-01T21:21:21.902189Z","iopub.status.idle":"2023-02-01T21:21:24.845475Z","shell.execute_reply.started":"2023-02-01T21:21:21.902147Z","shell.execute_reply":"2023-02-01T21:21:24.843199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# callbacks\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n\nearly_stop = EarlyStopping(patience = 5, restore_best_weights = True, verbose = 1) # val_loss\nreduce_lr = ReduceLROnPlateau(factor = 0.1, patience = 2, mode = 'min', verbose = 1) # val_loss ","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:24.846823Z","iopub.execute_input":"2023-02-01T21:21:24.847427Z","iopub.status.idle":"2023-02-01T21:21:24.854718Z","shell.execute_reply.started":"2023-02-01T21:21:24.847391Z","shell.execute_reply":"2023-02-01T21:21:24.853714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''history = model.fit(train_gen,\n                    validation_data = val_gen,\n                    epochs = 3,\n                    verbose = 1,\n                   workers = 8,\n                   callbacks = [reduce_lr])'''","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:24.856348Z","iopub.execute_input":"2023-02-01T21:21:24.856851Z","iopub.status.idle":"2023-02-01T21:21:24.868087Z","shell.execute_reply.started":"2023-02-01T21:21:24.856811Z","shell.execute_reply":"2023-02-01T21:21:24.867091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''history = model.fit(train_gen,\n                    validation_data = val_gen,\n                    epochs = 3,\n                    verbose = 1,\n                    workers = 8,\n                  callbacks = [reduce_lr])'''","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:24.873836Z","iopub.execute_input":"2023-02-01T21:21:24.8742Z","iopub.status.idle":"2023-02-01T21:21:24.881036Z","shell.execute_reply.started":"2023-02-01T21:21:24.874174Z","shell.execute_reply":"2023-02-01T21:21:24.879785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plotter_lib\n\nimport numpy as np\n\nimport PIL as image_lib\n\nimport tensorflow as tf\n\nfrom tensorflow.keras.layers import Dense,Flatten\n\nfrom tensorflow.keras.models import Sequential\n\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:24.882467Z","iopub.execute_input":"2023-02-01T21:21:24.883459Z","iopub.status.idle":"2023-02-01T21:21:24.890745Z","shell.execute_reply.started":"2023-02-01T21:21:24.883433Z","shell.execute_reply":"2023-02-01T21:21:24.889052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"demo_resnet_model = Sequential()\n\npretrained_model_for_demo= tf.keras.applications.ResNet50(include_top=False,\n\n                   input_shape=(180,180,3),\n\n                   pooling='avg',classes=5,\n\n                   weights='imagenet')\n\nfor each_layer in pretrained_model_for_demo.layers:\n\n        each_layer.trainable=False\n\ndemo_resnet_model.add(pretrained_model_for_demo)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:24.892259Z","iopub.execute_input":"2023-02-01T21:21:24.892732Z","iopub.status.idle":"2023-02-01T21:21:31.227861Z","shell.execute_reply.started":"2023-02-01T21:21:24.892696Z","shell.execute_reply":"2023-02-01T21:21:31.226907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"demo_resnet_model.add(Flatten())\n\ndemo_resnet_model.add(Dense(512, activation='relu'))\n\ndemo_resnet_model.add(Dense(5, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:31.229371Z","iopub.execute_input":"2023-02-01T21:21:31.229705Z","iopub.status.idle":"2023-02-01T21:21:31.253935Z","shell.execute_reply.started":"2023-02-01T21:21:31.229669Z","shell.execute_reply":"2023-02-01T21:21:31.253067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"demo_resnet_model.compile(optimizer=Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\n\nhistory = demo_resnet_model.fit(train_gen, validation_data=val_gen, epochs=3)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T21:21:31.255128Z","iopub.execute_input":"2023-02-01T21:21:31.255446Z","iopub.status.idle":"2023-02-01T21:23:00.796348Z","shell.execute_reply.started":"2023-02-01T21:21:31.255413Z","shell.execute_reply":"2023-02-01T21:23:00.332643Z"},"trusted":true},"execution_count":null,"outputs":[]}]}