Modul 11 Optimasi Model Deep Learning
Modul 11 Optimasi Model Deep Learning
Modul Pembelajaran
Pendahuluan
Deep learning has been evolving at a very fast pace and its application can be found in
every aspect of human life at the moment. With thousands of models being released every
month and capabilities improving with each one of them it has become essential to
optimize them such that they can be put to practical use. More often than not modelers
find it difficult to productionalize their models owing to hardware limitations or low
throughput rate caused by unoptimized models.
There are various methods one can use to optimize deep learning models, eg:
Quantization, where one performs computations and stores tensors at lower bit widths
than floating point precision. As a result, this allows for a more compact model
representation and the use of high-performance vectorized operations on many hardware
platforms thereby, saving us a lot of inference computation cost and maintaining inference
accuracies. There are other methods as well such as pruning, knowledge distillation, etc.
Here comes OpenVino, which is an open-source toolkit for optimizing and deploying
deep learning models developed by Intel. It has the potential to improve model
performance and efficiency on various hardware platforms. One can also deploy
OpenVino converted models to cloud instances such as AWS C5 which have dedicated
Intel CPUs and would be highly efficient for running inferences on these optimized
models.
In this module, I would walk you through an example to convert the Pytorch CycleGAN
model to OpenVino and perform inference with the same and also observe how it helps in
improving latency. We will first convert the Pytorch model to Onnx and then to OpenVino.
Conversion Pipeline
### Assuming you have cloned the Cycle GAN repo from github:
https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
### run download_cyclegan_model.sh script to download pretrained
summer2winter_yosemite_pretrained model (instructions on github page)
### Create a jupyter notebook and follow the below code snippets
opt = TestOptions.parse()
opt.netG = 'resnet_9blocks'
opt.norm = 'instance'
opt.checkpoints_dir = './checkpoints' ### directory storing the pre-
trained model
opt.name = 'summer2winter_yosemite_pretrained'
model = create_model(opt)
model.setup(opt)
source openvino_env/bin/activate
mo -h ### This will return the help message for Model Optimizer if
installation finished successfully
### To convert, run the following in your terminal (you can use either
onnx model or the folded onnx model)
### input_model - specify your input onnx file
### data_type - use fp16 to convert model to fp16 precision for better
performance
### output_dir - directory where your openvino files will be saved
Now that we have both our Pytorch and OpenVino models, let’s perform inference on the
below sample image and compare their runtimes and qualitative outputs
input_image_filename = 'Yosemite_summer.jpg'
input_image = Image.open(input_image_filename).convert('RGB')
transformation = get_transform(opt, grayscale = False)
transformed_img = transformation(input_image).unsqueeze(0)
if opt.eval:
ie_obj = IECore()
openvino_network = ie_obj.read_network(model =
'summer2winter_yosemite_folded.xml', weights =
'summer2winter_yosemite_folded.bin')
executable_network = ie_obj.load_network(network = openvino_network,
device='CPU')
plt.subplot(1, 2, 2)
plt.gca().set_title('OpenVino Output', fontsize = 'xx-large')
plt.imshow(openvino_output_image)
From the above images, we can see that there are almost no discernible differences
between the two output images. Also, on comparing the inference runtimes for
the Pytorch and OpenVino models for 19 runs it is observed that
the summer2winter_yosemite CycleGAN OpenVino model runs 50% faster as compared to
the Pytorch one, which is a significant gain if we are trying to integrate this model into an
application for a real-world use case.
Conclusion
IR along with an operation set for representing the deep learning network which is typically
graph or IR. To learn more about IR and Opsets you can refer to their official docs here.
OpenVino has definitely made the path to productionalizing a deep learning model much
more efficient. Not only on various hardware devices, we can also deploy these models on