# Binary image classifier

**URL:** <https://community.wolfram.com/t/binary-image-classifier/16453>\
**Category:** Questions\
**Tags:** image-processing, mathematica, machine-learning, neural-networks\
**Created:** [July 16, 2020, 8:43pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453 "2020-07-16T20:43:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [July 16, 2020, 8:43pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/1 "2020-07-16T20:43:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 16, 2020, 9:32pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/2 "2020-07-16T21:32:48Z")

</div>

Hi Francisco,

Can you explain what you mean by

> but this was obviously too heavy a task for it.

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [July 16, 2020, 11:54pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/3 "2020-07-16T23:54:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 17, 2020, 1:08am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/4 "2020-07-17T01:08:22Z")

</div>

- 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](https://reference.wolfram.com/language/ref/Classify.html)? It performs well on those images.

Could you post a few example images from each class?

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [July 17, 2020, 1:53am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/5 "2020-07-17T01:53:11Z")

</div>

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

 ![farc1.jpg](https://global.discourse-cdn.com/wolfram/original/3X/a/a/aa912e529f2f3fdfb05e2aa4c31a1c31532bdb69.jpeg)  
 ![params2.jpg](https://global.discourse-cdn.com/wolfram/original/2X/9/9fceb6f8fe711595ed90c7af32f0cf3287296cb9.jpeg)

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [July 17, 2020, 2:20am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/6 "2020-07-17T02:20:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [July 17, 2020, 2:38am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/7 "2020-07-17T02:38:25Z")

</div>

Welcome to Wolfram Community!  
Please make sure you know the rules: [https://wolfr.am/READ-1ST](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.

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 17, 2020, 3:20pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/8 "2020-07-17T15:20:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fgutiers2002](https://avatars.discourse-cdn.com/v4/letter/f/bb73d2/32.png) [@fgutiers2002](https://community.wolfram.com/u/fgutiers2002)\
**Post date:** [July 17, 2020, 3:24pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/9 "2020-07-17T15:24:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 17, 2020, 7:51pm UTC](https://community.wolfram.com/t/binary-image-classifier/16453/10 "2020-07-17T19:51:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yasmin](https://avatars.discourse-cdn.com/v4/letter/y/71c47a/32.png) [@yasmin](https://community.wolfram.com/u/yasmin)\
**Post date:** [July 30, 2020, 6:00am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/11 "2020-07-30T06:00:40Z")

</div>

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)

```

> **[How to build a neural network from zero | Towards Data Science](https://towardsdatascience.com/building-a-neural-network-from-scratch-8f03c5c50adc/)**
>
> No frameworks, just Python

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [July 31, 2020, 1:20am UTC](https://community.wolfram.com/t/binary-image-classifier/16453/12 "2020-07-31T01:20:01Z")

</div>

There is no need to write a NN from scratch in WL. It has extensive [built-in support](https://reference.wolfram.com/language/tutorial/NeuralNetworksOverview.html) for NN.

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