我创建了一个DataGenerator(Sequence)定义batch_size,batch_x和 的类batch_y。batch_x是一批图像(来自x_set,图像的文件路径列表),由 读入imread,调整大小resize并除以 255 以获得 0 到 1 之间的值。batch_y这些图像的标签来自y_set,a包含所有标签的列表。
class DataGenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
batch_x = np.array([resize(imread(file_name), (64, 128)) for file_name in batch_x])
batch_x = batch_x * 1./255
batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
batch_y = np.array(batch_y)
return batch_x, batch_y
因为这个生成器可以工作但在 Colab 上需要很长时间,所以我之前调整了图像的大小。因此,这不再是必需的,我现在想修改DataGenerator并保留该resize功能。这是我的代码DataGenerator_withoutresize(Sequence):
class DataGenerator_withoutresize(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx*self.batch_size : (idx + 1)*self.batch_size]
batch_x = np.array([(imread(file_name) for file_name in batch_x])
batch_x = batch_x * 1./255
batch_y = self.y[idx*self.batch_size : (idx + 1)*self.batch_size]
batch_y = np.array(batch_y)
return batch_x, batch_y
这段代码正确吗?
森林海
杨魅力
随时随地看视频慕课网APP
相关分类