{"id":8854,"date":"2022-01-03T10:19:20","date_gmt":"2022-01-03T15:19:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/deep-learning\/?p=8854"},"modified":"2022-01-03T10:19:20","modified_gmt":"2022-01-03T15:19:20","slug":"deep-learning-for-computer-vision-using-python-and-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/01\/03\/deep-learning-for-computer-vision-using-python-and-matlab\/","title":{"rendered":"Deep Learning for Computer Vision using Python and MATLAB"},"content":{"rendered":"<em>This post is from\u00a0<a href=\"http:\/\/www.ogemarques.com\/\">Oge Marques, PhD<\/a>\u00a0and Professor of Engineering and Computer Science at FAU. Oge is a\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/sFvRCERy7AhoOq41INYhhZ?domain=sigmaxi.org\">Sigma Xi Distinguished Speaker<\/a>,\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/SYBFCG6A7DIZE83WI7UFu1?domain=ogemarques.com\/\">book author<\/a>, and\u00a0<a href=\"https:\/\/protect-us.mimecast.com\/s\/RPeGCJ61jJILln9pizR7yv?domain=aaas.org\">AAAS Leshner Fellow<\/a>.\u00a0He also happens to be a MATLAB aficionado and has been using MATLAB in his classroom for more than 20 years. You can follow him on Twitter (<a href=\"https:\/\/twitter.com\/ProfessorOge\">@ProfessorOge<\/a>). In this blog post, Oge will cover how to do Deep Learning using both Python and MATLAB for a Computer Vision example.<\/em>\r\n<h6><\/h6>\r\nDeep Learning (DL) techniques have changed the field of computer vision significantly during the last decade, providing state-of-the-art solutions for classical tasks (e.g.,\u00a0<a href=\"https:\/\/paperswithcode.com\/task\/object-detection\" target=\"_blank\" rel=\"nofollow noopener\">object detection<\/a>\u00a0and\u00a0<a href=\"https:\/\/paperswithcode.com\/sota\/image-classification-on-imagenet\" target=\"_blank\" rel=\"nofollow noopener\">image classification<\/a>) and opening the doors for solving challenging new problems, such as\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/images\/get-started-with-gans-for-image-to-image-translation.html\" target=\"_blank\" rel=\"nofollow noopener\">image-to-image translation<\/a>\u00a0and\u00a0<a href=\"https:\/\/visualqa.org\/\" target=\"_blank\" rel=\"nofollow noopener\">visual question answering (VQA)<\/a>.\r\n<h6><\/h6>\r\nThe success and popularization of DL in computer vision and related areas (e.g., medical image analysis) has been fostered, in great part, by the availability of rich tools, apps and frameworks in the\u00a0<a href=\"https:\/\/keras.io\/\" target=\"_blank\" rel=\"nofollow noopener\">Python<\/a>\u00a0and\u00a0<a href=\"https:\/\/www.mathworks.com\/solutions\/deep-learning.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB<\/a>\u00a0ecosystems.\r\n<h6><\/h6>\r\nIn this blog post, I will show how your team can use both MATLAB and Python effectively and provide an easy-to-follow recipe that you should allow you to leverage \u201cthe best of both worlds\u201d when building computer vision solutions using deep learning.\r\n<h6><\/h6>\r\n<h1>Background<\/h1>\r\n<h6><\/h6>\r\n<a href=\"https:\/\/www.python.org\/\" rel=\"nofollow\">Python<\/a>\u00a0is a programming language created by Guido van Rossum in the early 1990s. It has been adopted by many data scientists and machine\/deep learning researchers thanks to popular packages (e.g.,\u00a0<a href=\"https:\/\/scikit-learn.org\/\" rel=\"nofollow\">scikit-learn<\/a>) and frameworks (e.g.,\u00a0<a href=\"https:\/\/keras.io\/\" target=\"_blank\" rel=\"nofollow noopener\">Keras<\/a>,\u00a0<a href=\"https:\/\/www.tensorflow.org\/\" target=\"_blank\" rel=\"nofollow noopener\">TensorFlow<\/a>,\u00a0<a href=\"https:\/\/pytorch.org\/\" target=\"_blank\" rel=\"nofollow noopener\">PyTorch<\/a>).\r\n<h6><\/h6>\r\n<a href=\"https:\/\/www.mathworks.com\/products\/matlab.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB<\/a>\u00a0is a programming and scientific computing platform used to analyze data, develop algorithms, and create models in a variety of fields of science and engineering. It has a successful history of widespread adoption by engineers and researchers in industry and academia. It features many specialized\u00a0<a href=\"https:\/\/www.mathworks.com\/products.html\" target=\"_blank\" rel=\"nofollow noopener\">toolboxes<\/a>\u00a0which encapsulate relevant algorithms, interactive tools, and rich examples in areas such as\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/statistics.html\" target=\"_blank\" rel=\"nofollow noopener\">machine learning<\/a>,\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/deep-learning.html\" target=\"_blank\" rel=\"nofollow noopener\">deep learning<\/a>,\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/image.html\" target=\"_blank\" rel=\"nofollow noopener\">image processing<\/a>, and\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/computer-vision.html\" target=\"_blank\" rel=\"nofollow noopener\">computer vision<\/a>\u00a0(to mention but a few). MATLAB also has a vibrant\u00a0<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/\" target=\"_blank\" rel=\"nofollow noopener\">community of users<\/a>\u00a0who contribute additional functionality (including\u00a0<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?group=apps\" target=\"_blank\" rel=\"nofollow noopener\">apps<\/a>\u00a0and entire\u00a0<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?group=toolboxes\" target=\"_blank\" rel=\"nofollow noopener\">toolboxes<\/a>) and a growing presence in popular code-sharing repositories such as\u00a0<a href=\"https:\/\/github.com\/mathworks\" target=\"_blank\" rel=\"nofollow noopener\">GitHub<\/a>.\r\n<h6><\/h6>\r\nIn my personal experience, I have used both MATLAB (for 25 years and counting) and Python (for less than a decade) in different research projects, classes, bootcamps, and publications, mostly in the context of image processing\/analysis, computer vision, and (more recently) data science, machine learning, and deep learning.\r\n<h6><\/h6>\r\nI have also worked with multidisciplinary teams who adopt a variety of tools and are well-versed in diverse skill sets. I know how important it is to promote and facilitate the adoption of a streamlined and well-documented deep learning workflow. I am also a strong proponent of always using the best available tools to get the job done in the best possible way. Fortunately, you can use the two languages together, which we will show next.\r\n<h6><\/h6>\r\n<h1>Context and scope<\/h1>\r\n<h6><\/h6>\r\nThe interoperability of MATLAB and Python has been extensively documented in\u00a0<a href=\"https:\/\/youtu.be\/RlMgQDvvAFQ\" target=\"_blank\" rel=\"nofollow noopener\">videos<\/a>,\u00a0<a href=\"https:\/\/www.mathworks.com\/videos\/using-matlab-with-python-1591216182793.html\" target=\"_blank\" rel=\"nofollow noopener\">webinars<\/a>, blog posts, and the\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/matlab\/matlab-and-python.html\" target=\"_blank\" rel=\"nofollow noopener\">official MATLAB documentation<\/a>. These resources can be extremely valuable when learning\u00a0<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72852-using-matlab-with-python\" target=\"_blank\" rel=\"nofollow noopener\">how to call Python scripts from MATLAB and vice-versa<\/a>.\r\n\r\n<a href=\"https:\/\/mathinking.github.io\/blog\/en\/a-trick-you-dont-know-about-python-matlab\/\" target=\"_blank\" rel=\"nofollow noopener\">Some of the main reasons<\/a>\u00a0for calling MATLAB from Python can be motivated by the need to:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li>Promote code integration among team members and collaborators using different frameworks and tools.<\/li>\r\n \t<li>Leverage functionality only available in MATLAB, such as\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/referencelist.html?type=app\" target=\"_blank\" rel=\"nofollow noopener\">apps<\/a>\u00a0and toolboxes (including third-party ones contributed by the MATLAB community).<\/li>\r\n<\/ol>\r\n<h6><\/h6>\r\nIn this blog post, I focus on\u00a0<strong>integrating MATLAB apps into a Python deep learning workflow for computer vision and image analysis tasks<\/strong>, with emphasis on the data preparation stage of the traditional deep learning workflow (Figure 1). More specifically, I show how your team can leverage the rich interactive capabilities of selected MATLAB apps to prepare, label, annotate, and preprocess your data\u00a0<em><u>before<\/u><\/em>\u00a0using it as the input to your neural network \u2013 and everything else that follows in the traditional deep learning pipeline.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-8890 size-large\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture16-1024x151.jpg\" alt=\"\" width=\"1024\" height=\"151\" \/>\r\n<h6><\/h6>\r\nFigure 1: Basic deep learning workflow.\r\n<h6><\/h6>\r\nI will assume that: (1) you have a deep learning pipeline for computer vision in Python that you plan to adapt and reuse for a new (set of) task(s); and (2) the images associated with the new task(s) will require interactive actions, such as annotation, labeling, and segmentation.\r\n<h6><\/h6>\r\n<h1>The basic recipe<\/h1>\r\n<h6><\/h6>\r\nAssuming that you have\u00a0<a href=\"https:\/\/www.mathworks.com\/products\/get-matlab.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB installed and configured in your machine<\/a>\u00a0and your favorite Python setup (e.g., using Jupyter notebooks), calling MATLAB from a Python script is a straightforward process, whose main steps are:\r\n<h6><\/h6>\r\n<ol>\r\n \t<li><em>(In MATLAB)<\/em>\u00a0Install the\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_external\/get-started-with-matlab-engine-for-python.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB Engine API for Python<\/a>, which provides a Python package called matlab that allows you to call MATLAB functions and exchange data between Python and MATLAB.<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Configure paths and working directory.<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Start a new MATLAB process in the background:<\/li>\r\n<\/ol>\r\n<code>import matlab.engine<\/code>\r\n\r\n<code>eng = matlab.engine.start_matlab('-desktop')<\/code>\r\n<ol start=\"4\">\r\n \t<li><em>(In Python)<\/em>\u00a0Set up your variables (e.g., path to image folders).<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Call a MATLAB app of your choice (e.g., Image Labeler app).<\/li>\r\n \t<li><em>(In MATLAB)<\/em>\u00a0Work (interactively) with the selected app and export results to variables in the workspace.<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Save the variables needed for the rest of the workflow, e.g., image filenames and associated labels (and their bounding boxes).<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Use the variables as needed, e.g., processing tabular data using pandas and using image-related labels as ground truth.<\/li>\r\n \t<li>Repeat steps 3 through 7 as many times as needed in your workflow.<\/li>\r\n \t<li><em>(In Python)<\/em>\u00a0Quit the MATLAB engine:<\/li>\r\n<\/ol>\r\n<code>eng.exit()<\/code>\r\n<h1>An example<\/h1>\r\n<h6><\/h6>\r\nHere is an\u00a0<a href=\"https:\/\/github.com\/ogemarques\/python-matlab\" target=\"_blank\" rel=\"nofollow noopener\">example<\/a>\u00a0of how to use Python and MATLAB together for two different tasks within the scope of medical image analysis (using deep learning): skin lesion segmentation and (medical) image (ROI) labeling.\r\n<h6><\/h6>\r\nDespite the differences among them, each task follows the same basic recipe presented earlier. The specifics of each case are described next.\r\n<h6><\/h6>\r\n<h2>Task A: Skin Lesion Segmentation<\/h2>\r\n<strong>The Task:<\/strong>\u00a0Given a dataset of images containing skin lesions, we want to build a deep learning solution for segmenting each image, i.e., classifying each pixel as belonging to either the lesion (foreground) or the rest of the image (background).\r\n<h6><\/h6>\r\n<strong>The Problem:<\/strong>\u00a0In order to train and validate a deep neural network for image segmentation, we need to input both the images as well as the segmentation masks (Figure 2), which are essentially binary images where foreground pixels (in this case corresponding to the lesion) are labeled white and background pixels are marked as black. The job of the network is to learn the segmentation masks for new images.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"624\" height=\"219\" class=\"alignnone size-full wp-image-8887\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture17.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nFigure 2: Skin lesion segmentation: input image (left); binary segmentation mask (center);\r\nsegmented image, with green contour outlining the lesion area (right).\r\n<h6><\/h6>\r\nThe basic workflow usually consists of using convolutional network architectures, such as\u00a0<a href=\"https:\/\/arxiv.org\/abs\/1505.04597\" target=\"_blank\" rel=\"nofollow noopener\">U-net<\/a>\u00a0and its variations, for which there are multiple examples of implementation in\u00a0<a href=\"https:\/\/github.com\/rezazad68\/BCDU-Net\" target=\"_blank\" rel=\"nofollow noopener\">Python<\/a>\u00a0and\u00a0<a href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2021\/05\/10\/semantic-segmentation-for-medical-imaging\/\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB<\/a>. A crucial component of the solution, however, is the manual creation of the binary masks needed for training and validation. Except for a few publicly available datasets, this time-consuming and specialized task must be performed using a powerful interactive tool.\r\n<h6><\/h6>\r\n<strong>The Solution:<\/strong>\u00a0Use the\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/images\/image-segmentation-using-the-image-segmenter-app.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB\u00a0<em>Image Segmenter<\/em>\u00a0app<\/a>\u00a0to create the binary masks and leverage the existing (Python, for the sake of this example) workflow for everything else. Image Segmenter allows you to create masks manually and provides several (semi-)automatic techniques to speed up the process and refine the results (Figures 3 and 4). Both the final segmentation mask image and the segmented version of the original image can be exported to the MATLAB workspace and\/or saved to disk.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"648\" class=\"alignnone size-large wp-image-8884\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture18-1024x648.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nFigure 3: Image Segmenter app: loading an image containing a skin lesion.\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"647\" class=\"alignnone size-large wp-image-8881\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture19-1024x647.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nFigure 4: Image Segmenter app: result of applying the Graph Cut algorithm after having selected a few foreground control points (in green) and a single background control point (in red).\u00a0The mask appears overlaid on top of the original image.\r\n<h6><\/h6>\r\n<h2>Task B: (Medical) Image (ROI) Labeling<\/h2>\r\n<strong>The Task:<\/strong>\u00a0In a similar context to Task A, we want to build a deep learning solution for detecting regions of interest (ROIs) in each image, i.e., placing a boundary around each relevant region in the image. The most common ROI will be a lesion; other possible ROIs could include stickers, ruler markers, water bubbles, ink marks, and other artifacts.\r\n<h6><\/h6>\r\n<strong>The Problem:<\/strong>\u00a0To train and validate a deep neural network for ROI\/object detection, we need to input both the images as well as the labels and coordinates of the relevant ROIs, which can be expressed as rectangles (most common), polygons, or pixel-based masks (similarly to the masks used in segmentation). The job of the network is to learn the location and labels of the relevant ROIs for new images.\r\n<h6><\/h6>\r\nOnce again, similarly to what we saw in Task A, a crucial component of the solution is the manual creation of the ROIs (polygons and labels) needed for training and validation. Except for a few publicly available datasets, this time-consuming and specialized task must be performed using a powerful interactive tool.\r\n<h6><\/h6>\r\n<strong>The Solution:<\/strong>\u00a0Use the\u00a0<a href=\"https:\/\/www.mathworks.com\/help\/vision\/ug\/get-started-with-the-image-labeler.html\" target=\"_blank\" rel=\"nofollow noopener\">MATLAB\u00a0<em>Image Labeler<\/em>\u00a0app<\/a>\u00a0to create and label the ROIs and leverage the existing workflow for everything else. Image Labeler allows you to create ROI labels of different shape, assign them different names and colors, and provides several algorithms to help automate and speed up the process and refine the results (Figure 5). The resulting ROIs can be exported to the MATLAB workspace and subsequently used as variables in your Python code (see\u00a0<a href=\"https:\/\/github.com\/ogemarques\/python-matlab\" target=\"_blank\" rel=\"nofollow noopener\">example on GitHub<\/a>\u00a0for details).\r\n<h6><\/h6>\r\n<img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"647\" class=\"alignnone size-large wp-image-8878\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture20-1024x647.png\" alt=\"\" \/>\r\n<h6><\/h6>\r\nFigure 5: Image Labeler app in the context of dermoscopic images containing artifacts.\r\nThe selected image contains two rectangular ROIs, labeled as\u00a0<em>lesion<\/em>\u00a0and\u00a0<em>sticker<\/em>.\r\n<h6><\/h6>\r\n<h1>Key takeaways<\/h1>\r\n<h6><\/h6>\r\nDeep Learning projects are often collaborative endeavors that require using the best tools for the job, enabling effective code integration, development, and testing strategies, promoting communication, and ensuring reproducibility of code. Your team can (and should) leverage the best of MATLAB and Python while developing your deep learning projects. In this blog post I have shown how to use Python and MATLAB together for a few tasks related to computer vision and medical image analysis problems.\r\n<h6><\/h6>\r\nIntegration of Python and MATLAB goes significantly beyond the scope of this blog post; check out the resources listed below for more.\r\n<h6><\/h6>\r\n<h1>Learn more about it<\/h1>\r\n<h6><\/h6>\r\nThis blog post was inspired by\u00a0<a href=\"https:\/\/mathinking.github.io\/blog\/\" rel=\"nofollow\">recent blog posts by Lucas Garc\u00eda<\/a>\u00a0and a series of great videos by\u00a0<a href=\"https:\/\/github.com\/hgorr\/matlab-with-python\" target=\"_blank\" rel=\"nofollow noopener\">Heather Gorr<\/a>, Yann Debray, and colleagues. I strongly encourage you to follow them and check out their very informative examples and tutorials.\r\n\r\nIf you\u2019re interested in other aspects of the deep learning workflow, these are some blog posts in which I:\r\n<h6><\/h6>\r\n<ul>\r\n \t<li>Discuss\u00a0<a href=\"https:\/\/www.kdnuggets.com\/2020\/09\/mathworks-deep-learning-workflow.html\" target=\"_blank\" rel=\"nofollow noopener\">the entire process (including often forgotten steps) in greater detail<\/a>;<\/li>\r\n \t<li>Show\u00a0<a href=\"https:\/\/www.datasciencecentral.com\/profiles\/blogs\/training-deep-neural-networks-using-a-low-code-app-in-matlab\" target=\"_blank\" rel=\"nofollow noopener\">how to use a low-code app in MATLAB, the\u00a0Deep Network Designer,\u00a0for...<\/a>; and<\/li>\r\n \t<li>Teach you\u00a0<a href=\"https:\/\/www.datasciencecentral.com\/profiles\/blogs\/tracking-experiments-to-improve-ai-accuracy\" target=\"_blank\" rel=\"nofollow noopener\">how to manage and track multiple deep learning experiments<\/a>\u00a0with different network architectures, hyperparameters, and other options. Check them out!<\/li>\r\n<\/ul>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/deep-learning\/files\/2021\/12\/Picture16-1024x151.jpg\" onError=\"this.style.display ='none';\" \/><\/div><p>This post is from\u00a0Oge Marques, PhD\u00a0and Professor of Engineering and Computer Science at FAU. Oge is a\u00a0Sigma Xi Distinguished Speaker,\u00a0book author, and\u00a0AAAS Leshner Fellow.\u00a0He also happens to be a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/deep-learning\/2022\/01\/03\/deep-learning-for-computer-vision-using-python-and-matlab\/\">read more >><\/a><\/p>","protected":false},"author":180,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8854"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/users\/180"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/comments?post=8854"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8854\/revisions"}],"predecessor-version":[{"id":8914,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/posts\/8854\/revisions\/8914"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/media?parent=8854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/categories?post=8854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/deep-learning\/wp-json\/wp\/v2\/tags?post=8854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}