def test_save_float_matrix(self): ds.save_matrix(self.test_matrix, os.path.join(BASE_PATH, "testdata", "writetest.mat")) loaded = ds.load_matrix( os.path.join(BASE_PATH, "testdata", "writetest.mat")) self.assertTrue( (self.test_matrix == loaded).all(), "Should be:\n%s\nbut is:\n%s" % (self.test_matrix, loaded))
def _stack_bow_vectors(self, bow_files): # log("Loading matrices") bow_vectors = [storage.load_matrix(os.path.join(self.PATHS["BOW"], f)) for f in bow_files] # log("Building sample matrix") # build a n_samples x n_features matrix for sklearn by concatenating the BoW-vectors matrix = np.vstack(bow_vectors) # log("Vocabulary size: %d" % matrix.shape[1]) return matrix
def heatmap_data(self, img_name, importances, img_size): """Creates a 2-dimensional float array, representing the heatmap of importances for the given image. The code to calculate a keypoint's environment is based on OpenCV's SIFT-implementation. Pixels that are contained in the environments of two or more distinct keypoints get assigned the maximum of the possible importances. Args: img_name: Filename of the image in question. importances: Array of feature importances. img_size: Tuple of (width, height) for the image. """ keypoints_file = util.keypoints_name(img_name) indices_file = util.cluster_name(img_name) keypoints = ds.keypoints_from_file(os.path.join(self.datamanager.PATHS["KEYPOINTS"], keypoints_file)) indices = [index[0] for index in ds.load_matrix(os.path.join(self.datamanager.PATHS["BOW"], indices_file))] assert len(keypoints) == len(indices), "Should be %d, but is %d" % (len(keypoints), len(indices)) heatmap = np.zeros(img_size[0:2]) rows = heatmap.shape[0] cols = heatmap.shape[1] for kp, index in zip(keypoints, indices): cosine = math.cos(math.radians(kp.angle)) / HIST_WIDTH sine = math.sin(math.radians(kp.angle)) / HIST_WIDTH for i in range(-RADIUS, RADIUS + 1): for j in range(-RADIUS, RADIUS + 1): r = kp.y + i c = kp.x + j c_rot = j * cosine - i * sine r_rot = j * sine + i * cosine; rbin = r_rot + HIST_ARRAY_WIDTH/2 - 0.5 cbin = c_rot + HIST_ARRAY_WIDTH/2 - 0.5 if (-1 < rbin < HIST_ARRAY_WIDTH) and (-1 < cbin < HIST_ARRAY_WIDTH) and (0 <= r < rows) and (0 <= c < cols): heatmap[r, c] = self.absmax(heatmap[r, c], importances[index]) return heatmap
def test_save_uint32_matrix(self): ds.save_matrix(self.test_matrix_uint32, os.path.join(BASE_PATH, "testdata", "writetest_uint32.mat")) loaded = ds.load_matrix(os.path.join(BASE_PATH, "testdata", "writetest_uint32.mat")) self.assertTrue((self.test_matrix_uint32 == loaded).all(), "Should be:\n%s\nbut is:\n%s" % (self.test_matrix_uint32, loaded))
def test_load_float_matrix(self): loaded_matrix = ds.load_matrix(os.path.join(BASE_PATH, "testdata", "test.mat")) self.assertTrue((self.test_matrix == loaded_matrix).all(), "Should be:\n%s\nbut is:\n%s" % (self.test_matrix, loaded_matrix))
def test_load_float_matrix(self): loaded_matrix = ds.load_matrix( os.path.join(BASE_PATH, "testdata", "test.mat")) self.assertTrue( (self.test_matrix == loaded_matrix).all(), "Should be:\n%s\nbut is:\n%s" % (self.test_matrix, loaded_matrix))