Add the coding exercise solutions for chapter 14

main
Aurélien Geron 2020-03-21 13:37:05 +13:00
parent 9dafb01893
commit 02e9847696
1 changed files with 71 additions and 134 deletions

View File

@ -65,7 +65,7 @@
"from tensorflow import keras\n", "from tensorflow import keras\n",
"assert tf.__version__ >= \"2.0\"\n", "assert tf.__version__ >= \"2.0\"\n",
"\n", "\n",
"if not tf.test.is_gpu_available():\n", "if not tf.config.list_physical_devices('GPU'):\n",
" print(\"No GPU was detected. CNNs can be very slow without a GPU.\")\n", " print(\"No GPU was detected. CNNs can be very slow without a GPU.\")\n",
" if IS_COLAB:\n", " if IS_COLAB:\n",
" print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n", " print(\"Go to Runtime > Change runtime and select a GPU hardware accelerator.\")\n",
@ -354,7 +354,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"cropped_images = np.array([crop(image) for image in images])\n", "cropped_images = np.array([crop(image) for image in images], dtype=np.float32)\n",
"output = max_pool(cropped_images)" "output = max_pool(cropped_images)"
] ]
}, },
@ -832,17 +832,23 @@
"dataset_size" "dataset_size"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning:** TFDS's split API has evolved since the book was published. The [new split API](https://www.tensorflow.org/datasets/splits) (called S3) is much simpler to use:"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 46,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"test_split, valid_split, train_split = tfds.Split.TRAIN.subsplit([10, 15, 75])\n", "test_set_raw, valid_set_raw, train_set_raw = tfds.load(\n",
"\n", " \"tf_flowers\",\n",
"test_set_raw = tfds.load(\"tf_flowers\", split=test_split, as_supervised=True)\n", " split=[\"train[:10%]\", \"train[10%:25%]\", \"train[25%:]\"],\n",
"valid_set_raw = tfds.load(\"tf_flowers\", split=valid_split, as_supervised=True)\n", " as_supervised=True)"
"train_set_raw = tfds.load(\"tf_flowers\", split=train_split, as_supervised=True)"
] ]
}, },
{ {
@ -1250,29 +1256,59 @@
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 9. High Accuracy CNN for MNIST\n", "## 9. High Accuracy CNN for MNIST\n",
"Exercise: Build your own CNN from scratch and try to achieve the highest possible accuracy on MNIST." "_Exercise: Build your own CNN from scratch and try to achieve the highest possible accuracy on MNIST._"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following model uses 2 convolutional layers, followed by 1 pooling layer, then dropout 25%, then a dense layer, another dropout layer but with 50% dropout, and finally the output layer. It reaches about 99.2% accuracy on the test set. This places this model roughly in the top 20% in the [MNIST Kaggle competition](https://www.kaggle.com/c/digit-recognizer/) (if we ignore the models with an accuracy greater than 99.79% which were most likely trained on the test set, as explained by Chris Deotte in [this post](https://www.kaggle.com/c/digit-recognizer/discussion/61480)). Can you do better? To reach 99.5 to 99.7% accuracy on the test set, you need to add image augmentation, batch norm, use a learning schedule such as 1-cycle, and possibly create an ensemble."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.mnist.load_data()\n",
"X_train_full = X_train_full / 255.\n",
"X_test = X_test / 255.\n",
"X_train, X_valid = X_train_full[:-5000], X_train_full[-5000:]\n",
"y_train, y_valid = y_train_full[:-5000], y_train_full[-5000:]\n",
"\n",
"X_train = X_train[..., np.newaxis]\n",
"X_valid = X_valid[..., np.newaxis]\n",
"X_test = X_test[..., np.newaxis]"
]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 68,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
}, "keras.backend.clear_session()\n",
{ "tf.random.set_seed(42)\n",
"cell_type": "code", "np.random.seed(42)\n",
"execution_count": null, "\n",
"metadata": {}, "model = keras.models.Sequential([\n",
"outputs": [], " keras.layers.Conv2D(32, kernel_size=3, padding=\"same\", activation=\"relu\"),\n",
"source": [] " keras.layers.Conv2D(64, kernel_size=3, padding=\"same\", activation=\"relu\"),\n",
" keras.layers.MaxPool2D(),\n",
" keras.layers.Flatten(),\n",
" keras.layers.Dropout(0.25),\n",
" keras.layers.Dense(128, activation=\"relu\"),\n",
" keras.layers.Dropout(0.5),\n",
" keras.layers.Dense(10, activation=\"softmax\")\n",
"])\n",
"model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=\"nadam\",\n",
" metrics=[\"accuracy\"])\n",
"\n",
"model.fit(X_train, y_train, epochs=10, validation_data=[X_valid, y_valid])\n",
"model.evaluate(X_test, y_test)"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
@ -1280,126 +1316,34 @@
"collapsed": true "collapsed": true
}, },
"source": [ "source": [
"## 10. Use transfer learning for large image classification\n", "## 10. Use transfer learning for large image classification"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"_Exercise: Use transfer learning for large image classification, going through these steps:_\n",
"\n", "\n",
"### 10.1)\n", "* _Create a training set containing at least 100 images per class. For example, you could classify your own pictures based on the location (beach, mountain, city, etc.), or alternatively you can use an existing dataset (e.g., from TensorFlow Datasets)._\n",
"Create a training set containing at least 100 images per class. For example, you could classify your own pictures based on the location (beach, mountain, city, etc.), or alternatively you can just use an existing dataset (e.g., from TensorFlow Datasets)." "* _Split it into a training set, a validation set, and a test set._\n",
"* _Build the input pipeline, including the appropriate preprocessing operations, and optionally add data augmentation._\n",
"* _Fine-tune a pretrained model on this dataset._"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### 10.2)\n", "See the Flowers example above."
"Split it into a training set, a validation set and a test set."
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 10.3)\n",
"Build the input pipeline, including the appropriate preprocessing operations, and optionally add data augmentation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 10.4)\n",
"Fine-tune a pretrained model on this dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 11.\n", "## 11.\n",
"Exercise: Go through TensorFlow's [DeepDream tutorial](https://goo.gl/4b2s6g). It is a fun way to familiarize yourself with various ways of visualizing the patterns learned by a CNN, and to generate art using Deep Learning.\n" "_Exercise: Go through TensorFlow's [Style Transfer tutorial](https://homl.info/styletuto). It is a fun way to generate art using Deep Learning._\n"
] ]
}, },
{ {
@ -1408,15 +1352,8 @@
"collapsed": true "collapsed": true
}, },
"source": [ "source": [
"Simply download the notebook and follow its instructions. For extra fun, you can produce a series of images, by repeatedly zooming in and running the DeepDream algorithm: using a tool such as [ffmpeg](https://ffmpeg.org/) you can then create a video from these images. For example, here is a [DeepDream video](https://www.youtube.com/watch?v=l6i_fDg30p0) I made... as you will see, it quickly turns into a nightmare. ;-) You can find hundreds of [similar videos](https://www.youtube.com/results?search_query=+deepdream) (often much more artistic) on the web." "Simply open the Colab and follow its instructions."
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
@ -1435,7 +1372,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.3" "version": "3.7.6"
}, },
"nav_menu": {}, "nav_menu": {},
"toc": { "toc": {