Python 如何在自定义数据生成器中实现重缩放?
我使用Python 如何在自定义数据生成器中实现重缩放?,python,tensorflow,keras,data-generation,image-preprocessing,Python,Tensorflow,Keras,Data Generation,Image Preprocessing,我使用tf.keras.utils.Sequence:()创建了一个自定义的数据生成器。 这是定制的数据生成器: # Here, `x_set` is list of path to the images # and `y_set` are the associated classes. class DataGenerator(Sequence): def __init__(self, x_set, y_set, batch_size): self.x, self.y
tf.keras.utils.Sequence
:()创建了一个自定义的数据生成器。
这是定制的数据生成器
:
# Here, `x_set` is list of path to the images
# and `y_set` are the associated classes.
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(batch_x)
batch_x = batch_x*1/255
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
resize(imread(file_name), (64, 128))
for file_name in batch_x]), np.array(batch_y)
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch = num_train_samples // 128,
validation_steps = num_val_samples // 128,
epochs = 10)
x_集
是指向我的图像的路径列表,y_集
是关联的类。
现在,我想添加一个函数,通过将图像的每个像素乘以rescale=1./255
来重新缩放图像,就像在ImageDataGenerator
类中一样:
当我将此代码应用于型号时。安装发电机
:
# Here, `x_set` is list of path to the images
# and `y_set` are the associated classes.
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(batch_x)
batch_x = batch_x*1/255
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
resize(imread(file_name), (64, 128))
for file_name in batch_x]), np.array(batch_y)
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch = num_train_samples // 128,
validation_steps = num_val_samples // 128,
epochs = 10)
我得到这个错误:
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-62-571a868b2d2a> in <module>()
3 steps_per_epoch = num_train_samples // 128,
4 validation_steps = num_val_samples // 128,
----> 5 epochs = 10)
8 frames
<ipython-input-54-d98c3b0c7c56> in __getitem__(self, idx)
15 self.batch_size]
16 batch_x = np.array(batch_x)
---> 17 batch_x = batch_x*1/255
18 batch_y = self.y[idx * self.batch_size:(idx + 1) *
19 self.batch_size]
UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U77'), dtype('<U77')) -> dtype('<U77')
---------------------------------------------------------------------------
UFuncTypeError回溯(最近一次调用上次)
在()
每个历元3个步骤=num列样本//128,
4个验证步骤=num\u val\u样本//128,
---->5个时代=10个)
8帧
in _uGetItem_uuu(self,idx)
15.自身批次大小]
16批次x=np.数组(批次x)
--->17批次x=批次x*1/255
18批次y=self.y[idx*self.batch_大小:(idx+1)*
19.自身批次大小]
UFuncTypeError:ufunc“multiply”不包含具有签名匹配类型的循环(数据类型(“请以这种方式尝试
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
batch_x*1./255?我必须在哪里实现此代码?如果定义batch_x或在batch_x=batch_x*1/255下面的行中,则我得到:TypeError:不支持的操作数类型对于/:“list”和“int”
要进行此批处理,x必须是一个数组,非常感谢您!它可以工作。但是,它确实很慢,我不知道。但是它以前已经很慢了。。。