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.0 * 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 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_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 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_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 load_from_json(self, filename): "Load all parameters of the dataset from a JSON file." with open(filename) as f: config = json.load(f) self.classes = config['classes'] for data in config['images']: self.images.append(Image.load_from_json_data(self.classes, data))
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 load_from_json(self, filename): "Load all parameters of the dataset from a JSON file." with open(filename) as f: config = json.load(f) self.classes = config["classes"] for data in config["images"]: self.images.append(Image.load_from_json_data(self.classes, data))
def test_get_det_gt(self): image = Image.load_from_json_data(self.classes, self.data) objects_table = Table( np.array([[0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0], [2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.get_objects() == objects_table) data = self.data.copy() data['objects'][0]['diff'] = 1 data['objects'][1]['trun'] = 1 image = Image.load_from_json_data(self.classes, data) objects_table = Table( np.array([[0, 0, 0, 0, 0, 1, 0], [1, 1, 1, 1, 1, 0, 1], [2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.get_objects(with_diff=True, with_trun=True) == objects_table) objects_table = Table( np.array([[1, 1, 1, 1, 1, 0, 1], [2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.get_objects(with_diff=False, with_trun=True) == objects_table) # this should be default behavior assert (image.get_objects() == objects_table) objects_table = Table(np.array([[2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.get_objects(with_diff=False, with_trun=False) == objects_table) objects_table = Table( np.array([[0, 0, 0, 0, 0, 1, 0], [2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.get_objects(with_diff=True, with_trun=False) == objects_table) # What if everything is filtered out? data['objects'] = data['objects'][:-1] objects_table = Table( np.array([[0, 0, 0, 0, 0, 1, 0], [1, 1, 1, 1, 1, 0, 1]]), self.columns) image = Image.load_from_json_data(self.classes, data) assert (image.get_objects(with_diff=True, with_trun=True) == objects_table) assert (image.get_objects(with_diff=False, with_trun=False).shape[0] == 0)
def test_load_json_data(self): image = Image.load_from_json_data(self.classes, self.data) assert (image.width == 640 and image.height == 480) assert (image.classes == ['A', 'B', 'C']) assert (image.name == 'test_image') objects_table = Table( np.array([[0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0], [2, 2, 2, 2, 2, 0, 0]]), self.columns) assert (image.objects_table == objects_table)
def test_load_json_data(self): image = Image.load_from_json_data(self.classes,self.data) assert(image.width == 640 and image.height == 480) assert(image.classes == ['A','B','C']) assert(image.name == 'test_image') objects_table = Table(np.array([ [0,0,0,0,0,0,0], [1,1,1,1,1,0,0], [2,2,2,2,2,0,0]]), self.columns) assert(image.objects_table == objects_table)
def test_get_det_gt(self): image = Image.load_from_json_data(self.classes,self.data) objects_table = Table(np.array([ [0,0,0,0,0,0,0], [1,1,1,1,1,0,0], [2,2,2,2,2,0,0]]), self.columns) assert(image.get_objects() == objects_table) data = self.data.copy() data['objects'][0]['diff'] = 1 data['objects'][1]['trun'] = 1 image = Image.load_from_json_data(self.classes,data) objects_table = Table(np.array([ [0,0,0,0,0,1,0], [1,1,1,1,1,0,1], [2,2,2,2,2,0,0]]), self.columns) assert(image.get_objects(with_diff=True,with_trun=True) == objects_table) objects_table = Table(np.array([ [1,1,1,1,1,0,1], [2,2,2,2,2,0,0]]),self.columns) assert(image.get_objects(with_diff=False,with_trun=True) == objects_table) # this should be default behavior assert(image.get_objects() == objects_table) objects_table = Table(np.array([ [2,2,2,2,2,0,0]]),self.columns) assert(image.get_objects(with_diff=False,with_trun=False) == objects_table) objects_table = Table(np.array([ [0,0,0,0,0,1,0], [2,2,2,2,2,0,0]]), self.columns) assert(image.get_objects(with_diff=True,with_trun=False) == objects_table) # What if everything is filtered out? data['objects'] = data['objects'][:-1] objects_table = Table(np.array([ [0,0,0,0,0,1,0], [1,1,1,1,1,0,1]]), self.columns) image = Image.load_from_json_data(self.classes,data) assert(image.get_objects(with_diff=True,with_trun=True) == objects_table) assert(image.get_objects(with_diff=False,with_trun=False).shape[0] == 0)
def test_get_cls_counts_and_gt(self): data = self.data.copy() image = Image.load_from_json_data(self.classes, data) assert (np.all(image.get_cls_counts() == np.array([1, 1, 1]))) assert (np.all(image.get_cls_gt() == np.array([True, True, True]))) assert (image.contains_class('A') == True) assert (image.contains_class('B') == True) data['objects'][0]['class'] = 'B' image = Image.load_from_json_data(self.classes, data) # doesn't actually have to be Series, can be ndarray for comparison assert (np.all(image.get_cls_counts() == np.array([0, 2, 1]))) assert (np.all(image.get_cls_gt() == np.array([False, True, True]))) assert (image.contains_class('A') == False) assert (image.contains_class('B') == True) data['objects'] = [] image = Image.load_from_json_data(self.classes, data) assert (np.all(image.get_cls_counts() == np.array([0, 0, 0]))) assert (np.all(image.get_cls_gt() == np.array([False, False, False]))) assert (image.contains_class('A') == False) assert (image.contains_class('B') == False)
def test_get_cls_counts_and_gt(self): data = self.data.copy() image = Image.load_from_json_data(self.classes,data) assert(np.all(image.get_cls_counts() == np.array([1,1,1]))) assert(np.all(image.get_cls_gt() == np.array([True,True,True]))) assert(image.contains_class('A') == True) assert(image.contains_class('B') == True) data['objects'][0]['class'] = 'B' image = Image.load_from_json_data(self.classes,data) # doesn't actually have to be Series, can be ndarray for comparison assert(np.all(image.get_cls_counts() == np.array([0,2,1]))) assert(np.all(image.get_cls_gt() == np.array([False,True,True]))) assert(image.contains_class('A') == False) assert(image.contains_class('B') == True) data['objects'] = [] image = Image.load_from_json_data(self.classes,data) assert(np.all(image.get_cls_counts() == np.array([0,0,0]))) assert(np.all(image.get_cls_gt() == np.array([False,False,False]))) assert(image.contains_class('A') == False) assert(image.contains_class('B') == False)
def load_from_pascal(self, name, force=False): """ Look up the filename associated with the given name. Read image names from provided filename, and construct a dataset from the corresponding .xml files. Save self to disk when loaded for caching purposes. If force is True, does not look for cached data when loading. """ tt = ut.TicToc().tic() print("Dataset: %s" % name), filename = config.get_cached_dataset_filename(name) if opexists(filename) and not force: with open(filename) as f: cached = cPickle.load(f) self.classes = cached.classes self.images = cached.images print("...loaded from cache in %.2f s" % tt.qtoc()) return print("...loading from scratch") filename = config.pascal_paths[name] self.classes = config.pascal_classes with open(filename) as f: imgset = [line.strip() for line in f.readlines()] for i, img in enumerate(imgset): tt.tic('2') if tt.qtoc('2') > 2: print(" on image %d/%d" % (i, len(imgset))) tt.tic('2') if len(img) > 0: xml_filename = opjoin(config.VOC_dir, 'Annotations', img + '.xml') self.images.append( Image.load_from_pascal_xml_filename( self.classes, xml_filename)) filename = config.get_cached_dataset_filename(name) print(" ...saving to cache file") with open(filename, 'w') as f: cPickle.dump(self, f) print(" ...done in %.2f s\n" % tt.qtoc())
def load_from_pascal(self, name, force=False): """ Look up the filename associated with the given name. Read image names from provided filename, and construct a dataset from the corresponding .xml files. Save self to disk when loaded for caching purposes. If force is True, does not look for cached data when loading. """ tt = ut.TicToc().tic() print("Dataset: %s" % name), filename = config.get_cached_dataset_filename(name) if opexists(filename) and not force: with open(filename) as f: cached = cPickle.load(f) self.classes = cached.classes self.images = cached.images print("...loaded from cache in %.2f s" % tt.qtoc()) return print("...loading from scratch") filename = config.pascal_paths[name] self.classes = config.pascal_classes with open(filename) as f: imgset = [line.strip() for line in f.readlines()] for i, img in enumerate(imgset): tt.tic("2") if tt.qtoc("2") > 2: print(" on image %d/%d" % (i, len(imgset))) tt.tic("2") if len(img) > 0: xml_filename = opjoin(config.VOC_dir, "Annotations", img + ".xml") self.images.append(Image.load_from_pascal_xml_filename(self.classes, xml_filename)) filename = config.get_cached_dataset_filename(name) print(" ...saving to cache file") with open(filename, "w") as f: cPickle.dump(self, f) print(" ...done in %.2f s\n" % tt.qtoc())
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')
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)))
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