{"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":"%matplotlib inline\nimport numpy as np \nimport pandas as pd\nimport os\nfrom glob import glob\nimport matplotlib.pyplot as plt\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import Xception\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\nimport tensorflow as tf\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom keras.models import Model","metadata":{"execution":{"iopub.status.busy":"2023-02-01T16:46:23.977673Z","iopub.execute_input":"2023-02-01T16:46:23.978254Z","iopub.status.idle":"2023-02-01T16:46:30.667498Z","shell.execute_reply.started":"2023-02-01T16:46:23.97814Z","shell.execute_reply":"2023-02-01T16:46:30.666386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -qU python-gdcm pydicom pylibjpeg","metadata":{"execution":{"iopub.status.busy":"2023-02-01T16:46:30.669656Z","iopub.execute_input":"2023-02-01T16:46:30.670356Z","iopub.status.idle":"2023-02-01T16:46:43.50096Z","shell.execute_reply.started":"2023-02-01T16:46:30.670316Z","shell.execute_reply":"2023-02-01T16:46:43.499661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport glob\nimport cv2\nimport seaborn as sns\n\n# To work with DICOM images\nimport gdcm\nimport pydicom\n\nfrom tqdm.notebook import tqdm\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2023-02-01T16:46:43.503348Z","iopub.execute_input":"2023-02-01T16:46:43.504163Z","iopub.status.idle":"2023-02-01T16:46:44.419689Z","shell.execute_reply.started":"2023-02-01T16:46:43.504118Z","shell.execute_reply":"2023-02-01T16:46:44.418633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_img_to_hu(dcm_ds):\n    \"\"\"\n    Rescales the image to Hounsfield unit.\n    Thank you https://www.kaggle.com/code/allunia/rsna-csf-cervical-spine-fracture-eda/notebook\n    \"\"\"\n    data = dcm_ds.pixel_array\n    if dcm_ds.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    return data * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept\n\ndef show_images_for_patient(patient_id):\n    \"\"\"\n    Thank you\n    https://www.kaggle.com/code/radek1/eda-training-a-fast-ai-model-submission\n    although with some changes.\n    \"\"\"\n    patient_dir = os.path.join('../input/rsna-breast-cancer-detection/train_images', str(patient_id))\n    print(patient_dir)\n    num_images = len([name for name in os.listdir(patient_dir)])\n    print(f\"Number of images for patient: {num_images}\")\n    fig, axs = plt.subplots(2, 2, figsize=(24,15))\n    axs = axs.flatten()\n    for i, img_file in enumerate(os.listdir(patient_dir)):\n        img_path = os.path.join(patient_dir, img_file)\n        ds = pydicom.dcmread(img_path)\n        axs[i].imshow(rescale_img_to_hu(ds), cmap=\"bone\")\n        # Break if there are more than 4 images for visualization purposes\n        if i==3: break","metadata":{"execution":{"iopub.status.busy":"2023-02-01T16:59:35.661923Z","iopub.execute_input":"2023-02-01T16:59:35.662309Z","iopub.status.idle":"2023-02-01T16:59:35.671737Z","shell.execute_reply.started":"2023-02-01T16:59:35.662271Z","shell.execute_reply":"2023-02-01T16:59:35.670505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_pd = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntrain_pd.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T16:59:35.927265Z","iopub.execute_input":"2023-02-01T16:59:35.927973Z","iopub.status.idle":"2023-02-01T16:59:36.00503Z","shell.execute_reply.started":"2023-02-01T16:59:35.927935Z","shell.execute_reply":"2023-02-01T16:59:36.003971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add path column for further data generator\ntrain_pd['path'] = '/kaggle/input/rsna-breast-cancer-256-pngs/' + train_pd['patient_id'].astype(str) + '_' + train_pd['image_id'].astype(str) + '.png'\n\n# Convert target to string\ntrain_pd['cancer'] = train_pd['cancer'].astype(str)\n\n# Convert target to onehot\nfrom sklearn.preprocessing import OneHotEncoder\nohe = OneHotEncoder()\ntrain_pd['cancer_one_hot'] = ohe.fit_transform(train_pd[['cancer']]).toarray().tolist()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:26.351262Z","iopub.execute_input":"2023-02-01T17:01:26.351638Z","iopub.status.idle":"2023-02-01T17:01:26.471247Z","shell.execute_reply.started":"2023-02-01T17:01:26.351606Z","shell.execute_reply":"2023-02-01T17:01:26.470215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Imbalanced target\ntrain_pd['cancer'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:28.12458Z","iopub.execute_input":"2023-02-01T17:01:28.124938Z","iopub.status.idle":"2023-02-01T17:01:28.136653Z","shell.execute_reply.started":"2023-02-01T17:01:28.124906Z","shell.execute_reply":"2023-02-01T17:01:28.135628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Relationship cancer with implant\n# train_pd['implant'].value_counts()\ntrain_pd.groupby('implant')['cancer'].mean()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:28.354609Z","iopub.execute_input":"2023-02-01T17:01:28.356413Z","iopub.status.idle":"2023-02-01T17:01:28.433867Z","shell.execute_reply.started":"2023-02-01T17:01:28.35636Z","shell.execute_reply":"2023-02-01T17:01:28.432658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Target wrt difficult case --> More than 10% were difficult to diagnose negative\ntrain_pd[['cancer', 'difficult_negative_case']].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:28.686675Z","iopub.execute_input":"2023-02-01T17:01:28.687057Z","iopub.status.idle":"2023-02-01T17:01:28.709195Z","shell.execute_reply.started":"2023-02-01T17:01:28.687024Z","shell.execute_reply":"2023-02-01T17:01:28.708129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show me one image with cancer\npatient_id_with_cancer = train_pd[train_pd.cancer=='1']['patient_id'].iloc[0]\nshow_images_for_patient(10011)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:29.063042Z","iopub.execute_input":"2023-02-01T17:01:29.06349Z","iopub.status.idle":"2023-02-01T17:01:34.260673Z","shell.execute_reply.started":"2023-02-01T17:01:29.063454Z","shell.execute_reply":"2023-02-01T17:01:34.256351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Should we care about implants?\npatient_id_with_implants = train_pd[train_pd.implant==1]['patient_id'].iloc[0]\nshow_images_for_patient(patient_id_with_implants)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:34.26256Z","iopub.execute_input":"2023-02-01T17:01:34.263597Z","iopub.status.idle":"2023-02-01T17:01:40.177629Z","shell.execute_reply.started":"2023-02-01T17:01:34.263551Z","shell.execute_reply":"2023-02-01T17:01:40.176441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NEW MODEL _BASELINE_","metadata":{}},{"cell_type":"code","source":"IMAGE_SHAPE = (224,224)\nnasnet_url = \"https://tfhub.dev/google/imagenet/nasnet_mobile/feature_vector/5\"\nmobile_net_url = \"https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4\"\nimport tensorflow\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers\ndef create_model(model_url_1,model_url_2,num_classes=2):\n    model_feature_extractor_1 = hub.KerasLayer(model_url_1,\n                                              trainable = False,\n                                              name=\"feature_extractor_1\",\n                                              input_shape = IMAGE_SHAPE+(3,)) \n    model_feature_extractor_2 = hub.KerasLayer(model_url_2,\n                                              trainable = False,\n                                              name=\"feature_extractor_2\",\n                                              input_shape = IMAGE_SHAPE+(3,)) \n    inputs = tf.keras.layers.Input(shape = (224,224,3))\n    x1 = model_feature_extractor_1(inputs)\n    print(x1.shape)\n    x2 = model_feature_extractor_2(inputs)\n    print(x2.shape)\n    x1 = tf.keras.layers.Dense(1,activation=\"sigmoid\")(x1)\n    x2 = tf.keras.layers.Dense(1,activation=\"sigmoid\")(x2)\n    print(x2.shape)\n    print(x1.shape)\n    x = tf.keras.layers.Average()([x1,x2])\n    outputs = tf.keras.layers.Dense(1,activation=\"sigmoid\")(x)\n    functional_model_breast = tf.keras.Model(inputs,outputs,name=\"cancer_model_baseline\")\n    \n    return functional_model_breast","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:40.179473Z","iopub.execute_input":"2023-02-01T17:01:40.184348Z","iopub.status.idle":"2023-02-01T17:01:40.197765Z","shell.execute_reply.started":"2023-02-01T17:01:40.184298Z","shell.execute_reply":"2023-02-01T17:01:40.196563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_baseline = create_model(mobile_net_url,nasnet_url)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:40.200346Z","iopub.execute_input":"2023-02-01T17:01:40.201219Z","iopub.status.idle":"2023-02-01T17:01:49.05606Z","shell.execute_reply.started":"2023-02-01T17:01:40.201178Z","shell.execute_reply":"2023-02-01T17:01:49.054976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_baseline.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:01:49.057927Z","iopub.execute_input":"2023-02-01T17:01:49.058627Z","iopub.status.idle":"2023-02-01T17:01:49.093875Z","shell.execute_reply.started":"2023-02-01T17:01:49.058579Z","shell.execute_reply":"2023-02-01T17:01:49.09276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pfbeta_tf(labels, preds, beta=1):\n    eps = 1e-5\n    preds = tf.clip_by_value(preds, 0, 1)\n    y_true_count = tf.reduce_sum(labels)\n    ctp = tf.reduce_sum(preds[labels==1])\n    cfp = tf.reduce_sum(preds[labels==0])\n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp + eps)\n    c_recall = ctp / (y_true_count + eps)\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall + eps)\n        return result\n    else:\n        return tf.constant(0, dtype=tf.float32)\npfbeta_tf.__name__='pF1'\n\n\n# finds best pf1 using thresholds\ndef pfbeta_thr(labels, preds):\n    thrs = tf.range(0, 1, 0.05)\n    best_score = tf.constant(0, dtype=tf.float32)\n    for thr in thrs:\n        score = pfbeta_tf(labels, tf.cast(preds>thr, tf.float32))\n        best_score = tf.cond(score > best_score, lambda: score, lambda: best_score)\n    return best_score\n\npfbeta_thr.__name__='pF1_thr'\n\n\n# numpy\ndef pfbeta(labels, preds, beta=1):\n    eps = 1e-5\n    preds = preds.clip(0, 1)\n    y_true_count = labels.sum()\n    ctp = preds[labels==1].sum()\n    cfp = preds[labels==0].sum()\n    beta_squared = beta * beta\n    c_precision = ctp / (ctp + cfp + eps)\n    c_recall = ctp / (y_true_count + eps)\n    if (c_precision > 0 and c_recall > 0):\n        result = (1 + beta_squared) * (c_precision * c_recall) / (beta_squared * c_precision + c_recall + eps)\n        return result\n    else:\n        return 0.0\nauc = tf.keras.metrics.AUC(name='auc')\naccuracy = tf.keras.metrics.BinaryAccuracy()\npf1 = pfbeta_tf\npf1_thr = pfbeta_thr\nMetrics = [pf1, pf1_thr, auc,accuracy]","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:40:33.342055Z","iopub.execute_input":"2023-02-01T17:40:33.343319Z","iopub.status.idle":"2023-02-01T17:40:33.370118Z","shell.execute_reply.started":"2023-02-01T17:40:33.343273Z","shell.execute_reply":"2023-02-01T17:40:33.368722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_baseline.compile(\n        loss=tf.keras.losses.BinaryCrossentropy(),\n        optimizer=tf.keras.optimizers.Adam(),\n        metrics=Metrics\n    )","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:40:34.096046Z","iopub.execute_input":"2023-02-01T17:40:34.096817Z","iopub.status.idle":"2023-02-01T17:40:34.113531Z","shell.execute_reply.started":"2023-02-01T17:40:34.096778Z","shell.execute_reply":"2023-02-01T17:40:34.112277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# NEW MODEL _BASELINE END_","metadata":{}},{"cell_type":"code","source":"#Knee code model \nxception = Xception(weights=\"imagenet\")#,input_shape=(256, 256, 3),\n   # include_top=False)\nx=  xception.layers[-3].output\n\nx = tf.keras.layers.Conv2D(filters= 1024, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 256, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 64, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 5, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nGAP = tf.keras.layers.GlobalAveragePooling2D()(x)\npred = tf.keras.activations.softmax(GAP)\n\nxception_model = Model(inputs=xception.input,outputs=pred)","metadata":{"execution":{"iopub.status.busy":"2023-01-23T18:43:10.866467Z","iopub.execute_input":"2023-01-23T18:43:10.866967Z","iopub.status.idle":"2023-01-23T18:43:17.268492Z","shell.execute_reply.started":"2023-01-23T18:43:10.866928Z","shell.execute_reply":"2023-01-23T18:43:17.26752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.Xception(\n    weights='imagenet',  # Load weights pre-trained on ImageNet.\n    input_shape=(256, 256, 3),\n    include_top=False\n)\n# Freeze the layers of the base model\nbase_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-01-23T19:22:23.201194Z","iopub.execute_input":"2023-01-23T19:22:23.201835Z","iopub.status.idle":"2023-01-23T19:22:29.936625Z","shell.execute_reply.started":"2023-01-23T19:22:23.201795Z","shell.execute_reply":"2023-01-23T19:22:29.935627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Input layer\ninputs = tf.keras.Input(shape=(256, 256, 3))\n\n# Base model layer\nx = base_model(inputs, training=False)\n\n#x=  base_model.layers[-3].output\n\nx = tf.keras.layers.Conv2D(filters= 1024, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 256, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 64, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n\nx = tf.keras.layers.Conv2D(filters= 5, kernel_size= 3, padding= \"same\")(x)\nx = tf.keras.layers.BatchNormalization()(x)\nx = tf.keras.layers.Activation(\"relu\")(x)\n# Pooling to reduce the number of dimensions\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\n\n# Dense layer to learn new stuff\nx = tf.keras.layers.Dense(16, activation='relu')(x)\n\n# Output layer for loss\noutputs = tf.keras.layers.Dense(2, activation='softmax')(x)\n\n# All togeteher now... all togeeeeether\nmodel = tf.keras.Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2023-01-23T19:22:29.938259Z","iopub.execute_input":"2023-01-23T19:22:29.938665Z","iopub.status.idle":"2023-01-23T19:22:30.349935Z","shell.execute_reply.started":"2023-01-23T19:22:29.938614Z","shell.execute_reply":"2023-01-23T19:22:30.348968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-23T19:07:08.774949Z","iopub.execute_input":"2023-01-23T19:07:08.775335Z","iopub.status.idle":"2023-01-23T19:07:08.793706Z","shell.execute_reply.started":"2023-01-23T19:07:08.775303Z","shell.execute_reply":"2023-01-23T19:07:08.792543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#knee code compile\nxception_model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate=0.00001,decay=0.0001),\n                 metrics=[\"acc\"],\n                 loss= tf.keras.losses.sparse_categorical_crossentropy)","metadata":{"execution":{"iopub.status.busy":"2023-01-23T18:43:17.269684Z","iopub.execute_input":"2023-01-23T18:43:17.269988Z","iopub.status.idle":"2023-01-23T18:43:17.288545Z","shell.execute_reply.started":"2023-01-23T18:43:17.269961Z","shell.execute_reply":"2023-01-23T18:43:17.28732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=tf.keras.optimizers.Adam(),\n    loss='categorical_crossentropy',\n    metrics=[tf.keras.metrics.BinaryAccuracy()]\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-23T19:22:30.35145Z","iopub.execute_input":"2023-01-23T19:22:30.351832Z","iopub.status.idle":"2023-01-23T19:22:30.370956Z","shell.execute_reply.started":"2023-01-23T19:22:30.351795Z","shell.execute_reply":"2023-01-23T19:22:30.370078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DATA GENERATOR _NEW","metadata":{}},{"cell_type":"code","source":"# Create data generator\nfrom keras.preprocessing.image import ImageDataGenerator\n\ndatagen = ImageDataGenerator(\n    horizontal_flip=True,\n    vertical_flip=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:02:02.563697Z","iopub.execute_input":"2023-02-01T17:02:02.56406Z","iopub.status.idle":"2023-02-01T17:02:02.569537Z","shell.execute_reply.started":"2023-02-01T17:02:02.564027Z","shell.execute_reply":"2023-02-01T17:02:02.568237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ndf_train, df_val = train_test_split(train_pd, test_size=0.3, stratify=train_pd['cancer'])","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:59:44.006338Z","iopub.execute_input":"2023-02-01T17:59:44.007177Z","iopub.status.idle":"2023-02-01T17:59:44.092402Z","shell.execute_reply.started":"2023-02-01T17:59:44.007134Z","shell.execute_reply":"2023-02-01T17:59:44.091363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create training flow\ntrain_flow = datagen.flow_from_dataframe(\n    df_train,\n    x_col='path',\n    y_col='cancer',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='binary',\n    batch_size=32,\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:59:47.027145Z","iopub.execute_input":"2023-02-01T17:59:47.027712Z","iopub.status.idle":"2023-02-01T18:00:45.68075Z","shell.execute_reply.started":"2023-02-01T17:59:47.027676Z","shell.execute_reply":"2023-02-01T18:00:45.678817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create validation flow\nval_flow = datagen.flow_from_dataframe(\n    df_val,\n    x_col='path',\n    y_col='cancer',\n    target_size=(224, 224),\n    color_mode='rgb',\n    class_mode='binary',\n    batch_size=32,\n    shuffle=True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T18:00:45.683796Z","iopub.execute_input":"2023-02-01T18:00:45.684897Z","iopub.status.idle":"2023-02-01T18:01:10.239909Z","shell.execute_reply.started":"2023-02-01T18:00:45.684853Z","shell.execute_reply":"2023-02-01T18:01:10.23879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['cancer_one_hot'][0]","metadata":{"execution":{"iopub.status.busy":"2023-02-01T18:01:10.241571Z","iopub.execute_input":"2023-02-01T18:01:10.242315Z","iopub.status.idle":"2023-02-01T18:01:10.252017Z","shell.execute_reply.started":"2023-02-01T18:01:10.242275Z","shell.execute_reply":"2023-02-01T18:01:10.250972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import class_weight\nclass_weights = class_weight.compute_class_weight('balanced',\n                                                 classes= (0.0,1.0),\n                                                 y= df_train['cancer_one_hot'][0])\nclass_weights = dict(enumerate(class_weights))","metadata":{"execution":{"iopub.status.busy":"2023-02-01T18:01:19.898953Z","iopub.execute_input":"2023-02-01T18:01:19.899719Z","iopub.status.idle":"2023-02-01T18:01:19.906567Z","shell.execute_reply.started":"2023-02-01T18:01:19.899682Z","shell.execute_reply":"2023-02-01T18:01:19.905312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(\n    x=train_flow,\n    epochs=3,\n    validation_data=val_flow,\n   class_weight={0: 1, 1:10}    # Since the dataset is very imbalanced I gave some weights to hopefully help the NN a bit\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-23T19:25:15.603765Z","iopub.execute_input":"2023-01-23T19:25:15.604229Z","iopub.status.idle":"2023-01-23T19:46:00.972749Z","shell.execute_reply.started":"2023-01-23T19:25:15.60419Z","shell.execute_reply":"2023-01-23T19:46:00.971586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cancer_baseline.fit(\n    x= train_flow,\n    epochs = 1,\n    validation_data=val_flow,\n    class_weight={0: 1, 1:10},\n    use_multiprocessing= True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T17:15:48.954165Z","iopub.execute_input":"2023-02-01T17:15:48.954553Z","iopub.status.idle":"2023-02-01T17:25:11.084211Z","shell.execute_reply.started":"2023-02-01T17:15:48.954521Z","shell.execute_reply":"2023-02-01T17:25:11.08292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.device(\"/gpu:0\"):\n    cancer_baseline.fit(\n    x= train_flow,\n    epochs = 1,\n    validation_data=val_flow,\n    class_weight={0: 1, 1:10},\n    use_multiprocessing= True\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T18:01:30.951748Z","iopub.execute_input":"2023-02-01T18:01:30.952145Z","iopub.status.idle":"2023-02-01T18:01:52.781Z","shell.execute_reply.started":"2023-02-01T18:01:30.95211Z","shell.execute_reply":"2023-02-01T18:01:52.77704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}