<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mateusz Sluszniak on Medium]]></title>
        <description><![CDATA[Stories by Mateusz Sluszniak on Medium]]></description>
        <link>https://medium.com/@mateusz.sluszniak?source=rss-fd34ad825c8c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*iiPR2aKDcf1r-DlK2WSuSg.jpeg</url>
            <title>Stories by Mateusz Sluszniak on Medium</title>
            <link>https://medium.com/@mateusz.sluszniak?source=rss-fd34ad825c8c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 10:03:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mateusz.sluszniak/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[EraserAI — How to create efficient app for edge device?]]></title>
            <link>https://medium.com/swmansion/eraserai-how-to-create-efficient-app-for-edge-device-04f09aa8072f?source=rss-fd34ad825c8c------2</link>
            <guid isPermaLink="false">https://medium.com/p/04f09aa8072f</guid>
            <category><![CDATA[image-inpainting]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[video-inpainting]]></category>
            <dc:creator><![CDATA[Mateusz Sluszniak]]></dc:creator>
            <pubDate>Mon, 16 Sep 2024 10:14:10 GMT</pubDate>
            <atom:updated>2024-09-26T11:15:00.830Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a9-yJWQSYwuNVpNtYeMFtg.png" /></figure><h3>EraserAI — How to Create an Efficient Image Manipulation App for Edge Devices?</h3><p>Imagine you’re trying to take a great photo in front of the famous Milan Cathedral, but there are too many people in the shot. You might find yourself waiting for the crowd to clear or searching for a better spot to capture your perfect selfie. Frustrating, isn’t it? If only there were a tool to solve this problem.</p><blockquote>Wait, isn’t it a good task for computer vision?</blockquote><p>Well, there is — and it’s a perfect job for computer vision! This technique, called inpainting, can help remove unwanted objects from a photo. In our example, inpainting would let us erase the people in the background and seamlessly fill in the empty space, making it look like they were never there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fHaRiDt0FpB-pvDa4M-rtA.jpeg" /><figcaption>Figure 1: (Left) Picture with people in the background; (Right) Picture with removed (inpainted) people.</figcaption></figure><p>We tackled inpainting tasks for videos in the past; if you haven’t seen it, use this <a href="https://blog.swmansion.com/pushing-the-boundaries-with-real-time-video-inpainting-35cd82c624fb">link</a>. However, image inpainting is tougher than video inpainting. Why? For a video, we have reference frames that give you a context of how intermediate frames should be inpainted. Hmm, so maybe we can do something similar? Let’s take a picture and use a reference image to guide the inpainting process by blending elements from the reference into the original.<strong> Since we already have some experience with reference inpainting, we decided to create a mobile app.</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=twitter&amp;url=https%3A//x.com/swmansion/status/1686002975276187648&amp;image=" width="500" height="281" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a7f723d1c13b9cfcd4566182ea7ac18/href">https://medium.com/media/9a7f723d1c13b9cfcd4566182ea7ac18/href</a></iframe><p>Before moving on to the next section, for better understanding, we introduce a term that is used frequently in the rest of the article: “mask.” A mask is an area that we want to change. In the case of the photo above, we want to alter sections of the image containing the crowd, so we refer to these areas as the mask.</p><h3>Motivations and Goals</h3><p>After the introduction, our motivation seems straightforward. We wanted to create an app with reference inpainting that will have an advantage over other inpainting apps thanks to the reference. It will mainly target images created in unfavorable, dynamic environments like crowded places.</p><p>Of course, we wanted to stand out from other similar solutions. At first, it seemed difficult, especially since there are a lot of inpainting apps available on the Apple Store, etc. We needed to think of some features that are not available in other apps. We researched currently available solutions on the Internet, and after that, we decided that our app should support:</p><ul><li>Computation on edge device</li><li>Leverage the potential of GPU computation on mobile</li><li>Add automated masking as an alternative to classic brush</li><li>Use reference image as a hint for a model</li></ul><p>Let&#39;s elaborate on them a little:</p><h4>Computation on Edge Device:</h4><p>We believe that the future of AI is tightly related to making it more accessible via edge devices. Reasons? Firstly, better privacy, as our data is not transferred anywhere. Secondly, when we run computation on the device, we don’t need to worry about scaling infrastructure. Next, no exposure to the network. This provides two benefits: first, we can use it in offline mode, implying full privacy; and second, there is no latency introduced by the transfer of data to a server. Additionally, it leads to lower energy consumption, which has a direct impact on the environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0AON48VCnUCZAWa_rA8m_A.jpeg" /><figcaption>Figure 2: Offline apps are safer because they protect you from data breaches.</figcaption></figure><h4>Leverage the Potential of GPU Computation on Mobile</h4><p>Modern smartphones have decent support for accelerated computation. For example, every iPhone since version 8 includes a neural engine — a unit specifically suited for AI computation, more so than a classic GPU. That’s why we decided to leverage the potential accumulated in them. Support for computing on the GPU solves the problem of slow inference.</p><h4>Add Automated Masking as an Alternative to Classic Brush</h4><p>As we tested available apps for inpainting, we discovered that almost all provide the functionality of a masking brush. The process is presented in Figure 3 (Left). The area for masking is provided directly by the user. We thought that it would be cool to add an alternative that would create a mask based on a simple click prompt. In Figure 3 (Right), we present a minimalist view of this feature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eXzZJLzzomVpqJaN9GNkAg.png" /><figcaption>Figure 3: (Left) Masking with a brush; (Right) Intelligent Masking.</figcaption></figure><h4>Use Reference Image as a Hint for a Model</h4><p>Inspired by the results of video reference inpainting, as explained in detail in this blog, we decided to move forward with image reference. This feature provides the inpainting model with a significant advantage over non-reference models. Regular models only generate inpainted areas based on the non-masked sections of the image, whereas reference inpainting models can intelligently “copy and paste” some parts of the image.</p><p>Okay, now you understand why we decided to make this app and what you can expect from it. Now, we can move on to creating the app.</p><h3>Peeking Under the Hood</h3><p>Since both the front-end and native code of the app in the project are typical layers, we won’t elaborate on them. Instead, we focus on exporting ML models into a format that fits into edge devices and enables compute operations on the GPU. That will be a task for <a href="https://pytorch.org/executorch/main/"><strong>ExecuTorch</strong></a>, a new library from the PyTorch team.</p><h4>Models Export</h4><p>For this part we used ExecuTorch. ExecuTorch is a library that provides tools to export machine learning models into a format suitable for edge devices. It allows for the use of optimized backends such as CoreML and XNNPack. Besides that, it provides a codebase for handling exported models on the backend.</p><p>We needed to export two models: a masking model and an inpainting model. For the masking model, we opted for EfficientSAM, and for the inpainting model, we selected our proprietary model. Now, we’ll make a quick remark. Intelligent masking is, in fact, a <strong>segmentation task</strong> — selecting sections of the picture based on input. In our case, the input consists of a set of points that we clicked. This is why, in the rest of the article, we refer to masking as segmentation, because here they are synonyms.</p><p>For both convenience and, more critically, performance reasons, we decided to split the segmentation model into three distinct sub-models — image encoder, prompt encoder, and mask decoder. This approach allows us to lazy load the models only when they are needed in the app. For example, we load the image encoder as soon as an image for inpainting is provided, before addressing the prompt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*gWSd9Fmg3t58BW3jFsa5jQ.png" /><figcaption>Figure 4: Segmentation model split into image encoder, prompt encoder, and mask decoder.</figcaption></figure><p>You may wonder how the split impacts the whole process of exporting. Well, it doesn’t. Usually, sub-models are distinct parts of the bigger model, and you have individual access to them. So, as long as they are implemented as torch.nn.Module, you can simply populate the whole model with weights and then export each sub-model separately. If you want to learn more about segmentation and inpainting models, please read the appendices at the end of this blog post.</p><p>Exporting model using ExecuTorch was presented in our previous blog posts (<a href="https://blog.swmansion.com/bringing-native-ai-to-your-mobile-apps-with-executorch-part-i-ios-f1562a4556e8">here</a>, <a href="https://blog.swmansion.com/bringing-native-ai-to-your-mobile-apps-with-executorch-part-ii-android-29431b6b9f7f">here</a>, and <a href="https://blog.swmansion.com/exporting-ai-models-on-android-with-xnnpack-and-executorch-3e70cff51c59">here</a>), so check them out as well as the <a href="https://pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html">ExecuTorch tutorials</a>. Now, we just briefly explain the exporting process.</p><h4>ExecuTorch Export Flow</h4><p>To run a model on a device, you first need to translate it into a computation graph. This is done with torch.export. Then, translate this graph into the edge dialect, optionally applying quantization beforehand. Now, some operations or the entire model might be delegated to the backend. At the end, we translate operations into an executable schema that is used on the device. Below is a picture presenting the graphical representation of the ExecuTorch workflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lYt0xidJ9O5cxBuO" /><figcaption>Figure 5: ExecuTorch Workflow. Source: <a href="https://pytorch.org/executorch/stable/tutorials/export-to-executorch-tutorial.html">PyTorch ExecuTorch Tutorial</a></figcaption></figure><p>As ExecuTorch is still in the beta phase, it is under dynamic development, and every day there are numerous improvements and fixes added to the repository. So, stay tuned with updates from ExecuTorch, as the user experience and variety of tools will continue to get better and better.</p><h3>Integrations and Results</h3><h4>App Walkthrough</h4><p>Now, let’s move on to the presentation of our prototype. We will guide you through our app with a simple example.</p><p>First, we need to select an image that we want to inpaint. If we’re satisfied with the image, then we can move to the next screen and select a reference image. Both steps are presented in the images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GbXmDSesN6PyP_YvMLI2Rg.png" /><figcaption>Figure 6: (Left) Image that we want to inpaint; (Right) Reference image.</figcaption></figure><p>Next, we select a mask to be inpainted. Here, we use the segmentation model to select all the masks based on clicks on the screen. Each tap generates a single mask. We can press the screen multiple times, effectively combining many different masks into one resultant mask. Once satisfied with the mask, hold the miniature of the masked area and proceed to the next step. Below, we present an example mask created with just one touch on the screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*wKPwOSSclxlkPiUla8UXlA.png" /><figcaption>Figure 7: Selected mask using intelligent segmentation functionality.</figcaption></figure><p>The final step is the inpainting itself. The resultant image does not contain an octopus as we inpainted it using reference image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*KmZDcaCwA5KJgWE0vifRZA.png" /><figcaption>Figure 8: Result of inpainting.</figcaption></figure><h4>Performance and Usability</h4><p>At the end of this article, we aim to discuss the performance optimizations implemented in our app. To significantly boost its overall performance, we chose CoreML as the backend delegate. Thanks to this enhancement, the total runtime of both masking and inpainting modules on iPhone 14 Pro Max is now reduced to less than <strong>1.52</strong> seconds — remarkably fast! By contrast, using the native Samsung inpainting app, which relies on cloud computations, the time taken to inpaint the image in Figure 1 was over 45 seconds on Samsung Galaxy S24 (across four sequenced inferences).</p><p>Last but not least, ExecuTorch offers an easy-to-use interface for both model exporting and operation. While hard to quantify, the intuitiveness of these tools is crucial when developing larger applications. In comparison, the alternative app for exporting models, TensorFlow Lite, provides a less user-friendly interface for accomplishing similar tasks.</p><h4>Conclusion</h4><p>To sum up, ExecuTorch is a perfect tool if you value both portability and performance, all fully integrated with the PyTorch ecosystem. It has an easy and well-documented export process. So, if you want to use your AI model on an edge device like a mobile phone, microcontroller, etc., then we highly recommend ExecuTorch.</p><h3>Next Steps</h3><p>Our app is still under development. We plan to release our app on the App Store. Currently, our software is available only for iOS, but we also plan to develop a version for Android. We still need to work on the quality of the inpainting itself. In the near future, we plan to share a repository with the code of the application, so stay tuned and wait for updates!</p><h3>Appendix A — Segmentation Model</h3><p>We decided to use a segmentation module from the family of Segment Anything models. The Segment Anything model was first introduced by Meta in 2023 in <a href="https://ai.meta.com/research/publications/segment-anything/">this paper</a>, and developed by Meta AI Research. Without going into details, the Segment Anything Model (SAM) consists of three parts:</p><ul><li><em>Heavyweight Image Encoder</em></li><li><em>Prompt Encoder</em></li><li><em>Mask Decoder</em></li></ul><p>The image encoder takes an input image and returns image embeddings. The prompt encoder accepts various types of prompt inputs such as points, boxes, or predefined masks, and returns prompt embeddings. Both the image and prompt embeddings are then passed to the mask decoder, which outputs the resultant mask(s). As some prompts might be ambiguous, the SAM was trained to return either one or three masks. The workflow is presented in Figure 9. To learn more about the SAM model, we strongly advise you to read the above-linked paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*DXmCZ2KquToXNo9x" /><figcaption>Figure 9: Overview of the architecture in the Segment Anything model. Source: <a href="https://segment-anything.com/">Segment Anything</a></figcaption></figure><p>However, the models provided by Meta were not exactly suitable for our purpose as they were too large for mobile devices. Fortunately, many variations of the original model have been created, and we decided to choose one of them. Our choice was <a href="https://github.com/yformer/EfficientSAM">EfficientSAM</a>, developed by Yunyang Xiong et al. It is a distilled version of the original SAM, offering a decent tradeoff between model size and segmentation quality.</p><h3>Appendix B — Inpainting Model</h3><p>Our inpainting model comes from what we used for video reference inpainting. It adopts an encoder-decoder framework based on the spatial-temporal decouple method, a concept introduced by Rui Liu et al. (DSTT). The process starts with the input passing through a hierarchical convolutional encoder. This is followed by several blocks of Swin Transformers, which are detailed extensively in <a href="https://arxiv.org/abs/2103.14030">the paper</a>. Outputs from these blocks then feed into a convolutional decoder. Below is a graphic that explains the architecture of the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XIiteQA7HUBr7cjN" /><figcaption>Figure 10: Overview of our model architecture. Details include: 1 — high-level diagram; 2 — hierarchical encoder used in DSTT; 3 — two consecutive Swin Transformers, the second one with window shift; 4 — our approach to windowed attention: no bounds along the temporal dimension and dividing into windows along spatial dimensions.</figcaption></figure><p>The inpainting model was trained on <a href="https://youtube-vos.org/">Youtube VOS</a> and then fine-tuned on DPED50k (available on <a href="https://github.com/Cameltr/TransRef">this repository</a>). Both datasets consist of <br>various images like buildings, objects, etc. Additionally, DPED50k is provided with a set of masks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04f09aa8072f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swmansion/eraserai-how-to-create-efficient-app-for-edge-device-04f09aa8072f">EraserAI — How to create efficient app for edge device?</a> was originally published in <a href="https://medium.com/swmansion">Software Mansion</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>