<?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 Azhan Mohammed on Medium]]></title>
        <description><![CDATA[Stories by Azhan Mohammed on Medium]]></description>
        <link>https://medium.com/@azhanmohammed?source=rss-f50976e3343d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*c8fHRolQoEAwq3wCPNB21Q.jpeg</url>
            <title>Stories by Azhan Mohammed on Medium</title>
            <link>https://medium.com/@azhanmohammed?source=rss-f50976e3343d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 14:50:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@azhanmohammed/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[RepGhost: A Hardware-Efficient Ghost Module via Re-parameterization]]></title>
            <link>https://azhanmohammed.medium.com/repghost-a-hardware-efficient-ghost-module-via-re-parameterization-904ba7701de3?source=rss-f50976e3343d------2</link>
            <guid isPermaLink="false">https://medium.com/p/904ba7701de3</guid>
            <category><![CDATA[convolutional-network]]></category>
            <category><![CDATA[reparameterization-trick]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Azhan Mohammed]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 22:06:59 GMT</pubDate>
            <atom:updated>2024-09-24T22:06:59.251Z</atom:updated>
            <content:encoded><![CDATA[<p>Convolutional Neural Networks (CNNs) are very well known for their ability to reuse features across layers. The lower layers of a CNN capture general features, while the higher layers learn more task-specific and complex features. The feature reuse capability of a CNN allows the network to leverage the previously learnt features and extract deeper information, without adding any additional FLOPs. So that begs the question, how exactly to do feature reuse? While ResNet used skip connection with addition to reuse features, DenseNet models relied on concatenation of features channel wise to get better features. So the question now becomes, addition vs concatenation, which is better? We will soon be learning more about this.</p><p>Initial CNNs like VGG were just vertically stacked convolutional layers that gave good performance on image tasks. As we developed further, we evolved into multi-branch networks like Inception Net, that passed the same feature through different convolutional blocks, and then concatenated the outputs together to form a larger feature space. While using multiple branches gave better performance, it came with increased cost both in terms of FLOPs and time. This is where re-parametrization stepped in, which allowed using a complex multi-branch network training, followed by feature fusion that transformed the network into a vertical stack of convolutional layers just like VGGs. This was first introduced in <a href="https://arxiv.org/pdf/2101.03697">RepVGG</a> (You can find an in detail blog about RepVGG <a href="https://medium.com/codex/making-vgg-style-convnets-great-again-with-repvgg-df8ed7205318">here</a>).</p><p>So, now the question becomes, how exactly does <a href="https://arxiv.org/pdf/2211.06088">RepGhost</a> comes into all of this?</p><p><strong>Background to the problem</strong></p><ul><li>For lightweight CNNs current methods employ feature reuse via concatenation</li><li>While concatenation seems to be parameter and FLOPs free technique, is it really an efficient manner to employ feature reuse in a CNN?</li><li>Most CNNs still use multiple branches to extract deeper and better features, while this gives a good performance, it makes the network computationally costlier both in terms of FLOPs and inference latency.</li></ul><p><strong>Understanding why exactly concatenation is not the best approach?</strong></p><p>Ablation study done by the authors in the <a href="https://arxiv.org/pdf/2211.06088">RepGhost paper</a> has shown that the concatenation operation takes 2x the time as taken by the addition operator. What is even more challenging is the fact that concatenation is memory costly as well, as it requires creating copies of the layer output. While all of this sounds easy on a good GPU driven machine, lightweight CNNs deployed on a mobile device aren’t really a fan of this approach.</p><p>Given below is a comparison of the concatenation operator with other operators.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MiwuYzTRBunD3UQWn35ZtQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l0L1aAduK07LC07fFZo0oQ.png" /><figcaption>Comparing concatenation with other operators</figcaption></figure><p>The blog <a href="https://medium.com/codex/making-vgg-style-convnets-great-again-with-repvgg-df8ed7205318">here</a> discusses in great detail why using re-parameterization is great, and how it helps both during training and inference time.</p><h3>RepGhost</h3><p>RepGhost is an evolution over the existing <a href="https://arxiv.org/pdf/1911.11907">GhostNet</a> model that:</p><ul><li>Uses a more efficient way to generate and fuse diverse feature maps via re-parametrization</li><li>Removes the inefficient concatenation operation saving inference time</li></ul><p>So this begs the questions, how exactly does RepGhost get the upper hand over GhostNet. This is done by simply removing the costlier and inefficient blocks from GhostNet, and replacing them with simpler yet effective blocks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Y9F9h7_BN--2Uzos" /><figcaption>Attaining the RepGhost module from GhostNet module</figcaption></figure><p>As shown in the figure above, we first shift the ReLU block downwards, to make sure the layers above could be activation free to support re-parametrization. Then the concatenation layer is replaced by the addition layer, that provides a faster feature reuse technique. To top it all off, the skip connection branch is replaced by a non-linear batch normalization branch.</p><p>But are these the only differences between GhostNet and RepGhost? Well no, certain changes have been made to the bottleneck layer as well, that improves the RepGhost model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EP24V6YlQJoLCx7XF0M5rA.png" /><figcaption>RepGhost vs GhostNet bottleneck</figcaption></figure><p>As compared to GhostNet bottleneck, RepGhost bottleneck has two major changes:</p><ul><li>Thinner middle channels (Cmid in GhostNet vs Cmid/2 in RepGhost)</li><li>Thicker channels for second depthwise convolutional layer (Cmid/2 to Cout in second 1x1 conv layer)</li></ul><p>Expanding from lower number of channels to higher number of channels using depth wise convolution provides better feature extraction in bottle neck layer. Which leads to better feature fusion.</p><p><strong>Results</strong></p><p>The author have used the RepGhost model as backbone for various tasks. The results below show the model performance on image classification and object detection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZHJTBrlHuEBQZVyesUMy7g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aXeifoN9Xkw-96TCBEWPDw.png" /></figure><p><strong>Discussion</strong></p><p>What makes RepGhost really efficient is:</p><ul><li><strong>Lower computation cost:</strong></li></ul><blockquote>Replacing concatenation with addition allows for faster computation making it suitable for low resource devices</blockquote><ul><li><strong>Improved feature fusion and extraction:</strong></li></ul><blockquote>Replacing concatenation with addition leads to implicit feature fusion</blockquote><blockquote>Expanding from lower number of channels to higher number of channels using depth wise convolution provides better feature extraction in bottle neck layer</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=904ba7701de3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LLMs are the best Zero Shot Classifiers (if used correctly)]]></title>
            <link>https://azhanmohammed.medium.com/llms-are-the-best-zero-shot-classifiers-if-used-correctly-c240472f9cb3?source=rss-f50976e3343d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c240472f9cb3</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[ai-hallucination]]></category>
            <category><![CDATA[classification-models]]></category>
            <category><![CDATA[zero-shot-learning]]></category>
            <category><![CDATA[classification-algorithms]]></category>
            <dc:creator><![CDATA[Azhan Mohammed]]></dc:creator>
            <pubDate>Sat, 03 Feb 2024 20:17:02 GMT</pubDate>
            <atom:updated>2024-02-03T20:17:02.673Z</atom:updated>
            <content:encoded><![CDATA[<h3>A bit of context</h3><p>So last week I was working on a problem I could not find much resources for. No existing embedding model for me to plug and play with my data, neither an existing dataset I could use to create my own model. And while I could still create a synthetic dataset using LLMs, the problem remained how to account for every corner case that exists.</p><p>After spending most of the week racking my brains, and still getting nowhere close to solving the problem I was almost out of ideas. But yeah, let us take a look at the problem I was trying to solve, technically not the exact problem, but yeah somewhat similar.</p><h3>The problem</h3><p>I had a pool of 400–500 categories that needed to be assigned to incoming queries. Since the categories were constantly expanding, fine-tuning a classifier wasn’t an option. I experimented with a LLM, hoping it could automatically assign the appropriate labels. However, due to hallucination, I could never get the appropriate label, or missed out on some significant labels. Thought RAG would help with grounding the tags, but even with that the hallucination problem was not resolved, I still got made up categories.</p><p>And after trying hundreds of different prompts, even resorting to extreme measures like threatening the extinction of the human race and warning the model that incorrect outputs would cost me my job, I still couldn’t get reliable results. With each failed attempt, I felt like I was losing my sanity and my will to live altogether. It seemed absurd that I couldn’t even make a LLM do something seemingly simple, perhaps it was time to abandon the idea altogether.</p><p>But finally it hit me, I could use the hallucination to my advantage. With just a couple of changes to the pipeline I could create a simple classifier for myself.</p><h3>The solution that worked for me</h3><p>So, as I mentioned earlier, the problem at hand was to classify an incoming query into multiple classes using a pre-defined set of classes I had already. The only issue was, even with RAG, the model would create classes of its own. So, rather than asking the model to pick tags from the list I had defined, I asked it to create it’s own classes (following the constraints I needed) and add a description for the class in a python dictionary. The goal was now simple, have a key value pair for my classes from the bucket, where they key was the class name, and the value was a description of the class. And for each query ask the LLM to create a bucket of classes it seems fit, and a description for these classes.</p><p>All that remained was to use a simple language encoder model to create embeddings for these two different buckets, perform cosine similarity match on the tags and get the desired classes out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dBilqyJ-xZgCvosTWWWbtQ.png" /><figcaption>The simple pipeline</figcaption></figure><p>Will be adding a simple demonstration with a dummy problem to demonstrate the end to end pipeline soon.</p><h3>Conclusion</h3><p>In summary, my journey navigating the complexities of language models led me to a pivotal realisation: instead of battling against the model’s tendencies, we could harness them to our advantage. By reframing the problem and adapting our approach, we can easily discover a novel solution that aligns the model’s outputs with our requirements.</p><p>This experience underscored the importance of persistence and creativity in problem-solving, showcasing the transformative power of thinking outside the box. Through perseverance and a willingness to explore unconventional paths, I transformed frustration into innovation, ultimately finding a practical and effective solution. In a field as dynamic as artificial intelligence, where challenges abound and boundaries are continually pushed, it’s our ability to adapt and innovate that paves the way for progress and discovery. In embracing the unknown and embracing new possibilities, we unlock the true potential of technology to shape the world around us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*xrExCKckf4m3Y_E0LoxkQg.jpeg" /><figcaption>Not my original image, but yeah you get the point</figcaption></figure><p>(Yeah I used an LLM to write the conclusion for me, cause I know the exact prompt for that, did not need a hacky way to make that work)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c240472f9cb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MLOps using neptune.ai]]></title>
            <link>https://azhanmohammed.medium.com/mlops-using-neptune-ai-41ae8b995eda?source=rss-f50976e3343d------2</link>
            <guid isPermaLink="false">https://medium.com/p/41ae8b995eda</guid>
            <category><![CDATA[mlops-platform]]></category>
            <category><![CDATA[image-classification]]></category>
            <category><![CDATA[mlops]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Azhan Mohammed]]></dc:creator>
            <pubDate>Thu, 03 Aug 2023 06:12:53 GMT</pubDate>
            <atom:updated>2023-08-03T06:12:53.326Z</atom:updated>
            <content:encoded><![CDATA[<p>While training a CNN model has been made relatively easier, keeping a record of your model’s training statistics and deploying it still remains a challenge. If you are struggling with it as well, do not worry, this article has got you covered. We will be discussing in depth about how to track your model’s different training runs, and deploying it as a streamlit application. The best part? You can do it all for free.</p><p>In this article we will be developing an MLOps pipeline for a simple image classification network, majorly focussing on the following:</p><ul><li>Setting up the development environment</li><li>Building model and tracking experiments</li><li>Visualising CNN outputs using GradCam</li><li>Uploading model to <a href="http://neptune.ai/">neptune.ai</a></li><li>Deploying the model</li></ul><h3>Aim of the project</h3><p>The aim of the project is to create a simple application image classifier that is able to classify between cats and dogs. We will be visualising the CNN outputs using GradCam to understand why exactly an image gets classified into cats and dogs. Since we also plan to deploy the model as an application, we need to track the model’s performance on test data, to determine which model should be used for deployment.</p><p>The whole project will be executed in the following manner:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nOc0XdCuhivz_8vyxlKm1Q.png" /><figcaption>Source: Author</figcaption></figure><p>The green blocks represent the main task in building the MLOps pipeline, while the yellow block represents an auxiliary task. The complete code for the project can be found <a href="https://github.com/sheikhazhanmohammed/mlops-neptune">here</a>.</p><h3>Setting up the development environment:</h3><p>One of the major challenge that exists even before you can start training your model is to set up your environment. The initial set up ensures that you can then ship the application to anyone and they can run it without facing any issues, cause honestly you cannot really ship your machine to everyone who wants to run the model, can you?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/825/1*SkIi9TZZEEpeClq1_usXlA.png" /><figcaption>Source: Author</figcaption></figure><p>If you wish to use a Kaggle notebook, or a Google Colab notebook for training you can directly jump to the next section.</p><h3>Downloading and installing Anaconda</h3><p>Anaconda is like a ready-to-go toolbox filled with everything you need for data science, scientific computing, and machine learning tasks. It’s a lifesaver for beginners because it comes with all these pre-installed data science packages, libraries, and tools. Say goodbye to the headache of installing them one by one!</p><p>To install Anaconda, just visit the <a href="https://www.anaconda.com/download">link</a>. You will get the following screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t2CfxxoOjAts01fWNStV4Q.png" /><figcaption>Source: <a href="https://www.anaconda.com/download">https://www.anaconda.com/download</a></figcaption></figure><p>Go ahead and hit that shiny download button, and voilà, Anaconda will be on its way to your computer! It’s super easy to install and set up — just follow the installer, and it’ll guide you through most of the steps.</p><p>Once you have setup anaconda we can start with installing all our dependencies which we will be using for our project.</p><h3>Installing dependencies</h3><p>For this project we will be creating a separate environment altogether, this practice ensures that we can easily copy the dependencies for a project, and we do not hinder with the specific library version on our base environment.</p><p>We will be calling our virtual environment pytorchvenv, you can choose to name it as per your choice, but keep it something related to the project as it makes it easier to track your virtual environments. To create the virtual environment, just run the following commands on your terminal.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc7f08b25ed58ae3159a2609490f292f/href">https://medium.com/media/bc7f08b25ed58ae3159a2609490f292f/href</a></iframe><p>Once our virtual environment is set up, we can easily activate is using conda activate pytorchvenv</p><p>Now that our environment is all set up, we can start with getting our training data.</p><h3>Gathering training and testing data</h3><p>Data has always been a pain point for training a deep learning model. Well fear not, we have got that covered too. With a simple <a href="https://chrome.google.com/webstore/detail/image-downloader/cnpniohnfphhjihaiiggeabnkjhpaldj">chrome plugin</a>, you can download all the images on a web page in a single click. The <a href="https://vimeo.com/756742272">video</a> shows a simple demonstration of how to use the tool.</p><p>All we need to do now is to download the dogs and cats images. Feeling lazy and do not want to do this step yourself? Do not worry, we have got you covered. You can directly download the data used in the project from <a href="https://app.neptune.ai/r3yna/dogsvscats/models?shortId=DVC-MOD-1&amp;type=modelVersion&amp;path=validation/&amp;attribute=dataset">here</a>. This is another reason why <a href="http://neptune.ai/">neptune.ai</a> is one of the best choices, you can keep a track of your data right in the model registry.</p><p>In case you decided to download the images directly, you can directly place the data directory in your project’s root folder and skip this step. For those of you who decided to download the images yourself, the images now need to be placed in different directories, and be renamed. We will be using python to rename our images and add a specific prefix to our images.</p><p>Just change the arguments PATH_TO_FOLDER_WITH_CAT_IMAGES and PATH_TO_FOLDER_WITH_DOG_IMAGES and run the file using python rename.py</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e9b52b28463fcd7d7ec2385fe72f98f/href">https://medium.com/media/7e9b52b28463fcd7d7ec2385fe72f98f/href</a></iframe><p>Once our data is renamed we can place them in the data directory in our project’s root folder. After this step the project’s root directory should look something like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ecqBG7GwjmCAYXLnTD9RQ.png" /><figcaption>Source: Author</figcaption></figure><p>Now that everything is set up, all that remains is training the network, and serving it using a streamlit application.</p><h3>Building deep learning model with PyTorch</h3><p>Create another file train.py in the root folder of the project, you can also create a Jupyter notebook if you want.</p><p>To start with the project, we first need to import libraries, and set up our <a href="http://neptune.ai/">neptune.ai</a> project. The question now arises why use <a href="http://neptune.ai/">neptune.ai</a> for tracking our models. While there are numerous reasons that can be listed, we have decided to list a few of them which demonstrate why <a href="http://neptune.ai/">neptune.ai</a> is the best choice:</p><ul><li>Data versioning: You can log experiment and model metadata like evaluation and training metrics, model hyperparameters, learning curves, console logs, hardware logs and many more artefacts.</li><li>Experiment tracking: Suppose you run multiple experiments for the same projects by varying hyperparameters, you can track them all together, compare them using charts and share the results with your team real time.</li><li>Model registry: This feature allows you to track the dataset, code, parameters and every other important parameter that was used to train a model. You can also use it to store model binaries and use them to deploy the application.</li></ul><h3>Creating a <a href="http://neptune.ai/">neptune.ai</a> project</h3><p>Now that we know why to use <a href="http://neptune.ai/">neptune.ai</a>, let us get started with how to use <a href="http://neptune.ai/">neptune.ai</a>. This <a href="https://docs.neptune.ai/setup/installation/">complete guide</a> provides a very detailed introduction on how to set up your Neptune projects with Python.</p><p>We can <a href="https://docs.neptune.ai/setup/creating_project/">create a project</a> and name it “CatsVsDogs” for this article. Now that our <a href="http://neptune.ai/">neptune.ai</a> project is created, we can start with the actual training code. We go over the training pipeline in small steps.</p><h3>Importing libraries and setting up the <a href="http://neptune.ai/">neptune.ai</a> project</h3><p>The first step is to import libraries that we will be needing for our training and logging. We also need to initiate an instance of the project we just created to track our experiment. The code below imports the needed libraries, sets up the training device (MPS for M1/M2 Mac and CUDA for NVIDIA GPUs), and create a class object run for our <a href="http://neptune.ai/">neptune.ai</a> project.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17faf87769c639796379f32ae105baa6/href">https://medium.com/media/17faf87769c639796379f32ae105baa6/href</a></iframe><h3>Creating the dataset and the dataloader</h3><p>To train the model we next need to load our images. PyTorch has it’s own dataset and dataloader class objects that allow us to load data in batches as per own requirement. We can apply any set of transformations or augmentations we want as well.</p><p>To create our dataset class we first need a list of the images and their respective labels. For this we the os.listdir() function to list all the files inside the folder. We then create a pandas dataframe using the list of files and their respective label. Since the model cannot understand text labels, we also need to convert them into integers. All of this can be done using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e8f6942dbb2ec373e0c557e3620838e/href">https://medium.com/media/1e8f6942dbb2ec373e0c557e3620838e/href</a></iframe><p>You can then check the dataframe by using print(data). Now that our dataframe is ready we can create our dataset splits for training and testing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a73239455739e3e8504e3a264ab70a38/href">https://medium.com/media/a73239455739e3e8504e3a264ab70a38/href</a></iframe><p>We randomly sample 90% of the dataset for training and save the rest 10% for testing purposes. We can use the 10% of the dataset to check which set of training hyperparameters work best for our model.</p><p>We also log our run hyperparameters to <a href="http://neptune.ai/">neptune.ai</a>, just so that we can keep a track of them later. To this we can just declare a dictionary with keys as the hyperparameter names.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ba3c61851e92ceb241367baf682fcd4/href">https://medium.com/media/7ba3c61851e92ceb241367baf682fcd4/href</a></iframe><p>Now that our dataframe is split into training and testing, we can create our dataset class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e75347f1b59a04e06a662d8760977bb/href">https://medium.com/media/9e75347f1b59a04e06a662d8760977bb/href</a></iframe><p>The __len__() function determines the complete size of the dataset. And the __getitem__()call is used to load the image and label. Now that our dataset class is ready, we create class objects for our training and testing dataset, and create the dataloader.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5cc066ef82c862ed5d0eb646be0fc3a9/href">https://medium.com/media/5cc066ef82c862ed5d0eb646be0fc3a9/href</a></iframe><p>The transform function has two transform options, we first use convert the image to tensors, and then normalise the image. We then create the class objects for our training and testing set, and then feed them into the dataloader class to create train and test loaders.</p><p>We also load some images using our dataloader and proceed to log them to <a href="http://neptune.ai/">neptune.ai</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aa8458dc78b773b0e7edad748694095/href">https://medium.com/media/3aa8458dc78b773b0e7edad748694095/href</a></iframe><p>The train and test loader can then be used to load our dataset in batches of size 8 and 4 for training and testing respectively.</p><h3>Training the model</h3><p>Now that we have our datasets ready we can start with training our model. To do this we first need to initiate our model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbec068fe73618255fccb2851c7b843c/href">https://medium.com/media/dbec068fe73618255fccb2851c7b843c/href</a></iframe><p>We use the pre-trained ResNet18 model, but the only issue is that the pre-trained model has 1000 output classes. We change the fc layer of the model to have only 2 output neurons, for the two output classes that we have. We then shift the model to our device, and print the layers.</p><p>Keeping a copy of your model architecture is necessary in case you want to debug it later. Since we are using PyTorch’s model hub here we cannot log a .py file, but we can use <a href="http://neptune.ai/">neptune.ai</a>’s log string function which can be used to log strings. To log the model we can directly use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c727a3b5b7d557cd72557ea4e24102e/href">https://medium.com/media/4c727a3b5b7d557cd72557ea4e24102e/href</a></iframe><p>This would log our model architecture as well in the run.</p><p>Now that the model is ready, we declare our loss function, optimiser function, and the evaluation function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb8fc427e5a9c3946ecda1894e85bdfc/href">https://medium.com/media/cb8fc427e5a9c3946ecda1894e85bdfc/href</a></iframe><p>This leave the last step for us, which is to write the training code and train our model. The training loop is shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/052a7373aba65e03df7f338f6a28b96c/href">https://medium.com/media/052a7373aba65e03df7f338f6a28b96c/href</a></iframe><p>Calling the run class functions helps us to log the training and validation metrics. The same trends can be visualised on the <a href="http://neptune.ai/">neptune.ai</a> project page <a href="https://app.neptune.ai/r3yna/dogsvscats/runs/details?viewId=standard-view&amp;detailsTab=charts&amp;shortId=DVC-1">here</a>.</p><h3>Visualising model output using GradCam</h3><p>Once the training is completed, all that remains is to visualise the model’s performance using GradCam and uploading the model to the model registry. We first visualise the model’s performance using GradCam.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/276173bad369f15062cab9c822ae5db1/href">https://medium.com/media/276173bad369f15062cab9c822ae5db1/href</a></iframe><p>The above code opens a visualisation window, where the red part indicates the region important for model’s decision making.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7FeNOF87DIha-79l9mOMEw.png" /><figcaption>Source: Author</figcaption></figure><p>Now that we have visualised the model’s inference. We can publish the model to <a href="http://neptune.ai/">neptune.ai</a>’s model registry.</p><h3>Uploading model to model registry</h3><p><a href="http://neptune.ai/">neptune.ai</a>’s model registry can hold several model related artefacts. In the current usage scenario we will be using it to store our model’s weight file and the dataset.</p><p>We first need to create our model registry where we will be uploading the files to. Creating a model registry is a one time task and can be done using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec21d0a5ba0d300b2275c47e74bb5d4a/href">https://medium.com/media/ec21d0a5ba0d300b2275c47e74bb5d4a/href</a></iframe><p>Once the model registry is set, we can upload both our model weights and data easily.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9bbd23d898e0c03a281e07fe74053fb/href">https://medium.com/media/c9bbd23d898e0c03a281e07fe74053fb/href</a></iframe><p>Now that our model is uploaded to the model registry, we can start with out streamlit application and deploy the model.</p><h3>Deploying the model</h3><p>Creating an application to demonstrate your model is the best way to demonstrate a model’s performance. A user will not really run your inference script now, would they?</p><p>We use streamlit to create a simple application, and then download the weights from <a href="http://neptune.ai/">neptune.ai</a>’s model registry.</p><p>This can be done using:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/280df52bda65568104b2a6afbf092627/href">https://medium.com/media/280df52bda65568104b2a6afbf092627/href</a></iframe><p>We first load the uploaded image using the pillow library, convert it to RGB color space. Then the image is resized to 224 pixels by 224 pixels. Next we define our transform function which was also used during training, this ensures that our image now follows the same pre-processing steps as done for the training steps.</p><p>The next step is to load the <a href="http://neptune.ai/">neptune.ai</a> model registry, this would give us a list of models we have uploaded on <a href="http://neptune.ai/">neptune.ai</a> for our specific project. We then choose the index 0 for our model as we have only stored one model.</p><p>We can then initialise the specific model version and use it to download the uploaded weights. Then we load our model architecture using the torchvision api, change the model architecture to have only two classes, and then load our weights. Next we use the model to perform inference on the uploaded image and print the predictions back on the streamlit application.</p><p>To run the application just run streamlit run app.py on your terminal and a browser window will open up. A video demonstration of the application can be found <a href="https://vimeo.com/848460276?share=copy">here</a>.</p><h3>Conclusion</h3><p>In this article, we have discussed how to build an MLOps pipeline for a simple image classification network, focusing on classifying cats and dogs. The major steps covered are:</p><ol><li>Setting up the development environment: We installed Anaconda to manage our dependencies and created a separate virtual environment for our project.</li><li>Gathering training and testing data: We downloaded the dataset using a Chrome plugin or accessed it from <a href="http://neptune.ai/">neptune.ai</a>’s model registry, renaming the images and organising them into appropriate folders.</li><li>Building the deep learning model with PyTorch: We used the pre-trained ResNet18 model, modified its fully connected layer for two output classes (cats and dogs), and set up training with loss function, optimiser, and evaluation metrics.</li><li>Tracking experiments using <a href="http://neptune.ai/">neptune.ai</a>: We created a <a href="http://neptune.ai/">neptune.ai</a> project to log and track our experiment metadata, model hyperparameters, and evaluation metrics, which allowed us to easily compare different runs and share results with the team.</li><li>Visualising model output using GradCam: We used GradCam to visualize the regions of the input image that were most important for the model’s decision-making, helping us gain insights into the model’s behaviour.</li><li>Uploading model to <a href="http://neptune.ai/">neptune.ai</a>’s model registry: We uploaded the trained model’s weight file and the dataset to <a href="http://neptune.ai/">neptune.ai</a>’s model registry for easy access and deployment.</li><li>Deploying the model as a Streamlit application: We created a simple Streamlit application that allows users to upload an image, and the model performs inference on it, displaying the predicted class (cat or dog).</li></ol><p><a href="http://neptune.ai/">neptune.ai</a> played a crucial role in this pipeline by providing a centralised platform for tracking experiments, managing data versioning, and storing models and artifacts in the model registry. This streamlined the development process, allowing easy collaboration among team members and ensuring reproducibility of experiments. Additionally, <a href="http://neptune.ai/">neptune.ai</a>’s visualization capabilities helped us gain insights into our model’s performance, making it easier to make informed decisions about model improvements.</p><p>By following this comprehensive pipeline, we successfully built an image classification model, tracked its performance, visualised its behaviour, and deployed it as an interactive Streamlit application. This demonstrates the power of MLOps and how tools like <a href="http://neptune.ai/">neptune.ai</a> can simplify the entire process, making it easier to manage and deploy machine learning models.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41ae8b995eda" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>