kaggle - Skin Cancer MNIST: HAM10000
import pandas as pd
import os
import shutil
# CSV 파일 경로와 이미지 폴더 경로 설정
csv_file_path = 'C:\\\\metadata.csv'
image_folder_path = 'C:\\\\images'
output_folder_base = 'C:\\\\type' # 새로운 폴더들을 저장할 기본 폴더
# CSV 파일을 읽어 데이터프레임으로 로드
data = pd.read_csv(csv_file_path)
# 이미지 파일들을 클래스 이름별로 폴더 생성 및 이미지 이동
for index, row in data.iterrows():
image_id = row['image_id'] # 이미지 파일 이름
class_name = row['dx'] # 클래스 이름으로 폴더 생성
class_folder = os.path.join(output_folder_base, class_name)
os.makedirs(class_folder, exist_ok=True) # 이미 폴더가 존재하면 무시
image_filename = image_id + '.jpg' # 이미지 파일 이름 생성
source_image_path = os.path.join(image_folder_path, image_filename)
destination_image_path = os.path.join(class_folder, image_filename)
# 이미지 파일이 존재하는 경우에만 이동
if os.path.exists(source_image_path):
# 이미지 파일을 대상 폴더로 이동
shutil.copy(source_image_path, destination_image_path)
print(f"Moved image {image_filename} to {class_folder}")
else:
print(f"Image {image_filename} not found. Skipping.")
print("Image classification completed.")
import os
import json
import random
image_folder_path = 'C:\\\\type' # 이미지 파일들이 저장된 폴더 경로
output_jsonl_path = 'C:\\\\type.jsonl' # 생성될 JSONL 파일 경로
bucket_name = 'skin_type_data' # GCS 버킷 이름
# 이미지 파일 확장자 목록
image_extensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp']
# 클래스별로 데이터 분배 비율 설정
class_data_ratio = {
"akiec": {"training": 0.7, "validation": 0.15, "test": 0.15},
"bcc": {"training": 0.7, "validation": 0.15, "test": 0.15},
"bkl": {"training": 0.7, "validation": 0.15, "test": 0.15},
"df": {"training": 0.7, "validation": 0.15, "test": 0.15},
"mel": {"training": 0.7, "validation": 0.15, "test": 0.15},
"nv": {"training": 0.7, "validation": 0.15, "test": 0.15},
"vasc": {"training": 0.7, "validation": 0.15, "test": 0.15},
}
# JSONL 파일을 생성하면서 이미지 파일들을 인식하여 정보를 기록
with open(output_jsonl_path, 'w') as jsonl_file:
for root, _, files in os.walk(image_folder_path):
for file in files:
if file.lower().endswith(tuple(image_extensions)):
image_path = os.path.join(root, file)
# 이미지 파일 이름에서 클래스 레이블 추출
class_label = os.path.basename(os.path.dirname(image_path))
# 데이터 분배 비율에 따라 ml_use 값 선택
ml_use = random.choices(
list(class_data_ratio[class_label].keys()),
weights=list(class_data_ratio[class_label].values())
)[0]
# GCS 경로 생성 (슬래시 사용)
image_uri = f'gs://{bucket_name}/{class_label}/{file}'
# JSONL 형식으로 작성
image_info = {
"imageGcsUri": image_uri,
"classificationAnnotation": {
"displayName": class_label
},
"dataItemResourceLabels": {
"aiplatform.googleapis.com/ml_use": ml_use
}
}
# JSONL 형식 문자열로 작성하여 파일에 기록
jsonl_file.write(json.dumps(image_info) + '\\n')
print("JSONL file creation completed.")
생성 파일
참고 문서 (분류용 이미지 학습 데이터 준비)
분류용 이미지 학습 데이터 준비 | Vertex AI | Google Cloud
주의**!** 로컬 컴퓨터에 있는 파일 불러와서 jsonl 파일 생성
참고**!** jsonl 파일 만들 때 'aiplatform.googleapis.com/annotation_set_name': 'value' 이거 안 넣은 이유 > 데이터셋의 개별 항목에 어노테이션이나 메타데이터같은 추가 정보를 부여할 수 있는데 일반적으로 각 이미지에 대한 class label을 어노테이션으로 부여 >여러 작업자가 동일한 데이터셋에 대해 서로 다른 어노테이션을 수행하고자 할 때, 각 작업자가 생성한 어노테이션을 개별 세트로 관리할 수 있다. (선택사항)
import os
import json
import random
image_folder_path = 'C:\\\\type' # 이미지 파일들이 저장된 폴더 경로
output_jsonl_path = 'C:\\\\type_tune.jsonl' # 생성될 JSONL 파일 경로
bucket_name = 'skin_type_data' # GCS 버킷 이름
# 이미지 파일 확장자 목록
image_extensions = ['.png', '.jpg', '.jpeg', '.gif', '.bmp']
# 클래스별로 데이터 분배 비율 설정
class_data_ratio = {
"akiec": {"training": 0.7, "validation": 0.15, "test": 0.15},
"bcc": {"training": 0.7, "validation": 0.15, "test": 0.15},
"bkl": {"training": 0.6, "validation": 0.2, "test": 0.2},
"df": {"training": 0.7, "validation": 0.15, "test": 0.15},
"mel": {"training": 0.6, "validation": 0.2, "test": 0.2},
"nv": {"training": 0.4, "validation": 0.3, "test": 0.3},
"vasc": {"training": 0.7, "validation": 0.15, "test": 0.15},
}
# JSONL 파일을 생성하면서 이미지 파일들을 인식하여 정보를 기록
with open(output_jsonl_path, 'w') as jsonl_file:
for root, _, files in os.walk(image_folder_path):
for file in files:
if file.lower().endswith(tuple(image_extensions)):
image_path = os.path.join(root, file)
# 이미지 파일 이름에서 클래스 레이블 추출
class_label = os.path.basename(os.path.dirname(image_path))
# 데이터 분배 비율에 따라 ml_use 값 선택
ml_use = random.choices(
list(class_data_ratio[class_label].keys()),
weights=list(class_data_ratio[class_label].values())
)[0]
# GCS 경로 생성 (슬래시 사용)
image_uri = f'gs://{bucket_name}/{class_label}/{file}'
# JSONL 형식으로 작성
image_info = {
"imageGcsUri": image_uri,
"classificationAnnotation": {
"displayName": class_label
},
"dataItemResourceLabels": {
"aiplatform.googleapis.com/ml_use": ml_use
}
}
# JSONL 형식 문자열로 작성하여 파일에 기록
jsonl_file.write(json.dumps(image_info) + '\\n')
print("JSONL file creation completed.")