<?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 Mateus de Assis Silva on Medium]]></title>
        <description><![CDATA[Stories by Mateus de Assis Silva on Medium]]></description>
        <link>https://medium.com/@mateus.d.assis.silva?source=rss-482a85636e71------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*yLXwnfSbXGesnXldRRlvFw.jpeg</url>
            <title>Stories by Mateus de Assis Silva on Medium</title>
            <link>https://medium.com/@mateus.d.assis.silva?source=rss-482a85636e71------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 03:52:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mateus.d.assis.silva/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[Processing Audio with Python!]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/processing-audio-with-python-b6ec37ac2f40?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6ec37ac2f40</guid>
            <category><![CDATA[fourier-transform]]></category>
            <category><![CDATA[sound-effects]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[audio-processing]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Sat, 10 Feb 2024 13:10:41 GMT</pubDate>
            <atom:updated>2024-02-10T13:14:27.737Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to get rid of noises and add some cool effects.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1hG0_UwT3J-fMdwt1wCQdQ.jpeg" /><figcaption>“As y’all can see, this is a colorful line”</figcaption></figure><p>This may sound crazy, but in 2022’s Xmas night, I decided to play around with <a href="https://youtu.be/80tmW5n5J-U?si=1tzXQMWoXqCZmdtK">wine glass sounds</a> for fun (I was doing the dishes, you know?). That time, the difference was that I decided to record it and analyze it with Python. It led to the cool experiments we see here.</p><p>Audio is everywhere. We do not only listen to them but produce them a lot. How do they look and what can we do with them? That’s the kind of questions we are going to have answers for.</p><h3>Disclaimer</h3><p>The field of audio processing makes use of several concepts. Though important, I am not going to elaborate on them in here. I will instead point them out for the infovores out there:</p><ul><li>Fourier Transform/Series;</li><li>How audio recording works;</li><li>Impulse Response and its applications;</li><li>Signals and Systems Analysis.</li></ul><p>For the time being, I’ll turn to the following:</p><ul><li>What is an audio (from a coding perspective);</li><li>Sine waves and the notion of spectrums;</li><li>Filters and effects</li></ul><p>That said, we can start this post for real. And, by the way, for those out there who want to replicate what I did here, you can pop in my <a href="https://github.com/mtxslv/audio-processing-with-python">Github repo</a>. All the codes and examples are there. If you like it, please consider dropping a star ⭐. I would really appreciate it 🥹.</p><h3>Picture yourself sending a voice message</h3><p>Imagine you are listening to a tune that brings childhood memories back and you definitely need to share this nostalgic moment with a friend. You pick up your phone and start recording. You send them this voice message and naturally decide to listen to what was recorded. But to your surprise, the quality is not that great.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttps%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F1692647880%26show_artwork%3Dtrue&amp;display_name=SoundCloud&amp;url=https%3A%2F%2Fsoundcloud.com%2Fmtxslv%2Fmario-and-cup%3Fin%3Dmateus-de-assis-silva%2Fsets%2Faudio-processing-with-python%26si%3D7c69b439acf846e7883ed8d826f5e54c%26utm_source%3Dclipboard%26utm_medium%3Dtext%26utm_campaign%3Dsocial_sharing&amp;image=https%3A%2F%2Fsoundcloud.com%2Fimages%2Ffb_placeholder.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=soundcloud" width="800" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/e83aa412b91555d317b0f1686ccbfa90/href">https://medium.com/media/e83aa412b91555d317b0f1686ccbfa90/href</a></iframe><p>It happens a noisy whistle messed up with the recording. Dang! What should we do? If the source is now off, there is no way to record again! Or maybe the whistle will pop up again.</p><p>But wait… what if we manage to remove it someway. In the end, this is an audio file and people play around with audio all the time! But what is an audio to start with?</p><h3>Ok, but what is an ‘audio’ anyway?</h3><p>With Python, we can open an audio file using Scipy’s Wav utilities. Once this is done, we can see the samples that make it up.</p><blockquote>As I said before, I’ll not dive in how a sound (mechanic wave) is converted to a digital file, but here goes an idea: this process usually involves some preprocessing to dumb the noise and highlight what we want, followed by a converter that breaks the continuous signal into short units (samples) that are stored as numbers.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FwkeeCzyRUxfUC0gMSKnFg.png" /><figcaption>Number and more numbers…</figcaption></figure><p>A’ight, these things are cool and whatnot, but do not look really useful. How can we process audio with it? Well, we actually need a new way of looking at it.</p><h4>A new visualization</h4><p>Keeping in mind we need to find clues on how to get rid of the noise, the previous graph is a letdown: it does not hint anything. But luckly, some Signal Processing geeks have developed neat tools for us to use. The first one is the <strong><em>Fourier Transform</em></strong>.</p><p>In a Signal Processing course you’ll probably see the math behind it. It is nice, but a bit of out of scope here. So let’s jump to the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIb7KrR_d0JZXESL3DxJiA.png" /><figcaption>A 20 Hz sine wave.</figcaption></figure><p>The graph above shows a 20 Hz sine wave (that is, it oscillates 20 times in a sec). We can understand sound waves as sines and vice-versa. For instance, you can listen to some in <a href="https://musiclab.chromeexperiments.com/Oscillators/">Chrome Music Lab’s Oscillators experiment</a>. Now the trick is… let’s apply the Fourier Transform to the 20 Hz toy wave and see what happens.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*LHxsdyfCuCiOxS9jk_5GsQ.png" /><figcaption>The Fourier spectrum associated with the 20 Hz sine wave.</figcaption></figure><p>The Fourier Transform modified our signal such that its <em>frequencies are highlighted. </em>The original signal oscillated 20 times per second, then we have a peak at 20 Hz (ignore the peak at -20 Hz, it is just a reflection).</p><blockquote>We can visualize the frequencies that comprises our audio on the fly by extracting a spectrogram. This type of transformation applies Fourier Transform (or some other transform) on patches of audio, creating a 2D graph of the frequencies that make up our signal. You can play with it in <a href="https://musiclab.chromeexperiments.com/Spectrogram/">Chrome Music Lab’s Spectrogram experiment</a>.</blockquote><p>This is cool because it underlines the ‘information’ contents of our signal. To elaborate on that and get some intuition of what can be done, let’s play around more with some signals. First, let’s create a wave by weighting several frequencies together. To be more specifically, we have:</p><p><strong><em>resulting_wave = 1*sine_20_hz + 0.5*sine_30_hz + 0.1*sine_50_hz</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MwiwdbmHMmarPrCqFIms3w.png" /><figcaption>A wave resulting from the sum of 20, 30, and 50 Hz frequencies.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*poVQrQKV5v0Bi3w827tkVw.png" /><figcaption>The Fourier Transform can see the individual frequencies even when they are mixed in the input signal.</figcaption></figure><p>Sweet! The Fourier Transform can see the individual frequencies in the input signal even when we are clueless which they are. Moreover, it notices the different sines have different weights!</p><p>Could we remove some frequencies? Actually we can, using a Notch Filter. Such filter works by attenuating a band of frequencies (other frequencies are left unchanged).</p><blockquote>Ideally, we would like to remove only the frequency we are mad at. But the nearby are always affected too (that’s why we say the filter ‘attenuates a band of frequencies’). We can control how heavily they are altered, but can never make it perfect. The nomenclature for the band of frequencies that are modified is ‘stop band’.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/0*Y9XqGu4zeFI5mIL6.png" /><figcaption>Notch Filter Response. <a href="https://commons.wikimedia.org/wiki/File:Frekvencijski_odziv_aktivnog_Twin_T_reznog_filtra.png">Source</a>.</figcaption></figure><p>I’ll remove the frequencies 30 and 50 of our toy example. The result can be seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JzjFWKGHWEao0mX_VRGc7Q.png" /><figcaption>The sine wave swing a little. But it has 20Hz anyway.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/567/1*ZCjny3-a_H2hH4a9PgvQrw.png" /><figcaption>Only the peak at 20 Hz remains, mostly.</figcaption></figure><p>We can qualitatively compare how good is this reconstruction…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ssX3o48ALzvCcJCSylT6dw.png" /><figcaption>Original input wave and filtered wave.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMPuHiOUUY6SG8s-0MyCCQ.png" /><figcaption>Pure 20 Hz wave and Filtered (reconstructed). See the swing?</figcaption></figure><p>We now have a good theory to help us out. Let’s see how it fares in our real-world example.</p><h3>Hands on!</h3><p>Applying the same logic I introduced above, let’s first how our corrupted audio looks like upon applying our Fourier Transform to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GbXQ92Rufh3sQaKP2Qigcw.png" /><figcaption>Fourier Transform of the corrupted audio.</figcaption></figure><p>Interesting… we see those twin peaks again! This implies the noise infecting our audio has a sinusoidal form. According to the zoomed-in picture on the right, such a noise signal has a frequency of 936 Hz, approximately.</p><p>The next step is to filter it. By removing the frequency of 936 Hz we have another audio, which can be listened to, and seen, below.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttps%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F1692647847%26show_artwork%3Dtrue&amp;display_name=SoundCloud&amp;url=https%3A%2F%2Fsoundcloud.com%2Fmtxslv%2Ffiltered-audio-9360-5%3Fsi%3D78b1b69b81eb4145a2391f225c799096%26utm_source%3Dclipboard%26utm_medium%3Dtext%26utm_campaign%3Dsocial_sharing&amp;image=https%3A%2F%2Fsoundcloud.com%2Fimages%2Ffb_placeholder.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=soundcloud" width="800" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/3712c8ea723bde98353a0fda089dff42/href">https://medium.com/media/3712c8ea723bde98353a0fda089dff42/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*fyzkP4LbEhmvutgsPtVmzQ.png" /><figcaption>Filtered audio profile.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*rCLifWuRu8PKK8zaOb4-kQ.png" /><figcaption>Fourier Transform of filtered audio.</figcaption></figure><p>The first conclusion we have with such results is: the noise is not that bothersome anymore (we call it ‘atenuated’, remember?). Ok, it is still there, but compared to our previous audio, this is a good result. Visually, we notice the profile of our audio has changed. It does not have that wide volume of signal, and now it is more smooth and concentrated around the X-axis. As for the Fourier Transform, we see the peak (and its twin) around 936 has disappeared, as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YlSJQ_4bBgNJquQsd1XG4Q.png" /><figcaption>Audio before and after filtering.</figcaption></figure><h3>BONUS: Transfering effects</h3><p>As a bonus, let’s apply an effect to our audio. This works by mixing two signals: our audio and the <em>sound effect signature</em>. Such a signature is called in the technical realm as <em>impulse response, </em>but this is a whole other story.</p><p>The effect I’ll be using here mimics the echo we expect to hear in a large, long hall. Here is its signature:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttps%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F1692647814%26show_artwork%3Dtrue&amp;display_name=SoundCloud&amp;url=https%3A%2F%2Fsoundcloud.com%2Fmtxslv%2Flarge-long-echo-hall%3Fsi%3D010213901b864992a8537ae7c468187f%26utm_source%3Dclipboard%26utm_medium%3Dtext%26utm_campaign%3Dsocial_sharing&amp;image=https%3A%2F%2Fsoundcloud.com%2Fimages%2Ffb_placeholder.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=soundcloud" width="800" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/852eab29edf9bb16135d92325f951f6d/href">https://medium.com/media/852eab29edf9bb16135d92325f951f6d/href</a></iframe><blockquote>It might be surprising or counterintuitive to take such blast sound as a hall echo (which tends to be smoother), but this is due to the nature of the signature. An <em>impulse response</em> is just a system’s response to an impulse signal. Such an impulse is basically a short-term, high-intensity, signal. In other words, a blast.</blockquote><p>The mixing process is called a convolution. I’ll not dive deeper in it, but to keep you curious, I’ll say it is the basis of a lot technologies, including how image processing works. Whatever… let’s see what does such a mixing result in:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fw.soundcloud.com%2Fplayer%2F%3Furl%3Dhttps%253A%252F%252Fapi.soundcloud.com%252Ftracks%252F1692647841%26show_artwork%3Dtrue&amp;display_name=SoundCloud&amp;url=https%3A%2F%2Fsoundcloud.com%2Fmtxslv%2Fechoed-filtered-audio%3Fsi%3D5fdf7cd79c344b1d8ccd22bf1baa7d4d%26utm_source%3Dclipboard%26utm_medium%3Dtext%26utm_campaign%3Dsocial_sharing&amp;image=https%3A%2F%2Fsoundcloud.com%2Fimages%2Ffb_placeholder.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=soundcloud" width="800" height="166" frameborder="0" scrolling="no"><a href="https://medium.com/media/84a70333ffc889532d7b29b3f74e4409/href">https://medium.com/media/84a70333ffc889532d7b29b3f74e4409/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*wep5W5HtUBdkcqJqv8tP2w.png" /><figcaption>Filtered audio after echo effect.</figcaption></figure><p>We see the effect makes our audio thicker, and it does sound like was recorded on a large, long hall. It has to do with how the mixing is done. Next we see a side-to-side comparison.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lM30qSj7-Q_avGty9VDAwg.png" /><figcaption>Comparison of audios before and after applying the effect.</figcaption></figure><h3>Conclusion</h3><p>This is a cool experiment, but often audio processing is done in dedicated audio editing softwares. The goal was to introduce the concepts and methodology, so, if you are not in a research project, or a college subject, or developing audio programs, I think you’re not going to deal with audio the way I did.</p><p>Anyway, I hope you liked it… my first contact with these concepts was in a college subject and I wanted to try them out on a real-world setting ever since. If you build on this work and want to share, please come back! I’ll appreciate talking about it with you! Moreover, consider talking about it in an event! I am sure the community will like it. I did the same in a Python Meetup and it was amazing! If you want to know more what we had there, you can <a href="https://blog.grupyrn.org/aconteceu-16-meetup-grupyrn/">read about it here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OjguFIdu0JtfpkdUEh03jw.jpeg" /><figcaption>Oh, yeah! Community vibes, baby!</figcaption></figure><p>See you in the next post!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6ec37ac2f40" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The thing behind Machine Learning…]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/the-thing-behind-machine-learning-fafba11bec2?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/fafba11bec2</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Sun, 29 Oct 2023 12:32:38 GMT</pubDate>
            <atom:updated>2023-10-29T12:32:38.299Z</atom:updated>
            <content:encoded><![CDATA[<p>Overviewing ML’s basic idea with tensorflow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dYKiJvIKEu3xGDeFr0m3yw.jpeg" /><figcaption>Hey! That’s me!</figcaption></figure><p>In 15th July, 2023, SEBRAE RN hosted another Google I/O Extended in Natal, RN. In that day I was chosen to share some thoughts on Machine Learning. This post presents the same ideas I brought there. I hope you find this reading entertaining and instructive.</p><p>Machine Learning is a popular topic today. Every year, every month, <em>maybe every week</em> a new flabbergasting technology pops up, astounding everybody with their mind-blowing abilities. But how do they work? Behind the high volume of code that brings these services to life, there is an as-large volume of math and computer science basing every computation. To understand them, you don’t necessarily need to have a PhD. In fact, some of them are seen in graduation level courses. Naturally, they can be a little confusing the first time we meet them, but with the right approach we can build some intuition and step by step understand why it happens the way it happens.</p><p>That said, I’d like to discuss some of its ideas based on two toy examples and then show how they help us to solve real challenges. Though these toy examples are not authentic, they serve the purpose of demonstrating the math and the type of problems we might meet in real life. Moreover, by reasoning about them, we can visualize the inner mechanics of the models, which is related to the idea of Explanable AI.</p><p>Here it goes an overview of what we’ll see here:</p><ol><li>Rule-based programming and ML: data-driven solutions, matrices, geometric planes and probabilities;</li><li>The representation problem and the concept of Deep Learning: images, filters and convolutions, architectures and the idea of ‘training’;</li><li>Advices on ML practice;</li><li>Key takeaways.</li></ol><h3>Rule-based programming and Machine Learning</h3><p>Suppose you work on the team responsible for your college’s Computer Assisted Assessment System. So far the system supports multiple choice and checkbox questions. A new module is being developed, in which professors will have the option of chaining mathematical operations to compute the answers automatically. To do that, you are tasked with developing the square root module in Python. More specifically, it must return <em>None</em> when the <em>float </em>number provided is negative.</p><p>Ok, which logic to apply? Well, this is simple: the input is a <em>float</em> value (real number). If negative, return <em>None</em>. Return the square root otherwise. In Python:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*YiZLu1x93bL-ltFozMJPJw.png" /><figcaption>A simple Python code for Square Root calculation.</figcaption></figure><p>Great! You merge-request your code, which is accepted and deployed. The popularity of the new features of the system is so remarkable that you are invited to work on another project: the IT team of a local festival wants you to develop a system capable of estimating who will attend their next event.</p><p>Wait… what? How are you supposed to create a system like that? Well.. given the title of this post, you might <em>correctly </em>guess that it involves using Machine Learning. Now, suppose the IT team has access to some data associated with the people attending the events over time. For instance, suppose Instagram stories polls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*jeA7Ij5Ms7dBbIf_BR2JtA.png" /><figcaption>Spotted’s stories.</figcaption></figure><blockquote>Spotted is a local festival held in Natal, Brazil. It is known for its open bar and the artists performing.</blockquote><p>For simplicity, suppose you now have access to three pieces of information:</p><ol><li>How much people like the open bar;</li><li>How much people like the artists performing;</li><li>If the person attended or not the event.</li></ol><p>The first two informations were generated through Instagram slider polls, while the last is created by checking if the person attended or not the event.</p><blockquote>Again, for the sake of simplicity, we will not debate how this data is curated, cleaned, streamlined or stored. Let’s just suppose the data engineering team is skilled and will provide you with the right data.</blockquote><p>As the first step of you Data Science task, you need to see the distribution of data. Since it is 2-D, you can actually plot it in a XY graph:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nruAaoNmtZsrEGzvY2OkPQ.png" /><figcaption>Distribution of data.</figcaption></figure><p>Nice! Apparently, the people requires a higher-grade artist in order to attend the show. They are more tolerant with the Open Bar, though. Visually, this behaviour obeys a linear equation, in a way that we can actually separate the classes using a straight line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-nI0kVFLw1Qb2YSY5s_u-w.png" /><figcaption>Let’s add a line to the graph!</figcaption></figure><p>Nice! In the classes interface some points are “mixed”, causing them to stay in the wrong side of the line. Even so, in general, we got a pretty good separation!</p><p>What we did so far was to <em>qualitatively</em> separate the data classes in a way that, points ‘below the line’ are classified as ‘will not attend’ and those in the opposite side are the ones ‘will attend’. This is good, but dealing with numbers are better. Let’s apply some algebra here. The line crosses the Y axis in 65 and the X axis in 85. The line equation is then:</p><p><strong><em>y = (-65/85)x + 65</em></strong></p><p><strong><em>y = -0.7647x + 65</em></strong></p><blockquote>Do not get hung up about how I came up with the <em>exact values</em> 65.0 and 85.0. I made up this toy example, thus I can assure you these are the real thing.</blockquote><p>The line equation is cool, but it is not in the right format yet. Let’s move everybody to one side only and then separate variables and parameters:</p><p><strong><em>y + 0.7647x - 65 = 0 [let’s call it equation A]</em></strong></p><p>This way we can consider this expression a <em>geometric plane </em>equation. Let’s define a variable <em>z</em> that accounts for a given height in a given (X,Y) position:</p><p><strong><em>z = y + 0.7647x + 65 [and this one will be equation B]</em></strong></p><p>which can be seen as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n5EJDm2azglvJI7TWN0Bug.png" /><figcaption>A Z height for every (X,Y) position. Z = 0 leads to the original equation.</figcaption></figure><p>A last modification is necessary for the sake of Tensorflow operations… Let’s extract all numerical information in a form of <em>vectors </em>(think of it as lists of numbers):</p><p><strong><em>z = dot([x,y], [0.7647; 1]) + 65 [equation C]</em></strong></p><p><strong><em>z = dot(weights, inputs) + bias</em></strong></p><p>Here, we define the vector <strong><em>weights = [0.7647; 1] </em></strong>and a single value called <strong><em>bias = 65</em></strong>. The variable <strong><em>inputs </em></strong>is just the collection<strong><em> [x,y]</em></strong>. Don’t sweat the function <strong><em>dot(). </em></strong>To our purposes it just generates our linear equation back.</p><p>Anyway, going back to the first definition of <strong><em>z..</em></strong><em>.</em> we notice the original line equation (the A equation) is just a particular result for the situation when the plane (the B equation) touches the ‘floor’. With this new variable Z, we can <strong><em>extract the probabilities</em></strong> of a given person attending the event by passing Z to the sigmoid function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*AOanv-asFtmQ9jqpJ29_-g.png" /><figcaption>Sigmoid function: negative inputs glides the function near 0, while positive ones drift it towards 1</figcaption></figure><p>The result of passing Z through the sigmoid can be seen below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zAZfJKUd_LYtSbVuKkNEig.png" /><figcaption>The sigmoid extracts the probability of the person going to the event</figcaption></figure><p>Great! Now we have the basic mathematical framework for the problem, we can apply it using <a href="http://www.tensorflow.org/">Tensorflow</a> code. For those not familiar with the framework, it is <strong>a end-to-end open source machine learning platform </strong>(it implements mathematical stuff, basically).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*IzpblCCxl1oqv25-nL2gag.png" /><figcaption>Coding the previous mathematical function with TF</figcaption></figure><p>The first lines of code import the necessary dependencies:</p><ul><li>Numpy is a python lib for numerical operations;</li><li>Tensorflow is our good ol’ Machine Learning lib.</li></ul><p>We then define our numerical parameters using the variables <strong><em>weights </em></strong>and <strong><em>bias</em></strong>. By the way… don’t mind line 12. It is necessary as a mathematical formalism.</p><p>In line 15 we define <strong><em>is_going</em></strong> as a <strong><em>Sequential()</em></strong> object, which receives a list of mathematical operations to be <em>sequentially</em> applied (hence the name, got it?). Here we have only one operation, <strong><em>Dense()</em></strong>, which executes the equations we defined. To this object we pass the numerical parameters, the quantity of inputs and outputs, and the sigmoid function. Such object is what we usually call <em>a model</em>.</p><p>We are good to go. Let’s see how it performs on a batch of examples!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*nSPCJ9QAK5nF-Kdt_SIIsg.png" /><figcaption>Applying our function to a batch of examples.</figcaption></figure><p>The code above submits a batch (a set) of inputs to the model. If you look closely and compare with the graph displaying the distribution of answers, you’ll notice the first two samples are points in the region ‘not going’ and ‘going’, respectively. The last two lines are points over the line. As expected, the first answer is a probability near 0, while the second is 1. The last two answers are near 50%.</p><p>Curious, isn’t it? We successfully learned a rule from the data. The ‘rule’ is the sigmoid of the Z value. Naturally, if the data change (the points change position or class/color), the rule (equation) must be updated.</p><p>I hope it is then intuitive to understand the following illustration. Such image is often displayed in the beginning of the comparison between ML and Traditional Programming, but I consider it confusing to be presented first without a proper introduction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WrH_WKKltU8yt0Xu296zzA.png" /><figcaption><a href="https://datalya.com/blog/machine-learning/machine-learning-vs-traditional-programming-paradigm">Machine Learning and Traditional Programming</a></figcaption></figure><p>As can be seen, in the Traditional Programming framework, we provide <strong><em>rules</em></strong> that operate on the <strong><em>data</em></strong> (which is the code itself, like the one that takes the square root), and we end up with <strong><em>answers</em></strong> (let’s say, the expected return). In Machine Learning, we have the <strong><em>data</em></strong> AND the <strong><em>answers</em></strong>, and based on them the ML algorithm will think up the <strong><em>rules</em></strong>. Moreover, we saw such rule is a mathematical formula.</p><p>Of course, there are a myriad of topics I didn’t cover. And it was on purpose, since this is meant to be a gentle introduction. But as a side note, here it goes a lilliputian list of ideas fundamental for a deep understanding of what I showed:</p><ul><li>The very definition of what Machine Learning is;</li><li>Prerequisites: Linear Algebra, (Multivariable) Calculus, Statistics/Probability;</li><li>Neural Nets modelling: computational graphs, backpropagation algorithm and activation functions;</li></ul><p>And this is just the theory. When practicing, we need some other concepts, like:</p><ul><li>How to do <strong>real</strong> Data Science;</li><li>How to ingest data in a automated fashion (pipelines);</li><li>Deploying and Monitoring ML aplications.</li></ul><p>And here it goes a <strong><em>disclaimer:</em></strong></p><ul><li>What I showed so far is another way of understanding the concept of Neural Nets, or NN. But there are many more ML algorithms that do not rely on NNs;</li><li>The task at hand is a classification, but there are others, like regression, for example;</li><li>Data often comes in high dimensionality (lots and lots of columns);</li><li>Finding the line equation C is an automated process, known as <em>learning</em>. You can visualize how learning occurs in the didatic <a href="https://playground.tensorflow.org/#activation=sigmoid&amp;batchSize=10&amp;dataset=gauss&amp;regDataset=reg-plane&amp;learningRate=0.0001&amp;regularizationRate=0&amp;noise=10&amp;networkShape=&amp;seed=0.24990&amp;showTestData=false&amp;discretize=false&amp;percTrainData=50&amp;x=true&amp;y=true&amp;xTimesY=false&amp;xSquared=false&amp;ySquared=false&amp;cosX=false&amp;sinX=false&amp;cosY=false&amp;sinY=false&amp;collectStats=false&amp;problem=classification&amp;initZero=false&amp;hideText=false">Tensorflow Playground</a>.</li></ul><blockquote>Wait, did I say ‘Neural Nets’? How come? Believe it or not, but we can understand NN as matrix multiplication followed by sigmoid instead of those eye-caching graphs. That happens because each neuron implements that <strong><em>dot()</em></strong><em> </em>function I mentioned followed by a sigmoid. Finally, when we stack several neurons, we are literally creating a matrix multiplication.</blockquote><h3>The Representation Problem and Deep Learning</h3><p>Now we have a basic understanding of how Machine Learning works and why it is different of traditional programming. Now let’s move on to Deep Learning. In order to do that, tell me: in the following image, can you separate the blue dots from the red dots using a single straight line?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ndV_qxIfam9Wm4VZgbJVEg.png" /><figcaption>Can you separate the red dots from the blue ones using a straight line?</figcaption></figure><p>Naturally, the answer is ‘no’. There is no way of separating the dots with a single straight line. But what if a <em>transform </em>them? Let’s say, by applying a cartesian-polar coordinate system transformation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zlCNNFo4xtrolYQeQSPTIg.png" /><figcaption>Now it is easier, uh?</figcaption></figure><p>Now it is possible, isn’t it? By transforming the data, a previous impossible operation is now not only possible, but straightforward. The trick was simply to change the <strong>representation</strong> of the data. That is, to change the way it is presented (or defined).</p><p>This trivial example makes clear a problem ML suffers from: how to represent the data in a way it is easy for the model to learn from (think of it as ‘finding the straight line’). It was clear to provide a solution here since the transformation is well known. But there are many problems whose ‘transformations’ are unknown, like in image classification. That’s the task we will tackle right now.</p><p>Suppose you were given a set of MRI brain images already analyzed by experts. That is, each image were labelled as ‘this brain has tumor’ or ‘this brain has no tumor’. Your task is to use this data to create a computer system capalble of determining if a new MRI brain scan indicate tumor or not. How to do so?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*hjoTfU240awBJdwAufKQaw.png" /><figcaption>MRI scans pre-labeled by experts.</figcaption></figure><p>As you can guess, the answer may use Machine Learning instead of Traditional Programming, since there is no clear rule and you might use the data to provide some solution. But unlike the previous task, there is no table. How are we supposed to ‘draw the line’ between the samples to classify them?</p><p>The trick here is to, instead of handcrafting features, let the model learn them in a end-to-end fashion. That is, we will use a ML model capable of extracting useful information directly from the data, instead of applying transformations manually (like the polar one in the start of this section). In this sense, we need to think about the nature of the data to prospect for useful properties.</p><p>The first question is then: what is an image?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*LU13MDOJTx6S6h1xaIVL1g.png" /><figcaption>Images are just matrices (source: <a href="https://anaghazachariah.medium.com/what-is-convolution-ac2cb3df3324">Anagha Zachariah</a>)</figcaption></figure><p>Images are just matrices of pixels values. For greyscale pictures, values near 255 appear as bright pixels, while values near 0 look dim. For colorful images, we have three matrices, one for each RGB color with values near 255 meaning a brighter color.</p><p>Ok, if images are matrices, can we just multiply them by our weights matrix? Such approach was actually used in the past, and nowadays we have better ones. Multiplying explicitly the images by the weight matrix is inefficient, requires many more images and lead to poor results. Let’s keep searching for useful image properties instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fi-eU42O7wFGGG0P0DnRsQ.png" /><figcaption>Some Taylor Swift’s Instagram pictures.</figcaption></figure><p>Above we have a portion of Taylor Swift’s Instagram. I personally like the variation of poses, colors, illumination, and style of her pictures. Truly lovely. And this hints two curious properties of images: <a href="https://storage.googleapis.com/deepmind-media/UCLxDeepMind_2020/L3%20-%20UUCLxDeepMind%20DL2020.pdf"><em>translation invariance</em> and <em>locality</em></a><em>.</em> What it means? It means meaningful patterns occur anywhere in the image and nearby pixels are strongly correlated. These properties make possible identifying miss Swift in the pictures, regardless of where her face appears.</p><p>What an interesting property! How can we exploit it? Certainly through a mathematical function that uses it! The name of such a function is <em>convolution</em>.</p><p>The <em>convolution </em>operates by sliding a small window over the input image matrix (the window itself is a matrix). We then pointwise multiply the values of the window by the values of the image ‘covered’ by the window for each window position. The resulting products are summed and this is defined in the output matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*ZCjPUFrB6eHPRi4eyP6aaA.gif" /><figcaption>The image is in green, the window in yellow. The result is the pink matrix. <a href="https://danielnouri.org/notes/2014/12/17/using-convolutional-neural-nets-to-detect-facial-keypoints-tutorial/">Source</a>.</figcaption></figure><p>We can see of the effect of some convolution filter below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VBA2HvJAlVV_LGFKABdBjQ.png" /><figcaption>Convolution filters change the nature of the images, adding ‘effects’ to them.</figcaption></figure><blockquote>Each convolution sliding window has a <em>effect </em>associated. Such matrices are also called <em>kernels </em>or <em>filters.</em></blockquote><p>Now we have a nice bulding block! Let’s create a Machine Learning model out of it. To do so, we stack some filters together and apply them to the input image. The output matrix (an ‘image’ with as many ‘channels’ as filters) undegoes another set of filters. This is done some times until the resulting matrix is short with many ‘channels’. We then turn such several-channels-short-matrix into a long vector that undergoes some matrix multiplications (like we did before). Some matrix multiplications later we pass the resulting vector into a sigmoid function.</p><blockquote>After undergoing a convolution, the border of the image ‘erodes’. That is why after a sequence of convolutions the image matrix is short.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8mu9c24YCs3ZETkFZJWhXA.png" /><figcaption>LeNet-5, a classic convolutional neural network. <a href="https://pabloinsente.github.io/the-convolutional-network">Source</a>.</figcaption></figure><p>Such machine learning models are called Convolutional Neural Networks (CNN). An extra operation called pooling is also performed. It serves to make the image shorter while keeping the most important details. You can see <a href="https://adamharley.com/nn_vis/cnn/2d.html">a CNN running here</a>.</p><p>We can now define a CNN ourselves and see how it performs on the MRI task!</p><h3>Practical Advice</h3><p>Here we have the definition of our CNN in Tensorflow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*p3iRBFV8LHGDMcxiy6YTJQ.png" /><figcaption>For some reason my VSCode is not detecting the imports 🙄.</figcaption></figure><p>First we rescale the pixels to a smaller range (this way avoiding the filters’ values to grow too large). Then we has three sequences of convolution-pooling. We turn the resulting set of ‘channels’ into a vector using <strong>flatten()</strong>, maps it into a 100-rows vector and then into a 1 output whose values range from 0 to 1. Let’s train it to see the performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EcRXZs0T8F0m5J99_1ioPQ.png" /><figcaption>Model performance.</figcaption></figure><p>What do these graphs say? Think about the <strong>Accuracy </strong>as a measure of the ratio of the times the model gives the correct 0–100% probability (greater is better). The <strong>Loss</strong>, on the other hand, can be seen as a distance to the ‘optimal configuration’ (smaller is better). The <em>Train </em>curve<em> </em>means we are computing such values (accuracy, loss) with the model knowing the correct answer (that is, while it is ‘learning the perfect config’). The <em>Te</em>st curve is, as it says, the <em>test</em> of the model (on previously unseen data).</p><p>It looks like the model is performing good while “learning”, while during test it performs poorly (a situation called <em>overfitting</em> for reasons beyond the scope of this text). Why is this happening? Let’s see the distribution of classes in our data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*3U5WIc9q5Swr1FaHe6kS-Q.png" /><figcaption>Looks like we don’t have the same amount of examples for both labels.</figcaption></figure><p>As can be seen, we have more MRI-scans with tumors than without. Why is this a problem? Suppose an extreme situation… 99 scans has tumors while just 1 has not. The model can say ‘all have tumors’ and it’ll still hit 99% of accuracy! But that one it misses is the most important! We then need to fix such imbalance. To do that, let’s generate more training data by flipping some scan images and then running training with a brand-new model instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aXFoA6th4V5-jvHAp2B9ag.png" /><figcaption>Yeah! That is what I am talking about!</figcaption></figure><p>The performance is good! We used our fancy math knowledge to create a model capable of distinguishing the presence of tumors in MRI scans. And best of all: we did not need to define ‘the rules’ ourselves!</p><p>This is good. Now wrapping up: we went from the traditional programming to the very definition of Machine Learning. The way it works was explored, as well as one of its shortcomings. The idea of Deep Learning was then introduced as a way of bypassing it! And this helped us to see how it can be applied in real-life applications.</p><p>Of course, I covered a lot of stuff in a short post, skimmed over some fundamental concepts and skipped many details. Machine Learning is a big area and, if you want to have a good understanding of what is going under the hood, I would suggest you to consider taking a course on it. My ultimate goal was to show ML technologies are basically clever concepts written in fancy math, instead of a black box only wizards in ivory towers can make sense of. Again: a long story short. But even though this was just a prototype of an introduction, there are things I want you to keep. Now let’s see some key takeaways that I think are the most important to keep in mind! <em>And let’s phrase them as literary quotes, because why not?</em></p><h3>Key Takeaways (Literary-phrased)</h3><blockquote>“A five-week sand blizzard?” said Deep Thought haughtily. “You ask this of me who have contemplated the very vectors of the atoms in the Big Bang itself? Molest me not with this pocket calculator stuff.” (Douglas Adams, The Hitchhiker’s Guide to the Galaxy)</blockquote><p>When asked to run a simulatition regarding Computational Fluid Dynamics (in order to have their computational power assessed), Deep Thought feels offended. For those who doesn’t remember, Deep Thought is the Computer Program famous for answering ‘the answer to life the universe and everything is 42’. But why did they feel offended? As they themself say, because running simulations is for ‘pocket calculators’. They is more than that, since it achieved ‘counciousness’. I like this quote because it makes clear that there are differences between Machine Learning and Traditional Programming. We should always ponder what we are applying and why we are doing so. Even though we see a hype-wave everytime a new tech appears, we must not have our judgement clouded. <em>We don’t use hammers for screws, right</em>?</p><blockquote>“They are a cleaner, better breed than we are” (Asimov, I. I, Robot. Greenwich, Conn: Fawcett Publications, 1950. p. 2–3)</blockquote><p>While reflecting on the nature of robots, doctor Susan Calvin calls them ‘a better breed than we are’. Such feeling may spill on how we relate to AI too. Not because we’ll say they are a nice breed, but by believing AI are bias-free, purely rational, thinkers. It is dangerous to think this way because we do not have AI (our ML models do not think) and they are ultimately not bias-free. As we saw in the post, these models ‘learn’ from data, and if the data is biased, they will be biased too.</p><blockquote>“It is not a mystery of magic, but a feat of engineering” (A Batalha do Apocalipse, Eduardo Spohr, 2007)</blockquote><p>When discussing how a tower (Babel Tower) could be so tall, Ablon tells Shamira their builders were clever enough to find a way of doing so. There was no magic holding it up. We often see these ML technologies as magic, as the perfect tool that will solve all our problems. We should be careful with that. Such tools, even though they are really useful, they are nonetheless just tools. The last judgement on their results will be done by humans anyway.</p><p>That is it! I want to thank the ones responsible for making the Google I/O Extended Natal happen. It was an honor to be chosen as a speaker, the team was very accommodating and the place was absolutely rad. If you want to get a little taste of how it was, <a href="https://gdg.community.dev/events/details/google-gdg-natal-presents-google-io-extended-2023-1/">jump to their webpage</a> or watch the aftermovie down below. If you want to see a bigger portion, <a href="https://www.youtube.com/live/xSiJJDlI-l8?si=RPMpMNZvW1d-buY3">here you have it</a>!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXJiK7XTGArA%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXJiK7XTGArA&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXJiK7XTGArA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a529b8b69fb513dc5674d35d7aeaf13/href">https://medium.com/media/8a529b8b69fb513dc5674d35d7aeaf13/href</a></iframe><p>If you like technology and want to discover cool applications (maybe you want to speak too?), I totally recommend you attending to these events.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fafba11bec2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Navigating the Data Mess: My Experience Refactoring Code to Access Open Data]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/navigating-the-data-mess-my-experience-refactoring-code-to-access-open-data-a582fea22614?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/a582fea22614</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[pytest]]></category>
            <category><![CDATA[open-data]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Sat, 25 Feb 2023 14:20:35 GMT</pubDate>
            <atom:updated>2023-02-25T14:20:35.015Z</atom:updated>
            <content:encoded><![CDATA[<p>An Open Dataset must be as accessible as possible. What do we do when they are not?</p><h4>The 5-Star Scheme for Open Data Quality</h4><p>As Tim Berners-Lee (the man behind the <em>www</em>, just in case you don’t know) stated in <a href="https://www.w3.org/DesignIssues/LinkedData.html">his 2006 post</a>, the value added by the web is <strong>the unexpected re-use of information</strong>. Furthemore, the web itself is not about what is there exactly, but <strong>how they are connected</strong>. He then suggests a <a href="https://dvcs.w3.org/hg/gld/raw-file/default/glossary/index.html#x5-star-linked-open-data">5-star deployment scheme</a> that <em>kinda </em><a href="https://internt.slu.se/globalassets/mw/foma/verksamhetsstod/miljodatastod/open-data/2018-01-open-data-steps-guide.pdf">measures how “open” your data is</a>. The idea is simple: the more connected, the better, and each additional star presumes the data meets the previous steps’ criteria.</p><ol><li>🌐🔓©️ It is on the web under a Public License 🌐🔓©️</li><li>️🗄️🤖👁️‍🗨️ It is structured and machine-readable 🗄️🤖👁️‍🗨️</li><li>🆓🗃️🧾️ Its format is non-proprietary, and it is documented 🆓🗃️🧾️</li><li>#️⃣ℹ️📍 RDF format #️⃣ℹ️📍</li><li>🛰️🔃🔗 RDF with identifiers (urls) to useful data sources 🛰️🔃🔗</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*hZnlN8aXgA9Kfzjb.png" /><figcaption>5 Star Linked Open Data Scheme (<a href="https://commons.wikimedia.org/wiki/File:5-star_deployment_scheme_for_Open_Data.png">Picture by Spinster on Wikimedia Commons</a>)</figcaption></figure><blockquote><a href="https://data.gov.ie/edpelearning/en/module1/#/id/co-01">According to Ireland’s open data portal, open data is data that <strong>anyone can access, use and share</strong>.</a> From this raw material information and knowledge can be derived.</blockquote><p>Several governments around the globe adheres to Open Data. For instance:</p><ul><li><a href="https://data.gov.ie/">Ireland’s Open Data Portal</a> (Ireland)</li><li><a href="https://dados.gov.br/home">Portal Brasileiro de Dados Abertos (Brazil)</a></li><li><a href="https://data.gov/">U.S. Government’s Open Data</a> (United States of America)</li></ul><h4>Challenges with Messy, Semi-Structured Data</h4><p>One of Brazil’s projects regarding open data is <a href="https://www.digipathos-rep.cnptia.embrapa.br/">Embrapa’s Digipathos</a>. It is a computer vision dataset comprising images of plants’ diseases and their descriptions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ve42zXuMuLGmeRip.png" /><figcaption><a href="https://www.digipathos-rep.cnptia.embrapa.br/jspui/image/logo_digipathos.png">Digitpathos logo</a></figcaption></figure><blockquote>The Brazilian Agricultural Research Corporation (brazilian acronym: Embrapa) is a state-owned research corporation affiliated with the Brazilian Ministry of Agriculture. (<a href="https://en.wikipedia.org/wiki/Brazilian_Agricultural_Research_Corporation">Wikipedia</a>)</blockquote><p>The first time I saw it I thought: “wow, this is amazing. As a data scientist/ML engineer, it is thrilling to find a dataset like this: a resource based on my own country’s context.”</p><p>And then I needed to face an incovenient truth: the data is not as much ‘open’ as it should (and as I wanted). The true picture is: there is no API, there is no global download button. You just keep clicking on all the pictures, downloading batches of them one by one. Based on the discussion starting this post, I would rather say Digipathos has only one star. I think it is open to dispute how much ‘machine readable’ and ‘structured’ the dataset is. I don’t believe it is as ‘readable’ as it could. In the age of APIs, we can have facilitated access to resources. Ok, we can search which images we do want, but this must be done manually (instead of using code instructions). Moreover, the ‘structure’ here is limited to the pictures batches, no more metadata is found (opposite to aggregated information like the amount of samples per edible, or the amount of samples per disease).</p><blockquote>Of course, this is not infrequent. I often hear/read stories about how unstructured data is, open or not. Any data scientist wanting to give their contribution will spend some of their time fetching/cleaning/structuring/tailoring the samples (actually, data-related steps are the most time-consuming and important of all the pipeline).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cQInazXs4SfRtUwe.jpg" /><figcaption>c’mon! You are kidding! (<a href="https://www.flickr.com/photos/hidufel/2304828851">Photo by hidufel on Flickr</a>)</figcaption></figure><h4>Navigating Limited Resources for Open Data Downloads</h4><p>Puzzled with how one would download the dataset, I searched projects in Github that could do the job. I found a promising one, named <a href="https://github.com/georg-un/digipathos-plant-disease-img-db-downloader"><strong>digipathos-plant-disease-img-db-downloader</strong></a>, written in Python by the user <a href="https://github.com/georg-un">george-un</a>. Kudos to him! His code does the job perfectly.</p><p>Meant for CLI use, the project contain scripts that, together, are responsible for fetching, downloading, and unzipping the samples into folders. “Great”, I thought, “people around the globe can access the dataset using it”. But I found the CLI a little odd… as a ML engineer, I’m used to access Data Lakehouses using code. I usually write pipelines that consumes the data directly. They are intended, by design, to be automated. No human interaction is required. Of course, george-un’s project is great. Having a resource is always better than don’t, but CLI use is good during the development stage only. If an engineer would need to provide CLI commands everytime the ML models need to be updated, this would be doom!</p><p>“Alright, I just need to read the docs to get the inner workings of it”. Unfortunatelly, the code does not have a clear documentation for its features, nor the functions had docstrings. Even though the functions names explain a lot, they did not hint the details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*Aj8DTnZuaChSfZ9h.jpg" /><figcaption>my mood was like: (<a href="https://imgflip.com/i/7bykjg">created with Imgflip</a>)</figcaption></figure><h4>Refactoring: a summary</h4><p>At this moment, my thoughts were like “man, if this project could be used in code as a lib it would be a great resource for those wanting to document their digipathos-based projects end-to-end”. For anyone wanting to download the dataset using code, a package is fundamental. I then decided to refactor george’s project, hoping to ease the community’s access to the dataset (and then moving towards a <strong>true open dataset</strong>, hopefully). Moreover, providing docstrings and tests would make the code more predictable and documented.</p><p>It was time to fork and start coding…</p><p>To understand the inner workings of the download process, I followed each step sequentially. The download is triggered when the script run.py is called. Here is possible to determine if all the images or just the <em>cropped </em>ones should be downloaded. The run.py then calls a function that sets off others. The process then goes like:</p><ol><li>Generate a folder structure to accommodate the downloads and the unzipped samples (<em>tmp/</em> and <em>dataset/</em>);</li><li>Fetch samples metadata (file name, size, link, and format);</li><li>Download zipped samples;</li><li>Validate the amount of downloaded zips matches the amount of fetched metadata (i.e., the download was done successfully);</li><li>Unzip the files;</li><li>Delete the temporary folder (the one keeping the zips);</li></ol><p>Of course there are more things going on during download, like exception handling, and users notifications (prints). These happen throughout the code, and are very case-specific. I decided not to explain them then.</p><blockquote>I understand that “cropped”, in this context, means the image is zoomed in the disease itself, instead of showing the whole vegetable body (leaf, branch, fruit, etc).</blockquote><p>In addition to the issues already commented, I found another one. It is listed below, alongside every detail I meant to change in my project’s fork:</p><ol><li>Add docstrings. This helps the user to understand what the function does and how it works;</li><li>Add tests to assure behaviour;</li><li>Remove global variables to prevent conflict and to add use flexibility (this is the new one). Using local variables saves the time in case the website’s url changes (originally this is a global variable);</li><li>Manage the dependencies (in this case, the <a href="https://requests.readthedocs.io/en/latest/">Requests lib</a>) and the packaging so the user will be able to <em>pip install</em> it in a different project (instead of git cloning the repo);</li><li>Create returns. These returns aim to provide information about the process itself. For instance, if you try to download a given set of images and some of them are not downloaded for an unknown reason, it is important to list they are not available, instead of just printing in terminal. In case you are working with a data pipeline, you can <strong>assert</strong> the function’s return is <em>None</em>, which guarantees all the images you want are available.</li></ol><p>And so I did. First of all, I moved the necesary files to the project folder (in this case, ./digipathos_downloader/digipathos_downloader/). These were: download.py, run.py , and setup_checks.py. Only the first one contains necessary code, since run.py is meant for CLI and setup_checks.py is not used whatsoever.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*DY28kB3tWN9_hLYjM5Utvw.png" /><figcaption>Codes folder (image by me).</figcaption></figure><p>I started a <a href="https://python-poetry.org/">Poetry</a> project and added the <a href="https://requests.readthedocs.io/en/latest/">Requests</a> package. Poetry is a tool for dependency management and is also used for virtual environment creation. For anyone working with machine learning in production, be sure to get familiar with Poetry and its features.</p><p>Regarding the tests, they aim to achieve two things in general: assure the code behaves as expected when input is ok (tested via returns); assure the code behaves as expected when the input is <strong>not </strong>ok (tested via the errors raised). To do it I used the <a href="https://docs.pytest.org/en/7.2.x/">Pytest</a> lib, which is a great resource for anyone who wants to test Python code. Its integration with VScode tests tab makes its use more straightforward.</p><blockquote>Python test tip: some processes (like downloading some samples) would take too long. Other steps would need broken links to simulate unexpected inputs. In both cases I used <a href="https://pypi.org/project/pytest-mock/">mocking</a> and <a href="https://docs.pytest.org/en/6.2.x/fixture.html">fixtures</a> to do the job. If you need to test a feature which requires other parts that are too expensive to run, take a time to read about such concepts. They will certainly save you time.</blockquote><p>The global variables (folder names and urls) were replaced with local, pre-defined arguments for the functions. In case the dataset urls changes, the features are still useful, and the user does not need to meddle with the implemented logic. If the user wants to download the dataset into different folders, this is not a problem. In the original project the folder were pre-defined to the place where the code runs.</p><p>Thus, the refactoring followed a TDD (test-driven-development) fashion: test a function, fail the code, and then fix it asserting the expected behaviour. Minor changes regarding variable naming and returns were done, but they are not worth describing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RD_ziI7Fpkv3H5xS.png" /><figcaption>Test Driven Development (<a href="https://marsner.com/blog/why-test-driven-development-tdd/">image by Marsner</a>)</figcaption></figure><p>At the end, I added an README to the project root to inform the users right away about how it works. It summarizes how to install the lib and about its features. Furthermore, it provides a short example of its use.</p><h4><strong>Last Words…</strong></h4><p>The project, at the end, intends to broaden the access to the Digipathos Dataset by easing the download process. The added docstrings ease the understanding and the tests assure certain aspects of the logic’s behavior. I hope it serves people around the world as a tool for science and technology development.</p><p>Of course, there is more to be done. For instance, towards the end of the tests I realized I could have employed more user-friendly functions. Furthermore, the refactored code is not the end. More things can be added to get closer and closer to a true Open Dataset. For instance, an API can be built upon the project. Another improvement is having metadata (number of samples, samples per class, classes, file extensions, etc) already available in the project itself, such that the user can select which kind of samples they want. This will shorter the download time.</p><p>I hope this lil’ tale to be informative to all the readers. I personally believe Open Access (software, hardware, data, science, education, etc) to be an excellent initiative that is good for everyone. The project itself was only possible because of the practice of making source code open and free. Once more and more developers, researchers, hobbyst, and so on, share their projects and resources, more and more developers, researchers, hobbysts, and so on will be able to develop incredible stuff.</p><p>To the data practioners and students out there, I want this story to give you an idea of what is like to work with data in the real world. The algorithms are just a bit of the job. You will probably work with messy datasets, to say nothing of creating your own tools. I would say a rather long part of my coding tasks involves developing tools. And with that I mean writing the code, the tests and the docstrings. Trust me: documentation matters. If you don’t have the documentation, you will need to test everything out to get how it works, and that often takes a lot of time. Tests are needed because they assure everything is in its place and once you modify parts of your code, the unmodified parts still behave as you expect (besides being good examples of use).</p><p><strong><em>Key Takeaways:</em></strong></p><ul><li>Open Data is a global initiate that everyone benefits from;</li><li>Open Data <strong>is not</strong> <strong>just</strong> about online datasets. They must be under a public license, documented, identified, and potentially linked to other informative information;</li><li>Datasets in the wild are messy;</li><li>Consuming datasets often involves you building tools for it (i.e., web scraping);</li><li>Building a tool starts with the code itself, but must be tested and well-documented such that others can use it;</li><li>Never start a project from scratch. Search for some time and you might find something useful;</li><li>Learn software testing and how to write proper docs. This will help others to start off with your project.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a582fea22614" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning robotics… made easier]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/learning-robotics-made-easier-536ec16a4b8a?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/536ec16a4b8a</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[coppeliasim]]></category>
            <category><![CDATA[robotics]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Sun, 15 Jan 2023 13:56:32 GMT</pubDate>
            <atom:updated>2023-04-23T13:51:09.358Z</atom:updated>
            <content:encoded><![CDATA[<h4>Enhancing robotic simulation practice with Python</h4><p>Picture yourself: you just watched your favorite movie and something catches your attention. What? A robot. They are everywhere in the screens these days… from animations like <a href="https://www.imdb.com/title/tt0910970/">Wall-E</a> to horror movies like <a href="https://www.imdb.com/title/tt8760708/">M3GAN</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9IcqBYJUwuP1mDXFlRZa7g.jpeg" /><figcaption>Fiction robots accross genres. From left to right: <a href="https://www.imdb.com/title/tt0910970/">Wall-E</a>, <a href="https://ew.com/article/2015/05/01/avengers-age-ultron-reviews/">Ultron</a>, and <a href="https://www.slashfilm.com/1129647/m3gan-trailer-sugar-and-spice-and-a-dancing-killer-robot/">M3GAN</a>.</figcaption></figure><p>You then have a burst of inspiration and decide to Google about them to learn more. You then notice they are everywhere: <a href="https://www.nytimes.com/wirecutter/reviews/best-robot-vacuum/">vacuum bots</a> clean houses, and <a href="https://www.motoman.com/en-us/products/robots/industrial">robotic arms</a> make cars.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZYruZQ7bxLp_uDPMPWR6cQ.jpeg" /><figcaption>Two applications of robotics: <a href="https://timesofindia.indiatimes.com/most-searched-products/electronics/vacuum-cleaners/robot-vacuum-cleaners-that-clean-your-house-with-just-a-mobile-click/articleshow/79174459.cms">domestic</a> and <a href="https://www.vox.com/2017/6/22/15763106/industrial-robotics-market-triple-ten-years-collaborative-robots">industrial</a>.</figcaption></figure><p>Knowing that just boost your curiosity and you decide to experiment with them. Maybe learning the theory? You discover there are simulators around the web, such that you can play around without the risk of breaking anything or without the need of buying a kit.</p><blockquote>Brief overview: Robotics is a multidisciplinary field that comprises both Engineering and Computer Science subjects. It aims to design, build, and operate programmable eletro-mechanical structures capable of carrying a particular task. During my Mechatronics engineering course, I took two robotics classes. The first one focused on defining physical and mathematical relations associated with the robotics parts. These concepts guarantee the robots can support a load, or if it can reach certain positions, for example. The second one elaborated on navigation systems to assure the device won’t bump into anything and to give the robot the capacity to navigate around. But again, that is just a part of the whole. There are other classes teaching important ideas applied to robotics as well, like Mechanics, Electronics, Programming, Algorithms, and even Calculus and Linear Algebra.</blockquote><p>Popular robotic simulators include <a href="https://www.coppeliarobotics.com/">CoppeliaSim</a> and <a href="https://www.ros.org/">ROS</a>. Let’s pick up CoppeliaSim for instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*k5IxByvU9iSIw1wi8YFx4w.jpeg" /><figcaption><a href="https://www.coppeliarobotics.com/helpFiles/">CoppeliaSim</a> and <a href="https://commons.wikimedia.org/wiki/File:Ros_logo.svg">ROS</a> logos.</figcaption></figure><p>CoppeliaSim (former V-REP) is based on simulated <em>scenes</em>. A scene is a collection of elements (named <em>models</em>) arranged in a tree-like hierarchical structure (like directories in an OS). Even though it is possible to create simulation robots, the simulator already have some. There are two ways of controlling stuff running as simulations: through scripts defined inside the simulator (using <a href="https://www.lua.org/">Lua</a>) or through the RemoteAPI (<a href="https://www.python.org/">Python</a>). The RemoteAPI gives you control over the simulation by calling certain endpoints associated with the scene elements. You can then connect your custom code (i.e. running in VSCode terminal) to the simulated scene. This eases debugging and testing.</p><blockquote><a href="https://www.coppeliarobotics.com/helpFiles/">According to the user guide</a>, you can do stuff using the following programming languages: C/C++, Python, Java, Lua, Matlab or Octave. I have only used Python and Lua.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0Pj--ySYURvPvaD8oDtRrA.png" /><figcaption>CoppeliaSim EDU 4.3.0 main screen. The simulated scene contains a P3DX robot and a cuboid. The Scene Hierarchy and Robots tabs are visible on the left.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*zZZTNpmP2mc1gWY4.gif" /><figcaption>A simulated scene in CoppeliaSim (<a href="https://bbs.huaweicloud.com/blogs/282212">zhangrelay, Huawei Developers</a>)</figcaption></figure><p>You download the simulator for your particular OS and… you don’t know what to do. So many options! So many things on screen! What are you supposed to do? How do you get a robot ready and running? How do you do anything?</p><p>That’s the feeling I had when I downloaded the simulator for the first time for my Mobile Robotics class. I needed to do a rather simple assignment (could I regard it so?): move a robot around and retrieve its position over time. This feeling (not knowing what to do) stuck around and every time I needed to use a new simulator feature I spent a lot of time searching the web for it.</p><p>Naturally, the simulator software has an user guide detailing its workings and functions. But as an e-learner, I see a lot of technology platforms around the internet providing quickstarts and tutorials, like <a href="https://docs.arduino.cc/learn/starting-guide/getting-started-arduino">Arduino</a> and <a href="https://projects.raspberrypi.org/en/projects/raspberry-pi-getting-started">Raspberry Pi</a>. I think that is awesome: it is good for the users, who will quickly get used to the thing, and it is good for the companies as well, since the users group tend to expand over time. When dealing with Coppelia, I seldomly found such things. In general, they were 1 hour long videos made by users demonstrating a particular example, based on several features. They were as useful as they were long, but they took a lot of my already scarce time.</p><p>Another issue I had to face was the Remote API documentation. Even though the function’s parameters were explained, it got confusing a lot of times. Some times I would read the same explanation again and again to get how to use it, some times I would find it ambiguous when comparing it with other official sources.</p><blockquote>The mobile robotics class tasks were based on certain functionalities that were used repeatedly (communicating with the simulator and sending commands, basically). They were developed as a set of Python classes designed for reuse.</blockquote><p>At that time I thought: gathering resources (quickstarts/tutorials and a simplified lib) in a single place would be great. It would ease the use of the software and speed up the new users’ learning. That’s the backstory of my robotics library: <a href="https://github.com/mtxslv/guira">Guira</a>. Besides the modules themselves, it has both a set of tutorials (quickstart) that comprises simulated scenes and the associated Python codes that demonstrate their use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*0RwFZ3wN9p1IHLnG.jpg" /><figcaption>Guira cuckoo (<a href="https://www.flickr.com/photos/schenfeld/8621923793/">David Schenfeld, Flickr</a>)</figcaption></figure><h4><a href="https://github.com/mtxslv/guira">Guira: a Python Library for Robotic Simulation with CoppeliaSim</a></h4><p>As stated previously, <a href="https://github.com/mtxslv/guira">Guira</a> is composed of two main elements: the Python package, and the tutorials. The former offers basic functionalities regarding the communication with the simulator and the latter demonstrate how these features work. This aims to simplify experiments with <strong>mobile robots.</strong></p><blockquote><a href="https://github.com/mtxslv/guira"><strong>Guira</strong></a><strong> was developed with Python 3.9 for CoppeliaSim EDU 4.3.0 in Ubuntu 20.04. Support for other Python and CoppeliaSim versions are not assured. Furthermore, it may not work properly in other OS, since CoppeliaSim RemoteAPI is tailored for each specific system.</strong></blockquote><p>The package has four main modules (more details on the <a href="https://github.com/mtxslv/guira/blob/master/README.md">repository’s readme file</a>):</p><ul><li><em>Curves: here you can generate some parametric curves to test your algorithms. Available functions: lemniscate and </em><a href="https://repositorio.ufrn.br/handle/123456789/15417"><em>cubic polynomials</em></a><em>;</em></li><li><em>Robot: how to drive a robot by assigning target velocities to its motors;</em></li><li><em>Scene: connection with the simulator and information transfer (i.e., robot’s position and orientation, and curve points transmission);</em></li><li><em>Sensors: ultrasonic sensor reading.</em></li></ul><p>There are 5 tutorials (two of them available as video):</p><ul><li>bounding_boxes shows how to retrieve objects configuration, as well as the bounding boxes&#39; corners positions;</li><li>connecting_to_a_scene explains how to connect to a simulation scene;</li><li>moving_robots teach you how to drive a robot;</li><li>sending_points exemplify the use of <em>Curves</em> and <em>Scene</em> package by generating a curve and sending some samples to the simulated scene;</li><li>ultrasonic_sensor shows how to read an ultrasonic sensor.</li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FM4mCUekoWF8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DM4mCUekoWF8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FM4mCUekoWF8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ad6481b2700418f012ece14af7b9783/href">https://medium.com/media/0ad6481b2700418f012ece14af7b9783/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F07_oNcmhMhY%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D07_oNcmhMhY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F07_oNcmhMhY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6410a074452e635caf3ee49723701f8c/href">https://medium.com/media/6410a074452e635caf3ee49723701f8c/href</a></iframe><h4>Installing the package…</h4><p>Interested? You can get the lib for free by running</p><p>pip install guira</p><p>In the case you have <a href="https://python-poetry.org/">poetry</a> you can use</p><p>poetry add guira</p><h4>Downloading the Tutorials</h4><p>In order to download the tutorials mentioned above, you can run a shell file tailored for that use. First, download the script by running the following on the target dir:</p><pre>curl https://raw.githubusercontent.com/mtxslv/guira/master/get_tutorials.sh --output get_tutorials.sh</pre><p>(alternatively you can download it by hand <a href="https://raw.githubusercontent.com/mtxslv/guira/master/get_tutorials.sh">here</a>).</p><p>Now you must grant the script permission to be run. Do:</p><pre>chmod +x get_tutorials.sh</pre><p>Now run it:</p><pre>./get_tutorials.sh</pre><p><strong>This will mount the tutorials in the folder you executed it</strong>. So, supposing if you are running the script inside <em>~/Videos </em>for any random reason, the tutorials will be available in <em>~/Videos/tutorials/</em>.</p><h4>Final words</h4><p>I hope this work helps you in your robotics studies. If you want to share your ideas or projects, feel free to do it here in the comments. You just made something great with Guira? I’ll be glad to read your story. Feedback is greatly appreciated. Found something odd in the lib? Maybe an improvement or a bug? <a href="https://github.com/mtxslv/guira/issues">Register your issue on the repo</a>, and I’ll read it as soon as possible. Want to reach out to me? You can find me on <a href="https://www.linkedin.com/in/mateus-assis-013a46140/">Linkedin</a> or on <a href="https://github.com/mtxslv">Github</a>.</p><p>See ya!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=536ec16a4b8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Giving your Machine Learning Model an Impulse to the Edge]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/giving-your-machine-learning-model-an-impulse-to-the-edge-47e46a1d4a8a?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/47e46a1d4a8a</guid>
            <category><![CDATA[microcontrollers]]></category>
            <category><![CDATA[edge-impulse]]></category>
            <category><![CDATA[arduino]]></category>
            <category><![CDATA[tinyml]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Sun, 01 Jan 2023 19:10:54 GMT</pubDate>
            <atom:updated>2023-01-03T12:25:38.337Z</atom:updated>
            <content:encoded><![CDATA[<h4>Deploying your ML solution to a microcontroller as fast as possible with Edge-Impulse</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*5ovE0NR9Fg4cmTyAgjEHyQ.jpeg" /><figcaption><a href="https://commons.wikimedia.org/wiki/File:Components_for_DIY_and_robotics_projects.jpg">Components for DIY and robotics projects (Raspberry Pi, Arduino, etc.), by Cortezthekiller (CC BY-SA 4.0)</a></figcaption></figure><p><a href="https://ml-ops.org/content/mlops-principles">One of the main things to consider while creating a Machine Learning (ML) based solution is the development speed</a>. That is, you must be aware of how fast it takes to deliver a new model version. Ideally, once your data workflow is finished, it must be automated such that you can run it again in a command line (or in few clicks, if you prefer). The speed may be a concern while prototyping a solution, too. The faster the solution is prototyped, the faster the idea is tested and changes are designed to tackle the viability of the project.</p><p>Like to any ML system, this discussion naturally applies to edge solutions (ML models deployed in constrained devices, like smartphones and microcontrollers). Even though <a href="https://arxiv.org/abs/2203.10923">Edge ML has its own issues and details (as stated by Leroux et al.</a>), this post is concerned with how the Edge-Impulse tools speed up the ML model’s deployment, from the model creation to deployment in a microcontroller.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y8l3mP7U898F__I5WdZXYQ.jpeg" /><figcaption><a href="https://external-preview.redd.it/UI3sbNrVjDOMXH1guEiacTUb5Km0lp5CzA879pM4Y4U.jpg?width=960&amp;crop=smart&amp;auto=webp&amp;s=91ab09abf1fc22e5d92a1b4565975f3e5d6359ca">Tony Stark’s AI cards (Captain America: Civil War)</a></figcaption></figure><blockquote><a href="https://www.redhat.com/en/topics/edge-computing/what-is-edge-machine-learning">Edge Machine Learning brings machine learning models to the data source. This data source (or edge devices) might be an smartphone, or an IoT device, for example. By running the ML-based applications on such elements, you place the model physically closer to the sensors (a camera, for example), speeding up insights without depending on cloud networks (traditional approach).</a></blockquote><p>The following section focuses on how to get your model done. The use of Edge Impulse platform is described, from data upload to model training. Then, the necessary software and connection details are related. If you already want to see the details of the project, you can get them. <a href="https://studio.edgeimpulse.com/public/166725/latest">Here is the link to the public project</a>.</p><h4>Edge Impulse Platform: from model creation to model training</h4><p>The Edge Impulse Platform is used to create the model. Let us see the details.</p><p>First of all, create a project in Edge Impulse. The one done was called <a href="https://studio.edgeimpulse.com/public/166725/latest"><em>cats_and_dogs_breeds</em></a><em>, </em>named after the dataset used.</p><blockquote>The dataset used here is a subset of <a href="https://www.robots.ox.ac.uk/~vgg/data/pets/">The Oxford-IIIT Pet Dataset</a>. Originally, the dataset contains 37 category pet dataset, with roughly 200 samples per class. However, this problem uses only 5 categories of them: two cats breeds (Russian Blue and Bengal); and three dogs breeds (Pug, Boxer, and Keeshond).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/229/1*l-tseZkgJXw4S6IXvk0wDQ.png" /><figcaption>“Create new project” option, available in the drop menu when you click in your profile picture.</figcaption></figure><p>Once you name your project, it is necessary to tell Edge Impulse the kind of data you are working with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*XfzkVvbTa939wPRHo7FFvg.png" /><figcaption>Wizard Screen: Task Data Type.</figcaption></figure><p>Ok… it is a visual task, but there are two possible types of visual task here: classification (<em>what is in front of the camera?</em>) and detection (<em>what are the multiple objects I’m seeing and where are they?</em>). Choose “image classification”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*uxyHU2G4X4J9j2Wr47ovUw.png" /><figcaption>Wizard Screen: Image Classification.</figcaption></figure><p>Now the project can be started. Let’s begin by importing existing data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*9QgE4Oi_VVep9O6BahRlpg.png" /><figcaption>Wizard Screen: Starting the Project.</figcaption></figure><p>The next screen lets you upload your data, class by class. The majority of the options were left unchanged, but the label name was defined by explicitly typing it in the box.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*Hua6j9BtVisZqwE0mh-Wng.png" /><figcaption>Upload data.</figcaption></figure><p>Edge Impulse Data Explorer (located at the Data Acquisition option at the left menu) gives you the possibility of Embedding your visual data in 2D. It lets you explore your dataset, which can lead to insights you did not think of at the beginning. A nice visualization we can have on our data are these clusters below. Honestly, it is amazing how the cats clusters are placed together, far from the dogs. Talking about the dogs, see how the Pugs and Boxers are close to each other? This makes sense, since they are <em>kinda </em>similar. Another used for such visualization is to check outliers and bad instances (points in wrong clusters).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gw23GP2H_4aFyXMaGbXDJg.png" /><figcaption>Data Explorer</figcaption></figure><p>Data is OK. Now it is time to try an architecture. One option is to define an architecture by hand (by selecting layers, activation functions, and loss functions), or even by trying out pre-trained architectures with Transfer Learning (<a href="https://medium.com/@mateus.d.assis.silva/transfer-learning-com-vgg16-e-resnet50-39e58c3e84c3">learn more in this post — written in Portuguese</a>), but today there are AutoML services that let you choose among several architectures, one click away. The Edge Impulse is no different, and you can try several architecturesin the ‘EON’ tab at the left menu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*mmK2dmVmpJjc2hyC_CN-Bg.png" /><figcaption>Eon Tuner option selected in left tab.</figcaption></figure><p>By clicking on “Start EON”, several models are experimented. Before doing that, though, it is necessary to set up some parameters, like the <strong>Target Device</strong> (where the model will be deployed) and the <strong>Time per inference</strong> (how many time does it take to classify an image). The <strong>Target Device</strong> I used was the <a href="https://store.arduino.cc/arduino-nano-33-ble-sense">Arduino Nano 33 BLE Sense</a> board. I actually played around with the <strong>Time per inference </strong>option for a while, but ended up choosing 1500 milliseconds. I believe 1.5 seconds is a enough time for a prediction (good enough, <em>tbh</em>).</p><blockquote>When defining the <strong>Time per inference</strong>, it is important to keep in mind <strong>which problem you are tackling</strong>. For some applications, time is a fundamental constraint, and tight limits must be taken into account (i.e., your recognition system must detect high speed vehicles in a road). On the other hand, some applications can perfectly afford a longer inference time (i.e., the system will detect persons comprising priority groups standing in a vaccine queue area).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*291cWsIguuWTBqYlz1nUsQ.png" /><figcaption>EON Tuner options.</figcaption></figure><p>Save the options and dash the <strong>Start EON tuner</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*1RKeByTfv-27t7d-4dArEA.png" /><figcaption>Start EON tuner button.</figcaption></figure><p><em>Testing several models will take some time. Take some coffee (or tea) while waiting…</em></p><p>Once finished, select the best model. The one I chose was based on <a href="https://paperswithcode.com/method/mobilenetv1">MobileNet v.1</a>. It had a good latency, would occupy just half of the RAM, and its accuracy was over 80%. Good enough for the example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*Fe4zgc2frPAP0OshYzXWww.png" /><figcaption>Model Selected.</figcaption></figure><p>At this point of the project I needed to connect the board. To do so I needed to get the drivers. Let’s dive into it…</p><h4>Getting your deployment platform ready and running…</h4><p>As stated before, this tutorial employs the <a href="https://store-usa.arduino.cc/products/arduino-tiny-machine-learning-kit">Arduino Tiny Machine Learning Kit</a>. It includes:</p><ul><li>1 <a href="https://store.arduino.cc/arduino-nano-33-ble-sense">Arduino Nano 33 BLE Sense</a> board;</li><li>1 <a href="https://www.arducam.com/products/camera-breakout-board/0-3mp-ov7675/">OV7675 Camera</a>;</li><li>1 Arduino Tiny Machine Learning Shield;</li><li>1 USB A to Micro USB Cable</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*_n4PzLxCWirKmt0lHxP9Pg.png" /><figcaption><a href="https://store-usa.arduino.cc/products/arduino-tiny-machine-learning-kit">Arduino Tiny Machine Learning Kit: components</a></figcaption></figure><p>Also, it is important to know that everything was done in a Linux machine. Differences between operational systems may occur, which may need to modificate the steps.</p><blockquote><a href="https://www.arduino.cc/en/about">The Open-source electronic prototyping platform Arduino allows people around the globe to develop technologies that interact with the physical world.</a> The core of its products is, in general, an AVR microcontroller (formerly Atmel microcontroller). <a href="https://all3dp.com/2/difference-between-microprocessor-and-microcontroller/">A <strong>microcontroller</strong> differs from a <strong>microprocessor</strong> because the former contains CPU, memory, timers, registers, etc, in a single chip.</a> The latter is just the CPU. In other words, a <strong>microprocessor </strong>is part of a <strong>microcontroller</strong>. This comes at a cost of a lower power (computing, energy, etc). Then, <strong>microcontrollers </strong>are employed in specific applications (like your microwave oven or washing machine), while a<strong> microprocessor</strong> powers your Gaming Computer.</blockquote><p>In order to get your arduino ready and running is necessary to install some drivers and assemble the kit. The drivers installation follows what is seen in the <a href="https://docs.edgeimpulse.com/docs/development-platforms/officially-supported-mcu-targets/arduino-nano-33-ble-sense#installing-dependencies">Edge Impulse docs about the Arduino Nano 33 BLE Sense</a>. The overall steps are listed below (docs links provided in an embedded manner):</p><ol><li><a href="https://docs.edgeimpulse.com/docs/edge-impulse-cli/cli-installation#installation-macos-and-windows">Install the Edge Impulse CLI</a>;</li><li><a href="https://arduino.github.io/arduino-cli/0.29/installation/">Install the Arduino CLI</a>. In order to avoid problems with <em>PATH</em>, move the executable to <em>.local/bin/</em> (linux machine);</li><li>Install GNU Screen (<em>$sudo apt install screen</em>).</li></ol><p>Stuff installed. Now it is time <a href="https://docs.edgeimpulse.com/docs/development-platforms/officially-supported-mcu-targets/arduino-nano-33-ble-sense#connecting-to-edge-impulse">to connect to the Edge Impulse</a>. Thus, connect the development board to the computer and update its firmware (as stated in the docs). Set your keys, and verify that your device is properly connected. If everything is alright, we can deploy the model to the microcontroller.</p><h4>Microcontroller deployment!</h4><p>Once the Arduino board is OK, let’s move to the Edge Platform to deploy the model! <a href="https://docs.edgeimpulse.com/docs/tutorials/image-classification#5.-running-the-model-on-your-device">To run the model on your device, you can follow these steps</a>. They can be summed up as:</p><ol><li>Select the ‘Deployment’ option on the left menu;</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*34bE_MhHMreXNrtsDqHtVQ.png" /><figcaption>Deployment option selected in the left menu.</figcaption></figure><p>2. In order to deploy the model, I chose the option ‘<strong>Build Firmware</strong>’ (scroll down the options in the deployment page) targeting the Arduino Nano 33 BLE Sense.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*7jYvOUBXd9w_r5mfrMKApQ.png" /><figcaption>Build Firmware option selected</figcaption></figure><p>3. Scroll down the page to get to the <strong>Build </strong>option. I did not explored the <em>Optimizations, </em>but that is certainly something to be tested.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*q2S8VZwhOCurBgtnv5askg.png" /><figcaption>Build model executable files.</figcaption></figure><p>Building the model (green button) will generate the files needed to deploy your model. This step generates a <em>binary</em> file containing your model and three files responsible for flashing your device (one per OS: windows, linux and mac).</p><blockquote>If you look closer, the pattern of files created here looks like the one in the Edge Impulse firmware files (<a href="https://docs.edgeimpulse.com/docs/development-platforms/officially-supported-mcu-targets/arduino-nano-33-ble-sense#2.-update-the-firmware">Arduino connection’s tutorial</a>). This makes sense, since the deployment form I chose was through <strong>building the firmware</strong>. This actually made me think: did I really need to build the firmware as the tutorial states? At the end, I would flash another one (with my model)! Honestly I don’t know…</blockquote><p>Unzip the downloaded folder and run the <em>.sh</em> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*Mv5lSNpG1lXH_cCI-78IHA.png" /><figcaption>Downloaded files.</figcaption></figure><p>Now, the last thing to do is the run the command</p><p><strong>$edge-impulse-run-impulse — debug</strong></p><p>Unfortunatelly, I did not record any testing screen. The Kit I employed was borrowed when I was taking <a href="https://github.com/ivanovitchm/embedded.ai">Embedded AI</a> and I returned it before finishing this post. But…………..</p><p>If you want to try it yourself, on your kit , do it! Again: the project is public (you can <a href="https://studio.edgeimpulse.com/public/166725/latest/deployment">get it here</a>). Here goes some sample images I used when testing the model:</p><ul><li><a href="http://t1.gstatic.com/licensed-image?q=tbn:ANd9GcRQRq57PPBe58-ycmLASoiCsF00B4X6_7NpwIwOqpLektRueBEzllFVcQMlBwAg__vruHUaFSCuHVd3Yr0">Pug</a></li><li><a href="http://t1.gstatic.com/licensed-image?q=tbn:ANd9GcQ3brIaGMdpm9AVF8CY9RoPjrw97VLn72UC6gGsse2yNKD1N1NYk080LJwft2vmJKKM1-C93h5_qTKy47g">Bengal</a></li><li><a href="https://upload.wikimedia.org/wikipedia/commons/thumb/2/26/Keeshond_Majic_standing_cropped.jpg/800px-Keeshond_Majic_standing_cropped.jpg">Keeshond</a></li><li><a href="https://rawznaturalpetfood.com/wp-content/uploads/russian-blue-cats.jpg">Russian Blue</a></li><li><a href="https://www.petlove.com.br/images/breeds/193522/profile/original/boxer-p.jpg?1532538439">Boxer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47e46a1d4a8a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using ChatGPT to improve DALLE prompts]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/using-chatgpt-to-improve-dalle-prompts-ead9f6df558f?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/ead9f6df558f</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[dalle-2]]></category>
            <category><![CDATA[art]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Mon, 19 Dec 2022 23:43:38 GMT</pubDate>
            <atom:updated>2022-12-19T23:43:38.560Z</atom:updated>
            <content:encoded><![CDATA[<h4>two AIs are better than one…</h4><p>DALL-E 2 “is a new AI system that can create realistic images and art from a description in natural language” (Dalle’s <a href="https://openai.com/dall-e-2/">website</a> own description). The system, developed by OpenAI, is able to generate a wide range of images, including photorealistic images, abstract patterns, and stylized illustrations, depending on the input text it is given.</p><blockquote>Over this post, “Dalle” is another way of calling DALL-E 2.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yNzD7Lf7O-s9zbcKToeGBg.png" /><figcaption>“a renaissance profile portrait of a female dj wearing a cyberpunk costume” (Prompt by me. <a href="https://www.instagram.com/p/Cj1SnXHrjez/">See variations</a>).</figcaption></figure><p>Ok, but how exactly do we interact with this deep learning model? Well… this is done by providing ‘prompts’. Prompts are text descriptions of what you want to see. The more detailed the better!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KuWhp3KXN_p3j_QPEFEQBg.png" /><figcaption>Dalle’s main screen. Text prompt at the center.</figcaption></figure><p>OpenAI has made Dalle free on its platform up to a certain limit of images. If you want to create more images there, it is necessary to buy credits to it. Using the platform is interesting for anyone who wants to create their own images without the need of writing any code.</p><p>Dalle is not the only generative model currently available. Another very popular option is <a href="https://www.midjourney.com/home/?callbackUrl=%2Fapp%2F">Midjourney</a>. Both these technologies are known for creating visual products from text prompts, but there are other AIs around, like <a href="https://prisma-ai.com/lensa">Lensa</a> and <a href="https://www.wombo.ai/">Wombo</a>, who work directly in the users’ images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sOKDlenWUbcvUaVDdl15cg.png" /><figcaption>“ a humpback whale swimming inside a square plastic bottle of water, digital art” (Prompt by me. <a href="https://www.instagram.com/p/ClXGt-vrsjM/">See variations</a>).</figcaption></figure><p>So, once you have picture what you want it is easy to get your creation. But what to do if you have only <em>an slightly idea of the final product</em>? Or if you want to create something, but <em>the prompt is not there yet? </em>Maybe you have used up all your credits? That is where ChatGPT comes in hand.</p><h3>Chat …what ?</h3><p>According to itself (when prompted with the question: “what is ChatGPT and what it is used for?”):</p><blockquote>ChatGPT is a variant of the GPT (Generative Pre-training Transformer) language model, specifically designed for chatbot applications. It is a machine learning model that has been trained on a large dataset of conversational text, and is able to generate human-like responses to a given input.</blockquote><blockquote>ChatGPT can be used to build chatbots that can have natural and engaging conversations with humans. These chatbots can be used for a variety of applications, such as customer service, online tutoring, or just for fun. They can be integrated into messaging platforms, websites, or mobile apps.</blockquote><blockquote>One of the main advantages of ChatGPT is its ability to generate responses that are tailored to the context of the conversation. This allows the chatbot to have more meaningful and relevant conversations with users, which can lead to more satisfying and engaging interactions.</blockquote><blockquote>Overall, ChatGPT is a powerful tool for building chatbots that can provide value to users in a variety of different contexts.</blockquote><p>I think the virtual chap introduced itself really well, uh? We interact with it the same way we interact with Dalle. The difference here is that we kinda chat with it like we would do with our friends.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*IfsHfZMYt9CgW5CpFPCODw.png" /><figcaption>ChatGPT main screen.</figcaption></figure><p>Ok, then we can ask this ChatGPT guy something we want. And, as stated before we want to generate a brief but rich description of a scene we pictured in our heads. The plan is thus simple: let us ask ChatGPT to elaborate on something we thought. Once it looks reasonably good, we transfer the response to DALL-E.</p><p>This may sound a little crazy... It is not enough only to create a high quality painting/photograph/picture with an AI, we can even ask another one to provide the details for the image?! Discussions on <a href="https://youtu.be/sFBfrZ-N3G4">what it means for human artists</a> or <a href="https://www.wired.com/story/openai-dalle-copyright-intellectual-property-art/">who will own the art of the future</a> have already started and they have a point. Personally (as an engineer who does not create and does not market art to make a living) I think these tools as a fast way to prototype ideas. I remember when I started working with Arduino and I did not need to wire up a complex circuitry to test if a couple of LEDs were ok, or when I started Python and I did not need to declare the data type of a dozen variables before trying out my newborn algorithm. At the end, we will need a time to digest all these new stuff. I think we are just in the beginning.</p><p>Philosophy aside… why don’t I try something to see if it works? Here it goes a meet up I had with the fellow.</p><blockquote><strong><em>Creating a good prompt…</em></strong></blockquote><blockquote>USER:<br>Hey Assistant! How are you doing?</blockquote><blockquote>AI:<br>Hello! As an artificial intelligence, I don’t have feelings or emotions in the same way that humans do. However, I’m here to assist you and answer any questions you have to the best of my ability. Is there anything specific you would like to know or discuss?</blockquote><blockquote>USER:<br>Please, describe for me a cinematic pink photograph of a purple Wolkswagen Microbus in a parking lot during a sunset.</blockquote><blockquote>AI:<br>Sure! Here is a description of a cinematic pink photograph of a purple Volkswagen Microbus in a parking lot during a sunset:The photograph is taken from a low angle, looking up at the Microbus as it sits in a parking lot. The sky is ablaze with pink and orange hues, the colors of the sunset reflecting off the windows and paint of the Microbus. The purple of the vehicle is vibrant and rich, standing out against the warm tones of the sunset. The parking lot is empty, save for the Microbus, adding to the sense of solitude and peacefulness. The Microbus itself looks well-loved and well-used, with small dents and scratches on the body and windows, giving it character. Overall, the photograph captures the essence of the Microbus and the peacefulness of the sunset, creating a nostalgicically cinematic image.</blockquote><blockquote>USER:<br>That is great! Repeat the same idea in 399 characters</blockquote><blockquote>AI:<br>A cinematic pink photograph of a purple VW Microbus in a parking lot during sunset. The sky is ablaze with pink and orange hues reflecting off the windows and paint of the Microbus. The parking lot is empty, adding to the sense of solitude. The Microbus looks well-loved and well-used, with dents and scratches giving it character. A nostalgicically cinematic image capturing the essence of the Microbus and the peacefulness of the sunset.</blockquote><blockquote>USER:<br>Ok, now in 299 characters</blockquote><blockquote>AI:<br>A pink sunset reflects off a purple VW Microbus in an empty parking lot. The Microbus is well-loved, with dents and scratches adding character. A nostalgicically cinematic image capturing the essence of the Microbus and the peaceful sunset.</blockquote><blockquote>USER:<br>Now describe the same idea, but with the 80s aesthetics</blockquote><blockquote>AI:<br>In this 80s-inspired photograph, a purple Volkswagen Microbus sits in a parking lot, basking in the glow of a dazzling pink and orange sunset. The retro vehicle is a true gem, with its vibrant color and classic design. It looks like it has been well-loved and well-used, with a few dents and scratches adding to its charm. The empty parking lot adds to the sense of solitude and peacefulness, making this photograph a true celebration of the carefree spirit of the 80s. The pink and orange hues of the sunset create a nostalgicically cinematic image, evoking memories of a bygone era.</blockquote><blockquote>USER:<br>Now make it look futuristic</blockquote><blockquote>AI:<br>In this futuristic photograph, a sleek purple Volkswagen Microbus sits in a bustling, high-tech parking lot. The vehicle itself is a marvel of modern design, with smooth lines and advanced technology on display. Despite the hustle and bustle of the surrounding area, the Microbus is a haven of calm, with its tinted windows and advanced noise-cancelling technology creating a sense of serenity within. As the sun sets, the sky is awash with vibrant hues of pink and orange, casting a warm glow over the Microbus and the bustling cityscape. The photograph captures the cutting-edge technology and futuristic design of the Microbus, while also paying homage to its classic roots.</blockquote><p>Amazing, uh? I decided to start the conversation in a polite manner (maybe when the machines rise against the humans ChatGPT will intercede for me, who knows?). We see ChatGPT answers politely as well, being realistic towards himself.</p><p>After they finished their train of thought, I brought the paragraph to Dalle. I thus found out it only accepts up to 399 characters. That is why I asked it to formulate the same idea using less symbols. I iterated it for some time until I got something reasonable. At the end, this is what I got.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kcgzxDzH7QlsfakM0KNHqA.png" /><figcaption>ChatGPT + DALLE art. <a href="https://www.instagram.com/p/CmXkvkcLliC/">See variations</a>.</figcaption></figure><blockquote>Some parts of this post was developed using ChatGPT</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ead9f6df558f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“please ChatGPT, write a four pages essay about the use of AI in the oil & gas industry”]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/please-chatgpt-write-a-four-pages-essay-about-the-use-of-ai-in-the-oil-gas-industry-e4da8a7f9fbd?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4da8a7f9fbd</guid>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[oil-and-gas-industry]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Fri, 09 Dec 2022 11:52:00 GMT</pubDate>
            <atom:updated>2023-01-02T18:21:08.609Z</atom:updated>
            <content:encoded><![CDATA[<h4>An introduction about AI… written by one..</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6CfNcvUrUGL425K_l88bIA.jpeg" /><figcaption><a href="https://commons.wikimedia.org/wiki/File:Bios_robotlab_writing_robot.jpg">Bios Robotlab writing (Wikimedia Commons)</a></figcaption></figure><p>The use of artificial intelligence (AI) in the oil and gas industry has grown significantly in recent years, with many companies adopting AI technology to improve efficiency and productivity. This essay will explore some of the ways in which AI is being used in the oil and gas industry, as well as some of the challenges and potential drawbacks of this technology.</p><p>One of the main ways in which AI is being used in the oil and gas industry is in the exploration and production of oil and natural gas. Using data from sensors and other sources, AI algorithms can help identify potential oil and gas deposits and predict the most likely locations for successful drilling operations. This can save companies a significant amount of time and money, as they no longer have to rely on trial and error to find oil and gas deposits.</p><p>Another area where AI is being used in the oil and gas industry is in the refinement and transportation of oil and gas products. AI algorithms can be used to optimize the refining process, allowing companies to produce more refined products with less waste. In addition, AI can be used to monitor and control the transportation of oil and gas products, helping to ensure that they are delivered safely and efficiently.</p><p>In addition to improving efficiency and productivity, AI can also help companies in the oil and gas industry to improve safety. For example, AI algorithms can be used to monitor equipment and processes, alerting workers to potential hazards and helping to prevent accidents. This can be particularly important in the oil and gas industry, where the potential risks are high.</p><p>However, there are also potential challenges and drawbacks to the use of AI in the oil and gas industry. One of the main concerns is the potential loss of jobs, as AI technology can automate many tasks that were previously done by human workers. This could lead to job losses and displacement, particularly for workers with lower levels of education and training.</p><p>Another concern is the potential for bias in AI algorithms. If the data used to train the algorithms is not representative or is biased in some way, the algorithms may produce biased results. This could lead to unfair treatment of certain groups of people, such as women or minorities, and could have significant negative consequences.</p><p>Overall, the use of AI in the oil and gas industry has the potential to improve efficiency, productivity, and safety. However, it is important for companies to consider the potential challenges and drawbacks of this technology, and to take steps to mitigate any negative effects. This may include providing training and support for workers who may be affected by job losses, and ensuring that AI algorithms are free from bias.</p><blockquote>The previous text was written by ChatGPT when prompted with: “please, write a four pages essay about the use of AI in the oil &amp; gas industry”.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4da8a7f9fbd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transfer Learning com VGG16 e ResNet50]]></title>
            <link>https://medium.com/@mateus.d.assis.silva/transfer-learning-com-vgg16-e-resnet50-39e58c3e84c3?source=rss-482a85636e71------2</link>
            <guid isPermaLink="false">https://medium.com/p/39e58c3e84c3</guid>
            <category><![CDATA[resnet50]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[transfer-learning]]></category>
            <category><![CDATA[vgg16]]></category>
            <dc:creator><![CDATA[Mateus de Assis Silva]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 21:11:08 GMT</pubDate>
            <atom:updated>2022-11-28T23:57:59.759Z</atom:updated>
            <content:encoded><![CDATA[<p>As técnicas de <em>Deep Learning</em> requerem um volume imenso de dados para que se possa ajustar os milhões de parâmetros das redes neurais. Entretanto, problemas cotidianos (seja de uma empresa que quer começar a usar IA em seus produtos ou mesmo um projeto acadêmico) não costumam ter essas bases gigantescas.</p><blockquote><em>Deep Learning</em> (DL) é um dos ramo de <em>Machine Learning</em> (ML), que por sua vez é um dos ramos de Inteligência Artificial (IA). Trata-se de um conjunto de técnicas baseadas em redes neurais: sistemas computacionais vagamente inspirados pelo funcionamento do cérebro. As redes neurais, nesse contexto, também são chamadas de ‘modelos’ ou ‘arquiteturas’.</blockquote><p>Além disso, mesmo nessas condições, treinar um modelo pode tomar um tempo que torna o projeto inviável, sem falar que o <em>hardware</em> necessário costuma ser caro. A título de curiosidade, o treinamento da <em>policy network</em> do AlphaGo utilizou 50 GPUs ao longo de 3 semanas. Caso se precise anotar os dados, é preciso ter em mente que esse processo é custoso e, dependendo do problema, isso pode exigir conhecimento especializado.</p><blockquote><em>Machine Learning</em> (e, consequentemente, <em>Deep Learning</em>) usa extensivamente <em>GPUs</em> pois esses processadores são capazes de executar simultaneamente vários cálculos. Isso acelera as operações necessárias para se criar um modelo.</blockquote><p>Ok. Com essa história fica claro que criar um modelo com <em>Deep Learning </em>do zero é uma tarefa complicada. Mas caso você precise (ou mesmo queira) fazer isso, como proceder? Por exemplo: supondo que você precise de um modelo de voz para um projeto de assistente virtual na empresa que você trabalha; ou de um modelo de visão embarcado para uma câmera de segurança, o que fazer?</p><p>O pulo do gato é o uso de uma técnica nomeada <em>Transfer Learning</em>. Se um modelo de uma dada tarefa foi treinado num <em>dataset</em> grande e generalista o suficiente, ele será genérico o suficiente para aquela tarefa. Assim, ele é um bom ponto inicial para o seu. Pense comigo: esse modelo antigo já aprendeu algo, certo? Portanto, treiná-lo em um <em>dataset</em> menor será o suficiente para atingir uma boa performance para o que você precisa. Isso é <em>Transfer Learning</em>.</p><blockquote><em>Dataset</em> é o conjunto de dados utilizado para gerar o modelo. É geralmente composto por entradas (como imagens) e saídas esperadas (rótulos como “gato” ou “cachorro”), separadas em instâncias (ou exemplos). O processo de se gerar as saídas para cada uma das entradas é nomeada “anotação”.</blockquote><p>Agora temos um outro jeito de pensar o problema. Ao invés de se pensar “preciso criar um modelo para essa tarefa”, temos “onde posso encontrar um modelo para essa tarefa?”. Felizmente, existem modelos famosos disponíveis para uso, como <a href="https://github.com/tensorflow/models/tree/master/official">esses no Github do Tensorflow</a>.</p><p>Ao longo desse post dois modelos de visão serão retreinados para uma tarefa diferente das que foram inicialmente planejados. Todo o processo utilizado será detalhado, mas se você deseja ir ver o código, <a href="https://github.com/Gildson/MODEL-CARD-Transfer-learning">basta clicar nesse link</a>. Caso você aprenda melhor de forma visual, <a href="https://www.loom.com/share/8a3a65f989204570b893dc33df7db367">essa explicação também se encontra em forma de vídeo</a>.</p><blockquote>Esse projeto foi construído com os esforços conjuntos de <a href="https://www.linkedin.com/in/gildsonbsantos/">Gildson Bezerra</a>, <a href="https://www.linkedin.com/in/mateus-assis-013a46140/">Mateus de Assis</a>, e <a href="https://www.linkedin.com/in/micael-balza/">Micael Balza</a>. Os códigos podem ser encontrados no <a href="https://github.com/Gildson/MODEL-CARD-Transfer-learning">Github do projeto</a>, enquanto que os resultados para cada modelo estão no WandB: <a href="https://wandb.ai/gildson/transfer_learning_VGG-16">VGG16</a> e <a href="https://wandb.ai/gildson/transfer_learning_ResNet50">ResNet50</a>.</blockquote><h3><em>Algumas palavras sobre Transfer Learning </em>…</h3><p>Anteriormente foi dito que <em>Transfer Learning</em> baseia-se na noção que, se um modelo já foi treinado para uma tarefa em uma base de dados considerável, ele pode ser considerado para aquela tarefa. Mas como isso funciona?</p><p>No livro Deep Learning (Ian Goodfellow et al., 2015) é dito que se um modelo visual aprende sobre um conjunto de categorias visuais (como cachorros e gatos) num <em>dataset</em> grande, ele é capaz de aprender outro conjunto de categorias visuais (formigas e vespas) num <em>dataset</em> menor. Isso ocorre pois as <strong>representações aprendidas inicialmente numa tarefa são úteis para atingir rapidamente generalização na nova tarefa</strong>. No contexto de <em>Deep Learning</em>, “representação” se refere ao que o modelo aprende a partir dos dados (no contexto de CNNs, as <em>feature maps</em>). Isso ocorre pois as categorias visuais tendem a compartilhar noções de baixo nível (bordas, formas, mudanças geométricas e de iluminação, etc). Isso é uma forte indicação que, nesse contexto, é importante manter as camadas inferiores (mais próximas da entrada) imutáveis enquanto se atualiza as camadas superiores (próximas à saída). Essa imutabilidade é também chamada de “congelamento de camadas”.</p><blockquote>CNN é uma sigla para “<em>Convolutional Neural Networks</em>” (Redes Neurais Convolucionais). Trata se de um modelo especializado para tarefas associadas a imagens e vídeos. Ele utiliza largamente a operação convolução, daí o nome.</blockquote><blockquote>“<strong>Feature maps</strong>” é o resultado da operação de convolução aplicada pelo modelo e mantido internamente a ele. À medida que se aproxima da saída a partir da entrada, é dito que o nível das informações carregadas nesses <strong>feature maps</strong><em> </em>são cada vez mais altas.</blockquote><p>Seguindo o <a href="https://www.tensorflow.org/tutorials/images/transfer_learning">tutorial do Tensorflow</a>, existem duas formas de personalizar o modelo pré-treinado:</p><ul><li>Usando <em>Feature Extraction</em>: aqui se utiliza as representações aprendidas originalmente pelo modelo<em> </em>para classificar novas instâncias. Basta gerar-se um classificador novo e pronto.</li><li>Usando <em>Fine-Tuning</em>: “descongela-se” as últimas camadas da rede original e treine-as junto ao novo classificador. Isso serve como um “ajuste fino” para as representações geradas ao fim do modelo.</li></ul><blockquote>“Classificador” é o nome dado a uma rede neural capaz de indicar a qual grupo um certo dado de entrada pertence.</blockquote><h3>Vamos para o Exemplo…</h3><p>O exemplo realizado é uma tarefa visual de classificação de raças de cães e gatos. Para isso, foram usadas duas redes neurais cuja performance foi comparada. O projeto é composto de vários arquivos <em>Jupyter Notebook</em>, cada um responsável por um passo do <em>pipeline </em>de <em>machine learning</em>. Esses códigos estão disponíveis no <a href="https://github.com/Gildson/MODEL-CARD-Transfer-learning">Github do projeto</a>. O uso de cada modelo está descrito em um arquivo separado, e os resultados de cada um se encontra no WandB (<a href="https://wandb.ai/gildson/transfer_learning_VGG-16">VGG16</a> e <a href="https://wandb.ai/gildson/transfer_learning_ResNet50">ResNet50</a>). Por fim, as funcionalidades do <a href="https://codecarbon.io/"><em>package</em> <em>CodeCarbon</em></a> foram adicionadas para estimar a pegada de carbono do processo de treino das redes. Cada etapa será descrita a seguir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Od5pGfw-VfKD5qH2Sz9i5g.png" /><figcaption>Workflow de Machine Learning (<a href="https://github.com/ivanovitchm/embedded.ai/blob/main/lessons/week_05/first_classifier/fig/workflow.png">ivanovitchm</a>).</figcaption></figure><h4><em>FETCH</em></h4><p>A etapa de <em>fetching</em> é responsável por buscar a base de dados e disponibilizá-la localmente. No caso do projeto, ela realiza o <em>download </em>das fotos dos animais direto do <a href="https://drive.google.com/file/d/1NFyLLyh1vpahzUcQh5sT_m9O9kNgplL0/view"><em>Google Drive</em></a><em> </em>(199 instâncias por classe). Apesar dessa etapa ter sido feita no Colab, é possível baixar os dados e acessar localmente. Conforme pode ser inspecionado no terminal da célula, existem três espécies de cachorro (Pug, Keeshond, e Boxer) e duas espécies de gatos (Bengal e <em>Russian Blue</em>). Uma vez devidamente baixadas, as imagens são adicionadas a um <em>Wandb artifact</em>, o qual é logado na plataforma.</p><blockquote>No contexto de práticas de <em>Machine Learning</em> (também conhecido como <em>MLOps</em>), um <a href="https://docs.wandb.ai/guides/artifacts"><em>artifact</em></a> é um arquivo gerado por qualquer etapa do <em>pipeline</em>, seja dados, seja modelos. O processo de salvar um artefato num servidor (nesse caso o WandB) é chamado “logar”.</blockquote><p>Vale mencionar que esse processamento é realizado uma vez que os pacotes são importados, o <em>logger</em> é configurado e o login do WandB é concluído. Esses detalhes são repetidos em todos os arquivos (etapas) seguintes, portanto serão suprimidos por questões de simplicidade. Outra informação que será suprimida se refere ao upload de artefatos gerados no WandB. Em suma, todo artefato gerado por qualquer etapa é salva no WandB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*ZJUDIhPM0s--E__PQ6WTlg.png" /><figcaption>Exemplos de Classes (imagens já processadas).</figcaption></figure><h4>PREPROCESSING</h4><p>O processamento aplicado é a transformação da imagem para uma dimensão padrão (aqui 32 por 32 por 3). Uma classe denominada <em>SimpleProcessor </em>é definida localmente no arquivo. Ela é responsável por aplicar a função <em>resize </em>da biblioteca OpenCV. Entretanto, isso não acontece diretamente. Uma classe nomeada <em>SimpleDataLoader</em> itera sobre as imagens e aplica o objeto <em>SimpleProcessor</em> em cada uma.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*0VR1zISy1yARHc1PZrWBQQ.jpeg" /><figcaption><em>Um exemplo de Russian Blue (imagem não processada). Observe que a imagem não é quadrada (comparar com a versão processada na figura anterior).</em></figcaption></figure><blockquote><a href="https://opencv.org/"><em>OpenCV</em></a> é uma biblioteca multiplataforma que oferece funcionalidades relativas a Visão Computacional.</blockquote><h4>DATA SEGREGATION</h4><p>A segregação de dados é responsável por separar os dados em conjuntos de treino e teste. Para isso utilizou se a função <a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html">train_test_split()</a> da biblioteca <a href="https://scikit-learn.org/">Scikit Learn</a>. Reservou-se 20% dos dados para testes, enquanto que a outra parte foi destinada para o treino do sistema.</p><blockquote>De forma a garantir que o modelo está aprendendo relações úteis entre os <em>inputs</em> e os <em>labels</em>, utiliza-se um subconjunto nomeado <em>Validation set. </em>Para garantir que as escolhas de projeto não enviesaram o resultado, usa-se um subconjunto nomeado <em>Test Set</em>. Apenas o <em>Training set </em>é utilizado para treinar o modelo.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*U0w4MU60Zp66hyP4qlsvuQ.png" /><figcaption>conjuntos de dados de ML: <strong>training</strong>, <strong>validation</strong>, e <strong><em>test</em></strong> (<a href="https://commons.wikimedia.org/wiki/File:ML_dataset_training_validation_test_sets.png">Wikimedia Commons</a>).</figcaption></figure><h4>TRAINING</h4><p>Duas redes foram utilizadas: <a href="https://www.tensorflow.org/api_docs/python/tf/keras/applications/vgg16/VGG16">VGG16</a> e a <a href="https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet50/ResNet50">ResNet50</a>. Ambas foram importadas do TensorFlow e treinadas com o <em>ImageNet</em>.</p><blockquote><a href="https://www.image-net.org/index.php">ImageNet</a> é um banco de dados de imagens com mais de 14 milhões de exemplos anotados. Embora tais exemplos posam ser separados em 1000 (mil) labels, eles já foram descritos de maneira diversa, com frases e palavras (um conceito nomeado <em>synset</em>). Até o momento da escrita desse artigo mais de 20 mil <em>synsets</em> já haviam sido submetidos ao dataset.</blockquote><p>A <a href="https://paperswithcode.com/method/vgg">rede neural VGG</a> tem a clássica arquitetura de CNN, composta por pares de camadas de convolução e <em>pooling . </em>Aqui é onde o <em>feature learning</em> ocorre (também chamado de <em>feature extractor</em>). Ao final, temos uma rede <em>Dense</em>, onde a classificação ocorre (também nomeada <em>classification head</em>). Enquanto isso, a <a href="https://paperswithcode.com/method/resnet">ResNet</a> introduz o conceito de <a href="https://paperswithcode.com/method/residual-block">Blocos Residuais</a>, onde conexões <em>skip </em>são adicionadas para facilitar o treino de arquiteturas mais profundas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mh1gt4eBjQWhdxQ3W1ZPWw.png" /><figcaption>Arquitetura VGG (<a href="https://en.wikipedia.org/wiki/File:VGG_neural_network.png">Wikimedia Commons</a>).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6HDuqhUzP92iXhHoS0Wl3w.png" /><figcaption>Conexões Residuais, o bloco básico da ResNet (<a href="https://paperswithcode.com/method/residual-connection">Papers With Code</a>)</figcaption></figure><p>Como comentado anteriormente, existem dois arquivos <em>Jupyter </em>(um pra cada rede neural). Apenas o arquivo da VGG16 será explicado.</p><p>Inicialmente realiza-se a separação dos dados em treino e validação. Esses conjuntos são então separados em <em>batches </em>a serem utilizados no treino.</p><blockquote>Obs: um breve trecho de código é executado para melhorar a performance através da otimização de alocação na CPU. Esse código utiliza as funções <em>tf.data.AUTOTUNE </em>e<em> prefetching.</em></blockquote><p>O próximo passo é preparar o modelo para o sistema de <em>Feature Extraction</em>. Lembre-se que esse sistema utiliza o que foi aprendido anteriormente por um modelo como <em>inputs</em> para uma nova rede neural. Isso implica que os parâmetros do <em>feature extractor</em> não variam ao longo do treino.</p><p>O modelo em questão é composto principalmente por:</p><ul><li>O <em>feature extractor </em>do modelo pré-treinado no Imagenet;</li><li><em>Global Average</em>, que diminui a dimensão da saída do <em>feature extractor</em>;</li><li>Dropout de 20%;</li><li>Uma camada classificadora <em>fully connected </em>(<em>Dense</em>). Há 5 neurônios aqui (um por classe).</li></ul><p>Além disso, outros processos ocorrem antes que as imagens a serem utilizadas no treinamento sejam inseridas. Duas camadas adicionais processam as imagens: uma de <em>Data Augmentation</em> (que vira horizontalmente e rotaciona a imagem aleatoriamente) e outra que aplica o preprocessamento original do <em>feature extractor</em> utilizado.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*pmv713qCbfWkurlcQDVugQ.png" /><figcaption>Exemplo de Data Augmentation.</figcaption></figure><blockquote>Embora “camada” seja um termo utilizado, no geral, para conjuntos de neurônicos que aplicam um processamento em paralelo (por exemplo, um conjunto de filtros de convolução), esse termo acaba ganhando um novo significado. Nesse contexto, operações extras como <em>Data Augmentation</em> (que visa aumentar a base de dados e prover certa robustez ao modelo) e <em>Global Average </em>(que faz a média artimética de uma certa entrada) também são denominadas “camadas”.</blockquote><p>Com o modelo pronto, realiza-se o treino utilizando um <em>learning rate</em> de <em>0.0001</em> ao longo de 20 épocas. Durante esse processo utiliza-se o <em>CodeCarbon </em>pra estimar a pegada ecológica do treino.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*y8CKFfJkf8-C1IIJP8SouQ.png" /><figcaption>Loss e Acurácia, modelo VGG — Feature Extraction</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*pGxWfxnVBxL6She3VzOuyA.png" /><figcaption>Matriz de Confusão, modelo VGG — Feature Extraction</figcaption></figure><p>Como visto nas figuras acima, o modelo é capaz de estimar, com alta acurácia, a raça dos animais em questão. A matriz de confusão mostra que a única dificuldade existente se refere a raça felina <em>Russian Blue. </em>Em termos de treino, o modelo rapidamente aprende como classificar as <em>features </em>geradas. Em torno de 10 épocas o modelo já atinge uma acurácia acima de 80%. Curiosamente, ao longo de todo o treino a acurácia de treino permanece abaixo da acurácia de validação.</p><p>Concluído o Sistema de <em>Feature Extraction</em>, temos o de <em>Fine Tunning</em>. O <em>feature extractor </em>é o mesmo, mudando apenas o classificador. A arquitetura desse pode ser vista abaixo.</p><ul><li>Uma camada de <em>Flatten</em> (transformando as últimas matrizes de <em>features</em> <em>maps </em>em um array);</li><li>Camada <em>Dense</em> com 256 neurônios (saída ReLU);</li><li>Dropout de 50%;</li><li>Camada <em>Dense</em> com 5 neurônios (saída Softmax).</li></ul><p>O treino é composto por duas etapas. Na primeira, utiliza-se RMSprop para treinar o classificador (os parâmetros do <em>feature extractor</em> estão congelados) e fazê-lo chegar a valores numericamente adequados (impedindo assim que o <em>extractor </em>se degrade). Depois disso, o conjunto final de camadas convolucionais do extractor é descongelado e o treino procede com SGD.</p><blockquote>Uma das etapas de criação de um modelo de <em>Machine Learning</em> é chamado treino. Para isso se utiliza otimizadores. Otimizadores são algoritmos numéricos que modificam os parâmetros do modelo para que esse tenha uma boa performance. O SGD é o otimizador clássico, enquanto que o RMSprop é mais rápido.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*7A_glEfbXaJF6b2yeFPfAw.png" /><figcaption>Loss e Acurácia, modelo VGG —Fine Tuning (segundo treino)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*H23MKf5n2L0izj7Hul0mtA.png" /><figcaption>Matriz de Confusão, model VGG — Fine Tuning</figcaption></figure><p>Ao contrário dos resultados do <em>Feature Extractor,</em> o modelo treinado com <em>Fine Tuning </em>não comete erro algum ao realizar o <em>predict</em> no conjunto de teste. Isso poderia indicar que é melhor treinar as últimas camadas convolucionais, ao invés de mantê-las estáticas. Entretanto, nota-se que a performance de treino ocorre com uma acurácia muito próxima de 1.0, mesmo no início do processo. Isso mostra como o classificador com mais de uma camada é melhor que o classificador de camada única do processo anterior (visto que o processo do <em>Feature Extractor </em>é igual à primeira etapa do <em>Fine Tuning</em>). A conclusão é que, para esse problema e essa rede, não é necessário utilizar <em>Fine Tuning</em>, bastando usar um classificador adequado.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*ZfMLvUFAB8xWShjev5QZ1Q.jpeg" /><figcaption>Run Summary — VGG</figcaption></figure><h4>RESULTADOS RESNET</h4><p>Esses mesmos experimentos foram feitos para o modelo ResNet50. Os resultados podem ser vistos a seguir.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*iHcPuqJ7YXWlfs_Tgr545g.png" /><figcaption>Loss e Acurácia, modelo ResNet— Feature Extraction</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*XCkr96_6xuWU6W5ba65DYQ.png" /><figcaption>Matriz de Confusão, modelo ResNet — Feature Extraction</figcaption></figure><p>Ao contrário do <em>Feature Extractor </em>com VGG16, o ResNet não comete nenhum erro ao classificar novas instâncias, conforme atesta a Matriz de Confusão. Corroborando com essa performance, temos uma acurácia de validação que atinge 100%. A mudança causada pela rede (visto que a ResNet é uma arquitetura mais profunda, o que implica que aprende uma representação mais rica) é nítida no problema.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*P1m2VZWabMkbkWZWK8ulBA.png" /><figcaption>Loss e Acurácia, modelo ResNet — Fine Tuning (segundo treino)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*KOlDZZSEfUTmGryH_axgPA.png" /><figcaption>Matriz de Confusão, modelo ResNet — Fine Tuning</figcaption></figure><p>Comparando o <em>Fine Tuning</em> da ResNet com o mesmo aplicado ao VGG, nota-se como as acurácias e o <em>l</em>oss<em> </em>são mais estáveis. Novamente, isso pode ocorrer devido as representações aprendidas serem mais ricas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*n3sZx3g1ILLTI_uWAnC9LA.jpeg" /><figcaption>Run Summary ResNet</figcaption></figure><p>Quanto ao <em>CodeCarbon</em>, nota-se que as emissões estimadas para a ResNet são maiores que aquelas estimadas para o VGG. Isso faz sentido, visto que a arquitetura anterior é bem mais complexa. Sendo a pegada de carbono associada com o consumo energético, e este aos custos do processo, é importante notar como o mesmo processo com redes diferentes pode gerar custos bem diferentes. No fim, observa-se que não seria necessário utilizar uma arquitetura mais complexa para atingir-se uma acurácia altíssima (considerando que esse é o objetivo). Como foi apresentado, um <em>Feature Extractor</em> com um classificador adequado seria o suficiente, poupando custos e tempo de experimentação.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39e58c3e84c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>