# How can one train a Neural Network in Mathematica such as to recognise and count all occurrences of a certain custom category in an image?

I'm trying to use Mathematica for the following task:

(1) Get an aereal view of a certain area in Mongolia

(2) Count the number of plots that contain houses, and the number of plots that contain only gers (traditional yurts) on that area, and no houses.

• Houses are typically rectangular colored areas (their roofs), while gers show up as white disks with a dark dot in the middle.
• Plots in Mongolia are usually surrounded by walls or wooden fences to keep the 65 million free roaming livestock (goats, sheep, cows, horses) that belong to the nomadic population off premises.

An example for an interesting area to examine would be:

GeoImage[{GeoPosition[{47.95, 106.78}],
GeoPosition[{47.97, 106.82}]},
GeoProjection -> "Mercator",
ImageSize -> 2500]


As you can see, GeoImage works nicely for (1). Cute and comfortable built-in functions like ImageCases and ImageBoundingBox are however of no help for (2), as they seem to only recognise categories of objects, hard coded by Wolfram into the underlying NNs. Unfortunately those don't include birds eye views of houses and gers.

I have no difficulty in elegantly solving this problem with Tensorflow in Python just following one of many available web tutorials. However I can't figure out, how to do this in Mathematica. Is it possible at all?

Any help/hint would be greatly appreciated.

• I presume tensorflow in python doesn’t come with the trained classification of gers vs houses, can you (make an) attempt to build/train a net (maybe same architecture you are accustomed to with tensorflow in python) to locate them? You would need some labeled/classified examples of what you want to locate, and there might honestly be a simpler way, without neural networks? But I would find the number of/locate each of the cases in each grid you designate(this should not be difficult, I wouldn’t think), then use a neural net to classify them! – CA Trevillian Jul 18 at 7:03
• Thank you, CA Trevillian. I agree. One could certainly just train a suitable net per hand and then program something that mimicks the ImageCases function of Mathematica. It would however be much nicer, to use ImageCases as it is, but re-train or replace the Neural Network it uses to deliver its functionality. Any idea if/how this is possible? The proposal in your final sentence is interesting, but I believe it would require much more training data and manual counting of houses and gers that just let ImageCases learn, how a house or ger looks like. – guru10at Jul 18 at 10:00
• I don’t anticipate it would need any more or less training data than you would need with other platforms—you will need to give trainable data on gers vs houses anyways, which I believe may end up being something like circles/round versus squares/sharp edges if you were using ImageCases—I don’t know of a way to change the internal training set/neural network, but you may find success in reducing the AcceptanceThreshold, then looking at the lower probability cases by hand, with possible preprocessing the images with EdgeDetect or similar function. – CA Trevillian Jul 18 at 14:48
• Thank you, CA Trevillian. Looking for Disks and Rectangles (which is more of an image processing than a ML task) will not do the job for me, as I need to detect if within one single (arbitrarily shaped) plot (framed by a fence or wall) there are only one or more gers or (exclusively or in addition to the gers) permanent houses. For that reason I’d like to train or change the NN that the ImageCases function is using. How could I do that in Mathematica? – guru10at Jul 19 at 7:07
• ah yes okay, I came to understand this while fooling at the problem yesterday...I don’t believe you can change that NN, exactly, but we may be able to use something like MorphologicalComponents in combination with something else to detect the edges of those plots. I tried combining EdgeDetect with something else (I can’t remember right now, its pre-coffee) in a manipulate, but I couldn’t seem to be able to find a sweet spot that left just the edges defined. I think, though, you may need to train to find plots, then what is within? – CA Trevillian Jul 19 at 12:04