def test_gather_wnid(self): p = env.get_data_folder() api = ImagenetAPI(p) api.gather("n11531193") path = env.get_path("wilding") self.assertTrue(os.path.isdir(path)) shutil.rmtree(path)
def test_file_log(self): p = get_data_folder() log_file = "log.txt" logger, root = create_file_logger(p, "test_logger", file_name=log_file) logger.info("test1") logger.info("test2") lines = open(os.path.join(root, log_file)).readlines() self.assertEqual(len(lines), 2) logger.close() shutil.rmtree(root)
def test_gather_subset(self): p = env.get_data_folder() api = ImagenetAPI(p, limit=3) api.gather("n09289331", include_subset=True) path = env.get_path("glacier") self.assertTrue(os.path.isdir(path)) for f in ["alpine_glacier", "continental_glacier", "piedmont_glacier"]: p = env.get_path("glacier/" + f) self.assertTrue(os.path.isdir(p)) shutil.rmtree(path)
def test_label_dir(self): # prepare the images p = env.get_data_folder() api = ImagenetAPI(p, limit=3) api.gather("n01316949", include_subset=True) folder = "work_animal" # make labeled data machine = LabelingMachine(p) lf_fixed = machine.label_dir(0, path_from_root=folder, label_file=os.path.join( p, "test_label_fixed.txt")) lf_auto, label_def = machine.label_dir_auto(path_from_root=folder, label_file=os.path.join( p, "test_label_auto.txt")) f_count = 0 for im in lf_fixed.fetch(): self.assertEqual(0, im.label) f_count += 1 label_and_path = {} a_count = 0 with open(label_def) as f: path_label = [ln.strip().split() for ln in f.readlines()] for pl in path_label: label_and_path[int(pl[0])] = pl[1] for im in lf_auto.fetch(): path = os.path.dirname(im.path) rel_path = machine.file_api.to_rel(path) self.assertTrue(im.label in label_and_path) self.assertEqual(os.path.join(label_and_path[im.label], ""), os.path.join(rel_path, "")) a_count += 1 else: im = None # release reference self.assertGreater(f_count, 0) self.assertEquals(f_count, a_count) shutil.rmtree(api.file_api.to_abs(folder)) os.remove(lf_fixed.path) os.remove(lf_auto.path) os.remove(label_def)
def test_prepare(self): p = get_data_folder() api = FileAPI(p) folders = ["one", "two"] relative = "/".join(folders) + "/three.txt" api.prepare_dir(relative) rmdirs = [] for f in folders: p = p + "/" + f self.assertTrue(os.path.exists(p)) rmdirs.append(p) self.assertFalse(os.path.exists(api.to_abs(relative))) for f in rmdirs[::-1]: os.rmdir(f)
def test_label_dir(self): # prepare the images p = env.get_data_folder() api = ImagenetAPI(p, limit=3) api.gather("n01316949", include_subset=True) folder = "work_animal" # make labeled data machine = LabelingMachine(p) lf_fixed = machine.label_dir(0, path_from_root=folder, label_file=os.path.join(p, "test_label_fixed.txt")) lf_auto, label_def = machine.label_dir_auto(path_from_root=folder, label_file=os.path.join(p, "test_label_auto.txt")) f_count = 0 for im in lf_fixed.fetch(): self.assertEqual(0, im.label) f_count += 1 label_and_path = {} a_count = 0 with open(label_def) as f: path_label = [ln.strip().split() for ln in f.readlines()] for pl in path_label: label_and_path[int(pl[0])] = pl[1] for im in lf_auto.fetch(): path = os.path.dirname(im.path) rel_path = machine.file_api.to_rel(path) self.assertTrue(im.label in label_and_path) self.assertEqual(os.path.join(label_and_path[im.label], ""), os.path.join(rel_path, "")) a_count += 1 else: im = None # release reference self.assertGreater(f_count, 0) self.assertEquals(f_count, a_count) shutil.rmtree(api.file_api.to_abs(folder)) os.remove(lf_fixed.path) os.remove(lf_auto.path) os.remove(label_def)
def test_download(self): api = API(env.get_data_folder()) f = "test.md" api.download_dataset("https://raw.githubusercontent.com/icoxfog417/mlimages/master/README.md", f) self.assertTrue(api.file_api.to_abs(f)) os.remove(api.file_api.to_abs(f))