Skip to content

Commit

Permalink
update path for saving models (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
eaidova authored Jul 31, 2024
1 parent 0f25b62 commit 28324d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -147,7 +146,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -230,7 +228,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -323,7 +320,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -354,6 +350,8 @@
"bit_model_url = \"https://www.kaggle.com/models/google/bit/frameworks/TensorFlow2/variations/m-r50x1/versions/1\"\n",
"bit_m = hub.KerasLayer(bit_model_url, trainable=True)\n",
"\n",
"tf_model_dir = Path(\"bit_tf_model\")\n",
"\n",
"# Customize the model for the new task\n",
"model = tf.keras.Sequential([bit_m, tf.keras.layers.Dense(NUM_CLASSES, activation=\"softmax\")])\n",
"\n",
Expand All @@ -370,11 +368,10 @@
" epochs=FINE_TUNING_STEPS,\n",
" validation_data=validation_dataset.take(1000),\n",
")\n",
"model.save(\"./bit_tf_model/\", save_format=\"tf\")"
"model.save(tf_model_dir, save_format=\"tf\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -396,7 +393,7 @@
}
],
"source": [
"ir_path = Path(\"./bit_ov_model/bit_m_r50x1_1.xml\")\n",
"ir_path = Path(\"bit_ov_model/bit_m_r50x1_1.xml\")\n",
"if not ir_path.exists():\n",
" print(\"Initiating model optimization..!!!\")\n",
" ov_model = ov.convert_model(\"./bit_tf_model\")\n",
Expand All @@ -406,7 +403,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -434,7 +430,7 @@
}
],
"source": [
"tf_model = tf.keras.models.load_model(\"./bit_tf_model/\")\n",
"tf_model = tf.keras.models.load_model(tf_model_dir)\n",
"\n",
"tf_predictions = []\n",
"gt_label = []\n",
Expand All @@ -460,7 +456,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -500,7 +495,7 @@
},
"outputs": [],
"source": [
"ov_fp32_model = core.read_model(\"./bit_ov_model/bit_m_r50x1_1.xml\")\n",
"ov_fp32_model = core.read_model(ir_path)\n",
"ov_fp32_model.reshape([1, IMG_SIZE[0], IMG_SIZE[1], 3])\n",
"\n",
"# Target device set to CPU (Other options Ex: AUTO/GPU/dGPU/)\n",
Expand All @@ -520,7 +515,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -627,19 +621,19 @@
" return image\n",
"\n",
"\n",
"int8_ir_path = Path(\"bit_ov_int8_model/bit_m_r50x1_1_ov_int8.xml\")\n",
"val_ds = validation_ds.map(nncf_preprocessing, num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(1).prefetch(tf.data.experimental.AUTOTUNE)\n",
"\n",
"calibration_dataset = nncf.Dataset(val_ds)\n",
"\n",
"ov_fp32_model = core.read_model(\"./bit_ov_model/bit_m_r50x1_1.xml\")\n",
"ov_fp32_model = core.read_model(ir_path)\n",
"\n",
"ov_int8_model = nncf.quantize(ov_fp32_model, calibration_dataset, fast_bias_correction=False)\n",
"\n",
"ov.save_model(ov_int8_model, \"./bit_ov_int8_model/bit_m_r50x1_1_ov_int8.xml\")"
"ov.save_model(ov_int8_model, int8_ir_path)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand All @@ -657,7 +651,7 @@
},
"outputs": [],
"source": [
"nncf_quantized_model = core.read_model(\"./bit_ov_int8_model/bit_m_r50x1_1_ov_int8.xml\")\n",
"nncf_quantized_model = core.read_model(int8_ir_path)\n",
"nncf_quantized_model.reshape([1, IMG_SIZE[0], IMG_SIZE[1], 3])\n",
"\n",
"# Target device set to CPU by default\n",
Expand All @@ -680,7 +674,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -713,7 +706,6 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
Expand Down Expand Up @@ -780,11 +772,11 @@
"\n",
"\n",
"# OpenVINO FP32 model\n",
"ov_fp32_model = core.read_model(\"./bit_ov_model/bit_m_r50x1_1.xml\")\n",
"ov_fp32_model = core.read_model(ir_path)\n",
"ov_fp32_model.reshape([1, IMG_SIZE[0], IMG_SIZE[1], 3])\n",
"\n",
"# OpenVINO INT8 model\n",
"ov_int8_model = core.read_model(\"./bit_ov_int8_model/bit_m_r50x1_1_ov_int8.xml\")\n",
"ov_int8_model = core.read_model(int8_ir_path)\n",
"ov_int8_model.reshape([1, IMG_SIZE[0], IMG_SIZE[1], 3])\n",
"\n",
"# OpenVINO FP32 model inference\n",
Expand Down
30 changes: 17 additions & 13 deletions notebooks/tensorflow-hub/tensorflow-hub.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,12 @@
}
],
"source": [
"Path(IMAGE_PATH).parent.mkdir(parents=True, exist_ok=True)\n",
"IMAGE_PATH = Path(IMAGE_PATH)\n",
"\n",
"IMAGE_PATH.parent.mkdir(parents=True, exist_ok=True)\n",
"\n",
"r = requests.get(IMAGE_URL)\n",
"with Path(IMAGE_PATH).open(\"wb\") as f:\n",
"with IMAGE_PATH.open(\"wb\") as f:\n",
" f.write(r.content)\n",
"grace_hopper = PIL.Image.open(IMAGE_PATH).resize(IMAGE_SHAPE)\n",
"grace_hopper"
Expand Down Expand Up @@ -246,7 +248,9 @@
"metadata": {},
"outputs": [],
"source": [
"if not Path(MODEL_PATH).exists():\n",
"MODEL_PATH = Path(MODEL_PATH)\n",
"\n",
"if not MODEL_PATH.exists():\n",
" converted_model = ov.convert_model(model)\n",
" ov.save_model(converted_model, MODEL_PATH)"
]
Expand Down Expand Up @@ -459,13 +463,13 @@
"outputs": [],
"source": [
"CONTENT_IMAGE_URL = \"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/525babb8-1289-45f8-a3a5-e248f74dfb24\"\n",
"CONTENT_IMAGE_PATH = \"./data/YellowLabradorLooking_new.jpg\"\n",
"CONTENT_IMAGE_PATH = Path(\"./data/YellowLabradorLooking_new.jpg\")\n",
"\n",
"STYLE_IMAGE_URL = \"https://github.com/openvinotoolkit/openvino_notebooks/assets/29454499/c212233d-9a33-4979-b8f9-2a94a529026e\"\n",
"STYLE_IMAGE_PATH = \"./data/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg\"\n",
"STYLE_IMAGE_PATH = Path(\"./data/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg\")\n",
"\n",
"MODEL_URL = \"https://www.kaggle.com/models/google/arbitrary-image-stylization-v1/frameworks/tensorFlow1/variations/256/versions/2\"\n",
"MODEL_PATH = \"./models/arbitrary-image-stylization-v1-256.xml\""
"MODEL_PATH = Path(\"./models/arbitrary-image-stylization-v1-256.xml\")"
]
},
{
Expand Down Expand Up @@ -519,8 +523,8 @@
"metadata": {},
"outputs": [],
"source": [
"if not Path(MODEL_PATH).exists():\n",
" Path(MODEL_PATH).parent.mkdir(parents=True, exist_ok=True)\n",
"if not MODEL_PATH.exists():\n",
" MODEL_PATH.parent.mkdir(parents=True, exist_ok=True)\n",
" converted_model = ov.convert_model(model)\n",
" ov.save_model(converted_model, MODEL_PATH)"
]
Expand Down Expand Up @@ -594,18 +598,18 @@
"metadata": {},
"outputs": [],
"source": [
"if not Path(STYLE_IMAGE_PATH).exists():\n",
"if not STYLE_IMAGE_PATH.exists():\n",
" r = requests.get(STYLE_IMAGE_URL)\n",
" with open(STYLE_IMAGE_PATH, \"wb\") as f:\n",
" with STYLE_IMAGE_PATH.open(\"wb\") as f:\n",
" f.write(r.content)\n",
"if not Path(CONTENT_IMAGE_PATH).exists():\n",
"if not CONTENT_IMAGE_PATH.exists():\n",
" r = requests.get(CONTENT_IMAGE_URL)\n",
" with open(CONTENT_IMAGE_PATH, \"wb\") as f:\n",
" with CONTENT_IMAGE_PATH.open(\"wb\") as f:\n",
" f.write(r.content)\n",
"\n",
"\n",
"def load_image(dst):\n",
" image = cv2.imread(dst)\n",
" image = cv2.imread(str(dst))\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert image color to RGB space\n",
" image = image / 255 # Normalize to [0, 1] interval\n",
" image = image.astype(np.float32)\n",
Expand Down

0 comments on commit 28324d2

Please sign in to comment.