Esempio n. 1
0
    def predict(self, X):
        """
        Makes predictions based on training and X.
        
        Args:
            X (ndarray): the testing data
        
        Returns:
            pred (ndarray): the predictions of the y data based 
                on training and X
        """

        if len(self.pre_processor):
            for j in range(len(self.pre_processor)):
                X = self.pre_processor[j].transform(X)

        if self.pad:
            X = np.pad(X, self.pad, mode='constant')

        base = np.copy(X)
        final = np.zeros(X.shape[:2] + (0, ), dtype=np.float32)

        for i, fe in enumerate(self.feature_extractor):
            if len(self.target_bands[i]) and len(self.target_fwhm[i])  and \
                len(self.source_bands) and len(self.source_fwhm):
                X = band_resample_hsi_cube(base, self.source_bands,
                                           self.target_bands[i],
                                           self.source_fwhm,
                                           self.target_fwhm[i])
            else:
                X = np.copy(base)

            #Feature extraction
            final = np.append(final, fe.transform(X), axis=-1)

        X = np.copy(final)
        del base, final

        if self.pad:
            #TODO: generalize this to N-dimensions
            X = X[self.pad[0][0]:-self.pad[0][1],
                  self.pad[1][0]:-self.pad[1][1]]

        if len(self.feature_scaler):
            for feature_scaler_i in self.feature_scaler:
                X = feature_scaler_i.transform(X)

        sh = X.shape
        if len(X.shape) > 2:
            X = X.reshape(-1, X.shape[-1])

        if self.post_processor:
            prob = self.classifier.predict_proba(X)
            pred = self.post_processor.predict(
                prob.reshape(sh[0], sh[1], -1) + 1e-50)
            pred = pred.ravel()
        else:
            pred = self.classifier.predict(X)

        return pred
Esempio n. 2
0
    def predict_proba(self, X):
        """
        Finds probabilities of possible outcomes using samples from X.
        
        Args:
            X (ndarray): the testing data
        
        Returns: ndarray of possible outcomes based on samples from X
        """

        if len(self.pre_processor):
            for j in range(len(self.pre_processor)):
                X = self.pre_processor[j].transform(X)

        if self.pad:
            X = np.pad(X, self.pad, mode='constant')

        base = np.copy(X)
        final = np.zeros(X.shape[:2] + (0, ), dtype=np.float32)

        for i, fe in enumerate(self.feature_extractor):
            if len(self.target_bands[i]) and len(self.target_fwhm[i])  and \
                len(self.source_bands) and len(self.source_fwhm):
                X = band_resample_hsi_cube(base, self.source_bands,
                                           self.target_bands[i],
                                           self.source_fwhm,
                                           self.target_fwhm[i])
            else:
                X = np.copy(base)

            #Feature extraction
            final = np.append(final, fe.transform(X), axis=-1)

        X = np.copy(final)
        del base, final

        if self.pad:
            #TODO: generalize this to N-dimensions
            X = X[self.pad[0][0]:-self.pad[0][1],
                  self.pad[1][0]:-self.pad[1][1]]

        if len(self.feature_scaler):
            for feature_scaler_i in self.feature_scaler:
                X = feature_scaler_i.transform(X)

        if len(X.shape) > 2:
            X = X.reshape(-1, X.shape[-1])

        proba = self.classifier.predict_proba(X)

        return proba
Esempio n. 3
0
    def fit(self, X_train, y_train, X_val, y_val):
        """
        Fits to training data.
        
        Args:
            X_train (ndarray): the X training data
            y_train (ndarray): the y training data
            X_val (ndarray): the X validation data
            y_val (ndarray): the y validation data
        """

        same = np.all(X_train == X_val)

        #Data pre-processing
        for i in range(len(self.pre_processor)):
            self.pre_processor[i].fit(X_train)
            X_train = self.pre_processor[i].transform(X_train)
            if not same:
                X_val = self.pre_processor[i].transform(X_val)

        if self.pad:
            X_train = np.pad(X_train, self.pad, mode='constant')
            if not same:
                X_val = np.pad(X_val, self.pad, mode='constant')

        if not self.feature_extractor:
            self.feature_extractor = [DummyOp()]
            self.target_bands = [[]]
            self.target_fwhm = [[]]
        elif not isinstance(self.feature_extractor, list):
            self.feature_extractor = [self.feature_extractor]
            self.target_bands = [self.target_bands]
            self.target_fwhm = [self.target_fwhm]

        base_train = np.copy(X_train)
        final_train = np.zeros((base_train.shape[:2] + (0, )),
                               dtype=np.float32)
        if not same:
            base_val = np.copy(X_val)
            final_val = np.zeros((base_val.shape[:2] + (0, )),
                                 dtype=np.float32)

        for i, fe in enumerate(self.feature_extractor):
            #Band resampling
            if len(self.target_bands[i]) and len(self.target_fwhm[i]) and \
                len(self.source_bands) and len(self.source_fwhm):
                X_train = band_resample_hsi_cube(base_train, self.source_bands,
                                                 self.target_bands[i],
                                                 self.source_fwhm,
                                                 self.target_fwhm[i])
                if not same:
                    X_val = band_resample_hsi_cube(base_val, self.source_bands,
                                                   self.target_bands[i],
                                                   self.source_fwhm,
                                                   self.target_fwhm[i])
            #Feature extraction
            X_train = fe.transform(X_train)
            if not same:
                X_val = fe.transform(X_val)

            final_train = np.append(final_train, X_train, axis=-1)
            if not same:
                final_val = np.append(final_val, X_val, axis=-1)

        X_train = np.copy(final_train)
        del base_train, final_train
        if not same:
            X_val = np.copy(final_val)
            del base_val, final_val

        if self.pad:
            X_train = X_train[self.pad[0][0]:-self.pad[0][1],
                              self.pad[1][0]:-self.pad[1][1]]
            if not same:
                X_val = X_val[self.pad[0][0]:-self.pad[0][1],
                              self.pad[1][0]:-self.pad[1][1]]

        print(X_train.shape)
        if len(self.feature_scaler):
            for feature_scaler_i in self.feature_scaler:
                feature_scaler_i.fit(X_train)  #TODO: Combine train and val
                X_train = feature_scaler_i.transform(X_train)
                if not same:
                    X_val = feature_scaler_i.transform(X_val)

        if same:
            X_val = X_train

        if self.post_processor:
            feature2d = X_val
            val_idx = y_val.ravel() > -1

        #Reshape to N x F
        if len(X_train.shape) > 2:
            X_train = X_train.reshape(-1, X_train.shape[-1])
        if len(X_val.shape) > 2:
            X_val = X_val.reshape(-1, X_val.shape[-1])
        if len(y_train.shape) > 1:
            y_train = y_train.ravel()
        if len(y_val.shape) > 1:
            y_val = y_val.ravel()

        if not self.semisupervised:
            X_train = X_train[y_train > -1]
            y_train = y_train[y_train > -1]
        else:
            X_train = X_train[y_train >= -1]
            y_train = y_train[y_train >= -1]

        X_val = X_val[y_val > -1]
        y_val = y_val[y_val > -1]

        print("Train: %d x %d" % (X_train.shape[0], X_train.shape[1]))
        print("Val: %d x %d" % (X_val.shape[0], X_val.shape[1]))

        #Fit classifier
        self.classifier.fit(X_train, y_train, X_val, y_val)

        #Fit MRF/CRF
        if self.post_processor:
            sh = feature2d.shape
            prob_map = self.classifier.predict_proba(
                feature2d.reshape(-1, sh[-1]))
            prob_map = prob_map.reshape(sh[0], sh[1], -1)
            self.post_processor.fit(prob_map + 1e-50, y_val, val_idx)
Esempio n. 4
0
 def band_resample(self):
     """Performs band resampling operation"""
     self.data = band_resample_hsi_cube(self.data, self.bands,
                                        self.tgt_bands, self.fwhm,
                                        self.tgt_fwhm)