-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobilenet.py
153 lines (118 loc) · 4.68 KB
/
mobilenet.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
import tensorflow as tf
import numpy as np
import os
import pandas as pd
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Dense, Input, Conv2D, BatchNormalization, MaxPooling2D, Flatten, GlobalAvgPool2D, Add, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam, SGD
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from tensorflow.keras import Sequential, layers, losses
import cv2 as cv
import matplotlib.image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
data_dir = '/home/praem90/packages/EachOneTeachOne/ImageClassification/Grapevine_Leaves_Image_Dataset/'
data_dir = '/home/praem90/packages/EachOneTeachOne/ImageClassification/Training set Oneplus8/'
class_list = os.listdir(data_dir)
class_directory = {"Ak": 0, "Ala_Idris": 1, "Buzgulu": 2, "Dimnit": 3, "Nazli": 4}
train_data = []
train_labels = []
for class_folder in class_list:
if class_folder[0] != 'G':
file_names = os.listdir(os.path.join(data_dir, class_folder))
for file_name in file_names:
image = matplotlib.image.imread(os.path.join(data_dir, class_folder, file_name))
try:
resized = cv.resize(image, (511, 511), interpolation=cv.INTER_AREA)
train_data.append(resized)
train_labels.append(class_directory[class_folder])
# print(class_directory[class_folder])
except:
break
train_data = np.array(train_data)
train_labels = np.array(train_labels)
train_labels = train_labels.reshape((-1, 1))
train_data, train_labels = shuffle(train_data, train_labels)
path_Ak = data_dir + 'Ak'
path_Ala_Idris = data_dir + 'Ala_Idris'
path_Buzgulu = data_dir + 'Buzgulu'
path_Dimnit = data_dir + 'Dimnit'
path_Nazli = data_dir + 'Nazli'
df = pd.DataFrame()
df['images'] = os.listdir(path_Ak) + os.listdir(path_Ala_Idris) + os.listdir(path_Buzgulu) + os.listdir(path_Dimnit) + os.listdir(path_Nazli)
classes = []
paths = []
for image in df['images']:
class_ = image.split(' (')[0]
classes.append(class_)
paths.append(data_dir+class_+'/'+image)
df['classes'] = classes
df['path'] = paths
X_train, X_test, y_train, y_test = train_test_split(df[['path', 'classes']], df[['classes']], test_size=0.2)
# X_train, X_test, y_train, y_test = train_test_split(train_data, train_labels, test_size=0.2, shuffle=True, random_state=101)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)
mobilenet_datagen = ImageDataGenerator(
rotation_range=20,
zoom_range=0.10,
brightness_range=[0.6,1.4],
channel_shift_range=0.7,
width_shift_range=0.15,
height_shift_range=0.15,
shear_range=0.15,
horizontal_flip=True,
fill_mode='nearest',
preprocessing_function=preprocess_input
)
# train_generator_mobilenet = mobilenet_datagen.flow_from_directory(
# data_dir,
# target_size=(511, 511),
# batch_size=4,
# class_mode="categorical",
# shuffle=True,
# )
# val_generator_mobilenet = mobilenet_datagen.flow_from_directory(
# data_dir,
# target_size=(511, 511),
# batch_size=4,
# class_mode="categorical",
# shuffle=True,
# )
train_generator_mobilenet = mobilenet_datagen.flow_from_dataframe(
X_train,
x_col='path',
y_col='classes',
target_size=(511, 511),
batch_size=4,
class_mode="categorical",
shuffle=True,
)
val_generator_mobilenet = mobilenet_datagen.flow_from_dataframe(
X_val,
x_col='path',
y_col='classes',
target_size=(511, 511),
batch_size=4,
class_mode="categorical",
shuffle=True,
)
mobilnet_model = tf.keras.applications.MobileNetV2(include_top = False, input_shape = (511,511,3), weights = 'imagenet')
mobilnet_model.trainable = False
inputs = Input((511, 511,3))
base_model_output = mobilnet_model(inputs)
x = GlobalAvgPool2D()(base_model_output)
x = Dense(units= 512, activation='relu')(x)
x = Dense(units= 256, activation='relu')(x)
x = Dropout(0.5)(x)
output = Dense(units= 5, activation='softmax')(x)
mobilenet = tf.keras.Model(inputs, output)
mobilenet.summary()
mobilenet.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
save_best = tf.keras.callbacks.ModelCheckpoint("best_mobilenet_model_binary_511.h5", save_best_only=True)
history_mobilenet = mobilenet.fit(
train_generator_mobilenet,
validation_data=val_generator_mobilenet,
epochs=10,
callbacks=[save_best])