Ejemplo n.º 1
0
 def test_lda_covw_whitened(self):
     '''cov_w should be whitened in the transformed space.'''
     classes = spy.create_training_classes(self.data, self.classes)
     fld = spy.linear_discriminant(classes)
     xdata = fld.transform(self.data)
     classes.transform(fld.transform)
     fld2 = spy.linear_discriminant(classes)
     assert_allclose(np.eye(fld2.cov_w.shape[0]), fld2.cov_w, atol=1e-8)
Ejemplo n.º 2
0
 def test_mahalanobis_classify_transformedimage_runs(self):
     '''Mahalanobis classifier works with a TransformedImage object.'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data), self.gt,
                                           calc_stats=True)
     gmlc = spy.MahalanobisDistanceClassifier(ts)
     ret = gmlc.classify_image(ximg)
Ejemplo n.º 3
0
 def test_gmlc_classify_transformedimage_runs(self):
     '''Tests that GaussianClassifier classifies a TransformedImage object.'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data), self.gt,
                                           calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     ret = gmlc.classify_image(ximg)
Ejemplo n.º 4
0
 def test_lda_covw_whitened(self):
     '''cov_w should be whitened in the transformed space.'''
     import spectral as spy
     classes = spy.create_training_classes(self.data, self.classes)
     fld = spy.linear_discriminant(classes)
     xdata = fld.transform(self.data)
     classes.transform(fld.transform)
     fld2 = spy.linear_discriminant(classes)
     assert_allclose(np.eye(fld2.cov_w.shape[0]), fld2.cov_w, atol=1e-8)
Ejemplo n.º 5
0
 def test_gmlc_classify_transformedimage_runs(self):
     '''Tests that GaussianClassifier classifies a TransformedImage object.'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data),
                                      self.gt,
                                      calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     ret = gmlc.classify_image(ximg)
Ejemplo n.º 6
0
 def test_mahalanobis_classify_transformedimage_runs(self):
     '''Mahalanobis classifier works with a TransformedImage object.'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data),
                                      self.gt,
                                      calc_stats=True)
     gmlc = spy.MahalanobisDistanceClassifier(ts)
     ret = gmlc.classify_image(ximg)
Ejemplo n.º 7
0
 def setup(self):
     if not os.path.isdir(testdir):
         os.mkdir(testdir)
     self.image = spy.open_image('92AV3C.lan')
     self.data = self.image.load()
     self.gt = spy.open_image('92AV3GT.GIS').read_band(0)
     self.ts = spy.create_training_classes(self.data, self.gt,
                                           calc_stats=True)
     self.class_filename = os.path.join(testdir, '92AV3C.classes')
Ejemplo n.º 8
0
 def setup(self):
     if not os.path.isdir(testdir):
         os.mkdir(testdir)
     self.image = spy.open_image('92AV3C.lan')
     self.data = self.image.load()
     self.gt = spy.open_image('92AV3GT.GIS').read_band(0)
     self.ts = spy.create_training_classes(self.data,
                                           self.gt,
                                           calc_stats=True)
     self.class_filename = os.path.join(testdir, '92AV3C.classes')
Ejemplo n.º 9
0
 def test_gmlc_classify_ndarray_transformedimage_equal(self):
     '''Gaussian classification of an ndarray and TransformedImage are equal'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data), self.gt,
                                           calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     cl_ximg = gmlc.classify_image(ximg)
     cl_ndarray = gmlc.classify_image(pc.transform(self.data))
     assert(np.all(cl_ximg == cl_ndarray))
Ejemplo n.º 10
0
 def test_gmlc_classify_ndarray_transformedimage_equal(self):
     '''Gaussian classification of an ndarray and TransformedImage are equal'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data),
                                      self.gt,
                                      calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     cl_ximg = gmlc.classify_image(ximg)
     cl_ndarray = gmlc.classify_image(pc.transform(self.data))
     assert (np.all(cl_ximg == cl_ndarray))
Ejemplo n.º 11
0
 def test_perceptron_learns_image_classes(self):
     '''Test that perceptron can learn image class means.'''
     fld = spy.linear_discriminant(self.ts)
     xdata = fld.transform(self.data)
     classes = spy.create_training_classes(xdata, self.gt)
     nfeatures = xdata.shape[-1]
     nclasses = len(classes)
     for i in range(10):
         p = spy.PerceptronClassifier([nfeatures, 20, 8, nclasses])
         success = p.train(classes, 1, 5000, batch=1, momentum=0.3,
                           rate=0.3)
         if success is True:
             return
     assert(False)
Ejemplo n.º 12
0
 def test_load_training_sets(self):
     '''Test that the data loaded is the same as was saved.'''
     ts = spy.create_training_classes(self.data, self.gt, calc_stats=True)
     ts.save(self.class_filename)
     ts2 = spy.load_training_sets(self.class_filename, image=self.data)
     ids = list(ts.classes.keys())
     for id in ids:
         s1 = ts[id]
         s2 = ts2[id]
         assert (s1.index == s2.index)
         np.testing.assert_almost_equal(s1.class_prob, s2.class_prob)
         assert_allclose(s1.stats.mean, s2.stats.mean)
         assert_allclose(s1.stats.cov, s2.stats.cov)
         np.testing.assert_equal(s1.stats.nsamples, s2.stats.nsamples)
Ejemplo n.º 13
0
 def test_load_training_sets(self):
     '''Test that the data loaded is the same as was saved.'''
     ts = spy.create_training_classes(self.data, self.gt, calc_stats=True)
     ts.save(self.class_filename)
     ts2 = spy.load_training_sets(self.class_filename, image=self.data)
     ids = ts.classes.keys()
     for id in ids:
         s1 = ts[id]
         s2 = ts2[id]
         assert(s1.index == s2.index)
         np.testing.assert_almost_equal(s1.class_prob, s2.class_prob)
         assert_allclose(s1.stats.mean, s2.stats.mean)
         assert_allclose(s1.stats.cov, s2.stats.cov)
         np.testing.assert_equal(s1.stats.nsamples, s2.stats.nsamples)
Ejemplo n.º 14
0
 def test_perceptron_learns_image_classes(self):
     '''Test that perceptron can learn image class means.'''
     fld = spy.linear_discriminant(self.ts)
     xdata = fld.transform(self.data)
     classes = spy.create_training_classes(xdata, self.gt)
     nfeatures = xdata.shape[-1]
     nclasses = len(classes)
     for i in range(10):
         p = spy.PerceptronClassifier([nfeatures, 20, 8, nclasses])
         success = p.train(classes,
                           1,
                           5000,
                           batch=1,
                           momentum=0.3,
                           rate=0.3)
         if success is True:
             return
     assert (False)
Ejemplo n.º 15
0
#Concatenate the classification image arrays and mask arrays
cm1 = 'maskar = np.concatenate((' + mskstr + '))'
cm2 = 'classar = np.concatenate((' + allstr + '))'
exec cm1
exec cm2

#Adjust (either zero-pad or cut) classification array to work with source raster
(cx, cy) = maskar.shape
if src_raster_num_bands > classification_images_num_bands:
    zerar = np.zeros((cx, cy))
    diff = src_raster_num_bands - classification_images_num_bands
    for i in range(diff):
        classar = np.dstack((classar, zerar))

#Create training class and model
trcls = sp.create_training_classes(classar, maskar)
gmlc = sp.GaussianClassifier(trcls)
mldc = sp.MahalanobisDistanceClassifier(trcls)

#Classify image and display results
(kclmap, c) = sp.kmeans(barall, 12, 30)  #unsupervised
gclmap = gmlc.classify_image(barall)  #supervised
mclmap = mldc.classify_image(barall)  #supervised


#Define classification colors for array conversion
def num2colR(arg):
    switcher = {
        1: 255,
        2: 0,
        3: 255,
Ejemplo n.º 16
0
 def test_save_training_sets(self):
     '''Test that TrainingClassSet data can be saved without exception.'''
     ts = spy.create_training_classes(self.data, self.gt, calc_stats=True)
     ts.save(self.class_filename)
Ejemplo n.º 17
0
              j] = trefs[i].GetRasterBand(j + 1).ReadAsArray()

    xpos = xpos + xs

# Close training images
for i in range(len(trefs)):
    trefs[i] = None

# Print source raster metadata
print 'Driver: ', data_f.GetDriver().LongName
print 'Raster Sixe: ', data_f.RasterXSize, 'x', data_f.RasterYSize
print 'Pixel Size: ', data_f.GetGeoTransform()[1], ',', data_f.GetGeoTransform(
)[5]

#Create training class and model
tclass = sp.create_training_classes(tdata, tmask)
gmlc = sp.GaussianClassifier(tclass)
mldc = sp.MahalanobisDistanceClassifier(tclass)

# Read in data raster
data = np.zeros((data_f.RasterYSize, data_f.RasterXSize, data_nband))
for i in range(len(trefs)):
    for j in range(data_nband):
        data[:, :, j] = data_f.GetRasterBand(j + 1).ReadAsArray()

#Classify image and display results
print("K-Means")
(kclmap, c) = sp.kmeans(data, kmeans_clusters,
                        kmeans_iterations)  #unsupervised
print("Gaussian")
gclmap = gmlc.classify_image(data)  #supervised
Ejemplo n.º 18
0
 def test_save_training_sets(self):
     '''Test that TrainingClassSet data can be saved without exception.'''
     ts = spy.create_training_classes(self.data, self.gt, calc_stats=True)
     ts.save(self.class_filename)
Ejemplo n.º 19
0
  for j in range(data_nband):
    tdata[0:ys, xpos:xpos+xs, j] = trefs[i].GetRasterBand(j+1).ReadAsArray()
    
  xpos = xpos + xs
	
# Close training images
for i in range(len(trefs)):
	trefs[i] = None

# Print source raster metadata
print 'Driver: ',data_f.GetDriver().LongName
print 'Raster Sixe: ',data_f.RasterXSize,'x',data_f.RasterYSize
print 'Pixel Size: ',data_f.GetGeoTransform()[1],',',data_f.GetGeoTransform()[5]

#Create training class and model
tclass = sp.create_training_classes(tdata, tmask)
gmlc = sp.GaussianClassifier(tclass)
mldc = sp.MahalanobisDistanceClassifier(tclass)

# Read in data raster
data = np.zeros((data_f.RasterYSize, data_f.RasterXSize, data_nband))
for i in range(len(trefs)):
  for j in range(data_nband):
    data[:, :, j] = data_f.GetRasterBand(j+1).ReadAsArray()

#Classify image and display results
print("K-Means")
(kclmap,c) = sp.kmeans(data,kmeans_clusters,kmeans_iterations) #unsupervised
print("Gaussian")
gclmap = gmlc.classify_image(data) #supervised
print("Mahalanobis")