diff --git a/Brain_Tumour_Detection_using_VIT_(2).ipynb b/Brain_Tumour_Detection_using_VIT_(2).ipynb new file mode 100644 index 000000000..c22762925 --- /dev/null +++ b/Brain_Tumour_Detection_using_VIT_(2).ipynb @@ -0,0 +1,1261 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mpEN5ZVTFSZx", + "outputId": "3885fb26-ba28-4cad-b38f-a42567d9f60c" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Sun Dec 8 11:22:53 2024 \n", + "+---------------------------------------------------------------------------------------+\n", + "| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Version: 12.2 |\n", + "|-----------------------------------------+----------------------+----------------------+\n", + "| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |\n", + "| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n", + "| | | MIG M. |\n", + "|=========================================+======================+======================|\n", + "| 0 Tesla T4 Off | 00000000:00:04.0 Off | 0 |\n", + "| N/A 45C P8 9W / 70W | 0MiB / 15360MiB | 0% Default |\n", + "| | | N/A |\n", + "+-----------------------------------------+----------------------+----------------------+\n", + " \n", + "+---------------------------------------------------------------------------------------+\n", + "| Processes: |\n", + "| GPU GI CI PID Type Process name GPU Memory |\n", + "| ID ID Usage |\n", + "|=======================================================================================|\n", + "| No running processes found |\n", + "+---------------------------------------------------------------------------------------+\n" + ] + } + ], + "source": [ + "!nvidia-smi" + ] + }, + { + "cell_type": "code", + "source": [ + "!python --version\n", + "!pip install visualkeras\n", + "# !pip install google-auth-oauthlib>=0.7.0 --upgrade\n", + "!pip install --upgrade google-auth-oauthlib\n", + "!pip install tensorflow==2.17.0\n", + "!pip install tensorflow-addons==0.16.1\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0ie6Nxi7zmRI", + "outputId": "516516f4-40e4-4722-d75d-a39c1da28d99" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Python 3.10.12\n", + "Collecting visualkeras\n", + " Downloading visualkeras-0.1.4-py3-none-any.whl.metadata (11 kB)\n", + "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from visualkeras) (11.0.0)\n", + "Requirement already satisfied: numpy>=1.18.1 in /usr/local/lib/python3.10/dist-packages (from visualkeras) (1.26.4)\n", + "Collecting aggdraw>=1.3.11 (from visualkeras)\n", + " Downloading aggdraw-1.3.19-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (655 bytes)\n", + "Downloading visualkeras-0.1.4-py3-none-any.whl (17 kB)\n", + "Downloading aggdraw-1.3.19-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (993 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m993.7/993.7 kB\u001b[0m \u001b[31m22.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hInstalling collected packages: aggdraw, visualkeras\n", + "Successfully installed aggdraw-1.3.19 visualkeras-0.1.4\n", + "Requirement already satisfied: google-auth-oauthlib in /usr/local/lib/python3.10/dist-packages (1.2.1)\n", + "Requirement already satisfied: google-auth>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib) (2.27.0)\n", + "Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib) (1.3.1)\n", + "Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth>=2.15.0->google-auth-oauthlib) (5.5.0)\n", + "Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth>=2.15.0->google-auth-oauthlib) (0.4.1)\n", + "Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth>=2.15.0->google-auth-oauthlib) (4.9)\n", + "Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib) (3.2.2)\n", + "Requirement already satisfied: requests>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib) (2.32.3)\n", + "Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth>=2.15.0->google-auth-oauthlib) (0.6.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib) (3.4.0)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib) (2.2.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.0.0->requests-oauthlib>=0.7.0->google-auth-oauthlib) (2024.8.30)\n", + "Collecting tensorflow==2.17.0\n", + " Downloading tensorflow-2.17.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.2 kB)\n", + "Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.4.0)\n", + "Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.6.3)\n", + "Requirement already satisfied: flatbuffers>=24.3.25 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (24.3.25)\n", + "Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (0.6.0)\n", + "Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (0.2.0)\n", + "Requirement already satisfied: h5py>=3.10.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (3.12.1)\n", + "Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (18.1.1)\n", + "Requirement already satisfied: ml-dtypes<0.5.0,>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (0.4.1)\n", + "Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (3.4.0)\n", + "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (24.2)\n", + "Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (4.25.5)\n", + "Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (2.32.3)\n", + "Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (75.1.0)\n", + "Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.16.0)\n", + "Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (2.5.0)\n", + "Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (4.12.2)\n", + "Requirement already satisfied: wrapt>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.17.0)\n", + "Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.68.1)\n", + "Requirement already satisfied: tensorboard<2.18,>=2.17 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (2.17.1)\n", + "Requirement already satisfied: keras>=3.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (3.5.0)\n", + "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (0.37.1)\n", + "Requirement already satisfied: numpy<2.0.0,>=1.23.5 in /usr/local/lib/python3.10/dist-packages (from tensorflow==2.17.0) (1.26.4)\n", + "Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow==2.17.0) (0.45.1)\n", + "Requirement already satisfied: rich in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow==2.17.0) (13.9.4)\n", + "Requirement already satisfied: namex in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow==2.17.0) (0.0.8)\n", + "Requirement already satisfied: optree in /usr/local/lib/python3.10/dist-packages (from keras>=3.2.0->tensorflow==2.17.0) (0.13.1)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.17.0) (3.4.0)\n", + "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.17.0) (3.10)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.17.0) (2.2.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorflow==2.17.0) (2024.8.30)\n", + "Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow==2.17.0) (3.7)\n", + "Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow==2.17.0) (0.7.2)\n", + "Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.18,>=2.17->tensorflow==2.17.0) (3.1.3)\n", + "Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.18,>=2.17->tensorflow==2.17.0) (3.0.2)\n", + "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.2.0->tensorflow==2.17.0) (3.0.0)\n", + "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich->keras>=3.2.0->tensorflow==2.17.0) (2.18.0)\n", + "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich->keras>=3.2.0->tensorflow==2.17.0) (0.1.2)\n", + "Downloading tensorflow-2.17.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (601.3 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m601.3/601.3 MB\u001b[0m \u001b[31m3.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hInstalling collected packages: tensorflow\n", + " Attempting uninstall: tensorflow\n", + " Found existing installation: tensorflow 2.17.1\n", + " Uninstalling tensorflow-2.17.1:\n", + " Successfully uninstalled tensorflow-2.17.1\n", + "Successfully installed tensorflow-2.17.0\n", + "Collecting tensorflow-addons==0.16.1\n", + " Downloading tensorflow_addons-0.16.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.metadata (1.8 kB)\n", + "Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.10/dist-packages (from tensorflow-addons==0.16.1) (4.4.1)\n", + "Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.10/dist-packages (from typeguard>=2.7->tensorflow-addons==0.16.1) (4.12.2)\n", + "Downloading tensorflow_addons-0.16.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.1/1.1 MB\u001b[0m \u001b[31m17.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hInstalling collected packages: tensorflow-addons\n", + "Successfully installed tensorflow-addons-0.16.1\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "!pip show tensorflow\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cVMf4dVP2Sbv", + "outputId": "6b366ba6-aa6f-410d-c51e-10c057d595de" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Name: tensorflow\n", + "Version: 2.17.0\n", + "Summary: TensorFlow is an open source machine learning framework for everyone.\n", + "Home-page: https://www.tensorflow.org/\n", + "Author: Google Inc.\n", + "Author-email: packages@tensorflow.org\n", + "License: Apache 2.0\n", + "Location: /usr/local/lib/python3.10/dist-packages\n", + "Requires: absl-py, astunparse, flatbuffers, gast, google-pasta, grpcio, h5py, keras, libclang, ml-dtypes, numpy, opt-einsum, packaging, protobuf, requests, setuptools, six, tensorboard, tensorflow-io-gcs-filesystem, termcolor, typing-extensions, wrapt\n", + "Required-by: dopamine_rl, tf_keras\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8V6JUEdFPdQh" + }, + "source": [ + "##Importing necessary libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 512 + }, + "id": "Qo2KIg5uFgA6", + "outputId": "12c4bfb3-a318-46d0-a937-17dfd563da6e" + }, + "outputs": [ + { + "output_type": "error", + "ename": "ImportError", + "evalue": "/usr/local/lib/python3.10/dist-packages/tensorflow/python/../libtensorflow_cc.so.2: undefined symbol: _ZN4toco9TocoFlags5Impl_66_i_give_permission_to_break_this_code_default_qdq_conversion_mode_E", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0msklearn\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmetrics\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mconfusion_matrix\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclassification_report\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mcollections\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCounter\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/__init__.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0m_tf2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0m__internal__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0m__operators__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0maudio\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/_api/v2/__internal__/__init__.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msys\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m_sys\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__internal__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mautograph\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__internal__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdecorator\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_api\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__internal__\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdispatch\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/_api/v2/__internal__/autograph/__init__.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0msys\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0m_sys\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mag_ctx\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcontrol_status_ctx\u001b[0m \u001b[0;31m# line: 34\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 9\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimpl\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapi\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtf_convert\u001b[0m \u001b[0;31m# line: 493\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/python/autograph/core/ag_ctx.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mthreading\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mag_logging\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutil\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtf_export\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtf_export\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/python/autograph/utils/__init__.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;34m\"\"\"Utility module that contains APIs usable in the generated code.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcontext_managers\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcontrol_dependency_on_returns\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmisc\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0malias_tensors\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mautograph\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtensor_list\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mdynamic_list_append\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/python/autograph/utils/context_managers.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcontextlib\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mframework\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 20\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtensor_array_ops\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/python/framework/ops.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[0;31m# pylint: disable=invalid-import-order,g-bad-import-order,unused-import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpywrap_tensorflow\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 46\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpywrap_tfe\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 47\u001b[0m \u001b[0;31m# pylint: enable=invalid-import-order,g-bad-import-order,unused-import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtf2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/tensorflow/python/pywrap_tfe.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;31m# pylint: disable=invalid-import-order,g-bad-import-order, wildcard-import, unused-import\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 24\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpywrap_tensorflow\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_pywrap_tfe\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mImportError\u001b[0m: /usr/local/lib/python3.10/dist-packages/tensorflow/python/../libtensorflow_cc.so.2: undefined symbol: _ZN4toco9TocoFlags5Impl_66_i_give_permission_to_break_this_code_default_qdq_conversion_mode_E", + "", + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n" + ], + "errorDetails": { + "actions": [ + { + "action": "open_url", + "actionText": "Open Examples", + "url": "/notebooks/snippets/importing_libraries.ipynb" + } + ] + } + } + ], + "source": [ + "\n", + "import os\n", + "import warnings\n", + "import itertools\n", + "import cv2\n", + "import seaborn as sns\n", + "import pandas as pd\n", + "import numpy as np\n", + "from PIL import Image\n", + "from sklearn.utils import class_weight\n", + "from sklearn.metrics import confusion_matrix, classification_report\n", + "from collections import Counter\n", + "import tensorflow as tf\n", + "\n", + "\n", + "import visualkeras\n", + "import plotly.express as px\n", + "import matplotlib.pyplot as plt\n", + "from sklearn.metrics import multilabel_confusion_matrix\n", + "\n", + "from tensorflow.keras.preprocessing.image import load_img\n", + "from tensorflow.keras.utils import plot_model\n", + "from tensorflow.keras.models import model_from_json\n", + "from tensorflow.keras import layers\n", + "from tensorflow.keras import regularizers\n", + "from sklearn.model_selection import train_test_split\n", + "from keras.preprocessing.image import ImageDataGenerator\n", + "\n", + "warnings.filterwarnings('ignore')\n", + "%matplotlib inline" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-oxf5CfOPrnu" + }, + "source": [ + "##Setting up general parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "64QeMq0yFly_" + }, + "outputs": [], + "source": [ + "# General parameters\n", + "epochs = 15\n", + "pic_size = 240\n", + "np.random.seed(42)\n", + "tf.random.set_seed(42)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lHmFDTJbP2KB" + }, + "source": [ + "##Mounting Gdrive for Loading Data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "g9kIrc5YFvzF" + }, + "outputs": [], + "source": [ + "from google.colab import drive\n", + "drive.mount('/content/gdrive/')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "1LqtXP0-P-gY" + }, + "source": [ + "##Checking the directories" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "PfZuCZN0Fyzb" + }, + "outputs": [], + "source": [ + "!ls \"/content/gdrive/My Drive/BTD/brain_tumor_dataset\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L4cgSTFbQILZ" + }, + "source": [ + "##Data Loading, Preperation and Visualization" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Sr7nop3bF6w4" + }, + "outputs": [], + "source": [ + "folder_path = \"/content/gdrive/My Drive/BTD/brain_tumor_dataset\"\n", + "no_images = os.listdir('/content/gdrive/My Drive/BTD/brain_tumor_dataset/no/')\n", + "yes_images = os.listdir('/content/gdrive/My Drive/BTD/brain_tumor_dataset/yes/')\n", + "dataset=[]\n", + "lab=[]\n", + "\n", + "for image_name in no_images:\n", + " image=cv2.imread('/content/gdrive/My Drive/BTD/brain_tumor_dataset/no/'+ image_name)\n", + " image=Image.fromarray(image,'RGB')\n", + " image=image.resize((240,240))\n", + " dataset.append(np.array(image))\n", + " lab.append(0)\n", + "\n", + "for image_name in yes_images:\n", + " image=cv2.imread('/content/gdrive/My Drive/BTD/brain_tumor_dataset/yes/' + image_name)\n", + " image=Image.fromarray(image,'RGB')\n", + " image=image.resize((240,240))\n", + " dataset.append(np.array(image))\n", + " lab.append(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "aLmCDrZIF-oE" + }, + "outputs": [], + "source": [ + "dataset = np.array(dataset)\n", + "lab = np.array(lab)\n", + "print(dataset.shape, lab.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "c_ev6uNWGINt" + }, + "outputs": [], + "source": [ + "x_train, x_test, y_train, y_test = train_test_split(dataset, lab, test_size=0.2, shuffle=True, random_state=42)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "uqObQ0seGLfH" + }, + "outputs": [], + "source": [ + "def plot_state(state):\n", + " plt.figure(figsize= (12,12))\n", + " for i in range(1, 10, 1):\n", + " plt.subplot(3,3,i)\n", + " img = load_img(folder_path + \"/\" + state + \"/\" + os.listdir(folder_path + \"/\" + state)[i], target_size=(pic_size, pic_size))\n", + " plt.imshow(img)\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "feZKDB1zGVQR" + }, + "outputs": [], + "source": [ + "plot_state('yes')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "t_YP0EVKGX8A" + }, + "outputs": [], + "source": [ + "plot_state(\"no\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kCzRCngbQPaD" + }, + "source": [ + "##CNN Modeling" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PjfNiLFM_f9Q" + }, + "source": [ + "*More layers added from cell.no 103 to 108" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "TZmcZbg0GdEq" + }, + "outputs": [], + "source": [ + "model = tf.keras.Sequential([\n", + "\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\",input_shape=(pic_size,pic_size,3)),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + "\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=64, activation='relu',\n", + " kernel_regularizer=regularizers.L1L2(l1=1e-3, l2=1e-3),\n", + " bias_regularizer=regularizers.L2(1e-2),\n", + " activity_regularizer=regularizers.L2(1e-3)),\n", + " tf.keras.layers.Dropout(0.5),\n", + " tf.keras.layers.Dense(units=1, activation='sigmoid'),\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "UN9OlZcTGjsY" + }, + "outputs": [], + "source": [ + "model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n", + "model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "AiXN3C9ZpXpj" + }, + "outputs": [], + "source": [ + "model_1 = tf.keras.Sequential([\n", + "\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\",input_shape=(pic_size,pic_size,3)),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=16,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + "\n", + "\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=64, activation='relu',\n", + " kernel_regularizer=regularizers.L1L2(l1=1e-3, l2=1e-3),\n", + " bias_regularizer=regularizers.L2(1e-2),\n", + " activity_regularizer=regularizers.L2(1e-3)),\n", + " tf.keras.layers.Dropout(0.5),\n", + " tf.keras.layers.Dense(units=1, activation='sigmoid'),\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "IsyrQDQJpitU" + }, + "outputs": [], + "source": [ + "model_1.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n", + "model_1.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "3QbobQSk3f0Z" + }, + "outputs": [], + "source": [ + "model_2 = tf.keras.Sequential([\n", + "\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\",input_shape=(pic_size,pic_size,3)),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + " tf.keras.layers.MaxPooling2D((2, 2)),\n", + " tf.keras.layers.Conv2D(filters=16,kernel_size=(3,3),strides=(2,2), activation=\"relu\", padding=\"valid\"),\n", + "\n", + "\n", + " tf.keras.layers.Flatten(),\n", + " tf.keras.layers.Dense(units=64, activation='relu',\n", + " kernel_regularizer=regularizers.L1L2(l1=1e-3, l2=1e-3),\n", + " bias_regularizer=regularizers.L2(1e-2),\n", + " activity_regularizer=regularizers.L2(1e-3)),\n", + " tf.keras.layers.Dropout(0.5),\n", + " tf.keras.layers.Dense(units=1, activation='sigmoid'),\n", + "])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "0ZLg59-z3kMv" + }, + "outputs": [], + "source": [ + "model_2.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n", + "model_2.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "fbDLWyt5Goby" + }, + "outputs": [], + "source": [ + "plot_model(model, show_shapes=True, show_layer_names=False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "9WtsW0-NGs9t" + }, + "outputs": [], + "source": [ + "visualkeras.layered_view(model, legend=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ZlocR13UGwn7" + }, + "outputs": [], + "source": [ + "class_weights = class_weight.compute_class_weight(class_weight = \"balanced\", classes= np.unique(y_train), y= y_train)\n", + "class_weights = dict(zip(np.unique(y_train), class_weights))\n", + "class_weights" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Y61ZqOQlGzOq" + }, + "outputs": [], + "source": [ + "history = model.fit(x_train,y_train,epochs = 100, class_weight=class_weights, validation_data=(x_test, y_test),verbose=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Bk84Q1zfR74N" + }, + "source": [ + "##Model Evaluation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "p6hDhaKpG2Oo" + }, + "outputs": [], + "source": [ + "plt.figure(figsize=(20,10))\n", + "plt.subplot(1, 2, 1)\n", + "plt.suptitle('Optimizer : Adam', fontsize=10)\n", + "plt.ylabel('Loss', fontsize=16)\n", + "plt.plot(history.history['loss'], label='Training Loss')\n", + "plt.plot(history.history['val_loss'], label='Validation Loss')\n", + "plt.legend(loc='upper right')\n", + "\n", + "plt.subplot(1, 2, 2)\n", + "plt.ylabel('Accuracy', fontsize=16)\n", + "plt.plot(history.history['accuracy'], label='Training Accuracy')\n", + "plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n", + "plt.legend(loc='lower right')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "DhnVRp8wILnq" + }, + "outputs": [], + "source": [ + "results = model.evaluate(x_test, y_test)\n", + "print('The current model achieved an accuracy of {}%!'.format(round(results[1]*100,2)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "wGbxSjWtIS-f" + }, + "outputs": [], + "source": [ + "# compute predictions\n", + "predictions = model.predict(x_test)\n", + "y_pred = []\n", + "for i in predictions:\n", + " if i >= 0.5:\n", + " y_pred.append(1)\n", + " else:\n", + " y_pred.append(0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8fJovgbnIdWz" + }, + "outputs": [], + "source": [ + "def plot_confusion_matrix(cm, classes, title='Confusion matrix', cmap=plt.cm.Blues):\n", + " cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n", + " plt.figure(figsize=(10,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)\n", + " plt.yticks(tick_marks, classes)\n", + "\n", + " fmt = '.2f'\n", + " thresh = cm.max() / 2.\n", + " for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):\n", + " plt.text(j, i, format(cm[i, j], fmt),\n", + " horizontalalignment=\"center\",\n", + " color=\"white\" if cm[i, j] > thresh else \"black\")\n", + "\n", + " plt.ylabel('True label')\n", + " plt.xlabel('Predicted label')\n", + " plt.tight_layout()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "M2hzhQXWIjs9" + }, + "outputs": [], + "source": [ + "# compute confusion matrix\n", + "cnf_matrix = confusion_matrix(y_test, y_pred)\n", + "np.set_printoptions(precision=2)\n", + "# plot normalized confusion matrix\n", + "plt.figure()\n", + "plot_confusion_matrix(cnf_matrix, classes=[\"Yes\", \"No\"], title='Normalized confusion matrix')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KhmOwFPwdmQY" + }, + "source": [ + "##Modeling using Vision Transformers(VIT)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vebPtLGJInhi" + }, + "outputs": [], + "source": [ + "learning_rate = 0.001\n", + "weight_decay = 0.0001\n", + "batch_size = 256\n", + "num_epochs = 100\n", + "image_size = 240 # We'll resize input images to this size\n", + "patch_size = 20 # Size of the patches to be extract from the input images\n", + "num_patches = (image_size // patch_size) ** 2\n", + "projection_dim = 64\n", + "num_heads = 4\n", + "transformer_units = [\n", + " projection_dim * 2,\n", + " projection_dim,\n", + "] # Size of the transformer layers\n", + "transformer_layers = 8\n", + "mlp_head_units = [2048, 1024] # Size of the dense layers of the final classifier" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PS1ydxrjdwzJ" + }, + "source": [ + "##Data Augmentation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "xVWFAQCAIuIS" + }, + "outputs": [], + "source": [ + "data_augmentation = tf.keras.Sequential(\n", + " [\n", + " layers.Normalization(),\n", + " layers.Resizing(image_size, image_size),\n", + " layers.RandomFlip(\"horizontal\"),\n", + " layers.RandomRotation(factor=0.02),\n", + " layers.RandomZoom(\n", + " height_factor=0.2, width_factor=0.2\n", + " ),\n", + " ],\n", + " name=\"data_augmentation\",\n", + ")\n", + "# Compute the mean and the variance of the training data for normalization.\n", + "data_augmentation.layers[0].adapt(x_train)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "f89u0fODd4p3" + }, + "source": [ + "##Multilayer Perceptron" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "HCQ-4L03Ix47" + }, + "outputs": [], + "source": [ + "def mlp(x, hidden_units, dropout_rate):\n", + " for units in hidden_units:\n", + " x = layers.Dense(units, activation=tf.nn.gelu)(x)\n", + " x = layers.Dropout(dropout_rate)(x)\n", + " return x" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jCmFd1QfeC4a" + }, + "source": [ + "##Patch Creation as Layer" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "vhW7Sn7FI01z" + }, + "outputs": [], + "source": [ + "class Patches(layers.Layer):\n", + " def __init__(self, patch_size):\n", + " super(Patches, self).__init__()\n", + " self.patch_size = patch_size\n", + "\n", + " def call(self, images):\n", + " batch_size = tf.shape(images)[0]\n", + " patches = tf.image.extract_patches(\n", + " images=images,\n", + " sizes=[1, self.patch_size, self.patch_size, 1],\n", + " strides=[1, self.patch_size, self.patch_size, 1],\n", + " rates=[1, 1, 1, 1],\n", + " padding=\"VALID\",\n", + " )\n", + " patch_dims = patches.shape[-1]\n", + " patches = tf.reshape(patches, [batch_size, -1, patch_dims])\n", + " return patches" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "jwUYEpFPI5wM" + }, + "outputs": [], + "source": [ + "plt.figure(figsize=(8, 8))\n", + "image = x_train[np.random.choice(range(x_train.shape[0]))]\n", + "plt.imshow(image.astype(\"uint8\"))\n", + "\n", + "resized_image = tf.image.resize(\n", + " tf.convert_to_tensor([image]), size=(image_size, image_size)\n", + ")\n", + "patches = Patches(patch_size)(resized_image)\n", + "print(f\"Image size: {image_size} X {image_size}\")\n", + "print(f\"Patch size: {patch_size} X {patch_size}\")\n", + "print(f\"Patches per image: {patches.shape[1]}\")\n", + "print(f\"Elements per patch: {patches.shape[-1]}\")\n", + "\n", + "n = int(np.sqrt(patches.shape[1]))\n", + "plt.figure(figsize=(8, 8))\n", + "for i, patch in enumerate(patches[0]):\n", + " ax = plt.subplot(n, n, i + 1)\n", + " patch_img = tf.reshape(patch, (patch_size, patch_size, 3))\n", + " plt.imshow(patch_img.numpy().astype(\"uint8\"))\n", + " plt.axis(\"off\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FSn355_aeTTE" + }, + "source": [ + "##Implementing PatchEncoder" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "okhg8naRI_78" + }, + "outputs": [], + "source": [ + "class PatchEncoder(tf.keras.layers.Layer):\n", + " def __init__(self, num_patches, projection_dim):\n", + " super(PatchEncoder, self).__init__()\n", + " self.num_patches = num_patches\n", + " self.projection = layers.Dense(units=projection_dim)\n", + " self.position_embedding = layers.Embedding(\n", + " input_dim=num_patches, output_dim=projection_dim\n", + " )\n", + "\n", + " def call(self, patch):\n", + " positions = tf.range(start=0, limit=self.num_patches, delta=1)\n", + " encoded = self.projection(patch) + self.position_embedding(positions)\n", + " return encoded" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "KUVZOrEMeeDs" + }, + "source": [ + "##Building Vision Transformers" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "pbV2Q07uJFsU" + }, + "outputs": [], + "source": [ + "def create_vit_classifier():\n", + " inputs = layers.Input(shape=(240, 240, 3))\n", + " # Augment data.\n", + " augmented = data_augmentation(inputs)\n", + " # Create patches.\n", + " patches = Patches(patch_size)(augmented)\n", + " # Encode patches.\n", + " encoded_patches = PatchEncoder(num_patches, projection_dim)(patches)\n", + "\n", + " # Create multiple layers of the Transformer block.\n", + " for _ in range(transformer_layers):\n", + " # Layer normalization 1.\n", + " x1 = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n", + " # Create a multi-head attention layer.\n", + " attention_output = layers.MultiHeadAttention(\n", + " num_heads=num_heads, key_dim=projection_dim, dropout=0.1\n", + " )(x1, x1)\n", + " # Skip connection 1.\n", + " x2 = layers.Add()([attention_output, encoded_patches])\n", + " # Layer normalization 2.\n", + " x3 = layers.LayerNormalization(epsilon=1e-6)(x2)\n", + " # MLP.\n", + " x3 = mlp(x3, hidden_units=transformer_units, dropout_rate=0.1)\n", + " # Skip connection 2.\n", + " encoded_patches = layers.Add()([x3, x2])\n", + "\n", + " # Create a [batch_size, projection_dim] tensor.\n", + " representation = layers.LayerNormalization(epsilon=1e-6)(encoded_patches)\n", + " representation = layers.Flatten()(representation)\n", + " representation = layers.Dropout(0.5)(representation)\n", + " # Add MLP.\n", + " features = mlp(representation, hidden_units=mlp_head_units, dropout_rate=0.5)\n", + " # Classify outputs.\n", + " logits = layers.Dense(2)(features)\n", + " # Create the Keras model.\n", + " model = tf.keras.Model(inputs=inputs, outputs=logits)\n", + " return model" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "PK6DZkqbJLiM" + }, + "outputs": [], + "source": [ + "def run_experiment(model):\n", + " optimizer = tfa.optimizers.AdamW(\n", + " learning_rate=learning_rate, weight_decay=weight_decay\n", + " )\n", + "\n", + " model.compile(\n", + " optimizer=optimizer,\n", + " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " metrics=[\n", + " tf.keras.metrics.SparseCategoricalAccuracy(name=\"accuracy\"),\n", + " tf.keras.metrics.SparseTopKCategoricalAccuracy(5, name=\"top-5-accuracy\"),\n", + " ],\n", + " )\n", + "\n", + " checkpoint_filepath = \"/tmp/checkpoint\"\n", + " checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n", + " checkpoint_filepath,\n", + " monitor=\"val_accuracy\",\n", + " save_best_only=True,\n", + " save_weights_only=True,\n", + " )\n", + "\n", + " history = model.fit(\n", + " x=x_train,\n", + " y=y_train,\n", + " batch_size=batch_size,\n", + " epochs=num_epochs,\n", + " validation_data=(x_test, y_test),\n", + " callbacks=[checkpoint_callback],\n", + " )\n", + "\n", + " model.load_weights(checkpoint_filepath)\n", + " _, accuracy, top_5_accuracy = model.evaluate(x_test, y_test)\n", + " print(f\"Test accuracy: {round(accuracy * 100, 2)}%\")\n", + " print(f\"Test top 5 accuracy: {round(top_5_accuracy * 100, 2)}%\")\n", + "\n", + " return history" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "njMr7v_nJRLE" + }, + "outputs": [], + "source": [ + "vit_classifier = create_vit_classifier()\n", + "vit_history = run_experiment(vit_classifier)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W7NzZzJjexAa" + }, + "source": [ + "##Model Evaluation" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "sdBMKhctJUBy" + }, + "outputs": [], + "source": [ + "plt.figure(figsize=(20,10))\n", + "plt.subplot(1, 2, 1)\n", + "plt.suptitle('Optimizer : Adam', fontsize=10)\n", + "plt.ylabel('Loss', fontsize=16)\n", + "plt.plot(vit_history.history['loss'], label='Training Loss')\n", + "plt.plot(vit_history.history['val_loss'], label='Validation Loss')\n", + "plt.legend(loc='upper right')\n", + "\n", + "plt.subplot(1, 2, 2)\n", + "plt.ylabel('Accuracy', fontsize=16)\n", + "plt.plot(vit_history.history['accuracy'], label='Training Accuracy')\n", + "plt.plot(vit_history.history['val_accuracy'], label='Validation Accuracy')\n", + "plt.legend(loc='lower right')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "wUii7f_5Jtds" + }, + "outputs": [], + "source": [ + "# compute predictions\n", + "vit_predictions = vit_classifier.predict(x_test)\n", + "vit_y_pred = [np.argmax(probas) for probas in vit_predictions]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "nepa9AXMJw7d" + }, + "outputs": [], + "source": [ + "# compute confusion matrix\n", + "cnf_matrix = confusion_matrix(y_test, vit_y_pred)\n", + "np.set_printoptions(precision=2)\n", + "# plot normalized confusion matrix\n", + "plt.figure()\n", + "plot_confusion_matrix(cnf_matrix, classes=[\"Yes\", \"No\"], title='Normalized confusion matrix')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "HT24ePkUJ0yG" + }, + "outputs": [], + "source": [ + "model_json = model.to_json()\n", + "with open(\"model.json\", \"w\") as json_file:\n", + " json_file.write(model_json)\n", + "# serialize weights to HDF5\n", + "model.save_weights(\"model.h5\")\n", + "print(\"Saved model to disk\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "2VLCg0QL5CVv" + }, + "outputs": [], + "source": [ + "json_file = open('model.json', 'r')\n", + "loaded_model_json = json_file.read()\n", + "json_file.close()\n", + "loaded_model = model_from_json(loaded_model_json)\n", + "# load weights into new model\n", + "loaded_model.load_weights(\"model.h5\")\n", + "print(\"Loaded model from disk\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "AxnKmLyS69Le" + }, + "outputs": [], + "source": [ + "loaded_model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "MbircsHC7oL1" + }, + "outputs": [], + "source": [ + "model.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "EI5kt9H_sFpG" + }, + "outputs": [], + "source": [ + "model_1.summary()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "5xH6yEpl--tF" + }, + "outputs": [], + "source": [ + "test_yes_image=cv2.imread('/content/gdrive/My Drive/BTD/brain_tumor_dataset/yes/Y7.jpg')\n", + "test_yes_image=Image.fromarray(test_yes_image,'RGB')\n", + "test_yes_image=test_yes_image.resize((240,240))\n", + "test_yes_imagearr=np.array(test_yes_image)\n", + "test_yes_imagearr=test_yes_imagearr.reshape(1,240,240,3)\n", + "test_yes_imagearr.shape\n", + "test_predict=loaded_model.predict(test_yes_imagearr)\n", + "print(\"The accuracy of the given image is: \", test_predict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Gmsz796tCLR2" + }, + "outputs": [], + "source": [ + "test_yes_image=cv2.imread('/content/gdrive/My Drive/BTD/brain_tumor_dataset/No11.jpg')\n", + "test_yes_image=Image.fromarray(test_yes_image,'RGB')\n", + "test_yes_image=test_yes_image.resize((240,240))\n", + "test_yes_imagearr=np.array(test_yes_image)\n", + "test_yes_imagearr=test_yes_imagearr.reshape(1,240,240,3)\n", + "test_yes_imagearr.shape\n", + "test_predict=loaded_model.predict(test_yes_imagearr)\n", + "print(test_predict)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "VsfhWJQYCuNI" + }, + "outputs": [], + "source": [ + "test_yes_image=cv2.imread('/content/gdrive/My Drive/BTD/brain_tumor_dataset/Y12.jpg')\n", + "test_yes_image=Image.fromarray(test_yes_image,'RGB')\n", + "test_yes_image=test_yes_image.resize((240,240))\n", + "test_yes_imagearr=np.array(test_yes_image)\n", + "test_yes_imagearr=test_yes_imagearr.reshape(1,240,240,3)\n", + "test_yes_imagearr.shape\n", + "test_predict=loaded_model.predict(test_yes_imagearr)\n", + "print(test_predict)" + ] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "bvYfGqFNCsmD" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "IBLrCR6HCQst" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "gpuType": "T4", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file