<jupyter_start><jupyter_text>Below is code with a link to a happy or sad dataset which contains 80 images, 40 happy and 40 sad. Create a convolutional neural network that trains to 100% accuracy on these images,  which cancels training upon hitting training accuracy of >.999Hint -- it will work best with 3 convolutional layers.<jupyter_code>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('accuracy') > DESIRED_ACCURACY):
      print('\Reached Accuracy 99.9% and stopped training')
      self.model.stop_training = True

# instantiate callback object
callbacks = myCallback()
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense
from tensorflow.keras.optimizers import RMSprop
# Model Building
model = Sequential()

model.add(Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPool2D((2,2)))

model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPool2D((2,2)))

model.add(Conv2D(32, (3,3), activation='relu'))
model.add(MaxPool2D((2,2)))

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer=RMSprop(learning_rate=0.001),
                loss='binary_crossentropy',
                metrics=['accuracy'])
# ImageGenerator
from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1/255)

# load it from directory and the generator directly
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'
# Model fitting
history = model.fit(
    train_generator,
    steps_per_epoch=8,
    epochs=15,
    verbose=1,
    callbacks=[callbacks]
)
    
# Expected output: "Reached 99.9% accuracy so cancelling training!""<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12526539278131635, 'fraction_numerical': 0.03524416135881104, 'mean_word_length': 4.378995433789954, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '308046', 'license': 'MIT', 'n_tokens_mistral': 871, 'n_tokens_neox': 793, 'n_words': 182, 'path': 'Tensorflow - DeepLearning.AI - 01.Introduction to TensorFlow for AI, ML and DL/Week_04_Handling_Complex_Images/Exercise_4_ Handling_Complex_Images.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '2355'}