【问题标题】:Preparing created dataset for semantic segmentation using fastai in google colab在 google colab 中使用 fastai 为语义分割准备创建的数据集
【发布时间】:2020-08-29 09:05:48
【问题描述】:

我已经问过一个关于这个问题的问题。我只是提供更多细节。我一直在尝试训练我以 Pascal Voc 格式创建的数据集。它是对每个图像中的人行道和背景进行语义分割。我想并且目前正在使用 google colab 中的 Python fastai 库来完成这项任务。当将标记图像传输到输入图像时,我收到错误消息。我需要能够在 fastai 中训练这个数据集。我改编自一个github源代码:https://github.com/keyurparalkar/Semantic-Segmentation-with-UNETs/blob/master/FASTai_UNET.ipynb

%reload_ext autoreload
%autoreload 2
%matplotlib inline

import fastai
from fastai import *
from fastai.vision import*

path = '/content/drive/My Drive/Umes2020'
image_ip = path + '/JPEGImages'
images_lbl = path + '/SegmentationClassPNG'
codes = np.array(["background","Sidewalk"])
keep_train_val = path + '/val.txt'
class SegmentationProcessor(PreProcessor):
    "`PreProcessor` that stores the classes for segmentation."
    def __init__(self, ds:ItemList): self.classes = ds.classes
    def process(self, ds:ItemList):  ds.classes,ds.c = self.classes,len(self.classes)
class SegmentationLabelList(ImageList):
    "`ItemList` for segmentation masks."
    _processor=SegmentationProcessor
    def __init__(self, items:Iterator, classes:Collection=None, **kwargs):
        super().__init__(items, **kwargs)
        self.classes,self.loss_func = classes,CrossEntropyFlat(axis=1)

    def new(self, items, classes=None, **kwargs):
        return self.new(items, ifnone(classes, self.classes), **kwargs)

    def open(self, fn): return open_mask(fn,convert_mode='P')   #HERE
    def analyze_pred(self, pred, thresh:float=0.5): return pred.argmax(dim=0)[None]
    def reconstruct(self, t:Tensor): return ImageSegment(t)
class SegmentationItemList(ImageList):
    "`ItemList` suitable for segmentation tasks."
    _label_cls,_square_show_res = SegmentationLabelList,False
get_y_fn = lambda x: images_lbl/f'{x.stem}.png'
data = (SegmentationItemList.from_folder(image_ip)
        .split_by_rand_pct()
        .label_from_func(get_y_fn,classes=codes)
        .transform(get_transforms(),size=128,tfm_y=True)
        .databunch(bs=4))
#          .normalize(imagenet_stats))
TypeError                                 Traceback (most recent call last)
<ipython-input-11-e9a537d7b20e> in <module>()
      1 data = (SegmentationItemList.from_folder(image_ip)
      2         .split_by_rand_pct()
----> 3         .label_from_func(get_y_fn,classes=codes)
      4         .transform(get_transforms(),size=128,tfm_y=True)
      5         .databunch(bs=4))

3 frames
<ipython-input-10-2728937c21a8> in <lambda>(x)
----> 1 get_y_fn = lambda x: images_lbl/f'{x.stem}.png'

TypeError: unsupported operand type(s) for /: 'str' and 'str'

【问题讨论】:

    标签: python deep-learning computer-vision object-detection image-segmentation


    【解决方案1】:

    您的 images_lbl 必须是 PosixPath

    如果您想将所有内容都转换为 PosixPath,我建议您这样做,因为 FastAI 就是基于它工作的。

    path = Path(r'/content/drive/My Drive/Umes2020')
    image_ip = path / 'JPEGImages'
    images_lbl = path / 'SegmentationClassPNG'
    

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 2021-08-18
      • 2020-08-19
      • 2020-08-14
      • 1970-01-01
      • 2018-08-24
      • 2019-03-16
      • 2020-10-10
      • 2021-03-21
      相关资源
      最近更新 更多