-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInceptionv3_model.py
138 lines (101 loc) · 4.78 KB
/
Inceptionv3_model.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
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.utils import plot_model
#tf.get_logger().setLevel(40) # suppress deprecation messages
#tf.compat.v1.disable_v2_behavior() # disable TF2 behaviour as alibi code still relies on TF1 constructs
import os
from distutils.dir_util import copy_tree, remove_tree
from PIL import Image
from random import randint
import matplotlib.pyplot as plt
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
from sklearn.metrics import matthews_corrcoef as MCC
from sklearn.metrics import balanced_accuracy_score as BAS
from sklearn.metrics import classification_report, confusion_matrix
from keras.utils.vis_utils import plot_model
from tensorflow.keras import Sequential, Input
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import Conv2D, Flatten
from tensorflow.keras.callbacks import ReduceLROnPlateau
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing.image import ImageDataGenerator as IDG
from tensorflow.keras.layers import SeparableConv2D, BatchNormalization, GlobalAveragePooling2D
print("working dir contents:", os.listdir("./dataset(kaggle)/"))
WORK_DIR = './dataset(kaggle)/'
CLASSES = [ 'NonDemented',
'VeryMildDemented',
'MildDemented',
'ModerateDemented']
IMG_SIZE = 152
IMAGE_SIZE = [152, 152]
DIM = (IMG_SIZE, IMG_SIZE)
#Performing Image Augmentation to have more data samples
ZOOM = [.99, 1.01]
BRIGHT_RANGE = [0.8, 1.2]
HORZ_FLIP = True
FILL_MODE = "constant"
DATA_FORMAT = "channels_last"
work_dr = IDG(rescale = 1./255, brightness_range=BRIGHT_RANGE, zoom_range=ZOOM, data_format=DATA_FORMAT, fill_mode=FILL_MODE, horizontal_flip=HORZ_FLIP)
train_data_gen = work_dr.flow_from_directory(directory=WORK_DIR, target_size=DIM, batch_size=6500, shuffle=False)
#Retrieving the data from the ImageDataGenerator iterator
train_data, train_labels = train_data_gen.next()
#Getting to know the dimensions of our dataset
print(train_data.shape, train_labels.shape)
#Getting to know the dimensions of our
sm = SMOTE(random_state=42)
train_data, train_labels = sm.fit_resample(train_data.reshape(-1, IMG_SIZE * IMG_SIZE * 3), train_labels)
train_data = train_data.reshape(-1, IMG_SIZE, IMG_SIZE, 3)
print(train_data.shape, train_labels.shape)
#Splitting the data into train, test, and validation sets
train_data, test_data, train_labels, test_labels = train_test_split(train_data, train_labels, test_size = 0.2, random_state=42)
train_data, val_data, train_labels, val_labels = train_test_split(train_data, train_labels, test_size = 0.2, random_state=42)
# saving the test data to use in my model
print(type(train_data))
np.save("/projectnb/ec523kb/students/gdmac/project/alibi/data/kaggle/test_data_kaggle.npy", test_data)
np.save("/projectnb/ec523kb/students/gdmac/project/alibi/data/kaggle/train_data_kaggle.npy", train_data)
np.save("/projectnb/ec523kb/students/gdmac/project/alibi/data/kaggle/test_labels_kaggle.npy", test_labels)
np.save("/projectnb/ec523kb/students/gdmac/project/alibi/data/kaggle/train_labels_kaggle.npy", train_labels)
inception_model = InceptionV3(input_shape=(152, 152, 3), include_top=False, weights="imagenet")
for layer in inception_model.layers:
layer.trainable=False
# The actual model
custom_inception_model = Sequential([
inception_model,
Dropout(0.5),
GlobalAveragePooling2D(),
Flatten(),
BatchNormalization(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(128, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(64, activation='relu'),
Dropout(0.5),
BatchNormalization(),
Dense(4, activation='softmax')
], name = "inception_cnn_model")
METRICS = [
tf.keras.metrics.BinaryAccuracy(name='accuracy'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc')
]
# CALLBACKS = rop_callback
custom_inception_model.compile(optimizer='adam',
loss=tf.losses.CategoricalCrossentropy(),
metrics=METRICS)
custom_inception_model.summary()
#Fit the training data to the model and validate it using the validation data
EPOCHS = 20
history = custom_inception_model.fit(train_data, train_labels, validation_data=(val_data, val_labels), epochs=EPOCHS)
custom_inception_model.save("./models/kaggle_model.h5", save_format='h5')
plot_model(custom_inception_model, to_file="model.png", show_shapes=True, show_layer_names=True)