def build_feature_files(base_directory,
new_directory,
data_loader,
n=None,
negative_example_keep_prob=1.0):
os.makedirs(new_directory, exist_ok=False)
episode_paths = frame.episode_paths(base_directory)
label_counts = [0, 0]
if n is not None:
np.random.shuffle(episode_paths)
episode_paths = episode_paths[:n]
for episode_path in tqdm.tqdm(episode_paths):
try:
features, labels = data_loader.load_features_and_labels([episode_path])
except:
traceback.print_exc()
else:
keep = np.logical_or(labels, (np.less(
np.random.rand(len(labels)), negative_example_keep_prob)))
labels = labels[keep]
for i in range(len(label_counts)):
label_counts[i] += np.count_nonzero(labels == i)
features = {k: v[keep] for k, v in features.items()}
new_path = path_relative_to_new_directory(base_directory, new_directory, episode_path,
".features")
os.makedirs(os.path.dirname(new_path), exist_ok=True)
with open(new_path, 'wb') as f:
pickle.dump((features, labels), f)
return label_counts
评论列表
文章目录