-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg19.py
215 lines (174 loc) · 6.03 KB
/
vgg19.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
"""vgg19.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1gJ6aQTeeISXC_T92A-KX3DOdKHNghigq
"""
# Commented out IPython magic to ensure Python compatibility.
import os
import shutil
import cv2
import pandas as pd
import tensorflow as tf
from keras.applications.vgg19 import preprocess_input
import matplotlib.pyplot as plt
import tensorflow
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import InputLayer, BatchNormalization, Dropout, Flatten, Dense, Activation, MaxPool2D, Conv2D
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.applications.vgg19 import VGG19
from keras.preprocessing.image import load_img, img_to_array
from keras.metrics import Recall,Precision
# %matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
from keras import models, layers
from keras.utils.np_utils import to_categorical
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from pathlib import Path
from sklearn.metrics import classification_report, confusion_matrix
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
root_path = Path("/content/Fish_Dataset/Fish_Dataset")
sub_paths = root_path.glob(r'**/*.png')
#converting to list, will deal with generator later
sub_paths = list(sub_paths)
#creating temporary list to handle generator
data = []
for i in range(len(sub_paths)):
#get the 2nd from last directory name
label = os.path.split(os.path.split(sub_paths[i])[0])[1]
data.append([str(sub_paths[i]), label])
#Appending data to dataframe
df = pd.DataFrame(data, columns=['path', 'label'])
#Removing ground truths
df = df[df['label'].apply(lambda x: x[-2:] != 'GT')].reset_index(drop=True)
#Splitting into training, validation, and testing data
train_df, test_df = train_test_split(df, test_size=0.2, random_state=2, shuffle=True)
train_df.count()
test_df.count()
#Importing images using image data preprocessing provided from keras
train_generator = ImageDataGenerator(validation_split=0.2)
test_generator = ImageDataGenerator()
train_imgs = train_generator.flow_from_dataframe(
dataframe = train_df,
x_col = "path",
y_col = "label",
target_size = (224, 224),
color_mode = "rgb",
class_mode = "categorical",
batch_size = 32,
shuffle = True,
subset = "training"
)
val_imgs = train_generator.flow_from_dataframe(
dataframe = train_df,
x_col = "path",
y_col = "label",
target_size = (224, 224),
color_mode = "rgb",
class_mode = "categorical",
batch_size = 32,
shuffle = True,
subset = "validation"
)
test_imgs = test_generator.flow_from_dataframe(
dataframe = test_df,
x_col = "path",
y_col = "label",
target_size = (224, 224),
color_mode = "rgb",
class_mode = "categorical",
batch_size = 32,
shuffle = False
)
# Model Initialization
base_model = VGG19(input_shape=(224,224,3),
include_top=False,
weights="imagenet")
# Freezing Layers
for layer in base_model.layers:
layer.trainable=False
# Building Model
model=Sequential()
model.add(base_model)
model.add(Dropout(0.5))
model.add(Flatten())
model.add(BatchNormalization())
model.add(Dense(512,kernel_initializer='he_uniform'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Dense(9,activation='softmax'))
# Summary
model.summary()
4
from keras.utils.vis_utils import plot_model
from IPython.display import SVG, Image
plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)
Image('model.png',width=400, height=200)
# Model Compile
OPT = tensorflow.keras.optimizers.Adam(lr=0.001)
model.compile(loss='categorical_crossentropy',
metrics=['accuracy',Precision(),Recall()],
optimizer=OPT)
# Defining Callbacks
filepath = './best_weights.hdf5'
earlystopping = EarlyStopping(monitor = 'val_accuracy',
mode = 'max' ,
patience = 5,
verbose = 1)
checkpoint = ModelCheckpoint(filepath,
monitor = 'val_accuracy',
mode='max',
save_best_only=True,
verbose = 1)
callback_list = [earlystopping, checkpoint]
model_history=model.fit(train_imgs,
validation_data=val_imgs,
epochs = 20,
callbacks = callback_list,
verbose = 1)
import keras
model.save('./best_weights.hdf5')
#model = keras.models.load_model('./best_weights.hdf5')
plt.plot(model_history.history['accuracy'])
plt.plot(model_history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='lower right')
plt.show()
plt.plot(model_history.history['loss'])
plt.plot(model_history.history['val_loss'])
plt.title('train set loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()
plt.plot(model_history.history['precision'])
plt.plot(model_history.history['val_precision'])
plt.title(' precision')
plt.ylabel('precision')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()
plt.plot(model_history.history['recall'])
plt.plot(model_history.history['val_recall'])
plt.title(' recall')
plt.ylabel('recall')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper right')
plt.show()
loss, accuracy , precision,recall=model.evaluate(test_imgs)
print('Test Accuracy: %.3f' % accuracy)
print('Test Precision: %.3f' % precision)
print('Test Recall: %.3f' % recall)
print('Test loss: %.3f' % loss)
y_pred = model.predict(test_imgs)
y_pred = np.argmax(y_pred,axis=1)
from sklearn.metrics import classification_report
print(classification_report(test_imgs.labels,y_pred))