{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Brain Tumor MRI Detection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install tensorflow" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n", "from tensorflow.keras.models import load_model\n", "from tensorflow.keras.preprocessing import image\n", "import os\n", "import numpy as np\n", "from PIL import Image" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Current Directory: e:\\Github Projects\\BrainTumorMRIDetection\n" ] } ], "source": [ "current_dir = os.getcwd()\n", "print (\"Current Directory: \" + current_dir)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "train_dir = os.path.join(current_dir, 'Testing')\n", "val_dir = os.path.join(current_dir, 'Training')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Define the target size and batch size\n", "target_size = (1250, 1250)\n", "batch_size = 32\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 394 images belonging to 4 classes.\n", "Found 2870 images belonging to 4 classes.\n" ] } ], "source": [ "# Define the training and validation data generators\n", "train_datagen = ImageDataGenerator(rescale=1./255)\n", "train_generator = train_datagen.flow_from_directory(\n", " train_dir,\n", " target_size=target_size,\n", " batch_size=batch_size,\n", " class_mode='categorical')\n", "\n", "val_datagen = ImageDataGenerator(rescale=1./255)\n", "val_generator = val_datagen.flow_from_directory(\n", " val_dir,\n", " target_size=target_size,\n", " batch_size=batch_size,\n", " class_mode='categorical')\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Define the model\n", "model = tf.keras.models.Sequential([\n", " tf.keras.layers.Conv2D(32, (3, 3), activation='relu',\n", " input_shape=(target_size[0], target_size[1], 3)),\n", " tf.keras.layers.MaxPooling2D((2, 2)),\n", " tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n", " tf.keras.layers.MaxPooling2D((2, 2)),\n", " tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),\n", " tf.keras.layers.MaxPooling2D((2, 2)),\n", " tf.keras.layers.Flatten(),\n", " tf.keras.layers.Dense(128, activation='relu'),\n", " tf.keras.layers.Dropout(0.5),\n", " tf.keras.layers.Dense(train_generator.num_classes, activation='softmax')\n", "])\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Compile the model\n", "model.compile(optimizer='adam',\n", " loss='categorical_crossentropy',\n", " metrics=['accuracy'])\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/15\n", "12/12 [==============================] - 753s 64s/step - loss: 17.4665 - accuracy: 0.2431 - val_loss: 1.4717 - val_accuracy: 0.2883\n", "Epoch 2/15\n", "12/12 [==============================] - 694s 60s/step - loss: 1.1907 - accuracy: 0.4779 - val_loss: 1.4602 - val_accuracy: 0.2798\n", "Epoch 3/15\n", "12/12 [==============================] - 704s 61s/step - loss: 0.8829 - accuracy: 0.6575 - val_loss: 1.5343 - val_accuracy: 0.2791\n", "Epoch 4/15\n", "12/12 [==============================] - 697s 61s/step - loss: 0.4633 - accuracy: 0.8398 - val_loss: 1.7458 - val_accuracy: 0.3206\n", "Epoch 5/15\n", "12/12 [==============================] - 690s 60s/step - loss: 0.2428 - accuracy: 0.9309 - val_loss: 2.3506 - val_accuracy: 0.3536\n", "Epoch 6/15\n", "12/12 [==============================] - 698s 61s/step - loss: 0.1575 - accuracy: 0.9558 - val_loss: 2.2596 - val_accuracy: 0.3588\n", "Epoch 7/15\n", "12/12 [==============================] - 694s 61s/step - loss: 0.1069 - accuracy: 0.9696 - val_loss: 1.9421 - val_accuracy: 0.3272\n", "Epoch 8/15\n", "12/12 [==============================] - 694s 61s/step - loss: 0.0688 - accuracy: 0.9807 - val_loss: 3.2596 - val_accuracy: 0.3711\n", "Epoch 9/15\n", "12/12 [==============================] - 685s 61s/step - loss: 0.1024 - accuracy: 0.9696 - val_loss: 2.0157 - val_accuracy: 0.3722\n", "Epoch 10/15\n", "12/12 [==============================] - 699s 61s/step - loss: 0.0556 - accuracy: 0.9890 - val_loss: 2.7399 - val_accuracy: 0.3430\n", "Epoch 11/15\n", "12/12 [==============================] - 696s 61s/step - loss: 0.0561 - accuracy: 0.9862 - val_loss: 2.4300 - val_accuracy: 0.3831\n", "Epoch 12/15\n", "12/12 [==============================] - 684s 60s/step - loss: 0.0320 - accuracy: 0.9917 - val_loss: 2.5653 - val_accuracy: 0.3511\n", "Epoch 13/15\n", "12/12 [==============================] - 681s 61s/step - loss: 0.0493 - accuracy: 0.9890 - val_loss: 2.8736 - val_accuracy: 0.3515\n", "Epoch 14/15\n", "12/12 [==============================] - 689s 60s/step - loss: 0.0213 - accuracy: 0.9917 - val_loss: 3.0044 - val_accuracy: 0.3704\n", "Epoch 15/15\n", "12/12 [==============================] - 692s 62s/step - loss: 0.0407 - accuracy: 0.9917 - val_loss: 2.8754 - val_accuracy: 0.3838\n" ] } ], "source": [ "# Train the model\n", "history = model.fit(\n", " train_generator,\n", " steps_per_epoch=train_generator.samples//batch_size,\n", " epochs=15,\n", " validation_data=val_generator,\n", " validation_steps=val_generator.samples//batch_size)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Saving The Model" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Save the trained model in the current directory\n", "model.save(os.path.join(current_dir, 'model.h5'))" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }