<jupyter_start><jupyter_text>For Problem Statement 2❏ UseTensorFlow2.x.❏ Build, compile and train machine learning (ML) models using TensorFlow.❏ Preprocess data to get it ready for use in a model.❏ Use models to predict results.❏ Build sequential models with multiple layers.❏ Build and train models for binary classification.❏ Build and train models for multi-class categorization.❏ Plot loss and accuracy of a trained model.❏ Identify strategies to prevent overfitting, including augmentation and dropout.❏ Use pre trained models(transfer learning).❏ Extract features from pre-trained models.❏ Ensure that that inputs to a model are in the correct shape.❏ Ensure that you can match test data to the input shape of a neural network.  Would like to addrss one or more above problems in a single model<jupyter_code>import tensorflow as tf
print(tf.__version__)<jupyter_output>2.5.0<jupyter_text>* Binary Classification Cat vs Dog* Plot Loss and accuracy of a trained model* Prevent overfitting (with dropout)* Use Callback to do early stoping* Image augmentation<jupyter_code>import os
import zipfile
import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator<jupyter_output><empty_output><jupyter_text>Cat vs Dog<jupyter_code>!wget --no-check-certificate \
    https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
    -O /tmp/cats_and_dogs_filtered.zip

local_zip = '/tmp/cats_and_dogs_filtered.zip'
zip_ref = zipfile.ZipFile(local_zip, 'r')
zip_ref.extractall('/tmp')
zip_ref.close()

base_dir = '/tmp/cats_and_dogs_filtered'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')

# Directory with our training cat pictures
train_cats_dir = os.path.join(train_dir, 'cats')

# Directory with our training dog pictures
train_dogs_dir = os.path.join(train_dir, 'dogs')

# Directory with our validation cat pictures
validation_cats_dir = os.path.join(validation_dir, 'cats')

# Directory with our validation dog pictures
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
# We already have the images properly placed in train / validation folder.
# Lets build the model. Consider the incoming images would be of 150x150

# 1. First Image generator
train_image_gen = 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')
val_image_gen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_image_gen.flow_from_directory(train_dir,
                                                      target_size=(150,150),
                                                      batch_size=20,
                                                      class_mode='binary')

validation_generator = val_image_gen.flow_from_directory(validation_dir,
                                                      target_size=(150,150),
                                                      batch_size=20,
                                                      class_mode='binary')
class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('val_accuracy')>0.75):
      print("\nReached 75% accuracy so cancelling training!")
      self.model.stop_training = True
#Lets build our simple model
model = tf.keras.models.Sequential([
                                    tf.keras.layers.Conv2D(32,(3,3),activation='relu', input_shape=(150, 150, 3)),
                                    tf.keras.layers.MaxPooling2D(pool_size=(2,2)),
                                    # The second convolution
                                    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    # The third convolution
                                    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    # The fourth convolution
                                    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    # The fifth convolution
                                    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
                                    tf.keras.layers.MaxPooling2D(2,2),
                                    tf.keras.layers.Dropout(0.3),
                                    tf.keras.layers.Flatten(),
                                    tf.keras.layers.Dense(512, activation='relu'),
                                    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentropy', 
              optimizer=RMSprop(lr=1e-4),
              metrics=['accuracy'])
callbacks = myCallback()
history = model.fit(
    train_generator,
      steps_per_epoch=100,  # 2000 images = batch_size * steps
      epochs=100,
      validation_data=validation_generator,
      validation_steps=50,  # 1000 images = batch_size * steps
      callbacks=[callbacks],
      verbose=2)
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', label='Training accuracy')
plt.plot(epochs, val_acc, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')

plt.figure()

plt.plot(epochs, loss, 'r', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label='Validation Loss')
plt.title('Training and validation loss')
plt.legend()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0945568163131064, 'fraction_numerical': 0.020226936359151456, 'mean_word_length': 2.1350515463917525, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 37, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '124407', 'license': 'Apache-2.0', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1669, 'n_words': 397, 'path': 'ExamPrep/Exercise_2.ipynb', 'repo_name': 'mohameddhameem/TensorflowCertification', 'size': '6081'}