#!/usr/bin/python3
import tensorflow as tf
class MyCB(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if (logs.get('loss') < 0.1):
self.model.stop_training = True
print("\nStopping Training..........\n")
cbs = MyCB()
dg = tf.keras.preprocessing.image.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
)
gen = dg.flow_from_directory(
'data/training',
target_size=(150,150),
batch_size=20,
class_mode='binary')
vdg = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1/255.
)
vgen = vdg.flow_from_directory(
'data/validation',
target_size=(150,150),
batch_size=20,
class_mode='binary')
saved_model_weights='data/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'
ptm = tf.keras.applications.inception_v3.InceptionV3(
input_shape=(150,150,3),
include_top=False,
weights=None)
ptm.load_weights(saved_model_weights)
for l in ptm.layers:
l.trainable = False
last_output = ptm.get_layer('mixed7').output
x = tf.keras.layers.Flatten()(last_output)
x = tf.keras.layers.Dense(units=512, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
x = tf.keras.layers.Dense(units=1, activation='sigmoid')(x)
model = tf.keras.Model(ptm.input, x)
model.compile(
optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.0001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['accuracy'])
model.fit(
gen,
validation_data=vgen,
epochs=20,
callbacks=[cbs])
Comments
Post a Comment