def test_expected_time(self): d = PerfectDetector(self.train_dataset,self.train_dataset,'dog') img = Image(500,375,self.train_dataset.classes,'test') print d.expected_time(img) assert(d.expected_time(img) == 10) img = Image(250,375,self.train_dataset.classes,'test') assert(d.expected_time(img) == 5)
def generate_synthetic(self): """ Generate a synthetic dataset of 4 classes that follows some simple but strong cooccurence rules. """ num_images = 1000 choice_probs = { (1, 0, 0, 0): 2, (0, 1, 0, 0): 1, (0, 0, 1, 0): 1, (0, 0, 0, 1): 1, (1, 1, 0, 0): 2, (1, 0, 1, 0): 0, (1, 0, 0, 1): 0, (0, 1, 1, 0): 0, (0, 1, 0, 1): 1, (0, 0, 1, 1): 2, (1, 1, 1, 0): 0, (1, 1, 0, 1): 1, (1, 0, 1, 1): 1, (0, 1, 1, 1): 2 } probs = np.array(choice_probs.values()) cum_probs = np.cumsum(1. * probs / np.sum(probs)) self.classes = ['A', 'B', 'C', 'D'] for i in range(0, num_images): image = Image(100, 100, self.classes, str(i)) choice = np.where(cum_probs > np.random.rand())[0][0] objects = [] for cls_ind, clas in enumerate(choice_probs.keys()[choice]): if clas == 1: objects.append(np.array([0, 0, 0, 0, cls_ind, 0, 0])) image.objects_table = Table(np.array(objects), Image.columns) self.images.append(image)
def test_get_random_windows(self): image = Image(width=3, height=2, classes=[], name='test') window_params = WindowParams(min_width=2, stride=1, scales=[1, 0.5], aspect_ratios=[0.5]) windows = image.get_random_windows(window_params, 2) assert (windows.shape[0] == 2) windows = image.get_random_windows(window_params, 3) assert (windows.shape[0] == 3)
def test_get_windows_lots(self): t = time.time() image = Image(width=640, height=480, classes=[], name='test') window_params = WindowParams() window_params.min_width = 10 window_params.stride = 8 window_params.aspect_ratios = [0.5, 1, 1.5] window_params.scales = 1. / 2**np.array([0, 0.5, 1, 1.5, 2]) print(window_params) windows = image.get_windows(window_params) time_passed = time.time() - t print("Generating windows took %.3f seconds" % time_passed) print(np.shape(windows)) print(windows[:10, :]) rand_ind = np.random.permutation(np.shape(windows)[0])[:10] print(windows[rand_ind, :])
def test_get_whole_image_bbox(self): image = Image(20, 10, [], 'test_image') assert (image.get_whole_image_bbox() == BoundingBox((0, 0, 20, 10))) image = Image(2, 100, [], 'test_image') assert (image.get_whole_image_bbox() == BoundingBox((0, 0, 2, 100)))
def test_constructor(self): image = Image(20, 10, ['A', 'B', 'C'], 'test_image') assert (image.width == 20 and image.height == 10) assert (image.classes == ['A', 'B', 'C']) assert (image.name == 'test_image')
slices = [] for i in range(num_bins): hist = np.matrix(count_histogram_for_slice(assignments, im_width, im_height, \ num_bins, i, num_words)[1]) hist_sum = float(np.sum(hist)) slices.append(hist / hist_sum) return slices if __name__ == '__main__': assignments = np.zeros((25, 3)) ind = 0 for i in range(5): for j in range(5): ass = 1 if (i, j) == (1, 1) or (i, j) == (2, 1) or (i, j) == (3, 1): ass = 2 if (i, j) == (1, 2) or (i, j) == (2, 2) or (i, j) == (3, 2): ass = 3 if (i, j) == (1, 3) or (i, j) == (2, 3) or (i, j) == (3, 3): ass = 4 assignments[ind, :] = np.matrix([[i, j, ass]]) ind += 1 image = Image(size=(5, 5)) num_words = 4 slices = extract_horiz_sclices(3, assignments, image, num_words) corr_stack = np.matrix([[7, 3, 0, 0], [2, 0, 3, 0], [7, 0, 0, 3]]) slice_stack = np.vstack(slices) assert (corr_stack.all() == slice_stack.all()) #print slices