コード例 #1
0
def main():
    #flag for debugging
    f_subset = True
    hard_neg = False
    boosting_type = 'Ada'
    te = 100 if not f_subset else 20  # training epochs
    acd = 'wc_activations.npy' if not f_subset else 'wc_activations_subset.npy'
    chosen_wc_cache_dir = 'chosen_wcs.pkl' if not f_subset else 'chosen_wcs_subset.pkl'

    #data configurations
    pos_data_dir = 'newface16'
    neg_data_dir = 'nonface16'  # HERE,  WE CONSIDER THE DIMENSIONS OF  THE FACE AND THE NON- FACE IMAGES TO BE 16X16 FOR PERFORMIMNG THE ADABOOST FACE RECOGNITION
    img_w = 16
    img_h = 16
    data, labels = load_data(pos_data_dir, neg_data_dir, img_w, img_h,
                             f_subset)
    data = integrate_imgs(normalize(data))

    #number of bins for boosting
    nb = 25

    #number of cpus for parallel computing
    nc = 8 if not f_subset else 1  #always use 1 when debugging

    filters = generate_Haar_filters(4, 4, 16, 16, img_w, img_h, f_subset)

    drawer = Visualizer([10, 20, 50, 100], [1, 10, 20, 50, 100])

    boost = Boosting_Classifier(filters, data, labels, te, nb, drawer, nc,
                                boosting_type)

    #calculate filter values for all training imgs
    start = time.clock()
    #boost.calculate_training_activations(acd, acd)
    boost.calculate_training_activations(acd, acd)
    end = time.clock()
    print('%f seconds for activation calculation' % (end - start))

    boost.train(chosen_wc_cache_dir, chosen_wc_cache_dir)

    boost.visualize()

    #face detection starts from here
    org_img = cv2.imread('./Testing_imgs/Face_1.jpg', cv2.IMREAD_GRAYSCALE)
    result_img = boost.face_detection(org_img)
    cv2.imwrite('Result_img_%s.png' % boosting_type, result_img)

    if hard_neg == True:
        #neg_img

        start = time.clock()
        for i in range(1, 3):
            import os
            sd = os.path.join(os.path.curdir, 'Testing_imgs')
            print(os.listdir(sd))
            ipath = os.path.join(sd, 'Non_Face_' + str(i) + '.jpg')
            print(ipath)
            img = cv2.imread(ipath, cv2.IMREAD_GRAYSCALE)
            print("i", i)
            new_data = boost.get_hard_negative_patches(img)
            #boost.calculate_training_activations(, )
            load_dir = "hard_neg_" + str(i) + acd
            save_dir = "hard_neg_" + str(i) + acd
            print(
                'Calculate activations for %d weak classifiers, using %d imags.'
                % (len(boost.weak_classifiers), new_data.shape[0]))
            if load_dir is not None and os.path.exists(load_dir):
                print('[Find cached activations, %s loading...]' % load_dir)
                wc_activations = np.load(load_dir)
            else:
                if boost.nc == 1:
                    wc_activations = [
                        wc.apply_filter(new_data)
                        for wc in boost.weak_classifiers
                    ]
                else:
                    from joblib import Parallel, delayed
                    wc_activations = Parallel(n_jobs=boost.nc)(
                        delayed(wc.apply_filter)(new_data)
                        for wc in boost.weak_classifiers)
                wc_activations = np.array(wc_activations)
                if save_dir is not None:
                    print('Writing results to disk...')
                    np.save(save_dir, wc_activations)
                    print('[Saved calculated activations to %s]' % save_dir)
            for wc in boost.weak_classifiers:
                wc.activations = np.concatenate(
                    (wc.activations, wc_activations[wc.id, :]))
            boost.data = np.concatenate((boost.data, new_data))
            newlabels = np.ones((new_data.shape[0]))
            newlabels = newlabels * -1
        boost.labels = np.concatenate((boost.labels, newlabels))
        end = time.clock()

        print('%f seconds for activation calculation' % (end - start))

        boost.train(chosen_wc_cache_dir, chosen_wc_cache_dir)

        boost.visualize()
コード例 #2
0
ファイル: main.py プロジェクト: shwetha1729/FaceDetection
def main():
    #flag for debugging
    flag_subset = True
    hard_neg = False
    boosting_type = 'Ada'  #'Real' or 'Ada'
    training_epochs = 100 if not flag_subset else 20
    act_cache_dir = 'wc_activations.npy' if not flag_subset else 'wc_activations_subset.npy'
    chosen_wc_cache_dir = 'chosen_wcs.pkl' if not flag_subset else 'chosen_wcs_subset.pkl'

    #data configurations
    pos_data_dir = 'newface16'
    neg_data_dir = 'nonface16'
    image_w = 16
    image_h = 16
    data, labels = load_data(pos_data_dir, neg_data_dir, image_w, image_h,
                             flag_subset)
    data = integrate_images(normalize(data))

    #number of bins for boosting
    num_bins = 25

    #number of cpus for parallel computing
    num_cores = 8 if not flag_subset else 1  #always use 1 when debugging

    #create Haar filters
    filters = generate_Haar_filters(4, 4, 16, 16, image_w, image_h,
                                    flag_subset)

    #create visualizer to draw histograms, roc curves and best weak classifier accuracies
    drawer = Visualizer([10, 20, 50, 100], [1, 10, 20, 50, 100])

    #create boost classifier with a pool of weak classifier
    boost = Boosting_Classifier(filters, data, labels, training_epochs,
                                num_bins, drawer, num_cores, boosting_type)

    #calculate filter values for all training images
    start = time.clock()
    #boost.calculate_training_activations(act_cache_dir, act_cache_dir)
    boost.calculate_training_activations(act_cache_dir, act_cache_dir)
    end = time.clock()
    print('%f seconds for activation calculation' % (end - start))

    boost.train(chosen_wc_cache_dir, chosen_wc_cache_dir)

    boost.visualize()
    #face detection
    original_img = cv2.imread('./Testing_Images/Face_1.jpg',
                              cv2.IMREAD_GRAYSCALE)
    result_img = boost.face_detection(original_img)
    cv2.imwrite('Result_img_%s.png' % boosting_type, result_img)

    if hard_neg == True:
        #neg_img

        start = time.clock()
        for i in range(1, 3):
            import os
            sd = os.path.join(os.path.curdir, 'Testing_images')
            print(os.listdir(sd))
            ipath = os.path.join(sd, 'Non_Face_' + str(i) + '.jpg')
            print(ipath)
            img = cv2.imread(ipath, cv2.IMREAD_GRAYSCALE)
            print("i", i)
            new_data = boost.get_hard_negative_patches(img)
            #boost.calculate_training_activations(, )
            load_dir = "hard_neg_" + str(i) + act_cache_dir
            save_dir = "hard_neg_" + str(i) + act_cache_dir
            print(
                'Calculate activations for %d weak classifiers, using %d imags.'
                % (len(boost.weak_classifiers), new_data.shape[0]))
            if load_dir is not None and os.path.exists(load_dir):
                print('[Find cached activations, %s loading...]' % load_dir)
                wc_activations = np.load(load_dir)
            else:
                if boost.num_cores == 1:
                    wc_activations = [
                        wc.apply_filter(new_data)
                        for wc in boost.weak_classifiers
                    ]
                else:
                    from joblib import Parallel, delayed
                    wc_activations = Parallel(n_jobs=boost.num_cores)(
                        delayed(wc.apply_filter)(new_data)
                        for wc in boost.weak_classifiers)
                wc_activations = np.array(wc_activations)
                if save_dir is not None:
                    print('Writing results to disk...')
                    np.save(save_dir, wc_activations)
                    print('[Saved calculated activations to %s]' % save_dir)
            for wc in boost.weak_classifiers:
                wc.activations = np.concatenate(
                    (wc.activations, wc_activations[wc.id, :]))
            boost.data = np.concatenate((boost.data, new_data))
            newlabels = np.ones((new_data.shape[0]))
            newlabels = newlabels * -1
        boost.labels = np.concatenate((boost.labels, newlabels))
        end = time.clock()

        print('%f seconds for activation calculation' % (end - start))

        boost.train(chosen_wc_cache_dir, chosen_wc_cache_dir)

        boost.visualize()
コード例 #3
0
def main():
	#flag for debugging
	flag_subset = False
	boosting_type = 'Ada' #'Real' or 'Ada'
	training_epochs = 100 if not flag_subset else 20
	act_cache_dir = 'wc_activations.npy' if not flag_subset else 'wc_activations_subset.npy'
	chosen_wc_cache_dir = 'chosen_wcs.pkl' if not flag_subset else 'chosen_wcs_subset.pkl'

	#data configurations
	pos_data_dir = 'newface16/newface16'
	neg_data_dir = 'nonface16/nonface16'
    
	image_w = 16
	image_h = 16
	data, labels = load_data(pos_data_dir, neg_data_dir, image_w, image_h, flag_subset)
	
	#compute integral image
	data = integrate_images(normalize(data))

	#number of bins for boosting
	num_bins = 25

	#number of cpus for parallel computing
	num_cores = -1 if not flag_subset else 1 #always use 1 when debugging
	
	#create Haar filters
	filters = generate_Haar_filters(4, 4, 16, 16, image_w, image_h, flag_subset)
    
	#create visualizer to draw histograms, roc curves and best weak classifier accuracies
	drawer = Visualizer([10, 20, 50, 100], [1, 10, 20, 50, 100])
	
	#create boost classifier with a pool of weak classifier
	boost = Boosting_Classifier(filters, data, labels, training_epochs,num_bins, drawer, num_cores, boosting_type)
    
	
    #calculate filter values for all training images
	start = time.clock()
	
	#calculating activations by applying the Haar filters to the integral images.
	boost.calculate_training_activations(act_cache_dir, act_cache_dir)
	end = time.clock()
	print('%f seconds for activation calculation' % (end - start))

	#Calling the training function for error calculationa nd weight updation
	boost.train(chosen_wc_cache_dir)

	#visualisation of results: Plotting of Histograms, ROC curves, graphs
	boost.visualize()
	
	#Face Detection for 3 test images
	for i in range(3):
		original_img = cv2.imread('Face_%d.jpg' %i, cv2.IMREAD_GRAYSCALE)
		result_img = boost.face_detection(original_img)
		cv2.imwrite('Result_img_%s_%d.png' % boosting_type, %i, result_img)


    #hard negative mining
	for i in range(3):
		hard_neg_data_dir='Hard_neg_data_%d.pkl' %i
		hard_neg_label_dir='Hard_neg_labels_%d.pkl' %i
		#load hard negatives into original data and retrain
		if hard_neg_data_dir is not None and os.path.exists(hard_neg_data_dir):
			print('[Find cached hard negative data %s loading...]' % hard_neg_data_dir)
			patches=pickle.load(open(hard_neg_data_dir, 'rb'))
			hard_neg_labels=pickle.load(open(hard_neg_label_dir,'rb'))
			hard_neg_labels=hard_neg_labels[:,0,0]
			boost.data=np.concatenate((boost.data,patches), axis=0)
			boost.labels=np.concatenate((boost.labels, hard_neg_labels), axis=0)
		#compute hard negatives from non-face images and store result in pickle file
		else:
			back_img = cv2.imread('Non_face_%d.jpg' %(i+1), cv2.IMREAD_GRAYSCALE)
			patches = boost.get_hard_negative_patches( back_img,scale_step = 10)
			patches=patches[0]
			hard_labels=np.full(boost.labels.shape, fill_value=-1)
			pickle.dump(patches, open('Hard_neg_data_%d.pkl' %i, 'wb'))
			pickle.dump(hard_labels,open('Hard_neg_labels_%d.pkl' %i, 'wb'))
コード例 #4
0
def main():
    #flag for debugging
    flag_subset = False
    boosting_type = 'Ada'  #'Real' or 'Ada'
    training_epochs = 100 if not flag_subset else 20
    act_cache_dir = 'wc_activations.npy' if not flag_subset else 'wc_activations_subset.npy'
    chosen_wc_cache_dir = 'chosen_wcs.pkl' if not flag_subset else 'chosen_wcs_subset.pkl'

    #data configurations
    pos_data_dir = 'newface16'
    neg_data_dir = 'nonface16'
    image_w = 16
    image_h = 16
    data, labels = load_data(pos_data_dir, neg_data_dir, image_w, image_h,
                             flag_subset)
    data = integrate_images(normalize(data))
    #number of bins for boosting
    num_bins = 25

    #number of cpus for parallel computing
    num_cores = 6 if not flag_subset else 1  #always use 1 when debugging

    #create Haar filters
    filters = generate_Haar_filters(4, 4, 16, 16, image_w, image_h,
                                    flag_subset)

    #create visualizer to draw histograms, roc curves and best weak classifier accuracies
    drawer = Visualizer([10, 20, 50, 100], [1, 10, 20, 50, 100])

    #create boost classifier with a pool of weak classifier
    boost = Boosting_Classifier(filters, data, labels, training_epochs,
                                num_bins, drawer, num_cores, boosting_type)

    #calculate filter values for all training images
    start = time.clock()
    boost.calculate_training_activations(act_cache_dir, act_cache_dir)
    end = time.clock()
    print('%f seconds for activation calculation' % (end - start))

    boost.train(chosen_wc_cache_dir)

    if (boosting_type == 'Ada'):
        boost.visualize()
        original_img = cv2.imread('./Testing_Images/Face_4.jpg',
                                  cv2.IMREAD_GRAYSCALE)
        original_rgb_img = cv2.imread('./Testing_Images/Face_4.jpg')
        result_img = boost.face_detection(original_img, original_rgb_img)
        cv2.imwrite('Result_img_%s.png' % boosting_type, result_img)
        save_hard_neg = 'hard_neg.pkl'
        save_hard_neg_labels = 'hard_neg_label.pkl'
        if (os.path.exists(save_hard_neg)):
            final_hard_negatives = pickle.load(open(save_hard_neg, 'rb'))
            final_neg_labels = pickle.load(open(save_hard_neg_labels, 'rb'))
        else:
            for i in range(3):
                hard_negative_img = cv2.imread(
                    './Testing_Images/Non_face_' + str(i + 1) + '.jpg',
                    cv2.IMREAD_GRAYSCALE)
                hard_negatives = boost.get_hard_negative_patches(
                    hard_negative_img)
                print("No. of hard negative patches: ", hard_negatives.shape)
                hard_neg_labels = np.full((hard_negatives.shape[0]), -1)
                if (i == 0):
                    final_hard_negatives = hard_negatives
                    final_neg_labels = hard_neg_labels
                else:
                    final_hard_negatives = np.append(final_hard_negatives,
                                                     hard_negatives,
                                                     axis=0)
                    final_neg_labels = np.append(final_neg_labels,
                                                 hard_neg_labels,
                                                 axis=0)
            pickle.dump(final_hard_negatives, open(save_hard_neg, 'wb'))
            pickle.dump(final_neg_labels, open(save_hard_neg_labels, 'wb'))
        boost.data = np.append(boost.data, final_hard_negatives, axis=0)
        boost.labels = np.append(boost.labels, final_neg_labels, axis=0)
        new_act_cache_dir = 'new_wc_activations.npy' if not flag_subset else 'new_wc_activations_subset.npy'
        new_chosen_wc_cache_dir = 'new_chosen_wcs.pkl' if not flag_subset else 'new_chosen_wcs_subset.pkl'
        start = time.clock()
        boost.calculate_training_activations(new_act_cache_dir,
                                             new_act_cache_dir)
        end = time.clock()
        boost.train(new_chosen_wc_cache_dir)
        original_img = cv2.imread('./Testing_Images/Face_4.jpg',
                                  cv2.IMREAD_GRAYSCALE)
        original_rgb_img = cv2.imread('./Testing_Images/Face_4.jpg')
        result_img = boost.face_detection(original_img, original_rgb_img)
        cv2.imwrite('New_result_img_%s.png' % boosting_type, result_img)
    else:
        boost.real_visualize()