<?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 Mickael Boillaud on Medium]]></title>
        <description><![CDATA[Stories by Mickael Boillaud on Medium]]></description>
        <link>https://medium.com/@mickael.boillaud?source=rss-37ace7bb19e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FIn_q6n06NfMag3vtV5Rfw.png</url>
            <title>Stories by Mickael Boillaud on Medium</title>
            <link>https://medium.com/@mickael.boillaud?source=rss-37ace7bb19e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:42:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mickael.boillaud/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[Streamline Your ML Pipeline: A Comprehensive Guide to DVC (Data Version Control)]]></title>
            <link>https://medium.com/@mickael.boillaud/streamline-your-ml-pipeline-a-comprehensive-guide-to-dvc-data-version-control-97251730b1cf?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/97251730b1cf</guid>
            <category><![CDATA[model-versioning]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-version-control]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Thu, 01 Feb 2024 14:38:05 GMT</pubDate>
            <atom:updated>2024-02-01T14:38:05.156Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VDE0bXI0wTPCIaC3QLRekg.png" /></figure><h4>Table Of Contents</h4><p><strong>I.Introduction</strong><br><strong>II The goal of the blog</strong><br> II.1 What we are going to cover<br> II.2 What we expect you to know<br><strong>III. Create our new environment<br> </strong>III.1 Init DVC into our repo<br> III.2 Add remote versioning storage<br> III.3 Finish our environment setup with the python package<br><strong>IV. Create our DVC Pipeline<br> </strong>IV.1 Transform<br> IV.2 Split<br> IV.3 Train<br> IV.4 Evaluation<br> IV.5 Metrics Logs<br> IV.6 Run it all<br><strong>V. Experiment, experiment and experiment!<br> </strong>V.1 Extension overview<br> V.2 Run other experiments<br><strong>VI. Conclusion<br>VII. Ressources</strong></p><h3>I Introduction</h3><p>After working on an AI task that necessitates the use of machine learning, you’ll likely end up with one or several models to train. This process involves making experiments and iterating over them. However, it remains a challenge to efficiently tweak hyper-parameters without the risk of breaking the model code. Additionally, maintaining metrics and versioning trained model parameters can be cumbersome, especially in large projects. This complexity often forces us to employ third-party tools or software for effective tracking and experimentation management.</p><p>Today’s mission is to introduce the use of DVC (Data Version Control), a tool that is part of an MLOps ecosystem. DVC integrates seamlessly with GitOps practices, allowing us to use familiar tools like GitHub/GitLab/BitBucket … for file versioning. By leveraging the power of DVC, we can code a fully automated pipeline that encompasses data gathering, splitting, model training, and evaluation, while simultaneously maintaining version control of both the data and the model. This approach not only enhances efficiency but also improves the reproducibility and scalability of ML projects. Sounds great, doesn’t it?<br>Grab a cup of tea or coffee and embark on a journey into the world of DVC.</p><p><strong><em>Before you comtinue your reading, feel free to follow me for more Deep Learning &amp; MLOps blogs and </em></strong><a href="https://www.linkedin.com/in/mickael-boillaud/"><strong><em>connect with me on LinkedIn</em></strong></a><strong><em> to not miss any content. Don’t forget to leave a like if the blog helps you through your research!</em></strong></p><h3>II The goal of the blog</h3><p>As discussed earlier, our primary objective is to demonstrate how DVC (Data Version Control) can facilitate the creation of an efficient training pipeline for our models. With this goal in mind, we’ll tackle a classic task in machine learning: 2D segmentation. Specifically, we’ll focus on creating masks for roads in aerial images. While the modeling aspect, using a standard U-Net architecture for segmentation, is important, it will not be our main focus. Instead, we will concentrate on illustrating how DVC can streamline our workflow. This approach will highlight DVC’s role in managing and versioning datasets and models, which is crucial for efficient and reproducible machine learning projects.</p><h4>II.1 What we are going to cover</h4><p>In this blog, we will harness DVC to create a reproducible model pipeline. The goal is to experiment with different training and model preprocessing hyper-parameters. We’ll guide you through every step of the data pipeline: downloading, preprocessing, splitting the data, training the model, and evaluating its performance. Each step will be framed to emphasize the functionality and benefits of using DVC, showcasing how it enhances the efficiency and reproducibility of machine learning workflows.</p><h4>II.2 What we expect you to know</h4><p>To get the most out of this blog, you should have a basic understanding of the entire Machine Learning pipeline, from data collection to model training. While we delve deeper into the AI aspects, we’ll be using PyTorch. Familiarity with PyTorch’s syntax is a bonus but not a requirement for following along. Please note, we will not specifically cover the task of image segmentation in detail; However, to begin, locate the initial <a href="https://github.com/Camaltra/aerial_road_segementation">GitHub repository</a> that contains the codebase without DVC integration. We will guide you through the process of incorporating DVC into this existing project by re-recreate it from scratch. Additionally, for those interested in exploring this topic further, we will provide resources and references at the end of the blog.</p><p><strong>For an optimal learning experience, we highly recommend having both this blog and the associated </strong><a href="https://github.com/Camaltra/personnal_ml/tree/main/dvc_implemetation"><strong>GitHub repositor</strong></a><strong>y open. This approach allows you to simultaneously grasp the theoretical concepts and engage with the practical applications.</strong></p><h3>III. Create our new environment</h3><h4>III.1 Init DVC into our repo</h4><p>Let’s begin by setting up a new repository on Git, and then we’ll integrate DVC into it. Once your repository is ready, the next step is to install DVC on your computer. You can find installation instructions for various system distributions <a href="https://dvc.org/doc/install">here</a>. After installing, it’s time to bring DVC into play by initialising it in our repository:</p><pre>dvc init</pre><p>This command will create a special directory in your repository .dvc, similar to how Git creates a .git folder. This directory is crucial as it enables version control of our data and experiments.</p><p>For an enhanced experience, we highly recommend using Visual Studio Code (VSCode) as your IDE. Installing the DVC extension in VSCode will significantly aid in tracking and managing your experiments. The extension provides a user-friendly interface for monitoring changes and progress.</p><p>Well done mates! You should now have a folder tree and IDE that look like this! (<em>README, main script and .gitignore were also created at this step</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*Pu8EgsHKbljPvhcFBsnKDg.png" /><figcaption>DVC init repository <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><h4>III.2 Add remote versioning storage</h4><p>To effectively track and share all our files, data, and models with your team, it’s essential to allocate storage space on a cloud provider. This allows us to upload our data and models, which DVC will manage by creating hashes (though we won’t delve into the technical details here, as this is a gentle introduction to the tool). For this example, we’ll use AWS S3, which offers 5 GB of free storage — more than enough for our project.</p><p>If you don’t already have an AWS account, you’ll need to create one. We’ll guide you through the process of setting up a user and a bucket with the appropriate permissions. This setup is crucial as it enables our local DVC to push and pull data and models as needed throughout our pipeline.</p><p><strong><em>If you’re already familiar with AWS</em></strong><em>, you can skip the detailed setup section. In essence, you just need to create a bucket and a user to interact with it. The necessary policies are detailed in the following section (III.2.B Create our IAM user to communicate with our repository). Once you have your bucket and user set up, simply connect your terminal to AWS using the AWS CLI. Then get back to the blog at III.2.C Connect user on our terminal and S3 to DVC</em></p><p><strong>III.2.A Creating the bucket<br></strong>When logged in to your AWS account, reach the search bar and start typing S3, click on the service and there we are. Hit the orange button Create Bucket</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dw0bN9uX4ayH7d40ixBT_w.png" /><figcaption>AWS S3 creation <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><p>Select the region closest to where you live; it should be set as the default. Next, create a name for your S3 storage. For simplicity, we’ll name ours dvcdemo. Most of the remaining parameters can be left at their default settings, as they won&#39;t significantly impact our project. However, you might consider enabling bucket versioning. This feature adds an additional layer of version control for your data and models, although it&#39;s not mandatory for our purposes.</p><p><strong>III.2.B Create our IAM user to communicate with our repository<br></strong>The next step involves creating an IAM (Identity and Access Management) user. This user will facilitate the interaction between our local DVC setup and the remote AWS S3 storage. According to the DVC documentation, the necessary permissions for this user include: s3:ListBucket, s3:GetObject, s3:PutObject, and s3:DeleteObject. Let&#39;s proceed to create these permissions to enable our IAM user to perform these operations on our S3 storage.</p><p>To set this up, go to the IAM panel in the AWS Management Console. On the left sidebar, select ‘Policies’. Now, you’re ready to create a new policy that suits our needs. Click on the JSON tab to enter the policy details. You can copy and paste the following policy configuration. Remember to replace dvcdemo in the ‘Resource’ section with the name of your S3 bucket:</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;s3:ListBucket&quot;,<br>                &quot;s3:GetObject&quot;,<br>                &quot;s3:PutObject&quot;,<br>                &quot;s3:DeleteObject&quot;,<br>                &quot;s3:ListBucketVersions&quot;,<br>                &quot;s3:GetObjectVersion&quot;,<br>                &quot;s3:DeleteObjectVersion&quot;<br>            ],<br>            &quot;Resource&quot;: [<br>                &quot;arn:aws:s3:::dvcdemo&quot;,<br>                &quot;arn:aws:s3:::dvcdemo/*&quot;<br>            ]<br>        }<br>    ]<br>}</pre><p>As you’ll notice, we’ve added three additional policies that facilitate managing versions on our bucket S3 server. However, as previously mentioned, this step is optional and will not be covered in detail in this blog.<br>Then give a name to the policy, for our purpose the policy will be named dvcdemo.</p><p>Now that our new policy is created, it’s time to create a user to facilitate communication. Navigate to the ‘Users’ section and click the ‘Create User’ button, which is typically colored orange. Name the user (we’ll name ours ‘dvcdemo’). In the next step, where you attach policies, select Attach Policies, search for the policy we just created and select it. No other special settings are needed, so you can proceed to create the user.</p><p>The last, but equally important step, is to generate an access key for our user. This key is crucial for logging into the AWS CLI, which we’ll discuss in the upcoming subsection. Go to the page of the user you just created, and click on ‘Create Access Key’. Then, download it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YkyMRpHJf97LXIblLkRZKw.png" /><figcaption>AWS User interface <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*3pmREFWMkwCA2N3Km3nIhA.png" /><figcaption>AWS User gather access key <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><p>Choose the ‘CLI’ option, and then download the CSV file which contains the user’s access key ID and secret access key. It’s crucial to keep these keys secure, as this is the only opportunity you’ll have to access the secret key. Losing these keys means you won’t be able to retrieve the secret again, and you’ll have to create a new access key if needed.</p><p><strong>III.2.C Connect user on our terminal and S3 to DVC<br></strong>The final step in establishing the connection involves working in the terminal. First, install the <a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html">AWS Command Line Interface</a> (CLI). Once installed, link it to your newly created IAM user using the command aws configure. You will be prompted to enter your access key ID and secret access key, which you obtained in the previous step.</p><p>After setting up the AWS CLI, it’s time to connect our AWS S3 storage to DVC. This is done by modifying the configuration file in the .dvc directory of your project. Open the config file and input the following settings:</p><pre>[core]<br>    remote = storage<br>[&#39;remote &quot;storage&quot;&#39;]<br>    url = s3://dvcdemo</pre><p>Now if you try to push files using DVC, you should have no error at all.</p><pre>dvc push</pre><p><strong>III.2.D Final<br></strong>We’ve come a long way in setting up AWS S3 storage to store our DVC data and artifacts. If this is your first time working with these tools, it’s understandable if the process seems overwhelming. Rest assured, with a bit more practice and exploration of AWS, you’ll soon become more comfortable and familiar with its functionalities. Remember, mastering new tools often involves a learning curve, but it gets easier with time and experience.</p><h4><strong>III.3 Finish our environment setup with the python package</strong></h4><p>As we will use python package, we need to create a virtual environement. For this project we will use a simple venv. You can find the requirement.txt on the github project and install it.</p><p><em>Important to note: We install dvc[s3] as we use AWS, take care of changing this to the could provider you use.</em></p><h3>IV. Create our DVC Pipeline</h3><p>Now that we have completed the setup, let’s assume our basic R&amp;D on the task has been successful, and we have all the necessary components to start our experiments. Our focus will be on conducting experiments with one model, primarily adjusting data preprocessing and training parameters, rather than making significant changes to the model structure itself.</p><p>To structure our workflow, we’ll use a file named dvc.yaml. This file will outline all the stages of our pipeline, including each step&#39;s dependencies and the full pipeline&#39;s requirements. Additionally, we&#39;ll create a params.yaml file. This file will be instrumental in managing the parameters we wish to adjust throughout our experiments, enabling easy and organised experimentation.</p><p>We will also import the model structure into the directory ./src/model/. As mentioned earlier, we will treat the model as a &#39;black box&#39; for the purposes of this project. To maintain a clean and readable structure, we&#39;ll create several folders: ./src/stages/, ./src/pipelines/, and ./src/utils.</p><ul><li>The ./src/stages/ folder will contain scripts that call the pipelines; these are the scripts referenced by the dvc.yaml orchestrator.</li><li>The ./src/pipelines/ folder will house the individual steps of our pipeline, each designed for different purposes like transforming data, splitting datasets, training models, etc.</li><li>Finally, the ./src/utils folder will serve as a repository for various useful functions that support our stage and pipeline processes.</li></ul><p>This structure is designed to enhance the readability and maintainability of our project, ensuring that each component is logically organised and easily accessible.</p><p>Let’s dive into the exciting part and start creating the first step of our pipeline! This is where we begin to bring our project to life, step by step.</p><h4>IV.1 Transform</h4><p>Before we dive into the coding, there’s an important note regarding the dataset. Since it’s available through a Google Drive link and is quite large, we can’t download it directly via API. Therefore, we’ll manually download the dataset and place it in the ./src/data folder. This approach ensures we have the dataset ready for the next stages of our pipeline. The dataset is available <a href="https://github.com/yhlleo/RoadNet">here</a> (<em>Rename the folder from Ottawa-Dataset to </em><em>raw_dataset</em>)</p><p>Returning to our DVC pipeline file, let’s create a stage dedicated to data transformation. This stage involves splitting the data into two sets as outlined in the dataset’s README, applying a patchify process to create sub-images, and storing the transformed data in new files.</p><p>To provide a brief overview, a DVC pipeline requires certain key elements to function effectively. It is always structured around the following components:</p><pre>stages:<br>  stage_name:<br>    cmd: `the command to run the stage (ie python script)`<br>    deps:<br>      - `file path dependance (as input)`<br>    params:<br>      - `params dependance, see params file`<br>    outs:<br>      - `out file path that DVC will keep track for versionning`</pre><p>We can also add a metrics value (Will be seen in the training part)</p><p>So here for our purpose of transform pipeline, we will create the dvc.yaml part and the linked params.yaml part.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb70a3a4d05b04736afb4555acb64a25/href">https://medium.com/media/eb70a3a4d05b04736afb4555acb64a25/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33db032a6d495a8345d948d171474555/href">https://medium.com/media/33db032a6d495a8345d948d171474555/href</a></iframe><p><em>Note here, we specify a random state for further stages while we need a reproducible pipeline.</em></p><p>To achieve our goals, we need to create two essential files: ./src/stages/transform.py and ./src/pipelines/transform.py. These files will carry out the code necessary to meet our objectives. We will explore these two files in detail to understand their structure and function. However, for subsequent stages, we might not delve as deeply into each file, focusing instead on the configuration and critical aspects of the DVC pipeline.</p><p>As explained earlier, the transform.py file in the stages folder is responsible for retrieving relevant parameters from the params.yaml file. This process is mostly boilerplate code. After retrieving the parameters, this file will pass them to the transform.py in the pipelines folder, which exposes a run method. The run method will transform our data, in essence, patchify it and split the data given the mandatory to test and train set. Then save it to appropriate folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37663a26949462daf37a2fa0310e6734/href">https://medium.com/media/37663a26949462daf37a2fa0310e6734/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7df358752d7cf2b8c65dd157b19d92cb/href">https://medium.com/media/7df358752d7cf2b8c65dd157b19d92cb/href</a></iframe><p>When you execute the newly created pipeline using the dvc repro command, you&#39;ll notice new files appearing in the data folder.</p><p><em>Important to note: Using DVC repro command will use the parameter saved in the </em><em>.lock file, or create a new experiment if no presence of </em><em>.lock file. It will not serve as our experiments explorer command later on.</em></p><p>Additionally, a dvc.lock file will be generated, tracking the specifics of this pipeline execution. This dvc.lock file is crucial as it keeps track not only of the current data but also of future artifacts. Therefore, you should commit this file to Git (and the created .gitignore) and push the newly created data to the DVC S3 remote storage using the dvc push command.</p><p><em>Important to note: It’s crucial to understand that we only push the </em><em>dvc.lock file to Git because it contains all the necessary information for pulling data from DVC. Data and experimental outputs, such as artifacts, will not be pushed to Git but will instead be stored in S3 or another remote storage solution. </em><strong><em>The </em></strong><strong><em>dvc.lock file is responsible for tracking all these elements</em></strong><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*-U0o-Dts8me-UHaifaE-4g.png" /><figcaption>`dvc repro` (1st execution) <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><p>If you attempt to rerun the dvc repro command, even without pushing the data to the S3 remote instance, you&#39;ll find that the pipeline has been cached. Since datas folder already exists and there have been no changes to the code or parameters, the pipeline&#39;s computation will be skipped.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*Ff2hFd82-GzynTXAUMomcA.png" /><figcaption>`dvc repro` (2nd execution) <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><p>Furthermore, after pushing the DVC history to remote storage, if you happen to delete the ./src/data/train or test folders, simply run dvc pull to restore the data. But be careful! Do not delete the dvc.lock file, as this would disrupt the link between your repository and its associated data.</p><p>Fantastic! With our setup complete, we’re now ready to run the transformation pipeline. This is a great opportunity to experiment with different parameters, such as the patch_size or even selecting different images for the test set. While it&#39;s generally recommended to adhere to the data handling best practices as outlined in the relevant GitHub data repository references, feel free to make adjustments to better understand the impact of these variables on your model&#39;s performance.</p><h4>IV.2 Split</h4><p>As we proceed, let’s adopt a more succinct approach to describing the Python code for our pipeline. Generally, it follows a consistent structure: a script in the stages folder to gather parameter variables, and domain-specific code in the pipelines folder to execute the desired step.</p><p>Focusing on the next step, our aim is to split our training data into validation and training datasets. This stage will depend on the training data folder and produce two new data folders (validation ones). It will also be linked to global, transformation, and splitting parameters. With this information at hand, we’re equipped to construct the stage in our pipeline. For a more in-depth understanding, you can always refer to the Python code for the pipelines in the GitHub repository.</p><p><em>Important to note: We append the code in the </em><em>dvc.yaml and </em><em>params.yaml files as we want only create one full and complete pipeline.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a56e7865d3d9a7d5ef79eed7e02b1556/href">https://medium.com/media/a56e7865d3d9a7d5ef79eed7e02b1556/href</a></iframe><p>On the parameters side, we’ll be configuring the output data file paths and setting the percentage of images to include in our validation dataset. These are the parameters we will define in our params.yaml file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/969891648cf27917b188cb76d9638ddb/href">https://medium.com/media/969891648cf27917b188cb76d9638ddb/href</a></iframe><p>Do you notice how the task of creating the pipeline is becoming more manageable? We simply need to list the dependencies of our script, define the outputs, and specify the parameters we wish to adjust. The remaining task is to express all these elements in the correct syntax and write the Python code to carry out the operations. As the python code as mostly already been write during the R&amp;D phase, we only need re-formate it to become more configurable, but the logic still the same.</p><p>I hope you’re starting to see how DVC can be a streamlined addition to our ML projects, enhancing our workflow with minimal overhead.</p><h4>IV.3 Train</h4><p>Now, let’s work through this exercise together. Before you continue reading, take a moment to list all the dependencies needed to run the script, the outputs, and the parameters you might want to adjust for experiments on the training data.</p><p>Ready? Let’s review what’s required. Our dependencies include the training and validation datasets, the model code itself, and our dataset manager file, which is particularly relevant for PyTorch users. If you’re not familiar with this file, don’t worry — it’s a specific requirement for our use case. The output we’re aiming for is our trained model, which will be saved in pickle format.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa9e4fb5f474b3511ba1e8429a7decea/href">https://medium.com/media/fa9e4fb5f474b3511ba1e8429a7decea/href</a></iframe><p>As for our parameters, we need to track the path where the model will be saved. We might also consider adding visualization for each training epoch. Additionally, there are several training parameters we could adjust: the number of epochs, batch size, data augmentations, learning rate, etc. For simplicity, we’ll focus on the ones listed here. And with that, our training pipeline is set up! You’re doing great!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddf80d4adea6c52ddc50891cfe7eb592/href">https://medium.com/media/ddf80d4adea6c52ddc50891cfe7eb592/href</a></iframe><p>Let’s take a quick look at the Python code, particularly focusing on how we track metrics during model training. To do this efficiently, we’ll leverage the capabilities of DVCLive. This tool allows us to log experiment metrics throughout the model’s training process. For each epoch, we’ll record the loss (both training and validation loss) and accuracy, along with the Dice score, which is particularly relevant for segmentation tasks. The importance of this approach will become clearer in the following section.</p><p>It’s important to note that the syntax for implementing <a href="https://dvc.org/doc/dvclive/ml-frameworks">DVCLive</a> may vary depending on the machine learning framework you’re using. So, be sure to adjust the code accordingly to fit your specific framework.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba730beeb8a23b46cfa1129539fbccb4/href">https://medium.com/media/ba730beeb8a23b46cfa1129539fbccb4/href</a></iframe><p><em>Important to note: we will need to add the metrics log into our pipeline step to make sure that DVC will take into account for the experiments plotting. (See it in the next section).</em></p><p><em>Important to note: We make use of the random state to init as well as model weight as the order of Dataset output. We do use it also when splitting the data. The ensure the reproducibility of our experiments.</em></p><h4>IV.4 Evaluation</h4><p>Now, let’s step into the model evaluation phase. Just like before, take a moment to consider all the elements needed for this pipeline stage to function correctly.</p><p>Given that this is a simpler part and we’ve already discussed pipeline creation at length, I’ll directly provide the dvc.yaml and params.yaml configurations for this stage.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0929e8f61c6a46bfc6f9cd8cb2b56a3e/href">https://medium.com/media/0929e8f61c6a46bfc6f9cd8cb2b56a3e/href</a></iframe><p><em>Important to note: During our evaluation pipeline, we generate a file named </em><em>./evaluation/metrics.json (as detailed in the Python code). It&#39;s essential to inform DVC about this file so that it can be spotted as metrics file for the experiments visualisation. Here as we don’t want to cache it, we specify it at false.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/591a3a2c7ff532c9bb69d82a4ae886b2/href">https://medium.com/media/591a3a2c7ff532c9bb69d82a4ae886b2/href</a></iframe><h4>IV.5 Metrics Logs</h4><p>We’ve successfully completed our pipelines. Now, it’s time to integrate DVCLive into our pipeline file. This integration will enable the tracking and logging of metrics, a crucial step for monitoring our model’s performance. Here’s how we do it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b011a4106cc714271a38b588ed0aa6dc/href">https://medium.com/media/b011a4106cc714271a38b588ed0aa6dc/href</a></iframe><h4>IV.6 Run it all</h4><p>We’ve reached the end of our incredible journey — well done! But what’s better than seeing everything come together? Let’s run our entire pipeline from scratch to see it in action. But before, let resume our entire DVC pipeline instructions file and params file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bcf1c828a2eb74ac48cbb8c5a5e92523/href">https://medium.com/media/bcf1c828a2eb74ac48cbb8c5a5e92523/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aed2e95bb0101e470ec628b9934b6fb3/href">https://medium.com/media/aed2e95bb0101e470ec628b9934b6fb3/href</a></iframe><p>We can also view the full pipeline by running the dvc dag command (<em>A dag is how we called the entire pipeline ie same as in Airflow</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*SGPtEt_vR0WFYKNxu2PIAA.png" /></figure><p>The to run the entire pipeline, as discuss previously, we can run:</p><pre>dvc repro</pre><p>If you’ve previously run dvc repro, you can use the command dvc repro --no-run-cache to force the pipeline to run again from the beginning. <em>You can also delete the </em><em>./.dvc/cache file along with the created one’s during the pipelines.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rP3Gsdy6-GTxo3eYshik4Q.png" /><figcaption>Run of the entire pipeline <a href="https://medium.com/u/37ace7bb19e">Mickael Boillaud</a></figcaption></figure><p>Fantastic! Everything works seamlessly! Our experiments have been conducted successfully, resulting in data and a model that are both versioned and ready to be pushed to our S3 remote storage.</p><p>But, have we achieved our ultimate goal? Recall that our task was to perform a multitude of experiments to identify the best one, or a few of the best, from which we could make a final selection. Now, with our complete pipeline in place, we can easily run experiments by modifying the parameters in the params.yaml file. To enhance user-friendliness in this process, the VSCode extension we installed earlier plays a crucial role!</p><p>By leveraging this tool, we streamline the experimentation process, making it more efficient and accessible. So, let’s not stop here; the real power of our setup lies in its capacity for rapid iteration and comparison of results. Dive back in, tweak those parameters, and let’s discover the optimal configurations for our model.</p><h3>V. Experiment, experiment and experiment!</h3><h4>V.1 Extension overview</h4><p>Let now unwrap the interface of the Vs Code extension</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9wJsDY172LLmEwTqhjIRLw.png" /></figure><p>If you followed along the turorial, you should have something similiar of this while clicking into the Show experiments button <em>(Except here I have some other experiments due to other branch of my repo, but there are not experiements I ran, just a miss from the extension)</em>. But the idea here is that, on our workspace, we have the result of our experiments we run on the last section, with its score and all its parameters, and all of this directly in our IDE! We can then check them as raw data, but we could also make the use of the plot step we wrote in our DVC yaml file, and by selecting the workspace experiment and click on the Show Plot button, we can access to them</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UjSrdDmUDm9op-el28rcxw.png" /></figure><p>As we here to talk about the DVC, we will not take into account the metrics analyses, but we can say that the model trained but not enough to get good results so far.</p><h4>V.2 Run other experiments</h4><p>To embark on further experiments, adjusting our params.yaml file is all that&#39;s required. For those using the graphical interface, simply click on &#39;run experiment&#39; to initiate a new trial. However, if the interface isn&#39;t available to you, the dvc run command serves as a powerful alternative, directly from the command line. Given that we&#39;re assuming the use of Visual Studio Code for this tutorial, we&#39;ll proceed with its integrated tools for simplicity and efficiency.</p><p>On our end, we plan to conduct three experiments, selecting parameters at random to demonstrate the model’s responsiveness to different configurations. This approach isn’t just about finding the best model settings; it’s also a showcase of how DVC facilitates rapid, flexible experimentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N4Zu6mz2zn1NGhRPYT8v0g.png" /></figure><p>We now have three impressive curves that will assist us in choosing the best model. Once you’ve identified the model that satisfies your criteria, you can easily create a new branch for it. Do this by hovering over the chosen experiment in the ‘Experiments’ tab, and then initiate the creation of a new branch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*OXlY_6gTitwTg7sBsLYTrw.png" /></figure><p>After creating this branch, you can open a Pull Request (PR) with the output metrics. If the PR is validated, you’re then ready to re-run the experiment. Following this, you can push the updated data and model to the DVC S3 storage. It’s important to note that data is typically pushed to DVC only when models are deemed potentially suitable for production. Not all experiments should be pushed to DVC, to avoid exhausting our storage capacity. This selective approach ensures we manage our resources efficiently, focusing on the most promising results.</p><p><em>Important to note: While these steps can be automated using CML (Continuous Machine Learning), this topic extends beyond the scope of our current blog, so we won’t cover it here. But it’s something to consider for further streamlining your MLOps workflow in the future.</em></p><h3>VI. Conclusion</h3><p>Congratulations, folks! You’ve reached the end of this blog, and now you’re equipped to use DVC for tracking your model experiments. This is a significant step forward in the world of MLOps! Of course, DVC is just one of many tools available to data scientists for such tasks — others include MLFlow, Weights &amp; Biases (W&amp;B), and more. The key takeaway is that by understanding how one tool works, you’ve laid the groundwork for learning similar tools, as they often share common workflows.</p><p>Your next mission, should you choose to accept it, is to apply what you’ve learned to one of your projects. Implement a full DVC pipeline tailored to your needs. Once you’ve done that, I encourage you to explore deeper functionalities of DVC and other tools within the <a href="https://github.com/iterative">Iterative</a> ecosystem. This exploration will not only solidify your understanding but also open doors to more advanced techniques in data science and MLOps.</p><p><strong><em>Before you leave, feel free to follow me for more Deep Learning &amp; MLOps blogs and </em></strong><a href="https://www.linkedin.com/in/mickael-boillaud/"><strong><em>connect with me on LinkedIn</em></strong></a><strong><em> to not miss any content. Don’t forget to leave a like if the blog helps you through your research!</em></strong></p><h3>VII. Ressources</h3><p><a href="https://github.com/Camaltra/personnal_ml/tree/main/dvc_implemetation"><strong>DVC GITHUB REPO PROJECT</strong></a><strong> (Covered through the blog)<br></strong><a href="https://github.com/Camaltra/aerial_road_segementation"><strong>AREAL ROAD GITHUB REPO PROJECT</strong></a><strong> (Base task used for implementing DVC)</strong></p><p><strong>Tools website and ecosystem<br></strong><a href="https://www.youtube.com/@dvcorg8370">DVC Youtube Channel</a><br><a href="https://dvc.ai/">DVC WebSite</a><br><a href="https://cml.dev/">CML WebSite</a></p><p><strong>Other Tools that perform the same or sub task<br></strong><a href="https://airflow.apache.org/">Airflow</a> (Pipelining tool)<br><a href="https://mlflow.org/">MLFlow</a> (Experiments tracker)<br><a href="https://wandb.ai/site">W&amp;B</a> (Experiments tracker and Display)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97251730b1cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Denoising Diffusion Model from scratch using PyTorch]]></title>
            <link>https://medium.com/@mickael.boillaud/denoising-diffusion-model-from-scratch-using-pytorch-658805d293b4?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/658805d293b4</guid>
            <category><![CDATA[pytorch-tutorial]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[diffusion-models]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Fri, 05 Jan 2024 10:26:42 GMT</pubDate>
            <atom:updated>2024-06-21T10:34:24.210Z</atom:updated>
            <content:encoded><![CDATA[<h3>Table Of Content</h3><p><strong>I Introduction</strong><br> I.1 VAE<br> I.2 GAN<br> I.3 Why we need to get another model architecture<br> I.4 What we are going to cover | Expected Knowledge<br><strong>II The goal of this blog<br>III DDPM Theory<br> </strong>III.1 Forward Process<br> III.2 Backward Diffusion Process<br> III.3 Training Algo<br> III.4 Sampling<br> III.5 Before We Conclude with the Theory<br> III.6 Conclusion<br><strong>IV Model Implementation<br> </strong>IV.1 A bit about the model<br> IV.2 ConvNext Block<br> IV.3 Sinusoidale timestamp Embedding<br> IV.4 DownSample &amp; UpSample<br> IV.5 Time Multi Layer Perceptron<br> IV.6 Attention is always all we need<br> IV.7 Putting all together<br><strong>V. Diffusion Implementation<br>VI. Training Key Points<br>VII. Annexe<br>VIII. Sources</strong></p><p><strong><em>Before you begin your reading, feel free to follow me for more Deep Learning blogs and </em></strong><a href="https://www.linkedin.com/in/mickael-boillaud/"><strong><em>connect with me on LinkedIn</em></strong></a><strong><em> to not miss any content.</em></strong></p><h3><strong>I Introduction</strong></h3><p>Before delving into the nitty-gritty details of how the Denoising Diffusion Probabilistic Model (DDPM) works, let’s take a look at some historical perspectives on Generative AI.</p><h4>I.1 VAE</h4><p>The first notable model resulting from research is the VAE — Variational Autoencoder. Basically, it employs what we call an encoder, a probabilistic latent space, and a decoder. During training, the encoder predicts mean and variance for each image. These values are then sampled from a Gaussian distribution and passed into the decoder, where the inputted image is expected to be similar to the outputted one. The process involves using KL Divergence to compute the loss. One remarkable strength of VAEs lies in their capacity to generate a diverse range of images. During the sampling phase, we simply sample from a Gaussian distribution, and the decoder creates a new images.</p><p><em>You can find here a </em><a href="https://github.com/Camaltra/holbertonschool-machine_learning/blob/main/unsupervised_learning/autoencoders/3-variational.py"><em>simple implementation</em></a><em> of only the VAE using Keras with tensorflow backend</em></p><h4>I.2 GAN</h4><p>After a short year of utilising Variational Autoencoders (VAEs), a groundbreaking generative family model emerged — Generative Adversarial Networks (GANs). This introduction marked the inception of a new class of generative models characterized by the collaboration of two neural networks: a generator and a discriminator, involved in an adversarial training process. The generator’s objective is to craft realistic data, such as images, from random noise, while the discriminator endeavours to differentiate between authentic and generated data. Throughout the training phase, the generator and discriminator continually refine their capabilities through a competitive learning process. The generator generates increasingly convincing data to outsmart the discriminator, which, in turn, sharpens its ability to discern between real and generated samples. This adversarial interplay culminates in the generator producing high-quality, lifelike data. In the sampling phase, following GAN training, the generator can create fresh samples by inputting random noise. It transforms this noise into data that often mirrors real examples, showcasing GANs’ proficiency in generating novel and realistic data.</p><h4>I.3 Why we need to get another model architecture</h4><p>Both models suffer from different issues, such as the possible non-convergence of GANs. However, a crucial point propels researchers to explore further. While GANs excel in generating realistic images that closely look like the one in the training set, VAEs specialize in creating a diverse range of images, albeit with a tendency towards producing blurry ones. Yet, no existing models have successfully combined both capabilities — creating images that are both highly realistic and diverse. This challenge has posed a significant hurdle for researchers to tackle and resolve.</p><p>Six years after the release of the first GAN paper, and seven years after the release of the VAE’s one, a groundbreaking model emerged: the Denoising Diffusion Probabilistic Model (DDPM). DDPM combines the strengths of both worlds, excelling in the creation of diverse and realistic images post-training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XXVUU6ampfI41E1iDymmzw.png" /><figcaption>logP(x) represent the diversity of the created sample while FID is the quality of these sample</figcaption></figure><h4>I.4 What we are going to cover | Expected Knowledge</h4><p>In this article, we will delve into the intricacies of DDPM, covering its training processes, including both the forward and backward processes, and exploring how the sampling is executed. Throughout this exploration, we will construct a DDPM from scratch using PyTorch, complete with its full training pipeline.</p><p>We assume that you are already familiar with the basics of deep learning and have a solid foundation in deep computer vision. We will not be introducing these fundamental concepts. Additionally, a basic understanding of probabilities is required.</p><p>Time to grab a cup of coffee, sit back, and let’s embark on this journey.</p><h3>II The goal of this blog</h3><p>Now that we’ve embarked on an introductory journey into the realm of generative models, let’s establish why we are delving into this study. The context for this exploration is rooted in my recent personal project, which also serves as my final school project. For details on personal motivations and project management, you can find the link to the blog <a href="https://medium.com/@mickael.boillaud/final-studies-project-arealquizz-82c6b9027533">here</a>. Today’s task centers around the creation of deepfake aerial satellite imagery of Earth. Despite its initial complexity, fear not, as each step will be thoroughly explained in the modeling theory, complemented by code snippets. Our objective is both ambitious and thrilling — to generate images where humans are convinced of their authenticity, achieving an impressive 80% credibility across all the generated images.</p><p><strong><em>Dataset exemple used for the task can be found in the Annexe section</em></strong></p><h3>III DDPM Theory</h3><p>Denoising Diffusion Probabilistic Models (DDPM) represent a cutting-edge approach in the realm of generative models. Unlike traditional models relying on explicit likelihood functions, DDPM operates by iteratively denoising a diffusion process. This involves gradually adding noise to an image and attempting to remove that noise. The underlying theory is grounded in the idea that transforming a simple distribution, such as a Gaussian, through a series of diffusion steps can result in a complex and expressive image data distribution. In other words, by shifting the sample from its original image distribution into the Gaussian distribution, we can create a model to reverse this process. This enables us to start from the full Gaussian distribution and end up with the image distribution, effectively generating a new image.</p><p>The training of DDPM involves two essential steps: the fixed and non-learnable forward process, designed to create noisy images, and the subsequent backward process. The primary objective of the backward process is to de-noise the images using a specialised machine learning model.</p><h4>III.1 Forward Process</h4><p>As mentioned earlier, the forward process is a fixed and non-learnable step. However, it does require some pre-defined settings. Before delving into these settings, let’s understand how it works.</p><p>The core concept of the process is to start with a clear image. Over a specified number of steps denoted by ‘T,’ a small amount of noise is gradually introduced, following a Gaussian distribution. Let’s take a closer look at how this process unfolds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QPPh9Zipx2MOoCsPQaPEgA.png" /></figure><p>As observed in the image, noise is incrementally added at each step. Now, let’s dig into the mathematical representation of this noise.</p><p>As previously mentioned, the noise is sampled from a Gaussian distribution. To introduce a small amount of noise at each step, we use a Markov Chain. This means that to generate the image at the current timestamp, we only need the image from the last timestamp. The Markov Chain concept is pivotal here and will prove crucial for the subsequent mathematical details.</p><p>It’s worth noting to mention that a Markov Chain is a stochastic process where the probability of transitioning to any particular state depends solely on the current state and time elapsed, not on the sequence of events that preceded it. This property simplifies the modelling of the noise addition process, making it more manageable for mathematical analysis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*-GzWIhLGd298QCR-DIODZA.png" /><figcaption>Transition between t-1 to t</figcaption></figure><p>Here, the variance parameter, denoted as beta, is intentionally set to a very small value. This choice aims to introduce only a minimal amount of noise at each step.</p><p>As previously mentioned, the step parameter ‘T’ dictates the number of steps the process undergoes to generate a fully noisy image. In the paper, this parameter is set to 1000, which might appear substantial. A valid concern arises: do we truly need to create 1000 noisy images for each original image in our dataset? The Markov Chain aspect proves instrumental in addressing this concern. Since we only need the image from the last step to predict the next one, and the noise added at each step remains constant, we can streamline computations by generating the noisy image for a specific timestamp. Moreover, employing a re-parametrization trick for betas allows us to further simplify the equations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fbspxG7-Cot4XvJBnAax3g.png" /><figcaption>A error here, (4) on the variance it’s a_t and x_t is x_0</figcaption></figure><p>The result is obtained through the development of equation (2) by incorporating the new parameters introduced in equation (3).</p><h4>III.2 Backward Diffusion Process</h4><p>Now that we have introduced noise to all the images, it’s time to perform the inverse operation. As it stands, conducting the backward process to denoise the images cannot be achieved mathematically unless we know the initial condition, i.e., the un-noised image at t = 0. However, our goal is to sample directly from the noise to create a new image, and thus, we lack information about the outcome. Consequently, we need to devise a method to denoise images step by step without prior knowledge of the outcome. What better solution than employing a deep learning model to approximate this intricate mathematical function?</p><p>With a bit of mathematical context, the model will approximate the equation (5). A noteworthy detail is that we will adhere to the DDPM original paper and keep the variance fixed, although it’s also possible to make the model learn it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PXRV1pLCfw2xCgekM0mhVQ.png" /><figcaption><em>Theta will always represent the model weights</em></figcaption></figure><p>As evident, the model’s task is to predict the mean of the noise added between the current timestamp and the previous one. By doing so, we can effectively withdraw the noise, achieving the desired outcome. However, what if, in the end, we don’t want the model to predict the operation of noise added between two steps? Instead, what if our goal is for the model to predict the noise added from the “original image” to the last timestamp? While this involves a bit of mathematics, let’s delve into the reasoning behind this choice.</p><p>As discussed earlier, performing the backward process mathematically is challenging unless we know the initial image without noise. Let’s start by defining the post-variance, denoted as beta tilde, and explore potential solutions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8bnoy0hDHi1lNqfqTISCCQ.png" /></figure><p>Hence, our model’s task is clear: predict the noise added from the initial image to the image at timestamp ‘t.’ Remarkably, our forward process enables us to execute this operation, starting from a clear image and progressing to a noisy image at timestamp ‘t.’ What a fortuitous coincidence!</p><h4>III.3 Training Algo</h4><p>Since we will dig into and explain the reasoning behind this choice shortly, let’s assume that the model architecture employed for making predictions will be a U-Net.</p><p>Now, for the training phase, our objective is to, for each image in the dataset, randomly select a timestamp within the range [0, T] and compute the Forward Diffusion Process. This yields a clear and somewhat noisy image, along with the actual noise used. Leveraging our understanding of the Backward Process, we will employ the model to predict the noise added to the image. With both the real and predicted noise available, it appears we’ve stepped into a supervised machine learning problem. This brings a sense of familiarity — a known territory.</p><p>But now, which loss function should we use to train our model? Since we are working with a probabilistic latent space, it becomes evident that the Kullback-Leibler (KL) Divergence is a suitable choice. <br>The KL Divergence measures the difference between two probability distributions, in our case, the distribution predicted by the model and the expected distribution. In our scenario, incorporating KL Divergence in the loss function guides the model not only to produce accurate predictions but also to ensure that the latent space representation adheres to the desired probabilistic structure.</p><p>Complex mathematical details that won’t be displayed here but are available in the sources section, the KL Divergence in this case can be approximated to an L2 loss function. Consequently, we arrive at the following loss function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0TN3f17af0J5-1xg0P3Gag.png" /></figure><p>Great, it seems that we have finally arrived at the training algorithm presented in the paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*1--Tzl4eG72ebyqk5pYVtg.png" /><figcaption>Algorithm (1) page (4) | DDPM Paper</figcaption></figure><h4>III.4 Sampling</h4><p>Now that we have a good understanding of how we can execute the backward process, it’s time to apply it. Let’s begin with a fully random image at time T and use the backward process T times to finally arrive at time 0. This constitutes the second algorithm outlined in the paper</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*JZs6e2rJlMGJlZ0wpHn6Tw.png" /></figure><h4>III.5 Before We Conclude with the Theory</h4><p>It appears we have a multitude of different parameters — beta, beta tildes, alpha, alpha bar, etc. However, we haven’t yet provided information on how these parameters are chosen. The only parameter known at this point is T, which is set to 1000.</p><p>For all the listed parameters, their selection is contingent on beta. Beta, in a sense, determines the amount of noise we aim to add at each step. Consequently, to ensure the success of the algorithm, it is crucial to carefully choose values for betas.<br>Various methods of sampling beta were explored during the experimentation phase of the paper. The original approach, linear sampling, raised concerns due to two plateaus — one at the beginning and a big one at the end — where the images either received insufficient noise or became excessively noisy. To address this issue, an alternative, more commonly used method known as cosine sampling was adopted. Cosine sampling provides a smoother and more consistent addition of noise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C9RYL4JrrYvsvpqCyWlwQg.png" /><figcaption><a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/">https://lilianweng.github.io/posts/2021-07-11-diffusion-models/</a></figcaption></figure><h4>III.6 Conclusion</h4><p>What a journey! We’ve covered the entire diffusion process and delved into the underlying mathematics. We’ve made great progress! However, we are only halfway through our journey as we still need to implement all the knowledge we’ve gained into our IDE as Python code. Refill your coffee cup, and let’s the second round start!</p><h3>IV Model Implementation</h3><h4>IV.1 A bit about the model</h4><p>As introduced in the last section, it’s no secret that we will utilize a U-Net architecture for noise prediction. The choice was driven by the fact that U-Net is an ideal architecture for image processing, capturing spatial and feature maps, and providing an output size identical to the input. The selection of U-Net came naturally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m8jYt_ye3sPIdIsasD0Y3Q.png" /><figcaption>Basic UNet (<a href="https://datascientest.com/u-net">https://datascientest.com/u-net</a>)</figcaption></figure><p>Given the complexity of the task and the requirement to use the same model for every step (where the model needs to be capable, with the same weights, of denoising both a fully noised image and a slightly noisy image), tuning the model is essential. This involves incorporating more complex blocks and introducing an awareness of the used timestamps through a pre-sinusoidal embedding step. These enhancements aim to make the model an expert at the denoising task. We’ll explore each building block before moving on to constructing the full model. A more complex version of the model can be found in the codebase, linked to the blog. You’ll also have access to the training steps and denoising process.</p><h4>IV.2 ConvNext Block</h4><p>To meet the need for enhancing model complexity, the fundamental convolution blocks play a crucial role. Instead of solely relying on the basic block outlined in the u-net paper, we will incorporate another inspired by the ConvNext paper. This includes utilising residual and timestamps subnets to align with the specified requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oyDF8j8zSucbElvFyyFXbw.png" /><figcaption>Modified ConvNext Block</figcaption></figure><p>Inputs consist of ‘x,’ representing the image, and ‘t,’ an embedded visualization of the timestamp with a size of ‘time_embedding_dim.’<br>The block plays a pivotal role in learning spatial and feature maps throughout the process, thanks to its complexity and the residual connection with the input and the last layer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46a868abf51fa06871f6e1463395308c/href">https://medium.com/media/46a868abf51fa06871f6e1463395308c/href</a></iframe><h4>IV.3 Sinusoidale timestamp Embedding</h4><p>One of the crucial blocks in the model is the Sinusoidal Timestamp Embedding block, which enables the encoding of a given timestamp to retain information about the current time that the model needs for decoding as the model will be used for all the different timestamps. This block will then be used in the model itself and become part of another block that we will examine along with the rest of the model.<br>This is at the end something pretty classic, that appear on a lots of other implementation and will not spent to much time here</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f164daf690dd23a14bc3d2d4ebdab41/href">https://medium.com/media/9f164daf690dd23a14bc3d2d4ebdab41/href</a></iframe><h4>IV.4 DownSample &amp; UpSample</h4><p>A notable improvement has been made to these process to gain more insight into spatial features and feature maps. It now includes an additional Conv2D layer for more comprehensive learning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*r1Mf5QBZOuIjWAxtoGyvNg.png" /><figcaption>Down and Up sampling layers</figcaption></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88ea241af49027e4bb51d375f2ee92a8/href">https://medium.com/media/88ea241af49027e4bb51d375f2ee92a8/href</a></iframe><h4>IV.5 Time Multi Layer Perceptron</h4><p>This subnet, as discussed in the time embedding section, utilizes it to create a time representation based on the given timestamps ‘t.’ The output of this Multi-Layer Perceptron (MLP) will also serve as the input “t” in all the modified ConvNext blocks.<br>This subnet is define directly in the model as a Sequential component, but could have it’s own block.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qd25ZP88ImYcw1_fek45fQ.png" /><figcaption>Time MLP</figcaption></figure><p>Here, ‘dim’ is a hyperparameter of the model representing the number of channels desired for the first block. It serves as the base computation for the number of channels in the subsequent blocks, making more sense when examining the entire model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef9a3b0f9d5b72fceaaae2cae009d4ad/href">https://medium.com/media/ef9a3b0f9d5b72fceaaae2cae009d4ad/href</a></iframe><h4>IV.6 Attention is always all we need</h4><p>Before wrapping up all the building blocks, let’s make a quick stop at something optional but utilized in one of the Unets in the codebase. The Attention block serves to enhance the residual connection’s role in learning. It does so by focusing more on the important spatial information derived from the left part of the Unet through an attention mechanism computed with the residual connection and the feature maps computed by its middle-low latent space. It’s derived from the ACC-UNet paper.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CH1HHmo6ckL2SdAgiHaFRQ.png" /><figcaption>Attention Block</figcaption></figure><p>The gate represents the upsampled output from the lower block, while the x residual represents the residual connection at the level where attention is applied.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f8394c07d9e274e4356b19c232766d0/href">https://medium.com/media/4f8394c07d9e274e4356b19c232766d0/href</a></iframe><h4>IV.7 Putting all together</h4><p>It’s time to consolidate all the previously discussed blocks (excluding the attention block) into a robust Unet. Notably, each level incorporates two residual connections instead of just one. This modification is intended to address potential overfitting concerns given the model’s complexity.</p><p>While creating a clear schema for this might be a bit challenging, we’ve made our best effort to provide the clearest representation. (Residual connection does not help)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IrJOCOCa43bZ3YbCon1BhQ.png" /><figcaption>Global UNet Arch</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*PrUqOItRmivLDOGv2hAo4A.png" /><figcaption>Legend</figcaption></figure><p>And so with all the knowledge we got, let’s build the model</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ba797e7c73a50679b5439beb0d068fb/href">https://medium.com/media/1ba797e7c73a50679b5439beb0d068fb/href</a></iframe><h3>V. Diffusion Implementation</h3><p>Now, let’s explore how the diffusion is implemented. Since we’ve already covered all the mathematical components used for the forward, backward, and sampling processes, we’ll focus on providing the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ca4b95e84b3ae5bc431f1a6b56f1b34/href">https://medium.com/media/1ca4b95e84b3ae5bc431f1a6b56f1b34/href</a></iframe><p>It’s important to note that the Diffusion Process serves as a model for the training part. Additionally, it opens up a sampling interface that allows us to generate samples using an already trained model.</p><p>You can also find all the betas scheduler in the code base directly, code will not be share here.</p><h3>VI Training Key Point</h3><p>For the training part, we utilised a dataset of 30,000 images obtained through the ETL process included in the codebase — <strong><em>Dataset exemple used for the task can be found in the Annexe section</em></strong>. The model underwent training for over 37,000 steps, with a batch size of 16 per step. The image size was constrained to 128x128 due to GPU memory allocation restrictions. Samples were generated every 1000 steps using an Exponential Moving Average (EMA) model weight to smooth the sampling, and the model version being saved.</p><p>A quick note before examining the results is that most of the data in the training set comes from the middle and south of Spain (not because we’re Spanish but mostly due to the rectangular shape of the country). As a result, the majority of the data in the training set look like dry areas or dense forests, which explains the model’s outcomes.</p><p>In the initial 1000 steps of training, the model began capturing some features but still missed certain areas. Progress became more evident around the 10,000-step mark, where the model started producing promising results. Towards the end of the 30,000 steps, the quality of results significantly improved, but there is a presence of black images. Not to worry, this is simply because the model did not have a sufficient variety of samples, and thus, the data distribution of real images was not entirely mapped to the Gaussian distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-wIMKOyBP1HL-Z-dxwA_0w.png" /><figcaption>Model Sampling during training</figcaption></figure><p>With the final model weights, we can generate some truly remarkable pictures. Despite the limitations in image quality due to the 128x128 size restriction, the model performed admirably.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zKDXm-yQmBRa1YsPWIWbJA.png" /><figcaption>Result at the end of training (37K steps)</figcaption></figure><p>It’s worth noting that the results could potentially be more beautiful and more realistic. However, a deliberate choice was made to introduce additional data (~5000 images) from France 🇫🇷 during the last 4000 steps. This decision might have slightly troubled the model as it attempted to adjust to this new dataset. The added images included cities and agricultural fields, noticeable in final images. With more examples and further training, the model has a great potential to produce highly convincing outcomes.</p><h3>VII. Conclusion</h3><p>I leave it to you as the sole judge of the outcomes. If you’ve reached this point, please share your thoughts in the comment section on whether you believe the model performed well.</p><p>Congratulations, you’ve made it through, learning all the necessary knowledge and practicing the implementation by following the blog to create your own Diffusion Model.</p><h3>VIII.Annexe</h3><h4>VIII.1 Dataset Exemple</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IZCcRdERhLGgy2LrCM2nww.png" /><figcaption>10 pictures of the 30K Dataset</figcaption></figure><h3>IX. Sources</h3><p><a href="https://github.com/Camaltra/this-is-not-real-aerial-imagery/tree/main/src/ai">PROJECT GITHUB</a></p><h4>IX.1 More on the math</h4><p><a href="https://lilianweng.github.io/posts/2021-07-11-diffusion-models/">What are Diffusion Models?</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FH45lF4sUgiE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DH45lF4sUgiE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FH45lF4sUgiE%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/da1615d41378a6b408657decefdd27d7/href">https://medium.com/media/da1615d41378a6b408657decefdd27d7/href</a></iframe><h4>IX. Paper Cover</h4><p>DDPM Paper <a href="https://arxiv.org/abs/2006.11239">https://arxiv.org/abs/2006.11239</a><br>ConvNext Paper <a href="https://arxiv.org/abs/2201.03545">https://arxiv.org/abs/2201.03545</a><br>UNet Paper: <a href="https://arxiv.org/abs/1505.04597">https://arxiv.org/abs/1505.04597</a><br>ACC UNet: <a href="https://arxiv.org/abs/2308.13680">https://arxiv.org/abs/2308.13680</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=658805d293b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Final Studies Project: ArealQuizz]]></title>
            <link>https://medium.com/@mickael.boillaud/final-studies-project-arealquizz-82c6b9027533?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/82c6b9027533</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[diffusion-models]]></category>
            <category><![CDATA[generative-model]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Fri, 05 Jan 2024 09:53:57 GMT</pubDate>
            <atom:updated>2024-01-05T10:33:15.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*wzBvh7y8NT7jvfMW-U1Nbg.png" /></figure><h3>Table of Content</h3><p><strong>Words From the Author (Optional)<br>I Introduction<br> </strong>I.1 Constraints<br> I.2 Stack<br><strong>II Web Application<br> </strong>II.1 Security and Login<br> II.2 User Quizz Interaction<br> II.3 Quizz Creation and Storage<br> II.4 Conclusion<br><strong>III ETL Data Gathering<br> </strong>III.1 Recorder<br> III.2 You said AI?<br> III.3 Output<br><strong>IV Diffusion Model<br> </strong><a href="https://medium.com/@mickael.boillaud/denoising-diffusion-model-from-scratch-using-pytorch-658805d293b4">Jump directly to the linked blog</a><br><strong>V. Communication between Services<br>VI. TimeLine and Time Management<br>VII Challenges, learning and looking forward<br>VIII Ethical Implication of the project<br>IX. Github Repository</strong></p><p><strong><em>Before you begin your reading, feel free to follow me for more Deep Learning blogs and </em></strong><a href="https://www.linkedin.com/in/mickael-boillaud/"><strong><em>connect with me on LinkedIn</em></strong></a><strong><em> to not miss any content.</em></strong></p><h3>Words from the Author</h3><p>In January 2024, I reached the culmination of my academic journey, signifying the conclusion of a significant chapter of my life. <strong><em>However, it doesn’t mark the end of my quest for knowledge.</em></strong></p><p>A few years ago, in 2021, after completing my physics studies at the Grenoble Alpes university, I embarked on a search for new challenges — one that could seamlessly blend my expertise in mathematics and computer science, acquired over these three years of university education, and that fuelled my passion. In 2021, I decided to all in and immerse myself completely, focusing all my efforts on becoming a Machine Learning Engineer, which I agree, it’s pretty far from physics.</p><p>The journey began with a dive into software engineering, starting with the fundamentals in the C language and progressing to the creation of full-stack webs applications using Python and JavaScript. Successfully completing the first year, I secured my inaugural 6-month internship as a Data Engineer — a transformative period where I honed both my technical skills and personal growth. The experience was so enriching that I extended my association with the company for an additional 6 months as a freelancer, simultaneously navigating through the final year of my studies in the field of AI.</p><p>From the beginning of 2023, and the onset of my final year, I delved into the vast field of AI, traversing various domains, including classical deep learning models, NLP, reinforcement learning, and so on... However, one field stood out prominently above all others: <strong>Computer Vision</strong>. Over the past nine months, I immersed myself in reading, implementation, coding, facing failures, but most importantly, <strong>experiencing continuous improvement</strong> and evolution in this field. Starting by the building a simple CNN using only Numpy, I progressed through different challenges and Computer Vision tasks, such as object detection, object segmentation, neural style transfer, implementation of advanced classification models like Vision Transformer (ViT)... As this foundational phase reached its zenith, it was time to consolidate these newfound skills and embark on a more ambitious project.</p><p>My project’s focus revolved around two major fields: 3D image segmentation and generative models. Four weeks before publishing this blog, while adhering to specific constraints (which I will elaborate on shortly), I made the decisive choice to build and train a diffusion model from scratch.</p><p>And so, this project represents the culmination of my journey, a testament to the commitment I made in 2021, and the beginning of a new phase in my continuous pursuit of knowledge and growth.</p><p>Reflecting on the past, I am genuinely pleased with the decision to make a drastic change in my path, even though physics still holds a special place in my heart, and my passion for it persists. I firmly believe that artificial intelligence, especially computer vision, will give rise to new technologies that have the potential to positively impact the lives of many.</p><p>Allow me a moment to take a bit of time and share with you the behind-the-scenes of this final project, the culmination of two years worth of hard work and dedication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>I Introduction</h3><p>Before delving into the intricate technical and managerial details, let’s introduce the project that has been crafted over these four intensive weeks. ArealQuizz is a quiz platform that allows users to engage in a unique challenge. Given a list of images, players must decide whether the displayed image is real or artificially generated by our trained model. While the spotlight often shines on the application itself, showcasing what users see, the entire project is anchored in three significant components, each of which we will explore in detail in the following section of this blog.</p><p>The first component is the web application, serving as the user interface. Following closely is the Diffusion Model, a core element responsible for generating and distinguishing between real and fake images. The third, often overlooked component, is the ETL data gathering process. Although these three services may seem unrelated, it is crucial to recognize that without their seamless integration, the project would never come to fruition. Each plays a unique and indispensable role, contributing to the success of ArealQuizz.</p><p>Firstly, let’s delve into the web application, with a primary focus on the back-end aspects. We will then proceed to explore the ETL data gathering process and finally, discuss the modeling part and the seamless communication between these components.</p><p>Before we proceed further, it’s important to address the constraints imposed on the project and provide an overview of the entire tech stack used. Let’s step back and look at the big picture before delving into the intricacies of each component.</p><h4>I.1 Contraints</h4><p>Embarking on a new project while keeping the entire environment familiar, including frameworks and other tools, may streamline the overall process but can result in fewer learning opportunities. Introducing constraints is crucial to inject excitement and creativity into the project’s development phase. In the case of this project, several constraints were deliberately introduced to foster a more challenging and educational experience:</p><ol><li><strong>No use of any deep learning framework from my scholar curriculum (Learned AI based on TensorFlow and Keras): </strong>This constraint challenged me to explore alternative approaches beyond the familiar frameworks and broaden my skills in the domain of artificial intelligence.</li><li><strong>No use of any pre-existing dataset available on the internet (Data had to be gathered independently):</strong> This constraint emphasised the need for hands-on data gathering, encouraging a more personalised and project-specific dataset.</li><li><strong>For data gathering, the only tools allowed were a web browser (No third-party applications):</strong> This constraint limited the data collection process to be conducted exclusively through a web browser, adding an additional layer of complexity and resourcefulness to the data acquisition phase.</li></ol><p>With these constraints setting the stage, let’s now dig into the technologies we employed to bring this project to life.</p><h4>I.2 Stack</h4><p>Let details the technologies used for each services</p><p>WebApp:</p><ol><li>Python: The primary programming language for developing the back-end logic and server-side functionality.</li><li>Flask: A web framework in Python used for building the back-end of the web application.</li><li>React: A JavaScript library for building user interfaces, used to develop the front-end of the web application.</li><li>Docker: Containerization tool facilitating the deployment of applications and ensuring consistency across different environments.</li><li>PostgreSQL: A powerful open-source relational database management system, serving as the database for the web application.</li><li>Redis: An in-memory data structure store, utilized for session management in the back-end.</li><li>AWS (Amazon Web Services): Cloud computing services used for various functionalities such as hosting, storage, and more.</li><li>Kubernetes: Container orchestration tool ensuring efficient deployment, scaling, and management of containerized applications.</li></ol><p>ETL:</p><ol><li>Pure Python: Utilised for the ETL process to handle data extraction and transformation.</li><li>PyTorch: A deep learning framework, used for creating classification model to enhance the data quality</li></ol><p>Modeling:</p><ol><li>PyTorch: Employed as the deep learning framework for building and training the diffusion model.</li></ol><p>Restricting the number of technologies used and maintaining consistency throughout the entire database was a brilliant decision. This approach ensured that we didn’t lose track or compromise on consistency, contributing to a streamlined and cohesive development process. By adhering to a defined set of technologies, we could focus on the project’s core objectives without unnecessary complexities or divergences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h4>II Web Application</h4><p>While the front-end is still in the early stages of development, let’s take a closer look at the back-end to understand its structure and functionalities.</p><p>The back-end has been intentionally designed to be streamlined and devoid of unnecessary embellishments. All components have been containerized under Docker, and future deployment is planned using Kubernetes (K8S). Currently, two distinct environments are available: DEV (Development) and PROD (Production). The differences between these environments lie in databases connections and the management of S3 users.</p><h4><strong>II.1 Security and Login</strong></h4><p>The back-end functionality includes user actions such as sign up, sign in, and logout. To enhance security, the application incorporates server-side sessions, ensuring a more robust and protected user experience. This deliberate design choice aims to create a back-end system that is not only efficient but also sets the foundation for future scalability and deployment.</p><p>The user session is maintained differently in the two environments. In the DEV environment, the session is stored in the associated Redis service, which is present and active within the Docker network. This provides a straightforward and efficient solution for managing user sessions during the development phase.<br>For the production environment, a more robust approach is taken. The user sessions are stored in a DynamoDB database hosted by AWS. This choice not only ensures secure session storage but also allows for full horizontal scalability if the need arises. DynamoDB’s capabilities align with the demands of a production environment, providing a scalable and reliable solution for handling user sessions at scale.</p><p>The decision not to use a third-party authentication service like Auth0 for the login component was influenced by the existence of a already developed app connected to Google and GitHub services, and so we wanted to gain knowledge on this method. However, it’s worth noting that if the application progresses into live and gains users, the login system will transition to leverage the established solution.</p><p>At present, user passwords are stored in the database and encrypted, ensuring a secure and standard practice for handling sensitive information. This approach provides a functional and secure login system for the current development stage, with the flexibility to evolve based on the application’s growth and user requirements in the future.</p><p>In conclusion, for security considerations, the application stores all its secrets and tokens within the Docker environment variables. At the current development stage, this is considered a secure approach for safeguarding sensitive information. However, it’s important to note that, similar to the login system, if the application transitions to a live production environment, alternative methods such as utilizing a credentials-less service may be explored to enhance security measures further. Adapting security practices based on the evolving needs and scale of the application is a crucial aspect of maintaining robust protection for sensitive data.</p><h4><strong>II.2 User Quizz Interaction</strong></h4><p>Users can now navigate through the back-end, modify their profiles, and start playing quizzes available on the platform. The back-end strategically serves quiz pictures one by one to the front-end, keeping the answers stored in the back-end to prevent developers from gaining an advantage by inspecting request outputs. When the user finishes answering all the questions, the back-end receives the responses, computes the score, and saves it to maintain a history log.</p><p>An alternative flow has been envisioned, involving the storage of each response after every question, thus keeping track of all responses and allowing for a “resume quiz” possibility. While this approach may seem optimal for user experience, implementing it requires additional work on the back-end and was deemed beyond the scope of the Minimum Viable Product (MVP). The decision to prioritise specific features for the MVP ensures a focused and efficient development process while setting the foundation for potential future enhancements</p><h4><strong>II.3 Quizz Creation and Storage</strong></h4><p>All the images used for quizzes and the quizzes themselves are stored in an AWS S3 database. Only administrators have the authority to create quizzes. When a request is sent, the backend will either take the number of fake images provided in the parameters or randomise it. The process then proceeds to select up to four images each from both real and fake categories to form a new folder exclusively containing quiz images. These selected quiz images are reserved and will only appear in the designated quiz.<br>Administrators have the option to disable a quiz, making it inaccessible to users without entirely removing it. In case of deletion, the quiz is removed, and all associated images are returned to their respective folders. This flexible system allows administrators to manage quizzes effectively, ensuring control over their availability and content.</p><h4>II.4 Conclusion</h4><p>The back-end is fully prepared for production and is awaiting the integration of the front-end, which is expected to be implemented soon.</p><p>While detailed database schema UML diagrams won’t be shared here, as they are relatively straightforward, you can refer to the GitHub repository if you need to explore how the objects are linked with each other. The repository should provide a comprehensive overview of the database structure and relationships between different components. If you have any specific questions or need further clarification, the repository is the place to find the detailed information on the database schemas.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>III ETL Data Gathering</h3><p>Let’s delve into one of the most interesting aspects — the ETL (Extract, Transform, Load) process. As mentioned in the constraints, the exclusive means of obtaining data was through the use of a web browser. In the quest for Earth Imagery Satellite data, what better platform to explore than Google Earth?</p><h4>III.1 Recorder</h4><p>Instead of manually taking screenshots one by one, a more powerful and efficient solution was required to enhance the speed of data gathering. Thus, it was time to leverage Python skills to create a custom recorder. The primary goal of the recorder is to streamline the data gathering process by accepting arguments such as image size, batch size, and the desired number of batches. Additional arguments can be passed as needed.</p><p>Essentially, the recorder employs a strategy for recording, utilizing a screen manager and optionally a classifier that employs AI for classification purposes. This automated approach not only accelerates the data gathering speed but also adds flexibility through customisable parameters to suit specific requirements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rqy6UiuCmOyfd05xMztL2A.png" /><figcaption>Recorder and its components</figcaption></figure><p>The whole process start with a simple script to run.</p><h4>III.2 You said AI?</h4><p>Incorporating a bit of AI into the project, the focus shifted towards avoiding the simplicity of creating deep fakes of the ocean, where filling a blue square would suffice. Instead of manually recording images of the ocean and populating the database, a classifier was trained to distinguish and classify between Earth and ocean pictures. Costly images were also considered as ocean pictures to ensure no water-related images would go undetected.</p><p>While the task seemed straightforward — a classic binary classification — we aimed to experiment with different models without the need to modify the script each time and rerun it, as it could be time-consuming. Thus, a model registry and experimenter tracking system were created. This system, driven by a simple configuration file, facilitated the training of models with variable hyperparameters and both different optimisers different architectures. All the trained models were stored in a local file registry, and a summary was generated for each experiment for a holistic view and for each model for a more detailed perspective.</p><p>The experimenter tracking file contains the last metrics for all models, along with information about the best model of the experiment, including its tag and accuracy. On the other hand, the model tracking file records all the model’s epoch metrics, such as accuracy and loss.</p><p>Throughout this process, all models were trained on the same dataset. The only variable across experiments was the modeling part. This approach embraced the Model Centric View rather than the Data Centric Approach, providing flexibility and efficiency in experimenting with different model architectures and hyperparameters.</p><p>Obviously, we already know which model will work the best among the list, noting that we only have a dataset composed of 2000 images, 1000 for each class. We let you figure out which one will work the best in the following list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A5tgC_j8Zrz3ydCIe6lHfw.png" /></figure><p>If you chose the Pretrained VGG16, you guessed right! However, in the end, despite knowing which model would perform the best, the process of creating this experiment tracker and model registry proved to be a valuable and enriching experience.</p><h4>III.3 Output</h4><p>In the end, by executing this ETL process that gathers 4000 images per hour, we collected over 30K images primarily from Spain and France. This provides us with a substantial dataset to initiate iterations over our Diffusion Model training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>IV. Diffusion Model</h3><p>As the blog starts to become lengthy, and with much more to cover, we’ve decided to shift this part into its own blog. This is because it requires detailed explanations. If you’re interested in learning how to code a Diffusion Model from scratch using PyTorch, the upcoming blog is tailored just for you.</p><p><a href="https://medium.com/@mickael.boillaud/denoising-diffusion-model-from-scratch-using-pytorch-658805d293b4">Denoising Diffusion Model from scratch using PyTorch</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>V. Communication between Services</h3><p>As an image is worth more than a thousand words, I’ll let you explore the full production architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*KQbv6002zScfYE19VY3b2Q.png" /><figcaption>Production Architecture</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>VI TimeLine and Time Management</h3><p>As the project was created and coded by one person, no project management tools such as managers or task managers were used. This also explains why certain parts, like the frontend, are missing due to time constraints. The planning was structured within broad steps. For instance, the ETL and Back-end were intended to be finished within the first two weeks, allocating the third week for a full focus on the model, and the final week dedicated to presentation, communication, and blog writing. The timeline was largely adhered to, as demonstrated in the actual progression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SFB3n_EVIFiHReq1p3R6iw.png" /><figcaption>Time line, each anchors represent a week</figcaption></figure><p>The gray zone represents the Christmas vacation period.</p><p>All the roles covered included backend engineer, DevOps, data engineer, machine learning scientist, and soon frontend engineer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>VII Challenges, learning and looking forward</h3><p>Before reaching the end of the blog, let’s reflect on the challenging aspects and the learning experiences.</p><p>The most significant challenge was constructing a Diffusion Model capable of generating realistic deep fake images. However, this turned out to be the most enjoyable part as it involved constant learning and exploration of the latest optimizations for such models. Unfortunately, due to time constraints, not all of these optimizations could be implemented, but the learning process was invaluable.</p><p>The second challenge arose in the Back-End part. Given that it had been more than a year and a half since dealing with these technologies, reacquainting ourselves and revisiting them proved initially challenging. However, after two or three days, it became increasingly familiar again. This experience served as a real refresher, allowing us to effectively navigate and work with the Back-End technologies.</p><p>Moving forward with the project, evidently, the front-end will be the primary focus for the next days/weeks. However, some issues have already been raised, such as the ETL AI dataset or the ETL efficiency in the GitHub issues. Moreover, the priority lies in training the model to be 100% capable of mapping the distribution of Earth images to the Gaussian one (refer to the blog on the Diffusion Model) and automating the process of feeding the S3 with the model without any human verification.</p><p>Thus, the deployment of the model, contingent on financial resources, would represent a significant upgrade. But for now, we are sticking to the edge service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>VIII Ethical Implication of the project</h3><p>The ethical implications of this project, particularly in the context of generating Satellite Earth Imagery, are significant. The development of a Diffusion Model for creating deep fake images of Earth from satellite data introduces ethical considerations related to the potential misuse of geographical information. Concerns arise regarding the creation of deceptive or manipulated images that could impact decision-making processes, environmental monitoring, or public perceptions. The responsible and ethical use of such technology demands strict adherence to guidelines that prevent the dissemination of misleading information or the creation of images that could compromise the integrity of Earth observation data. Moreover, the ETL process, involving AI for dataset creation, requires careful attention to avoid biases and inaccuracies, ensuring that the generated imagery is representative, respectful of cultural and privacy considerations, and adheres to ethical standards. As the project advances, a robust ethical framework must be in place to address the specific challenges associated with the generation and dissemination of Satellite Earth Imagery.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOgKeAbA-focNscTOEzJKA.png" /></figure><h3>IX GitHub Repository</h3><p><a href="https://github.com/Camaltra/this-is-not-real-aerial-imagery">PROJECT GITHUB</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=82c6b9027533" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vision Transformer from scratch using PyTorch]]></title>
            <link>https://medium.com/@mickael.boillaud/vision-transformer-from-scratch-using-pytorch-d3f7401551ef?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3f7401551ef</guid>
            <category><![CDATA[object-classification]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[vision-transformer]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Thu, 23 Nov 2023 10:35:39 GMT</pubDate>
            <atom:updated>2023-11-23T10:35:39.503Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FpLK9ol0Nt51HAifs6pFKw.png" /></figure><h3><strong>I Introduction</strong></h3><p>Before hit the real subject of Vision Transformer, it is essentiels to see where we did come from to this idea. So let’s embark into a chronological exploration of computer vision’s image classification evolution!</p><p>We trace its roots to Yann LeCun’s, the pioneering of Convolutional Neural Networks (ConvNets). These early foundations led to innovations like Inception and ResNet, pushing the limits of image recognition. After a long period of no big discover in the area, in 2020, a breakthrough occurred when the Google Research Team introduced the concept of self-attention, adapted from natural language processing, to address image classification challenges (<em>An Image worth 16x16 words: Transformers for image recognition at scale</em>). This innovation, particularly the utilization of the robust Multi-Head Attention mechanism, led to the creation of the Vision Transformer (ViT). The ViT’s departure from traditional convolutional approaches marked a pivotal moment, showcasing the transformative potential of leveraging attention mechanisms in image processing.</p><p>Vision Transformers revolutionise computer vision by replacing conventional convolutional layers with self-attention mechanisms, enabling the capture of global context and intricate relationships in visual data. This shift brings enhanced scalability and interpretability, marking a transformative leap in the field of image processing.</p><p><strong>Before we continue, let’s take a glimpse of what we going to cover and what we expect you to already know.</strong></p><p>Familiarity with Convolutional Neural Networks (CNNs) and Multi Perceptron Network (MPN) is essential, as we won’t be diving into the details here. Additionally, a nod to the buzzworthy concepts of Self-Attention and Multi-Head Attention — if these aren’t in your toolkit yet, fear not, we’ll cover the basics.<br>And so, we’ll be crafting a Vision Transformer from scratch, drawing inspiration from the groundbreaking paper. Our today mission? Applying this novel architecture to the classic Cat vs. Dog image classification problem. <strong><em>The implementation’s link awaits you in the annex, so don’t hesitate to check it out</em>.</strong> Feel free to open issues for discussion and propose improvements as we collectively explore the frontier of Vision Transformers in action.</p><p>Let the coding adventure begin!</p><h3><strong>II Brief Overview of the architecture</strong></h3><p>In the realm of complex concepts, sometimes an image speaks volumes — 16x16 words to be precise. Let’s forego lengthy explanations for a moment and dive straight into the visuals presented in the paper. By dissecting each key point illustrated in the image, we’ll unravel the intricacies of the Vision Transformer architecture, letting the picture paint a vivid understanding of this transformative model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oxSTSzssQ9dIfTc-376A-A.png" /><figcaption>Figure (1) Section 3.1</figcaption></figure><h4><strong><em>II.1 Embedding layer</em></strong></h4><p>At the very beginning, the input structure takes a departure from the conventional approach. Rather than straightforwardly feeding the network a complete image, we opt for a sequence of image patches. This sequence undergoes a transformative process where the patches are flattened, encountering the influential impact of two key learnable parameters: a class embedding and a position embedding. For those inclined towards the mathematical intricacies, this concept finds concise expression in the paper’s first equation, encapsulating the essence of this foundational step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kVcn3jLGXCA6owOlavXWvw.png" /><figcaption>Equation (1) Section 3.1</figcaption></figure><h4>II.2 MSA (Multi-head Self Attention)</h4><p>Before plunging into the Multi-Head Self Attention (MSA) layer, let’s unravel the basics of Self-Attention and Multi-Head Attention. If any aspects seem unclear, feel free to explore the provided resources or continue with the subsequent content, including code snippets, and return here for further clarification.</p><p><strong>II.2.A Self Attention<br></strong>In the realm of self-attention, each element in a sequence calculates its attention weights concerning all others, allowing the model to selectively emphasise pertinent information and unravel intricate dependencies within the data. <br>Fundamentally, three vectors—the key (K), query (Q), and value (V)—form the backbone of the self-attention process. These vectors are computed using the input sequence and three learnable parameter weight matrices embedded within the model.<br>The key vector encapsulates content details, aiding in discerning relationships and identifying crucial features in image recognition. <br>Query vectors, representing an element’s content, gauge similarity, offering insights into the significance of specific regions in images. <br>Value vectors hold information influencing other elements during attention computation, contributing to capturing and expressing the importance of visual features.</p><p>Attention computation involves pairing key and query vectors, generating scores via dot products, scaling for gradient control, and using softmax to determine attention weights. These weights guide the model to focus on specific elements based on their content and relationships, a dynamic process critical for capturing intricate patterns and dependencies in the data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*888YYWbNk787iUJ4SOjzlA.png" /><figcaption>Equation (1) Section 3.2.1 (Attention is all you need)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*NA-A1MBx1W7emN_md7h7lg.png" /><figcaption>Figure (2) Section 3.2 (Attention is all you need)</figcaption></figure><p><strong>II.2.B Multi-Head Attention<br></strong>Having delved into the intricacies of self-attention, let’s now venture into its evolved form — the multi-head attention mechanism. This sophisticated iteration introduces parallel processing through the utilization of multiple sets of key (K), query (Q), and value (V) vectors, aptly named attention heads. Each attention head operates independently, capturing a diverse array of aspects and nuances within the data. The outputs from these heads are subsequently concatenated and linearly transformed, resulting in a more nuanced and comprehensive representation of the input sequence. As a cornerstone of the Vision Transformer architecture, the multi-head attention mechanism significantly amplifies the model’s capacity to discern intricate patterns and dependencies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*KrvYfSiua78_ATWeJjUD1w.png" /><figcaption>Figure (2) Section 3.2 (Attention is all you need)</figcaption></figure><p><strong>II.2.C MSA Layer</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-GWJ5yTnIU7wd4l-L7TUVA.png" /><figcaption>Equation (2) Section 3.1</figcaption></figure><p>Now equipped with a refresher on the workings of self-attention and multi-head attention, let’s proceed to construct the Multi-Head Self Attention (MSA) layer. Effectively, this layer encapsulates a Multi-Head Attention mechanism. Additionally, a crucial step in this process involves applying Layer Normalization to the embedded output. This normalisation step enhances the stability and convergence of the model, contributing to the overall efficiency of the Vision Transformer architecture. <br>The intricacies of determining the optimal number of heads for the multi-head attention mechanism will be explored in more detail at a later stage. This nuanced decision is a crucial aspect of fine-tuning the Vision Transformer architecture for optimal performance, and we’ll delve into it as we progress further.</p><h4>II.3 MLP Layer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MAEdYHdLk2hbs7NxMIUWtg.png" /><figcaption>Equation (3) Section 3.1</figcaption></figure><p>The Multi-Layer Perceptron (MLP) serves as the output layer following the Multi-Head Self Attention (MSA) layer. It comprises a Layer Normalization, followed by two linear layers separated by a GELU activation layer. This MLP design introduces non-linearity and complexity to the model’s transformation process. To prevent overfitting and enhance robustness, a dropout layer is strategically inserted after the two linear layers. This combination of linear transformations, non-linear activation, and dropout mechanisms contributes to the Vision Transformer’s capacity to capture intricate patterns and relationships within the input data.</p><p>Notably, between the MSA and MLP layers, a residual connection of the embedding layer is present. This not only facilitates optimal gradient flow but also addresses the challenges of gradient vanishing or exploding problems. Furthermore, a residual connection is also incorporated right after the MLP layer with the output of the MSA layer, adding an extra layer of stability to the model’s training process.</p><h4><strong>II.4 Classification Output Layer</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uQjhFMxQNhKft7OCk9W2RQ.png" /><figcaption>Equation (4) Section 3.1</figcaption></figure><p>The output of the MLP layer undergoes a specific process to retain only the first logit, as illustrated in the final equation. Subsequently, the classification layers output is formed to address the classification problem. This involves a Layer Normalization followed by a linear layer comprising X perceptrons, where X represents the number of classes. This configuration tailors the model to effectively generate class-specific predictions.</p><h4>II.5 Conclusion</h4><p>The theoretical exploration of the model was a lengthy but enriching journey, we hope, for you! Now, let’s transition to the practical implementation in code to deepen your understanding of the subject. Without further ado, we invite you to open your favorite code editor. If you haven’t installed PyTorch yet, please do so, and let’s dive into the code.</p><h3>III Code implementation</h3><h4>III.1 Details about the implementation</h4><p>Certainly, we’ll take note of the different model sizes and other implementation details mentioned in the paper, including the use of various optimizers and schedulers. Elements that won’t be directly implemented here will be marked with an italic notation. Let’s proceed with the implementation and address these details as needed.</p><p>Different model sizes, such as “small,” “base,” and “large,” exist, but their selection won’t significantly impact our implementation; nevertheless, it’s worth examining these variations beforehand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H7Cg4JNL-5Av6Ch2Jv228w.png" /><figcaption>Table (1) Section 4.1</figcaption></figure><p>In the training phase, the paper outlines the use of the Adam optimizer with β1 = 0.9 and β2 = 0.999, a batch size of 4096 (we’ll adjust due to computational constraints), a dropout of probability of 0.1, <em>a high weight decay of 0.1, and the application of linear learning rate warmup over 10,000 steps, followed by a Cosine/linear learning rate decay. Gradient clipping is also employed. On the data side, the model is trained on various big datasets across different epochs for each.</em></p><h4><strong>III.2 Embedding Layer</strong></h4><p>Commencing with the embedding layer, we’ll leverage the properties of convolutional layers for creating patches. Subsequently, we’ll directly incorporate the class token onto the flattened patches and introduce the position token. The output will then undergo control by a dropout filter to regularize the embedding part.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2897ec470c90c3d6180fb732e50d4556/href">https://medium.com/media/2897ec470c90c3d6180fb732e50d4556/href</a></iframe><p>The patching process, followed by the flattening layer, generates a tensor of shape <em>[batch_size, num_of_patches, embedding_dimension]</em> (where the embedding dimension is the hidden state D). Next, we create a learnable tensor of size <em>[batch_size, 1, embedding_dimension]</em> (Class tokens) and concatenate it with the previous output. This results in a tensor of shape <em>[batch_size, num_of_patches + 1, embedding_dimension]</em>, to which we add another tensor of the same shape (Position tokens). The entire operation produces the embedding tensor representing the image.</p><h4>III.3 MSA</h4><p>For the Multi-Head Self Attention (MSA), we have the option to utilize the built-in MultiHeadAttention from the nn package from PyTorch, or alternatively, we can opt to re-code it from scratch. Here, we’ll choose the latter approach and implement the MSA from scratch. It does not contain the Layer Norm as discuss before, it will be implement in the Transformer Encoder</p><p>In the implementation of the Multi-Head Self Attention (MSA), we begin by creating the Key, Query, and Value vectors from the input <em>x</em>. These vectors are then split into separate heads to facilitate the computation of the scaled dot product and subsequent matrix multiplication with the Value vectors. This process culminates in obtaining the attention output.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae5e2b27b6652deef73267b6ed1f227e/href">https://medium.com/media/ae5e2b27b6652deef73267b6ed1f227e/href</a></iframe><h4>III.4 MLP</h4><p>Similarly to the MSA, we’ll implement the Multi-Layer Perceptron (MLP) from scratch. Given its conventional nature, we won’t delve into too many details as the structure of an MLP is a well-established and standard component in neural network architectures.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49ac2007ba112fb10eba6f57f5601c29/href">https://medium.com/media/49ac2007ba112fb10eba6f57f5601c29/href</a></iframe><h4>III.5 Bring MSA and MLP into the Transformer Encoder</h4><p>Within the transformer encoder, we’ll instantiate both the Multi-Layer Perceptron (MLP) block and the Multi-Head Self Attention (MSA) block. The encoder will then compute layer normalization and incorporate residual connections to enhance the model’s stability and gradient flow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1931e2b58e8458e0e8ab4d687e2b6bbf/href">https://medium.com/media/1931e2b58e8458e0e8ab4d687e2b6bbf/href</a></iframe><h4>III.6 Create the ViT</h4><p>Now, let’s consolidate all the building blocks we’ve developed for the Vision Transformer (ViT) into a single class. This class will take as input a configuration object, which we’ll cover later. With this configuration, we’ll have access to crucial information such as the number of transformers, the size of the embedding space, and other essential parameters for the ViT model. We can see at the end, the added classifier to perform the classification task.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b68c61d57bfeca877dd49677bda526e/href">https://medium.com/media/6b68c61d57bfeca877dd49677bda526e/href</a></iframe><h4>III.7 Class config</h4><p>A brief overview of the configuration object reveals that it will serve a dual purpose: not only does it provide essential parameters for the model architecture, but it will also store pertinent information for the training part, as described in the upcoming section. This versatility makes the configuration object a central component in managing both the model structure and the training process. We also provide and instance exemple of the ViTBASE parametters</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d26f673f7f5390bb84c914b78a35642c/href">https://medium.com/media/d26f673f7f5390bb84c914b78a35642c/href</a></iframe><h4>III.8 Wrap Up</h4><p>With this, we conclude the implementation section. The model is now prepared for training. If any doubts arise during the coding process, feel free to revisit the theoretical part to gain clarity on how the model functions.</p><h3>IV. Training Part</h3><p>Fantastic! The moment of truth has arrived — training our model to distinguish between the best animals in the world, cats, and dogs. With the Kaggle dataset at our disposal, boasting 12,500 pictures for each class, we’re about to witness the prowess of our newly implemented Vision Transformer. If you’ve explored the paper, you might already have expectations, but let’s dive in without spoilers and see how our model performs!</p><p>The configuration used for the training, due to computation restriction is not the same as for the ViT Basic, is the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3527f5d5ad3544aac6e58680a955ed5/href">https://medium.com/media/d3527f5d5ad3544aac6e58680a955ed5/href</a></iframe><p>We trained the model using a classical approach, excluding weight decay, linear learning rate warmup over 10,000 steps, Cosine/linear learning rate decay, and gradient clipping. Binary Classification Entropy was employed for training over 10 epochs. For a comprehensive view of the training process, you can refer to the GitHub repository; the link is provided in the resources.</p><p>As anticipated, the results didn’t showcase strong performance, which can be attributed to the limited amount of data and the simplicity of the Vision Transformer (ViT) architecture used, with only two transformers and a few training epochs. In scenarios like this, transfer learning becomes a more suitable approach for achieving better performance on image classification tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dN7YLp3aNpa1LTx5yjmYbQ.png" /><figcaption>Valid Accuracy</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bR_wnMud16WDBMeO_uTsKA.png" /><figcaption>Train Loss (Blue) | Valid Loss (Red)</figcaption></figure><h3>V. Conclusion</h3><p>While our primary goal of creating a model for the cat vs. dog classification task wasn’t achieved, this journey provided a valuable deep dive into the inner workings of a powerful tool in the computer vision arsenal — the Vision Transformer (ViT). Reconstructing the ViT transformer and Multi-Head Attention from scratch marked a significant achievement, laying a solid foundation for future projects. Despite the outcome, the knowledge gained sets the stage for exciting endeavors ahead. Well done, and best of luck on your future projects!</p><h3>VI Ressources</h3><p><a href="https://github.com/Camaltra/personnal_ml/tree/main/vision_transformer"><strong>PROJECT CODE GITHUB</strong></a></p><p>Paper Vision Transformer: <a href="https://arxiv.org/abs/2010.11929">https://arxiv.org/abs/2010.11929</a></p><p>Kaggle Dataset: <a href="https://www.kaggle.com/competitions/dogs-vs-cats/data?select=train.zip">https://www.kaggle.com/competitions/dogs-vs-cats/data?select=train.zip</a></p><p>Self-Attention: <a href="https://towardsdatascience.com/illustrated-self-attention-2d627e33b20a">https://towardsdatascience.com/illustrated-self-attention-2d627e33b20a</a><br>Multi Head Attention: <a href="https://towardsdatascience.com/transformers-explained-visually-part-3-multi-head-attention-deep-dive-1c1ff1024853">https://towardsdatascience.com/transformers-explained-visually-part-3-multi-head-attention-deep-dive-1c1ff1024853</a></p><p>Other approach to Vision Transformer: <a href="https://www.learnpytorch.io/08_pytorch_paper_replicating/#61-the-mlp-layers">https://www.learnpytorch.io/08_pytorch_paper_replicating/#61-the-mlp-layers</a><br><a href="https://viso.ai/deep-learning/vision-transformer-vit/#:~:text=Vision%20Transformers%20(ViT)%20is%20an,and%20a%20feed%2Dforward%20layer.">https://viso.ai/deep-learning/vision-transformer-vit/#:~:text=Vision%20Transformers%20(ViT)%20is%20an,and%20a%20feed%2Dforward%20layer.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3f7401551ef" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hippo’nterview — The platform that make you hired.]]></title>
            <link>https://medium.com/@mickael.boillaud/hipponterview-the-platform-that-make-you-hired-93e97d54adc7?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/93e97d54adc7</guid>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[solving-a-problem]]></category>
            <category><![CDATA[code-checker]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Wed, 13 Jul 2022 07:09:39 GMT</pubDate>
            <atom:updated>2022-07-13T09:51:40.409Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LgwBn7ggIrDTJWluFwAjEQ.png" /></figure><h3>Hippo’nterview — The platform that make you hired.</h3><p>The aim of the project was to created a completely free platform for Holberton School’s student to allow them to practice technical interview questions. The main exciting challenge was to create a code checker that is both fast and flawless, we’ll see more about it in a few paragraph. As I worked alone on the project, I had to work on both back-end and front-end, and on the top of it, the deployment. But thought this long journey, I really wanted to focus on the back-end, because in my thoughts, this is the most valuable part.</p><h4>But the main question is why I choose this project ?</h4><p>And you right by thinking about it ! It’s a short story about my experience as fresh software engineer. Take a coffee, find a comfortable place, and here we go.</p><p>All started at the end of my first trimester, we’ve learn and practice a lot on C language. I found myself enough confortable to challenge my new knowledge on more complex and difficult questions, and learn the basics of algorithmic — As you will continue to read, you will discover how I love solving problems — So I took my computer, make some search on the google bible, and found some really good platform to perform coding &amp; algorithmic questions that are widely used on coding interviews. So I start my journey here, a really long but funny journey. After some time on theses platforms, I needed something more on blocking questions, some help and clues to guide me to the solutions. That extra something was the paid service, and I was really frustrated to had to pay more — Already paid my school, and was running out money — to get solutions and some help. An other hand this important missing part was that there aren’t a full courses from A to Z to allow users to be fully prepared to coding interviews.<br>All started from this, and since December, I was sure about the subject of my project, create a full free platform to help Holberton School’s student as max as I can.</p><p>A last reason to argue my choice, and it was really important to me. In my opinion, being software engineer, and in the close futur, machine learning engineer, is all about help people in their lives. For me nothing is more important to give to people some tools to ease their daily, and also, learn new stuff. I needed to find a project according to this philosophy which is mine.</p><h3>A quick presentation of Hippo’nterview</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bkZHxWzXMz7T770NiWsDeg.png" /></figure><h4>Full Path courses</h4><p>The main idea, and also the main feature of Hippo’nterview is the full path courses. It contain 5 sections for 5 keys areas of programming — String and Array | Data Structures | Non-linear data structures | More data structures | Dynamic programming — Every sections are populated with fews tasks, but not random chosen tasks.<br>Every exercices are commun pattern questions. It mean that if you succeed every tasks from the String and Array section, you will be prepared to every other questions on sequence, even unknown questions, and know what to use, how start the problem, what trick to use etc…<br>Some credits here, I don’t have enough practice and knowledge to choose every single questions, so I pick them from the <a href="https://www.techinterviewhandbook.org/best-practice-questions/">author of Blind 75</a> big shout out to him.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*93V0L-4z9cid53r--2ZceA.png" /></figure><p>Every questions come with a description, a function prototype, a exemple of input and output, some edges cases about the I/O, and some informations about the location of the file (On GitHub). There are also 3 buttons in addition to the checker one, that are useful in case the student is blocked, to give him clues and also the most optimize solution.</p><h4>Randomize Section</h4><p>To be fully prepared to THE FINAL DAY, the randomize section is here. It will take a random question from the full path feature, or even other questions, and the student will have the mission to solve it. There is also a button to skip it and change the tasks in the case he feels comfortable with this type of problem.</p><h4>Admin Section</h4><p>The last feature, but not least, is the AdminPanel, a section only visible to the pedagogic assistant. In this one, we can find everything we need to help student with their bad check from tasks. The admin can have access to the output of the checker, and give more clues to students if he still blocked after those given ones.<br>To help the admin, a search bar is available to find as quick as possible student work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mw4Dh03aWmPy299rulw0Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y6i3qoF422vROE6mBjF3UQ.png" /></figure><h4>Authentification</h4><p>A crucial point, the authentification. The auth is made trough Google OAuth 2.0 API, to get more flexibility and also a password-less database.<br>Why choose Google over Github ? Only because in the futur, e-mail will be check before get the permission to sign-up and login to see if it’s a Holberton School student that try to login. Github account could be created with a personnal address e-mail.</p><h3>What’s under the hood ?</h3><p>Time to see more technical aspect of the platform. We will start with a quick look to the architecture, and then see and discuss about the choice of languages and framework.</p><h4>Project Architecture</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xb94vsNhm-pZUUXhv79k6A.png" /></figure><p>The architecture has been designed, and created in a such way that every server are stateless, so a good point to the scalability of the project. The whole project is spread in three different servers. One for the front end, which only send the visual part to the user, one for the API, which will send to the client all the needed information from the database, and the last but not least, the server checker, which will perform data manipulation before send all the code to a running-code service.<br>To scale the web-site Hippo’nterview, we simply have to add load-balancer behind every type of server, and add bunch of it.<br>A last word about the DB, we use a dedicated server just because it’s a time saving and also to not have the same database on each server, and lost money and time by synchronizing them.</p><h4>Language and Framework</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nqeEkeLhMznAizQbvQ7I2Q.png" /><figcaption>Green -&gt; Back-end | Blue -&gt; Front-end | Gray -&gt; Deployment | Red -&gt; Tiers services used</figcaption></figure><p>Here is the major technologies used for the project, and to avoid going into the details of each framework, I will just explain why I choose them trough the entire project.</p><p>For the front-end part, the choice was made on React for two main raisons. The first one was the utility of components. As I have to make a lots of tasks, in the same design, it came really quick to use a framework that allow to create template of tasks card, and just re-use it through the app. The second one is, in the futur, if the idea came or there is a need, to deploy the application on a desktop, or in a mobile app.</p><p>The choice of using Node.js for the back-end part was simply because it made sense for me to use a full JS stack, and also because we already did a back-end with python, so it’s make totally sense to try new langage and discover Node.js</p><p>So, why there is some python there ? Python was choose to be the language on which the user will have to write his answers. This decision was all about find the most user friendly language at first. And then, on a close futur, add more language as JS, or maybe lower languages like C or C++.</p><p>For the database, mongo seems to be the best choice in view of the choice of architecture. Indeed, we plan, if needed, to scale horizontally the architecture by adding servers, and NoSQL are good according to this vision. Also, at the end MERN stack are widely use for now in the real world application.</p><p>To continue on the devops part on the techno used, to deploy services really quick on each server, it was unavoidable to use Docker. We created image of the server, server-checker, and with Nginx, deploy it with only one command.</p><p>Last point of discussion, as you see, we used a tiers service app to run the code, Judge0. The choice of it was simply because 2 weeks is a very short time to develop everything from scratch. At the begin, the idea was to create our own code running service. But this idea is not forgotten, and in a few time will be on the pipeline ;)</p><p>To conclude this part, all the choice for every technos was also made in a way that I never saw them before, never learn something from through my year. The aim of it was to challenger myself, to learn as quick as I can, and get enough knowledge to build a full-stack app from scratch.</p><h3>The big challenge of this project.</h3><p>As I said earlier, there were a big and scary challenge at the beginning : the checker. This is the main feature of the project, and without it, nothing would have been possible. So it was really important to think about how implement it on the code, and also how to create it without any errors or bugs on checking.<br>So the time was on thinking, and the first problem was on how get the user code, and two choices were possible. First one was to created a online text editor, directly on the web-site, but the drawback of it is that any recruiters could access to students work. So the second choice, which is to push it into a GitHub repo, was preferred, even if it require the student to have a GitHub (every programmer got one, so it should not be a problem).<br>So now, we got the access to the user code, ready to be check.<br>The second major problems was how run test files, without damage the server — In case of malicious person push code to destroy server — So the main idea was to created a container to run every piece of code, and get the result. But as I said before, we didn’t get enough time to code it, and was preferable to use a tiers service to do it.<br>So at the end of the day, I had to make some big decision about how the project way will be, and pretty happy about it, and really happy on what I created.</p><h3>Why this project was a game changer in my software development journey</h3><p>I’m really glad to made this project! First-able only because I’ve learned a lot about technical aspect, learn a new stack from scratch on a short period of time, but also I’ve to challenge myself and have a good understanding on coding questions to, after it, create my own checker. This was a lot to do in such a short time.<br>Apart from all that I have learned from a technical point of view — That I’ve already talk with you earlier — This was a amazing personal experience to challenge myself on an entier full stack project from only an idea, to make an MVP, manage my time according to the deadlines, to code and face some difficulties, and then share the project through a presentation. I really learn to think quick and improve my discussion making.<br>If I had to revive this project, I think I’ll keep the same idea, and the same MVP and deadline, not that I made everything good, but the principal in theses kind of project is to make mistakes, ask dumps questions, and at the end of the day, be a better person!<br>I know that I want to work on these project as long as I can, and also implement some IA, make it smart. And I also know that my learning journey is not over, and it will never end, I LOVE LEARN.</p><h3>About Me</h3><p>Always looking forward to expend my knowledges in various fields, especially in data science and machine learning. Also love trying to solve any kind of problems and involve all type of skills, both professionally and personally. Do not hesitate to take risks in order to achieve my ambitions.</p><p>Find me on <a href="https://www.linkedin.com/in/mickael-boillaud/">LinkedIn</a> | <a href="https://github.com/Camaltra">GitHub</a></p><p>Find the <a href="https://github.com/Camaltra/portfolio-project">project on GitHub</a></p><p>/!\ Server was shutting down | No money to get one /!\</p><p>Find the project on internet</p><p>Find the landing page</p><p>Thank you for reading me, hope the you liked the idea and the project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93e97d54adc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Don’t be afraid of recursion]]></title>
            <link>https://medium.com/@mickael.boillaud/dont-be-afraid-of-recursion-fbedbb0988d5?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbedbb0988d5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[c-language]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Tue, 15 Feb 2022 16:02:20 GMT</pubDate>
            <atom:updated>2022-02-15T16:02:20.321Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*7ZuOMhkxl0XbmTIKDu7Kxg.jpeg" /></figure><p>Yeah, the title is correct, recursive methods are not complicated, and also are a good practice to write beautiful code. <br>You may be worrying about what I said, but trust me, after read this article, you will be able to laugh to</p><blockquote>To understand recursion, you have to understand recursion.</blockquote><p>But first at all, let’s define what recursion is, then after will se how to create a beautiful recursive and functional function on your code.</p><h3>What is recursion</h3><p>Recursion is very simple to define. In computer science, cause this concept exist in a lot of other application as language, or mathematics, recursion is define by a function that call itself.<br>You may not dreaming, it’s that simple !<br>But in pratique, this is not as simple as. Let’s build a really easy recursive function, and let’s see how this work.</p><p>Our function has as objective to print a passed char as argument. And after print it, call itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z6VuCA0bdsVrIwKD75zQvg.png" /></figure><p>So basically, this the exact translation into C language of our function. Now let’s see what happen when we run it into a main function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZSh_P8iOFqNCzzbQ19WrtA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uTxz66eudRGpnkL8TGv8VQ.png" /></figure><p>For sure, when you try recursion for the first time, the output of the function might be similar to what you said when you run your own first recursive function. But why it never stop to print ‘A’ in the terminal ?<br>To explain it, the function will call itself an infinite number of times, and never stop it. It will cause some error that will see a next part.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bvCAQMum4pYNNjsMdylRcQ.jpeg" /></figure><p>But what if I wanted to create a function that make a count of the number of second that I want, let’s say 10 to 0. With this exemple, this will print 10 on each line. So we have to create a recursive case.</p><h4>Recursive Case</h4><p>So let’s move on the recursive case, one of the 2 needed case to make run a recursive function successfully. The recursive case gonna change each time the given argument, at each function call. Remember, last paragraph, we wanted to create a counter, so what if, instead of printing and pass trough as argument a char, go pass an int, and each time the function call itself, let’s decrease the number by 1.<br>So you might guess what will be the output, and I encourage you to create the function, in the language of your choice before see the code.</p><p>So let’s create a count from 10 to 0 (We will try using the recursive case)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vNNZPsiwMK8ih28LA3an5w.png" /></figure><p>As I didn’t mentioned before, we can return the call function, and are mostly use when you have to deal with more complicated problem that will see in few lines.<br>So same as the last function except that, each time the function it call itself, the count bean decrease by 1.<br>So let’s run it with a main function and check the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D5aHTgKUahgG5n-v_-cx5g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rq7DfLnKaiXISRP0EeSfmg.png" /></figure><p>So, as you see, we got good result, but not as we expected. The goal was to created a count form 10 to 0, and we are stuck in infinite loop. So as for or while loops, we need to give to the function a statement, to tell when to stop. This case it call the base case.</p><h4>Base case</h4><p>The base case, as I said is the statement to stop the function until we finish what we wanted. So here, the base case might be when count is 0, we need to stop the count. So let’s just add it into the function, and I will didn’t show you the result because here, it’s will only be a count from 10 to 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8ZG2hnzqPu0Rot8KvFTQiw.png" /></figure><p>Congrat ! You created your first recursive function.</p><p>The base case is really important, because it will stop the function. If the function goes into a infinite loop, you gonna get an error, named stack-overflow, which tell to you computer that there aren’t enough space in the stack to continue.</p><p>But, recursion is a more complex subject, and need more explanation. What happen in our computer when we use recursion, and how it’s work.<br>There is a huge work, named the call stack, that allow recursive function to work.</p><h3>Call stack</h3><p>After seen the base of recursion, let’s go in deep an take a most difficult exemple, let’s create a function that return the result of the pow operation between two integers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*31Sg1KBJPF0kQfhM3gYwBA.png" /></figure><p>So, before see the call stack, let’s see a quick reminder of the previous part, and see where are our two different cases.<br>The first one, the recursive case, is whenever the function call itself, the power of x decrease by one if it is positive, or increase by one if it is negative. So every time the function call itself, it make an different and useful work for the “parent” function.<br>The second one, the base case, is whenever the power is equal to 0. So if we take 3 power 0, it’s equal to 1, so the function return 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z0D43lcKdHX0SLEmm5R6kQ.png" /></figure><p>So we got all our elements to get a perfect and working recursive function ! Great! As exemple of this fucntion, given 2, 4 as argument, the final return will be 16.</p><p>But how this is working, is not that simple as the count function that we made earlier! And it’s time to put in light the call stack. This concept will clear your mind, and make you a pro of recursion.</p><p>The stack, if you’re not familiar with this data struct, is like a waiting line in real life, but in reverse. It work in the backstage of your computer, and you not got any information of what it doing. So it’s important to understand how it work.<br>Each time you input an element into the stack, as a new guy come into a waiting line, it will be the last on the waiting line, and if we follow this exemple, the first guy that entered into the waiting line, will be the first to the checkout. So this concept is the concept of queue in computer science. First come, first served. But the stack work a little bit different as the queue, in a reversed order. Last come, first served. <br>So each time you add a function to be process into the stack, this function will be process in the first time, then the next function etc… It’ may be complicated to understand the concept by word, but let’s take exemple with our function.<br>x gonna be 2 and y gonna be 4.</p><p>Here is two different ways to see the stack, each of these two is the same, but from other angle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WOT5aMOAFgWrgr7M9o_qmg.jpeg" /></figure><p>As we see here, we continue to add sub-function, until get to the base case, et then, when reach this case, climb up, getting each result from the last process stack, et return it to the next stack, next sub-function. Then, when we get into our main function, it’s done, we only need to return the result.<br>The other angle is to take the stack as a heap, and every time the function call itself, put on the work to do in the top of the heap. Then when the function reach the base case, start to process all the heap until get to our parent funtion.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jrUHti81BDMWRcH5NLmyOg.jpeg" /></figure><p>We first take the blue road, and then take the red one (same on two diagram).</p><p>Now you can master the concept of recusion. If you need some other exemple, here is a <a href="https://www.youtube.com/watch?v=KEEKn7Me-ms">video</a> from Gayle Laakmann Mcdowell, and you can also check into the wonderful <a href="https://www.amazon.fr/Grokking-Algorithms-Aditya-Y-Bhargava/dp/1617292230/ref=sr_1_1?crid=2RX3ZWKAO4U8E&amp;keywords=grokking+algorithms&amp;qid=1644940211&amp;sprefix=grokking+al%2Caps%2C987&amp;sr=8-1">grokking algorithms</a> book from Aditya Y.Bahrgav. These two ressources can complete all your knowledge about recursion.</p><p>Then, a question can be raised, why using recursion when we can do it only by using a for or while loops, like go through an iteration method.</p><h3>Recursion vs Iteration</h3><p>To finish this article, let’s just see why and when use recursive function.</p><p>Recursion function is useful to create a simplest way to solve problems, or visualize it, like <a href="https://en.wikipedia.org/wiki/Divide-and-conquer_algorithm">divide and conquer</a> algorithms, that iteration cannot do. So the best practice in these case is to create recursive functions.<br>Here is some sorting algorithms that use it.<br><a href="https://github.com/Camaltra/sorting_algorithms/blob/master/3-quick_sort.c">Quick Sort Lomuto partition</a><br><a href="https://github.com/Camaltra/sorting_algorithms/blob/master/107-quick_sort_hoare_fast.c">Quick Sort Hoare partition</a><br><a href="https://github.com/Camaltra/sorting_algorithms/blob/master/106-bitonic_sort.c">Bitonic Sort</a></p><p>To get a talk about performance, these two way are both the same on a computer, so it never change a lot about it if you choose the recursion on iterations.</p><h3>Conclusion</h3><p>Congrats! You become a pro of recursion, now you just need to put your hand on, code some recursive functions like factorial operation and other to be the big boss of it.<br>Have a good day tho, see you for the next article ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbedbb0988d5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Class in Python : All you need to know]]></title>
            <link>https://medium.com/@mickael.boillaud/class-in-python-all-you-need-to-know-524fadaf172?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/524fadaf172</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[class]]></category>
            <category><![CDATA[methods]]></category>
            <category><![CDATA[attributes]]></category>
            <category><![CDATA[pythonic]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Thu, 27 Jan 2022 16:58:42 GMT</pubDate>
            <atom:updated>2022-01-27T17:11:39.503Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*-NwmHUMbmxxYzry8qwYg9A.jpeg" /></figure><h3>Class in Python : All you need to know</h3><p>Today, we gonna introduce a fundamental principe in python: class.<br>Class is everywhere, and almost all in python is a class, but first, we might take a look of what python is.</p><h3>Python</h3><p>Python is a high level language and an OOP language, an oriented object programming language. It been created by Guido van Rossum in 1991. Now it’s very commun to see this language in back-end, data analyse, or machine learning because it got a lot math functionality among other things.</p><p>Python is also an interpreted language, mean that he don’t need to be compilated before using it and a cross OS language (available in Macos, Window or Linux).</p><h3>What is a class in python ?</h3><p>A class in python is an object that contain some methods, attributes and are the parent, or the constructor of instances. <br>If you are familiar with python, str is a class and islower is one of methods in this class.</p><p>So as we see, there is some particular thing about class, some word to know, so let’s see what it is.</p><h4>Instances</h4><p>Instances are a sort of copy of class, but some particular values, that need to be passed (or not) when an new class instance is created. Let’s take a quick exemple, and create a square class. That allow us to create a lots of square in the futur, and develop more about the rest. But first let’s see how create a class and an instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*IORfWyZ-h5cY4f5CWXIOxw.png" /></figure><p>So as you see, squareA and squareB are created from the class, and they are instances of the class.</p><h4>Attributes</h4><p>Attributes are some variables, depend on where it’s define, can be called by the instance or the class, or the two at the same time. So, there are the class and the instance attributes. You can start seeing how we can access them, and where create it, only by their name.</p><p>Class attribute are create directly in the class, outside of the constructor (the __init__ method) and can be access by the class directly, but also by the instance. <br>You can update a class attribute, and if you did, in all the instance it gonna be modified, as we see in the exemple.</p><p>Instance attributes are created mostly in the constructor method, but can also be created, if the class allow it, outside of the class. These attribute are specific and unique to the instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Uf12B6J6VCGiplUvonPkhg.png" /></figure><p>Before quit our definition of attributes, one thing to note is there is, whether in class or instance, some kind of attribute.</p><h4>Attributs types</h4><p>The public attribute : It can be modify by everybody, everywhere.</p><p>The protected attribute : Can be used outside of the class under some conditions, and start with a “_”</p><p>The private attribute: Can only been access inside of the class, and start with a “__”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W2AbF50ZGbQms25FMRfPqg.png" /></figure><p>Now, you know everything about attributes, and if you don’t, we gonna make a quick reminder before the end by confront class and instances attributes by dress a table of differences, advantages and drawbacks. But before, time to understand what methods are.</p><h4>Methods</h4><p>Method are similar of function, but they are inside a class. As attributes, method has particular thing to know.</p><p>First, and to understand the __init__ mentioned before, method can be magic. No kidding, some methods are called magic methods, and are particular. Like __init__ magic method, all of them start by “__” and finish by “__”. They got a lot a specificities, as init that been automatically run after created instance (as the constructor of the instance). As they are a lot, we cannot go in details about each of these, but <a href="https://www.tutorialsteacher.com/python/magic-methods-in-python">here</a> is a link that mention all of these.</p><p>But still, there are some other method, as setter, or getter method to access to privates attributes, that are specify to the program by a decorator.</p><p>And to finish, some method are called classmethod, or staticmethod, also specify by a decorator before the function definition. all of these can be acces directly by the class, and can modify class attributes. They get some particular thing too, and to learn more, <a href="https://www.geeksforgeeks.org/class-method-vs-static-method-python/">here</a> is an other link.</p><h3>Let’s make a recap</h3><p>As I told you, it’s time to compare class and instance attribute, and see their advantages and their drawbacks.</p><h4>Comparative</h4><p>First, class attributes and instances attributes are not define in the same place. The first one is in the class directly, outside of any method, the second one is mostly inside the constructor method. <br>So class attributes can be reach in all classed, but instante attributes can be only reach by the instances, and are unique.</p><h4>Advantage and disadvantages of class attributes</h4><p>The most advantage of the the class attributes is that can be use as a counter, or an array of which instance are created and still present. But the bad side of it, is that every instances you created from this class gonna have the same variable, so you can’t do different things.</p><h4>What about instance attributes</h4><p>Easy to get and set as we see with the constructor, and also the setter and getter method, and after del a instance, these value are delete too, so no leack of memory possible. The only drawback is we can get access of these value between instances.</p><h3>To conclude</h3><p>As we saw, class are very important in python, there are the fundamental part of this language. <br>A last word, to see all methods in a class, you can use the __dict__ method (yeah, a method to see other methods, methodception), and that will display all the disponible method.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=524fadaf172" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A walk into the shell processus]]></title>
            <link>https://medium.com/@mickael.boillaud/a-walk-into-the-shell-processus-2880a6a92faa?source=rss-37ace7bb19e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2880a6a92faa</guid>
            <category><![CDATA[shell]]></category>
            <category><![CDATA[command-line]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mickael Boillaud]]></dc:creator>
            <pubDate>Thu, 09 Dec 2021 20:29:28 GMT</pubDate>
            <atom:updated>2021-12-09T20:58:10.005Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*B-FnvcI3Za0ykiQ60ebqFw.jpeg" /></figure><h3>A walk into the command line interpreter</h3><p>Hi mates, how’s going ?<br>Will you never asked how work the shell command line interpreter when you work on ? How did it find the right command, and execute it. <br>For us, shell is a very commun tool, that we use everyday, kind a friend, but for the most of us, we don’t really know what going on when we write a command, and press enter.</p><h3>What a shell command line interpreter ?</h3><p>Yeah, that a good way to start, let’s define it. <br>Shell is a computer program which exposes an operationg services to a human user or other programs, that can work on every computer.<br>The command line interpreter is the terminal, that always here, and wait for instruction. It’s work with a certain language to execute commands, navigate through files, etc…</p><h3>How work a command line interpreter ?</h3><p>There are few importants steps to make sure that your inputed command will be execute. There are always the same, in the same order. <br>To be more clear about this subject, let’s take an exemple : ls -l *.c<br>You should know the result, but that not the most important.</p><h3>Get and formatting user input</h3><h4>Waiting for a user input</h4><p>After prompt a test message (usually the value of the $PWD followed by a $) the command line will wait you, wait until you press the enter button. You can write anything that you want, like only space, some weird sequence of letters, real command, even the description of the Disneyland park, he only wait that you press enter.<br>In fact, there is an other button, the CTRL-D that stop the current waiting, and go on an other waiting.<br>Then, when it’s done, when the special button is press, the command line will stock it in a variable, and this will be all for the first step.</p><p>If you are a C fan, to recreate this kind of stuff, you can easily use the getline command, and if your are a fanatic fan of C, can also re-code it, by using only sys-call. A clue to this, the useful sys-call to use a read, that will read the user input</p><p>To resume, in your point of view, you only open the terminal, then write “ls -l *.c” and press enter</p><h4>Check the inputed</h4><p>The process will check the user input, check if the command are not fulfilled of spaces, if there are not comments, if there are not special char like ‘\t’ or ‘\r’… There will be a lot of check to make sure that it only get what we want to execute.<br>It will also split into token word separate by space, or special char, to only have the command, flags or operator.</p><p>Let take a look at our exemple. So we enter “ls -l *.c”, the output after this process will be : “ls” “-l” “*.c”.</p><h3>Search the command and execute it</h3><p>After get only what it wanted, the program gonna take the first token, the one that represent the command, and will check if the command exist. The command can exist in 4 type : The original command of bash, often in the /bin/… directory, can be a built-in command, can also be an alias, or can be an executable in the current directory.<br>But you guess, there are a particular check order.</p><h4>First one : Aliases</h4><p>An <em>alias</em> is a short name that the shell translates into another longer name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command. <br>So it will check into this list of “shortened” executable if the command inputed are not one.</p><p>If the command is part of the list, then, it will execute the command as what the alias refers to. <br>But if you have a good healthy computer, ls is not a alias, so let’s move into the next step</p><h4>Second one : Built-in</h4><p>Built-in are some add-on that the creator of the command line interpreter can add as his wish. It’s also a list of command, and the computer will check if the inputed command is not here.</p><p>Same as aliases, if the command is in the list, it will be execute as. <br>But ls is not a built-in, so keep continue on the way.</p><h4>Third one : Take a look to the PATH</h4><p>After all these check, the computer will check if the command do not existe in the PATH. It will take the $PATH value, and fit the command at the end of all the different PATH. If the command fit, the interpreter will create a child process that will get the same look as the current process(like the same env …), execute the command into it, and wait the command execution is finish before move on.<br>The creation of the child process will be made by the fork sys-call, and the waiting will be made by the wait sys-call.<br>The execution, the most important, is made by the execve sys-call.</p><p>Obviously, the ls command is in the PATH, as /bin/ls, or /usr/bin/ls, the command line interpreter will run the ls command, with all the flag etc..</p><p>But, there is a last step, that we will cover too, but our exemple stop here, and see want the command line interpreter will do after this short explanation.</p><h4>Last one : Check if it’s not a executable in the curent directory</h4><p>As exemple, if you compile a C program, and you want to execute it, in the command line, you will type : ./a.out (I haven’t a lot of imagination to find name). The system will search if the ./a.out file exist in all the last step, but obviously, it’s in the current directory that it’ll find it, and execute it.</p><p>A last thing to mention, the * char will refer to all possible char, like *.c, will be all string finished by .c. And the command line interpreter will understand it during the execution of the command, don’t worry about it.</p><h3>Print the command and wait (again)</h3><p>At the end, if the command was found in one of these 4 steps, it will be execute and print the result. Also, if not find, there will be an output, but an error output, in the stderr.<br>The output of the command, whether it be in stdout or stderr will be print by the write sys-call.</p><p>After all that work, the command line will prompt the same, and wating another user input.<br>The loop will continue, continue, and continue.</p><p>For our exemple the result of the search should be something like that :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PkJ7ZSX77BcBvtHVTQIRAQ.png" /></figure><p>If you are curious, our final trimester project was to recreate a simple_shell, and you can click <a href="https://github.com/Camaltra/simple_shell">here</a> to get the code.</p><p>Thank you for your time, and hope you learned something :)</p><p>Author : <br><a href="https://www.linkedin.com/in/axel-valentin-5616bb221/">Axel Valentin</a><br><a href="https://www.linkedin.com/in/jasonleuchart/">Jason Leuchart</a><br><a href="https://www.linkedin.com/in/mickael-boillaud/">Mickael Boillaud</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2880a6a92faa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>