我已经问过有关此问题的问题。我只是提供更多细节。我一直在尝试训练以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'
答案 0 :(得分:0)
您的images_lbl
必须是PosixPath
如果您想将所有内容都转换为PosixPath,这是我自FastAI在此基础上提出的建议。
path = Path(r'/content/drive/My Drive/Umes2020')
image_ip = path / 'JPEGImages'
images_lbl = path / 'SegmentationClassPNG'