From 43d2b11c7d03b1d059051b1d4ec62ce1d9e0c915 Mon Sep 17 00:00:00 2001 From: Tilak <81610181+tilakrayal@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:48:13 +0530 Subject: [PATCH 1/4] Update transfer_learning_with_hub.ipynb --- .../images/transfer_learning_with_hub.ipynb | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/site/en/tutorials/images/transfer_learning_with_hub.ipynb b/site/en/tutorials/images/transfer_learning_with_hub.ipynb index 58ebfc2d20f..33bbf469bd3 100644 --- a/site/en/tutorials/images/transfer_learning_with_hub.ipynb +++ b/site/en/tutorials/images/transfer_learning_with_hub.ipynb @@ -68,7 +68,7 @@ "\n", "This tutorial demonstrates how to:\n", "\n", - "1. Use models from TensorFlow Hub with `tf.keras`.\n", + "1. Use models from TensorFlow Hub with `keras`.\n", "1. Use an image classification model from TensorFlow Hub.\n", "1. Do simple transfer learning to fine-tune a model for your own image classes." ] @@ -90,6 +90,7 @@ }, "outputs": [], "source": [ + "!pip install tf-keras\n" "import numpy as np\n", "import time\n", "\n", @@ -97,6 +98,7 @@ "import matplotlib.pylab as plt\n", "\n", "import tensorflow as tf\n", + "import tf_keras as keras\n", "import tensorflow_hub as hub\n", "\n", "import datetime\n", @@ -150,7 +152,7 @@ "source": [ "IMAGE_SHAPE = (224, 224)\n", "\n", - "classifier = tf.keras.Sequential([\n", + "classifier = keras.Sequential([\n", " hub.KerasLayer(classifier_model, input_shape=IMAGE_SHAPE+(3,))\n", "])" ] @@ -181,7 +183,7 @@ }, "outputs": [], "source": [ - "grace_hopper = tf.keras.utils.get_file('image.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg')\n", + "grace_hopper = keras.utils.get_file('image.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg')\n", "grace_hopper = Image.open(grace_hopper).resize(IMAGE_SHAPE)\n", "grace_hopper" ] @@ -261,7 +263,7 @@ }, "outputs": [], "source": [ - "labels_path = tf.keras.utils.get_file('ImageNetLabels.txt','https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')\n", + "labels_path = keras.utils.get_file('ImageNetLabels.txt','https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')\n", "imagenet_labels = np.array(open(labels_path).read().splitlines())" ] }, @@ -323,7 +325,7 @@ "source": [ "import pathlib\n", "\n", - "data_file = tf.keras.utils.get_file(\n", + "data_file = keras.utils.get_file(\n", " 'flower_photos.tgz',\n", " 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',\n", " cache_dir='.',\n", @@ -338,7 +340,7 @@ "id": "jFHdp18ccah7" }, "source": [ - "First, load this data into the model using the image data off disk with `tf.keras.utils.image_dataset_from_directory`, which will generate a `tf.data.Dataset`:" + "First, load this data into the model using the image data off disk with `keras.utils.image_dataset_from_directory`, which will generate a `tf.data.Dataset`:" ] }, { @@ -353,7 +355,7 @@ "img_height = 224\n", "img_width = 224\n", "\n", - "train_ds = tf.keras.utils.image_dataset_from_directory(\n", + "train_ds = keras.utils.image_dataset_from_directory(\n", " str(data_root),\n", " validation_split=0.2,\n", " subset=\"training\",\n", @@ -362,7 +364,7 @@ " batch_size=batch_size\n", ")\n", "\n", - "val_ds = tf.keras.utils.image_dataset_from_directory(\n", + "val_ds = keras.utils.image_dataset_from_directory(\n", " str(data_root),\n", " validation_split=0.2,\n", " subset=\"validation\",\n", @@ -399,7 +401,7 @@ "id": "L0Btd0V3C8h4" }, "source": [ - "Second, because TensorFlow Hub's convention for image models is to expect float inputs in the `[0, 1]` range, use the `tf.keras.layers.Rescaling` preprocessing layer to achieve this." + "Second, because TensorFlow Hub's convention for image models is to expect float inputs in the `[0, 1]` range, use the `keras.layers.Rescaling` preprocessing layer to achieve this." ] }, { @@ -408,7 +410,7 @@ "id": "Rs6gfO-ApTQW" }, "source": [ - "Note: You could also include the `tf.keras.layers.Rescaling` layer inside the model. Refer to the [Working with preprocessing layers](https://www.tensorflow.org/guide/keras/preprocessing_layers) guide for a discussion of the tradeoffs." + "Note: You could also include the `keras.layers.Rescaling` layer inside the model. Refer to the [Working with preprocessing layers](https://www.tensorflow.org/guide/keras/preprocessing_layers) guide for a discussion of the tradeoffs." ] }, { @@ -419,7 +421,7 @@ }, "outputs": [], "source": [ - "normalization_layer = tf.keras.layers.Rescaling(1./255)\n", + "normalization_layer = keras.layers.Rescaling(1./255)\n", "train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y)) # Where x—images, y—labels.\n", "val_ds = val_ds.map(lambda x, y: (normalization_layer(x), y)) # Where x—images, y—labels." ] @@ -620,7 +622,7 @@ "source": [ "### Attach a classification head\n", "\n", - "To complete the model, wrap the feature extractor layer in a `tf.keras.Sequential` model and add a fully-connected layer for classification:" + "To complete the model, wrap the feature extractor layer in a `keras.Sequential` model and add a fully-connected layer for classification:" ] }, { @@ -633,9 +635,9 @@ "source": [ "num_classes = len(class_names)\n", "\n", - "model = tf.keras.Sequential([\n", + "model = keras.Sequential([\n", " feature_extractor_layer,\n", - " tf.keras.layers.Dense(num_classes)\n", + " keras.layers.Dense(num_classes)\n", "])\n", "\n", "model.summary()" @@ -671,7 +673,7 @@ "source": [ "### Train the model\n", "\n", - "Use `Model.compile` to configure the training process and add a `tf.keras.callbacks.TensorBoard` callback to create and store logs:" + "Use `Model.compile` to configure the training process and add a `keras.callbacks.TensorBoard` callback to create and store logs:" ] }, { @@ -683,12 +685,12 @@ "outputs": [], "source": [ "model.compile(\n", - " optimizer=tf.keras.optimizers.Adam(),\n", - " loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", + " optimizer=keras.optimizers.Adam(),\n", + " loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n", " metrics=['acc'])\n", "\n", "log_dir = \"logs/fit/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\")\n", - "tensorboard_callback = tf.keras.callbacks.TensorBoard(\n", + "tensorboard_callback = keras.callbacks.TensorBoard(\n", " log_dir=log_dir,\n", " histogram_freq=1) # Enable histogram computation for every epoch." ] @@ -846,7 +848,7 @@ }, "outputs": [], "source": [ - "reloaded = tf.keras.models.load_model(export_path)" + "reloaded = keras.models.load_model(export_path)" ] }, { From bcdf8e023443e980781536c5390310953546d181 Mon Sep 17 00:00:00 2001 From: Tilak <81610181+tilakrayal@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:05:02 +0530 Subject: [PATCH 2/4] Update transfer_learning_with_hub.ipynb --- site/en/tutorials/images/transfer_learning_with_hub.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/images/transfer_learning_with_hub.ipynb b/site/en/tutorials/images/transfer_learning_with_hub.ipynb index 33bbf469bd3..b6b5867962b 100644 --- a/site/en/tutorials/images/transfer_learning_with_hub.ipynb +++ b/site/en/tutorials/images/transfer_learning_with_hub.ipynb @@ -90,7 +90,7 @@ }, "outputs": [], "source": [ - "!pip install tf-keras\n" + "!pip install tf-keras\n", "import numpy as np\n", "import time\n", "\n", From ef9082289d9e6628cc11cb77f77d4c868b37f230 Mon Sep 17 00:00:00 2001 From: Tilak <81610181+tilakrayal@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:10:36 +0530 Subject: [PATCH 3/4] Update transfer_learning_with_hub.ipynb --- .../images/transfer_learning_with_hub.ipynb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/site/en/tutorials/images/transfer_learning_with_hub.ipynb b/site/en/tutorials/images/transfer_learning_with_hub.ipynb index b6b5867962b..f4240d8d587 100644 --- a/site/en/tutorials/images/transfer_learning_with_hub.ipynb +++ b/site/en/tutorials/images/transfer_learning_with_hub.ipynb @@ -68,7 +68,7 @@ "\n", "This tutorial demonstrates how to:\n", "\n", - "1. Use models from TensorFlow Hub with `keras`.\n", + "1. Use models from TensorFlow Hub with `tf.keras`.\n", "1. Use an image classification model from TensorFlow Hub.\n", "1. Do simple transfer learning to fine-tune a model for your own image classes." ] @@ -340,7 +340,7 @@ "id": "jFHdp18ccah7" }, "source": [ - "First, load this data into the model using the image data off disk with `keras.utils.image_dataset_from_directory`, which will generate a `tf.data.Dataset`:" + "First, load this data into the model using the image data off disk with `tf.keras.utils.image_dataset_from_directory`, which will generate a `tf.data.Dataset`:" ] }, { @@ -401,7 +401,7 @@ "id": "L0Btd0V3C8h4" }, "source": [ - "Second, because TensorFlow Hub's convention for image models is to expect float inputs in the `[0, 1]` range, use the `keras.layers.Rescaling` preprocessing layer to achieve this." + "Second, because TensorFlow Hub's convention for image models is to expect float inputs in the `[0, 1]` range, use the `tf.keras.layers.Rescaling` preprocessing layer to achieve this." ] }, { @@ -410,7 +410,7 @@ "id": "Rs6gfO-ApTQW" }, "source": [ - "Note: You could also include the `keras.layers.Rescaling` layer inside the model. Refer to the [Working with preprocessing layers](https://www.tensorflow.org/guide/keras/preprocessing_layers) guide for a discussion of the tradeoffs." + "Note: You could also include the `tf.keras.layers.Rescaling` layer inside the model. Refer to the [Working with preprocessing layers](https://www.tensorflow.org/guide/keras/preprocessing_layers) guide for a discussion of the tradeoffs." ] }, { @@ -622,7 +622,7 @@ "source": [ "### Attach a classification head\n", "\n", - "To complete the model, wrap the feature extractor layer in a `keras.Sequential` model and add a fully-connected layer for classification:" + "To complete the model, wrap the feature extractor layer in a `tf.keras.Sequential` model and add a fully-connected layer for classification:" ] }, { @@ -673,7 +673,7 @@ "source": [ "### Train the model\n", "\n", - "Use `Model.compile` to configure the training process and add a `keras.callbacks.TensorBoard` callback to create and store logs:" + "Use `Model.compile` to configure the training process and add a `tf.keras.callbacks.TensorBoard` callback to create and store logs:" ] }, { From 6390ae8dc60fa972e90aee8fc38d76fb2b788f82 Mon Sep 17 00:00:00 2001 From: Tilak <81610181+tilakrayal@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:12:11 +0530 Subject: [PATCH 4/4] Update transfer_learning_with_hub.ipynb --- site/en/tutorials/images/transfer_learning_with_hub.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/en/tutorials/images/transfer_learning_with_hub.ipynb b/site/en/tutorials/images/transfer_learning_with_hub.ipynb index f4240d8d587..0d5e04fc9b6 100644 --- a/site/en/tutorials/images/transfer_learning_with_hub.ipynb +++ b/site/en/tutorials/images/transfer_learning_with_hub.ipynb @@ -90,7 +90,7 @@ }, "outputs": [], "source": [ - "!pip install tf-keras\n", + "pip install tf-keras\n", "import numpy as np\n", "import time\n", "\n",