Checking how many agents are working

How this works
Contacto
← Blog
A heap of fine olive grains in a shallow dark tray lit from one side, with a faint ruler of dots along its edge, on a near-black navy ground, the AMARBARO mark centred over it.
✎publicación de blog

Measuring soil from a phone photo

The short version

Two image features, the spread of a pattern spectrum and one wavelet texture level, scored 33.86 on a leave-site-out split against 117 for the label prior, yet the public board scored our first entries at 78.82 and 61.996 because the test soils held sands and cobbles the training set lacked.

What this is about

Engineers describe soil by a sieve curve. You pass a sample through a stack of meshes, from coarse to fine, and weigh what stays on each one. The curve says what share of the soil is finer than each mesh. It tells you if you hold gravel, sand or clay.

A Kaggle competition asked: can you predict that curve from one photo of loose soil in a tray? We tried. This article follows our steps, in plain order, and tells what went well and what did not.

Why it matters

A sieve test takes a lab, scales and time. A photo takes a second. If a phone could give a rough curve, a field worker could check a sample on the spot. It also makes a good lesson in honest testing. With only 24 labelled samples, it is very easy to fool yourself.

How we did it, step by step

1. Understand the score. The score is the area between the true curve and our curve, with grain size on a log axis. Each factor of ten in size counts the same, so fine sand matters as much as gravel. It runs from 0 to 500 and lower is better. Guessing flat 9.09 percent steps scored 100.3. Because the score adds up absolute gaps, the best single guess at each mesh is the median, not the mean. We built our method around medians.

2. Look at the data. The training set had 24 labelled samples, the test set 10, in 162 photos from five phones. No phone appeared in both sets. Training photos came from a Motorola and a Samsung. Test photos came from an iPhone 14 and an iPhone 16.

3. Get a scale. A curve is in millimetres. A photo is in pixels. There was no ruler or coin in the pictures, only the edge of the tray. The training photos had been shrunk, so each pixel covered about 0.22 millimetres, against 0.05 to 0.07 on the test photos. That is our inference from the supplied values. Grains under about 0.7 millimetres are invisible in the training pictures.

4. Match the sharpness. The same soils on the two training phones gave curves that differed by a median factor of 4.37. The Samsung photos were 2.4 times less sharp. We blurred the sharper photos until the sharpness matched and the factor fell to 1.45. Blur changes which grains survive an opening, so sharpness is a hidden cause. We matched it before computing anything else.

5. Pick features. One feature already helped: how much of the image survives a square opening of 20 millimetres. Then we moved to the shape of the whole distribution. The spread of the pattern spectrum scored 42.95 on a fair split. Adding one wavelet texture level took it to 33.86. We tried about 21 configurations to get there, which is a risk of its own, so we report that number with the count beside it.

6. Validate fairly. At first we held out one sample at a time. That rewarded memory of the site, since samples from one site look alike. When we held out whole sites, the first feature scored 51.0 instead of 47.9. The honest estimate was the worse one.

The bugs that changed the answer

Five bugs each moved our number, and we fixed them all. A crop margin of 8 pixels meant 1.7 millimetres on one set and 0.4 on the other. The tray rim sat in the measured area on some cameras and not others. The training and test photos took different scale paths. Some photos were in a wide colour space and some were not. And our image library returned believable numbers for invalid input, for example a colour image given to a grey operation. It now raises an error.

Two of our early beliefs were wrong and we retracted them. We thought our score was ten times too big. The host confirmed the formula, so the gap was in our model. We also thought the phone's focus distance could give us the scale. It cannot. The same sample at two distances had the same framing.

What the real test said

Our best fair-split score was 33.86, against 117 for guessing the typical curve. That is a good sign. It is not the real test.

On the public board, which scores a small part of the test set, our first entry got 78.82 and a second version got 61.996. The two are not close to 33.86. Our diagnosis, from the notes, is that the test soils held sands and cobbles that the 24 training samples did not. A model cannot read what it has never seen. Our own notes called the public score only a sanity check on three soils, so we do not read much into it. The final ranking uses seven soils the public board does not show.

We tried adding outside data of 315 soils from another set. It did not help. Any weight we gave it hurt. A model of frozen image features did see sand better inside that set, but it did not beat our two features on our own 24 gravel soils.

What this does not show

Our method may work on gravel and fail on sand. We do not yet know the private score. The competition's labels came from a lab method we do not know, and the tray's true size is unconfirmed. Each could move the scale, and the scale moves every number.

Words you may not know

  • Sieve curve: a chart of how much of a soil sample is finer than each mesh size.
  • Median: the middle value when you sort a list.
  • Validation: a test you run on data you held back.
  • Leave-site-out: hold back every sample from one place, train on the rest, and repeat.
  • Pixels per millimetre: how many pixels cover one millimetre of the real object.
  • Feature: a number we compute from a photo that we hope predicts the answer.
  • Opening: an image step that erases every bright shape smaller than a chosen square, so only bigger grains survive.
  • Pattern spectrum: how much of the image survives openings of each size, from small to large. It is a picture of the grain sizes.
  • Wavelet texture: a measure of how much fine or coarse detail an image has at one scale.

Try it

Our computer vision library, with each operation checked against OpenCV, is mojo-cv on GitHub. Try one of its operations on a photo of your own and compare it to the same step in another library. If they differ, you found something.

To see how a competition like this works, make an account at kaggle.com and open the soil grain size competition's discussion board. For questions, we are on gllm.forum.

Reading Soil Grain Size From a Phone Photo is the full technical account, with every table.

Comentarios

Aún no hay comentarios.

Iniciar sesión para comentar.