def ComputeFeatures(self): """Compute SVM feature vectors for all images. Generally, you do not need to call this method yourself, as it will be called automatically by :meth:`RunSvm`. """ if self.train_images == None or self.test_images == None: sys.exit("Please specify the corpus.") train_sizes = map(len, self.train_images) train_size = sum(train_sizes) test_sizes = map(len, self.test_images) test_size = sum(test_sizes) train_images = util.UngroupLists(self.train_images) test_images = util.UngroupLists(self.test_images) images = train_images + test_images start_time = time.time() # Compute features for all images. features = self.GetImageFeatures(images, block=True) self.compute_feature_time = time.time() - start_time # Split results by training/testing set train_features, test_features = util.SplitList(features, [train_size, test_size]) # Split training set by class train_features = util.SplitList(train_features, train_sizes) # Split testing set by class test_features = util.SplitList(test_features, test_sizes) # Store features as list of 2D arrays self.train_features = [ np.array(f, util.ACTIVATION_DTYPE) for f in train_features ] self.test_features = [ np.array(f, util.ACTIVATION_DTYPE) for f in test_features ]
def ImprintS2Prototypes(self, num_prototypes): """Imprint a set of S2 prototypes from the training images. Patches are drawn from all classes of the training data. :param int num_prototypes: The number of C1 patches to sample. """ if self.train_images == None: sys.exit("Please specify the training corpus before imprinting " "prototypes.") model = self.model start_time = time.time() image_files = util.UngroupLists(self.train_images) # Represent each image file as an empty model state. input_states = map(model.MakeStateFromFilename, image_files) try: prototypes, locations = misc.ImprintKernels( model, model.LayerClass.C1, model.s2_kernel_sizes, num_prototypes, input_states, normalize=model.s2_kernels_are_normed, pool=self.pool) except InputSourceLoadException, ex: if ex.source != None: path = ex.source.image_path else: path = '<unknown>' logging.error("Failed to process image (%s): image read error", path) sys.exit(-1)
def PrepareFeatures(features_per_class): """Format feature vectors for use by the sklearn library. :param features_per_class: Per-class feature vectors. :type features_per_class: 2D array-like :returns: Feature vectors and labels. :rtype: 2D-ndarray and 1D-ndarray """ num_classes = len(features_per_class) if num_classes == 2: labels = [1, -1] else: labels = range(1, num_classes + 1) class_sizes = map(len, features_per_class) labels_per_class = [[label] * size for label, size in zip(labels, class_sizes)] features = util.UngroupLists(features_per_class) features = np.array(features) labels = util.UngroupLists(labels_per_class) labels = np.array(labels) return features, labels