-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusingImageDataGen.py
More file actions
130 lines (107 loc) · 4 KB
/
usingImageDataGen.py
File metadata and controls
130 lines (107 loc) · 4 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import urllib.request
# import zipfile
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from tensorflow.keras.applications.inception_v3 import InceptionV3
# import keras
# url = "https://storage.googleapis.com/learning-datasets/horse-or-human.zip"
# validation_url = "https://storage.googleapis.com/learning-datasets/validation-horse-or-human.zip"
file_name = "horseorhuman.zip"
validation_file_name = "validationhorseorhuman.zip"
training_dir = "horse-or-human/training"
validation_dir = "horse-or-human/validation"
#
#
# urllib.request.urlretrieve(url, file_name)
# urllib.request.urlretrieve(validation_url, validation_file_name)
#
# zip_ref = zipfile.ZipFile(file_name, 'r')
# zip_ref.extractall(training_dir)
# zip_ref.close()
#
# zip_ref = zipfile.ZipFile(validation_file_name, 'r')
# zip_ref.extractall(validation_dir)
# zip_ref.close()
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
train_datagen = ImageDataGenerator(
rescale=1 / 255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
validation_datagen = ImageDataGenerator(rescale=1 / 255)
train_generator = train_datagen.flow_from_directory(
training_dir,
target_size=(300, 300),
class_mode='binary'
)
validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(300, 300),
class_mode='binary'
)
weights_url = "https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"
weights_file = "inception_v3.h5"
urllib.request.urlretrieve(weights_url, weights_file)
pre_trained_model = InceptionV3(input_shape=(150, 150, 3),
include_top=False,
weights=None)
pre_trained_model.load_weights(weights_file)
pre_trained_model.summary()
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output
# x = tf.keras.layers.Flatten()(last_output)
# # Add a fully connected layer with 1,024 hidden units and ReLU activation
# x = tf.keras.layers.Dense(1024, activation='relu')(x)
# # Add a dropout rate of 0.2
# x = tf.keras.layers.Dropout(0.2)(x)
# # Add a final sigmoid layer for classification
# x = tf.keras.layers.Dense(1, activation='sigmoid')(x)
#
# model = tf.keras.Model(pre_trained_model.input, x)
#
# model.compile(optimizer="rmsprop(lr=0.0001)",
# loss='binary_crossentropy',
# metrics=['acc'])
data_input = tf.keras.Input(shape=(300, 300, 3))
x = tf.keras.layers.Conv2D(16, (3, 3), activation='relu')(data_input)
x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Conv2D(64, (3, 3), activation='relu')(x)
# x = tf.keras.layers.MaxPooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(512, activation='relu')(x)
x = tf.keras.layers.Dense(128, activation='relu')(x)
output = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.Model(inputs=data_input, outputs=output)
model.summary()
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=["accuracy"],
)
class MyCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if logs.get('val_accuracy') - logs.get('accuracy') < 0.05 and logs.get('accuracy') > 0.95:
print("\nReached 95% accuracy ending training at epoch ", epoch)
self.model.stop_training = True
my_callback = MyCallback()
model.fit(
train_generator,
epochs=15,
validation_data=validation_generator,
callbacks=[my_callback],
)