Я следовал следующему руководству для создания своего собственного генератора, безопасного для потоков.Генератор изображений в значительной степени вдохновлен этим .
Вот код для генератора изображений
class ImageSequence(Sequence):
def __init__(self, dataset_csv_file, class_names, source_image_dir, batch_size=16,
target_size=(224, 224), augmenter=None, verbose=0, steps=None,
shuffle_on_epoch_end=True, random_state=1):
"""
:param dataset_csv_file: str, path of dataset csv file
:param class_names: list of str
:param batch_size: int
:param target_size: tuple(int, int)
:param verbose: int
"""
self.dataset_df = pd.read_csv(dataset_csv_file)
self.source_image_dir = source_image_dir
self.batch_size = batch_size
self.target_size = target_size
self.augmenter = augmenter
self.verbose = verbose
self.shuffle = shuffle_on_epoch_end
self.random_state = random_state
self.class_names = class_names
self.prepare_dataset()
if steps is None:
self.steps = int(np.ceil(len(self.x_path) / float(self.batch_size)))
else:
self.steps = int(steps)
def __bool__(self):
return True
def __len__(self):
return self.steps
def __getitem__(self, idx):
batch_x_path = self.x_path[idx * self.batch_size:(idx + 1) * self.batch_size]
batch_x = np.asarray([self.load_image(x_path) for x_path in batch_x_path])
batch_x = self.transform_batch_images(batch_x)
batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]
return (batch_x, batch_y)
def load_image(self, image_file):
image_path = os.path.join(self.source_image_dir, image_file)
image = Image.open(image_path)
image_array = np.asarray(image.convert("RGB"))
image_array = image_array / 255.
#image_array = resize(image_array, self.target_size)
return image_array
def transform_batch_images(self, batch_x):
if self.augmenter is not None:
batch_x = self.augmenter.augment_images(batch_x)
imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_std = np.array([0.229, 0.224, 0.225])
batch_x = (batch_x - imagenet_mean) / imagenet_std
return batch_x
def get_y_true(self):
return self.y[:self.steps*self.batch_size, :]
def prepare_dataset(self):
df = self.dataset_df.sample(frac=1., random_state=self.random_state)
self.x_path, self.y = df["Image Index"].as_matrix(), df[self.class_names].as_matrix()
def on_epoch_end(self):
if self.shuffle:
self.random_state += 1
self.prepare_dataset()
Когда я компилирую этот код в TPU, используя метод fit_generator, я получаю ошибку
'ImageSequence' object has no attribute 'shape'
Генератор работает нормально.Если я позвоню генератору, это даст перебор.Любая помощь приветствуется.Заранее спасибо.