Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check existence of coco/train2017 before attemp to download (lar… #1195

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,48 @@
},
{
"cell_type": "markdown",
"source": [
"## Setup\n",
"### Install the relevant packages"
],
"id": "d74f9c855ec54081",
"metadata": {
"collapsed": false
},
"id": "d74f9c855ec54081"
"source": [
"## Setup\n",
"### Install the relevant packages"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c7fa04c9903736f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TF_VER = '2.14.0'\n",
"\n",
"!pip install -q tensorflow=={TF_VER}\n",
"!pip install -q pycocotools\n",
"!pip install 'huggingface-hub<=0.21.4'"
],
"metadata": {
"collapsed": false
},
"id": "7c7fa04c9903736f"
]
},
{
"cell_type": "markdown",
"source": [
"Install MCT (if it’s not already installed). Additionally, in order to use all the necessary utility functions for this tutorial, we also copy [MCT tutorials folder](https://github.com/sony/model_optimization/tree/main/tutorials) and add it to the system path."
],
"id": "57717bc8f59a0d85",
"metadata": {
"collapsed": false
},
"id": "57717bc8f59a0d85"
"source": [
"Install MCT (if it’s not already installed). Additionally, in order to use all the necessary utility functions for this tutorial, we also copy [MCT tutorials folder](https://github.com/sony/model_optimization/tree/main/tutorials) and add it to the system path."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9728247bc20d0600",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import sys\n",
Expand All @@ -76,25 +80,25 @@
" !pip install model_compression_toolkit\n",
"!git clone https://github.com/sony/model_optimization.git temp_mct && mv temp_mct/tutorials . && \\rm -rf temp_mct\n",
"sys.path.insert(0,\"tutorials\")"
],
"metadata": {
"collapsed": false
},
"id": "9728247bc20d0600"
]
},
{
"cell_type": "markdown",
"source": [
"### Download COCO evaluation set"
],
"id": "7a1038b9fd98bba2",
"metadata": {
"collapsed": false
},
"id": "7a1038b9fd98bba2"
"source": [
"### Download COCO evaluation set"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8bea492d71b4060f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"if not os.path.isdir('coco'):\n",
Expand All @@ -104,11 +108,7 @@
" !wget -nc http://images.cocodataset.org/zips/val2017.zip\n",
" !unzip -q -o val2017.zip -d ./coco\n",
" !echo Done loading val2017 images"
],
"metadata": {
"collapsed": false
},
"id": "8bea492d71b4060f"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -149,6 +149,10 @@
{
"cell_type": "code",
"execution_count": null,
"id": "56393342-cecf-4f64-b9ca-2f515c765942",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import model_compression_toolkit as mct\n",
Expand Down Expand Up @@ -213,59 +217,63 @@
" core_config=config,\n",
" target_platform_capabilities=tpc)\n",
"print('Quantized model is ready')"
],
"metadata": {
"collapsed": false
},
"id": "56393342-cecf-4f64-b9ca-2f515c765942"
]
},
{
"cell_type": "markdown",
"id": "3be2016acdc9da60",
"metadata": {
"collapsed": false
},
"source": [
"### Model Export\n",
"\n",
"Now, we can export the quantized model, ready for deployment, into a `.keras` format file. Please ensure that the `save_model_path` has been set correctly. "
],
"metadata": {
"collapsed": false
},
"id": "3be2016acdc9da60"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"mct.exporter.keras_export_model(model=quant_model, save_model_path='./qmodel.keras')"
],
"id": "72dd885c7b92fa93",
"metadata": {
"collapsed": false
},
"id": "72dd885c7b92fa93"
"outputs": [],
"source": [
"mct.exporter.keras_export_model(model=quant_model, save_model_path='./qmodel.keras')"
]
},
{
"cell_type": "markdown",
"id": "ba1ade49894e4e22",
"metadata": {
"collapsed": false
},
"source": [
"\n",
"### Gradient-Based Post Training Quantization using Model Compression Toolkit\n",
"Here we demonstrate how to further optimize the quantized model performance using gradient-based PTQ technique.\n",
"**Please note that this section is computationally heavy, and it's recommended to run it on a GPU. For fast deployment, you may choose to skip this step.** \n",
"\n",
"We will start by loading the COCO training set, and re-define the representative dataset accordingly. "
],
"metadata": {
"collapsed": false
},
"id": "ba1ade49894e4e22"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5276ec7291d28603",
"metadata": {
"collapsed": false,
"tags": [
"long_run"
]
},
"outputs": [],
"source": [
"!wget -nc http://images.cocodataset.org/zips/train2017.zip\n",
"!unzip -q -o train2017.zip -d ./coco\n",
"!echo Done loading train2017 images\n",
"if not os.path.isdir('coco/train2017'):\n",
" !wget -nc http://images.cocodataset.org/zips/train2017.zip\n",
" !unzip -q -o train2017.zip -d ./coco\n",
" !echo Done loading train2017 images\n",
"\n",
"REPRESENTATIVE_DATASET_FOLDER = './coco/train2017/'\n",
"REPRESENTATIVE_DATASET_ANNOTATION_FILE = './coco/annotations/instances_train2017.json'\n",
Expand All @@ -281,28 +289,28 @@
"\n",
"# Get representative dataset generator\n",
"representative_dataset_gen = get_representative_dataset(n_iters, rep_data_loader)"
],
"metadata": {
"tags": [
"long_run"
],
"collapsed": false
},
"id": "5276ec7291d28603"
]
},
{
"cell_type": "markdown",
"source": [
"Next, we'll set up the Gradient-Based PTQ configuration and execute the necessary MCT command. Keep in mind that this step can be time-consuming, depending on your runtime."
],
"id": "fce524abd2f1e750",
"metadata": {
"collapsed": false
},
"id": "fce524abd2f1e750"
"source": [
"Next, we'll set up the Gradient-Based PTQ configuration and execute the necessary MCT command. Keep in mind that this step can be time-consuming, depending on your runtime."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30f0a0c1c497ba2",
"metadata": {
"collapsed": false,
"tags": [
"long_run"
]
},
"outputs": [],
"source": [
"# Specify the necessary configuration for Gradient-Based PTQ.\n",
Expand All @@ -319,14 +327,7 @@
" target_platform_capabilities=tpc)\n",
"\n",
"print('Quantized model is ready')"
],
"metadata": {
"tags": [
"long_run"
],
"collapsed": false
},
"id": "30f0a0c1c497ba2"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -399,6 +400,10 @@
},
{
"cell_type": "markdown",
"id": "6d93352843a27433",
"metadata": {
"collapsed": false
},
"source": [
"\\\n",
"Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.\n",
Expand All @@ -414,14 +419,13 @@
"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."
],
"metadata": {
"collapsed": false
},
"id": "6d93352843a27433"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
Expand All @@ -438,9 +442,6 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
Expand Down
Loading