in tcav/tcav_examples/image_models/imagenet/imagenet_and_broden_fetcher.py [0:0]
def download_texture_to_working_folder(broden_path, saving_path, texture_name,
number_of_images):
# Create new experiment folder where we're moving the data to
texture_saving_path = os.path.join(saving_path, texture_name)
tf.io.gfile.makedirs(texture_saving_path)
# Get images from broden
broden_textures_path = os.path.join(broden_path, kBrodenTexturesPath)
tf.compat.v1.logging.info("Using path " + str(broden_textures_path) + " for texture: " +
str(texture_name))
for root, dirs, files in os.walk(broden_textures_path):
# Broden contains _color suffixed images. Those shouldn't be used by tcav.
texture_files = [
a for a in files if (a.startswith(texture_name) and "color" not in a)
]
number_of_files_for_concept = len(texture_files)
tf.compat.v1.logging.info("We have " + str(len(texture_files)) +
" images for the concept " + texture_name)
# Make sure we can fetch as many as the user requested.
if number_of_images > number_of_files_for_concept:
raise Exception("Concept " + texture_name + " only contains " +
str(number_of_files_for_concept) +
" images. You requested " + str(number_of_images))
# We are only moving data we are guaranteed to have, so no risk for infinite loop here.
save_number = number_of_images
while save_number > 0:
for file in texture_files:
path_file = os.path.join(root, file)
texture_saving_path_file = os.path.join(texture_saving_path, file)
tf.io.gfile.copy(
path_file, texture_saving_path_file,
overwrite=True) # change you destination dir
save_number -= 1
# Break if we saved all images
if save_number <= 0:
break