<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Manish Aradwad</title>
    <description>The latest articles on DEV Community by Manish Aradwad (@toji).</description>
    <link>https://dev.to/toji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F248646%2F0a145408-e21e-420d-9a5c-6c19c7656b92.png</url>
      <title>DEV Community: Manish Aradwad</title>
      <link>https://dev.to/toji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toji"/>
    <language>en</language>
    <item>
      <title>Learn HPC with me: CPU vs GPU</title>
      <dc:creator>Manish Aradwad</dc:creator>
      <pubDate>Sat, 23 Nov 2024 12:03:29 +0000</pubDate>
      <link>https://dev.to/toji/learn-hpc-with-me-cpu-vs-gpu-44eb</link>
      <guid>https://dev.to/toji/learn-hpc-with-me-cpu-vs-gpu-44eb</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39aocsngcuuhg7wxg68e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39aocsngcuuhg7wxg68e.png" alt="A Ryzen CPU" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;20 Nov 2024&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I wasn’t able to provide much time for learning HPC in the last few(lots of) days. The reason is mostly my laziness but also I am in the phase of preparing and giving interviews. Anyways, I finally convinced myself to start the learning once again wherein I started reading the 4th chapter from beginning although I had stopped the reading after some pages of 4th chapter.&lt;/p&gt;

&lt;p&gt;The first sentence of the chapter:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;In Chapter 1, Introduction, we saw that CPUs are designed to minimize the latency of instruction execution and that GPUs are designed to maximize the throughput of executing instructions.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;made me think deeper about the difference between CPU and GPU.&lt;/p&gt;

&lt;p&gt;It’s a general consensus that GPUs are &lt;em&gt;better&lt;/em&gt; than CPUs, although this is true only in the sense of speed of execution of a problem statement whose solution can be coded as a parallel program. But does this mean &lt;strong&gt;any parallel program in general can be coded as a sequential program&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;When I asked Claude:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Consider a problem statement whose solution can be a implemented as a parallel solution. For ex. conversion of an image from RGB to grayscale can be one such problem. \&lt;br&gt;
My question is “In general, can any parallel solution be written as a sequential solution?” In other words, “Are there problems whose solution is strictly parallel in nature and cannot be solved using sequential instructions?” \&lt;br&gt;
Please let me know the correct answer with the proper logic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;it gave me the ultimate answer as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Church-Turing Thesis&lt;/strong&gt; states that any computational problem solvable by a parallel algorithm is also solvable by a sequential algorithm. No computational problem exists whose solution is strictly parallel and cannot be sequentially implemented. The differences lie in efficiency, not fundamental solvability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;which was convincing enough for me. So, after understanding this, coming back to the original argument, “are CPUs worse than GPUs?”. The answer is obviously no because there are lots of things which CPUs can do better than GPUs. So, both CPUs and GPUs are equally important in a good gaming rig.&lt;/p&gt;

&lt;p&gt;CPU design is to minimize the latency of single instruction while GPU design is to maximize the throughput of instruction execution. The way both the above goals affect the design philosophy of both types of processors:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Control Unit Design&lt;br&gt;
— CPU: Complex control unit with sophisticated branch prediction and speculation&lt;br&gt;
— GPU: Simple control units replicated many times, focusing on parallel execution&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cache and Memory&lt;br&gt;
— CPU: Large caches to reduce memory latency for individual operations&lt;br&gt;
— GPU: Smaller caches but higher memory bandwidth for parallel data access&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Execution Units&lt;br&gt;
— CPU: Few but complex ALUs optimized for diverse operations&lt;br&gt;
— GPU: Many simple ALUs designed for parallel floating-point operations&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pipeline Design&lt;br&gt;
— CPU: Deep pipelines with out-of-order execution to minimize stalls&lt;br&gt;
— GPU: Simpler pipelines with in-order execution, compensating with thread-level parallelism&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread Management&lt;br&gt;
— CPU: Optimized for few high-performance threads&lt;br&gt;
— GPU: Massive thread parallelism with hardware thread scheduler&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instruction Handling&lt;br&gt;
— CPU: Complex instruction decoder, branch prediction, speculative execution&lt;br&gt;
— GPU: SIMD (Single Instruction Multiple Data) architecture for parallel execution&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, after understanding this, I was thinking if it’s possible to create an entity which improves the performance of this whole big system. This system is a big system of Computation wherein:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We start from the silicon to make the chips which combine with the Von-neumann architecture to create the processors on which the problem statement solution is ran as a program written using various programming paradigms.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I wanted to know how the different chips-for-AI hardware startups as well as companies like Cerebras, grok, apple, intel, graphcore, etc. are making changes at different stages of this system to make things faster. Even programming language like Mojo target another stage of the system.&lt;/p&gt;

&lt;p&gt;Hope I find enough time in the future to understand how these things work, but for now, I think this much wandering is enough.&lt;/p&gt;




</description>
      <category>nvidia</category>
      <category>programming</category>
      <category>beginners</category>
      <category>ai</category>
    </item>
    <item>
      <title>"Learn HPC with me" kickoff</title>
      <dc:creator>Manish Aradwad</dc:creator>
      <pubDate>Sat, 23 Nov 2024 11:18:53 +0000</pubDate>
      <link>https://dev.to/toji/learn-hpc-with-me-kickoff-4djb</link>
      <guid>https://dev.to/toji/learn-hpc-with-me-kickoff-4djb</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1y7qx29hsvty4cji0mg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1y7qx29hsvty4cji0mg.png" alt="GPU Image" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey there! Manish here on the other side. I am not really gonna introduce myself. &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fmanish-aradwad.bearblog.dev%2F" rel="noopener noreferrer"&gt;Here&lt;/a&gt;'s my personal portfolio if you wanna more about me.&lt;/p&gt;

&lt;p&gt;In this blog, I'm kicking off my blog series (which I hopefully won't abandon in future) about the journey of learning HPC. I am learning it from &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fwww.amazon.in%2FProgramming-Massively-Parallel-Processors-Hands%2Fdp%2F0124159923" rel="noopener noreferrer"&gt;Programming Massively Parallel Processors&lt;/a&gt; book.&lt;/p&gt;

&lt;p&gt;I plan on finishing all the exercises as well along with the concepts. Till now I have finished the 3rd chapter and going through the 4th chapter currently. I create notes while reading the chapter on my iPad and finally I write the blogs on &lt;em&gt;bearblog&lt;/em&gt; platform. This blog is available on my personal portfolio.&lt;/p&gt;

&lt;p&gt;I am treating these blogs as go to notes to revise any concept in future. Along with this notes blog, I will be writing about any new things discovered while learning HPC in the form of casual blogs here. I will write the first blog after this first kickoff blog.&lt;/p&gt;

&lt;p&gt;If you've read till here then you're probably also interested in the blogs I will write. Here's the first blog:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://dev.to/toji/learn-hpc-with-me-cpu-vs-gpu-44eb"&gt;Learn HPC: CPU vs GPU&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Will see you later! 👋🏻&lt;/p&gt;

</description>
      <category>c</category>
      <category>gpu</category>
      <category>cuda</category>
      <category>ai</category>
    </item>
    <item>
      <title>Why kNN doesn't scale...</title>
      <dc:creator>Manish Aradwad</dc:creator>
      <pubDate>Thu, 12 Oct 2023 17:55:50 +0000</pubDate>
      <link>https://dev.to/toji/why-knn-doesnt-scale-3461</link>
      <guid>https://dev.to/toji/why-knn-doesnt-scale-3461</guid>
      <description>&lt;p&gt;...And how Approximate Nearest Neighbors mitigates that. &lt;/p&gt;

&lt;h2&gt;
  
  
  k-Nearest Neighbours:
&lt;/h2&gt;

&lt;p&gt;k-Nearest Neighbors (kNN) is a simple and intuitive algorithm used for classification and regression tasks. Given a new data point, the algorithm searches the training dataset for the 'k' training examples that are closest to the point and then returns the output based on these 'k' examples.&lt;/p&gt;

&lt;p&gt;kNN is used in some of the modern AI solutions like LLMs. But one of the major issues with kNN is its scalability. There are 3 important points regarding this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalability Issues of kNN:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Brute Force Search&lt;/strong&gt;: In its basic form, for every new data point, KNN performs a brute force search to calculate distances to all points in the training dataset. This means the time complexity is O(N), where N is the number of data points in the dataset. This is assuming calculation of these distances happens in constant time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Memory Intensive&lt;/strong&gt;: Storing the entire dataset is necessary for KNN, which can be memory intensive for large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;High Dimensionality&lt;/strong&gt;: As the dimensionality of the data increases, the distance between most pairs of points tends to become more similar (known as the curse of dimensionality). This can make the distance metric less meaningful and degrade the performance of KNN.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Approximate Nearest Neighbors (ANN):
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Approximate Nearest Neighbors (ANN) algorithms aim to find the nearest neighbors in sub-linear time by allowing a small probability of error. The key idea is that in many applications, an approximate answer is sufficient. Here I will explain one of the most common ANN algorithm, Locality Sensitive Hashing(LSH). &lt;/p&gt;

&lt;p&gt;LSH is a technique which is used to hash similar dataset elements together in the same buckets. For ex. Vector embeddings of the similar words in a vocabulary will lie in the same bucket. But instead of typical buckets, you can think of clustering the points by deciding whether they lie above or below a hyperplane. &lt;/p&gt;

&lt;p&gt;These hyperplanes are randomly generated and are used to divide the vector space into small sections. If the vector embeddings of a word are two dimensional then this hyperplane will be a line.&lt;/p&gt;

&lt;p&gt;To decide whether a vector embedding lie above or below a line, we take the &lt;a href="https://www.cuemath.com/geometry/projection-vector/" rel="noopener noreferrer"&gt;dot product&lt;/a&gt; of the vector embedding with the line(hyperplane in higher dimensions) which is also known as &lt;em&gt;Projecting a Vector onto the Line&lt;/em&gt;. The sign of this dot product will help us decide whether embedding lies above or below a line. &lt;/p&gt;

&lt;p&gt;Here is a visualisation of a projection in 2D space&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7eh59fzns9n429onbgx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7eh59fzns9n429onbgx6.png" alt="Projection Visualisation" width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is how sign of the projection tells us about its position&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhc1ny8ih6halfd7x4brs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhc1ny8ih6halfd7x4brs.png" alt="Sign of the projection" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's say you are trying to optimise below loss function:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;|| h - X ||&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;where &lt;code&gt;h&lt;/code&gt; is the test value and &lt;code&gt;X&lt;/code&gt; represents value from the dataset&lt;/p&gt;

&lt;p&gt;In the normal kNN, you will have to go through each &lt;code&gt;X&lt;/code&gt; in the dataset, compare it with &lt;code&gt;h&lt;/code&gt; using some metric like Cosine Similarity or Euclidean Distance. Calculating this metric for each &lt;code&gt;X&lt;/code&gt; is very cumbersome in case of a large dataset. &lt;/p&gt;

&lt;p&gt;So, we do the following steps in the ANN:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use LSH to find the hash values of all the &lt;code&gt;X&lt;/code&gt;'s in the dataset (which involves only simple matrix multiplications).&lt;/li&gt;
&lt;li&gt;Find the hash value of the test value &lt;code&gt;h&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Compare &lt;code&gt;h&lt;/code&gt; with only those &lt;code&gt;X&lt;/code&gt;'s which got the same hash value using the above metrics. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can probably guess, this method doesn't gurantee the answer we might get in case of kNN. So, this is done for multiple iterations by generating random planes to increase the likelihood of getting the right answer.&lt;/p&gt;

&lt;p&gt;This is how LSH with Approximate Nearest Neighbors trades accuracy with efficiency.&lt;/p&gt;

&lt;p&gt;Apart from LSH, ANN uses some other methods too like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Random Projection Trees&lt;/strong&gt;: These are space partitioning data structures that divide the dataset into increasingly smaller subsets using random hyperplanes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;KD-Trees, Ball Trees&lt;/strong&gt;: These are also space partitioning trees but may not perform as well in very high dimensions.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To conclude, while KNN is straightforward and can be very accurate, it can be computationally expensive and unfeasible for large datasets or high-dimensional data. ANN provides a trade-off between accuracy and speed, allowing for efficient querying on large datasets. Depending on the specific requirements (e.g., acceptable error rate, query speed), one might choose an appropriate ANN method over traditional KNN.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>tutorial</category>
      <category>ai</category>
    </item>
    <item>
      <title>Assorted Neural Style Transfer: An extension of Vanilla NST</title>
      <dc:creator>Manish Aradwad</dc:creator>
      <pubDate>Sat, 02 Jul 2022 15:27:58 +0000</pubDate>
      <link>https://dev.to/toji/assorted-neural-style-transfer-an-extension-of-vanilla-nst-5e46</link>
      <guid>https://dev.to/toji/assorted-neural-style-transfer-an-extension-of-vanilla-nst-5e46</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;Neural Style Transfer&lt;/em&gt;&lt;/strong&gt; explores methods for artistic style transfer based on Convolutional Neural Networks. The core idea proposed by &lt;a href="https://arxiv.org/abs/1508.06576" rel="noopener noreferrer"&gt;Gatys et al.&lt;/a&gt; became very popular, and with further research, &lt;a href="https://arxiv.org/abs/1603.08155" rel="noopener noreferrer"&gt;Johnson et al.&lt;/a&gt; overcame a significant limitation to achieve style transfer in real-time.&lt;/p&gt;

&lt;p&gt;This article uses the VGG16 model for &lt;a href="https://harishnarayanan.org/writing/artistic-style-transfer/" rel="noopener noreferrer"&gt;manual implementation&lt;/a&gt; of Neural Style Transfer. Another implementation is based on an &lt;a href="https://www.tensorflow.org/tutorials/generative/style_transfer" rel="noopener noreferrer"&gt;article&lt;/a&gt; by TensorFlow, which uses a pre-trained model for NST.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what is Assorted Neural Style Transfer? (Yes, I came up with that name myself 😅)
&lt;/h2&gt;

&lt;p&gt;Well, as we know, NST combines the style of Style Image with the content of Content Image as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeep25gatek3h81q1g96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgeep25gatek3h81q1g96.png" alt="Vanilla NST" width="788" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We propose the &lt;strong&gt;Assorted NST&lt;/strong&gt;, which combines the style of 3 Style Images with the content of Content Image. Below are a few examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmasjk4xetp6uh4l8oc0x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmasjk4xetp6uh4l8oc0x.png" alt="Input Images to Assorted NST" width="788" height="188"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91bc833l87y21hwaq0sa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F91bc833l87y21hwaq0sa.png" alt="The output of Assorted NST (Style Weights = [0.3, 0.3, 0.4])" width="557" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We not only combine the three styles, but we can also control how much weight to give to which style. The above output was generated with weights &lt;em&gt;[0.3, 0.3, 0.4]&lt;/em&gt;. The weights &lt;em&gt;[0.1, 0.1, 0.8]&lt;/em&gt; (where style 3 has more weight) will give the following output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7o2hef3y3bzsnbo5sec7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7o2hef3y3bzsnbo5sec7.png" alt="The output of Assorted NST (Style Weights = [0.1, 0.1, 0.8])" width="557" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So, how does the Assorted NST work??
&lt;/h2&gt;

&lt;p&gt;It's pretty straightforward. Instead of giving the model a single style image as input, we take the weighted combination of all three style images and feed that to the model. Before taking the weighted combination, we resize the style images to have the exact dimensions.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this way, the model can extract the style of the corresponding final image, which can be used for final image generation.&lt;/p&gt;

&lt;p&gt;The above Assorted NST example is based on the &lt;a href="https://colab.research.google.com/drive/1zTUF0uKeAIv51V9cWxu8EAbKjBAlNkyv?usp=sharing" rel="noopener noreferrer"&gt;TF Hub's model&lt;/a&gt;, while below are some examples of &lt;a href="https://colab.research.google.com/drive/1fHHzQOzaO_F7nsJsq9qPCqofv-pFv5Wn?usp=sharing" rel="noopener noreferrer"&gt;manual Assorted NST implementation&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5tcqnn3bfkiv6ho93yc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5tcqnn3bfkiv6ho93yc.png" alt="Input Images to Assorted NST" width="788" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk77jeojvo4t5nwhs6e6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk77jeojvo4t5nwhs6e6i.png" alt="The output of Assorted NST (Style Weights = [0.35, 0.1, 0.45])" width="557" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg6s07c9uynjvfu4iwyf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg6s07c9uynjvfu4iwyf.png" alt="The output of Assorted NST (Style Weights = [0.75, 0.1, 0.15])" width="557" height="574"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Few Limitations of this method:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For each set of content and style images, we have to do fine variations in weight values for the output to be better. It is impossible to have a fixed set of weights that work on all images. If the weights are not proper, then the outcome might be invalid like below:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frb32b3q1mqqa76a7mqa7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frb32b3q1mqqa76a7mqa7.png" alt="Not Good Outputs!" width="788" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The time taken for output image generation is almost 8 seconds per iteration in manual implementation. We need at least ten iterations to get valid output. This can further be reduced using an end-to-end CNN model explicitly built for NST as introduced in Johnson et al. (which is used in TFHub implementation).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Thanks for reading!&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://towardsdatascience.com/mixed-neural-style-transfer-with-two-style-images-9469b2681b54" rel="noopener noreferrer"&gt;Mixed Neural Style Transfer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://harishnarayanan.org/writing/artistic-style-transfer/" rel="noopener noreferrer"&gt;Manual Implementation of NST&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.tensorflow.org/tutorials/generative/style_transfer" rel="noopener noreferrer"&gt;TFHub's NST Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>deeplearning</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Creating gradient images using only Python NumPy</title>
      <dc:creator>Manish Aradwad</dc:creator>
      <pubDate>Sun, 26 Jun 2022 13:24:20 +0000</pubDate>
      <link>https://dev.to/toji/creating-gradient-images-using-only-python-numpy-3lgc</link>
      <guid>https://dev.to/toji/creating-gradient-images-using-only-python-numpy-3lgc</guid>
      <description>&lt;h2&gt;
  
  
  Here’s the backstory of this article:
&lt;/h2&gt;

&lt;p&gt;I was working on a Computer Vision Project as a part of an internship and I needed a script which can generate custom B/W gradient images. By custom gradient, I mean images like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3iwiv7aawk645ysg0gzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3iwiv7aawk645ysg0gzt.png" alt="Cross Gradient" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3f2n8sutxfnahn97z429.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3f2n8sutxfnahn97z429.png" alt="Linear Gradient" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7og1x8dqfx8od84tid7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk7og1x8dqfx8od84tid7.png" alt="Parabolic Gradient" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Along with the above structures, I also wanted to vary the spread of the gradient and combine different such structures to get the following samples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F914qf7wxwl6bhof5752e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F914qf7wxwl6bhof5752e.png" alt="Combination of Cross and Parabolic Gradient" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7vq7rjvwyq5a2jv0zq1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7vq7rjvwyq5a2jv0zq1.png" alt="Combination of Linear and Parabolic Gradient" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9iuciae47iir9zuuec7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9iuciae47iir9zuuec7n.png" alt="Combination of Two Parabolic Gradients" width="475" height="470"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tried to search online but couldn’t find the solution which I really wanted. To help others like me, I decided to write this article. I use NumPy arrays with loops to generate such gradient images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;

&lt;p&gt;Gradients are essentially uneven arrays of numbers. So first, we have to write a function for uneven array creation. Refer &lt;a href="https://stackoverflow.com/questions/32504766/python-generate-unevenly-spaced-array#:~:text=Find%20a%20function%20f%20which,*(ub%2Dlb)%20.&amp;amp;text=Now%20with%20the%20parameter%20spacing,your%20values%20gather%20around%20ub" rel="noopener noreferrer"&gt;this&lt;/a&gt; for explanation:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We can use this function for creating different types of gradients as follows:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, we can write a function which returns a gradient of one of the above types with random parameters:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Finally, when we run the custom_gradient() function, it will return one of the gradient image type. Complete code is available in &lt;a href="https://colab.research.google.com/drive/1v_euV4liOWx0HdY97vxDh8K0d1fhQNZP?usp=sharing" rel="noopener noreferrer"&gt;this notebook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

&lt;p&gt;Have a nice day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>machinelearning</category>
    </item>
  </channel>
</rss>
