我使用Python和ImageDataGenerator从目录中生成图像。我有大约20节课,我想用某种方式把它们统一起来。例如,类1-4是x,5-8是y。ImageDataGenerator可以在flow_from_directory中这样做吗?还是我必须根据统一类的需要(例如将目录1-4合并成dir x)而将目录分开?
发布于 2018-09-18 18:40:21
我不认为有一个内置的方式这样做。但是,另一种方法是将生成器封装到另一个生成器中,并修改其中的标签(为了演示,这里我假设我们最初有四个类,我们希望将类一和类二看作新的类一,类三和四被认为是新的类二):
# define the generator
datagen = ImageDataGenerator(...)
# assign class_mode to 'sparse' to make our work easier
gen = datagen.flow_from_directory(..., class_mode= 'sparse')
# define a mapping from old classes to new classes (i.e. 0,1 -> 0 and 2,3 -> 1)
old_to_new = np.array([0, 0, 1, 1])
# the wrapping generator
def new_gen(gen):
for data, labels in gen:
labels = old_to_new[labels]
# now you can call np_utils.to_categorical method
# if you would like one-hot encoded labels
yield data, labels
# ... define your model
# fit the model
model.fit(new_gen(gen), ...)https://stackoverflow.com/questions/52384386
复制相似问题