-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCNN_COVID_Classification.py
162 lines (122 loc) · 5.17 KB
/
CNN_COVID_Classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 19:37:59 2020
This is CNN model used for COVID classification
@author: cdnguyen
"""
import os
import numpy as np
import cv2
from random import shuffle
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.utils import plot_model
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau , ModelCheckpoint
from collections import Counter
import efficientnet.tfkeras as efn
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
import numpy as np
import pandas as pd
from keras.preprocessing import image
import os
# scikit-learn k-fold cross-validation
from sklearn.model_selection import KFold
import torch.nn as nn
from keras.optimizers import SGD, Adam
from tensorflow import keras
from tensorflow.keras import layers
TrianImage="dataset_train_New"
TestImage="dataset_train_New"
Normalimages = os.listdir(TrianImage + "/normal")
Pneumonaimages = os.listdir(TrianImage + "/pneumonia_bac")
COVID19images = os.listdir(TrianImage + "/covid")
print(len(Normalimages), len(Pneumonaimages), len(COVID19images))
NUM_TRAINING_IMAGES = len(Normalimages) + len(Pneumonaimages) + len(COVID19images)
print(NUM_TRAINING_IMAGES)
image_size = 32
BATCH_SIZE = 8
epochs = 10
STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
data_path = 'dataset_train_New'
data_path1 = 'dataset_test_New'
train_datagen = ImageDataGenerator(rescale = 1./255,
zoom_range = 0.2,
rotation_range=15,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory(data_path,
target_size = (image_size, image_size),
batch_size = BATCH_SIZE,
class_mode = 'categorical',
shuffle=True)
testing_set = test_datagen.flow_from_directory(data_path1,
target_size = (image_size, image_size),
batch_size = BATCH_SIZE,
class_mode = 'categorical',
shuffle = True)
def define_model1():
model = Sequential()
model.add(Conv2D(32, (3, 3),
input_shape = (32, 32, 3),
activation = 'relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(3, activation='softmax'))
# compile model
opt = SGD(lr=0.001, momentum=0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
return model
def define_model():
#Initialing the CNN
classifier = Sequential()
#Step 1 - Convolution
#Extract features from the images
classifier.add(Conv2D(32, (3, 3),
input_shape = (32, 32, 3),
activation = 'relu'))
#Step 2 - Pooling
classifier.add(MaxPooling2D(pool_size = (2, 2)))
#Step 3 - Flattening
classifier.add(Flatten())
#Step 4 - Full connection
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.5))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout(0.5))
#classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.add(Dense(3,activation="softmax"))
#Compiling the CNN
classifier.compile(optimizer = 'Adam',
loss = 'categorical_crossentropy', #'binary_crossentropy',
metrics = ['accuracy'])
return classifier
model = define_model1()
history = model.fit(training_set, validation_data = testing_set,
steps_per_epoch=10,
epochs=epochs, verbose= 1, batch_size=16)
from sklearn.metrics import classification_report, confusion_matrix
Y_pred = model.predict(testing_set, steps=10)
predicted_classes = np.argmax(Y_pred, axis=1)
true_classes = testing_set.classes
class_labels = list(testing_set.class_indices.keys())
report = classification_report(true_classes, predicted_classes, target_names=class_labels)
print(report)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
confusion_matrix= confusion_matrix(testing_set.classes, y_pred)
print(confusion_matrix)
testing_set.class_indices.keys()
print(testing_set.class_indices.keys())
#To get better visual of the confusion matrix:
print('Classification Report')