Problems doing ImageRegression with ANN created model

Hi all,

I’m having trouble running ImageRegression, after I have created a model using ANN in TrainImagesRegression. ImageRegression fails and gives the following error in the log:

(FATAL) ImageRegression: itk::ERROR: MultiThreader(000001D5109CC040): Exception occurred during SingleMethodExecute OpenCV(4.1.1) C:\build\otb\build\OPENCV\src\OPENCV\modules\ml\src\ann_mlp.cpp:350: error: (-215:Assertion failed) (type == CV_32F || type == CV_64F) && inputs.cols == layer_sizes[0] in function ‘cv::ml::ANN_MLPImpl::predict’

Configuration setup:
My system:: Windows 10
Version of the OTB: 7.2.0
I installed the OTB with: the binaries

Description of my issue
In TrainImagesRegression, my input raster is a layerstack with 3 bands in Web mercator projection. I have made sure that the bands do not accidentally contain NoData.

My label raster is a single band raster with values on the cells where I have ground truth data, the other cells have the value -1. Again, I checked hat the raster does not accidentally contain NoData.

I used a polygon shapefile that contains features with the same size and location of the cells in the label raster that have ground truth data values. I did this to make sure that the model is only trained on the locations I have ground truth data.

I used the Artificial Neural Networks classifier, with the back-propagation algorithm as training method. I created 2 intermediate layers, with 100 neurons per layer. I’m not sure whether this is the right thing to do.
I left all the other parameters as default.

The tool does work, and does provide a RMSE and a model textfile, with a size of about 500Kb, but is finished rather quickly. After selecting training samples, the training is done in seconds.The total processing time of TrainImagesRegression is 47 seconds on my laptop.

But when I run the model in ImageRegression, with the same layerstack as input rasters, I get the fatal error and the tool stops.
The error notice refers to OpenCV on a pathname which does not exist on my system:
C:\build\otb\build\OPENCV\src\OPENCV\modules\ml\src\ann_mlp.cpp:350

Should I have installed OpenCV? And should it be installed in this path? What am I doing wrong?

I have tried to do TrainImagesRegression using the random forest classifier, to see whether something was wrong with my input data. Using this classifier results in a working model, which also results in a prediction raster when used in ImageRegression. So it seems that the problem is with me using the ANN classifier.

Thank you for your help.

Hello,

I just tried to run TrainImagesRegression and ImageRegression and i experienced the same error. The error is not related to the installation of OpenCV, the OpenCV libraries are shipped with the binaries. I think the error comes from TrainImagesRegression. When I try to open the text file containing the trained model I noticed that the size of the input layer is 1, whereas my input image was of size 4 :

%YAML:1.0
---
opencv_ml_ann_mlp:
  format: 3
  layer_sizes: [ 1, 10, 1 ]
[...]

This explains the error " inputs.cols == layer_sizes[0] ".

I think there is something wrong with the configuration of OpenCV by TrainImagesRegression.

I managed to train a model with the correct size with TrainVectorRegression instead of TrainImagesRegression and it seems to work (the output model has the correct size).

TrainVectorRegression needs an input vector file containing samples with feature and predicted fields. This vector can be created with SampleSelection and SampleExtraction. See this tutorial.

You can also reuse the vector file created and used internally by TrainImagesRegression :

otbcli_TrainImagesRegression -io.il "input.tif" -io.ip "predicted.tif"  -io.out /tmp/model.txt -classifier ann -classifier.ann.sizes 10 10 -sample.nt 1000 -cleanup false

otbcli_TrainVectorRegression -io.vd /tmp/model.txt_trainsamples0.shp -io.out /tmp/model2.txt  -feat value_0 value_1 value_2 value_3 -cfield prediction -classifier ann  -classifier.ann.sizes 10 10

TrainImagesRegression creates the /tmp/model.txt_trainsamples0.shp file (the name is generated from the output model name). This file is used for the training and is deleted afterward if the cleanup option is not set to false. This solution is not optimal because two model are trained (the first one being incorrect), but it requires less work than generating the vector data by hand.

I will create an issue on OTB gitlab, there is something wrong in TrainImagesRegression.

Cédric

1 Like

Hi! Sorry, I’m gonna ask another thing. I encountered error in TrainImagesRegression, and I think it is because my raster data contain NoData values. Do you have any idea how to solve it?

Deha

Hello,

What is the error you are encountering ? Why do you think it is caused by the no-data value?

TrainImagesRegression has an input vector parameter (io.vd). The points used to train the model are extracted from the polygons contained in this vector and the input images. Maybe you could use this parameter to avoid using no data values in the training ?

Cédric

Thank you Cédric, for your clear answer. It is good to know that I’m not the only one getting this error, and that it had probably to do with the tool. I will use TrainVectorRegression instead.

Here is the error I got:

Warning 1: Value -3.4028234663852886e+38 of field prediction of feature 0 not successfully written. Possibly due to too larger number with respect to field width
Warning 1: Value -3.4028234663852886e+38 of field prediction of feature 1 not successfully written. Possibly due to too larger number with respect to field width
Warning 1: Value -3.4028234663852886e+38 of field prediction of feature 2 not successfully written. Possibly due to too larger number with respect to field width

Warning 1: Value -3.40282346638528846e+22 of field prediction of feature 237 not successfully written. Possibly due to too larger number with respect to field width
Warning 1: Value -3.40282346638528846e+22 of field prediction of feature 238 not successfully written. Possibly due to too larger number with respect to field width
Warning 1: Value -3.40282346638528846e+22 of field prediction of feature 239 not successfully written. Possibly due to too larger number with respect to field width
More than 1000 errors or warnings have been reported. No more will be reported from now.

Isn’t it because of the no-data values?

But, thank you Cédric! I tried to include io.vd and it worked!

However, it is a bit difficult for my data since the no-data values are scattered in the study area. Do you have another suggestion for this case?

@dehaumarhadi ,

you could try to build io.vd automatically to exclude all no-data pixels. This can be done for example by using OTB and gdal:

# Create a raster mask from the input image
otbcli_ManageNoData -mode buildmask -in input.tif -out mask.tif

# Vectorize the mask, gdal_polygonize.py should take no data into account
gdal_polygonize.py mask.tif mask_vector.shp 

This should create a vector mask file that can be used as io.vd in TrainImagesRegression.

See the documentation of ManageNoData and gdal_polygonize

Note: gdal_polygonize is available in QGIS too.

Cédric

Thank you very much Cédric!
Really appreciate your helpful answer.