Created
March 19, 2019 14:45
-
-
Save mgomes/fe71b80d409b5fe732bbc79ce15b428b to your computer and use it in GitHub Desktop.
Convolutional Neural Net using TensorFlow on Happy/Sad image dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import os | |
import zipfile | |
DESIRED_ACCURACY = 0.999 | |
!wget --no-check-certificate \ | |
"https://storage.googleapis.com/laurencemoroney-blog.appspot.com/happy-or-sad.zip" \ | |
-O "/tmp/happy-or-sad.zip" | |
zip_ref = zipfile.ZipFile("/tmp/happy-or-sad.zip", 'r') | |
zip_ref.extractall("/tmp/h-or-s") | |
zip_ref.close() | |
class myCallback(tf.keras.callbacks.Callback): | |
def on_epoch_end(self, epoch, logs={}): | |
if(logs.get('acc')>DESIRED_ACCURACY): | |
print("\nReached 99.9% accuracy so cancelling training!") | |
self.model.stop_training = True | |
callbacks = myCallback() | |
model = tf.keras.models.Sequential([ | |
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)), | |
tf.keras.layers.MaxPooling2D(2, 2), | |
tf.keras.layers.Conv2D(32, (3,3), activation='relu'), | |
tf.keras.layers.MaxPooling2D(2,2), | |
tf.keras.layers.Conv2D(32, (3,3), activation='relu'), | |
tf.keras.layers.MaxPooling2D(2,2), | |
tf.keras.layers.Flatten(), | |
tf.keras.layers.Dense(512, activation='relu'), | |
tf.keras.layers.Dense(1, activation='sigmoid') | |
]) | |
from tensorflow.keras.optimizers import RMSprop | |
model.compile(loss='binary_crossentropy', | |
optimizer=RMSprop(lr=0.001), | |
metrics=['acc']) | |
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
train_datagen = ImageDataGenerator(rescale=1/255) | |
train_generator = train_datagen.flow_from_directory( | |
"/tmp/h-or-s", | |
target_size=(150, 150), | |
batch_size=10, | |
class_mode='binary') | |
# Expected output: 'Found 80 images belonging to 2 classes' | |
history = model.fit_generator( | |
train_generator, | |
steps_per_epoch=2, | |
epochs=15, | |
verbose=1, | |
callbacks=[callbacks]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Definitely overfits. Accuracy approx. 1.