【问题标题】:Applying transforms to fastai v2 vision将变换应用于 fastai v2 视觉
【发布时间】:2020-12-07 19:29:33
【问题描述】:

在 fastai v2 中,我正在尝试添加图像增强

所以

tfms = aug_transforms(do_flip = True,
                                 flip_vert=True, 
                                 max_lighting=0.1, 
                                 )
data = ImageDataLoaders.from_df(df,bs=5,item_tfms=tfms,folder=path_to_data)

这给出输出

Could not do one pass in your dataloader, there is something wrong in it

当我这样做时

data.show_batch()

它给了

RuntimeError: "check_uniform_bounds" not implemented for 'Byte'

如何解决

【问题讨论】:

  • 你有没有想过这个问题?我遇到了同样的问题。似乎我可以进行填充和裁剪,但任何更奇特的东西都会引发同样的错误。
  • 大声笑没有。可能通过投票我们可以回答这个问题

标签: fast-ai


【解决方案1】:

我没有尝试 do_flip 转换,但对我有用的是将它们应用为不是 item_tfms 而是作为 batch_tfms:

item_tfms = [ Resize((200, 150), method='squish')]
 
batch_tfms = [Brightness(max_lighting = 0.3, p = 0.4),
    Contrast(max_lighting = 0.6, p = 0.4),
    Saturation(max_lighting = 0.75, p = 0.4)]
 
db = DataBlock(blocks = (ImageBlock, CategoryBlock),
                  get_items = get_image_files,
                  splitter = RandomSplitter(valid_pct=0.2, seed=42),
                  item_tfms=item_tfms,
                  batch_tfms=batch_tfms,
                  get_y = parent_label)

然后您可以将 DataBlock 输入到 DataLoader 中,例如 fastbook tutorial

【讨论】:

  • 将尝试更新您。谢谢回复
  • 撞头。呃。有时这是最简单的事情。
  • @JudoWill 并且 orozco 得到了可以帮助我们检查的答案
【解决方案2】:

我从 fastai 文档中得到了这个。添加与问题相关的内容,您可以查看与 augs 相关的所有内容here

class AlbumentationsTransform(RandTransform):
    "A transform handler for multiple `Albumentation` transforms"
    split_idx,order=None,2
    def __init__(self, train_aug, valid_aug): store_attr()
    
    def before_call(self, b, split_idx):
        self.idx = split_idx
    
    def encodes(self, img: PILImage):
        if self.idx == 0:
            aug_img = self.train_aug(image=np.array(img))['image']
        else:
            aug_img = self.valid_aug(image=np.array(img))['image']
        return PILImage.create(aug_img)

基本上这就是你所需要的

用于设置 diff augs 使用

def get_train_aug(): return albumentations.Compose([
            albumentations.RandomResizedCrop(224,224),
            albumentations.Transpose(p=0.5),
])

def get_valid_aug(): return albumentations.Compose([
    albumentations.CenterCrop(224,224, p=1.),
    albumentations.Resize(224,224)
], p=1.)

然后

item_tfms = [Resize(256), AlbumentationsTransform(get_train_aug(), get_valid_aug())]

dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=item_tfms)
dls.train.show_batch(max_n=4)

享受

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 2020-08-13
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多