예제 #1
0
파일: fhog_detector.py 프로젝트: markpp/MTB
    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)
예제 #2
0
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")
예제 #3
0
    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_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
예제 #5
0
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")))
예제 #6
0
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))
예제 #7
0
파일: dlib.py 프로젝트: MatthewYancey/core
# 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)
예제 #8
0
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)
예제 #9
0
# -*- coding: utf-8 -*-
import os
import dlib

# 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
# 支持向量机的C参数,通常默认取为5.自己适当更改参数以达到最好的效果
options.C = 5
# 线程数,你电脑有4核的话就填4
options.num_threads = 4
options.be_verbose = True

# 获取路径
current_path = os.getcwd()
# train_folder = current_path + '/cats_train/'
# test_folder = current_path + '/cats_test/'
# train_xml_path = train_folder + 'cat.xml'
# test_xml_path = test_folder + 'cats.xml'
train_folder = 'C:/Users/kk/Downloads/dlib-19.15/tools/imglab/build/images/cat/'

test_folder = 'C:/Users/kk/Downloads/dlib-19.15/tools/imglab/build/images/cat/test/'
train_xml_path = train_folder + 'cat.xml'
# test_xml_path = test_folder + 'cats.xml'
print("training file path:" + train_xml_path)
# print(train_xml_path)
# print("testing file path:" + test_xml_path)
# print(test_xml_path)
예제 #10
0
def train(image_folder, append):

    if append == 0:

        #code to open createXML for the noob user with all the required params

        cmd = 'createXML.exe -c' + image_folder + '/training.xml ' + image_folder
        run(cmd, 5)

        cmd = 'createXML.exe ' + image_folder + '/training.xml'
        run(cmd, 50)

    # <NOTE> <IN PROGRESS> include code to write new XML to the old XML and use the latter for the training

    elif append == 1:

        #code to open createXML for the noob user with all the required params

        cmd = 'createXML.exe -c' + image_folder + '/trainingTemp.xml ' + image_folder
        run(cmd, 5)

        cmd = 'createXML.exe ' + image_folder + '/trainingTemp.xml'
        run(cmd, 50)

        dlib.hit_enter_to_continue()

        # doing all the magic stuff to append the new XML to the old one

        xml1 = image_folder + "/training.xml"
        xml2 = image_folder + "/trainingTemp.xml"

        removeUselessText(xml1)
        removeUselessText(xml2)

        #combineXML(xml1,xml2)
        r = XMLCombiner((xml1, xml2)).combine()

        with open(xml1, "r+") as f:
            f.write(et.tostring(r.getroot()))

        #Convert the XML to better format before saving it for the training as there may be some improper indentation

    # setting option in dlib

    options = dlib.simple_object_detector_training_options()

    # symmetric detector
    options.add_left_right_image_flips = True

    # SVM C parameter.larger value will lead to overfitting
    options.C = 2

    # 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(image_folder, "training.xml")
    #testing_xml_path = os.path.join(image_folder, "testing.xml")

    # saving the detector as detector.svm with input as the xml file after doing the training

    dlib.train_simple_object_detector(training_xml_path, "detector.svm",
                                      options)

    # Printing the accuracy with training data

    print("\nTraining accuracy: {}".format(
        dlib.test_simple_object_detector(training_xml_path, "detector.svm")))

    # Doing the detection
    detector = dlib.simple_object_detector("detector.svm")

    # Looking at the HOG filter the machine has learned.
    win_det = dlib.image_window()
    win_det.set_image(detector)
예제 #11
0
def train_dlib_detector(images,
                        output_path,
                        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. At the moment, the output is written to file
    and must be loaded back up when training is completed. No verbose
    messages will be provided by default.

    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.
    output_path : Path or str
        The output path for dlib to save the detector to.
    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.
    """
    rectangles = [[
        pointgraph_to_rect(lgroup.lms.bounding_box())
        for lgroup in im.landmarks.values()
    ] for im in images]
    images = [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

    output_path_str = str(output_path)
    dlib.train_simple_object_detector(images, rectangles, output_path_str,
                                      options)
예제 #12
0
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)