{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "db88e11e-4a21-4288-a233-058e155843eb", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import os\n", "import pandas as pd\n", "import numpy as np\n", "import seaborn as sns\n", "import matplotlib.pyplot as plt \n", "#Matplot Images\n", "import matplotlib.image as mpimg\n", "# Tensflor and Keras Layer and Model and Optimize and Loss\n", "import tensorflow as tf\n", "from tensorflow import keras\n", "from keras import Sequential\n", "from keras.layers import *\n", "from tensorflow.keras.losses import BinaryCrossentropy\n", "#Kernel Intilizer \n", "from sklearn.preprocessing import LabelEncoder\n", "# import tensorflow_hub as hub\n", "from tensorflow.keras.optimizers.legacy import Adam, Adamax\n", "#PreTrained Model\n", "from tensorflow.keras.applications import *\n", "#Early Stopping\n", "from tensorflow.keras.callbacks import EarlyStopping\n", "# Warnings Remove \n", "import warnings \n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "markdown", "id": "e02b62cc-5b40-4163-9f50-fd7c22f81ac7", "metadata": {}, "source": [ "## Loading the Data\n", "\n", "For the train data" ] }, { "cell_type": "code", "execution_count": 4, "id": "39be046d-d5a9-49a2-b03b-9fc0e87c8772", "metadata": {}, "outputs": [], "source": [ "directory = \"/Users/sourasishmondal/Desktop/chest_xray/train\"\n", "\n", "filepath = []\n", "label = []\n", "\n", "folds = os.listdir(directory)\n", "\n", "for fold in folds:\n", " f_path = os.path.join(directory, fold)\n", " imgs = os.listdir(f_path)\n", "\n", " for img in imgs:\n", " img_path = os.path.join(f_path, img)\n", " filepath.append(img_path)\n", " label.append(fold)\n", "\n", "# concat data paths with label\n", "filePathSeries = pd.Series(filepath, name='filepath')\n", "labelPathSeries = pd.Series(label, name='label')\n", "train_path = pd.concat([filePathSeries, labelPathSeries], axis=1) # concat filepath and folder label\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "44dd728d-339f-47fd-8823-672869a50548", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "1 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "2 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "3 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "4 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", " ... \n", "5211 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "5212 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "5213 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "5214 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "5215 /Users/sourasishmondal/Desktop/chest_xray/trai...\n", "Name: filepath, Length: 5216, dtype: object" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "filePathSeries" ] }, { "cell_type": "code", "execution_count": 6, "id": "954db609-340d-4026-82e6-d0e5a8fe4116", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 PNEUMONIA\n", "1 PNEUMONIA\n", "2 PNEUMONIA\n", "3 PNEUMONIA\n", "4 PNEUMONIA\n", " ... \n", "5211 NORMAL\n", "5212 NORMAL\n", "5213 NORMAL\n", "5214 NORMAL\n", "5215 NORMAL\n", "Name: label, Length: 5216, dtype: object" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "labelPathSeries" ] }, { "cell_type": "code", "execution_count": 7, "id": "68d0160f-ea45-4bff-915f-530c8d58774c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | filepath | \n", "label | \n", "
---|---|---|
0 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "PNEUMONIA | \n", "
1 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "PNEUMONIA | \n", "
2 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "PNEUMONIA | \n", "
3 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "PNEUMONIA | \n", "
4 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "PNEUMONIA | \n", "
... | \n", "... | \n", "... | \n", "
5211 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "NORMAL | \n", "
5212 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "NORMAL | \n", "
5213 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "NORMAL | \n", "
5214 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "NORMAL | \n", "
5215 | \n", "/Users/sourasishmondal/Desktop/chest_xray/trai... | \n", "NORMAL | \n", "
5216 rows × 2 columns
\n", "\n", " | filepath | \n", "label | \n", "
---|---|---|
43 | \n", "/Users/sourasishmondal/Desktop/chest_xray/test... | \n", "PNEUMONIA | \n", "
287 | \n", "/Users/sourasishmondal/Desktop/chest_xray/test... | \n", "PNEUMONIA | \n", "
531 | \n", "/Users/sourasishmondal/Desktop/chest_xray/test... | \n", "NORMAL | \n", "
111 | \n", "/Users/sourasishmondal/Desktop/chest_xray/test... | \n", "PNEUMONIA | \n", "
609 | \n", "/Users/sourasishmondal/Desktop/chest_xray/test... | \n", "NORMAL | \n", "