fastai vision tutorial/segmentation
segmentation = predict a class for every pixel in image.
Camvid dataset는 차에서 카메라로 찍은 데이터셋이다. 모든 픽셀에 길, 차, 보행자와 같은 라벨링이 되어있다.
1. path = untar_data(URLs.CAMVID_TINY)
path.ls()
->이해x
=> 아마도 path에는 codes, images, labels 폴더가 있는데 그 폴더에 뭐가 있는 지 설명해주는 듯하다.
=>images는 이미지 파일들, labels는 숫자로 된 정답, codes는 그 숫자를 class로 나타내주는 파일
The images folder contains the images, and the corresponding segmentation masks of labels are in the labels folder. The codes file contains the corresponding integer to class (the masks have an int value for each pixel).
2. codes = np.loadtxt(path/'codes.txt' , dtype = str)
codes
넘파이를 이용해서 해당 경로의 txt파일을 받아와준다.
numpy array로 받아온다.
해당 클래스로 분류하여 보여진다.
3-1. using high level API
4. fnames = get_image_files(path/"images")
fnames[0]
모든 이미지파일을 가져온다.
5.(path/"labels").ls()[0]
이렇게 세개의 폴더가 있다.
6.def label_func(fn): return path/"labels"/f"{fn.stem}_P{fn.suffix}"
4번과 5번 코드의 결과, 이미지파일명에 _P를 추가한 것이 라벨파일명임을 유추할 수 있었다.
뒤에는 정규표현식같은데 일단은 넘어가도록 하자.
7.dls = SegmentationDataLoaders.from_label_func(path, bs = 8, fnames = fanames, label_func = label_func, codes = codes)
이전에는 ImageDataLoaders를 썼는데 이번에는 다른 메소드를 사용하였다.
item_tfms는 이미 데이터셋의 사이즈가 모두 동일하기때문에 사용할 필요 없다.
8.dls.show_batch(max_n = 6)
최대 6개까지 볼 수 있다.
9.learn = unet_learner(dls, resnet34)
learn.fine_tune(6)
전통적인 cnn_learner은 segmentation에서는 쓰이지 않는다. 새로운 모델인 unet을 사용해보려고 한다.
이때는 learning_rate가 사용되지 않나..?
10.learn.show_results(max_n = 6, figsize = (7,8))
target/prediction이 보여진다.
3-2. data block API
우리는 path와 files... 등을 사용하는 DataLoaders를 datablock을 통해 데이터를 모을 수 있다.
4.camvid = DataBlock(blocks = ImageBlock, MaskBlock(codes)),
get_items = get_image_files,
get_y = label_func,
splitter = RandomSplitter(),
batch_tfms = aug_transforms(size = (120, 160)))
어떤 타입이 쓰였는지.(데이터에 무엇이 있는 지 알기 어려우므로 MaskBlock에 codes를 제공합니다.)
아이템을 모으는 방법
아이템으로부터 라벨을 구하는 방법
아이템을 분리하는 방법
data augmentation(노이즈 줘서 오버핏 막는 방법)
5.dls = camvid.dataloaders(path/"images", path = path, bs = 8)
6.dls.show_batch(max_n = 6)