def _create(n_fold, parser, file_dir): # ID抽出用正規表現 re_compile = re.compile("\d+") cla_items = parse_cla(PATH_RES_SHAPE_SHREC_CLASS_TEST_CLA).items() def find_class(binvox_id): for i, pair in enumerate(cla_items): name, ids = pair if binvox_id in ids: return i raise IndexError x = [] y = [] for f in tqdm.tqdm(os.listdir(file_dir)): # binvoxファイルフルパス fullpath = os.path.join(file_dir, f) # binvox配列 shrec_voxel = parser(fullpath) # shrecデータに割り振られたID shrec_voxel_id = int(re_compile.findall(f)[0]) x.append([shrec_voxel, ]) y.append(find_class(shrec_voxel_id)) x = np.vstack(x).astype(np.uint8) x = x.reshape((x.shape[0], 1, x.shape[1], x.shape[2], x.shape[3])) y = np.asarray(y, dtype=np.uint8) x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=1. - 1. / n_fold) return SHRECVoxel(x_train, x_test, y_train, y_test)
def test_shrec_voxel(): """ unittestを使わないSHRECVoxelの簡易テスト :return: """ # TODO:SHRECデータセットをダウンロード・binvox変換の機構ができたら適宜unittest対応 classes = parse_cla(PATH_RES_SHAPE_SHREC_CLASS_TEST_CLA).keys() shrec = SHRECVoxel.create_shrec_voxel(n_fold=6) for x, y in zip(shrec.x_train, shrec.y_train): print classes[y] plot_voxel(x[0])
def _create(n_fold, parser, file_dir): # ID抽出用正規表現 re_compile = re.compile("\d+") cla_items = parse_cla(PATH_RES_SHAPE_SHREC_CLASS_TEST_CLA).items() def find_class(binvox_id): for i, pair in enumerate(cla_items): name, ids = pair if binvox_id in ids: return i raise IndexError x = [] y = [] for f in tqdm.tqdm(os.listdir(file_dir)): # binvoxファイルフルパス fullpath = os.path.join(file_dir, f) # binvox配列 shrec_voxel = parser(fullpath) # shrecデータに割り振られたID shrec_voxel_id = int(re_compile.findall(f)[0]) x.append([ shrec_voxel, ]) y.append(find_class(shrec_voxel_id)) x = np.vstack(x).astype(np.uint8) x = x.reshape((x.shape[0], 1, x.shape[1], x.shape[2], x.shape[3])) y = np.asarray(y, dtype=np.uint8) x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=1. - 1. / n_fold) return SHRECVoxel(x_train, x_test, y_train, y_test)
def create(is_co_class=False, is_cached=False, from_cached=False, align_data=False): if from_cached: print 'load voxels from .npy ...' if is_co_class: load_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_CO_CLASS_ORIGIN else: load_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_DEFAULT_ORIGIN try: return PSBVoxel.load(load_path) except IOError: warnings.warn('psb_voxel data was not loaded.') print 'load voxels from .off ...' cla_train = parse_cla( os.path.join(PATH_RES_SHAPE_PSB_CLASS, PSB_CLS_TRAIN)) cla_test = parse_cla( os.path.join(PATH_RES_SHAPE_PSB_CLASS, PSB_CLS_TEST)) if is_co_class: cla_list = list( set(cla_train.keys()).intersection(set(cla_test.keys()))) else: cla_list = list(set(cla_train.keys() + cla_test.keys())) re_compile = re.compile("\d+") def check_binvox_y(binvox_id, is_train): cla = cla_train if is_train else cla_test for c_label, c_ids in cla.items(): if binvox_id in c_ids: try: return cla_list.index(c_label) except ValueError: break return None x_train = [] x_test = [] y_train = [] y_test = [] for f in tqdm.tqdm(os.listdir(PATH_RES_SHAPE_PSB_BINVOX)): binvox_path = os.path.join(PATH_RES_SHAPE_PSB_BINVOX, f) if os.path.isdir(binvox_path): continue binvox = parse_binvox(binvox_path) binvox_id = int(re_compile.findall(f)[0]) train_binvox_y = check_binvox_y(binvox_id, True) if train_binvox_y is not None: x_train.append(binvox) y_train.append(train_binvox_y) else: test_binvox_y = check_binvox_y(binvox_id, False) if test_binvox_y is not None: x_test.append(binvox) y_test.append(test_binvox_y) else: continue x_train = np.asarray(x_train, dtype=np.uint8) x_test = np.asarray(x_test, dtype=np.uint8) y_train = np.asarray(y_train, dtype=np.uint8) y_test = np.asarray(y_test, dtype=np.uint8) x_train = x_train.reshape( [x_train.shape[0], 1] + list(x_train.shape[1:])) x_test = x_test.reshape([x_test.shape[0], 1] + list(x_test.shape[1:])) if align_data: aligned_size = min([len(x_train), len(x_test)]) x_train = x_train[:aligned_size] x_test = x_test[:aligned_size] y_train = y_train[:aligned_size] y_test = y_test[:aligned_size] data = PSBVoxel(x_train, x_test, y_train, y_test) if is_cached: if is_co_class: save_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_CO_CLASS_ORIGIN else: save_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_DEFAULT_ORIGIN data.save(save_path) return data
def create(is_co_class=False, is_cached=False, from_cached=False, align_data=False): if from_cached: print 'load voxels from .npy ...' if is_co_class: load_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_CO_CLASS_ORIGIN else: load_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_DEFAULT_ORIGIN try: return PSBVoxel.load(load_path) except IOError: warnings.warn('psb_voxel data was not loaded.') print 'load voxels from .off ...' cla_train = parse_cla( os.path.join(PATH_RES_SHAPE_PSB_CLASS, PSB_CLS_TRAIN)) cla_test = parse_cla( os.path.join(PATH_RES_SHAPE_PSB_CLASS, PSB_CLS_TEST)) if is_co_class: cla_list = list( set(cla_train.keys()).intersection(set(cla_test.keys()))) else: cla_list = list(set(cla_train.keys() + cla_test.keys())) re_compile = re.compile("\d+") def check_binvox_y(binvox_id, is_train): cla = cla_train if is_train else cla_test for c_label, c_ids in cla.items(): if binvox_id in c_ids: try: return cla_list.index(c_label) except ValueError: break return None x_train = [] x_test = [] y_train = [] y_test = [] for f in tqdm.tqdm(os.listdir(PATH_RES_SHAPE_PSB_BINVOX)): binvox_path = os.path.join(PATH_RES_SHAPE_PSB_BINVOX, f) if os.path.isdir(binvox_path): continue binvox = parse_binvox(binvox_path) binvox_id = int(re_compile.findall(f)[0]) train_binvox_y = check_binvox_y(binvox_id, True) if train_binvox_y is not None: x_train.append(binvox) y_train.append(train_binvox_y) else: test_binvox_y = check_binvox_y(binvox_id, False) if test_binvox_y is not None: x_test.append(binvox) y_test.append(test_binvox_y) else: continue x_train = np.asarray(x_train, dtype=np.uint8) x_test = np.asarray(x_test, dtype=np.uint8) y_train = np.asarray(y_train, dtype=np.uint8) y_test = np.asarray(y_test, dtype=np.uint8) x_train = x_train.reshape([x_train.shape[0], 1] + list(x_train.shape[1:])) x_test = x_test.reshape([x_test.shape[0], 1] + list(x_test.shape[1:])) if align_data: aligned_size = min([len(x_train), len(x_test)]) x_train = x_train[:aligned_size] x_test = x_test[:aligned_size] y_train = y_train[:aligned_size] y_test = y_test[:aligned_size] data = PSBVoxel(x_train, x_test, y_train, y_test) if is_cached: if is_co_class: save_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_CO_CLASS_ORIGIN else: save_path = PATH_RES_SHAPE_PSB_BINVOX_CACHE_DEFAULT_ORIGIN data.save(save_path) return data