Binary image classifier

Hi. I have a repository of images, which are classified in two categories, say A and B.

I tried to train a classifier with Classify, but this was obviously too heavy a task for it.

So I want to train a Neural Network to perform the task. I searched at the Wolfram NN Repository, but found no NN that could serve my purposes.

Can somebody guide me with respect to this?:
a. If there is already an NN that acts as a binary image classifier, which one and how can I retrieve/use it?

b. Otherwise, how to build the NN myself?

Thanks and best.
Francisco

Hi Francisco,

Can you explain what you mean by

but this was obviously too heavy a task for it.

Of course. It classified the photos very poorly. Basically, put everything into a single category. I tried other methods than the default (NaiveBayes for example), without success

  • What is the subject in the images?
  • Are the images very similar even though they are labeled differently?
  • How large is the training set?
  • Is there a large class imbalance in the training set?

Have you looked at the image classification examples in the Applications section of the documentation? It performs well on those images.

Could you post a few example images from each class?

Many thanks for your questions.
The answers are the following:

a. What is the subject in the images? These are photos of people who belong to different non-state armed groups (guerrillas or militias)

b. Are the images very similar even though they are labeled differently? I frankly do not know. This is the very sense of trying to find a classifier that is able to tell them apart. My hypothesis is that they are different enough to be separated by a good classifier.

c. How large is the training set? I used 400 pics for each group --that means 800 in total. Maybe this is too small?

d. Is there a large class imbalance in the training set? No imbalance

I am attaching two examples (one for each group) of the type of photos I am dealing with (by the way, retrieved with WebImageSearch).

I will give a hard look at the documentation --thanks again


Many thanks for your questions.
The answers are the following:

a. What is the subject in the images? These are photos of people who belong to different non-state armed groups (guerrillas or militias)

b. Are the images very similar even though they are labeled differently? I frankly do not know. This is the very sense of trying to find a classifier that is able to tell them apart. My hypothesis is that they are different enough to be separated by a good classifier.

c. How large is the training set? I used 400 pics for each group --that means 800 in total. Maybe this is too small?

d. Is there a large class imbalance in the training set? No imbalance

I am attaching two examples (one for each group) of the type of photos I am dealing with (by the way, retrieved with WebImageSearch).

I will give a hard look at the Classification function documentation, though I have already know it (I think) quite well --thanks again

Welcome to Wolfram Community!
Please make sure you know the rules: https://wolfr.am/READ-1ST

Your post is too vague. Please “Edit” your post with all the answers of the replies you had here.
Thank you.

I am confused by the response to question b.

I frankly do not know. This is the very sense of trying to find a
classifier that is able to tell them apart. My hypothesis is that they
are different enough to be separated by a good classifier.

In supervised learning the training data has to be accurately labeled (ground truth) as in the examples in the documentation. How were the images labeled?

They were labelled: “Guerrilla”, “Militia” (or if you prefer “Class A”, “Class B”). There is no problem or ambiguity related to the labels.
Your question was how different were the photos from each other, so I answered to that question.
Francisco

Something to try.

To get a random 80%, 10%, 10% split for training, validation and testing. images is the list of image -> class

{training, testing} = ResourceFunction["TrainTestSplit"][images];
{testing, validation} = ResourceFunction["TrainTestSplit"][testing, "TestSetSize" -> Scaled[0.5]]

classifier = Classify[training, ValidationSet -> validation, PerformanceGoal -> "Quality"]
cm = ClassifierMeasurements[classifier, testing]

cm["ConfusionMatrixPlot"]
cm /@ {"Accuracy", "Precision", "Recall", "F1Score"}

If the results are not satisfactory there are a lot of things to try to improve it. I would do that rather than building a DNN from scratch or even attempting transfer learning.

Creating a Neural Network class in Python is easy.

class NeuralNetwork:

def __init__(self, x, y):
    self.input      = x
    self.weights1   = np.random.rand(self.input.shape[1],4) 
    self.weights2   = np.random.rand(4,1)                 
    self.y          = y
    self.output     = np.zeros(y.shape)

There is no need to write a NN from scratch in WL. It has extensive built-in support for NN.

Writing a good image classifying NN from scratch is not an easy task.