diff --git a/14_deep_computer_vision_with_cnns.ipynb b/14_deep_computer_vision_with_cnns.ipynb index cd2bdaf..25ea828 100644 --- a/14_deep_computer_vision_with_cnns.ipynb +++ b/14_deep_computer_vision_with_cnns.ipynb @@ -1147,6 +1147,13 @@ "model = tf.keras.applications.ResNet50(weights=\"imagenet\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Warning**: The expression `load_sample_images()[\"images\"]` returns a Python list of images. However, in the latest versions, Keras does not accept Python lists anymore, so we must convert this list to a tensor. We can do this using `tf.constant()`, but here I have used `K.constant()` instead: it simply calls `tf.constant()` if you are using TensorFlow as the backend (as is the case here), but if you ever decide to use JAX or PyTorch as the backend instead, `K.constant()` will call the appropriate function from the chosen backend." + ] + }, { "cell_type": "code", "execution_count": 35, @@ -1155,7 +1162,8 @@ }, "outputs": [], "source": [ - "images = load_sample_images()[\"images\"]\n", + "K = tf.keras.backend\n", + "images = K.constant(load_sample_images()[\"images\"])\n", "images_resized = tf.keras.layers.Resizing(height=224, width=224,\n", " crop_to_aspect_ratio=True)(images)" ] @@ -1852,7 +1860,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Note**: the code below used to reuse the optimizer from the previous model. This was fine in earlier versions of TensorFlow, but in more recent versions it can cause some issues, so I added a line to create a new optimizer here." + "**Note**: the code below used to reuse the optimizer from the previous model. This was fine in earlier versions of TensorFlow, but in more recent versions it can cause some issues, so I added a line to create a new optimizer here. Also, recent versions of Keras expect one metric per output, so I added the `\"mse\"` metric to the list." ] }, { @@ -1874,7 +1882,7 @@ "optimizer = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9) # added this line\n", "model.compile(loss=[\"sparse_categorical_crossentropy\", \"mse\"],\n", " loss_weights=[0.8, 0.2], # depends on what you care most about\n", - " optimizer=optimizer, metrics=[\"accuracy\"])" + " optimizer=optimizer, metrics=[\"accuracy\", \"mse\"])" ] }, { @@ -2215,7 +2223,7 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -2229,7 +2237,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.13" + "version": "3.9.10" }, "nav_menu": {}, "toc": {