Skip to content

Commit

Permalink
Update the relevant notebook with a stand alone dataset download
Browse files Browse the repository at this point in the history
  • Loading branch information
Idan-BenAmi committed Dec 28, 2023
1 parent a135bed commit 545422c
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions tutorials/notebooks/example_keras_imagenet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,18 @@
},
"outputs": [],
"source": [
"from typing import Iterator, Tuple\n",
"from typing import Generator\n",
"\n",
"REPRESENTATIVE_DATASET_FOLDER = '/content/imagenet/val'\n",
"BATCH_SIZE = 50\n",
"n_iter=10\n",
"\n",
"def get_representative_dataset() -> Iterator:\n",
" \"\"\"Load the dataset and create a representative dataset for quantization.\n",
"# Create representative dataset generator\n",
"def get_representative_dataset() -> Generator:\n",
" \"\"\"A function that loads the dataset and returns a representative dataset generator.\n",
"\n",
" Returns:\n",
" Iterator: An iterator yielding batches of preprocessed images.\n",
" Generator: A generator yielding batches of preprocessed images.\n",
" \"\"\"\n",
"\n",
" # Load the dataset from folder\n",
Expand All @@ -238,11 +239,12 @@
" image_size=[224, 224],\n",
" shuffle=True,\n",
" crop_to_aspect_ratio=True,\n",
" interpolation='bilinear')\n",
" interpolation='bilinear') \n",
" # Preprocess the data\n",
" dataset = dataset.map(lambda x, y: (imagenet_preprocess_input(x, y)))\n",
"\n",
" def representative_dataset() -> Iterator[Tuple]:\n",
" \"\"\"Generate representative dataset for quantization.\n",
" def representative_dataset() -> Generator:\n",
" \"\"\"A generator function that yields batch of preprocessed images.\n",
"\n",
" Yields:\n",
" A batch of preprocessed images.\n",
Expand Down Expand Up @@ -372,7 +374,7 @@
"id": "0ce4fc61-e13c-48be-9f7c-d441ad76a386"
},
"source": [
"In order to evaluate our models, we first need to load the validation dataset."
"In order to evaluate our models, we first need to load the validation dataset. As before, please ensure that the dataset path has been set correctly."
]
},
{
Expand Down Expand Up @@ -466,7 +468,7 @@
{
"cell_type": "markdown",
"source": [
"Now, we can export the model to Keras and TFLite:"
"Now, we can export the model to Keras and TFLite. Please ensure that the file path has been set correctly."
],
"metadata": {
"id": "6YjIdiRRjgkL"
Expand All @@ -476,12 +478,11 @@
{
"cell_type": "code",
"source": [
"import tempfile\n",
"_, tflite_file_path = tempfile.mkstemp('.tflite') # Path of exported model\n",
"tflite_file_path = '/content/model.tflite'\n",
"mct.exporter.keras_export_model(model=quantized_model, save_model_path=tflite_file_path,\n",
" serialization_format=mct.exporter.KerasExportSerializationFormat.TFLITE, quantization_format=mct.exporter.QuantizationFormat.FAKELY_QUANT)\n",
"\n",
"_, keras_file_path = tempfile.mkstemp('.keras') # Path of exported model\n",
"keras_file_path = '/content/model.keras'\n",
"mct.exporter.keras_export_model(model=quantized_model, save_model_path=keras_file_path)\n"
],
"metadata": {
Expand Down

0 comments on commit 545422c

Please sign in to comment.