1

I am trying to perform classification on a satellite image using libSVM library. What I want is to display the classified image and save it,not only to get the accuracy results on my terminal. I have extracted the pixel values from teh training datasets (shown below) and I used the script csv2libsvm (https://github.com/zygmuntz/phraug/blob/master/csv2libsvm.py) to bring my data in the right format for libsvm. There are 4 different classes in the image to be classified. My satellite image and the training data are displayed below.

enter image description here fig 1: Image to be classified with training data.

The steps I followed are based on the following tutorial https://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf.

  1. split training and testing data (70% training and 30% testing).

    svm - subset.py datasets 12000 training.tr testing.te

  2. Train the model

    svm-train training.tr

  3. make prediction

    svm-predict testing.te training.tr.model classification output

The accuracy of this classification was 95%, which was great.

What I am really interested in now, is to displayed the classified image. So, I need to construct my classified image out of the csv file which containes the classified labels. This is where the problem comes and do not know how to do it. What I have done (and did not work) is the following:

  1. I imported the csv file produced by lbSVM into python using csv module.

  2. I tried to reshape the csv file into the shape of my image

My code is shown below:

with open('/home/io/Desktop/training/1TESTING/libSVM_classification/classification_results', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    results = []
    for row in reader:
        results.append(row)
    
results_arr = np.asarray(results, dtype=int) 
predicted = results_arr[results_arr>0] #here is my predicted labels

#load the image to be classified and read the projection system
raster_dataset = gdal.Open(sea_ice, gdal.GA_ReadOnly)
geo_transform = raster_dataset.GetGeoTransform()
proj = raster_dataset.GetProjectionRef()

#loop over all bands of the image and append them.
bands_data = []
for b in range(1, raster_dataset.RasterCount+1):
    band = raster_dataset.GetRasterBand(b)
    bands_data.append(band.ReadAsArray())

bands_data = np.dstack(bands_data)
row, col, n_bands = bands_data.shape

#get the classified labels from libsvm and reshape them into the initial image
#in order to display it using matplotlib
class_prediction = predicted.reshape(bands_data[:, :, 0].shape)

The size of the image to be classified is(303 x 498) and the size of the predicted classes produced by libsvm is 1807. Hence, the error I get when try to reshape the libsvm results i get the following error.

ValueError: cannot reshape array of size 1807 into shape (303,498)

This error makes sense. I have 1907 rows and try to reshape it to match my initial image, which is obviously impossible.

So, How can I display my classified image? I achieved an accuracy of 95% but have not found a way of seeing the classification results. I though that libsvm might have an option for exporting the classification results into tiff but it does not.

I would appreciate any help, thoughs or tips

Community
  • 1
  • 1
Johny
  • 319
  • 3
  • 12

0 Answers0