在训练图像分割之前无法预处理数据

问题描述

我正在尝试对ocr进行图像分割,我的遮罩图像是3类图像,像这样

enter image description here

我的原始图像是这样的灰色图像

enter image description here

但是当我尝试拟合模型时会出现此错误

无法将形状(128,128,3)的输入数组广播到形状(128,128)

这是我用来创建数据集的代码

    img_size = (128,128)
    batch_size = 32
    input_img_paths = sorted(
        [  os.path.join(input_dir,fname)
            for fname in os.listdir(input_dir)
            if fname.endswith(".jpg") ] )
    target_img_paths = sorted(
        [   os.path.join(target_dir,fname)
            for fname in os.listdir(target_dir)
            if fname.endswith(".jpg") and not fname.startswith(".") ])

class OxfordPets(keras.utils.Sequence):
    """Helper to iterate over the data (as Numpy arrays)."""

    def __init__(self,batch_size,img_size,input_img_paths,target_img_paths):
        self.batch_size = batch_size
        self.img_size = img_size
        self.input_img_paths = input_img_paths
        self.target_img_paths = target_img_paths

    def __len__(self):
        return len(self.target_img_paths) // self.batch_size

    def __getitem__(self,idx):
        """Returns tuple (input,target) correspond to batch #idx."""
        i = idx * self.batch_size
        batch_input_img_paths = self.input_img_paths[i : i + self.batch_size]
        batch_target_img_paths = self.target_img_paths[i : i + self.batch_size]
        x = np.zeros((batch_size,) + self.img_size,dtype="float32")
        for j,path in enumerate(batch_input_img_paths):
            img = load_img(path,target_size=self.img_size)

            x[j] = img
        y = np.zeros((batch_size,path in enumerate(batch_target_img_paths):
            img = load_img(path,target_size=self.img_size,color_mode="rgb")
            y[j] = img
        return x,y



val_samples = 150
random.Random(1337).shuffle(input_img_paths)
random.Random(1337).shuffle(target_img_paths)
train_input_img_paths = input_img_paths[:-val_samples]
train_target_img_paths = target_img_paths[:-val_samples]
val_input_img_paths = input_img_paths[-val_samples:]
val_target_img_paths = target_img_paths[-val_samples:]

# Instantiate data Sequences for each split
train_gen = OxfordPets(
    batch_size,train_input_img_paths,train_target_img_paths
)
val_gen = OxfordPets(batch_size,val_input_img_paths,val_target_img_paths)

但是当我尝试适合这种情况

model_history = model.fit(train_gen,epochs=30,steps_per_epoch=50,validation_steps=25,validation_data=val_gen)

我收到错误,我正在尝试调整此解决方https://keras.io/examples/vision/oxford_pets_image_segmentation/?fbclid=IwAR2wFYju-N0X7FUaWkhvOVaAAaVqLdOryBwg7xDC0Rji9LQ5F2jYOkeNnns 来自喀拉拉邦

进入tensorflow页面的示例 https://www.tensorflow.org/tutorials/images/segmentation

并且我觉得问题与原始图像是灰度图像有关,我该如何解决呢?任何建议都会很棒!

解决方法

您的遮罩是RGB,具有3个通道。但是您的图像是灰度的,并且只有一个通道。有关将RGB图像转换为灰度图像的信息,请参见This question

,

您应该先将图像转换为RGB。您的图像是灰度的,只有1个通道。它的形状是(128,128,1)。它们适用于opencv:()之类的东西,对您数据中的每张图片都可以