in tcav/activation_generator.py [0:0]
def load_images_from_files(self,
filenames,
max_imgs=500,
do_shuffle=True,
run_parallel=True,
shape=(299, 299),
num_workers=50):
"""Return image arrays from filenames.
Args:
filenames: locations of image files.
max_imgs: maximum number of images from filenames.
do_shuffle: before getting max_imgs files, shuffle the names or not
run_parallel: get images in parallel or not
shape: desired shape of the image
num_workers: number of workers in parallelization.
Returns:
image arrays
"""
imgs = []
# First shuffle a copy of the filenames.
filenames = filenames[:]
if do_shuffle:
np.random.shuffle(filenames)
if run_parallel:
pool = multiprocessing.Pool(num_workers)
imgs = pool.map(
lambda filename: self.load_image_from_file(filename, shape),
filenames[:max_imgs])
pool.close()
imgs = [img for img in imgs if img is not None]
if len(imgs) <= 1:
raise ValueError(
'You must have more than 1 image in each class to run TCAV.')
else:
for filename in filenames:
img = self.load_image_from_file(filename, shape)
if img is not None:
imgs.append(img)
if len(imgs) >= max_imgs:
break
if len(imgs) <= 1:
raise ValueError(
'You must have more than 1 image in each class to run TCAV.')
return np.array(imgs)