Пример #1
0
    def train(self, svm_kernel, k, des_name, test_image, des_option=constants.ORB_FEAT_OPTION, is_interactive=True):
        """
        Gets the descriptors for the training set and then calculates the SVM for them.

        Args:
            svm_kernel (constant): The kernel of the SVM that will be created.
            codebook (NumPy float matrix): Each row is a center of a codebook of Bag of Words approach.
            des_option (integer): The option of the feature that is going to be used as local descriptor.
            is_interactive (boolean): If it is the user can choose to load files or generate.

        Returns:
            cv2.SVM: The Support Vector Machine obtained in the training phase.
        """
        isTrain= True
        des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
        x_filename = filenames.vlads_train(k, des_name)
        print("Getting global descriptors for the training set.")
        start = time.time()
        x, y, x_test, y1, testimg_h, cluster_model = self.get_data_and_labels(self.dataset.get_train_set(), test_image,
                                                                   KMeans(n_clusters=k), k, des_name ,isTrain,des_option,isTrain)
        utils.save(x_filename, x)
        end = time.time()
        svm_filename = filenames.svm(k, des_name, svm_kernel)
        utils.save(svm_filename, x)
        print("Calculating the Support Vector Machine for the training set...")

        return x, y, x_test, y1, testimg_h, cluster_model
    def train(self, svm_kernel, codebook, des_option=constants.ORB_FEAT_OPTION, is_interactive=True):
        """
        Gets the descriptors for the training set and then calculates the SVM for them.

        Args:
            svm_kernel (constant): The kernel of the SVM that will be created.
            codebook (NumPy float matrix): Each row is a center of a codebook of Bag of Words approach.
            des_option (integer): The option of the feature that is going to be used as local descriptor.
            is_interactive (boolean): If it is the user can choose to load files or generate.

        Returns:
            cv2.SVM: The Support Vector Machine obtained in the training phase.
        """
        des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
        k = len(codebook)
        x_filename = filenames.vlads_train(k, des_name)
        if is_interactive:
            data_option = input("Enter [1] to calculate VLAD vectors for the training set or [2] to load them.\n")
        else:
            data_option = constants.GENERATE_OPTION
        if data_option == constants.GENERATE_OPTION:
            # Getting the global vectors for all of the training set
            print("Getting global descriptors for the training set.")
            start = time.time()
            x, y = self.get_data_and_labels(self.dataset.get_train_set(), codebook, des_option)
            utils.save(x_filename, x)
            end = time.time()
            print("VLADs training vectors saved on file {0}".format(x_filename))
            self.log.train_vlad_time(end - start)
        else:
            # Loading the global vectors for all of the training set
            print("Loading global descriptors for the training set.")
            x = utils.load(x_filename)
            y = self.dataset.get_train_y()
            x = np.matrix(x, dtype=np.float32)
        svm = cv2.SVM()
        svm_filename = filenames.svm(k, des_name, svm_kernel)
        if is_interactive:
            svm_option = input("Enter [1] for generating a SVM or [2] to load one\n")
        else:
            svm_option = constants.GENERATE_OPTION
        if svm_option == constants.GENERATE_OPTION:
            # Calculating the Support Vector Machine for the training set
            print("Calculating the Support Vector Machine for the training set...")
            svm_params = dict(kernel_type=svm_kernel, svm_type=cv2.SVM_C_SVC, C=1)
            start = time.time()
            svm.train_auto(x, y, None, None, svm_params)
            end = time.time()
            self.log.svm_time(end - start)
            # Storing the SVM in a file
            svm.save(svm_filename)
        else:
            svm.load(svm_filename)
        return svm
Пример #3
0
    def train(self,
              svm_kernel,
              k,
              des_name,
              des_option=constants.ORB_FEAT_OPTION,
              is_interactive=True):
        """
        Gets the descriptors for the training set and then calculates the SVM for them.

        Args:
            svm_kernel (constant): The kernel of the SVM that will be created.
            codebook (NumPy float matrix): Each row is a center of a codebook of Bag of Words approach.
            des_option (integer): The option of the feature that is going to be used as local descriptor.
            is_interactive (boolean): If it is the user can choose to load files or generate.

        Returns:
            cv2.SVM: The Support Vector Machine obtained in the training phase.
        """
        isTrain = True
        des_name = constants.ORB_FEAT_NAME if des_option == constants.ORB_FEAT_OPTION else constants.SIFT_FEAT_NAME
        x_filename = filenames.vlads_train(k, des_name)
        print("Getting global descriptors for the training set.")
        start = time.time()
        x, y, cluster_model = self.get_data_and_labels(
            self.dataset.get_train_set(), None, k, des_name, des_option,
            isTrain)
        utils.save(x_filename, x)
        end = time.time()
        svm_filename = filenames.svm(k, des_name, svm_kernel)
        print("Calculating the Support Vector Machine for the training set...")
        svm = cv2.ml.SVM_create()
        svm.setType(cv2.ml.SVM_C_SVC)
        svm.setKernel(svm_kernel)
        svm.setTermCriteria((cv2.TERM_CRITERIA_MAX_ITER, 100, 1e-6))
        svm.train(x, cv2.ml.ROW_SAMPLE, y)
        return svm, cluster_model