초기 배경
머신러닝 , 데이터 분석 등 해당 task 들을 진행하다 보면 train dataset과 test dataset으로 나눠야 할 때가 있습니다.
저 또한 데이터를 나눠야 했을 때 어떻게 진행해야 할지 과정을 몰라 공부하며 배운 내용을 정리하며 습득하기 위해 해당 글을 작성하였습니다.
참고 사이트를 통해 번역하여 해당 과정들을 진행하였습니다. split을 할 data는 AI-Hub 사이트에서 가져왔습니다.
AI-Hub
AI 허브 데이터 검색 추천검색어
aihub.or.kr
split-folder를 사용하기 위해선 반드시 다음과 같은 directory-file 경로로 되어있어야 합니다. 다시 말하자면 input 폴더에 class 폴더가 있어야지 작동합니다.
input/
class1/
img1.jpg
img2.jpg
...
class2/
imgWhatever.jpg
...
...
해당 input 폴더를 split-folders를 사용하면 다음과 같은 output 형태가 나오게 됩니다.
output/
train/
class1/
img1.jpg
...
class2/
imga.jpg
...
val/
class1/
img2.jpg
...
class2/
imgb.jpg
...
test/
class1/
img3.jpg
...
class2/
imgc.jpg
...
설치
pip install split-folders
모듈 불러오기
import splitfolders
splitfolders.ratio("input_folder", output="output",
seed=1337, ratio=(.8, .1, .1), group_prefix=None, move=False) # default values
input_folder에는 split하고자 하는 폴더의 경로, output은 split한 후에 train val test폴더로 나뉘었을 때 저장할 경로를 의미합니다.
또한 ratio 가 .8 .1 .1로 되어있는 것은 80% triain , 10% val, 10 % test 데이터 셋으로 나누겠다는 의미입니다.
만약 train 80% test 20%로 나누고 싶다면 ration = (0.8,0.2) 이런식으로도 작성해주시면 됩니다.
이때 Anotation file ( .txt, .xml, .json)이 존재한다면 이미지 파일과 짝지어서 나눠 줄 수도 있습니다.
group_prefix = 2 를 설정하면 됩니다.
group_prefix = group의 길이로 설정합니다. 한 이미지에 해당하는 anotation file 유형이 몇개인지에 따라 입력하시면 됩니다.
Usage:
splitfolders [--output] [--ratio] [--fixed] [--seed] [--oversample] [--group_prefix] [--move] folder_with_images
Options:
--output path to the output folder. defaults to `output`. Get created if non-existent.
--ratio the ratio to split. e.g. for train/val/test `.8 .1 .1 --` or for train/val `.8 .2 --`.
--fixed set the absolute number of items per validation/test set. The remaining items constitute
the training set. e.g. for train/val/test `100 100` or for train/val `100`.
Set 3 values, e.g. `300 100 100`, to limit the number of training values.
--seed set seed value for shuffling the items. defaults to 1337.
--oversample enable oversampling of imbalanced datasets, works only with --fixed.
--group_prefix split files into equally-sized groups based on their prefix
--move move the files instead of copying
Example:
splitfolders --ratio .8 .1 .1 -- folder_with_images
마지막으로 해당 변수에 대한 설명을 추가하고 마무리하겠습니다.
감사합니다.
'Big data > Data mining' 카테고리의 다른 글
Selenium 으로 쿠팡 리뷰데이터 Crawling 해보기 (0) | 2022.10.10 |
---|---|
Apriori Algorithm(연관규칙분석) 이란? (0) | 2022.08.10 |
Bloom filter (블룸 필터)란? (0) | 2022.06.30 |