我得到了如标题所示的错误,当我试图用2个输入训练一个U-Net并且预期输出也是2个时,Id为2个输入创建了两个训练生成器,如这段代码所示。< / p>
def trainGenerator1(batch_size,train_path1=('data\\membrane\\dataroad\\train'), mask_folder1= ('data\\membrane\\dataroad\\train\\ label'),image_folder1= ('data\\membrane\\dataroad\\train\\image'),mask_folder2= ('data\\membrane\\datacenterline\\train\\label'),image_color_mode1 = "rgb",
mask_color_mode1 = "grayscale", image_save_prefix = "image", mask_save_prefix = "mask",
flag_multi_class = False,num_class = 2,save_to_dir = None,image_size1 = (224,224),seed = 1):
'''
can generate image and mask at the same time
use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same
if you want to visualize the results of generator, set save_to_dir = "your path"
'''
data_gen_args1 = dict(rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=0.05,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest')
image_datagen1 = ImageDataGenerator(**data_gen_args1)
mask_datagen1 = ImageDataGenerator(**data_gen_args1)
image_generator1 = image_datagen1.flow_from_directory(
train_path1,
classes = [image_folder1],
class_mode = None,
color_mode = "rgb",
target_size = image_size1,
batch_size = 2,
save_to_dir = save_to_dir,
save_prefix = image_save_prefix,
seed = seed)
mask_generator1 = mask_datagen1.flow_from_directory(
train_path1,
classes = [mask_folder1],
class_mode = None,
color_mode = "grayscale",
target_size = image_size1,
batch_size = 2,
save_to_dir = save_to_dir,
save_prefix = mask_save_prefix,
seed = seed)
train_generator1 = zip(image_generator1, mask_generator1)
for (img1,mask1) in train_generator1:
img1,mask1 = adjustData(img1,mask1,flag_multi_class,num_class)
yield (img1,mask1)
其中的AdjustData如
所示def adjustData(img,mask,flag_multi_class,num_class):
if(flag_multi_class):
img = img / 255
mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]
new_mask = np.zeros(mask.shape + (num_class,))
for i in range(num_class):
#for one pixel in the image, find the class in mask and convert it into one-hot vector
#index = np.where(mask == i)
#index_mask = (index[0],index[1],index[2],np.zeros(len(index[0]),dtype = np.int64) + i) if (len(mask.shape) == 4) else (index[0],index[1],np.zeros(len(index[0]),dtype = np.int64) + i)
#new_mask[index_mask] = 1
new_mask[mask == i,i] = 1
new_mask = np.reshape(new_mask,(new_mask.shape[0],new_mask.shape[1]*new_mask.shape[2],new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask,(new_mask.shape[0]*new_mask.shape[1],new_mask.shape[2]))
mask = new_mask
elif(np.max(img) > 1):
img = img / 255
mask = mask /255
mask[mask > 0.5] = 1
mask[mask <= 0.5] = 0
return (img,mask)
并结合两个生成器
myGene1 = trainGenerator1(2,'data\\membrane\\dataroad\\train','image','label', data_gen_args1,save_to_dir = None)
myGene2 = trainGenerator2(2,'data\\membrane\\datacenterline\\train','image','label', data_gen_args2,save_to_dir = None)
并如此处Joining two DirectoryIterators in Keras所示使用fit_generator,如图
class CombinedGen():
def __init__(self, *gens):
self.gens = gens
def generate(self):
while True:
for myGene in self.gens:
yield next(myGene)
def __len__(self):
return sum([len(myGene) for myGene in self.gens])
cg = CombinedGen(myGene1, myGene2)
history= model.fit_generator(cg.generate(),steps_per_epoch=3240,epochs=150,callbacks=[model_checkpoint,tensorboard])
请提供任何帮助