<?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: CLAIRE</title>
    <description>The latest articles on DEV Community by CLAIRE (@claire_d06ed33be71378af3a).</description>
    <link>https://dev.to/claire_d06ed33be71378af3a</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%2F4151970%2Fe2dffe0e-0938-4788-95ed-e6b35e57d056.png</url>
      <title>DEV Community: CLAIRE</title>
      <link>https://dev.to/claire_d06ed33be71378af3a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/claire_d06ed33be71378af3a"/>
    <language>en</language>
    <item>
      <title>Choosing the Right AI Model for My FAQ Chatbot: From TF-IDF to Transformers</title>
      <dc:creator>CLAIRE</dc:creator>
      <pubDate>Mon, 26 Oct 2026 12:41:48 +0000</pubDate>
      <link>https://dev.to/claire_d06ed33be71378af3a/introduction-to-large-language-models-llms-what-i-learned-4pj8</link>
      <guid>https://dev.to/claire_d06ed33be71378af3a/introduction-to-large-language-models-llms-what-i-learned-4pj8</guid>
      <description>&lt;h1&gt;
  
  
  Choosing the Right AI Model for My FAQ Chatbot: From TF-IDF to Transformers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;description: "What I learned about LLMs and how I plan to improve my NLP FAQ chatbot by comparing traditional text matching, sentence embeddings, and open-weight language models."&lt;/p&gt;

&lt;p&gt;While studying Natural Language Processing (NLP), I learned that building a chatbot is not only about collecting questions and answers. It is also about helping a computer identify what a user means, even when the question is written differently from the examples it has seen.&lt;/p&gt;

&lt;p&gt;In my FAQ chatbot project, I explored TF-IDF and word embeddings as approaches to text representation. Learning about Large Language Models (LLMs) made me interested in finding a better way to handle questions that do not exactly match the wording stored in the system.&lt;/p&gt;

&lt;p&gt;This raised an important question for me: &lt;strong&gt;Should I improve my existing text-matching approach, use a sentence-embedding model, or introduce an LLM?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, I compare three approaches and explain which one I would test first.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Understanding the Problem
&lt;/h2&gt;

&lt;p&gt;Imagine that a chatbot's FAQ contains this question:&lt;/p&gt;

&lt;p&gt;"What are the requirements for registration?"&lt;/p&gt;

&lt;p&gt;A user might instead ask:&lt;/p&gt;

&lt;p&gt;"What do I need to sign up?"&lt;/p&gt;

&lt;p&gt;A basic keyword-matching system may struggle if the two questions use different words, even though their meanings are similar.&lt;/p&gt;

&lt;p&gt;This is where the difference between matching words and representing meaning becomes important.&lt;/p&gt;

&lt;p&gt;My goal is to make the chatbot identify relevant questions more reliably, return useful answers, and avoid generating information that is not supported by its FAQ data.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Three Approaches Worth Comparing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Approach 1: TF-IDF
&lt;/h3&gt;

&lt;p&gt;TF-IDF stands for Term Frequency–Inverse Document Frequency. It converts text into numerical features based on how important words are within a document and across a collection of documents.&lt;/p&gt;

&lt;p&gt;For an FAQ chatbot, it can help rank stored questions according to their similarity to a user's question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight and relatively simple to implement.&lt;/li&gt;
&lt;li&gt;Fast for small FAQ collections.&lt;/li&gt;
&lt;li&gt;Does not require a large language model.&lt;/li&gt;
&lt;li&gt;Provides a useful baseline for evaluating improvements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TF-IDF primarily represents word importance. Questions that use different vocabulary can receive low similarity scores even when their meanings are closely related.&lt;/p&gt;

&lt;p&gt;For example, "How can I register?" and "What is the sign-up procedure?" may be difficult to match if they share few important terms.&lt;/p&gt;

&lt;p&gt;TF-IDF is therefore a useful starting point, but I want to investigate whether semantic representations can improve the results.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction" rel="noopener noreferrer"&gt;Scikit-learn TF-IDF documentation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 2: Sentence Transformers
&lt;/h3&gt;

&lt;p&gt;Instead of relying mainly on individual word frequencies, sentence-embedding models convert entire sentences into numerical vectors that represent their semantic features.&lt;/p&gt;

&lt;p&gt;One model I would consider is &lt;strong&gt;sentence-transformers/all-MiniLM-L6-v2&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is designed to produce sentence and paragraph embeddings that can be compared using similarity measures such as cosine similarity. This makes it suitable for exploring semantic search in a question-and-answer system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can identify similarities between differently worded sentences.&lt;/li&gt;
&lt;li&gt;Suitable for searching a collection of stored FAQ questions.&lt;/li&gt;
&lt;li&gt;More lightweight than using a large generative language model.&lt;/li&gt;
&lt;li&gt;Can be used locally without sending every question to a paid API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similarity scores do not guarantee that two questions have exactly the same meaning.&lt;/li&gt;
&lt;li&gt;The model may not perform equally well across all languages and domains.&lt;/li&gt;
&lt;li&gt;The chatbot still needs a rule for deciding when a match is too weak to return.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these reasons, I see sentence embeddings as a promising way to improve question retrieval while keeping the system relatively simple.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://sbert.net/docs/sentence_transformer/pretrained_models.html" rel="noopener noreferrer"&gt;Sentence Transformers documentation&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach 3: Qwen3-4B-Instruct-2507
&lt;/h3&gt;

&lt;p&gt;The LLM lecture introduced me to open-weight models, including Qwen3-4B-Instruct-2507.&lt;/p&gt;

&lt;p&gt;Unlike TF-IDF and sentence-embedding models, this is a generative language model. It can follow instructions and generate responses based on the prompt and context it receives.&lt;/p&gt;

&lt;p&gt;Its model card lists 4 billion parameters and a native context length of 262,144 tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can generate answers in natural language.&lt;/li&gt;
&lt;li&gt;Can interpret questions using surrounding context.&lt;/li&gt;
&lt;li&gt;Can be instructed to answer using retrieved FAQ information.&lt;/li&gt;
&lt;li&gt;Can be run using tools such as Hugging Face Transformers, subject to available hardware and the model's licence.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires substantially more computing memory than a small embedding model.&lt;/li&gt;
&lt;li&gt;Can generate incorrect information if it is not properly constrained.&lt;/li&gt;
&lt;li&gt;A large context window does not guarantee correct answers.&lt;/li&gt;
&lt;li&gt;It may be unnecessarily complex for a small FAQ collection where retrieving an existing answer is sufficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For my project, I would not automatically replace the existing system with an LLM. I would first determine whether the main problem is retrieving the right FAQ or generating a more natural response.&lt;/p&gt;

&lt;p&gt;Reference: &lt;a href="https://huggingface.co/Qwen/Qwen3-4B-Instruct-2507" rel="noopener noreferrer"&gt;Qwen3-4B-Instruct-2507 model card&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Comparing the Approaches
&lt;/h2&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;TF-IDF&lt;/th&gt;
&lt;th&gt;Sentence Transformers&lt;/th&gt;
&lt;th&gt;Qwen3-4B-Instruct&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Main purpose&lt;/td&gt;
&lt;td&gt;Word-based matching&lt;/td&gt;
&lt;td&gt;Semantic similarity&lt;/td&gt;
&lt;td&gt;Text generation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Understands paraphrases&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;td&gt;Designed to capture semantic similarity&lt;/td&gt;
&lt;td&gt;Can interpret them, but results vary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Generates answers&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;Resource requirements&lt;/td&gt;
&lt;td&gt;Low&lt;/td&gt;
&lt;td&gt;Low to moderate&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Requires an LLM?&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;Best role in my chatbot&lt;/td&gt;
&lt;td&gt;Baseline retrieval&lt;/td&gt;
&lt;td&gt;Improved FAQ retrieval&lt;/td&gt;
&lt;td&gt;Context-based response generation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This comparison highlights an important lesson: these approaches do not perform exactly the same job. TF-IDF and sentence embeddings can retrieve relevant questions, while an LLM can generate a response from the information provided to it.&lt;/p&gt;

&lt;p&gt;A fair comparison should therefore evaluate each approach according to its intended role.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. My Choice: Sentence Transformers First
&lt;/h2&gt;

&lt;p&gt;For the first improvement to my FAQ chatbot, I would choose &lt;strong&gt;sentence-transformers/all-MiniLM-L6-v2&lt;/strong&gt; for semantic retrieval.&lt;/p&gt;

&lt;p&gt;My main reason is that the chatbot's first responsibility is to find the correct answer in its FAQ collection. If the system retrieves the wrong information, generating a fluent response will not solve the underlying problem.&lt;/p&gt;

&lt;p&gt;Sentence embeddings offer a practical middle ground between traditional keyword matching and a larger generative model.&lt;/p&gt;

&lt;p&gt;I would keep TF-IDF as a baseline so that I can measure whether the new approach actually improves retrieval. If semantic retrieval performs well, I could later experiment with Qwen3 to generate more conversational answers using the retrieved FAQ information.&lt;/p&gt;

&lt;p&gt;This is a proposed model choice, not a claim that I have already tested it or proven that it is the most accurate option for my dataset.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. How I Would Evaluate the Improvement
&lt;/h2&gt;

&lt;p&gt;Before deciding whether to keep the new model, I would prepare a small test dataset containing real or representative FAQ questions and their expected answers.&lt;/p&gt;

&lt;p&gt;I would then compare TF-IDF and sentence embeddings using the same questions.&lt;/p&gt;

&lt;p&gt;My evaluation plan would include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Correct retrieval:&lt;/strong&gt; Does the system retrieve the FAQ entry containing the expected answer?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paraphrase handling:&lt;/strong&gt; Can it match questions that express the same meaning using different words?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Response time:&lt;/strong&gt; How quickly does it return a result?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unanswered questions:&lt;/strong&gt; Does it avoid selecting an unrelated FAQ when no suitable answer exists?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource usage:&lt;/strong&gt; Can it run efficiently on the hardware available to me?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would record the results for both approaches instead of relying only on a few successful examples.&lt;/p&gt;

&lt;p&gt;If I later add Qwen3, I would also check whether its generated responses remain faithful to the retrieved FAQ content. The system should be able to say that it does not have enough information instead of inventing an answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. What I Learned About LLMs
&lt;/h2&gt;

&lt;p&gt;Studying LLMs helped me understand that these systems process text through tokenization, embeddings, Transformer layers, and next-token prediction.&lt;/p&gt;

&lt;p&gt;I also learned that model size, context windows, decoding settings, and computational resources influence how a model can be used.&lt;/p&gt;

&lt;p&gt;Most importantly, I learned that &lt;strong&gt;the most powerful model is not always the best model for a particular problem&lt;/strong&gt;. The right choice depends on the task, the available data, the required accuracy, and the resources available.&lt;/p&gt;

&lt;p&gt;For my FAQ chatbot, improving retrieval may be more valuable as a first step than immediately introducing a large generative model.&lt;/p&gt;

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

&lt;p&gt;My exploration of TF-IDF, sentence embeddings, and LLMs has helped me think more carefully about how to improve my FAQ chatbot.&lt;/p&gt;

&lt;p&gt;TF-IDF provides a simple baseline, Sentence Transformers offer a promising way to retrieve questions based on semantic similarity, and Qwen3 provides an option for generating more natural responses when needed.&lt;/p&gt;

&lt;p&gt;My next step is to compare TF-IDF with sentence embeddings using the same test questions and measure the results. I would then decide whether adding an LLM provides enough value to justify its additional complexity.&lt;/p&gt;

&lt;p&gt;For me, this is the most useful lesson from studying NLP: choosing a model should begin with understanding the problem, not simply selecting the largest or newest technology.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction" rel="noopener noreferrer"&gt;Scikit-learn: Text Feature Extraction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sbert.net/docs/sentence_transformer/pretrained_models.html" rel="noopener noreferrer"&gt;Sentence Transformers: Pretrained Models&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://sbert.net/docs/sentence_transformer/usage/semantic_textual_similarity.html" rel="noopener noreferrer"&gt;Sentence Transformers: Semantic Similarity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://huggingface.co/Qwen/Qwen3-4B-Instruct-2507" rel="noopener noreferrer"&gt;Qwen3-4B-Instruct-2507: Model Card&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  AI #NLP #MachineLearning #Python #LearningInPublic
&lt;/h1&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>nlp</category>
    </item>
    <item>
      <title>How Machines Learn to See: A Beginner's Intro to Computer Vision (with Python)</title>
      <dc:creator>CLAIRE</dc:creator>
      <pubDate>Tue, 13 Oct 2026 11:42:40 +0000</pubDate>
      <link>https://dev.to/claire_d06ed33be71378af3a/how-machines-learn-to-see-a-beginners-intro-to-computer-vision-with-python-2502</link>
      <guid>https://dev.to/claire_d06ed33be71378af3a/how-machines-learn-to-see-a-beginners-intro-to-computer-vision-with-python-2502</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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0rfvo09ple0gd87hhns4.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.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0rfvo09ple0gd87hhns4.png" alt=" " width="799" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;u look at a photo of a street and instantly see a bus, some people, maybe a lamp post. It takes you a fraction of a second.&lt;/p&gt;

&lt;p&gt;A computer gets something very different: &lt;strong&gt;a grid of numbers&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this post I'll walk through the core ideas of computer vision (CV) from the first day of the kLab TekHer AI Program (Cohort 2). We follow one photo from raw numbers all the way to a model that can say what is in it, and where. Everything runs in Google Colab, and a CPU is enough.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does the computer actually see?
&lt;/h2&gt;

&lt;p&gt;Let's load a photo and look at what the computer receives:&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;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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;ultralytics.utils&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ASSETS&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;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;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="n"&gt;ASSETS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bus.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="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;Shape (height, width, channels):&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&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 numbers in this image:&lt;/span&gt;&lt;span class="sh"&gt;"&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;img&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&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="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 photo is &lt;strong&gt;1080 rows x 810 columns&lt;/strong&gt; of pixels, and each pixel has &lt;strong&gt;3 numbers&lt;/strong&gt; (red, green, blue). That is 1080 x 810 x 3 = &lt;strong&gt;2,624,400 numbers&lt;/strong&gt;. The computer never sees "a bus", only these numbers.&lt;/p&gt;

&lt;p&gt;A few things worth remembering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each value is a &lt;code&gt;uint8&lt;/code&gt;: a whole number from &lt;strong&gt;0 to 255&lt;/strong&gt; (8 bits give 2^8 = 256 values). 0 means none of that colour, 255 means full.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;shape&lt;/code&gt;, &lt;strong&gt;height comes first, then width&lt;/strong&gt;. Rows go down (y), columns go across (x).&lt;/li&gt;
&lt;li&gt;A colour image is really &lt;strong&gt;three grids stacked together&lt;/strong&gt;: one each for red, green and blue. That shape, height x width x 3, is called a &lt;em&gt;tensor&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The three questions computer vision answers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;What is in the image?&lt;/strong&gt; A cat, a cassava leaf.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Where is it?&lt;/strong&gt; Which pixels, which part of the image.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is happening?&lt;/strong&gt; A person crossing the road.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These map to five core tasks:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Classification&lt;/td&gt;
&lt;td&gt;One label for the whole image&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Detection&lt;/td&gt;
&lt;td&gt;A box around each object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Segmentation&lt;/td&gt;
&lt;td&gt;A label for every pixel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OCR&lt;/td&gt;
&lt;td&gt;Reads text in images&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tracking&lt;/td&gt;
&lt;td&gt;Follows objects across video&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Filters: finding edges by hand
&lt;/h2&gt;

&lt;p&gt;Before deep learning, we could already find patterns in images using a &lt;strong&gt;filter&lt;/strong&gt; (or kernel): a small grid of weights that slides across the image. Each output pixel is a weighted sum of its neighbours.&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&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="n"&gt;sobel_x&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="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="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="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="mi"&gt;0&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="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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
&lt;span class="n"&gt;sobel_y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sobel_x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;blur&lt;/code&gt; averages 25 neighbours, so fine detail fades.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sobel_x&lt;/code&gt; computes &lt;em&gt;right side minus left side&lt;/em&gt;, so it finds &lt;strong&gt;vertical edges&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sobel_y&lt;/code&gt; (the same filter flipped with &lt;code&gt;.T&lt;/code&gt;) computes &lt;em&gt;bottom minus top&lt;/em&gt;, so it finds &lt;strong&gt;horizontal edges&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why does &lt;code&gt;sobel_x&lt;/code&gt; work? Try it on two small patches:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Patch&lt;/th&gt;
&lt;th&gt;Left side&lt;/th&gt;
&lt;th&gt;Right side&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Flat (every pixel = 100)&lt;/td&gt;
&lt;td&gt;100 x (-1 -2 -1) = -400&lt;/td&gt;
&lt;td&gt;100 x (1 + 2 + 1) = +400&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;0&lt;/strong&gt;, no edge&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Edge (left = 0, right = 200)&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;200 x (1 + 2 + 1) = +800&lt;/td&gt;
&lt;td&gt;
&lt;strong&gt;800&lt;/strong&gt;, an edge&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same brightness on both sides gives zero. Dark on the left and bright on the right gives a big number. &lt;strong&gt;Big result = brightness changes = edge.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is the function that slides the kernel over a grayscale image:&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;def&lt;/span&gt; &lt;span class="nf"&gt;convolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kernel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kernel&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="n"&gt;pad&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="n"&gt;padded&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;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;edge&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;out&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;zeros_like&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dtype&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;float&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;dy&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&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;dx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
            &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;kernel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;padded&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;dy&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;gray&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="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;gray&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt;

&lt;span class="n"&gt;gray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;img&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="n"&gt;axis&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="n"&gt;edges&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;hypot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sobel_x&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;convolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sobel_y&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;np.hypot&lt;/code&gt; combines both directions like Pythagoras, so we get edge strength whichever way the edge points.&lt;/p&gt;

&lt;h3&gt;
  
  
  So what does a CNN do?
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;Convolutional Neural Network (CNN)&lt;/strong&gt; does exactly this, with one big difference: &lt;strong&gt;we don't choose the filters. Training learns them&lt;/strong&gt; from thousands of labelled images. Early layers learn edges, later layers learn textures, and the last layers learn whole objects:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pixels -&amp;gt; Edges -&amp;gt; Textures -&amp;gt; Objects&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Classify, detect, segment in a few lines
&lt;/h2&gt;

&lt;p&gt;We use &lt;a href="https://docs.ultralytics.com/" rel="noopener noreferrer"&gt;Ultralytics YOLO&lt;/a&gt;, which ships small pretrained models.&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="err"&gt;!&lt;/span&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="n"&gt;ultralytics&lt;/span&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1. Classification: one label for the whole image
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&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="n"&gt;r&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="n"&gt;ASSETS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bus.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="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;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="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;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="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;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="si"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;6.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 model answers &lt;strong&gt;minibus (57%)&lt;/strong&gt;, with &lt;strong&gt;police_van (34%)&lt;/strong&gt; in second place. It was trained on ImageNet's 1,000 categories, and there is no class called "street scene", so it picks the closest thing it knows. Notice that high confidence does not mean correct. It only means the model is sure.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Detection: what and where
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&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="n"&gt;r&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="n"&gt;ASSETS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bus.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;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="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;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="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;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="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="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="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="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&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="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="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get a bounding box for each object: one bus (94% confidence) and four people. The &lt;code&gt;conf&lt;/code&gt; threshold is &lt;em&gt;your&lt;/em&gt; choice. Lower it and you get more boxes but more mistakes. Raise it and you get fewer boxes you can trust more. It is the same trade-off as medical screening, where a low threshold misses fewer real cases but raises more false alarms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is YOLO fast?&lt;/strong&gt; YOLO stands for &lt;em&gt;You Only Look Once&lt;/em&gt;. Older detectors first proposed many regions and then classified each one separately. YOLO makes one pass over the whole image and predicts every box and label at once, which is fast enough for live video.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Segmentation: which pixels belong to which object
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&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="n"&gt;r&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="n"&gt;ASSETS&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bus.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;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="mi"&gt;0&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="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 &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 image&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;Each object gets a &lt;strong&gt;mask&lt;/strong&gt;: a grid of 0s and 1s where 1 means "this pixel belongs to the object". The mean of a grid of 0s and 1s is the fraction of 1s, so one line tells us objects cover 44% of the image. The same trick measures building area in satellite images or a tumour in a scan.&lt;/p&gt;

&lt;h2&gt;
  
  
  When models fail
&lt;/h2&gt;

&lt;p&gt;A model only knows what it was trained on. I made the same photo harder in three ways that are common with cheap phones, poor lighting or dusty lenses:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Change&lt;/th&gt;
&lt;th&gt;What happened&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Original&lt;/td&gt;
&lt;td&gt;1 bus, 4 people&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Very dark&lt;/td&gt;
&lt;td&gt;Adds a fake skateboard&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Upside down&lt;/td&gt;
&lt;td&gt;Misses the bus&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Low resolution&lt;/td&gt;
&lt;td&gt;Finds nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Same scene, same model, very different answers. Most of these models were trained mostly on photos from Europe, North America and Asia, so it is worth asking where they might fail on African images: crops, clothing, roads, skin tones, lighting. &lt;strong&gt;Local data matters.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Computer vision across Africa
&lt;/h2&gt;

&lt;p&gt;This is not just a lab exercise. CV is already being used here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Agriculture:&lt;/strong&gt; Nuru diagnoses crop disease from a photo of a cassava leaf, offline, on a basic phone (East Africa).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Health:&lt;/strong&gt; Software scored X-rays to find TB in 5,298 people screened in Gombe and Adamawa, Nigeria. High scores got a lab test, and 85 people with TB were found.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conservation:&lt;/strong&gt; Smart camera traps in Gabon spot elephants and people, then alert rangers by satellite within minutes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finance:&lt;/strong&gt; Smile Identity matches a selfie to an ID photo and checks it is a real, live person, with over 1 million identity checks a month across Africa (2021).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mapping:&lt;/strong&gt; Google's Open Buildings project outlined 1.8 billion building footprints from satellite images, used for vaccination planning and disaster response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accessibility:&lt;/strong&gt; BLAIVA, built by FabLab Rwanda in Kigali and in beta testing, is a wearable camera that describes objects, text and faces through earphones.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each one, ask three questions: &lt;em&gt;What goes in? What comes out? Who is harmed if it fails?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why now?
&lt;/h2&gt;

&lt;p&gt;CV has taken off because several things arrived together: cameras everywhere (every phone is a camera and a computer), huge image datasets, cheap cloud GPUs, pretrained models you can adapt with a few thousand local photos, and multimodal AI that reads images as well as text. There is even a link to NLP: a Vision Transformer cuts an image into 16 x 16 patches and treats each patch like a word.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools to start with
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PyTorch&lt;/strong&gt;: build and train your own networks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;TensorFlow / Keras&lt;/strong&gt;: Google's alternative, common in mobile apps&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hugging Face&lt;/strong&gt;: download pretrained models and fine-tune them&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ultralytics YOLO&lt;/strong&gt;: ready-made detection, used in this post&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Images are numbers: height x width x 3.&lt;/li&gt;
&lt;li&gt;Filters find patterns, and CNNs learn them.&lt;/li&gt;
&lt;li&gt;Classify, detect, segment: three different questions about the same image.&lt;/li&gt;
&lt;li&gt;Models fail on data unlike their training data, so local data matters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Your turn:&lt;/strong&gt; try a photo of a street, a market or a crop leaf. Find one hit and one miss, and think about what data you would need to fix the miss.&lt;/p&gt;

&lt;p&gt;Next up for me: datasets, CNNs in PyTorch, augmentation and metrics, and building an image classifier with an honest test set. I'll share what I learn.&lt;/p&gt;

&lt;p&gt;If you've tried computer vision on local images, I'd love to hear how it went in the comments!&lt;/p&gt;

&lt;p&gt;You can find more of my front-end and portfolio projects on &lt;a href="https://github.com/Niyigenaclaire" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>computervision</category>
      <category>ai</category>
      <category>womenintech</category>
      <category>rwanda</category>
    </item>
    <item>
      <title>Cracking Word Embeddings: Why One-Hot Fails and How Word2Vec Actually Works</title>
      <dc:creator>CLAIRE</dc:creator>
      <pubDate>Wed, 30 Sep 2026 13:03:43 +0000</pubDate>
      <link>https://dev.to/claire_d06ed33be71378af3a/cracking-word-embeddings-why-one-hot-fails-and-how-word2vec-actually-works-54h3</link>
      <guid>https://dev.to/claire_d06ed33be71378af3a/cracking-word-embeddings-why-one-hot-fails-and-how-word2vec-actually-works-54h3</guid>
      <description>&lt;p&gt;Building natural language processing models always brings you face-to-face with a core challenge: computers have no clue what words actually mean. Feed a machine a sentence like "the programmer writes code," and it sees nothing. Computers don't process text natively; they need numbers.&lt;/p&gt;

&lt;p&gt;For a long time, traditional strategies like bag-of-words or TF-IDF handled this conversion. But when you look closely at how they represent text, you realize they hit a hard ceiling when it comes to capturing actual meaning. That's where word embeddings and the &lt;code&gt;word2vec&lt;/code&gt; framework change the game. &lt;/p&gt;

&lt;p&gt;Let's break down why traditional approaches fall short and how self-supervised vector representations solve the problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why One-Hot Vectors Fall Short&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A straightforward way to numericize text is using one-hot encoding. If your vocabulary contains a specific number of unique words, say a dictionary size of $\vert{}V\vert{}$, you assign each word an integer index from $0$ to $\vert{}V\vert{}-1$. To represent any specific word, you build a vector of length $\vert{}V\vert{}$ filled entirely with zeros, except for a single &lt;code&gt;1&lt;/code&gt; placed at that word's index.&lt;/p&gt;

&lt;p&gt;While these vectors are simple to set up, they are a terrible choice for machine learning models. The biggest issue is that they &lt;strong&gt;cannot capture semantic similarity&lt;/strong&gt;. If you measure the cosine similarity between two different one-hot vectors, the result is always zero. &lt;/p&gt;

&lt;p&gt;Because every orthogonal vector is completely independent, a one-hot encoder treats words like "cat" and "dog" as just as distant from each other as "cat" and "refrigerator." They completely fail to encode relationships or shared meanings.&lt;/p&gt;




&lt;h2&gt;
  
  
  Self-Supervised Word2Vec
&lt;/h2&gt;

&lt;p&gt;To fix this, researchers introduced &lt;code&gt;word2vec&lt;/code&gt;, mapping each word to a &lt;strong&gt;dense, fixed-length vector&lt;/strong&gt; that captures semantic similarity and analogies. &lt;/p&gt;

&lt;p&gt;The clever part about &lt;code&gt;word2vec&lt;/code&gt; is that it relies on &lt;strong&gt;self-supervised learning&lt;/strong&gt;. Instead of manual data labeling, the model extracts supervision straight from raw text by predicting words based on their surrounding context. It splits into two primary model architectures: &lt;strong&gt;Skip-Gram&lt;/strong&gt; and &lt;strong&gt;Continuous Bag of Words (CBOW)&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Skip-Gram Model
&lt;/h2&gt;

&lt;p&gt;The skip-gram architecture operates on the assumption that a central target word can generate its surrounding context words in a sequence. &lt;/p&gt;

&lt;p&gt;Take the text sequence: &lt;code&gt;"the", "man", "loves", "his", "son"&lt;/code&gt;. If we select &lt;code&gt;"loves"&lt;/code&gt; as our center word and set a context window size of 2, skip-gram looks at the conditional probability of generating the words that appear no more than two steps away: &lt;code&gt;"the"&lt;/code&gt;, &lt;code&gt;"man"&lt;/code&gt;, &lt;code&gt;"his"&lt;/code&gt;, and &lt;code&gt;"son"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this model, every word gets &lt;strong&gt;two separate vector representations&lt;/strong&gt;: one used when it acts as a center word, and another used when it acts as a context word. During training, we maximize the likelihood function using stochastic gradient descent. Once trained, the center word vectors are typically pulled out and used as the final word embeddings for downstream tasks.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Continuous Bag of Words (CBOW) Model
&lt;/h2&gt;

&lt;p&gt;The continuous bag of words (CBOW) model flips the script. Instead of predicting context from a center word, CBOW assumes that &lt;strong&gt;a center word is generated based on its surrounding context words&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Using that same sequence (&lt;code&gt;"the", "man", "loves", "his", "son"&lt;/code&gt;), with &lt;code&gt;"loves"&lt;/code&gt; as the center word and a window size of 2, CBOW takes the surrounding context words (&lt;code&gt;"the"&lt;/code&gt;, &lt;code&gt;"man"&lt;/code&gt;, &lt;code&gt;"his"&lt;/code&gt;, &lt;code&gt;"son"&lt;/code&gt;) and averages their vectors to predict the target center word. &lt;/p&gt;

&lt;p&gt;While training follows a similar gradient optimization process to skip-gram, CBOW typically uses the &lt;strong&gt;context word vectors&lt;/strong&gt; as the final word representations.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Moving away from sparse frequency matrices to dense vector representations completely transforms how text data is handled in machine learning. By mapping words into a continuous vector space where geometric distance reflects semantic meaning, models can finally understand relationships, synonyms, and context.&lt;/p&gt;




&lt;h2&gt;
  
  
  References &amp;amp; Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dive into Deep Learning (D2L.ai): Chapter 15.1 – &lt;em&gt;Word Embedding (word2vec)&lt;/em&gt;. Available at &lt;a href="https://d2l.ai/chapter_natural-language-processing-pretraining/word2vec.html" rel="noopener noreferrer"&gt;d2l.ai&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Mikolov, T., et al. (2013): &lt;em&gt;Efficient Estimation of Word Representations in Vector Space.&lt;/em&gt; arXiv preprint arXiv:1301.3781.&lt;/li&gt;
&lt;li&gt;Mikolov, T., et al. (2013): &lt;em&gt;Distributed Representations of Words and Phrases and their Compositionality.&lt;/em&gt; Advances in Neural Information Processing Systems (NeurIPS).&lt;/li&gt;
&lt;/ul&gt;




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