-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnnmodel.py
More file actions
112 lines (108 loc) · 3.96 KB
/
cnnmodel.py
File metadata and controls
112 lines (108 loc) · 3.96 KB
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
from skimage import io,color
from skimage.transform import resize
import numpy as np
from keras import layers
from keras.layers import Input, Add, Dense,Dropout,Concatenate, Activation,merge , ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, GlobalAveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.initializers import he_uniform
import scipy.misc
from keras.optimizers import *
from matplotlib import pyplot as plt
from numpy import array
from matplotlib.pyplot import imshow,savefig
import keras.backend as K
from keras.applications import densenet
from itertools import chain
from keras.preprocessing.image import ImageDataGenerator
import json
import tensorflow as tf
import sklearn
from sklearn.metrics import precision_recall_fscore_support,classification_report,confusion_matrix,roc_auc_score,confusion_matrix, f1_score, precision_score, recall_score
from keras.callbacks import Callback
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
K.get_session().run(tf.local_variables_initializer())
K.get_session().run(tf.global_variables_initializer())
#using sklelarn metrics
def class_report_metric(y_true,y_pred):
yt=np.array(y_true)
yp=np.array(K.round(y_pred))
st=classification_report(yt, yp)
return st
def auroc(y_true,y_pred):
yt=np.array(y_true)
yp=np.array((y_pred))
st=roc_auc_score(yt, yp)
return st
#using Tensorflow metrics
def auroc_metric(y_true,y_pred):
yt=y_true
yp=y_pred
auroc=tf.metrics.auc(yt, yp)[0]
K.get_session().run(tf.local_variables_initializer())
return auroc
def recall_metric(y_true, y_pred):
yt=y_true
yp=K.round(y_pred)
recall=tf.metrics.recall(yt, yp)[0]
return recall
def precision_metric(y_true, y_pred):
yt=y_true
yp=K.round(y_pred)
precision=tf.metrics.recall(yt, yp)[0]
return precision
def f1_metric(y_true, y_pred):
precision=precision_metric(y_true, y_pred)[0]
recall=recall_metric(y_true, y_pred)[0]
f1=(2*precision*recall)/(precision+recall)
return f1
adam=Adam(lr=0.001)
def cnnmodel(classes=14,input_shape=(224,224,3)):
X_input = Input(input_shape)
model1=densenet.DenseNet121(include_top=False, weights='imagenet')
X = model1.layers[-1].output
print(X.shape)
X= GlobalAveragePooling2D()(X)
print(X.shape)
X=Dense(classes, activation='sigmoid', name= 'fc121', kernel_initializer = he_uniform(seed=0))(X)
print(X.shape)
model = Model(model1.input,outputs=X,name="CNN")
return model
model = cnnmodel(classes = 1,input_shape = (224,224,3))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[class_report_metric])
#model.summary()
train_datagen = ImageDataGenerator( rescale=1./255)
val_datagen = ImageDataGenerator(rescale=1./255)
test_datagen=ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
'Train',
target_size=(224,224),
batch_size=64,
class_mode='binary')
validation_generator = val_datagen.flow_from_directory(
'Val',
target_size=(224, 224),
batch_size=64,
class_mode='binary')
test_generator=test_datagen.flow_from_directory(
'Test',
target_size=(224,224),
batch_size=64,
class_mode='binary')
print(train_generator.class_indices)
print(test_generator.class_indices)
print(validation_generator.class_indices)
history=model.fit_generator(train_generator, epochs =10,steps_per_epoch=50,validation_data=validation_generator, validation_steps=50)
model_files='mymodel'
model.save(model_files)
print(history.history)
model=load_model(total_model[-1], custom_objects={'class_report_metric': class_report_metric})
preds = model.evaluate_generator(train_generator, steps=50)
print(preds)
preds = model.evaluate_generator(validation_generator, steps=50)
print(preds)
preds = model.evaluate_generator(test_generator, steps=50)
print(preds)
print("DONE")