forked from lmoroney/dlaicourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
132 additions
and
0 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
...rFlow Deployment/Course 3 - TensorFlow Datasets/Week 1/Examples/rps-exercise-answer.ipynb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"name": "horse-or-human.ipynb", | ||
"provenance": [], | ||
"collapsed_sections": [], | ||
"toc_visible": true, | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"accelerator": "GPU" | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/TensorFlow%20Deployment/Course%203%20-%20TensorFlow%20Datasets/Week%201/Examples/rps-exercise-answer.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "zX4Kg8DUTKWO", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", | ||
"# you may not use this file except in compliance with the License.\n", | ||
"# You may obtain a copy of the License at\n", | ||
"#\n", | ||
"# https://www.apache.org/licenses/LICENSE-2.0\n", | ||
"#\n", | ||
"# Unless required by applicable law or agreed to in writing, software\n", | ||
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n", | ||
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", | ||
"# See the License for the specific language governing permissions and\n", | ||
"# limitations under the License." | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "coY1OkmCnT_8", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"Good to run this to ensure you are using TF2.x" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"colab_type": "code", | ||
"id": "ioLbtB3uGKPX", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"try:\n", | ||
" # %tensorflow_version only exists in Colab.\n", | ||
" %tensorflow_version 2.x\n", | ||
"except Exception:\n", | ||
" pass" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "iSq4t32ZHHpt", | ||
"colab_type": "code", | ||
"colab": {} | ||
}, | ||
"source": [ | ||
"import tensorflow as tf\n", | ||
"import tensorflow_datasets as tfds\n", | ||
"\n", | ||
"\n", | ||
"def my_one_hot(feature, label):\n", | ||
" return feature, tf.one_hot(label, depth=3)\n", | ||
"\n", | ||
"\n", | ||
"data = tfds.load('rock_paper_scissors', split='train', as_supervised=True)\n", | ||
"val_data = tfds.load('rock_paper_scissors', split='test', as_supervised=True)\n", | ||
"\n", | ||
"data = data.map(my_one_hot)\n", | ||
"val_data = val_data.map(my_one_hot)\n", | ||
"\n", | ||
"\n", | ||
"train_batches = data.shuffle(100).batch(10)\n", | ||
"validation_batches = val_data.batch(32)\n", | ||
"\n", | ||
"model = tf.keras.models.Sequential([\n", | ||
" tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(300, 300, 3)),\n", | ||
" tf.keras.layers.MaxPooling2D(2, 2),\n", | ||
" tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),\n", | ||
" tf.keras.layers.MaxPooling2D(2, 2),\n", | ||
" tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n", | ||
" tf.keras.layers.MaxPooling2D(2, 2),\n", | ||
" tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n", | ||
" tf.keras.layers.MaxPooling2D(2, 2),\n", | ||
" tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),\n", | ||
" tf.keras.layers.MaxPooling2D(2, 2),\n", | ||
" tf.keras.layers.Flatten(),\n", | ||
" tf.keras.layers.Dense(512, activation='relu'),\n", | ||
" tf.keras.layers.Dense(3, activation='softmax')\n", | ||
"])\n", | ||
"\n", | ||
"model.summary()\n", | ||
"\n", | ||
"model.compile(loss = 'categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])\n", | ||
"\n", | ||
"history = model.fit(train_batches, epochs=10, validation_data=validation_batches, validation_steps=1)\n", | ||
"model.save(\"test2.h5\")" | ||
], | ||
"execution_count": 0, | ||
"outputs": [] | ||
} | ||
] | ||
} |