Exemplo n.º 1
0
    def train(self, train_ds):

        face_count = 0
        nonface_count = 0
        image_count = len(train_ds)

        # face and nonface image count
        for x in range(image_count):
            label = train_ds[x][1]
            if label == 1:  # Face
                face_count = face_count + 1
            else:
                nonface_count = nonface_count + 1

        # loading integral images of train_ds
        with open("train_integral_ds.pkl", 'rb') as f:
            train_ds_integral = pickle.load(f)
            print(
                "Integral images are loaded from pickle file for training the model"
            )

        w = np.zeros(image_count)  # sample_Weight

        for x in range(image_count):
            # Initial weight of every image (sample weight)
            if label == 1:  # Face
                w[x] = 1.0 / (2 * face_count)
            else:
                w[x] = 1.0 / (2 * nonface_count)

        # feature generation
        f = Feature(train_ds[0][0].shape)
        features = f.creating_all_features()

        # load features value (X) and classification (y) from pickle file
        # saves a lot of time while tuning in training phase
        with open("features_value_1.pkl", 'rb') as f:
            a = pickle.load(f)
        with open("features_value_2.pkl", 'rb') as f:
            b = pickle.load(f)
            X = np.concatenate((a, b), axis=0)
        with open("y.pkl", 'rb') as f:
            y = pickle.load(f)

        # Selecting weak classifiers
        for wc_i in range(self.total_wc):

            w = w / np.linalg.norm(w)

            trainedWeakClassifier = self.weakClassifier_training(
                X, y, features, w, face_count, nonface_count)

            bc = None
            bacc = None
            be = float('inf')

            for twc in trainedWeakClassifier:
                e = 0
                acc = []

                for integral_image, ww in zip(train_ds_integral, w):

                    predicted_label = twc.classify(integral_image[0])
                    true_label = integral_image[1]
                    acc.append(abs(predicted_label - true_label))

                    e += ww * abs(predicted_label - true_label)

                e = e / len(train_ds_integral)

                if e < be:
                    bc = twc
                    be = e
                    bacc = acc

            beta = be / (1.0 - be)

            for i in range(len(bacc)):
                w[i] = w[i] * (beta**(1 - bacc[i]))

            alpha = math.log(1.0 / beta)
            self.classifiersWeights.append(alpha)
            self.classifiers.append(bc)
Exemplo n.º 2
0
from rectangleArea import Rectangle
from feature import Feature


with open("train_ds.pkl", 'rb') as f:
    train_ds = pickle.load(f)

with open("train_integral_ds.pkl", 'rb') as f:
    train_ds_integral = pickle.load(f)


# creating all features for a certain window shape
s = time.time()

f = Feature(train_ds[0][0].shape)
features = f.creating_all_features()

e = time.time()

print("Feature creation is done")
print("Time for creating all features :")
print(e-s)


start = time.time()

X1, y1 = f.features_value(train_ds_integral)

end = time.time()

print("Feature value is calculated for all training images")