-
Oxford pet dataset는 무엇인가?개인 프로젝트/Oxford pet dataset 2020. 9. 28. 17:42
Oxford pet dataset 을 이용해서
1. Image Classification
2. Object Localization
3. Semantic Segmentation
이 3가지를 진행하려고 한다.
Oxford pet dataset
Overview
we have created a 37 category pet dataset with roughly 200 images for each class. The images have a large variations in scale, pose and lighting. All images have an associated ground truth annotation of bread, head ROI, and pixel level trimap segmentation
head ROI가 있고 bounding box가 있으며 정답으로 제공된다. Segmentation map도 제공된다.
classification 할때 파일 이름으로 labeling 한다.
annotations 폴더 안에 정답 data가 있다.
segmentation을 위한 annotation(주석)이 있다.
README 파일을 통해 파일의 Content를 확인 할 수 있다.
Contents :
trimaps / Trimap annotations for every image in the dataest
위 그림에서 Pixel Annotations : 노란색이 1. foreground 파랑색이 2. Background 빨강색이 3. Not classified
xmls/ Head bounding box annotations in PASCAL VOC Format
list.txt : Combined list of all images in the dataset
Image CLASS-ID SPECIES BREED ID
ID : 1:37 Class ids
SPECIES : 1:Cat 2: Dog
BREED ID : 1-25 : Cat 1:12 : DOG
trainval.txt랑 test.txt로 나눠서 labeling 한게 있지만 나는 전체 list 데이터에서 임의로 지정해서 train와 val data를 나눠 줄 예정이다.
XML이 text 파일로 되어 있다.
<이름> ~ </이름> 까지가 그 해당하는 내용이다.
bndbox 내용은 Localization 할때 bounding box를 그릴 때 좌표 값들이다.
고양이면 1, 개면 2를 의미하고 BREED는 두개로 클래스를 만들어 놓은 것이다.
cat image는 첫번째 문자가 대문자(captial)이고 dog image는 첫번째 문자가 소문자(samll)이다.
Bombay_140 은 고양이이며 8번째 클래스이고 1이니까 고양이고 고양이 안에서만 class 매길때 4를 의미한다.
Jupyer notebook으로 Dataset을 확인하자
import os cur_dir = os.getcwd() data_dir = os.path.join(cur_dir,'oxford_pet') image_dir = os.path.join(data_dir, 'images') bbox_dir = os.path.join(data_dir,'annotations','xmls') seg_dir = os.path.join(data_dir, 'annotations','trimaps') image_files = [fname for fname in os.listdir(image_dir) if os.path.splitext(fname)[-1] == '.jpg'] print(len(image_files)) bbox_files = [fname for fname in os.listdir(bbox_dir) if os.path.splitext(fname)[-1] == '.xml'] print(len(bbox_files)) seg_files = [fname for fname in os.listdir(seg_dir) if os.path.splitext(fname)[-1] == '.png'] print(len(seg_files))
import os 를 이용해 경로를 설정한다.
dataset와 impnyb 파일이 같은 폴더안에 저장되어 있어서
cur_dir = os.getcwd()를 이용해 접근한다.
image_files을 구할때 image_dir 안에 image가 아닌 것을 고려하여 splitext를 이용해 image 파일은 마지막이 .jpg로 끝나니까 리스트[-1]을 이용한 if문을 넣어준다.
bbox_file 이랑 seg_file도 같은 방법으로 구해준다.
import random import numpy as np from PIL import Image import xml.etree.ElementTree as et import matplotlib.pyplot as plt from matplotlib.patches import Rectangle rnd_idx = random.randint(1, len(image_files)) fname = image_files[rnd_idx] fpath = os.path.join(image_dir, fname) image = Image.open(fpath) image = np.array(image) bbox_name = os.path.splitext(fname)[0]+'.xml' bbox_path = os.path.join(bbox_dir, bbox_name) tree = et.parse(bbox_path) width = float(tree.find('./size/width').text) height = float(tree.find('./size/height').text) xmin = float(tree.find('./object/bndbox/xmin').text) xmax = float(tree.find('./object/bndbox/xmax').text) ymin = float(tree.find('./object/bndbox/ymin').text) ymax = float(tree.find('./object/bndbox/ymax').text) rect_x = xmin rect_y = ymin rect_w = xmax-xmin rect_h = ymax-ymin rect = Rectangle((rect_x, rect_y), rect_w, rect_h, fill=False, color='red') plt.axes().add_patch(rect) plt.imshow(image) plt.show()
rnd_idx로 1부터 전체 갯수만큼 설정하고 랜덤으로 하나를 뽑는다.
from PIL import Image를 이용해 image를 open한다.
bbox_name은 image 파일과 동일한걸로 잡아줄려고 고른 이미지의 jpg를 빼고 xml로 바꿔준다.
여기서 xml 파싱이라는 것을 사용한다.
이 내용은 dololak.tistory.com/253를 참고했다.
데이터가 트리처럼 되어 있다고 생각하면 된다.
만약 width를 가져오고 싶으면 파일에서 width가 어디 있는지 찾고 위에서는 size 밑에 있는 것을 확인 할 수 있다.
그러면 tree.find('./size/width').text 이런식으로 해당하는 값을 가져오면 된다.
이런식으로 랜덤으로 그림과 Bbox가 나온다.
sname = os.path.splitext(fname)[0]+'.png' spath = os.path.join(seg_dir, sname) seg = Image.open(spath) seg = np.array(seg) plt.figure(figsize=(10,4)) plt.subplot(1,2,1) plt.imshow(image) plt.subplot(1,2,2) plt.imshow(seg) plt.show()
이렇게 seg image도 확인할 수 있다.
'개인 프로젝트 > Oxford pet dataset' 카테고리의 다른 글
Image Classification (0) 2020.10.21