-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment3_2.py
131 lines (86 loc) · 3.99 KB
/
Assignment3_2.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
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 15 13:38:39 2019
@author: s164616
"""
import os
import numpy as np
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.layers import Conv2D, MaxPool2D
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, TensorBoard
# unused for now, to be used for ROC analysis
from sklearn.metrics import roc_curve, auc
# the size of the images in the PCAM dataset
IMAGE_SIZE = 96
def get_pcam_generators(base_dir, train_batch_size=32, val_batch_size=32):
# dataset parameters
TRAIN_PATH = os.path.join(base_dir, 'train+val', 'train')
VALID_PATH = os.path.join(base_dir, 'train+val', 'valid')
RESCALING_FACTOR = 1./255
# instantiate data generators
datagen = ImageDataGenerator(rescale=RESCALING_FACTOR)
train_gen = datagen.flow_from_directory(TRAIN_PATH,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=train_batch_size,
class_mode='binary')
val_gen = datagen.flow_from_directory(VALID_PATH,
target_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=val_batch_size,
class_mode='binary',
shuffle=False)
return train_gen, val_gen
def get_model(kernel_size=(3,3), pool_size=(4,4), first_filters=32, second_filters=64):
# build the model
model = Sequential()
model.add(Conv2D(first_filters, kernel_size, activation = 'relu', padding = 'same', input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3)))
model.add(MaxPool2D(pool_size = pool_size))
model.add(Conv2D(second_filters, kernel_size, activation = 'relu', padding = 'same'))
model.add(MaxPool2D(pool_size = pool_size))
model.add(Flatten())
model.add(Dense(1, activation = 'sigmoid'))
# compile the model
model.compile(SGD(lr=0.01, momentum=0.95), loss = 'binary_crossentropy', metrics=['accuracy'])
return model
# get the model
model = get_model()
# get the data generators
train_gen, val_gen = get_pcam_generators('C:/Users/s164616/Documents/MATLAB/Project Imaging')
# save the model and weights
model_name = 'my_first_cnn_model'
model_filepath = model_name + '.json'
weights_filepath = model_name + '_weights.hdf5'
model_json = model.to_json() # serialize model to JSON
with open(model_filepath, 'w') as json_file:
json_file.write(model_json)
# define the model checkpoint and Tensorboard callbacks
checkpoint = ModelCheckpoint(weights_filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
tensorboard = TensorBoard(os.path.join('logs', model_name))
callbacks_list = [checkpoint, tensorboard]
# train the model
train_steps = train_gen.n//train_gen.batch_size
val_steps = val_gen.n//val_gen.batch_size
history = model.fit_generator(train_gen, steps_per_epoch=train_steps,
validation_data=val_gen,
validation_steps=val_steps,
epochs=3,
callbacks=callbacks_list)
predictions=model.predict_generator(val_gen,steps=val_steps)
y_pred = np.rint(predictions)
y_true = val_gen.classes
# ROC analysis
fpr_keras, tpr_keras, thresholds_keras = roc_curve(y_true, y_pred)
auc_keras = auc(fpr_keras, tpr_keras)
plt.figure()
plt.plot(fpr_keras, tpr_keras, color='darkorange', label='ROC curve (area = %0.2f)' % auc_keras)
plt.plot([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic')
plt.legend(loc="lower right")
plt.show()