코딩연습장/Keras
[데이터 준비] Tiny-Imagenet
인공지능스타터
2021. 6. 29. 18:08
CIFAR-100은 모델들을 비교하는데 좋은 데이터셋이 아닌 것 같아서 Tiny-imagenet 데이터셋을 사용하려고 한다. 이미지 Shape는 64 x 64이며, 200개의 클래스를 가지고 있다.
이제 데이터셋을 준비해보자!

먼저 Tiny-ImageNet을 API를 통해 다운로드 하여야 한다.
wget http://cs231n.stanford.edu/tiny-imagenet-200.zip
wget 명령어를 써야하다보니, Ubuntu나 linux기반에서는 아주 손쉽게 다운로드가 진행되는데 윈도우에서는 뭔가 잘 안된다....PowerShell도 이용해보았지만 다운로드 실패...
결국 우분투 서버에서 다운로드 받고, 윈도우로 옮겼다...(서버가 없으신 분들은 가상환경을 이용하시길..)
**혹시 윈도우에서 다운로드를 성공하였다면 댓글로 방법 좀 알려주세요~
압축을 풀어보면 다음과 같은 클래스별로 폴더가 묶여져있는데,
나중에 데이터 로드할 때 편의를 두기 위해서 train-set과 test-set을 미리 나누어 놓을 것이다. 이것은 개인의 취향이니 txt파일로 경로로만 나누어 놓아도 되고, 나처럼 아예 이미지 자체를 나누어 놓아도 된다.
import glob
import os
import shutil
src_path = "./tiny_imagenet/"
train_dst_path = "./train/"
test_dst_path = "./test/"
split_ratio = 0.9
def check_folder_and_make(path):
if not os.path.isdir(path):
os.mkdir(path)
def main():
check_folder_and_make(train_dst_path)
check_folder_and_make(test_dst_path)
folders = os.listdir(src_path)
for folder in folders:
check_folder_and_make(train_dst_path+folder)
check_folder_and_make(test_dst_path+folder)
img_paths = glob.glob(src_path+folder+"/images/*.JPEG")
img_len = len(img_paths)
train_index = int(img_len * split_ratio)
train_set = img_paths[:train_index]
test_set = img_paths[train_index:]
for train in train_set:
shutil.copy2(train, train_dst_path+folder)
for test in test_set:
shutil.copy2(test, test_dst_path+folder)
if __name__ == "__main__":
main()
준비 끝! 이제 위 데이터셋을 load해서 사용하면 된다.
다음 글에서는 Tiny-imagenet을 받아오기 위한 Geneartor를 만들 것이다!