<?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: Yvette Kazeneza</title>
    <description>The latest articles on DEV Community by Yvette Kazeneza (@yvette_kazeneza_853bb8ef5).</description>
    <link>https://dev.to/yvette_kazeneza_853bb8ef5</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4151841%2Fbc9fbe6b-64f7-438f-8ca9-f121c66b5ded.png</url>
      <title>DEV Community: Yvette Kazeneza</title>
      <link>https://dev.to/yvette_kazeneza_853bb8ef5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yvette_kazeneza_853bb8ef5"/>
    <language>en</language>
    <item>
      <title>From Pixels to Predictions: Exploring Computer Vision with Python</title>
      <dc:creator>Yvette Kazeneza</dc:creator>
      <pubDate>Tue, 13 Oct 2026 13:48:04 +0000</pubDate>
      <link>https://dev.to/yvette_kazeneza_853bb8ef5/from-pixels-to-predictions-exploring-computer-vision-with-python-lne</link>
      <guid>https://dev.to/yvette_kazeneza_853bb8ef5/from-pixels-to-predictions-exploring-computer-vision-with-python-lne</guid>
      <description>&lt;p&gt;When I first started learning computer vision, I thought it was simply about teaching computers to recognize images. However, as I explored the concepts and experimented with Python, I discovered that there is much more happening behind the scenes.&lt;/p&gt;

&lt;p&gt;How does a computer represent an image? How can it detect the edges of an object? How does a model recognize a person or a vehicle? And how can it identify the exact pixels belonging to an object rather than just drawing a rectangle around it?&lt;/p&gt;

&lt;p&gt;In this article, I will share what I have learned about computer vision so far, including image representation, filters, image classification, object detection, and segmentation. I will also share some practical experiments using Python, NumPy, Matplotlib, and YOLO.&lt;/p&gt;

&lt;p&gt;My goal is to explain these concepts in a way that is easy for other beginners to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What Is Computer Vision?
&lt;/h2&gt;

&lt;p&gt;Computer vision is a field of artificial intelligence (AI) that enables computers to analyze and interpret visual information, such as images and videos.&lt;/p&gt;

&lt;p&gt;Humans can look at a photograph and immediately recognize people, vehicles, buildings, or animals. Computers do not naturally understand images in the same way. They process numerical representations of visual information and use algorithms or trained models to identify patterns and make predictions.&lt;/p&gt;

&lt;p&gt;Computer vision has many real-world applications, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Self-driving cars that detect pedestrians, traffic signs, and other vehicles.&lt;/li&gt;
&lt;li&gt;Medical imaging systems that help identify abnormalities.&lt;/li&gt;
&lt;li&gt;Security systems that recognize or track objects.&lt;/li&gt;
&lt;li&gt;Agriculture systems that detect crop diseases.&lt;/li&gt;
&lt;li&gt;Satellite imagery systems that identify buildings and roads.&lt;/li&gt;
&lt;li&gt;Retail systems that recognize and count products.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding computer vision starts with a fundamental question: &lt;strong&gt;How does a computer see an image?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Understanding Pixels: How Computers Represent Images
&lt;/h2&gt;

&lt;p&gt;An image may look like a continuous photograph to us, but a computer represents a digital image as a grid of tiny elements called &lt;em&gt;pixels&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Each pixel stores numerical information about the color or brightness at a particular position in the image.&lt;/p&gt;

&lt;p&gt;For example, suppose we have an image that is 400 pixels high and 600 pixels wide.&lt;/p&gt;

&lt;p&gt;The total number of pixels is:&lt;/p&gt;

&lt;p&gt;400 × 600 = 240,000 pixels.&lt;/p&gt;

&lt;p&gt;If the image uses RGB color, each pixel has three color channels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Red (R)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Green (G)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Blue (B)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each channel stores a value representing the intensity of that color. In a standard 8-bit image, these values usually range from 0 to 255.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;(255, 0, 0)&lt;/code&gt; represents red.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(0, 255, 0)&lt;/code&gt; represents green.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(0, 0, 255)&lt;/code&gt; represents blue.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(255, 255, 255)&lt;/code&gt; represents white.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;(0, 0, 0)&lt;/code&gt; represents black.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since our example image contains 240,000 pixels and each pixel has three color-channel values, it contains:&lt;/p&gt;

&lt;p&gt;240,000 × 3 = 720,000 channel values.&lt;/p&gt;

&lt;p&gt;The important distinction is that the image still contains &lt;strong&gt;240,000 pixels&lt;/strong&gt;, not 720,000 pixels. The larger number represents the individual red, green, and blue values stored across those pixels.&lt;/p&gt;

&lt;p&gt;This helped me understand that computers process images as numerical data rather than as pictures in the human sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Exploring Images with Python
&lt;/h2&gt;

&lt;p&gt;Python provides useful libraries for working with images. Two important tools I explored are NumPy and Matplotlib.&lt;/p&gt;

&lt;p&gt;NumPy allows us to manipulate numerical arrays, while Matplotlib helps us display images and visualize results.&lt;/p&gt;

&lt;p&gt;For example, an RGB image can be represented as a three-dimensional array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;PIL&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;

&lt;span class="c1"&gt;# Load an image
&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;convert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;RGB&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Convert the image into a NumPy array
&lt;/span&gt;&lt;span class="n"&gt;img_array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Display image information
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Image shape:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;img_array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Total pixels:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;img_array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;img_array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# Display the image
&lt;/span&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;imshow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;img_array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;axis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;off&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the image is 400 pixels high and 600 pixels wide, its shape will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(400, 600, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first number represents the height, the second represents the width, and the third represents the three RGB channels.&lt;/p&gt;

&lt;p&gt;I also learned that we can manipulate individual color channels. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;red_removed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img_array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;red_removed&lt;/span&gt;&lt;span class="p"&gt;[:,&lt;/span&gt; &lt;span class="p"&gt;:,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;imshow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red_removed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;axis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;off&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;[:, :, 0]&lt;/code&gt; selects the red channel, and assigning zero removes the red contribution from every pixel.&lt;/p&gt;

&lt;p&gt;The resulting image looks different because we have changed the numerical information used to represent its colors.&lt;/p&gt;

&lt;p&gt;This experiment demonstrated how simple array operations can directly change an image's appearance.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Image Filters: How Computers Extract Visual Information
&lt;/h2&gt;

&lt;p&gt;After understanding pixels, I moved on to image filters.&lt;/p&gt;

&lt;p&gt;An image filter is a mathematical operation applied to an image to change its appearance or emphasize certain visual features.&lt;/p&gt;

&lt;p&gt;Many filters use a small matrix called a &lt;em&gt;kernel&lt;/em&gt; or &lt;em&gt;filter&lt;/em&gt;. This kernel moves across the image, examining small neighborhoods of pixels and calculating new values.&lt;/p&gt;

&lt;p&gt;Depending on the kernel, the operation can blur an image, sharpen details, or highlight edges.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.1 Blurring an image
&lt;/h3&gt;

&lt;p&gt;Blurring reduces fine details and makes an image look smoother.&lt;/p&gt;

&lt;p&gt;One way to do this is to calculate the average of the pixel values in a small neighborhood.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;blur_kernel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ones&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blur_kernel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a 5 × 5 matrix containing 25 equal weights, each equal to 0.04.&lt;/p&gt;

&lt;p&gt;When applied as a filter, each output pixel is calculated from the average of the corresponding 25-pixel neighborhood, assuming the usual uniform averaging operation.&lt;/p&gt;

&lt;p&gt;The effect is that sudden changes in brightness and small details become less pronounced.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.2 Sharpening an image
&lt;/h3&gt;

&lt;p&gt;Sharpening emphasizes differences between a pixel and its neighbors, making edges and details appear more distinct.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;sharpen_kernel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This kernel gives the center pixel a weight of 5 and subtracts the values of its four direct neighbors.&lt;/p&gt;

&lt;p&gt;Conceptually, the calculation is:&lt;/p&gt;

&lt;p&gt;New pixel value = 5 × center − left − right − top − bottom.&lt;/p&gt;

&lt;p&gt;This can emphasize details, although it can also amplify noise. The resulting values may fall outside the normal 0–255 range, so they may need to be clipped or otherwise converted appropriately.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.3 Detecting edges with Sobel filters
&lt;/h3&gt;

&lt;p&gt;Edges are important because they often indicate boundaries between objects or regions with different brightness levels.&lt;/p&gt;

&lt;p&gt;I also learned about Sobel filters, which help detect brightness changes in particular directions.&lt;/p&gt;

&lt;p&gt;A Sobel Y kernel can be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;sobel_y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This kernel measures brightness changes from the top toward the bottom of an image. As a result, it emphasizes horizontal edges.&lt;/p&gt;

&lt;p&gt;A Sobel X kernel measures changes from left to right and emphasizes vertical edges.&lt;/p&gt;

&lt;p&gt;These operations helped me understand that computers can extract useful visual patterns from images through mathematical calculations.&lt;/p&gt;

&lt;p&gt;It is also important to note that traditional filters and modern deep learning models are not the same thing. Filters such as Sobel use predefined mathematical operations, whereas a convolutional neural network can learn useful filters from training data.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Understanding Convolutional Neural Networks (CNNs)
&lt;/h2&gt;

&lt;p&gt;As I continued learning, I encountered convolutional neural networks, commonly known as CNNs.&lt;/p&gt;

&lt;p&gt;A CNN is a type of neural network widely used in image-related tasks. It learns patterns from image data, often starting with simple features such as edges and textures and building toward more complex patterns.&lt;/p&gt;

&lt;p&gt;For example, a CNN might learn to recognize:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Simple visual patterns, such as edges and lines.&lt;/li&gt;
&lt;li&gt;More complex features, such as shapes and textures.&lt;/li&gt;
&lt;li&gt;Combinations of features associated with objects, such as faces, vehicles, or animals.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Unlike a manually designed Sobel kernel, the filters in a CNN are typically learned during training.&lt;/p&gt;

&lt;p&gt;CNNs are used in tasks such as image classification, object detection, and segmentation.&lt;/p&gt;

&lt;p&gt;This introduced an important distinction in my learning: traditional image processing applies operations that people explicitly define, while deep learning can learn useful visual patterns from examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Image Classification: What Is in This Image?
&lt;/h2&gt;

&lt;p&gt;My first practical experiment with YOLO focused on image classification.&lt;/p&gt;

&lt;p&gt;Image classification answers a simple question:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does this image contain?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, given a photograph of a dog, a classification model might predict:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog             92%
wolf             5%
fox              3%
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These values are illustrative. They represent possible model scores, not results from a particular experiment.&lt;/p&gt;

&lt;p&gt;For my experiment, I used a pretrained YOLO classification model with the Ultralytics Python library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ultralytics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YOLO&lt;/span&gt;

&lt;span class="c1"&gt;# Load a pretrained classification model
&lt;/span&gt;&lt;span class="n"&gt;classifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;YOLO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yolo11n-cls.pt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Classify an image
&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;classifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verbose&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Get the first result
&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Display the top five predictions
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top5&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;confidence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;probs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;item&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;confidence&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The filename &lt;code&gt;yolo11n-cls.pt&lt;/code&gt; identifies a YOLO11 nano classification model. The &lt;code&gt;-cls&lt;/code&gt; suffix indicates that it is designed for classification.&lt;/p&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;classifier(...)&lt;/code&gt; runs the model on the image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;r.probs&lt;/code&gt; contains the classification scores.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;r.probs.top5&lt;/code&gt; provides the indices of the five highest-scoring classes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;r.names[idx]&lt;/code&gt; retrieves the corresponding class name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pretrained classification model used in this experiment is trained on ImageNet classes, so its predictions are limited to the categories it knows.&lt;/p&gt;

&lt;p&gt;One important lesson was that a model's prediction is not always correct. A model may assign a high score to an incorrect class, especially when an image contains unfamiliar objects or differs from the data used during training.&lt;/p&gt;

&lt;p&gt;Classification is useful when we need an overall label for an image, but it does not tell us where an object is located.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Object Detection: What Is in the Image, and Where Is It?
&lt;/h2&gt;

&lt;p&gt;Object detection goes one step further than classification.&lt;/p&gt;

&lt;p&gt;Instead of only predicting which objects are present, a detection model identifies individual objects, predicts their classes, and estimates their locations using &lt;em&gt;bounding boxes&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A bounding box is a rectangle drawn around a detected object.&lt;/p&gt;

&lt;p&gt;For example, a street photograph may contain a bus, two people, and a car. A detection model can draw separate boxes around these objects and provide a confidence score for each detection.&lt;/p&gt;

&lt;p&gt;I used the following YOLO model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ultralytics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YOLO&lt;/span&gt;

&lt;span class="c1"&gt;# Load the pretrained object detection model
&lt;/span&gt;&lt;span class="n"&gt;detector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;YOLO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yolo11n.pt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Detect objects in an image
&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;detector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Display the annotated image
&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike the classification model, &lt;code&gt;yolo11n.pt&lt;/code&gt; is the object detection version.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;conf=0.4&lt;/code&gt; argument sets a confidence threshold of 40%. Detections below the configured threshold are generally filtered out.&lt;/p&gt;

&lt;p&gt;I also explored how to retrieve information about each detected object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;boxes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;class_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;confidence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;label&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;class_id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;box&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xyxy&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;tolist&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;confidence&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;box=(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;y1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)-(&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;x2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;y2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output provides three important pieces of information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class:&lt;/strong&gt; the predicted object category.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confidence:&lt;/strong&gt; the model's confidence score for that detection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coordinates:&lt;/strong&gt; the estimated location of the bounding box.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The coordinates are represented as &lt;code&gt;(x1, y1, x2, y2)&lt;/code&gt;, where the first pair identifies the top-left corner and the second pair identifies the bottom-right corner. The x-coordinate increases from left to right, while the y-coordinate increases from top to bottom.&lt;/p&gt;

&lt;p&gt;The width and height can be calculated as:&lt;/p&gt;

&lt;p&gt;Width = x2 − x1&lt;/p&gt;

&lt;p&gt;Height = y2 − y1&lt;/p&gt;

&lt;h3&gt;
  
  
  Experimenting with confidence thresholds
&lt;/h3&gt;

&lt;p&gt;I also explored how changing the confidence threshold affects the detections.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Lower threshold
&lt;/span&gt;&lt;span class="n"&gt;results_low&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;detector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Higher threshold
&lt;/span&gt;&lt;span class="n"&gt;results_high&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;detector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lower threshold can retain more detections, including uncertain or incorrect predictions. A higher threshold generally retains fewer detections, but the remaining detections have higher model scores.&lt;/p&gt;

&lt;p&gt;This is a trade-off between retaining potential objects and filtering out uncertain predictions. A high confidence score does not guarantee that a detection is correct.&lt;/p&gt;

&lt;p&gt;The YOLO model used here is pretrained on the COCO dataset, which contains 80 object categories. It cannot automatically recognize every possible object in the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Image Segmentation: Which Pixels Belong to Each Object?
&lt;/h2&gt;

&lt;p&gt;The next step in my learning journey was segmentation.&lt;/p&gt;

&lt;p&gt;Segmentation provides more detailed information than a bounding box. It identifies the pixels associated with an object, allowing the model to follow its shape more closely.&lt;/p&gt;

&lt;p&gt;For example, a bounding box around a person may also include some of the surrounding road or background. A segmentation mask can outline the person more precisely.&lt;/p&gt;

&lt;p&gt;Segmentation is useful in applications such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Medical imaging, where models can outline organs or potential abnormalities.&lt;/li&gt;
&lt;li&gt;Satellite imagery, where models can identify buildings, roads, or agricultural areas.&lt;/li&gt;
&lt;li&gt;Autonomous driving, where systems can distinguish roads, vehicles, pedestrians, and other scene elements.&lt;/li&gt;
&lt;li&gt;Image editing, where a foreground object needs to be separated from its background.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Running a segmentation model
&lt;/h3&gt;

&lt;p&gt;I used the YOLO11 nano segmentation model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ultralytics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YOLO&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;

&lt;span class="c1"&gt;# Load the segmentation model
&lt;/span&gt;&lt;span class="n"&gt;segmenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;YOLO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yolo11n-seg.pt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Run segmentation
&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;segmenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image.jpg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Display the segmentation result
&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-seg&lt;/code&gt; suffix indicates that the model is designed for segmentation.&lt;/p&gt;

&lt;p&gt;For each detected object, the model can provide a class, a confidence score, a bounding box, and a segmentation mask.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding segmentation masks
&lt;/h3&gt;

&lt;p&gt;A mask represents the pixels associated with an object. Conceptually, a binary mask can contain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt;: the pixel belongs to the object.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt;: the pixel does not belong to the object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following example shows how to access the masks and count their marked pixels:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;masks&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class_id&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;zip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;masks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;boxes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cls&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;class_id&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
        &lt;span class="n"&gt;pixel_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mask&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;pixel_count&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; mask pixels&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;No objects were detected.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact mask representation depends on the model and library. In this example, the mask data is represented numerically, and summing the mask values gives an approximate count of the marked pixels.&lt;/p&gt;

&lt;p&gt;These counts are based on the model's mask resolution, which may be smaller than the original image. They are useful for comparing mask areas, but they are not automatically exact measurements of real-world object sizes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Combining masks
&lt;/h3&gt;

&lt;p&gt;I also explored how multiple object masks can be combined into one grid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;masks&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;combined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;masks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cpu&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;numpy&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;imshow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;combined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cmap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;gray&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Combined object masks&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;axis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;off&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Object pixels cover approximately &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;combined&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; of the mask grid&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The operation &lt;code&gt;.max(axis=0)&lt;/code&gt; combines the masks by taking the maximum value at each corresponding pixel position. For binary masks, a pixel is marked if at least one mask marks it as belonging to an object.&lt;/p&gt;

&lt;p&gt;The mean of a binary mask represents the fraction of its values that are marked as 1. Multiplying this fraction by 100 gives a percentage.&lt;/p&gt;

&lt;p&gt;This is a useful introduction to how pixel-level information can be used to estimate the area covered by detected objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Classification vs. Detection vs. Segmentation
&lt;/h2&gt;

&lt;p&gt;After experimenting with these approaches, I found it helpful to compare them directly.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Classification&lt;/th&gt;
&lt;th&gt;Object detection&lt;/th&gt;
&lt;th&gt;Segmentation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Main question&lt;/td&gt;
&lt;td&gt;What is in the image?&lt;/td&gt;
&lt;td&gt;What objects are present, and where?&lt;/td&gt;
&lt;td&gt;Which pixels belong to each object?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Identifies classes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Locates individual objects&lt;/td&gt;
&lt;td&gt;Not generally&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Produces bounding boxes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Usually available with instance segmentation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Produces pixel-level masks&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example use&lt;/td&gt;
&lt;td&gt;Categorizing product images&lt;/td&gt;
&lt;td&gt;Counting vehicles in a street image&lt;/td&gt;
&lt;td&gt;Outlining buildings in satellite imagery&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The main difference is the level of detail each task provides.&lt;/p&gt;

&lt;p&gt;Classification predicts categories for an image. Object detection identifies and locates individual objects. Segmentation goes further by identifying the pixels associated with objects or regions.&lt;/p&gt;

&lt;p&gt;One additional distinction is worth knowing: &lt;em&gt;instance segmentation&lt;/em&gt; identifies separate objects individually, while &lt;em&gt;semantic segmentation&lt;/em&gt; assigns a category to each pixel. For example, instance segmentation can distinguish one person from another, whereas semantic segmentation may label both as the category &lt;code&gt;person&lt;/code&gt; without separating their individual identities.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. My Main Lessons and Takeaways
&lt;/h2&gt;

&lt;p&gt;Working through these experiments helped me connect the theory of computer vision with practical Python code.&lt;/p&gt;

&lt;p&gt;Here are the key lessons I have learned so far:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Images are numerical data.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A computer represents a digital image using pixel values arranged in arrays. Understanding image dimensions and color channels makes image manipulation easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Filters can reveal visual features.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Blurring, sharpening, and edge detection demonstrate how mathematical operations can change images and emphasize useful information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. CNNs can learn visual patterns.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Rather than relying only on manually designed filters, convolutional neural networks learn useful representations from training examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Classification, detection, and segmentation solve different problems.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Classification identifies categories, detection locates objects, and segmentation identifies object pixels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Confidence scores need careful interpretation.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A model's score is not a guarantee of correctness. Changing a detection threshold affects which predictions are retained.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Pretrained models have limitations.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A model can only reliably recognize categories it has learned to represent, and its performance depends on the quality and suitability of its training data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Practical experimentation makes concepts clearer.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Running models on different images, inspecting predictions, and changing parameters helped me understand the concepts better than reading definitions alone.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. What I Want to Explore Next
&lt;/h2&gt;

&lt;p&gt;This is only the beginning of my computer vision journey. There are several areas I would like to explore next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding convolutional neural networks in greater depth.&lt;/li&gt;
&lt;li&gt;Learning how YOLO detects objects internally.&lt;/li&gt;
&lt;li&gt;Exploring how models are trained and evaluated.&lt;/li&gt;
&lt;li&gt;Comparing pretrained models with models trained on custom datasets.&lt;/li&gt;
&lt;li&gt;Testing computer vision applications using local images and real-world problems.&lt;/li&gt;
&lt;li&gt;Exploring how computer vision can contribute to agriculture, healthcare, and other challenges in Rwanda and beyond.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am particularly interested in understanding not only how to use pretrained models, but also how they work and how to evaluate their limitations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Computer vision has helped me see images differently. What looks like a simple photograph to a human is a structured collection of numerical values that a computer can process to identify patterns, recognize objects, and understand visual scenes.&lt;/p&gt;

&lt;p&gt;Starting with pixels and filters gave me the foundation to understand how images are represented and processed. Experimenting with YOLO then showed me how pretrained models can perform classification, object detection, and segmentation.&lt;/p&gt;

&lt;p&gt;I am still learning, but these practical experiments have given me a stronger foundation and a clearer picture of how computer vision works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you are also learning computer vision, I encourage you to start with a simple image, experiment with it, and observe how different techniques interpret the same scene.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You might be surprised by how much you can learn by looking beyond the picture itself.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This article reflects my learning and practical experiments with computer vision so far. I welcome feedback, corrections, and suggestions from others who are exploring this field.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>computervision</category>
      <category>python</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Word Embeddings Explained: From Words to Vectors</title>
      <dc:creator>Yvette Kazeneza</dc:creator>
      <pubDate>Wed, 30 Sep 2026 14:53:21 +0000</pubDate>
      <link>https://dev.to/yvette_kazeneza_853bb8ef5/word-embeddings-explained-from-words-to-vectors-13b</link>
      <guid>https://dev.to/yvette_kazeneza_853bb8ef5/word-embeddings-explained-from-words-to-vectors-13b</guid>
      <description>&lt;p&gt;&lt;strong&gt;Understanding Word Embedding&lt;/strong&gt; &lt;br&gt;
When I first started working on Natural Language Processing (NLP), I thought the main challenge was simply getting a computer to understand text. During my movie-review sentiment analysis project, I learned that there is a much deeper problem: computers do not naturally understand words the way humans do.&lt;br&gt;
For a computer, a word such as “amazing” is initially just text. To process it, we need to convert it into numbers. But simply assigning a number to each word does not tell a machine that “amazing” and “excellent” have related meanings, or that “terrible” expresses a very different sentiment.&lt;br&gt;
This is where word embeddings become useful.&lt;br&gt;
Word embeddings are a way of representing words as numerical vectors in a continuous mathematical space. Instead of treating every word as an unrelated symbol, embeddings allow words with similar meanings or usage patterns to have similar representations&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What Are Word Embeddings?&lt;/strong&gt;&lt;br&gt;
A word embedding is a numerical representation of a word as a vector of real numbers. In simple terms, it is a way of turning words into numbers while trying to preserve information about their meaning and how they are used.&lt;br&gt;
For example, imagine that we have these words:&lt;br&gt;
king&lt;br&gt;
queen&lt;br&gt;
man&lt;br&gt;
woman&lt;br&gt;
apple&lt;br&gt;
orange&lt;br&gt;
If we represented each word using a simple token IDs, we might have:&lt;br&gt;
king   → 1&lt;br&gt;
queen  → 2&lt;br&gt;
man    → 3&lt;br&gt;
woman  → 4&lt;br&gt;
apple  → 5&lt;br&gt;
orange → 6&lt;br&gt;
The problem is that these numbers do not tell us anything about the relationships between the words. The fact that “king” has ID 1 and “apple” has ID 5 does not mean anything useful to the model.&lt;br&gt;
Word embeddings work differently. Each word is represented by a vector containing many numerical values, for example:&lt;br&gt;
king  → [0.25, -0.13, 0.72, 0.41, ...]&lt;br&gt;
queen → [0.27, -0.11, 0.70, 0.45, ...]&lt;br&gt;
apple → [-0.62, 0.31, 0.08, -0.54, ...]&lt;br&gt;
These numbers are learned from text. Words that appear in similar contexts tend to develop similar vector representations.&lt;br&gt;
This is based on an important idea in NLP: a word's meaning can be understood partly from the words that appear around it.&lt;br&gt;
For example, words such as “doctor”, “hospital”, “patient”, and “medicine” may frequently occur in related contexts. An embedding model can learn representations that reflect some of these relationships.&lt;br&gt;
This is one of the major advantages of embeddings: instead of treating every word as completely independent, they give a model a way to capture relationships between words.&lt;br&gt;
&lt;strong&gt;Why Do Word Embeddings Matter?&lt;/strong&gt;&lt;br&gt;
Before embeddings, one common way to represent text was using approaches such as Bag-of-Words or TF-IDF.&lt;br&gt;
These methods can be useful, especially for tasks such as text classification. However, they generally represent words based on their occurrence in documents rather than learning a rich representation of their meaning.&lt;br&gt;
Consider these two sentences:&lt;br&gt;
The movie was excellent.&lt;br&gt;
The movie was amazing.&lt;br&gt;
A traditional representation can tell us that “excellent” and “amazing” are different words. However, it does not naturally understand that they express similar ideas.&lt;br&gt;
Embeddings can capture this kind of relationship because they learn from how words are used in context.&lt;br&gt;
This is particularly useful in NLP tasks such as:&lt;br&gt;
• Sentiment analysis&lt;br&gt;
• Text classification&lt;br&gt;
• Machine translation&lt;br&gt;
• Recommendation systems&lt;br&gt;
• Question answering&lt;br&gt;
• Named Entity Recognition&lt;br&gt;
• Text similarity&lt;br&gt;
For example, in my movie-review sentiment analysis project, the goal is to determine whether a review expresses a positive or negative opinion. Understanding relationships between words can be useful because different words and phrases can express similar sentiments.&lt;br&gt;
The important point is that embeddings do not simply give a word a number. They create a representation in which the relationships between vectors can contain useful information about language.&lt;br&gt;
&lt;strong&gt;How Does Word2Vec Work?&lt;/strong&gt;&lt;br&gt;
One of the most well-known methods for creating word embeddings is Word2Vec. Word2Vec was introduced by researchers at Google and became popular because it can learn useful relationships between words from large amounts of text.&lt;br&gt;
• Converts words into numerical vectors for machine learning models&lt;br&gt;
• Captures semantic relationships between words&lt;br&gt;
• Words with similar meanings have similar vector representations&lt;br&gt;
• Developed by Google researchers&lt;br&gt;
The main idea behind Word2Vec is simple: words that appear in similar contexts tend to have similar meanings or relationships.&lt;br&gt;
For example, consider these sentences:&lt;br&gt;
The cat drinks milk.&lt;br&gt;
The cat eats food.&lt;br&gt;
The dog drinks water.&lt;br&gt;
The dog eats food.&lt;br&gt;
The words “cat” and “dog” appear in similar contexts. They are often surrounded by words such as “eats”, “drinks”, “food”, and “water”. Because of these patterns, Word2Vec can learn vector representations for “cat” and “dog” that are relatively close to each other in the embedding space.&lt;br&gt;
Word2Vec mainly uses two approaches:&lt;br&gt;
&lt;strong&gt;1. CBOW — Continuous Bag of Words&lt;/strong&gt;&lt;br&gt;
CBOW tries to predict a target word from the words around it,the model  predicts the current word given context words within a specific window. The input layer contains the context words and the output layer contains the current word. The hidden layer contains the dimensions we want to represent the current word present at the output layer. &lt;br&gt;
For example, consider:&lt;br&gt;
The cat drinks milk.&lt;br&gt;
If the target word is “drinks”, the surrounding words can provide context:&lt;br&gt;
The cat ___ milk&lt;br&gt;
The model learns from many examples like this and gradually adjusts the word vectors so that they become useful for predicting words from their context.&lt;br&gt;
&lt;strong&gt;2. Skip-gram&lt;/strong&gt;&lt;br&gt;
Skip-gram works in the opposite direction. Instead of using surrounding words to predict the target word, it uses a target word to predict surrounding words.&lt;br&gt;
The Skip gram predicts the surrounding context words within specific window given current word. The input layer contains the current word and the output layer contains the context words. The hidden layer contains the number of dimensions in which we want to represent current word present at the input layer.&lt;br&gt;
For example:&lt;br&gt;
The cat drinks milk.&lt;br&gt;
If “cat” is the target word, the model tries to predict nearby words such as:&lt;br&gt;
the&lt;br&gt;
drinks&lt;br&gt;
By repeating this process across a large collection of text, the model learns vector representations for words.&lt;br&gt;
The difference can be summarized as:&lt;br&gt;
CBOW:&lt;br&gt;
Context words → Target word&lt;/p&gt;

&lt;p&gt;Skip-gram:&lt;br&gt;
Target word → Context words&lt;br&gt;
One of the interesting things about Word2Vec is that the resulting vectors can capture relationships between words. For example, with a suitable trained embedding, vector operations can produce relationships such as:&lt;br&gt;
king - man + woman ≈ queen&lt;br&gt;
This does not mean the model is performing human-like reasoning. Rather, the mathematical relationships between the learned vectors can reflect patterns that were present in the training data.&lt;br&gt;
This is what makes word embeddings different from simply assigning an ID to every word. The numbers in an embedding are learned from language patterns, and the distance and direction between vectors can contain useful information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Practical Word2Vec Experiment&lt;/strong&gt;&lt;br&gt;
To understand word embeddings beyond the theory, I decided to run a small experiment using the IMDB movie review dataset. Instead of using a pre-trained embedding, I trained my own Word2Vec model on the reviews.&lt;br&gt;
I first cleaned and tokenized the reviews so that each review became a list of words. I then used Gensim to train a Word2Vec model using the Skip-gram approach.&lt;/p&gt;

&lt;p&gt;import pandas as pd&lt;br&gt;
from gensim.models import Word2Vec&lt;br&gt;
import re&lt;/p&gt;

&lt;h1&gt;
  
  
  Load the dataset
&lt;/h1&gt;

&lt;p&gt;df = pd.read_csv(&lt;br&gt;
    "/kaggle/input/datasets/rehanliaqat17/imbd-dataset/IMDB_Dataset.csv"&lt;br&gt;
)&lt;/p&gt;

&lt;h1&gt;
  
  
  Clean and tokenize reviews
&lt;/h1&gt;

&lt;p&gt;def tokenize(text):&lt;br&gt;
    text = re.sub(r"&amp;lt;.*?&amp;gt;", "", text)&lt;br&gt;
    text = text.lower()&lt;br&gt;
    text = re.sub(r"[^a-z\s]", "", text)&lt;br&gt;
    return text.split()&lt;/p&gt;

&lt;p&gt;sentences = df["review"].apply(tokenize).tolist()&lt;/p&gt;

&lt;h1&gt;
  
  
  Train Word2Vec
&lt;/h1&gt;

&lt;p&gt;model = Word2Vec(&lt;br&gt;
    sentences=sentences,&lt;br&gt;
    vector_size=100,&lt;br&gt;
    window=5,&lt;br&gt;
    min_count=5,&lt;br&gt;
    workers=4,&lt;br&gt;
    sg=1&lt;br&gt;
)&lt;br&gt;
chose a vector size of 100, meaning that every word in the vocabulary is represented by a vector containing 100 numerical values.&lt;/p&gt;

&lt;p&gt;The window=5 parameter tells the model to look at nearby words within a context window of five words. I also used min_count=5, so words that appeared fewer than five times were ignored. Finally, sg=1 specifies that the model should use the Skip-gram architecture.&lt;/p&gt;

&lt;p&gt;Looking at a Word's Embedding&lt;/p&gt;

&lt;p&gt;After training the model, I could inspect the numerical representation learned for a word:&lt;/p&gt;

&lt;p&gt;model.wv["movie"]&lt;/p&gt;

&lt;p&gt;The result is a vector containing 100 numbers. The individual numbers do not have an obvious meaning by themselves. What matters is how this vector relates to the vectors of other words.&lt;/p&gt;

&lt;p&gt;Finding Similar Words&lt;/p&gt;

&lt;p&gt;I also used Word2Vec to find words that were close to "movie" in the learned embedding space:&lt;/p&gt;

&lt;p&gt;similar_words = model.wv.most_similar("movie", topn=10)&lt;/p&gt;

&lt;p&gt;similar_words&lt;/p&gt;

&lt;p&gt;The model returned a list of words together with similarity scores.&lt;/p&gt;

&lt;p&gt;My actual result was:&lt;/p&gt;

&lt;p&gt;[('film', 0.919495165348053),&lt;br&gt;
 ('movieand', 0.8412395715713501),&lt;br&gt;
 ('filmit', 0.8398792147636414),&lt;br&gt;
 ('filmbut', 0.835565447807312),&lt;br&gt;
 ('movieit', 0.8224337697029114),&lt;br&gt;
 ('moviebut', 0.8216363787651062),&lt;br&gt;
 ('filmand', 0.793904721736908),&lt;br&gt;
 ('filmso', 0.7863085269927979),&lt;br&gt;
 ('flick', 0.7821688652038574),&lt;br&gt;
 ('filmwhat', 0.7720096111297607)]&lt;/p&gt;

&lt;p&gt;This was interesting because the model was not given a dictionary telling it which words were related. It learned these relationships from the contexts in which words appeared throughout the reviews.&lt;/p&gt;

&lt;p&gt;** What I Learned**&lt;/p&gt;

&lt;p&gt;One thing I found surprising while working with Word2Vec was that the numbers in a word embedding do not have an obvious meaning by themselves. When I looked at the 100 numerical values representing a word such as &lt;code&gt;"movie"&lt;/code&gt;, I could not look at one number and say that it represented something specific like sentiment or meaning.&lt;/p&gt;

&lt;p&gt;Instead, the important information comes from the &lt;strong&gt;relationships between the vectors&lt;/strong&gt;. Words that are used in similar contexts can end up close to each other in the embedding space.&lt;/p&gt;

&lt;p&gt;I also found it interesting that Word2Vec could discover relationships between words simply by learning from the text. I did not manually tell the model that certain words were related. The model learned these patterns from the surrounding words in the movie reviews.&lt;/p&gt;

&lt;p&gt;One thing I struggled with at first was understanding how converting words into vectors could actually preserve information about language. I initially thought that turning a word into numbers would just give the model numerical data without much meaning. After working with &lt;code&gt;most_similar()&lt;/code&gt; and seeing the words that the model learned to associate with &lt;code&gt;"movie"&lt;/code&gt;, I understood better how the relationships between vectors can represent useful information.&lt;/p&gt;

&lt;p&gt;This experiment also helped me understand why word embeddings are important in NLP. They provide a way for machine learning models to work with text while preserving some of the relationships between words, rather than treating every word as completely unrelated.&lt;/p&gt;

&lt;p&gt;** References**&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Mikolov, T., Chen, K., Corrado, G., &amp;amp; Dean, J. (2013). &lt;em&gt;Efficient Estimation of Word Representations in Vector Space.&lt;/em&gt; This is the original Word2Vec paper describing the CBOW and Skip-gram approaches.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Gensim Documentation. &lt;em&gt;Word2Vec Model.&lt;/em&gt; Used as a reference for the Python implementation and methods such as &lt;code&gt;most_similar()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pennington, J., Socher, R., &amp;amp; Manning, C. D. (2014). &lt;em&gt;GloVe: Global Vectors for Word Representation.&lt;/em&gt; Stanford NLP. Used as a reference for the broader concept of word embeddings and vector representations.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
    </item>
  </channel>
</rss>
