Coursera

Horses or Humans

#@title Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
!pip install tensorflow_addons
Collecting tensorflow_addons
  Downloading tensorflow_addons-0.22.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (612 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 612.3/612.3 kB 8.6 MB/s eta 0:00:00
[?25hRequirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from tensorflow_addons) (23.2)
Collecting typeguard<3.0.0,>=2.7 (from tensorflow_addons)
  Downloading typeguard-2.13.3-py3-none-any.whl (17 kB)
Installing collected packages: typeguard, tensorflow_addons
Successfully installed tensorflow_addons-0.22.0 typeguard-2.13.3
try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass
Colab only includes TensorFlow 2.x; %tensorflow_version has no effect.
import tensorflow as tf
import tensorflow_datasets as tfds
import tensorflow_addons as tfa

data = tfds.load('horses_or_humans', split='train', as_supervised=True)
val_data = tfds.load('horses_or_humans', split='test', as_supervised=True)
/usr/local/lib/python3.10/dist-packages/tensorflow_addons/utils/tfa_eol_msg.py:23: UserWarning: 

TensorFlow Addons (TFA) has ended development and introduction of new features.
TFA has entered a minimal maintenance and release mode until a planned end of life in May 2024.
Please modify downstream libraries to take dependencies from other repositories in our TensorFlow community (e.g. Keras, Keras-CV, and Keras-NLP). 

For more information see: https://github.com/tensorflow/addons/issues/2807 

  warnings.warn(


Downloading and preparing dataset 153.59 MiB (download: 153.59 MiB, generated: Unknown size, total: 153.59 MiB) to /root/tensorflow_datasets/horses_or_humans/3.0.0...



Dl Completed...: 0 url [00:00, ? url/s]



Dl Size...: 0 MiB [00:00, ? MiB/s]



Generating splits...:   0%|          | 0/2 [00:00<?, ? splits/s]



Generating train examples...:   0%|          | 0/1027 [00:00<?, ? examples/s]



Shuffling /root/tensorflow_datasets/horses_or_humans/3.0.0.incompleteF4I17E/horses_or_humans-train.tfrecord*..…



Generating test examples...:   0%|          | 0/256 [00:00<?, ? examples/s]



Shuffling /root/tensorflow_datasets/horses_or_humans/3.0.0.incompleteF4I17E/horses_or_humans-test.tfrecord*...…


Dataset horses_or_humans downloaded and prepared to /root/tensorflow_datasets/horses_or_humans/3.0.0. Subsequent calls will reuse this data.
train_batches = data.shuffle(100).batch(32)
validation_batches = val_data.batch(32)

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16, (3,3), activation='relu',
                                      input_shape=(300, 300, 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(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(64, (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')
])


model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy'])


history = model.fit(train_batches, epochs=10, validation_data=validation_batches, validation_steps=1)
Epoch 1/10
33/33 [==============================] - 26s 243ms/step - loss: 6.8233 - accuracy: 0.7556 - val_loss: 0.1708 - val_accuracy: 0.9375
Epoch 2/10
33/33 [==============================] - 2s 59ms/step - loss: 0.0937 - accuracy: 0.9659 - val_loss: 0.5107 - val_accuracy: 0.9062
Epoch 3/10
33/33 [==============================] - 2s 51ms/step - loss: 0.0601 - accuracy: 0.9747 - val_loss: 0.6741 - val_accuracy: 0.8438
Epoch 4/10
33/33 [==============================] - 1s 44ms/step - loss: 0.0249 - accuracy: 0.9932 - val_loss: 0.5732 - val_accuracy: 0.8750
Epoch 5/10
33/33 [==============================] - 1s 45ms/step - loss: 0.0018 - accuracy: 1.0000 - val_loss: 1.3451 - val_accuracy: 0.8438
Epoch 6/10
33/33 [==============================] - 1s 45ms/step - loss: 7.7915e-04 - accuracy: 1.0000 - val_loss: 0.9090 - val_accuracy: 0.8438
Epoch 7/10
33/33 [==============================] - 1s 42ms/step - loss: 2.3543e-04 - accuracy: 1.0000 - val_loss: 0.8705 - val_accuracy: 0.8438
Epoch 8/10
33/33 [==============================] - 1s 42ms/step - loss: 1.4482e-04 - accuracy: 1.0000 - val_loss: 0.7085 - val_accuracy: 0.8750
Epoch 9/10
33/33 [==============================] - 1s 45ms/step - loss: 6.7622e-05 - accuracy: 1.0000 - val_loss: 0.7443 - val_accuracy: 0.9062
Epoch 10/10
33/33 [==============================] - 2s 46ms/step - loss: 4.0875e-05 - accuracy: 1.0000 - val_loss: 0.7568 - val_accuracy: 0.9062