def __getitem__(self, idx):

        # 图片路径
        batch_x = self.x_y[idx * self.batch_size:(idx + 1) * self.batch_size,
                           0]
        # 图片标签
        batch_y = self.x_y[idx * self.batch_size:(idx + 1) * self.batch_size,
                           1:]
        # 这里是像素数组 (224,224,3)

        batch_x = np.array(
            [self.preprocess_img(img_path) for img_path in batch_x])
        # smooth labels
        batch_y = np.array(batch_y).astype(np.float32) * (1 - 0.05) + 0.05 / 40

        # # 训练集数据增强
        if self.is_train:
            indexs = np.random.choice([0, 1, 2],
                                      batch_x.shape[0],
                                      replace=True,
                                      p=[0.4, 0.4, 0.2])
            mask_indexs = np.where(indexs == 1)
            multi_indexs = np.where(indexs == 2)

            if len(multi_indexs):
                # 数据增强
                multipy_batch_x = batch_x[multi_indexs]
                multipy_batch_y = batch_y[multi_indexs]

                train_datagenerator = self.train_datagen.flow(
                    multipy_batch_x,
                    multipy_batch_y,
                    batch_size=self.batch_size)
                (multipy_batch_x, multipy_batch_y) = train_datagenerator.next()

                batch_x[multi_indexs] = multipy_batch_x
                batch_y[multi_indexs] = multipy_batch_y

            if len(mask_indexs[0]):
                # 随机遮挡
                mask_batch_x = batch_x[mask_indexs]
                mask_batch_y = batch_y[mask_indexs]
                mask_batch_x = np.array(
                    [self.cutout_img(img) for img in mask_batch_x])

                batch_x[mask_indexs] = mask_batch_x
                batch_y[mask_indexs] = mask_batch_y

        # 预处理
        batch_x = np.array([preprocess_input(img) for img in batch_x])

        #  # plt 绘制图像时需要将其换成整型
        # for index,label in enumerate(batch_y):
        #     print(np.argmax(label))
        #     plt.subplot(2,8,index+1)
        #     plt.imshow(batch_x[index].astype(int))
        # plt.show()
        # exit()

        return batch_x, batch_y
Ejemplo n.º 2
0
 def preprocess_img(self, img_path):
     """
     image preprocessing
     you can add your special preprocess method here
     """
     img = Image.open(img_path)
     #resize_scale = self.img_size[0] / max(img.size[:2])
     #img = img.resize((int(img.size[0] * resize_scale), int(img.size[1] * resize_scale)))
     img = img.resize((256, 256))
     img = img.convert('RGB')
     img = np.array(img)
     img = img[16:16 + 224, 16:16 + 224]
     img = preprocess_input(img)
     #img = img[:, :, ::-1]
     #img = self.center_img(img, self.img_size[0])
     return img
Ejemplo n.º 3
0
def preprocess_img(img_path, img_size):
    try:
        img = Image.open(img_path)
        img = img.resize((256, 256))
        img = img.convert('RGB')
        # img.show()
        img = np.array(img)
        imgs = []
        for _ in range(10):
            i = random.randint(0, 32)
            j = random.randint(0, 32)
            imgg = img[i:i + 224, j:j + 224]
            imgg = preprocess_input(imgg)
            imgs.append(imgg)
        return imgs
    except Exception as e:
        print('发生了异常data_process:', e)
        return 0
def preprocess_img(img_path, img_size):
    """
    image preprocessing
    you can add your special preprocess method here
    """
    img = Image.open(img_path)
    # resize_scale = img_size / max(img.size[:2])
    # img = img.resize((int(img.size[0] * resize_scale), int(img.size[1] * resize_scale)))
    img = img.resize((256, 256))
    img = img.convert('RGB')
    img = np.array(img)
    imgs = []
    for _ in range(10):
        i = random.randint(0, 32)
        j = random.randint(0, 32)
        imgg = img[i:i + 224, j:j + 224]
        imgg = preprocess_input(imgg)
        imgs.append(imgg)
    return imgs