# Test issue in semantic segmentation using Deep learning 4j

**URL:** <https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150>\
**Category:** DL4J\
**Created:** [February 22, 2020, 5:23pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150 "2020-02-22T17:23:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 22, 2020, 5:23pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/1 "2020-02-22T17:23:32Z")

</div>

Hello @AlexBlack , please can you see the following issue: [Test issue in semantic segmentation using DL4J · Issue #8644 · eclipse/deeplearning4j · GitHub](https://github.com/eclipse/deeplearning4j/issues/8644#event-3047497958)

it is closed but fixed yet, i uploaded all the resources that can you need. Thank you so much for the help.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 22, 2020, 5:26pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/2 "2020-02-22T17:26:14Z")

</div>

What about the solution there doesn’t work for you?

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 22, 2020, 6:09pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/3 "2020-02-22T18:09:16Z")

</div>

thank you so much @treo for your reply.

I already used `NormalizerMinMaxScaler in training process: [SemanticSegmentation.java · GitHub](https://gist.github.com/AbdelmajidB/49ae82743d8d27046397283c366db4a1#file-semanticsegmentation-java-L38)

and don’t work

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 22, 2020, 6:17pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/4 "2020-02-22T18:17:32Z")

</div>

That is the point. NormalizerMinMaxScaler is your problem here.  
As I’ve explained already in the original answer on Github:  
You can not just create a new one and fit it on your test data.  
You have to serialize it when you originally train your model, and when testing it, you have to load that serialized normalizer.

When you create a new normalizer and fit it on your _test_ data, it will only have the statistics of that test data. So if you only have a single picture in your test folder, it will only have the statistics of that single picture. This in turn results in plain _wrong_ normalization.

_You always have to use the **exactly** same normalization for inference / evaluation / testing as you’ve used for training_. Otherwise you put yourself into a garbage in, garbage out situation.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 22, 2020, 6:32pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/5 "2020-02-22T18:32:57Z")

</div>

ahh ok, thank you.

now i will try the second solution that you have proposed by using ImagePreProcessingScaler instead of NormalizerMinMaxScaler.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 22, 2020, 6:35pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/6 "2020-02-22T18:35:19Z")

</div>

Unless your pictures have the range of 0 to 255, then you will need retraining with that solution as well. But If I remember correctly the grayscale images in your dataset excerpt that you shared with us did indeed range from pure black to pure white, so that might actually work without retraining in your case.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 22, 2020, 6:43pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/7 "2020-02-22T18:43:24Z")

</div>

no problem i will retrain the model, the training process take only 5 hours.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 8:38am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/8 "2020-02-24T08:38:23Z")

</div>

Hi @treo @AlexBlack , i serialised the NormalizerMinMaxScaler after training like this:

FileOutputStream fi = new FileOutputStream(DATA\_PATH+“/NormalizerMinMaxScaler.txt”);  
ObjectOutputStream oi = new ObjectOutputStream(fi);  
oi.writeObject(scaler);  
oi.close();

and i loaded it before testing like this:

NormalizerMinMaxScaler scaler = (NormalizerMinMaxScaler)oi.readObject();;  
scaler.fitLabel(true);  
scaler.fit(iter);  
iter.setPreProcessor(scaler);  
oi.close();

but the issue still not fixed.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 8:49am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/9 "2020-02-24T08:49:56Z")

</div>

There are multiple problems here.

First, you should serialize your Normalizer using `NormalizerSerializer` like that:

```java
NormalizerSerializer.getDefault().write(scaler, DATA_PATH+"/NormalizerMinMaxScaler.bin");

```

And deserialize it like that:

```java
NormalizerMinMaxScaler scaler = NormalizerSerializer.getDefault().restore(DATA_PATH+"/NormalizerMinMaxScaler.bin");

```

When you serialize it like you did, you risk that you will not be able to deserialize it with a newer version of dl4j.

For now it should still work the way you did it (so you don’t have to go out and retrain to try the next step).

> [@AbdelmajidB](#):
>
> ```java
> NormalizerMinMaxScaler scaler = (NormalizerMinMaxScaler)oi.readObject();;
> scaler.fitLabel(true);
> scaler.fit(iter);
> 
> ```

The problem here is that you are still fitting on the test data. You should not do that. It should load all the necessary statistics from the serialized object.  
So remove the `fit...` lines and it should work.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 9:55am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/10 "2020-02-24T09:55:02Z")

</div>

i deleted file from test:  
FileInputStream fi = new FileInputStream(DATA\_PATH+“/NormalizerMinMaxScaler.txt”);  
ObjectInputStream oi = new ObjectInputStream(fi);  
NormalizerMinMaxScaler scaler = (NormalizerMinMaxScaler)oi.readObject();  
iter.setPreProcessor(scaler);  
oi.close();

The results enhanced significantly when using only one image. But the the segmentation results changes little bit when adding other image. Anyway is better than the first solution.

now i wil retrain the model with and using NormalizerSerializer.

i tested ImagePreProcessingScaler for training segmentation and not work, i think there is a problem in this class for segmentation, i found this issue [ND4J: ImagePreProcessingScaler should support segmentation · Issue #8135 · eclipse/deeplearning4j · GitHub](https://github.com/eclipse/deeplearning4j/issues/8135)

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 10:00am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/11 "2020-02-24T10:00:22Z")

</div>

> [@AbdelmajidB](#):
>
> i tested ImagePreProcessingScaler for training segmentation and not work, i think there is a problem in this class for segmentation, i found this issue [ND4J: ImagePreProcessingScaler should support segmentation · Issue #8135 · deeplearning4j/deeplearning4j · GitHub](https://github.com/eclipse/deeplearning4j/issues/8135)

The referenced issue has been fixed in November 2019, and the fix was included in beta6. What exactly doesn’t work for you?

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 11:03am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/12 "2020-02-24T11:03:28Z")

</div>

I get wrong values of loss during training process. for example when i used Cross Entropy: Binary Classification as a loss function i get negative values.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 11:23am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/13 "2020-02-24T11:23:03Z")

</div>

I see. Even though the scaler has support for transforming labels for segmentation, it can’t actually be configured to do so when used as a normalizer.

I’ve created an issue: [ImagePreProcessingScaler can not be configured to preprocess labels · Issue #8731 · eclipse/deeplearning4j · GitHub](https://github.com/eclipse/deeplearning4j/issues/8731)

Thank you for noticing this 👍

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 11:29am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/14 "2020-02-24T11:29:29Z")

</div>

so i can still using NormalizerMinMaxScaler for images normalization or is better using ImagePreProcessingScaler? i don’t know if they will give the same results, as my objective just rescaling the images and labels between 0 and 1.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 11:33am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/15 "2020-02-24T11:33:56Z")

</div>

if you want to use ImagePreProcessingScaler, the easiest way to do it _right now_ would be to subclass it, and override `preProcess` like this:

```java
@Override
    public void preProcess(DataSet toPreProcess) {
        this.preProcess(toPreProcess.getFeatures());
        this.transformLabel(toPreProcess.getLabels());
    }

```

You can also take a look at your NormalizerMinMaxScaler, if its stats show that the range it is working on is 0 to 255, then you should get essentially the same results.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 11:58am UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/16 "2020-02-24T11:58:29Z")

</div>

no its working thank you so much, thank you for the effort.  
Please just last question what is the utility of using fit method in ImagePreProcessingScaler and NormalizerMinMaxScaler?

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 12:05pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/17 "2020-02-24T12:05:24Z")

</div>

> [@AbdelmajidB](#):
>
> what is the utility of using fit method in ImagePreProcessingScaler and NormalizerMinMaxScaler?

### ImagePreProcessingScaler

`fit(dataSetIterator)` will do nothing. All the configuration that this normalizer needs will have to be provided upon construction. Because `fit` is a no-op here, you don’t have to serialize and save the normalizer after training and you don’t have to restore it for inference, you can just create a new one.

### NormalizerMinMaxScaler

`fit(dataSetIterator)` will iterate through the provided DataSetIterator and learn the statistics of the underlying data. Those statistics will inform how it actually applies the normalization. For this reason you have to serialize and save this normalizer so you can use the same statistics for normalizing test data as you’ve used when you originally trained your model.

---

<div class="post-metadata">

**Author:** ![AbdelmajidB](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/abdelmajidb/32/56_2.png) [@AbdelmajidB](https://community.konduit.ai/u/AbdelmajidB)\
**Post date:** [February 24, 2020, 12:14pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/18 "2020-02-24T12:14:32Z")

</div>

@treo thank you so much, i really appreciate your help.

---

<div class="post-metadata">

**Author:** ![treo](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/treo/32/47_2.png) [@treo](https://community.konduit.ai/u/treo)\
**Post date:** [February 24, 2020, 12:21pm UTC](https://community.konduit.ai/t/test-issue-in-semantic-segmentation-using-deep-learning-4j/150/19 "2020-02-24T12:21:57Z")

</div>

@AbdelmajidB feel free to mark a post here as the solution, so this topic will get marked as solved 🙂
