Supervised and unsupervised classification, Sentinel 2

Depends on the goal, but it’s a good start.

Ok thank you, my goal is to do the mapping of the annual crop types, and I need the bands of SWIR and red Edge for the classification.

I could not run the KNN classifier despite reproducing exact steps as described. I made vector layers with training areas and saved the product. Then I ran KNN on vanilla SNAP 5.0. The classifier ran without error, but output data were null, as someone else had.
Then I upgraded the SNAP installation to the latest version and I got Error: [NodeId: KNN-Classifier] node when entering the KNN-Classifier tab.

Can you look into it? :frowning:

Here’s a log snippet I managed to find:

org.esa.snap.core.gpf.graph.GraphException: [NodeId: KNN-Classifier] node
	at org.esa.snap.core.gpf.graph.NodeContext.initTargetProduct(
	at org.esa.snap.core.gpf.graph.GraphContext.initNodeContext(
	at org.esa.snap.core.gpf.graph.GraphContext.initNodeContext(
	at org.esa.snap.core.gpf.graph.GraphContext.initOutput(
	at org.esa.snap.core.gpf.graph.GraphContext.<init>(
	at org.esa.snap.core.gpf.graph.GraphContext.<init>(
	at org.esa.snap.graphbuilder.rcp.dialogs.GraphBuilderDialog.InitGraph(
	at org.esa.snap.graphbuilder.rcp.dialogs.GraphBuilderDialog.ValidateAllNodes(
	at org.esa.snap.graphbuilder.rcp.dialogs.GraphBuilderDialog.access$000(
	at org.esa.snap.graphbuilder.rcp.dialogs.GraphBuilderDialog$1.stateChanged(
	at javax.swing.JTabbedPane.fireStateChanged(Unknown Source)
	at javax.swing.JTabbedPane$ModelListener.stateChanged(Unknown Source)
	at javax.swing.DefaultSingleSelectionModel.fireStateChanged(Unknown Source)
	at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(Unknown Source)
	at javax.swing.JTabbedPane.setSelectedIndexImpl(Unknown Source)
	at javax.swing.JTabbedPane.setSelectedIndex(Unknown Source)
	at javax.swing.plaf.basic.BasicTabbedPaneUI$Handler.mousePressed(Unknown Source)
	at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$ Source)

Nothing has changed in the updates for the classifiers. However, this looks like a similar bug to the one reported recently involving S2 masks. This should be fixed now and will be available soon.

Same issue for me. I saved the product with the training areas as vectors, I erased the masks (and saved as well), but I have a blank image. When I save this image under a common image format (jp2, jpg, gif, png…) the image reader provides a message error saying the image is invalid.
Any idea?

I do have the same issue

you can also ressamping the [S2A_USER…] to save as the ENVI format(choose you need resolution), and then processing in the ENVI software.

Dear zgramos,

I have your same problem about “no data” and I did the reprojection but I didn’t get through the problem. Do you have some news about this “bug”?


Dear Pierro,
My issues were solved with the reprojection of the data before the classification.

1 Like

I updated everything to the latest version and it STILL doesn’t work. Now, there’s no mysterious red error, it informs you that classification has been completed at couple MB/s, but the resulting classified image doesn’t load properly. In fact, there’s no data in it (img file contains only 0xFF).
I have to say I am very frustrated. Did you ever test this thing?! :angry: I can show you step by step what I do for you to repeat, but if you promise to actually repeat it and look what causes it.

I did supervised classification also. To explain what I did, I write down step by step because something went wrong and the result were not what I expected:

  1. resampling image
  2. subseting image
  3. defining geometries
  4. saving
  5. reprojecting
  6. supervised classification ( KNN-classifier, took 4 hours)
    and result

Almost the same (even worse) happened when I tried unsupervised classification ( K-means ):

Any ideas what I did wrong?

what was your input data and why do you think the results are wrong?

How does one carry out a validation using the training dataset? I dont see where to separate the training and testing data

if you validate using the training data you just validate how good the classifier was able to fit your training data.
To validate the classification independent validation areas have to be collected as well.


My input file name is: S2A_OPER_MTD_SAFL1C_PDMC_20160519T175247_R036_V20150804T094005_20150804T094005.xml
While resampling I used these parameters:

After resampling I subsetted a specific area using these parameters:

For the next step i defined geometries on the subsetted image and saved the project (file):

After saving I did these steps : Raster -> Geometric operations -> reprojection. When the reprojection was done i noticed that the image was streched from left to right.

The finished product was ( as shown in the previous comment):

I know that the result is wrong because half of the image is covered with water, but it should be mainland including forest, roads etc.

I hope that now you understand my problem and have a solution for it. Thank you in advance!

Thanks Andreas for your speedy response. Question is, how does one do the
validation? How is it setup in SNAP?


I don’t think it’s possible in SNAP, but I can recommend the SCP plugin for QGIS

1 Like

That’s the semi-automatic classification plugin?

yes, it provides great post-processing options for classification. img-files of the dimap format can directly loaded into QGIS.

Ok, great. I will try QGIS. Cheers and thanks