Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanjushekhawat submission: Predicting Pneumonia using CNN #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.

This file was deleted.

Empty file.

This file was deleted.

Binary file added [email protected]/report.pdf
Binary file not shown.
153 changes: 153 additions & 0 deletions [email protected]/sanju_cnn.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow\n",
"import tensorflow.keras\n",
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Convolution2D\n",
"from tensorflow.keras.layers import MaxPooling2D\n",
"from tensorflow.keras.layers import Flatten\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.layers import Reshape\n",
"\n",
"classifier = Sequential()\n",
"\n",
"classifier.add(Convolution2D(32,(3,3),input_shape=(64,64,3),activation='relu'))\n",
"classifier.add(MaxPooling2D(pool_size=(2,2)))\n",
"\n",
"classifier.add(Convolution2D(32,(3,3),input_shape=(64,64,3),activation='relu'))\n",
"classifier.add(MaxPooling2D(pool_size=(2,2)))\n",
"\n",
"classifier.add(Convolution2D(32,(3,3),input_shape=(64,64,3),activation='relu'))\n",
"classifier.add(MaxPooling2D(pool_size=(2,2)))\n",
"\n",
"\n",
"classifier.add(Convolution2D(32,(3,3),input_shape=(64,64,3),activation='relu'))\n",
"classifier.add(MaxPooling2D(pool_size=(2,2)))\n",
"\n",
"\n",
"classifier.add(Flatten())\n",
"classifier.add(Dense(units=128,activation='relu'))\n",
"classifier.add(Dense(units=1,activation='sigmoid'))\n",
"classifier.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])\n",
"\n",
"print(classifier.summary())\n",
"\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"\n",
"train_datagen=ImageDataGenerator(rescale=1/1.255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)\n",
"test_datagen = ImageDataGenerator(rescale=1./255)\n",
"training_set = train_datagen.flow_from_directory(\n",
" 'C:/Users/rishi/OneDrive/Desktop/chest_xray/chest_xray/train',\n",
" target_size=(64, 64),\n",
" batch_size=32,\n",
" class_mode='binary')\n",
"\n",
"test_set = train_datagen.flow_from_directory(\n",
" 'C:/Users/rishi/OneDrive/Desktop/chest_xray/chest_xray/test',\n",
" target_size=(64, 64),\n",
" batch_size=32,\n",
" class_mode='binary')\n",
"\n",
"val_set = train_datagen.flow_from_directory(\n",
" 'C:/Users/rishi/OneDrive/Desktop/chest_xray/chest_xray/val',\n",
" target_size=(64, 64),\n",
" batch_size=32,\n",
" class_mode='binary')\n",
"\n",
"cl=classifier.fit_generator(\n",
" training_set,\n",
" steps_per_epoch=163,\n",
" epochs=10,\n",
" validation_data=test_set,\n",
" validation_steps=624/32)\n",
" \n",
"\n",
"from sklearn.metrics import classification_report, confusion_matrix\n",
"\n",
"\n",
"\n",
"\n",
"labels = test_set.class_indices\n",
"\n",
"labels = {v: k for k, v in labels.items()}\n",
"\n",
"classes = list(labels.values())\n",
"\n",
"y_pred = classifier.predict(test_set)\n",
"\n",
"y_pred =(y_pred>0.5)\n",
"\n",
"print(confusion_matrix(test_set.classes, y_pred))\n",
"\n",
"print(classification_report(test_set.classes, y_pred, target_names=classes))\n",
"print(labels)\n",
"\n",
"\n",
"test_accuracy=classifier.evaluate_generator(test_set,624)\n",
"print(\"Test accuracy:\",test_accuracy[1]*100,'%')\n",
"\n",
"\n",
"from sklearn.metrics import accuracy_score\n",
"print(accuracy_score(test_set.classes, y_pred))\n",
"\n",
"from sklearn.metrics import recall_score\n",
"recall_score(test_set.classes, y_pred)\n",
"\n",
"from sklearn.metrics import precision_score\n",
"precision_score(test_set.classes, y_pred)\n",
"\n",
"from sklearn.metrics import f1_score\n",
"f1_score(test_set.classes, y_pred)\n",
"\n",
"\n",
"\n",
"\n",
"import matplotlib.pyplot as plt\n",
"\n",
"plt.plot(cl.history['acc'])\n",
"plt.plot(cl.history['val_acc'])\n",
"plt.title('Model Accuracy')\n",
"plt.ylabel('Accuracy')\n",
"plt.xlabel('Epoch')\n",
"plt.legend(['training_set', 'test_set'], loc='upper left')\n",
"plt.show()\n",
"\n",
"#Loss\n",
"plt.plot(cl.history['loss'])\n",
"plt.plot(cl.history['val_loss'])\n",
"plt.title('Loss')\n",
"plt.ylabel('Loss')\n",
"plt.xlabel('Epoch')\n",
"plt.legend(['training_set', 'test_set'], loc='upper left')\n",
"plt.show()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}