{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":1426603,"sourceType":"datasetVersion","datasetId":835414}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-25T06:07:20.739255Z","iopub.execute_input":"2023-12-25T06:07:20.740045Z","iopub.status.idle":"2023-12-25T06:07:21.674269Z","shell.execute_reply.started":"2023-12-25T06:07:20.740008Z","shell.execute_reply":"2023-12-25T06:07:21.673094Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import keras\nfrom keras.models import Sequential\nimport numpy as np\nfrom keras.layers import Conv2D,Flatten,Dense,MaxPooling2D,Dropout,BatchNormalization\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:07:21.676002Z","iopub.execute_input":"2023-12-25T06:07:21.676305Z","iopub.status.idle":"2023-12-25T06:07:21.681083Z","shell.execute_reply.started":"2023-12-25T06:07:21.676278Z","shell.execute_reply":"2023-12-25T06:07:21.680065Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import ipywidgets as widgets\nimport io\nfrom PIL import Image\nimport tqdm\nfrom sklearn.model_selection import train_test_split\nimport cv2\nfrom sklearn.utils import shuffle\nimport tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:07:21.682451Z","iopub.execute_input":"2023-12-25T06:07:21.682819Z","iopub.status.idle":"2023-12-25T06:07:21.693298Z","shell.execute_reply.started":"2023-12-25T06:07:21.682789Z","shell.execute_reply":"2023-12-25T06:07:21.692368Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train = []\nY_train = []\nimage_size = 150\nlabels = ['NORMAL','PNEUMONIA']\nfor i in labels:\n    folderPath = os.path.join('/kaggle/input/labeled-chest-xray-images/chest_xray/train',i)\n    for j in os.listdir(folderPath):\n        img = cv2.imread(os.path.join(folderPath,j))\n        img = cv2.resize(img,(image_size,image_size))\n        X_train.append(img)\n        Y_train.append(i)\nfor i in labels:\n    folderPath = os.path.join('/kaggle/input/labeled-chest-xray-images/chest_xray/test',i)\n    for j in os.listdir(folderPath):\n        img = cv2.imread(os.path.join(folderPath,j))\n        img = cv2.resize(img,(image_size,image_size))\n        X_train.append(img)\n        Y_train.append(i)\n\nX_train = np.array(X_train)\nY_train = np.array(Y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:07:21.695406Z","iopub.execute_input":"2023-12-25T06:07:21.696187Z","iopub.status.idle":"2023-12-25T06:08:33.931802Z","shell.execute_reply.started":"2023-12-25T06:07:21.696162Z","shell.execute_reply":"2023-12-25T06:08:33.930839Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,Y_train = shuffle(X_train,Y_train,random_state=101)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:08:33.933029Z","iopub.execute_input":"2023-12-25T06:08:33.933326Z","iopub.status.idle":"2023-12-25T06:08:34.055816Z","shell.execute_reply.started":"2023-12-25T06:08:33.933302Z","shell.execute_reply":"2023-12-25T06:08:34.054945Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_test,y_train,y_test = train_test_split(X_train,Y_train,test_size=0.1,random_state=101)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:08:34.057134Z","iopub.execute_input":"2023-12-25T06:08:34.057462Z","iopub.status.idle":"2023-12-25T06:08:34.17514Z","shell.execute_reply.started":"2023-12-25T06:08:34.057436Z","shell.execute_reply":"2023-12-25T06:08:34.174186Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train_new = []\nfor i in y_train:\n    y_train_new.append(labels.index(i))\ny_train=y_train_new\ny_train = tf.keras.utils.to_categorical(y_train)\n\ny_test_new = []\nfor i in y_test:\n    y_test_new.append(labels.index(i))\ny_test=y_test_new\ny_test = tf.keras.utils.to_categorical(y_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:09:12.54246Z","iopub.execute_input":"2023-12-25T06:09:12.543164Z","iopub.status.idle":"2023-12-25T06:09:12.653918Z","shell.execute_reply.started":"2023-12-25T06:09:12.543132Z","shell.execute_reply":"2023-12-25T06:09:12.653163Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(32,kernel_size=(3,3),padding='valid',activation='relu',input_shape=(150,150,3)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Conv2D(64,kernel_size=(3,3),padding='valid',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Conv2D(128,kernel_size=(3,3),padding='valid',activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.1))\nmodel.add(Dense(2,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:09:30.193531Z","iopub.execute_input":"2023-12-25T06:09:30.194363Z","iopub.status.idle":"2023-12-25T06:09:33.152848Z","shell.execute_reply.started":"2023-12-25T06:09:30.194329Z","shell.execute_reply":"2023-12-25T06:09:33.151981Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:09:47.466395Z","iopub.execute_input":"2023-12-25T06:09:47.466761Z","iopub.status.idle":"2023-12-25T06:09:47.511372Z","shell.execute_reply.started":"2023-12-25T06:09:47.466723Z","shell.execute_reply":"2023-12-25T06:09:47.508693Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:10:08.727257Z","iopub.execute_input":"2023-12-25T06:10:08.727603Z","iopub.status.idle":"2023-12-25T06:10:08.745562Z","shell.execute_reply.started":"2023-12-25T06:10:08.727574Z","shell.execute_reply":"2023-12-25T06:10:08.74448Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train,y_train,epochs=10,validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:10:28.908237Z","iopub.execute_input":"2023-12-25T06:10:28.908581Z","iopub.status.idle":"2023-12-25T06:11:06.064187Z","shell.execute_reply.started":"2023-12-25T06:10:28.908552Z","shell.execute_reply":"2023-12-25T06:11:06.06305Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:11:28.704395Z","iopub.execute_input":"2023-12-25T06:11:28.704995Z","iopub.status.idle":"2023-12-25T06:11:28.797705Z","shell.execute_reply.started":"2023-12-25T06:11:28.70496Z","shell.execute_reply":"2023-12-25T06:11:28.796724Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nepochs = range(len(acc))\nfig = plt.figure(figsize=(14,7))\nplt.plot(epochs,acc,'r',label=\"Training Accuracy\")\nplt.plot(epochs,val_acc,'b',label=\"Validation Accuracy\")\nplt.legend(loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:11:38.804369Z","iopub.execute_input":"2023-12-25T06:11:38.804726Z","iopub.status.idle":"2023-12-25T06:11:39.050687Z","shell.execute_reply.started":"2023-12-25T06:11:38.80469Z","shell.execute_reply":"2023-12-25T06:11:39.049816Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(len(loss))\nfig = plt.figure(figsize=(14,7))\nplt.plot(epochs,loss,'r',label=\"Training loss\")\nplt.plot(epochs,val_loss,'b',label=\"Validation loss\")\nplt.legend(loc='upper left')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:11:50.524306Z","iopub.execute_input":"2023-12-25T06:11:50.525125Z","iopub.status.idle":"2023-12-25T06:11:50.965581Z","shell.execute_reply.started":"2023-12-25T06:11:50.525092Z","shell.execute_reply":"2023-12-25T06:11:50.964718Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/labeled-chest-xray-images/chest_xray/test/PNEUMONIA/BACTERIA-1514320-0001.jpeg')\nimg = cv2.resize(img,(150,150))\nimg_array = np.array(img)\nimg_array.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:12:38.055069Z","iopub.execute_input":"2023-12-25T06:12:38.056146Z","iopub.status.idle":"2023-12-25T06:12:38.072892Z","shell.execute_reply.started":"2023-12-25T06:12:38.05611Z","shell.execute_reply":"2023-12-25T06:12:38.072005Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_array = img_array.reshape(1,150,150,3)\nimg_array.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:12:50.581874Z","iopub.execute_input":"2023-12-25T06:12:50.582233Z","iopub.status.idle":"2023-12-25T06:12:50.588536Z","shell.execute_reply.started":"2023-12-25T06:12:50.582203Z","shell.execute_reply":"2023-12-25T06:12:50.587649Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nimg = image.load_img('/kaggle/input/labeled-chest-xray-images/chest_xray/test/PNEUMONIA/BACTERIA-1514320-0001.jpeg')\nplt.imshow(img,interpolation='nearest')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:13:21.357123Z","iopub.execute_input":"2023-12-25T06:13:21.357773Z","iopub.status.idle":"2023-12-25T06:13:21.665544Z","shell.execute_reply.started":"2023-12-25T06:13:21.35774Z","shell.execute_reply":"2023-12-25T06:13:21.664631Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"a=model.predict(img_array)\nindices = a.argmax()\nindices","metadata":{"execution":{"iopub.status.busy":"2023-12-25T06:13:43.825963Z","iopub.execute_input":"2023-12-25T06:13:43.826654Z","iopub.status.idle":"2023-12-25T06:13:44.068357Z","shell.execute_reply.started":"2023-12-25T06:13:43.826625Z","shell.execute_reply":"2023-12-25T06:13:44.067493Z"},"trusted":true},"outputs":[],"execution_count":null}]}