{"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":"markdown","source":"# **Histopathologic Cancer Detection**","metadata":{}},{"cell_type":"code","source":"import os\nimport math\nimport pickle\nfrom tqdm import trange\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\n\nfrom datasets import load_dataset, Dataset\nfrom datasets import Image as ds_Image\nfrom collections import OrderedDict\nfrom PIL import Image\nimport torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-10T10:09:56.125818Z","iopub.execute_input":"2022-11-10T10:09:56.126166Z","iopub.status.idle":"2022-11-10T10:09:58.012258Z","shell.execute_reply.started":"2022-11-10T10:09:56.126104Z","shell.execute_reply":"2022-11-10T10:09:58.010083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.014046Z","iopub.execute_input":"2022-11-10T10:09:58.015007Z","iopub.status.idle":"2022-11-10T10:09:58.466906Z","shell.execute_reply.started":"2022-11-10T10:09:58.014970Z","shell.execute_reply":"2022-11-10T10:09:58.465787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.468193Z","iopub.execute_input":"2022-11-10T10:09:58.468893Z","iopub.status.idle":"2022-11-10T10:09:58.483727Z","shell.execute_reply.started":"2022-11-10T10:09:58.468854Z","shell.execute_reply":"2022-11-10T10:09:58.482503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = plt.subplots()\ny = df['label'].value_counts()\nlabels = ['No Cancer', 'Cancer']\n\nplt.xticks(range(len(y)), labels, fontsize=20)\nplt.xlabel('Class', fontsize=20)\nplt.ylabel('Amounts', fontsize=20)\nplt.title('Porportion of the data set', fontsize=15)\nplt.bar(range(len(y)), y)\n\nplt.rcParams['figure.figsize'] = [10, 10]\nplt.rcParams['font.size'] = 9.0\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.487916Z","iopub.execute_input":"2022-11-10T10:09:58.488369Z","iopub.status.idle":"2022-11-10T10:09:58.692884Z","shell.execute_reply.started":"2022-11-10T10:09:58.488342Z","shell.execute_reply":"2022-11-10T10:09:58.691902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.pie(y, labels=labels)\nplt.legend(fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.694256Z","iopub.execute_input":"2022-11-10T10:09:58.695087Z","iopub.status.idle":"2022-11-10T10:09:58.881514Z","shell.execute_reply.started":"2022-11-10T10:09:58.695050Z","shell.execute_reply":"2022-11-10T10:09:58.880265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Data Pre-processing","metadata":{}},{"cell_type":"markdown","source":"### 2.1. Create Image Path from `image_id`","metadata":{}},{"cell_type":"code","source":"df['image_path']=df['id'].apply(\n    lambda row : '../input/histopathologic-cancer-detection/train/'+ row + '.tif')\ndf = df.sample(10_000)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.883167Z","iopub.execute_input":"2022-11-10T10:09:58.884273Z","iopub.status.idle":"2022-11-10T10:09:58.978736Z","shell.execute_reply.started":"2022-11-10T10:09:58.884224Z","shell.execute_reply":"2022-11-10T10:09:58.977689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image']=df['image_path']\ndf=df.drop(columns=['id'])\ndf = df.rename(columns={'label': 'labels'})","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.981042Z","iopub.execute_input":"2022-11-10T10:09:58.981721Z","iopub.status.idle":"2022-11-10T10:09:58.992882Z","shell.execute_reply.started":"2022-11-10T10:09:58.981685Z","shell.execute_reply":"2022-11-10T10:09:58.991773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# os.listdir('../input/histopathologic-cancer-detection/train')[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:58.994375Z","iopub.execute_input":"2022-11-10T10:09:58.995508Z","iopub.status.idle":"2022-11-10T10:09:59.002981Z","shell.execute_reply.started":"2022-11-10T10:09:58.995449Z","shell.execute_reply":"2022-11-10T10:09:59.001989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.2. Create Image Object from `image` ( `image` contains the image path )","metadata":{}},{"cell_type":"code","source":"dataset = Dataset.from_pandas(df).cast_column(\"image\", ds_Image())\ndataset","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:59.004580Z","iopub.execute_input":"2022-11-10T10:09:59.004903Z","iopub.status.idle":"2022-11-10T10:09:59.039031Z","shell.execute_reply.started":"2022-11-10T10:09:59.004863Z","shell.execute_reply":"2022-11-10T10:09:59.038156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.3. Save a list of `labels`","metadata":{}},{"cell_type":"code","source":"labels = df['labels'].unique().tolist()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:59.040361Z","iopub.execute_input":"2022-11-10T10:09:59.040689Z","iopub.status.idle":"2022-11-10T10:09:59.046905Z","shell.execute_reply.started":"2022-11-10T10:09:59.040655Z","shell.execute_reply":"2022-11-10T10:09:59.045751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.4. Resize Image\nResize image to fit to the input size of the model","metadata":{}},{"cell_type":"code","source":"IMAGE_WIDTH, IMAGE_HEIGHT = 224, 224\n\ndef resize(examples):\n    examples[\"image\"] = [image.convert(\"RGB\").resize((IMAGE_WIDTH,IMAGE_HEIGHT)) \n                         for image in examples[\"image\"]]\n    return examples\n\ndataset = dataset.map(resize, batched=True, batch_size=8)\ndataset","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:09:59.048673Z","iopub.execute_input":"2022-11-10T10:09:59.049097Z","iopub.status.idle":"2022-11-10T10:10:18.178370Z","shell.execute_reply.started":"2022-11-10T10:09:59.049063Z","shell.execute_reply":"2022-11-10T10:10:18.177423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.5. Split data set to train, validation and test set","metadata":{}},{"cell_type":"code","source":"train_ds, eval_ds = train_test_split(dataset, test_size=0.2)\ntest_ds, eval_ds = train_test_split(Dataset.from_dict(eval_ds), test_size=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:18.179772Z","iopub.execute_input":"2022-11-10T10:10:18.180429Z","iopub.status.idle":"2022-11-10T10:10:20.737563Z","shell.execute_reply.started":"2022-11-10T10:10:18.180393Z","shell.execute_reply":"2022-11-10T10:10:20.736538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datasets import Dataset, DatasetDict\nds = DatasetDict({'train': Dataset.from_dict(train_ds), \n                 'eval': Dataset.from_dict(eval_ds),\n                 'test': Dataset.from_dict(test_ds)})\nds","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:20.742704Z","iopub.execute_input":"2022-11-10T10:10:20.743004Z","iopub.status.idle":"2022-11-10T10:10:27.627990Z","shell.execute_reply.started":"2022-11-10T10:10:20.742977Z","shell.execute_reply":"2022-11-10T10:10:27.627104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/working/dataset_224x224.pkl', 'wb') as file:\n#      pickle.dump(ds, file)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:27.629319Z","iopub.execute_input":"2022-11-10T10:10:27.629770Z","iopub.status.idle":"2022-11-10T10:10:27.635269Z","shell.execute_reply.started":"2022-11-10T10:10:27.629732Z","shell.execute_reply":"2022-11-10T10:10:27.633281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=ds['train']['image']\nimage_label=ds['train']['labels']","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:27.636633Z","iopub.execute_input":"2022-11-10T10:10:27.637791Z","iopub.status.idle":"2022-11-10T10:10:28.451955Z","shell.execute_reply.started":"2022-11-10T10:10:27.637754Z","shell.execute_reply":"2022-11-10T10:10:28.450942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### 2.6. Display Some Data Points","metadata":{}},{"cell_type":"code","source":"import cv2\nrows=6\ncols=6\nlabel_converter = {0: 'No Cancer', 1: 'Cancer'}\nfig, axs = plt.subplots(rows, cols, figsize=(14, 14))\naxs = axs.flatten()\nfor (img, img_label), ax in zip(zip(image, image_label), axs):\n    ax.imshow(img)\n    ax.set_title(label_converter[img_label], fontsize=20)\n    fig.tight_layout(pad=2.0)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:28.453639Z","iopub.execute_input":"2022-11-10T10:10:28.454047Z","iopub.status.idle":"2022-11-10T10:10:43.096940Z","shell.execute_reply.started":"2022-11-10T10:10:28.454009Z","shell.execute_reply":"2022-11-10T10:10:43.095690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Models Training and Inference\n## 3.1. ViTransformer","metadata":{}},{"cell_type":"markdown","source":"### Loading ViT Feature Extractor","metadata":{}},{"cell_type":"code","source":"from transformers import ViTFeatureExtractor\n\nmodel_name_or_path = 'google/vit-base-patch16-224-in21k'\nfeature_extractor = ViTFeatureExtractor(do_resize=False).from_pretrained(model_name_or_path)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:43.098281Z","iopub.execute_input":"2022-11-10T10:10:43.098607Z","iopub.status.idle":"2022-11-10T10:10:44.414448Z","shell.execute_reply.started":"2022-11-10T10:10:43.098578Z","shell.execute_reply":"2022-11-10T10:10:44.413475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"To process an image, simply pass it to the feature extractor's call function. This will return a dict containing pixel values, which is the numeric representation to be passed to the model.\n<br>We got a NumPy array by default, but if we add the return_tensors='pt' argument, we'll get back torch tensors instead.</br>","metadata":{}},{"cell_type":"markdown","source":"### Processing the data set\nWe know how to read images and transform them into inputs, now we have to write a function that will put those two things together to process a single example from the dataset.","metadata":{}},{"cell_type":"code","source":"def process_example(example):\n    inputs = feature_extractor(example['image'], return_tensors='pt')\n    inputs['labels'] = example['labels']\n    return inputs\n\ndef transform(example_batch):\n    # Take a list of PIL images and turn them to pixel values\n    inputs = feature_extractor([x for x in example_batch['image']], return_tensors='pt')\n\n    # Don't forget to include the labels!\n    inputs['labels'] = example_batch['labels']\n    return inputs","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:44.416092Z","iopub.execute_input":"2022-11-10T10:10:44.416771Z","iopub.status.idle":"2022-11-10T10:10:44.423966Z","shell.execute_reply.started":"2022-11-10T10:10:44.416732Z","shell.execute_reply":"2022-11-10T10:10:44.423049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prepared_ds = ds.with_transform(transform)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:44.425300Z","iopub.execute_input":"2022-11-10T10:10:44.426227Z","iopub.status.idle":"2022-11-10T10:10:44.440299Z","shell.execute_reply.started":"2022-11-10T10:10:44.426169Z","shell.execute_reply":"2022-11-10T10:10:44.439025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('/kaggle/working/dataset_224x224.pkl', 'wb') as file:\n#     pickle.dump(prepared_ds, file)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:44.441976Z","iopub.execute_input":"2022-11-10T10:10:44.442427Z","iopub.status.idle":"2022-11-10T10:10:44.446870Z","shell.execute_reply.started":"2022-11-10T10:10:44.442391Z","shell.execute_reply":"2022-11-10T10:10:44.445936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Training and Evaluation\n\nThe data is processed and we are ready to start setting up the training pipeline. We are going to use `Trainer`, that'll require \n\n- Define a collate function.\n\n- Define an evaluation metric. During training, the model should be evaluated on its prediction accuracy. We should define a `compute_metrics` function accordingly.\n\n- Load a pretrained checkpoint. We need to load a pretrained checkpoint and configure it correctly for training.\n\n- Define the training configuration.\n\nAfter fine-tuning the model, we will correctly evaluate it on the evaluation data and verify that it has indeed learned to correctly classify the images.","metadata":{}},{"cell_type":"markdown","source":"### Define our data collator\nBatches are coming in as lists of dicts, so we can just unpack + stack those into batch tensors.\n<br>Since the `collate_fn` will return a batch dict, we can unpack the inputs to the model later</br>","metadata":{}},{"cell_type":"code","source":"import torch\n\ndef collate_fn(batch):\n    return {\n        'pixel_values': torch.stack([x['pixel_values'] for x in batch]),\n        'labels': torch.tensor([x['labels'] for x in batch])\n    }","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:44.448220Z","iopub.execute_input":"2022-11-10T10:10:44.449222Z","iopub.status.idle":"2022-11-10T10:10:44.457381Z","shell.execute_reply.started":"2022-11-10T10:10:44.449171Z","shell.execute_reply":"2022-11-10T10:10:44.456366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Define an evaluation metric\n\nThe accuracy metric from datasets can easily be used to compare the predictions with the labels. \n<br>Below, we can see how to use it within a `compute_metrics` function that will be used by the `Trainer`.</br>","metadata":{}},{"cell_type":"code","source":"import numpy as np\nfrom datasets import load_metric\n\nmetric = load_metric(\"accuracy\")\ndef compute_metrics(p):\n    return metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:44.458950Z","iopub.execute_input":"2022-11-10T10:10:44.459543Z","iopub.status.idle":"2022-11-10T10:10:45.410761Z","shell.execute_reply.started":"2022-11-10T10:10:44.459509Z","shell.execute_reply":"2022-11-10T10:10:45.409692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load model\nNext we'll add the pretrained model. We'll add `num_labels` on init so the model creates a classification head (last layer of the neuron network) with the right number of units. ","metadata":{}},{"cell_type":"code","source":"from transformers import ViTForImageClassification\n\nmodel = ViTForImageClassification.from_pretrained(\n    model_name_or_path,\n    num_labels=len(labels),\n    id2label={'0': 'No Cancer', '1': 'Cancer'},\n    label2id={c: str(i) for i, c in enumerate(labels)}\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:45.412387Z","iopub.execute_input":"2022-11-10T10:10:45.412759Z","iopub.status.idle":"2022-11-10T10:10:55.145839Z","shell.execute_reply.started":"2022-11-10T10:10:45.412722Z","shell.execute_reply":"2022-11-10T10:10:55.144895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Setting up the training configuration by defining `TrainingArguments`.","metadata":{}},{"cell_type":"code","source":"from transformers import TrainingArguments\n\ntraining_args = TrainingArguments(\n  output_dir=\"./fine_tuned_ViT\",\n  per_device_train_batch_size=16,\n  evaluation_strategy=\"steps\",\n  num_train_epochs=30,\n  #fp16=False,\n  fp16=True,\n  save_steps=100,\n  eval_steps=100,\n  logging_steps=10,\n  learning_rate=2e-4,\n  save_total_limit=2,\n  remove_unused_columns=False,\n  push_to_hub=False,\n  report_to='tensorboard',\n  load_best_model_at_end=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:55.147916Z","iopub.execute_input":"2022-11-10T10:10:55.148315Z","iopub.status.idle":"2022-11-10T10:10:59.183099Z","shell.execute_reply.started":"2022-11-10T10:10:55.148272Z","shell.execute_reply":"2022-11-10T10:10:59.182025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Set up `Trainer`\nNow, all instances can be passed to `Trainer` and we are ready to start training!","metadata":{}},{"cell_type":"code","source":"from transformers import EarlyStoppingCallback\nfrom transformers import Trainer\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    data_collator=collate_fn,\n    compute_metrics=compute_metrics,\n    train_dataset=prepared_ds['train'],\n    eval_dataset=prepared_ds['eval'],\n    tokenizer=feature_extractor,\n    callbacks = [EarlyStoppingCallback(early_stopping_patience=5)]\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:10:59.185357Z","iopub.execute_input":"2022-11-10T10:10:59.185955Z","iopub.status.idle":"2022-11-10T10:11:05.145404Z","shell.execute_reply.started":"2022-11-10T10:10:59.185911Z","shell.execute_reply":"2022-11-10T10:11:05.144465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train model","metadata":{}},{"cell_type":"code","source":"train_results = trainer.train()\ntrainer.save_model()\ntrainer.log_metrics(\"train\", train_results.metrics)\ntrainer.save_metrics(\"train\", train_results.metrics)\ntrainer.save_state()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:11:05.147022Z","iopub.execute_input":"2022-11-10T10:11:05.147873Z","iopub.status.idle":"2022-11-10T10:15:58.957249Z","shell.execute_reply.started":"2022-11-10T10:11:05.147824Z","shell.execute_reply":"2022-11-10T10:15:58.955955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate","metadata":{}},{"cell_type":"code","source":"metrics = trainer.evaluate(prepared_ds['eval'])\ntrainer.log_metrics(\"eval\", metrics)\ntrainer.save_metrics(\"eval\", metrics)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:15:58.959473Z","iopub.execute_input":"2022-11-10T10:15:58.960391Z","iopub.status.idle":"2022-11-10T10:15:59.784430Z","shell.execute_reply.started":"2022-11-10T10:15:58.960336Z","shell.execute_reply":"2022-11-10T10:15:59.783245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot training","metadata":{}},{"cell_type":"code","source":"import json\ntrain_state_ViT = json.load(open('/kaggle/working/fine_tuned_ViT/trainer_state.json', 'r'))\n# train_state['log_history'] is a dictionary which saved validation loss and accuracy","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:15:59.787579Z","iopub.execute_input":"2022-11-10T10:15:59.787883Z","iopub.status.idle":"2022-11-10T10:16:02.821782Z","shell.execute_reply.started":"2022-11-10T10:15:59.787854Z","shell.execute_reply":"2022-11-10T10:16:02.820700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss_ViT = pd.DataFrame.from_dict(train_state_ViT['log_history'])\ndf_loss_ViT","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:02.823140Z","iopub.execute_input":"2022-11-10T10:16:02.823793Z","iopub.status.idle":"2022-11-10T10:16:02.855883Z","shell.execute_reply.started":"2022-11-10T10:16:02.823755Z","shell.execute_reply":"2022-11-10T10:16:02.854604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train loss, Validation loss graph","metadata":{}},{"cell_type":"code","source":"plt.plot(df_loss_ViT.loc[df_loss_ViT['loss'].isnull() == False, 'epoch'], \n         df_loss_ViT.loc[df_loss_ViT['loss'].isnull() == False, 'loss'])\nplt.plot(df_loss_ViT.loc[df_loss_ViT['eval_loss'].isnull() == False, 'epoch'],\n         df_loss_ViT.loc[df_loss_ViT['eval_loss'].isnull() == False,'eval_loss'])\nplt.legend(['Train loss', 'Validation loss'], fontsize=20);\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:02.857445Z","iopub.execute_input":"2022-11-10T10:16:02.858057Z","iopub.status.idle":"2022-11-10T10:16:03.080266Z","shell.execute_reply.started":"2022-11-10T10:16:02.858021Z","shell.execute_reply":"2022-11-10T10:16:03.079137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Validation accuracy graph","metadata":{}},{"cell_type":"code","source":"plt.plot(df_loss_ViT.loc[df_loss_ViT['eval_accuracy'].isnull() == False, 'epoch'], \n         df_loss_ViT.loc[df_loss_ViT['eval_accuracy'].isnull() == False, 'eval_accuracy'])\nplt.legend(['Validation accuracy'], fontsize=20);\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:03.082046Z","iopub.execute_input":"2022-11-10T10:16:03.082446Z","iopub.status.idle":"2022-11-10T10:16:03.299025Z","shell.execute_reply.started":"2022-11-10T10:16:03.082410Z","shell.execute_reply":"2022-11-10T10:16:03.298131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test","metadata":{}},{"cell_type":"code","source":"prepared_test = ds['test'].with_transform(transform)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:03.300293Z","iopub.execute_input":"2022-11-10T10:16:03.300649Z","iopub.status.idle":"2022-11-10T10:16:03.308417Z","shell.execute_reply.started":"2022-11-10T10:16:03.300614Z","shell.execute_reply":"2022-11-10T10:16:03.307234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = trainer.evaluate(prepared_test)\ntrainer.log_metrics(\"test\", metrics)\ntrainer.save_metrics(\"test\", metrics)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:03.310275Z","iopub.execute_input":"2022-11-10T10:16:03.310633Z","iopub.status.idle":"2022-11-10T10:16:03.915131Z","shell.execute_reply.started":"2022-11-10T10:16:03.310606Z","shell.execute_reply":"2022-11-10T10:16:03.914231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from transformers import ViTFeatureExtractor, ViTForImageClassification\n# import torch\n# from datasets import load_dataset\n\n# image = test_ds[\"image\"][0]\n\n# feature_extractor = ViTFeatureExtractor.from_pretrained(\"/kaggle/working/fine_tuned_ViT\")\n# model = ViTForImageClassification.from_pretrained(\"/kaggle/working/fine_tuned_ViT\")\n\n# inputs = feature_extractor(image, return_tensors=\"pt\")\n\n# with torch.no_grad():\n#     logits = model(**inputs).logits\n\n# predicted_label = logits.argmax(-1).item()\n# print(model.config.id2label[predicted_label])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:03.916555Z","iopub.execute_input":"2022-11-10T10:16:03.917032Z","iopub.status.idle":"2022-11-10T10:16:03.921978Z","shell.execute_reply.started":"2022-11-10T10:16:03.916991Z","shell.execute_reply":"2022-11-10T10:16:03.920787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ViTForImageClassification.from_pretrained(\"/kaggle/working/fine_tuned_ViT\")\nfeature_extractor = ViTFeatureExtractor.from_pretrained(\"/kaggle/working/fine_tuned_ViT\")\n\npreds_ViT_ = []\n# Create a list to save the results of model's prediction\n\nfor i in trange(len(test_ds['image'])):\n    image = test_ds['image'][i]\n    inputs = feature_extractor(image, return_tensors=\"pt\")\n\n    with torch.no_grad():\n        logits = model(**inputs).logits\n    \n    predicted_label = logits.argmax(-1).item()\n    preds_ViT_.append(predicted_label)\n#     print(model.config.id2label[predicted_label])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:28:27.196292Z","iopub.execute_input":"2022-11-10T10:28:27.196663Z","iopub.status.idle":"2022-11-10T10:28:46.314677Z","shell.execute_reply.started":"2022-11-10T10:28:27.196633Z","shell.execute_reply":"2022-11-10T10:28:46.313685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute metrics","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_fscore_support as score\nprecision, recall, f1, support = score(test_ds['labels'], preds_ViT_, average='macro')\naccuracy = metrics[\"eval_accuracy\"]\n\nprint(f'precision: {precision}')\nprint(f'recall: {recall}')\nprint(f'f1-score: {f1}')\nprint(f'accuracy: {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:31:28.762454Z","iopub.execute_input":"2022-11-10T10:31:28.762809Z","iopub.status.idle":"2022-11-10T10:31:28.771513Z","shell.execute_reply.started":"2022-11-10T10:31:28.762779Z","shell.execute_reply":"2022-11-10T10:31:28.770453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save metrics in a dataframe\ncompare_df = pd.DataFrame(columns=['Metrics', 'ViT', 'ConvNeXt'])\ncompare_df['Metrics'] = ['Precision', 'Recall', 'F1-score', 'Accuracy']\ncompare_df['ViT'] = [precision, recall, f1, accuracy]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:31:30.258496Z","iopub.execute_input":"2022-11-10T10:31:30.258850Z","iopub.status.idle":"2022-11-10T10:31:30.267351Z","shell.execute_reply.started":"2022-11-10T10:31:30.258820Z","shell.execute_reply":"2022-11-10T10:31:30.266280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Confusion matrix ","metadata":{}},{"cell_type":"code","source":"# Creat plot_confusion_matrix function\nimport itertools\nimport matplotlib.pyplot as plt\n\ndef plot_confusion_matrix(cm,classes,normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):\n    \n    plt.figure(figsize=(15,10))\n    plt.imshow(cm,interpolation='nearest',cmap=cmap)\n    plt.title(title)\n    plt.colorbar()\n    tick_marks=np.arange(len(classes))\n    plt.xticks(tick_marks,classes,rotation=45,fontsize=15)\n    plt.yticks(tick_marks,classes,fontsize=15,rotation=90)\n\n    if normalize:\n        cm=cm.astype('float')/cm.sum(axis=1)[:,np.newaxis]\n        cm=np.around(cm,decimals=2)\n        cm[np.isnan(cm)]=0.0\n        print('Normalized confusion matrix')\n    else:\n        print('Confusion matrix, without normalization')\n    thresh=cm.max()/2\n\n    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n\n        plt.text(j, i, cm[i, j],\n                horizontalalignment=\"center\",fontsize=15,\n                color=\"white\" if cm[i, j] > thresh else \"black\")\n        plt.tight_layout()\n        plt.ylabel('True label',fontsize=20)\n        plt.xlabel('Predicted label',fontsize=20)\n\n    plt.grid(False)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:24.155408Z","iopub.execute_input":"2022-11-10T10:16:24.155757Z","iopub.status.idle":"2022-11-10T10:16:24.167119Z","shell.execute_reply.started":"2022-11-10T10:16:24.155724Z","shell.execute_reply":"2022-11-10T10:16:24.166229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate confusion_matrix_ViT\nfrom sklearn.metrics import confusion_matrix\n\nconfusion_matrix_ViT = confusion_matrix(test_ds['labels'], preds_ViT_)\nconfusion_matrix_ViT","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:24.168441Z","iopub.execute_input":"2022-11-10T10:16:24.168846Z","iopub.status.idle":"2022-11-10T10:16:24.181883Z","shell.execute_reply.started":"2022-11-10T10:16:24.168813Z","shell.execute_reply":"2022-11-10T10:16:24.180768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_confusion_matrix(confusion_matrix_ViT, ['No Cancer','Cancer'],normalize=True,title='Confusion matrix',cmap=plt.cm.Blues)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:24.190850Z","iopub.execute_input":"2022-11-10T10:16:24.191332Z","iopub.status.idle":"2022-11-10T10:16:24.558226Z","shell.execute_reply.started":"2022-11-10T10:16:24.191306Z","shell.execute_reply":"2022-11-10T10:16:24.557161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3.2. ConvNeXt Model","metadata":{}},{"cell_type":"markdown","source":"### Loading ConvNeXt Feature Extractor","metadata":{}},{"cell_type":"code","source":"from transformers import ConvNextFeatureExtractor\n\nmodel_name_or_path = 'facebook/convnext-tiny-224'\nfeature_extractor =  ConvNextFeatureExtractor(do_resize=False).from_pretrained(model_name_or_path)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:24.559630Z","iopub.execute_input":"2022-11-10T10:16:24.560746Z","iopub.status.idle":"2022-11-10T10:16:25.526204Z","shell.execute_reply.started":"2022-11-10T10:16:24.560707Z","shell.execute_reply":"2022-11-10T10:16:25.525245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label = {'0':'No Cancer', '1':'Cancer'}\nlabel2id = {v:k for k,v in enumerate(labels)}","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:25.527829Z","iopub.execute_input":"2022-11-10T10:16:25.528497Z","iopub.status.idle":"2022-11-10T10:16:25.533988Z","shell.execute_reply.started":"2022-11-10T10:16:25.528459Z","shell.execute_reply":"2022-11-10T10:16:25.532776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Processing the data set\nWe know how to read images and transform them into inputs, now we have to write a function that will put those two things together to process a single example from the dataset.","metadata":{}},{"cell_type":"code","source":"def process_example(example):\n    inputs = feature_extractor(example['image'], return_tensors='pt')\n    inputs['labels'] = example['labels']\n    return inputs\ndef transform(example_batch):\n    # Take a list of PIL images and turn them to pixel values\n    inputs = feature_extractor([x for x in example_batch['image']], return_tensors='pt')\n\n    inputs['labels'] = example_batch['labels']\n    return inputs","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:25.535659Z","iopub.execute_input":"2022-11-10T10:16:25.536335Z","iopub.status.idle":"2022-11-10T10:16:25.546168Z","shell.execute_reply.started":"2022-11-10T10:16:25.536300Z","shell.execute_reply":"2022-11-10T10:16:25.545153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom datasets import load_metric\n\nmetric = load_metric(\"accuracy\")\ndef compute_metrics(p):\n    return metric.compute(predictions=np.argmax(p.predictions, axis=1), references=p.label_ids)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:25.547815Z","iopub.execute_input":"2022-11-10T10:16:25.548483Z","iopub.status.idle":"2022-11-10T10:16:26.345635Z","shell.execute_reply.started":"2022-11-10T10:16:25.548448Z","shell.execute_reply":"2022-11-10T10:16:26.344692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prepared_ds = ds.with_transform(transform)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:26.346943Z","iopub.execute_input":"2022-11-10T10:16:26.347311Z","iopub.status.idle":"2022-11-10T10:16:26.356484Z","shell.execute_reply.started":"2022-11-10T10:16:26.347277Z","shell.execute_reply":"2022-11-10T10:16:26.355328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Load model\nNext we'll add the pretrained model. We'll add `num_labels` on init so the model creates a classification head with the right number of units. ","metadata":{}},{"cell_type":"code","source":"from transformers import AutoModelForImageClassification\n\nmodel = AutoModelForImageClassification.from_pretrained(\"facebook/convnext-tiny-224\",\n                                                        num_labels=len(labels),\n                                                        id2label=id2label,\n                                                        label2id=label2id,\n                                                        ignore_mismatched_sizes=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:26.357828Z","iopub.execute_input":"2022-11-10T10:16:26.358813Z","iopub.status.idle":"2022-11-10T10:16:30.699834Z","shell.execute_reply.started":"2022-11-10T10:16:26.358776Z","shell.execute_reply":"2022-11-10T10:16:30.698849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Setting up the training configuration by defining `TrainingArguments`.### Setting up the training configuration by defining `TrainingArguments`","metadata":{}},{"cell_type":"code","source":"from transformers import TrainingArguments\n\ntraining_args = TrainingArguments(\n  output_dir=\"./fine_tuned_convnext\",\n  per_device_train_batch_size=16,\n  evaluation_strategy=\"steps\",\n  num_train_epochs=30,\n  #fp16=False,\n  fp16=True,\n  save_steps=100,\n  eval_steps=100,\n  logging_steps=10,\n  learning_rate=2e-4,\n  save_total_limit=2,\n  remove_unused_columns=False,\n  push_to_hub=False,\n  report_to='tensorboard',\n  load_best_model_at_end=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:30.701515Z","iopub.execute_input":"2022-11-10T10:16:30.701896Z","iopub.status.idle":"2022-11-10T10:16:30.745981Z","shell.execute_reply.started":"2022-11-10T10:16:30.701847Z","shell.execute_reply":"2022-11-10T10:16:30.745104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Set up `Trainer`\nNow, all instances can be passed to `Trainer` and we are ready to start training!","metadata":{}},{"cell_type":"code","source":"from transformers import EarlyStoppingCallback\nfrom transformers import Trainer\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    data_collator=collate_fn,\n    compute_metrics=compute_metrics,\n    train_dataset=prepared_ds['train'],\n    eval_dataset=prepared_ds['eval'],\n    tokenizer=feature_extractor,\n    callbacks = [EarlyStoppingCallback(early_stopping_patience=5)]\n\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:30.748834Z","iopub.execute_input":"2022-11-10T10:16:30.749095Z","iopub.status.idle":"2022-11-10T10:16:30.795721Z","shell.execute_reply.started":"2022-11-10T10:16:30.749070Z","shell.execute_reply":"2022-11-10T10:16:30.794825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train Model","metadata":{}},{"cell_type":"code","source":"train_results = trainer.train()\ntrainer.save_model()\ntrainer.log_metrics(\"train\", train_results.metrics)\ntrainer.save_metrics(\"train\", train_results.metrics)\ntrainer.save_state()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:16:30.797915Z","iopub.execute_input":"2022-11-10T10:16:30.798323Z","iopub.status.idle":"2022-11-10T10:19:27.350684Z","shell.execute_reply.started":"2022-11-10T10:16:30.798286Z","shell.execute_reply":"2022-11-10T10:19:27.349441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Evaluate","metadata":{}},{"cell_type":"code","source":"metrics = trainer.evaluate(prepared_ds['eval'])\ntrainer.log_metrics(\"eval\", metrics)\ntrainer.save_metrics(\"eval\", metrics)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:27.352769Z","iopub.execute_input":"2022-11-10T10:19:27.353228Z","iopub.status.idle":"2022-11-10T10:19:27.837573Z","shell.execute_reply.started":"2022-11-10T10:19:27.353160Z","shell.execute_reply":"2022-11-10T10:19:27.836010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot training","metadata":{}},{"cell_type":"code","source":"import json\ntrain_state_ConvNeXt = json.load(open('/kaggle/working/fine_tuned_convnext/trainer_state.json', 'r'))","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:27.838846Z","iopub.execute_input":"2022-11-10T10:19:27.839200Z","iopub.status.idle":"2022-11-10T10:19:27.846064Z","shell.execute_reply.started":"2022-11-10T10:19:27.839147Z","shell.execute_reply":"2022-11-10T10:19:27.844688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_loss_ConvNeXt = pd.DataFrame.from_dict(train_state_ConvNeXt['log_history'])\ndf_loss_ConvNeXt","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:27.847886Z","iopub.execute_input":"2022-11-10T10:19:27.848500Z","iopub.status.idle":"2022-11-10T10:19:27.877545Z","shell.execute_reply.started":"2022-11-10T10:19:27.848465Z","shell.execute_reply":"2022-11-10T10:19:27.876675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Train loss, Validation loss graph","metadata":{}},{"cell_type":"code","source":"plt.plot(df_loss_ConvNeXt.loc[df_loss_ConvNeXt['loss'].isnull() == False, 'epoch'],\n         df_loss_ConvNeXt.loc[df_loss_ConvNeXt['loss'].isnull() == False, 'loss'])\nplt.plot(df_loss_ConvNeXt.loc[df_loss_ConvNeXt['eval_loss'].isnull() == False, 'epoch'],\n         df_loss_ConvNeXt.loc[df_loss_ConvNeXt['eval_loss'].isnull() == False,'eval_loss'])\nplt.legend(['Train loss', 'Validation loss'], fontsize=20);\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:27.878978Z","iopub.execute_input":"2022-11-10T10:19:27.879342Z","iopub.status.idle":"2022-11-10T10:19:28.111066Z","shell.execute_reply.started":"2022-11-10T10:19:27.879307Z","shell.execute_reply":"2022-11-10T10:19:28.110158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Validation accuracy graph","metadata":{}},{"cell_type":"code","source":"plt.plot(df_loss_ConvNeXt.loc[df_loss_ConvNeXt['eval_accuracy'].isnull() == False, 'epoch'], df_loss_ConvNeXt.loc[df_loss_ConvNeXt['eval_accuracy'].isnull() == False, 'eval_accuracy'])\nplt.legend(['Validation accuracy'], fontsize=20);\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:28.113231Z","iopub.execute_input":"2022-11-10T10:19:28.113837Z","iopub.status.idle":"2022-11-10T10:19:28.339097Z","shell.execute_reply.started":"2022-11-10T10:19:28.113798Z","shell.execute_reply":"2022-11-10T10:19:28.338160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test","metadata":{}},{"cell_type":"code","source":"prepared_test = ds['test'].with_transform(transform)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:28.340727Z","iopub.execute_input":"2022-11-10T10:19:28.341406Z","iopub.status.idle":"2022-11-10T10:19:28.347588Z","shell.execute_reply.started":"2022-11-10T10:19:28.341367Z","shell.execute_reply":"2022-11-10T10:19:28.346940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics = trainer.evaluate(prepared_test)\ntrainer.log_metrics(\"test\", metrics)\ntrainer.save_metrics(\"test\", metrics)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:28.348755Z","iopub.execute_input":"2022-11-10T10:19:28.349547Z","iopub.status.idle":"2022-11-10T10:19:28.845268Z","shell.execute_reply.started":"2022-11-10T10:19:28.349507Z","shell.execute_reply":"2022-11-10T10:19:28.844240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = AutoModelForImageClassification.from_pretrained(\"/kaggle/working/fine_tuned_convnext\")\nfeature_extractor = ConvNextFeatureExtractor(do_resize=False).from_pretrained(\"/kaggle/working/fine_tuned_convnext\")\n\npreds_ConvNeXt_ =[]\n# Create a list to save the results of model's prediction\n\nfor i in trange(len(test_ds['image'])):\n    image = test_ds['image'][i]\n    inputs = feature_extractor(image, return_tensors=\"pt\")\n\n    with torch.no_grad():\n        logits = model(**inputs).logits\n    \n    predicted_label = logits.argmax(-1).item()\n    preds_ConvNeXt_.append(predicted_label)\n#     print(model.config.id2label[predicted_label])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:28.847377Z","iopub.execute_input":"2022-11-10T10:19:28.848214Z","iopub.status.idle":"2022-11-10T10:19:36.185716Z","shell.execute_reply.started":"2022-11-10T10:19:28.848158Z","shell.execute_reply":"2022-11-10T10:19:36.184572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compute metrics","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import precision_recall_fscore_support as score\nprecision, recall, f1, support = score(test_ds['labels'], preds_ConvNeXt_, average='macro')\naccuracy = metrics[\"eval_accuracy\"]\n\nprint(f'precision: {precision}')\nprint(f'recall: {recall}')\nprint(f'f1-score: {f1}')\nprint(f'accuracy: {accuracy}')\n\ncompare_df['ConvNeXt'] = [precision, recall, f1, accuracy]","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:31:46.999030Z","iopub.execute_input":"2022-11-10T10:31:46.999709Z","iopub.status.idle":"2022-11-10T10:31:47.009430Z","shell.execute_reply.started":"2022-11-10T10:31:46.999672Z","shell.execute_reply":"2022-11-10T10:31:47.008294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Confusion matrix","metadata":{}},{"cell_type":"code","source":"# Calculate confusion_matrix_ConvNeXt\nconfusion_matrix_ConvNeXt = confusion_matrix(test_ds['labels'], preds_ConvNeXt_)\nconfusion_matrix_ConvNeXt","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:36.200210Z","iopub.execute_input":"2022-11-10T10:19:36.201038Z","iopub.status.idle":"2022-11-10T10:19:36.209773Z","shell.execute_reply.started":"2022-11-10T10:19:36.201003Z","shell.execute_reply":"2022-11-10T10:19:36.208660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot confusion matrix graph\nplot_confusion_matrix(confusion_matrix_ConvNeXt, ['No Cancer','Cancer'],\n                      normalize=True,title='Confusion matrix',cmap=plt.cm.Greens)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:19:36.211061Z","iopub.execute_input":"2022-11-10T10:19:36.211899Z","iopub.status.idle":"2022-11-10T10:19:36.566597Z","shell.execute_reply.started":"2022-11-10T10:19:36.211853Z","shell.execute_reply":"2022-11-10T10:19:36.565496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4. Performance comparison between ViT and ConvNeXt","metadata":{}},{"cell_type":"code","source":"compare_df.set_index('Metrics')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T10:37:17.771981Z","iopub.execute_input":"2022-11-10T10:37:17.772351Z","iopub.status.idle":"2022-11-10T10:37:17.785433Z","shell.execute_reply.started":"2022-11-10T10:37:17.772321Z","shell.execute_reply":"2022-11-10T10:37:17.784416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}