-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCOVID19_Detector.py
93 lines (73 loc) · 2.63 KB
/
COVID19_Detector.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
# -*- coding: utf-8 -*-
"""COVID-19 Detector
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/dgeorge1000/COVID-19-CNN-Image-Detection-from-Lung-X-Rays/blob/main/COVID19_Detector.ipynb
"""
# Dataset #1: http://cb.lk/covid_19
# Dataset #2: https://www.dropbox.com/sh/7hrnecm53hk0ih3/AABsfKTooQP0Tc3QddOQAfZJa?dl=0
# download the dataset from the Dropbox link
!wget http://cb.lk/covid_19
# unzip the folder to get each image for local access
!unzip covid_19
# paths for both the training dataset and the validation set
# training will be used for the CNN to learn how to differentiate between COVID and non COVID lung images
# validation will be used to test the CNN on data not seen to see its accuracy
TRAIN_PATH = "CovidDataset/Train"
VAL_PATH = "CovidDataset/Val"
# various libraries and imports needed
import numpy as np
import matplotlib.pyplot as plt
import keras
from keras.layers import *
from keras.models import *
from keras.preprocessing import image
# CNN Based Model using Keras
model = Sequential()
model.add(Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(224,224,3)))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(64,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128,(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(64,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss=keras.losses.binary_crossentropy,optimizer='adam',metrics=['accuracy'])
# get the general information about the CNN created
model.summary()
# Train from scratch
train_datagen = image.ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True,
)
test_dataset = image.ImageDataGenerator(rescale=1./255)
# using generator saves memory
train_generator = train_datagen.flow_from_directory(
'CovidDataset/Train',
target_size = (224,224),
batch_size = 32,
class_mode = 'binary')
# 2 classifications
train_generator.class_indices
validation_generator = test_dataset.flow_from_directory(
'CovidDataset/Val',
target_size = (224,224),
batch_size = 32,
class_mode = 'binary')
# CNN will begin training and testing on the given data
# "accuracy" and "val_accuracy" will show how accurate the CNN is
hist = model.fit_generator(
train_generator,
steps_per_epoch=5,
epochs = 8,
validation_data = validation_generator,
validation_steps=2
)