Пример #1
0
    def train(self, data_folder, objects='all',gmm=None, feature_extraction='msc_fv_3', mc_feature=True):
        objects_to_train = []
        object_directories = np.array(glob.glob(data_folder + '/*'))
        if objects == 'all':
            for obj_dir in object_directories:
                print (obj_dir)
                object_name = obj_dir.split('/')[-1]
                objects_to_train.append(str(object_name))
        else:
            objects_to_train = objects

        print ("Extracting features....")
        n = 0
        feature_pool = np.empty([0, 0])
        label_pool = []
        for obj in objects_to_train:
            files = np.array(glob.glob(data_folder + '/' + obj + '/*'))
            print (obj, ": ", len(files))
            for f in files:
                points = self.parse_pcd(f, enable_color=True)
                #print ("points",points.shape)
                if feature_extraction == 'mc':
                    features = calculate_feature_vector(points, enable_color=True)
                else:
                    features = calculate_maxrdd_fv_features(points, gmm, feature_extraction, mean_circle=mc_feature)
                
                if n < 1:
                    feature_pool = np.array(features)
                    label_pool = [obj]
                else:
                    feature_pool = np.vstack([feature_pool, features])
                    label_pool.append(obj)
                n += 1

        mean = np.mean(feature_pool, axis=0)
        std = np.std(feature_pool, axis=0)
        #feature_pool -= mean
        #feature_pool /= std

        print (feature_pool.shape)

        feature_pool = np.nan_to_num(feature_pool)
        print ("Classifying....", feature_pool.shape)

        label_encoder = sklearn.preprocessing.LabelEncoder()
        label_encoder.fit(label_pool)
        encoded_labels = label_encoder.transform(label_pool)[:, np.newaxis]
        encoded_labels = np.squeeze(encoded_labels.T)

        classifier = sklearn.ensemble.RandomForestClassifier(n_estimators=10)
        classifier.fit(feature_pool, encoded_labels)

        return SVMObjectClassifier(classifier, label_encoder, mean, std)
Пример #2
0
    def train_using_pgz(self, data_folder, gmm, feature_extraction='msc_fv_3', objects='all', datasize=2):
        data = None
        labels = None
        count = 0
        for i in range(datasize):
            print ("Processing: {}data{}.pgz".format(data_folder,i))
            dataset = self.load_compressed_pickle_file('{}data{}.pgz'.format(data_folder,i))
            if i == 0:
                data = dataset['data']
                labels = dataset['labels']
            else:
                data = np.concatenate((data, dataset['data']))
                labels = np.concatenate((labels, dataset['labels']))
        
        print ("Computing features for datasize: {}".format(data.shape))
        print ("Label shape: ", labels.shape)
        n = 0
        feature_pool = np.empty([0, 0])
        label_pool = []
        for i,points in enumerate(data):
            if feature_extraction == 'mc':
                features = calculate_feature_vector(points)
            else:
                features = calculate_maxrdd_fv_features(points, gmm, feature_extraction, mean_circle=False)
            if i < 1:
                #if not np.isnan(features.any()):
                feature_pool = np.array(features)
                label_pool = [labels[i]]
            else:
                #if not np.isnan(features.any()):
                feature_pool = np.vstack([feature_pool, features])
                label_pool.append(labels[i])

        print ("Done features computation")
        
        # Whitenin, and remember to whiten the input for inference
        mean = np.mean(feature_pool, axis=0)
        std = np.std(feature_pool, axis=0)
        #feature_pool -= mean
        #feature_pool /= std
        feature_pool = np.nan_to_num(feature_pool)
        print ("Classifying....", feature_pool.shape)
        label_encoder = sklearn.preprocessing.LabelEncoder()
        label_encoder.fit(label_pool)
        encoded_labels = label_encoder.transform(label_pool)[:, np.newaxis]
        encoded_labels = np.squeeze(encoded_labels.T)

        classifier = sklearn.ensemble.RandomForestClassifier(n_estimators=10)
        classifier.fit(feature_pool, encoded_labels)

        return SVMObjectClassifier(classifier, label_encoder, mean, std)
def evaluate_cls_using_pgz(data_folder,
                           gmm,
                           classifier,
                           feature_extraction='mc',
                           idx=[5, 6],
                           objects='all',
                           color=False):
    data = None
    labels = None
    count = 0
    for i in range(idx[0], idx[1]):
        print("Loading:...", '{}data{}.pgz'.format(data_folder, i))
        dataset = load_compressed_pickle_file('{}data{}.pgz'.format(
            data_folder, i))
        print(dataset['data'].shape)
        if count == 0:
            data = dataset['data']
            labels = dataset['labels']
        else:
            data = np.concatenate((data, dataset['data']))
            labels = np.concatenate((labels, dataset['labels']))
        count += 1

    print(labels.shape)

    seen = np.zeros((len(labels)))
    correct = np.zeros((len(labels)))

    for i, points in enumerate(data):
        #print ("ITERA",i)
        #files = np.array(glob.glob(self.data_folder + '/' + obj + '/*'))
        if feature_extraction == 'mc':
            features = calculate_feature_vector(points, False)
        else:
            features = calculate_maxrdd_fv_features(points, gmm,
                                                    feature_extraction)
        #features = np.nan_to_num(features)
        features = np.reshape(features, (1, -1))
        pred, probability = classifier.classify(features)
        seen[i] += 1
        if pred == labels[i]:
            correct[i] += 1
        print("True: {}, Pred: {}".format(pred, labels[i]))

    print("Seen: ", seen)
    print("Correct: ", correct)
    print("Avg class acc: ", correct / seen)
    print("Accuracy: ", np.mean(correct, axis=0))

    return np.mean(correct, axis=0)
def evaluate_maxrdd_fv_cls(data_folder,
                           classifier,
                           objects='all',
                           color=False,
                           gmm=None,
                           feature_extraction='msc_fv_3',
                           mc_feature=True):

    objects_to_eval = []
    object_directories = np.array(glob.glob(data_folder + '/*'))
    for obj_dir in object_directories:
        print(obj_dir)
        object_name = obj_dir.split('/')[-1]
        objects_to_eval.append(str(object_name))
    seen = np.zeros((len(objects_to_eval)))
    correct = np.zeros((len(objects_to_eval)))
    print(objects_to_eval)
    for i, obj in enumerate(objects_to_eval):
        files = np.array(glob.glob(data_folder + '/' + obj + '/*'))
        for f in files:
            points = parse_pcd(f, enable_color=True)
            if feature_extraction == 'mc':
                features = calculate_feature_vector(points, enable_color=True)
            else:
                features = calculate_maxrdd_fv_features(points,
                                                        gmm,
                                                        feature_extraction,
                                                        mean_circle=mc_feature)
            #xyzhsv = parse_pcd(f)
            #features = calculate_feature_vector(xyzhsv, False)
            features = np.reshape(features, (1, -1))
            print("Shape: ", features.shape)
            label, probability = classifier.classify(features)
            seen[i] += 1
            if label == str(obj):
                correct[i] += 1
            print("True: {}, Pred: {}".format(str(obj), label))

    print("Seen: ", seen)
    print("Correct: ", correct)
    print("Avg class acc: ", correct / seen)
    print("Accuracy: ", np.sum(correct / seen) / len(objects_to_eval))

    return np.mean(np.sum(correct / seen) / len(objects_to_eval))
def evaluate_cls(data_folder, classifier, objects='all', color=False):
    objects_to_test = os.listdir(os.path.abspath(data_folder))
    print("Training classifer for objects: ", objects_to_test)

    seen = np.zeros((len(objects_to_test)))
    correct = np.zeros((len(objects_to_test)))

    for i, obj in enumerate(objects_to_test):
        class_folder = os.path.join(data_folder, obj, "")
        files = glob.glob(class_folder + '**/*.pcd', recursive=True)
        for f in files:
            #xyzhsv = convert_to_xyzhsv(f)
            xyzhsv = parse_pcd(f)
            features = calculate_feature_vector(xyzhsv, False)
            features = np.reshape(features, (1, -1))
            label, probability = classifier.classify(features)
            seen[i] += 1
            if label == str(obj):
                correct[i] += 1

    print("Seen: ", seen)
    print("Correct: ", correct)
    print("Avg class acc: ", correct / seen)
    print("Accuracy: ", np.mean(correct, axis=0))