Part 5: Neo-ML: Two Ways to Recognise a Flower – An Introduction to Convolutional Neural Networks (CNNs)

A ruler and two rules, or four million numbers and a camera

I have now built a flower classifier twice.

The first time was while I was studying machine learning at Imperial College London. The model was a handful of lines of code, trained in a blink, and you could read its entire reasoning aloud in a few seconds.

The second time was this week. A convolutional neural network, nearly four million adjustable numbers, a few thousand photos. For fun, I showed it a picture of my dog and it told me, with 50% confidence, she was a tulip. I don’t see the likeness. For fun, an impression of what my CNN sees is shown below. I find it a bit creepy.

What my CNN say with 50% confidence, very disturbing...

Both models do the same job on paper: look at a flower, classify it. But they go about it in completely different ways, and the difference tells you most of what you need to know about choosing a model for real AI work. So let’s put them side by side, code and all.

Model one: measure first, then decide

The Iris dataset is the “hello world” of machine learning. It holds 150 iris flowers, 50 from each of three species: setosa, versicolor and virginica. For each flower, a botanist took four measurements with a ruler: sepal length, sepal width, petal length and petal width. The statistician Ronald Fisher made the dataset famous in 1936.

Notice what the model is given. Not a photo. Just four numbers per flower.

Here is the whole model, using a decision tree from the scikit-learn library.

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier, export_text

iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42, stratify=iris.target)

model = DecisionTreeClassifier(max_depth=2, random_state=42)
model.fit(X_train, y_train)

print(export_text(model, feature_names=iris.feature_names))
print(f”Test accuracy: {model.score(X_test, y_test):.1%}”)

Line by line:

  • load_iris() loads the 150 flowers and their species labels.
  • train_test_split holds back 20% of the flowers (30 of them) as test data the model never sees during training.
  • DecisionTreeClassifier(max_depth=2) asks for a tree that may ask at most two questions in a row.
  • fit does the learning. It searches for the measurement and the cut-off value that best separate the species.
  • export_text prints what the model learned, in plain text.

And this is what it prints:

|— petal length (cm) <= 2.45
|   |— class: 0
|— petal length (cm) >  2.45
|   |— petal width (cm) <= 1.65
|   |   |— class: 1
|   |— petal width (cm) >  1.65
|   |   |— class: 2

Test accuracy: 93.3%

That is the entire model. In English:

  1. If the petal is 2.45 cm long or less, it’s a setosa (class 0).
  2. Otherwise, if the petal is 1.65 cm wide or less, it’s a versicolor (class 1).
  3. Otherwise it’s a virginica (class 2).

Two rules, and it gets 28 of the 30 unseen flowers right. The first rule never misses, because setosa petals are much smaller than the others. The mistakes all come from versicolor and virginica, whose petal sizes overlap a little. The model also ignored the sepals completely. It worked out for itself that two of the four measurements were enough.

One honest footnote: the exact cut-off moves slightly depending on which flowers land in the training set. Train on all 150 and the second rule becomes 1.75 cm. The shape of the answer stays the same.

Model two: look, and learn what matters

Now take away the botanist and the ruler. All we have is photographs: 3,670 of them, sorted into five folders for daisies, dandelions, roses, sunflowers and tulips. And it’s the folder names that act as the image labels.

A photo, to a computer, is a grid of numbers. Each image here is resized to 180 by 180 pixels with three colour values per pixel. That is 97,200 numbers per flower, against the Iris model’s four. And no single one of those numbers means anything. Pixel 4,012 being a bit red tells you nothing about roses.

A close-up image of two white roses with yellow centers, surrounded by green foliage, alongside a color palette showing various shades of yellow and white.

So the model has to do two jobs. First work out what to look for, then decide. The tool for that is a convolutional neural network, or CNN.

A short detour: what is a CNN?

In 2012, a small team from the University of Toronto entered the ImageNet challenge, an annual competition in which software had to recognise objects in photographs across 1,000 categories, learning from about 1.2 million labelled images. Alex Krizhevsky, Ilya Sutskever and their supervisor Geoffrey Hinton submitted a CNN that became known as AlexNet.

It didn’t just win. Its error rate was about 15%, against about 26% for the runner-up. In a contest where teams usually fought over a point or two, that was a different league, and within a couple of years almost everyone in the field had switched to the same approach. The current AI boom is usually dated from that result.

The idea itself wasn’t new. Yann LeCun had CNNs reading handwritten digits on bank cheques in the 1990s. What changed by 2012 was a huge labelled dataset and GPUs fast enough to train on it. The breakthrough was as much about data and hardware as about the model.

So what is it? A CNN is a neural network built for images, and it rests on three ideas.

  1. Filters. A filter is a tiny window, often 3 by 3 pixels, that slides across the whole image looking for one simple pattern, such as a vertical edge. Wherever it finds the pattern, it lights up. The sliding is the “convolution” in the name.
  2. Layers. Filters are stacked in layers, and each layer looks at the output of the one before. The first finds edges and colours. The next combines edges into textures and curves. The next combines those into parts, such as a petal. It is a little like reading: strokes make letters, letters make words, words make meaning.
  3. Learning. Nobody designs the filters. They start as random numbers, and training nudges them, photo by photo, until they detect patterns that help tell the classes apart.

That third point is the big one. In the Iris model a botanist chose what to measure. In a CNN, the filters are the measurements, and the network works them out for itself.

Here is mine, written with Keras.

model = Sequential([
  layers.Input(shape=(180, 180, 3)),
  layers.Rescaling(1./255),
  layers.Conv2D(16, 3, padding=’same’, activation=’relu’),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding=’same’, activation=’relu’),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding=’same’, activation=’relu’),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation=’relu’),
  layers.Dense(5)
])

Read it top to bottom, because that is the route a photo takes.

  • Input and Rescaling take the photo in and shrink the pixel values from 0 to 255 down to 0 to 1, which networks learn from more easily.
  • Each Conv2D layer slides a set of small 3 by 3 windows, called filters, over the image. Each filter hunts for one pattern. The first layer finds edges and patches of colour, the second combines those into textures and curves, the third into things like petals and flower centres.
  • Each MaxPooling2D halves the image size, keeping only the strongest signals.
  • Flatten and the two Dense layers weigh up everything that was found and produce five scores, one per flower. Highest score wins.

Here is the part that matters for this comparison. Nowhere in that code is there a word about petals, colours or stems. The code only describes the shape of the model. Everything it knows about flowers is learned during training and stored as 3,989,285 numbers.

The convolution layers are doing the botanist’s job. They are deciding what is worth measuring.

For the curious: counting the parameters

Keras will count the numbers for you. Run model.summary() and the last lines read:

 Total params: 3,989,285 (15.22 MB)
 Trainable params: 3,989,285 (15.22 MB)
 Non-trainable params: 0 (0.00 B)

  • Total params is every adjustable number in the model.
  • Trainable params are the ones training is allowed to change. Here that is all of them.
  • Non-trainable params are numbers that are locked. There are none, because this model was trained from scratch.
  • 15.22 MB is the memory they take up. Each parameter is stored in four bytes.

Where do they all live? Not where you might expect.

LayerParameters
Conv2D, 16 filters448
Conv2D, 32 filters4,640
Conv2D, 64 filters18,496
Dense, 128 units3,965,056
Dense, 5 outputs645

The three convolution layers, which do all the seeing, account for well under 1% of the total. Over 99% sit in one dense layer that weighs up the evidence. Filters are cheap because the same small window is reused across the whole image.

And for all that, four million parameters is small by today’s standards. No GPU necessary: this model trained on my laptop.

It works, up to a point. The first version of this model typically reaches around 60% on photos it hasn’t seen, and it improves once you add a couple of standard fixes for overfitting. But ask it why it chose “rose” and all it can give you is a confidence score. And because it only knows five answers, everything in the world is one of five flowers. Including my dog now nicknamed Tulip.

Side by side

 Iris decision treeFlower CNN
What goes in4 measurements taken by a person97,200 raw pixel values
Who chose what to measureA botanistThe network, during training
Training examples120 flowers2,936 photos
What the model stores2 rules3,989,285 numbers
HardwareAny laptop, instantlyA laptop, more slowly; no GPU needed at this size
Explanation for an answer“The petal is under 2.45 cm”A confidence score
Works on a raw photoNoYes

This is not a fair fight, and it isn’t meant to be. They are different datasets and different problems: three species against five, measurements against photos. The accuracy figures can’t be compared.

What can be compared is where the hard work happens. With Iris, a human had already done the difficult part by deciding that petal size mattered and measuring it. The model only had to draw two lines. With the CNN, nobody told it where to look, so it had to learn that too. That is why it needs twenty-four times the examples and millions of times the parameters.

Pros and cons

The rule-based model

For

  • You can read it. A rejected item comes with a reason a person can check.
  • It is easy to verify. It’s just measurements and maths, so anyone with a ruler and the two rules can confirm an answer by hand.
  • It needs very little data. 120 examples were plenty.
  • It trains in an instant and runs on anything.
  • There is very little to go wrong, and when it does go wrong you can see where.

Against

  • Somebody has to measure the right things first. On a production line that means a sensor, a calibrated camera or a person with a ruler.
  • It only works when the decision can be written as rules on those measurements.
  • It can’t cope with raw, messy input such as a photo taken at an odd angle in poor light.

The CNN

For

  • It works directly on images. No one has to decide in advance what to measure.
  • It handles variety that no rulebook could cover: angle, lighting, background, shape.
  • The same approach transfers to other visual problems, from cracked bottles to medical scans.

Against

  • It is hungry for labelled examples, and labelling is slow, costly work.
  • It needs far more compute to train. This small one ran on a laptop, but larger models need a GPU.
  • It can’t explain itself beyond a score, which matters when someone asks “why was this rejected?”
  • It fails in strange ways, and confidently. See: dog, tulip.
  • It needs watching after it goes live, because the world it was trained on keeps changing.

So which one is better?

Neither. It’s the wrong question.

The right question is: can you measure the thing that matters?

If you can, and the decision can be written as a rule, use the rule. It is cheaper to build, cheaper to run, and it can explain itself. A fill level on a bottling line is a height against a mark. You don’t need four million parameters to read a ruler.

If you can’t, because what matters is buried in pixels and varies every time, that is where a network earns its keep. Nobody can write a rule for “looks like a rose” or “looks cracked”.

Plenty of real systems use both. A camera and some image processing take the measurement, and a simple rule makes the call. Deep learning comes in only for the part that won’t reduce to a measurement.

AI wasn’t born from deep learning, and sledgehammers don’t crack nuts. The Iris dataset is ninety years old and a two-rule tree is still the right answer to its own problem. The best model isn’t the newest or the biggest. It’s the one that works for your use case.

Computer Vision: Just Point a Camera at It. Right?

Over the weekend my girlfriend was busy. So what’s a guy to do? This guy built a CNN image recognition system and trained it on five types of flower. With over 3,000 flower images, it worked. Point it at a sunflower and yes, it predicted a sunflower. Same with roses, tulips, dandelions and daises.

But what flower is my dog? Yes, I pointed my model at an image of my dog sat upright. The model predicted with 49% confidence my dog is a tulip. My model isn’t broken. It only knows 5 answers, so everything in its world is one of five flowers. It answered the question it was built for, not the one I asked. This is the problem Marco has just brought home to Castellan Wines – our fictitious winery.

Diagram illustrating a quality control system for bottles on a conveyor belt, showing inspection points for fill level, chips, cracks, contamination, and crooked labels with data on good and defective items.

“It all just works”

Castellan Wines has a good problem. Sales have grown faster than the bottling hall, and the manual inspection that coped with a quieter line no longer does. It is still done by eye. It is slow, it varies by shift, and a bad bottle reaching a retailer costs far more than the bottle is worth.

CEO Marco has seen the answer. He came back from an AI conference with a tote bag and a conviction: you point cameras at the bottling line, the AI learns what a good bottle looks like, and it all just works. He watched it happen on a stand. And he was filled with the promise of AI.

The demo was real. It was also a demo: one bottle shape, perfect lighting, and defects chosen to be found.

So Clare has a one-line brief, “build me a defect detection model”. Her harder job, is to deliver AI Reality. She is Castellan’s data scientist, and she is honest about being new to computer vision and convolutional neural networks (CNNs). So she does the sensible thing. She experiments first and writes the blueprint second, and she organises both around three questions.

Question one: what decision are we supporting?

Marco’s brief is one model, pass or fail. Clare spends a morning on the line with the bottling line inspectors and writes down what they check.

They look for damage: is there a chip or a crack, and where? They check the fill level, which is a measurement. They check the cap is sitting square. And they check the label: the right one, straight and readable, which is reading text and checking position.

Four different jobs. Roll them into one pass/fail and QA gets a red light with no idea why.

Then there’s the camera. Clare props her iPhone beside the line and films a few minutes. Cracks show up reasonably well. The fill line doesn’t. Through dark, reflective glass, reflections can move the apparent liquid line more than any real variation does.

A setup good enough to spot a cracked bottle is not automatically good enough to measure one. I wrote about this in Now You See It, Now You Don’t: a model can only work with what the camera lets it see.

And as for the data, which is Charlie’s department. Castellan has a year of QA logs and almost no labelled images. Defects are rare, which is good news for the winery and awkward for anyone training a computer vision model.

So the first page of the blueprint doesn’t say “quality model”. It says four tasks, each with its own evidence, its own camera requirement, and its own answer.

Question two: do we need machine learning at all?

Here’s the answer nobody puts on a conference slide. For fill level and cap alignment, no machine learning.

Clare gets there by experiment. Her first instinct is Marco’s: collect images, train a network. Then she looks at what she would be asking it. Is the liquid line above or below a known mark? Is the cap sitting square?

Those are questions about where an edge is, and engineers have had reliable ways of finding edges in images for decades.

So she builds a short, old-fashioned pipeline in an afternoon. Capture the frame, remove the noise, boost the contrast, find the edges, compare the liquid line against a calibrated reference, and pass or fail.

The order matters. On her first attempt she boosts the contrast before removing the noise, and amplifies the very speckle she wanted rid of.

No training data. No GPU. Nothing to retrain. And when a bottle is rejected, the reason is a measurement a supervisor can check with a ruler, not a confidence score.

It comes with two honest conditions. It only works once the lighting is fixed, most likely with a light behind the bottle so the liquid line shows as a clean edge. And a fixed reference needs recalibrating whenever the bottle format or the lighting changes. That is a job for a technician with a checklist, not a data scientist with a GPU.

Marco is not thrilled. He was promised AI. Clare’s answer is that you can crack a nut with a sledgehammer, but you don’t get much nut. He’s not impressed.

Where the environment is controlled and the decision can be written as a rule, the classical approach isn’t the budget option. It’s the right approach every time.

Question three: where does deep learning earn its place?

Damage. Chips, cracks and contamination come in every shape, in any position, on glass that reflects everything around it. Clare can’t write a rule for “looks cracked”. This is the kind of uncertainty neural networks are good at, so this is where Marco gets his AI.

Even then, most of the blueprint is restraint.

She doesn’t start from zero. Defects are rare, so there will never be enough examples to train a network from scratch. The blueprint starts with a model that has already learned to see from millions of general images, and adapts it.

It’s like hiring an experienced inspector from another industry. They already know how to look, and you only have to teach them what a bad bottle is. Dark glass is not much like holiday photos, though, so the adapting has to go deeper than swapping the last layer.

She picks speed where the line can afford it. The line doesn’t stop, so the blueprint specifies a fast single-pass detector over a slower, more careful two-stage one. That’s normally a risk with small defects.

The camera sits at a fixed distance, so every bottle arrives the same size in the frame, which helps. A hairline crack is still a hairline crack. So the blueprint also names the evidence that would justify the heavier model: cracks missed in testing.

She decides which mistake is worse. A false alarm sends a good bottle to a human for a second look. A miss sends a cracked bottle to a customer. Those are not equal, so the threshold is set to over-report, and borderline cases go to a person.

Charlie, (our data engineer in this story), spots the catch. Defects are rare, so even a small false-alarm rate means most flagged bottles are perfectly good. Make the system too cautious and the inspectors spend their shift re-checking good bottles. The blueprint has to state both numbers: the misses Castellan will tolerate, and the false alarms the bench can absorb in a shift.

And she respects the boring details, two of which she learns the hard way.

The first is labelling. A hairline crack is thin and diagonal, so a box drawn round it is mostly good glass. Clare and Charlie mark up the same set of images separately and find they have drawn the boxes differently almost every time. Unless the people doing the labelling agree how to draw it, the model learns from an argument.

The second is her proudest experiment. She films the line for an afternoon, splits the frames at random into training and test sets, and gets a score that would look wonderful on a conference slide.

Charlie points out that consecutive frames are nearly identical. Her model was being tested on pictures it had practically seen. She splits by production batch instead, and the score comes back down to earth.

Neither detail came up on the conference stand, and there’s no reason it would in ten minutes. Both decide whether an accuracy figure means anything.

The blueprint doesn’t say whether Castellan buys this or builds it. Mature products exist for damage detection and one may well be the right choice. What Clare’s experiments give Marco is the list of questions to ask whoever supplies it.

One line, three answers

Marco asked for one model. The blueprint gives him three answers.

Damage gets deep learning, adapted from a pretrained model, because it is too variable to write as rules. Fill level and cap alignment get classical image processing, once the lighting is sorted, because they are geometry against a known reference.

Labels wait. Reading text is something you can buy off the shelf. Nobody has yet shown that a camera on this line can see a curved, moving label well enough to read it.

It isn’t what Marco heard at the conference. It is something he can cost, schedule and check.

Computer vision is not black magic. It’s a chain of engineering decisions, and most of them are about the camera and the data, not the model. Use the simplest thing that works, and step up only when the evidence tells you to.

There’s one question Clare hasn’t answered, and Marco has already asked it: how do we know it works, and how do we know it keeps working? Lighting changes, new bottle formats arrive, cameras get knocked. A vision system is not fit and forget. That’s the next post.

Reality has to be built in, not bolted on.