def train_dlib_detector(self): # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. options.add_left_right_image_flips = True # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 5 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True options.detection_window_size = 60 * 80 options.upsample_limit = 2 # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. dlib.train_simple_object_detector("../data/training/training_mtb.xml", "../data/models/detector_mtb.svm", options) options.detection_window_size = 40 * 80 dlib.train_simple_object_detector("../data/training/training_ped.xml", "../data/models/detector_ped.svm", options)
def train(self): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 dlib.train_simple_object_detector('../resources/model.xml', '../resources/model.svm', options)
def train(request): import os import sys import glob import dlib from skimage import io # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. options.add_left_right_image_flips = True # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 3 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = multiprocessing.cpu_count() options.be_verbose = True training_xml_path = os.path.join(settings.BASE_DIR, "training.xml") dlib.train_simple_object_detector(training_xml_path, "detector.svm", options) return HttpResponse('{"status": "completed"}', content_type="application/json")
def train(self): print("start training:") # 注:必须要将unicode转成普通字符串,否则dlib无法读取,这个编码上的bug也是坑了我两个小时 train_xml_path = str(self.train_xml_path) model_path = str(self.model_path) # print(train_xml_path) # print(model_path) # print(chardet.detect(train_xml_path)) # xml = 'F:\\Python\\Programs\\my_dlib_face_detection_application\\images\\test_object_detector\\cats_train\\cat.xml' # model = 'F:\\Python\\Programs\\my_dlib_face_detection_application\\images\\test_object_detector\\cats_train\\detector.svm' # print(xml) # print(model) # print(chardet.detect(xml)) dlib.train_simple_object_detector(train_xml_path, model_path, self.options) print("Training accuracy: {}".format(dlib.test_simple_object_detector(train_xml_path, model_path))) print("The SVM model is saved in {0}".format(model_path)) if self.log: self.log.append(u'--'*30) self.log.append("Training complete!") self.log.append("Training accuracy: {}".format(dlib.test_simple_object_detector(train_xml_path, model_path))) self.log.append("The SVM model is saved in {0}".format(model_path)) self.log.append(u'--'*30)
def train_dlib_detector(self): # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. #options.add_left_right_image_flips = True # seems to degrade performance in this case # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 9 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True options.upsample_limit = 2 # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. #options.detection_window_size = 60 * 80 options.detection_window_size = int((50 * 70)) # 468, 1.44 dlib.train_simple_object_detector("../data/annotations/bb/training/combined_mtb.xml", "../data/models/detector_mtb.svm", options) options.detection_window_size = int((40 * 90)) # 448, 2.29 dlib.train_simple_object_detector("../data/annotations/bb/training/combined_ped.xml", "../data/models/detector_ped.svm", options)
def trainObjects(images_folder): folders = os.listdir(images_folder) folders.remove('extras') detectors = [] for folder in folders: xml = os.path.join(images_folder, "{0}\{0}.xml".format(folder)) #print("{0}.svm".format(folder)) dlib.train_simple_object_detector(xml, "{0}.svm".format(folder), options) # save all detectors as list detectors.append("{0}.svm".format(folder)) # Next, suppose you have trained multiple detectors and you want to run them # efficiently as a group. You can do this as follows detectorsModels = list() for detector in detectors: detectorsModels.append(dlib.fhog_object_detector(detector)) # testing multiple detectors with image image = dlib.load_rgb_image( images_folder + '/head n shoulder/head-and-shoulder-best-oily-hair-shampoo.jpg') [boxes, confidences, detector_idxs ] = dlib.fhog_object_detector.run_multiple(detectorsModels, image, upsample_num_times=1, adjust_threshold=0.5) for i in range(len(boxes)): print("detector {} found box {} with confidence {}.".format( detector_idxs[i], boxes[i], confidences[i])) return detectorsModels, detectors
def test_params(C, nuclear_norm): options = dlib.simple_object_detector_training_options() # our dataset is oriented, so definitely don't add in flips. options.add_left_right_image_flips = False options.C = C options.num_threads = 1 options.be_verbose = False options.nuclear_norm_regularization_strength = nuclear_norm options.max_runtime_seconds = 5 # SET REALLY SMALL SO THE DEMO DOESN'T TAKE TO LONG, USE BIGGER VALUES FOR REAL USE!!!!! dlib.train_simple_object_detector( "images/small_face_dataset/cluster_001_train.xml", "detector1_.svm", options) # You can do a lot here. Run the detector through # dlib.threshold_filter_singular_values() for instance to make sure it # learns something that will work once thresholded. We can also add a # penalty for having a lot of filters. Run this program a few times and # try out different ways of penalizing the return from test_params() and # see what happens. result = dlib.test_simple_object_detector( "images/small_face_dataset/cluster_001_test.xml", "detector1_.svm") print("C = {}, nuclear_norm = {}".format(C, nuclear_norm)) print("testing accuracy: ", result) sys.stdout.flush() # For settings with the same average precision, we should prefer smaller C # since smaller C has better generalization. return result.average_precision - C * 1e-8
def train_object_detector(self): self.__print_training_message('object detector') opt = dlib.simple_object_detector_training_options() opt.add_left_right_image_flips = True opt.C = 5 opt.num_threads = self.cpu_cores opt.be_verbose = True opt.detection_window_size = self.window_size**2 dlib.train_simple_object_detector(self.xml, DETECTOR_SVM, opt)
def test_train_xml_detector(self): # This effectively tests that we can successfully load images options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 1 options.num_threads = 1 dlib.train_simple_object_detector('images.xml', "test.svm", options) self.assertTrue(os.path.exists('./test.svm'))
def train_object_detector_for_path(self, xmlPath, outputPath): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = False options.C = 5 options.num_threads = 4 options.be_verbose = True training_xml_path = os.path.join('tmp', xmlPath) dlib.train_simple_object_detector(training_xml_path, outputPath, options)
def training_data(train_xml, test_xml, detectorName, Csvm=5): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = Csvm options.num_threads = 4 options.be_verbose = True dlib.train_simple_object_detector(train_xml, detectorName + ".svm", options) print("Testing accuracy: {}".format( dlib.test_simple_object_detector(test_xml, detectorName + ".svm")))
def test_train_xml_detector(self): # This effectively tests that we can successfully load images options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 1 options.num_threads = 1 dlib.train_simple_object_detector(self.images_xml_path, './test.svm', options) self.assertTrue(os.path.exists('./test.svm'))
def train_shape_detector(self, output_name, input_xml): print('[INFO] training shape detector....') # get the training options options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = False options.C = 5 options.num_threads = 4 options.be_verbose = True # start training the model dlib.train_simple_object_detector(input_xml, output_name, options)
def train_with_dlib(self, obj_folder, detector_path, train_file_name, test_file_name): pdb.set_trace() logger = MyUtils.Tee("{0}/{1}.log".format(obj_folder, 'run'), 'w') logger.write('tee: dlib training called') logger.write('dlib training called') # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 5 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 2 options.be_verbose = True training_xml_path = os.path.join(obj_folder, train_file_name) testing_xml_path = os.path.join(obj_folder, test_file_name) # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. logger.write('start training. saved detector path: ' + detector_path) dlib.train_simple_object_detector(training_xml_path, detector_path, options) logger.write( 'end training') # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will logger.write((the precision, recall, and then) # average precision. logger.write("") # Print blank line to create gap from previous output logger.write("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, detector_path))) # However, to get an idea if it really worked without overfitting we need to # run it on images it wasn't trained on. The next line does this. Happily, we # see that the object detector works perfectly on the testing images. accuracy = dlib.test_simple_object_detector(testing_xml_path, "detector.svm") logger.write("Testing accuracy: {}".format(accuracy)) logger.flush() return accuracy
def train(): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 options.num_threads = 4 options.be_verbose = True options.detection_window_size = 1024 options.match_eps = 0.1 training_xml_path = "../pics/train/training-single.xml" # training_xml_path = "/home/external/moderation-p**n-detector/oboobs.dlibxml" dlib.train_simple_object_detector(training_xml_path, "../boobs.svm", options) print("Training accuracy: {}".format(dlib.test_simple_object_detector(training_xml_path, "../boobs.svm")))
def training(): """ 训练样本 """ print('training samples ...') options = dlib.simple_object_detector_training_options() # 要识别的图形是否左右对称 options.add_left_right_image_flips = True # C越大表示更好地去拟合训练集,当然也有可能造成过拟合。通过尝试不同C在测试集上的效果得到最佳值 options.C = 5 # 训练数据时开启的线程数 options.num_threads = 2 # 是否输出详细日志 options.be_verbose = False dlib.train_simple_object_detector(samples_xml, samples_svm, options) print('finished.')
def create_svm(request,pk,template_name='create_svm.html'): brand = get_object_or_404(Brands, pk=pk) data = {} data[ 'pk' ] = pk if request.method == 'POST': svm = request.POST.get('svm') cval = request.POST.get('cval') paths = Paths.objects.filter( brand_id = pk ) if paths.exists(): for pt in paths: train_path = pt.train_path train_path = os.path.relpath(train_path,settings.MEDIA_ROOT) test_path = pt.test_path test_path = os.path.relpath(test_path,settings.MEDIA_ROOT) svm_path = pt.svm_path #cmd = "python 35Hawkdetect.py /var/sites/thirdauth/static/images/companies/Stovekraft\ Private\ Limited\(mohsin\)/idea/test/ cola123.svm jpg" print svm_path print settings.MEDIA_ROOT+"/"+train_path+"/" faces_folder = settings.MEDIA_ROOT+"/"+train_path+"/" test_folder = settings.MEDIA_ROOT+"/"+test_path+"/" print faces_folder #dest = "/var/sites/thirdauth/static/images/companies/Stovekraft Private Limited(mohsin)/Docomo/svm" timestr = svm+time.strftime("_%m_%d_%H_%M")+".svm" print timestr outputpath = str(svm)+".svm" cval = cval options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = int(cval) options.num_threads = 4 options.be_verbose = True training_xml_path = os.path.join(str(faces_folder), "training.xml") testing_xml_path = os.path.join(str(test_folder), "training.xml") dlib.train_simple_object_detector(training_xml_path,outputpath, options) print("") print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, outputpath))) print("Testing accuracy: {}".format( dlib.test_simple_object_detector(testing_xml_path, outputpath))) result = "Training accuracy: {}"+format(dlib.test_simple_object_detector(training_xml_path, outputpath))+" Testing accuracy: {}"+format(dlib.test_simple_object_detector(testing_xml_path, outputpath)) os.rename(str(outputpath),timestr) if os.path.exists(str(svm_path)+"/"+str(timestr)): os.remove(str(svm_path)+"/"+str(timestr)) shutil.move("/var/sites/thirdauth/"+str(timestr),str(svm_path)) Svms(svm_name = str(timestr), brand_id = pk , company_id = brand.company_id ).save() return HttpResponse(result) else: return render(request,template_name, data ,context_instance=RequestContext(request))
def do_training(self): trainpath = os.path.join(self.translate_path(self.path), "train.txt") trainfile = open(trainpath, 'r') testdatadef = trainfile.readlines() trainfile.close() result = [] images = [] boxes = [] for testdata in testdatadef: testvalues = testdata.split(";") boxes.append([ dlib.rectangle(int(testvalues[0]), int(testvalues[1]), int(testvalues[2]), int(testvalues[3])) ]) imagepath = os.path.join(self.translate_path(self.path), testvalues[4].strip("\n")) result.append(testvalues[4]) images.append(dlib.load_grayscale_image(imagepath)) options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 # options.be_verbose = False start_time = time.clock() SimpleHTTPRequestHandler.trained_detector = dlib.train_simple_object_detector( images, boxes, options) self.step_times.append("Trained HOG + SVM Detection: %.3f s" % (time.clock() - start_time)) print(type(SimpleHTTPRequestHandler.trained_detector)) return result
def fit(self, paths, annotations, target_path, vis=True, n_thread=1, C=5, verbose=True, add_left_right_image_flips=True): self.__option.num_threads = n_thread self.__option.C = C self.__option.be_verbose = verbose self.__option.add_left_right_image_flips = add_left_right_image_flips images = self.__prep_image(paths) print('[IMAGE SIZE] {:.2f} MB'.format( sys.getsizeof(images) / 1024 * 1024)) annotations = self.__prep_annotations(annotations) print('[ANNOT SIZE] {:.2f} MB'.format( sys.getsizeof(annotations) / 1024 * 1024)) self.__detector = dlib.train_simple_object_detector( images, annotations, self.__option) if vis: win = dlib.image_window() win.set_image(self.__detector) dlib.hit_enter_to_continue() self.__detector.save(target_path)
def training(boxes, images): u"""学習""" # simple_object_detectorの訓練用オプションを取ってくる options = dlib.simple_object_detector_training_options() # 左右対照に学習データを増やすならtrueで訓練(メモリを使う) options.add_left_right_image_flips = True # SVMを使ってるのでC値を設定する必要がある options.C = 5 # スレッド数指定 options.num_threads = 16 # 学習途中の出力をするかどうか options.be_verbose = True # 停止許容範囲 # options.epsilon = 0.001 options.epsilon = 0.01 # サンプルを増やす最大数(大きすぎるとメモリを使う) options.upsample_limit = 1 # 矩形検出の最小窓サイズ(80*80=6400となる) options.detection_window_size = 6400 # options.detection_window_size = 100 # 学習してsvmファイルを保存 print('train...') detector = dlib.train_simple_object_detector(images, boxes, options) detector.save('./detector.svm')
def train_detector_given_image_list(myImageList, Annotation_Path, DetectorOutputFolder): # Set Default Options options = dlib.simple_object_detector_training_options() images = [] imagename = [] boxes = [] annotations = [] for imagefilename in myImageList: images.append(cv2.imread(imagefilename)) imagename.append(name) annotations = loadmat(os.path.join(Annotation_Path, name + ".xml")) bb = [ dlib.rectangle(left=x, top=y, right=w, bottom=h) for (y, h, x, w) in annotations ] boxes.append(bb) # Train the object detector print("[INFO] Training the Support Vector Machine Deector") mySVM_Detector = dlib.train_simple_object_detector(images, boxes, options) # Save the detector print("[INFO] Saving SVM Detector") mySVM_Detector.save(DetectorOutputFolder)
def train_detector(FolderName_Class, Annotation_Path, DetectorOutputFolder): # Set Default Options options = dlib.simple_object_detector_training_options() images = [] imagename = [] boxes = [] annotations = [] exts = [ '.bmp', '.pbm', '.pgm', '.ppm', '.sr', '.ras', '.jpeg', '.jpg', '.jpe', '.jp2', '.tiff', '.tif', '.png' ] # Loop over all images inside the specified folder (single class) myImageList = os.listdir(FolderName_Class) for imagefilename in myImageList: name, ext = os.path.splitext(imagefilename) if ext in exts: images.append(cv2.imread(imagefilename)) imagename.append(name) annotations = loadmat(os.path.join(Annotation_Path, name + ".xml")) bb = [ dlib.rectangle(left=x, top=y, right=w, bottom=h) for (y, h, x, w) in annotations ] boxes.append(bb) # Train the object detector print("[INFO] Training the Support Vector Machine Deector") mySVM_Detector = dlib.train_simple_object_detector(images, boxes, options) # Save the detector print("[INFO] Saving SVM Detector") mySVM_Detector.save(DetectorOutputFolder)
def model_training(imgs_path, xml_name): # 训练参数 options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True # 支持向量机的C参数,通常默认取为5.自己适当更改参数以达到最好的效果 options.C = 5 # 线程数,根据CPU核心数填写 options.num_threads = 1 options.be_verbose = True import time start_time = time.time() print(' -- 开始训练 -- ') dlib.train_simple_object_detector(imgs_path + xml_name, 'detector.svm', options) # print("训练精准度: {}".format( # dlib.test_simple_object_detector(imgs_path + xml_name, "detector.svm"))) print(f'--训练完成-- 耗时:{time.time() - start_time}s')
def main(): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 4 options.epsilon = 0.05 options.num_threads = 8 options.be_verbose = True training_xml_path = os.path.join(training_dir, file_name) # testing_xml_path = os.path.join(testing_dir, file_name) dlib.train_simple_object_detector(training_xml_path, detector_name, options) print("") print("================================") print("")
def do_train(self, training_xml_path, testing_xml_path, name_svm, hyperparameters): options = dlib.simple_object_detector_training_options() options.detection_window_size = int(hyperparameters[0]) # options.add_left_right_image_flips = True options.C = int(hyperparameters[2]) options.num_threads = int(hyperparameters[1]) options.epsilon = hyperparameters[3] options.be_verbose = True dlib.train_simple_object_detector(training_xml_path, name_svm, options) print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, name_svm))) print("Testing accuracy: {}".format( dlib.test_simple_object_detector(testing_xml_path, name_svm))) self.detector = name_svm
def train(): options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 options.num_threads = 4 options.be_verbose = True options.detection_window_size = 1024 options.match_eps = 0.1 training_xml_path = "../pics/train/training-single.xml" # training_xml_path = "/home/external/moderation-p**n-detector/oboobs.dlibxml" dlib.train_simple_object_detector(training_xml_path, "../boobs.svm", options) print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, "../boobs.svm")))
def train(training_xml_path, model_file="detector.svm"): assert os.path.isfile(training_xml_path) assert not os.path.isfile(model_file) # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. options.add_left_right_image_flips = True # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 10 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 6 options.epsilon = 0.001 options.be_verbose = True options.detection_window_size = 4096 #(32, 32) # options.upsample_limit = 8 # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. print("Goingt to train ...") dlib.train_simple_object_detector(training_xml_path, model_file, options) # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will print(the precision, recall, and then) # average precision. print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, model_file)))
def train(path_xml): # objeto contenedor de las opciones para la rutina train_simple_object_detector() # todas las opciones vienen con valores por defecto razonables # http://dlib.net/python/index.html#dlib.simple_object_detector_training_options options = dlib.simple_object_detector_training_options() options.C = 6 # parametro C de las SVM, valores grandes pueden llevar al overfitting options.add_left_right_image_flips = True # para objetos simetricos como las caras options.be_verbose = True options.epsilon = 0.005 # epsilon de detencion, valores pequeños -> accurate training # utilizando la herramienta https://imglab.ml dlib.train_simple_object_detector(path_xml, "detector.svm", options) print("\nTraining accuracy: ", dlib.test_simple_object_detector(path_xml, "detector.svm"))
def CREATE_SVM_DETECTOR(): options = dlib.simple_object_detector_training_options() options.C = 5 options.num_threads = 4 options.be_verbose = True # options.add_left_right_image_flips = True fname_xml_train = 'data/xml/cat_BHS.xml' # fname_xml_test = 'data/xml/testing.xml' dlib.train_simple_object_detector(fname_xml_train, "cat_detector.svm", options) print() print("Training accuracy: {}".format( dlib.test_simple_object_detector(fname_xml_train, "cat_detector.svm")))
def learnFeatureDlib(feat, tester): testImages = [] testImageBoxes = [] featureName = feat.featureName pathToClippingDirectory = tester.basePath + "Training/" + featureName + '/TrainDlib' pathToTrainingDirectory = pathToClippingDirectory + '/' + 'dlibTraining' trainingList = getListOfDlibSamples(pathToTrainingDirectory) for file in trainingList: testImageColor = cv2.imread(file) testImages.append(cv2.cvtColor(testImageColor, cv2.COLOR_BGR2GRAY)) numRows, numCols, numColors = testImageColor.shape boxList = getBoundingBoxList(file) boxesForThisImage = [] if not boxList is None: for box in boxList: left = box[0] right = left + feat.roiSideLength top = box[1] bottom = top + feat.roiSideLength print("Left: " + str(left) + ", Top: " + str(top) + "Right: " + str(right) + ", Bottom: " + str(bottom)) # if left<0 or top<0 or right>=numCols or bottom>=numRows: # print("Tossed box: Left: " + str(left) + ", Top: " + str(top) + "Right: " + str(right) + ", Bottom: " + str(bottom)) # else: # testBox=dlib.rectangle(left=box.xLeft,top=box.yTop,right=box.xRight,bottom=box.yBottom) testBox = dlib.rectangle(left=left, top=top, right=right, bottom=bottom) boxesForThisImage.append(testBox) testImageBoxes.append(boxesForThisImage) # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = feat.cParmValue options.U = feat.upSampling # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True detector = dlib.train_simple_object_detector(testImages, testImageBoxes, options) # We could save this detector to disk by uncommenting the following. detectorLocation = pathToClippingDirectory + "/detector.svm" detector.save(detectorLocation) print('Dlib Training Done') feat.model = detector
def step4(self, btn): #Based on dlib example: # Now let's do the training. The train_simple_object_detector() function has a # bunch of options, all of which come with reasonable default values. The next # few lines goes over some of these options. options = dlib.simple_object_detector_training_options() # Since faces are left/right symmetric we can tell the trainer to train a # symmetric detector. This helps it get the most value out of the training # data. options.add_left_right_image_flips = True # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 5 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True trainingXML = os.path.join(self.tmp, 'training.xml') #Ideally there would be half training, half testing: testingXML = os.path.join(self.tmp, "training.xml") # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. dlib.train_simple_object_detector( trainingXML, os.path.join(self.tmp, "detector.svm"), options) print("Training accuracy: {}".format( dlib.test_simple_object_detector( trainingXML, os.path.join(self.tmp, "detector.svm"))))
def train_detector(save_name): #Point to folder folder = "../data/TrainHOG/" coins_folder = folder #Now train a simple object detector using dlib #Using the folowing options options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 10 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True training_xml_path = os.path.join(coins_folder, "merged.xml") detective = os.path.join(coins_folder, str(save_name)) dlib.train_simple_object_detector(training_xml_path, detective, options) print("done training")
def trainer(self, imgs, ants): self.images = imgs self.annots = ants options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 options.num_threads = 4 options.be_verbose = True detector = dlib.train_simple_object_detector(self.images, self.annots, options) return detector
def simple_training(trainpath="Training/", nbthreads=4, cvalue=5): train_folder = trainpath #Para treinarmos nosso dataset, chamaremos a classe train_simple_object_detector() com todos os seus valores default (que são razoavelmente bons) options = dlib.simple_object_detector_training_options() #Cartas são de certa forma simétricas, então, melhoraremos a qualidade do resultado mudando o padrão de add_left_right_image_flips para True options.add_left_right_image_flips = True #O valor de C encoraja um fitting melhor nos dados, mas um C muito grande encoraja overfitting, valor 5 ainda é experimental, precisamos de mais teste options.C = cvalue # Quantos Threads temos disponíveis para treinar em paralelo? options.num_threads = nbthreads #Vamos acompanhar o processo options.be_verbose = True #Concatene o diretório do treino e seu .xml correspondente numa string training_xml_path = os.path.join(train_folder, "training.xml") #testing_xml_path = os.path.join(train_folder, "testing.xml") #Finalmente chamamos a função que faz o treino de fato. Salva o resultado em um detector .svm # a partir do nosso .xml (Dados do treino supervisionado) dlib.train_simple_object_detector(training_xml_path, "detector.svm", options) print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, "detector.svm"))) print("Process done sucesssfully")
def fit(self,images,annotations,visualize=False, savePath=None): annotations = self._prepare_annotations(annotations) images = self._prepare_images(images) print('IMAGES',images) print('annots',annotations) print(self.options) self._detector = dlib.train_simple_object_detector(images,annotations,self.options) if visualize: win = dlib.image_window() win.set_image(self._detector) dlib.hit_enter_to_continue() if savePath is not None: self._detector.save(savePath) return self
# loop over the image paths for imagePath in paths.list_images(args["class"]): # extract the image ID from the image path and load the annotations file imageID = imagePath[imagePath.rfind("/") + 1:].split("_")[1] imageID = imageID.replace(".png", "") p = "{}/annotation_{}.txt".format(args["annotations"], imageID) #annotations = loadmat(p)["box_coord"] annotations = getAnnotation(p) # loop over the annotations and add each annotation to the list of bounding # boxes bb = [dlib.rectangle(left=long(annotations[0]), top=long(annotations[1]), right=long(annotations[0]+annotations[2]), bottom=long(annotations[1]+annotations[3]))] boxes.append(bb) # add the image to the list of images images.append(io.imread(imagePath)) # train the object detector print("[INFO] training detector...") detector = dlib.train_simple_object_detector(images, boxes, options) # dump the classifier to file print("[INFO] dumping classifier to file...") detector.save(args["output"]) # visualize the results of the detector win = dlib.image_window() win.set_image(detector) dlib.hit_enter_to_continue()
import cv2 import dlib import os from skimage import filter, data, io from skimage.viewer import ImageViewer learning_folder = 'learning' options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 5 options.num_threads = 1 options.be_verbose = True training_xml_path = os.path.join(learning_folder,'info.xml') dlib.train_simple_object_detector(training_xml_path, 'info.svm', options) print ("") print ("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, "info.svm") )) detector = dlib.simple_object_detector("info.svm") win_det = dlib.image_window() win_det.set_image(detector) dlib.hit_enter_to_continue()
def train_dlib_detector(images, epsilon=0.01, add_left_right_image_flips=False, verbose_stdout=False, C=5, detection_window_size=6400, num_threads=None): r""" Train a dlib detector with the given list of images. This is intended to easily train a list of menpo images that have their bounding boxes attached as landmarks. Each landmark group on the image will have a tight bounding box extracted from it and then dlib will train given these images. Parameters ---------- images : `list` of `menpo.image.Image` The set of images to learn the detector from. Must have landmarks attached to **every** image, a bounding box will be extracted for each landmark group. epsilon : `float`, optional The stopping epsilon. Smaller values make the trainer's solver more accurate but might take longer to train. add_left_right_image_flips : `bool`, optional If ``True``, assume the objects are left/right symmetric and add in left right flips of the training images. This doubles the size of the training dataset. verbose_stdout : `bool`, optional If ``True``, will allow dlib to output its verbose messages. These will only be printed to the stdout, so will **not** appear in an IPython notebook. C : `int`, optional C is the usual SVM C regularization parameter. Larger values of C will encourage the trainer to fit the data better but might lead to overfitting. detection_window_size : `int`, optional The number of pixels inside the sliding window used. The default parameter of ``6400 = 80 * 80`` window size. num_threads : `int` > 0 or ``None`` How many threads to use for training. If ``None``, will query multiprocessing for the number of cores. Returns ------- detector : `dlib.simple_object_detector` The trained detector. To save this detector, call save on the returned object and pass a string path. Examples -------- Training a simple object detector from a list of menpo images and save it for later use: >>> images = list(mio.import_images('./images/path')) >>> in_memory_detector = train_dlib_detector(images, verbose_stdout=True) >>> in_memory_detector.save('in_memory_detector.svm') """ rectangles = [[pointgraph_to_rect(lgroup.lms.bounding_box()) for lgroup in im.landmarks.values()] for im in images] image_pixels = [menpo_image_to_uint8(im) for im in images] if num_threads is None: import multiprocessing num_threads = multiprocessing.cpu_count() options = dlib.simple_object_detector_training_options() options.epsilon = epsilon options.add_left_right_image_flips = add_left_right_image_flips options.be_verbose = verbose_stdout options.C = C options.detection_window_size = detection_window_size options.num_threads = num_threads return dlib.train_simple_object_detector(image_pixels, rectangles, options)
import dlib, sys, glob from skimage import io # cups_training = '/home/jyotiska/Dropbox/Computer Vision/cupdataset.xml' bottle_training = "/home/jyotiska/Dropbox/Computer Vision/bottledataset.xml" # bottle_test_dir = 'bottle_train' options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 4 options.epsilon = 0.01 options.num_threads = 8 options.be_verbose = True dlib.train_simple_object_detector(bottle_training, "bottledetector.svm", options) # print "\nTraining accuracy: ", dlib.test_simple_object_detector(cups_training,"cupdetector.svm")
# images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 5 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. dlib.train_simple_object_detector(faces_folder+"/training.xml","detector.svm", options) # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will print the precision, recall, and then # average precision. print "\ntraining accuracy:", dlib.test_simple_object_detector(faces_folder+"/training.xml", "detector.svm") # However, to get an idea if it really worked without overfitting we need to # run it on images it wasn't trained on. The next line does this. Happily, we # see that the object detector works perfectly on the testing images. print "testing accuracy: ", dlib.test_simple_object_detector(faces_folder+"/testing.xml", "detector.svm") # Now let's use the detector as you would in a normal application. First we
# Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 4 options.be_verbose = True training_xml_path = os.path.join(faces_folder, "training.xml") testing_xml_path = os.path.join(faces_folder, "testing.xml") # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. dlib.train_simple_object_detector(training_xml_path, "detector.svm", options) # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will print(the precision, recall, and then) # average precision. print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format( dlib.test_simple_object_detector(training_xml_path, "detector.svm"))) # However, to get an idea if it really worked without overfitting we need to # run it on images it wasn't trained on. The next line does this. Happily, we # see that the object detector works perfectly on the testing images. print("Testing accuracy: {}".format( dlib.test_simple_object_detector(testing_xml_path, "detector.svm")))
import dlib, sys, glob from skimage import io cups_training = '/home/jyotiska/Dropbox/Computer Vision/cupdataset.xml' cups_training_2 = '/home/jyotiska/Dropbox/Computer Vision/cupdataset_3.xml' cup_test_dir = 'Cups_train' options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True options.C = 4 options.epsilon = 0.01 options.num_threads = 8 options.be_verbose = True dlib.train_simple_object_detector(cups_training_2,"cupdetector_3.svm",options) # print "\nTraining accuracy: ", dlib.test_simple_object_detector(cups_training,"cupdetector.svm")
# does all the training import os import sys import glob import dlib # from skimage import io path = os.getcwd() + '/../' options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True # The trainer is a kind of support vector machine and therefore has the usual # SVM C parameter. In general, a bigger C encourages it to fit the training # data better but might lead to overfitting. You must find the best C value # empirically by checking how well the trained detector works on a test set of # images you haven't trained on. Don't just leave the value set at 5. Try a # few different C values and see what works best for your data. options.C = 5 # Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 2 options.be_verbose = True training_xml_path = os.path.join(path, 'data/mdl_detector/train.xml') testing_xml_path = os.path.join(path, 'data/mdl_detector/test.xml') # does the training dlib.train_simple_object_detector(training_xml_path, path + 'data/detector.svm', options)
# Tell the code how many CPU cores your computer has for the fastest training. options.num_threads = 8 options.be_verbose = True training_xml_path = "signs.xml" ## testing_xml_path = os.path.join(faces_folder, "testing.xml") # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. dlib.train_simple_object_detector(training_xml_path, TRAINING, options) # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will print(the precision, recall, and then) # average precision. print("") # Print blank line to create gap from previous output print("Training accuracy: {}".format(dlib.test_simple_object_detector(training_xml_path, TRAINING))) # Now let's use the detector as you would in a normal application. First we # will load it from disk. detector = dlib.simple_object_detector(TRAINING) # We can look at the HOG filter we learned. It should look like a face. Neat! win_det = dlib.image_window() win_det.set_image(detector)
options.num_threads = 2 options.be_verbose = True training_xml_path = os.path.join(obj_folder, "training.xml") testing_xml_path = os.path.join(obj_folder, "testing.xml") # This function does the actual training. It will save the final detector to # detector.svm. The input is an XML file that lists the images in the training # dataset and also contains the positions of the face boxes. To create your # own XML files you can use the imglab tool which can be found in the # tools/imglab folder. It is a simple graphical tool for labeling objects in # images with boxes. To see how to use it read the tools/imglab/README.txt # file. But for this example, we just use the training.xml file included with # dlib. print "start training" dlib.train_simple_object_detector(training_xml_path, "detector.svm", options) print "end training" # Now that we have a face detector we can test it. The first statement tests # it on the training data. It will print(the precision, recall, and then) # average precision. print ("") # Print blank line to create gap from previous output print ("Training accuracy: {}".format(dlib.test_simple_object_detector(training_xml_path, "detector.svm"))) # However, to get an idea if it really worked without overfitting we need to # run it on images it wasn't trained on. The next line does this. Happily, we # see that the object detector works perfectly on the testing images. print ("Testing accuracy: {}".format(dlib.test_simple_object_detector(testing_xml_path, "detector.svm"))) # Now let's use the detector as you would in a normal application. First we
import datetime import os bottle_training_data = 'helpers/bottles_dataset.xml' img_list = 'fetch_images/image_urls.txt' options = dlib.simple_object_detector_training_options() options.add_left_right_image_flips = True # options.detection_window_size = 8000 options.C = 4 options.epsilon = 0.01 options.num_threads = 8 options.be_verbose = True dlib.train_simple_object_detector(bottle_training_data,"square_bottle_classifier.svm",options) ts = time.time() st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') + '/' parentDir = 'models/' if not os.path.exists(parentDir + st): os.makedirs(parentDir + st) shutil.copyfile("square_bottle_classifier.svm", parentDir + st + "square_bottle_classifier.svm") shutil.copyfile(bottle_training_data, parentDir + st + "bottles_dataset.xml") shutil.copyfile(img_list, parentDir + st + "image_urls.txt") detector = dlib.simple_object_detector("square_bottle_classifier.svm") win = dlib.image_window() win.set_image(detector)