-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup2.py
113 lines (93 loc) · 3.64 KB
/
setup2.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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 7 13:48:50 2020
"""
from keras.models import load_model
from keras import optimizers
import tensorflow as tf
import numpy as np
import keras
from keras import backend as k
from keras.models import Sequential
from keras.layers import Activation
from keras.layers.core import Dense, Flatten,Dropout
from keras.optimizers import Adam
from keras.optimizers import SGD
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint, EarlyStopping
from keras.layers.convolutional import *
from keras.layers.convolutional import Conv2D,MaxPooling2D
from matplotlib import pyplot as plt
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt
import h5py
from sklearn.metrics import classification_report
from IPython import get_ipython
ipy = get_ipython()
if ipy is not None:
ipy.run_line_magic('matplotlib', 'inline')
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config)
#Without Preprocesssing
train_path='dataset/train'
test_path='dataset/test'
valid_path='dataset/val'
'''
#Preprocessed with Clahe
train_path='datasetC/train'
test_path='datasetC/test'
valid_path='datasetC/val'
'''
nb_train_samples=3514 #60%of dataset
nb_valid_samples=1172 #20% of dataset
nb_test_samples=1170 #20% of dataset
img_width,img_height=150,150
epochs=100
batch_size=32
if k.image_data_format()=='channels_first':
input_shape=(3,img_width,img_height)
else:
input_shape=(img_width,img_height,3)
optimize=ImageDataGenerator(
rotation_range=90,
horizontal_flip=True,
fill_mode='nearest',
shear_range=0.2,
zoom_range=0.2,
height_shift_range=0.2,
width_shift_range=0.2,
rescale=1./255)
optimize_test=ImageDataGenerator(
rescale=1./255)
train_batches=optimize.flow_from_directory(train_path,target_size=(img_width,img_height),classes=['Normal','Pneumonia'],batch_size=batch_size,shuffle=True)
valid_batches=optimize.flow_from_directory(valid_path,target_size=(img_width,img_height),classes=['Normal','Pneumonia'],batch_size=12,shuffle=True)
test_batches=optimize_test.flow_from_directory(test_path,target_size=((img_width,img_height),classes=['Normal','Pneumonia'],batch_size=1170,shuffle=False)
#create model
model = Sequential()#add model layers
model.add(Conv2D(32,(3,3),activation='relu',padding='same',input_shape=(img_width,img_height,3)))
model.add(Conv2D(32,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(2, activation='softmax'))
model.summary()
adam = optimizers.Adam(lr=0.001)
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
checkpoint= ModelCheckpoint("model.h5",monitor='val_accuracy',verbose=1,save_best_only=True,save_weights_only=False,mode='auto',period=1)
early=EarlyStopping(monitor='val_accuracy', min_delta=0,patience=5,verbose=1,mode='auto')
hist=model.fit_generator(train_batches,steps_per_epoch=nb_train_samples//batch_size,
validation_data=valid_batches,validation_steps=nb_valid_samples//12,epochs=epochs,callbacks=[checkpoint,early])
print(hist.history.keys())
plt.plot(hist.history["accuracy"])
plt.plot(hist.history['val_accuracy'])
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title("model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","loss","Validation Loss"])
plt.show()