【问题标题】:How to change class labels of a custom Keras data generator如何更改自定义 Keras 数据生成器的类标签
【发布时间】:2018-12-07 14:58:57
【问题描述】:

我为我的 Keras 应用程序准备了一个自定义的自定义图像数据生成器。它运作良好,但我对类标签有疑问。这是代码的相关部分:

    def _get_batches_of_transformed_samples(self, index_array):
    # create array to hold the images
    batch_x = np.zeros((4*len(index_array),) + self.target_size+(3,), dtype='float32')
    # create array to hold the labels
    batch_y = np.zeros(4*len(index_array), dtype='float32')
    target_angles = [0, 90, 180, 270]

    for i, j in enumerate(index_array):           
        is_color = int(self.color_mode == 'rgb')
        image = cv2.imread(self.filenames[j], is_color)
        if is_color:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)                               
        for rotation_angle in target_angles:
            rotated_im = rotate(image, rotation_angle, self.target_size[:2])
            if self.preprocess_func: rotated_im = self.preprocess_func(rotated_im)                  
            batch_x[i] = rotated_im
            batch_y[i] = rotation_angle

    batch_y = to_categorical(batch_y, 271)            
    return batch_x, batch_y

如代码所示,我必须在 to_categorical 方法中使用 271。但是我只生成 4 个类。那么,如何将 0,1,2,3 分配给 0,90,180 和 270 并在 to_categorical 方法中使用 4 而不是 271?

【问题讨论】:

    标签: python-3.x keras conv-neural-network


    【解决方案1】:

    使用两个列表:

    target_angles = [0,90,180,270]
    target_cat_angles = np.array(to_categorical([0,1,2,3]))
    

    在循环中:

    for rotation_angle, cat_angle in zip(target_angles, target_cat_angles):
        ...
        batch_y[i] = cat_angle
        ...
    

    【讨论】:

      猜你喜欢
      • 2020-12-10
      • 2019-03-16
      • 1970-01-01
      • 2018-10-23
      • 2019-09-28
      • 2020-09-08
      • 2018-02-09
      • 2020-06-04
      • 1970-01-01
      相关资源
      最近更新 更多