{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.) Importing Libraries" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-01-30 10:13:32.230370: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2025-01-30 10:13:32.234656: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2025-01-30 10:13:32.248550: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "E0000 00:00:1738212212.279462 1320083 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "E0000 00:00:1738212212.287111 1320083 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "2025-01-30 10:13:32.317759: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n" ] } ], "source": [ "import os\n", "from PIL import Image\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "from glob import glob\n", "\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import classification_report, confusion_matrix\n", "\n", "import tensorflow as tf\n", "from tensorflow.keras.layers import Input, Dense, Flatten, Dropout, GlobalAveragePooling2D\n", "from tensorflow.keras.models import Model\n", "from tensorflow.keras.optimizers import Adamax\n", "from tensorflow.keras.metrics import Precision, Recall\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.) Preprocessing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2.1 Load Data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Path to dataset files: /home/agilecpu154/.cache/kagglehub/datasets/masoudnickparvar/brain-tumor-mri-dataset/versions/1\n" ] } ], "source": [ "import kagglehub\n", "\n", "# Download latest version\n", "path = kagglehub.dataset_download(\"masoudnickparvar/brain-tumor-mri-dataset\")\n", "\n", "print(\"Path to dataset files:\", path)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['glioma', 'pituitary', 'notumor', 'meningioma']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train_dir = r'/home/agilecpu154/.cache/kagglehub/datasets/masoudnickparvar/brain-tumor-mri-dataset/versions/1/Training'\n", "\n", "folds = os.listdir(train_dir)\n", "folds" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def train_df(tr_path):\n", " classes, class_paths = zip(*[(label, os.path.join(tr_path, label, image))\n", " for label in os.listdir(tr_path) if os.path.isdir(os.path.join(tr_path, label))\n", " for image in os.listdir(os.path.join(tr_path, label))])\n", "\n", " tr_df = pd.DataFrame({'Class Path': class_paths, 'Class': classes})\n", " return tr_df" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def test_df(ts_path):\n", " classes, class_paths = zip(*[(label, os.path.join(ts_path, label, image))\n", " for label in os.listdir(ts_path) if os.path.isdir(os.path.join(ts_path, label))\n", " for image in os.listdir(os.path.join(ts_path, label))])\n", "\n", " ts_df = pd.DataFrame({'Class Path': class_paths, 'Class': classes})\n", " return ts_df" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Class Path | \n", "Class | \n", "
---|---|---|
0 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
1 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
2 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
3 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
4 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
... | \n", "... | \n", "... | \n", "
5707 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
5708 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
5709 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
5710 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
5711 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
5712 rows × 2 columns
\n", "\n", " | Class Path | \n", "Class | \n", "
---|---|---|
0 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
1 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
2 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
3 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
4 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
... | \n", "... | \n", "... | \n", "
1306 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
1307 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
1308 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
1309 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
1310 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
1311 rows × 2 columns
\n", "\n", " | Class Path | \n", "Class | \n", "
---|---|---|
837 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "notumor | \n", "
1034 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
770 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "notumor | \n", "
158 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
1296 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
... | \n", "... | \n", "... | \n", "
1182 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
127 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
109 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "glioma | \n", "
758 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "notumor | \n", "
1175 | \n", "/home/agilecpu154/.cache/kagglehub/datasets/ma... | \n", "meningioma | \n", "
655 rows × 2 columns
\n", "