def generate_test_vectors(): vs = vectors() vs.append(vector([0, 1, 2])) vs.append(vector([3, 4, 5])) vs.append(vector([6, 7, 8])) assert len(vs) == 3 return vs
def train(tagged): """ Trains an SVM classifier based on the training data passed. Mostly based on http://dlib.net/svm_binary_classifier.py.html. :param tagged: list of TaggedFace to train on :return: dlib.svm """ x = dlib.vectors() # will carry the facial encodings y = dlib.array() # will carry the gender label print("Preparing dataset...") total = len(tagged) for i, t in enumerate(tagged): print(f"\rEncoding {t.path} ({i + 1}/{total})...", end="") faces = encode(t.img) x.append(dlib.vector(faces[0])) y.append(t.tag) img = t.img for _ in range(5): faces = encode(img) if not faces: break x.append(dlib.vector(faces[0])) y.append(t.tag) img = cv2.resize(img, None, fx=0.7, fy=0.7) print("Training SVM...") trainer = dlib.svm_c_trainer_radial_basis() #trainer.be_verbose() trainer.set_c(10) model = trainer.train(x, y) with open(PATH_SVMFILE, "wb") as filehandle: pickle.dump(model, filehandle) return None
def sentence_to_vectors(sentence): # Create an empty array of vectors vects = dlib.vectors() for word in sentence.split(): # Our vectors are very simple 1-dimensional vectors. The value of the single # feature is 1 if the first letter of the word is capitalized and 0 otherwise. if (word[0].isupper()): vects.append(dlib.vector([1])) else: vects.append(dlib.vector([0])) return vects
def sentence_to_vectors(sentence): # Create an empty array of vectors vects = dlib.vectors() for word in sentence.split(): # Our vectors are very simple 1-dimensional vectors. The value of the single # feature is 1 if the first letter of the word is capitalized and 0 otherwise. if (word[0].isupper()): vects.append(dlib.vector([1])) else: vects.append(dlib.vector([0])) return vects
def training_data(): r = Random(0) predictors = vectors() sparse_predictors = sparse_vectors() response = array() for i in range(30): for c in [-1, 1]: response.append(c) values = [r.random() + c * 0.5 for _ in range(3)] predictors.append(vector(values)) sp = sparse_vector() for i, v in enumerate(values): sp.append(pair(i, v)) sparse_predictors.append(sp) return predictors, sparse_predictors, response
def training_data(): r = Random(0) predictors = vectors() sparse_predictors = sparse_vectors() response = array() for i in range(30): for c in [-1, 1]: response.append(c) values = [r.random() + c * 0.5 for _ in range(3)] predictors.append(vector(values)) sp = sparse_vector() for i, v in enumerate(values): sp.append(pair(i, v)) sparse_predictors.append(sp) return predictors, sparse_predictors, response
def cluster(): s = time.time() query = '' descriptors = [] dvec = dlib.vectors() date = input("enter a date in dd-mm-yyy format") from_time = input("enter start time in hh:mm format") to_time = input("enter end time in hh:mm format") data = ptf.retrive(date, from_time, to_time) for d in data: descriptors.append(dlib.vector(d)) # Cluster the faces. labels = dlib.chinese_whispers_clustering(descriptors, 0.5) e = time.time() print(labels) print(len(descriptors)) print(len(labels)) labset = set(labels) print(labset) num_classes = len(set(labels)) #total number of clusters print("Number of clusters: {}".format(num_classes)) print(e - s) return num_classes
# root folder and run: # python setup.py install # # Compiling dlib should work on any operating system so long as you have # CMake installed. On Ubuntu, this can be done easily by running the # command: # sudo apt-get install cmake # import dlib try: import cPickle as pickle except ImportError: import pickle x = dlib.vectors() y = dlib.array() # Make a training dataset. Here we have just two training examples. Normally # you would use a much larger training dataset, but for the purpose of example # this is plenty. For binary classification, the y labels should all be either +1 or -1. x.append(dlib.vector([1, 2, 3, -1, -2, -3])) y.append(+1) x.append(dlib.vector([-1, -2, -3, 1, 2, 3])) y.append(-1) # Now make a training object. This object is responsible for turning a # training dataset into a prediction model. This one here is a SVM trainer # that uses a linear kernel. If you wanted to use a RBF kernel or histogram # intersection kernel you could change it to one of these lines:
def test_vectors_extend(): vs = vectors() vs.extend([vector([1, 2, 3]), vector([4, 5, 6])]) assert len(vs) == 2
def test_vectors_resize(): vs = vectors() vs.resize(100) assert len(vs) == 100 for i in range(100): assert len(vs[i]) == 0
# if you have a CPU that supports AVX instructions, since this makes some # things run faster. # # Compiling dlib should work on any operating system so long as you have # CMake and boost-python installed. On Ubuntu, this can be done easily by # running the command: # sudo apt-get install libboost-python-dev cmake # import dlib try: import cPickle as pickle except ImportError: import pickle x = dlib.vectors() y = dlib.array() # Make a training dataset. Here we have just two training examples. Normally # you would use a much larger training dataset, but for the purpose of example # this is plenty. For binary classification, the y labels should all be either +1 or -1. x.append(dlib.vector([1, 2, 3, -1, -2, -3])) y.append(+1) x.append(dlib.vector([-1, -2, -3, 1, 2, 3])) y.append(-1) # Now make a training object. This object is responsible for turning a # training dataset into a prediction model. This one here is a SVM trainer # that uses a linear kernel. If you wanted to use a RBF kernel or histogram