|
49 | 49 | "from nncf.tensorflow.helpers.model_creation import create_compressed_model\n",
|
50 | 50 | "from nncf.tensorflow.initialization import register_default_init_args\n",
|
51 | 51 | "from nncf.common.logging.logger import set_log_level\n",
|
| 52 | + "from openvino.runtime import serialize\n", |
| 53 | + "from openvino.tools import mo\n", |
52 | 54 | "\n",
|
53 | 55 | "set_log_level(logging.ERROR)\n",
|
54 | 56 | "\n",
|
|
60 | 62 | "BASE_MODEL_NAME = \"ResNet-18\"\n",
|
61 | 63 | "\n",
|
62 | 64 | "fp32_h5_path = Path(MODEL_DIR / (BASE_MODEL_NAME + \"_fp32\")).with_suffix(\".h5\")\n",
|
63 |
| - "fp32_sm_path = Path(OUTPUT_DIR / (BASE_MODEL_NAME + \"_fp32\"))\n", |
64 | 65 | "fp32_ir_path = Path(OUTPUT_DIR / \"saved_model\").with_suffix(\".xml\")\n",
|
65 | 66 | "int8_pb_path = Path(OUTPUT_DIR / (BASE_MODEL_NAME + \"_int8\")).with_suffix(\".pb\")\n",
|
66 |
| - "int8_pb_name = Path(BASE_MODEL_NAME + \"_int8\").with_suffix(\".pb\")\n", |
67 | 67 | "int8_ir_path = int8_pb_path.with_suffix(\".xml\")\n",
|
68 | 68 | "\n",
|
69 | 69 | "BATCH_SIZE = 128\n",
|
|
222 | 222 | "outputs": [],
|
223 | 223 | "source": [
|
224 | 224 | "IMG_SHAPE = IMG_SIZE + (3,)\n",
|
225 |
| - "model = ResNet18(input_shape=IMG_SHAPE)" |
| 225 | + "fp32_model = ResNet18(input_shape=IMG_SHAPE)" |
226 | 226 | ]
|
227 | 227 | },
|
228 | 228 | {
|
|
245 | 245 | "outputs": [],
|
246 | 246 | "source": [
|
247 | 247 | "# Load the floating-point weights.\n",
|
248 |
| - "model.load_weights(fp32_h5_path)\n", |
| 248 | + "fp32_model.load_weights(fp32_h5_path)\n", |
249 | 249 | "\n",
|
250 | 250 | "# Compile the floating-point model.\n",
|
251 |
| - "model.compile(loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n", |
252 |
| - " metrics=[tf.keras.metrics.CategoricalAccuracy(name='acc@1')])\n", |
| 251 | + "fp32_model.compile(\n", |
| 252 | + " loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n", |
| 253 | + " metrics=[tf.keras.metrics.CategoricalAccuracy(name='acc@1')]\n", |
| 254 | + ")\n", |
253 | 255 | "\n",
|
254 | 256 | "# Validate the floating-point model.\n",
|
255 |
| - "test_loss, acc_fp32 = model.evaluate(validation_dataset,\n", |
256 |
| - " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1']))\n", |
| 257 | + "test_loss, acc_fp32 = fp32_model.evaluate(\n", |
| 258 | + " validation_dataset,\n", |
| 259 | + " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1'])\n", |
| 260 | + ")\n", |
257 | 261 | "print(f\"\\nAccuracy of FP32 model: {acc_fp32:.3f}\")"
|
258 | 262 | ]
|
259 | 263 | },
|
260 |
| - { |
261 |
| - "cell_type": "markdown", |
262 |
| - "id": "b80f67d6", |
263 |
| - "metadata": {}, |
264 |
| - "source": [ |
265 |
| - "Save the floating-point model to the saved model, which will be later used for conversion to OpenVINO IR and further performance measurement." |
266 |
| - ] |
267 |
| - }, |
268 |
| - { |
269 |
| - "cell_type": "code", |
270 |
| - "execution_count": null, |
271 |
| - "id": "450cbcb2", |
272 |
| - "metadata": {}, |
273 |
| - "outputs": [], |
274 |
| - "source": [ |
275 |
| - "model.save(fp32_sm_path)\n", |
276 |
| - "print(f'Absolute path where the model is saved:\\n {fp32_sm_path.resolve()}')" |
277 |
| - ] |
278 |
| - }, |
279 | 264 | {
|
280 | 265 | "cell_type": "markdown",
|
281 | 266 | "id": "13b81167",
|
|
346 | 331 | "metadata": {},
|
347 | 332 | "outputs": [],
|
348 | 333 | "source": [
|
349 |
| - "compression_ctrl, model = create_compressed_model(model, nncf_config)" |
| 334 | + "compression_ctrl, int8_model = create_compressed_model(fp32_model, nncf_config)" |
350 | 335 | ]
|
351 | 336 | },
|
352 | 337 | {
|
|
365 | 350 | "outputs": [],
|
366 | 351 | "source": [
|
367 | 352 | "# Compile the INT8 model.\n",
|
368 |
| - "model.compile(optimizer=tf.keras.optimizers.Adam(lr=LR),\n", |
369 |
| - " loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n", |
370 |
| - " metrics=[tf.keras.metrics.CategoricalAccuracy(name='acc@1')])\n", |
| 353 | + "int8_model.compile(\n", |
| 354 | + " optimizer=tf.keras.optimizers.Adam(lr=LR),\n", |
| 355 | + " loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),\n", |
| 356 | + " metrics=[tf.keras.metrics.CategoricalAccuracy(name='acc@1')]\n", |
| 357 | + ")\n", |
371 | 358 | "\n",
|
372 | 359 | "# Validate the INT8 model.\n",
|
373 |
| - "test_loss, test_acc = model.evaluate(validation_dataset,\n", |
374 |
| - " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1']))\n", |
| 360 | + "test_loss, test_acc = int8_model.evaluate(\n", |
| 361 | + " validation_dataset,\n", |
| 362 | + " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1'])\n", |
| 363 | + ")\n", |
375 | 364 | "print(f\"\\nAccuracy of INT8 model after initialization: {test_acc:.3f}\")"
|
376 | 365 | ]
|
377 | 366 | },
|
|
393 | 382 | "scrolled": true,
|
394 | 383 | "tags": [],
|
395 | 384 | "test_replace": {
|
396 |
| - "fit(train_dataset,": "fit(validation_dataset," |
| 385 | + "train_dataset,": "validation_dataset," |
397 | 386 | }
|
398 | 387 | },
|
399 | 388 | "outputs": [],
|
400 | 389 | "source": [
|
401 | 390 | "# Train the INT8 model.\n",
|
402 |
| - "model.fit(train_dataset,\n", |
403 |
| - " epochs=2)\n", |
| 391 | + "int8_model.fit(\n", |
| 392 | + " train_dataset,\n", |
| 393 | + " epochs=2\n", |
| 394 | + ")\n", |
404 | 395 | "\n",
|
405 | 396 | "# Validate the INT8 model.\n",
|
406 |
| - "test_loss, acc_int8 = model.evaluate(validation_dataset,\n", |
407 |
| - " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1']))\n", |
| 397 | + "test_loss, acc_int8 = int8_model.evaluate(\n", |
| 398 | + " validation_dataset,\n", |
| 399 | + " callbacks=tf.keras.callbacks.ProgbarLogger(stateful_metrics=['acc@1'])\n", |
| 400 | + ")\n", |
408 | 401 | "print(f\"\\nAccuracy of INT8 model after fine-tuning: {acc_int8:.3f}\")\n",
|
409 | 402 | "print(f\"\\nAccuracy drop of tuned INT8 model over pre-trained FP32 model: {acc_fp32 - acc_int8:.3f}\")"
|
410 | 403 | ]
|
411 | 404 | },
|
412 |
| - { |
413 |
| - "cell_type": "markdown", |
414 |
| - "id": "7af453ef", |
415 |
| - "metadata": {}, |
416 |
| - "source": [ |
417 |
| - "Save the `INT8` model to the frozen graph (saved model does not work with quantized model for now). Frozen graph will be later used for conversion to OpenVINO IR and further performance measurement." |
418 |
| - ] |
419 |
| - }, |
420 |
| - { |
421 |
| - "cell_type": "code", |
422 |
| - "execution_count": null, |
423 |
| - "id": "6b208b6c", |
424 |
| - "metadata": {}, |
425 |
| - "outputs": [], |
426 |
| - "source": [ |
427 |
| - "compression_ctrl.export_model(int8_pb_path, 'frozen_graph')\n", |
428 |
| - "print(f'Absolute path where the int8 model is saved:\\n {int8_pb_path.resolve()}')" |
429 |
| - ] |
430 |
| - }, |
431 | 405 | {
|
432 | 406 | "cell_type": "markdown",
|
433 | 407 | "id": "1248a563",
|
434 | 408 | "metadata": {},
|
435 | 409 | "source": [
|
436 |
| - "## Export Frozen Graph Models to OpenVINO Intermediate Representation (IR)\n", |
| 410 | + "## Export Models to OpenVINO Intermediate Representation (IR)\n", |
437 | 411 | "\n",
|
438 |
| - "Use Model Optimizer to convert the Saved Model and Frozen Graph models to OpenVINO IR. The models are saved to the current directory.\n", |
| 412 | + "Use Model Optimizer Python API to convert the models to OpenVINO IR.\n", |
439 | 413 | "\n",
|
440 |
| - "For more information about Model Optimizer, see the [Model Optimizer Developer Guide](https://docs.openvino.ai/latest/openvino_docs_MO_DG_Deep_Learning_Model_Optimizer_DevGuide.html).\n", |
| 414 | + "For more information about Model Optimizer, see the [Model Optimizer Developer Guide](https://docs.openvino.ai/latest/openvino_docs_MO_DG_Python_API.html).\n", |
441 | 415 | "\n",
|
442 |
| - "Executing this command may take a while. There may be some errors or warnings in the output. When Model Optimization successfully exports to OpenVINO IR, the last lines of the output will include: `[ SUCCESS ] Generated IR version 11 model`" |
| 416 | + "Executing this command may take a while." |
443 | 417 | ]
|
444 | 418 | },
|
445 | 419 | {
|
|
449 | 423 | "metadata": {},
|
450 | 424 | "outputs": [],
|
451 | 425 | "source": [
|
452 |
| - "!mo --input_shape=\"[1,64,64,3]\" --input=data --saved_model_dir=$fp32_sm_path --output_dir=$OUTPUT_DIR" |
| 426 | + "model_ir_fp32 = mo.convert_model(\n", |
| 427 | + " fp32_model,\n", |
| 428 | + " input_shape=[1, 64, 64, 3],\n", |
| 429 | + ")" |
453 | 430 | ]
|
454 | 431 | },
|
455 | 432 | {
|
|
459 | 436 | "metadata": {},
|
460 | 437 | "outputs": [],
|
461 | 438 | "source": [
|
462 |
| - "!mo --input_shape=\"[1,64,64,3]\" --input=Placeholder --input_model=$int8_pb_path --output_dir=$OUTPUT_DIR" |
| 439 | + "model_ir_int8 = mo.convert_model(\n", |
| 440 | + " int8_model,\n", |
| 441 | + " input_shape=[1, 64, 64, 3],\n", |
| 442 | + ")" |
463 | 443 | ]
|
464 | 444 | },
|
465 | 445 | {
|
|
483 | 463 | },
|
484 | 464 | "outputs": [],
|
485 | 465 | "source": [
|
| 466 | + "serialize(model_ir_fp32, str(fp32_ir_path))\n", |
| 467 | + "serialize(model_ir_int8, str(int8_ir_path))\n", |
| 468 | + "\n", |
| 469 | + "\n", |
486 | 470 | "def parse_benchmark_output(benchmark_output):\n",
|
487 | 471 | " parsed_output = [line for line in benchmark_output if 'FPS' in line]\n",
|
488 | 472 | " print(*parsed_output, sep='\\n')\n",
|
|
0 commit comments