import
from transformers import AutoModelForImageClassification, AutoImageProcessor from datasets import load_dataset import torch from huggingface_hub import hf_hub_download import random
test dataset relabeling!!!
- 데이터셋의 라벨링이 잘못되어있음..
- anger는 1이지만 0으로, 나머지도 모두 그렇게 되어있음
def remap_labels(dataset, label_map): """ 데이터셋의 레이블을 지정된 매핑에 따라 변환합니다. Args: dataset: 변환할 데이터셋. label_map: 기존 레이블에서 새로운 레이블로 매핑된 딕셔너리. Returns: 레이블이 변환된 데이터셋. """ def map_labels(example): example['label'] = label_map[example['label']] return example return dataset.map(map_labels) # 1. 레이블 매핑 정의 label_mapping = { 3: 0, # happiness -> 0 0: 1, # anger -> 1 5: 2, # surprise -> 2 1: 3, # disgust -> 3 2: 4, # fear -> 4 4: 5 # sadness -> 5 } # 2. 데이터셋 로드 및 레이블 변환 dataset = load_dataset("JANGJIWON/UGRP_sketchset_textbook", split="train") # 테스트 데이터셋 print("Sample Test Dataset:") for i in range(10): print(f"Index {i}: {dataset[i]}") # 테스트 데이터셋 레이블 변환 dataset = remap_labels(dataset, label_mapping) print("Sample Test Dataset:") for i in range(10): print(f"Index {i}: {dataset[i]}")
Sample Test Dataset: Index 0: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1009x1360 at 0x7F14EEE485E0>, 'label': 0} Index 1: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=989x720 at 0x7F14EEE480A0>, 'label': 0} Index 2: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=794x675 at 0x7F14EEE485E0>, 'label': 0} Index 3: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1146x829 at 0x7F14EEE48070>, 'label': 1} Index 4: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=4000x3000 at 0x7F14EEE48100>, 'label': 1} Index 5: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3690x2330 at 0x7F14EEE480A0>, 'label': 1} Index 6: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=794x1123 at 0x7F14EEDA8280>, 'label': 1} Index 7: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2208x3416 at 0x7F14EEDA82B0>, 'label': 2} Index 8: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3056x3056 at 0x7F14EEDA8220>, 'label': 2} Index 9: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1635x1264 at 0x7F14EEDA82B0>, 'label': 2} Sample Test Dataset: Index 0: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1009x1360 at 0x7F13F3785220>, 'label': 1} Index 1: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=989x720 at 0x7F13F3785220>, 'label': 1} Index 2: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=794x675 at 0x7F13F3785220>, 'label': 1} Index 3: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1146x829 at 0x7F13F3785220>, 'label': 3} Index 4: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=4000x3000 at 0x7F13F3785220>, 'label': 3} Index 5: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3690x2330 at 0x7F13F3785220>, 'label': 3} Index 6: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=794x1123 at 0x7F13F3785220>, 'label': 3} Index 7: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2208x3416 at 0x7F13F3785220>, 'label': 4} Index 8: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=3056x3056 at 0x7F13F3785220>, 'label': 4} Index 9: {'image': <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1635x1264 at 0x7F13F3785220>, 'label': 4}
Emoset
# 1. 모델 및 프로세서 로드 model_name = "google/vit-base-patch16-224" model = AutoModelForImageClassification.from_pretrained(model_name, num_labels=6, ignore_mismatched_sizes=True) # 감정 클래스 수 processor = AutoImageProcessor.from_pretrained(model_name) # 로컬로 저장된 모델 가중치 로드 model_weights_path = hf_hub_download(repo_id="xodhks/ViT-emotion-LoRA", filename="model_emoset_34.33.pth") model.load_state_dict(torch.load(model_weights_path, map_location='cpu'), strict=False) # 모델을 평가 모드로 설정 model.eval() # # 2. 데이터셋 로드 # dataset = load_dataset("JANGJIWON/UGRP_sketchset_textbook") # 레이블 정보 확인 print("Checking label distribution...") print(f"Dataset label information: {dataset.features['label']}") # 전체 데이터셋 평가 correct = 0 total = len(dataset) # "train" 키가 필요하지 않습니다. print("\nEvaluating dataset...") # GPU 사용 여부 확인 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) for sample in dataset: image = sample['image'] label = sample['label'] # 이미지 전처리 inputs = processor(images=image, return_tensors="pt").to(device) # 모델 추론 with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits # 클래스에 대한 점수 predicted_idx = logits.argmax().item() # 가장 높은 점수를 가진 클래스 인덱스 if predicted_idx == label: correct += 1 # 정확도 계산 accuracy = correct / total * 100 print(f"\nAccuracy: {accuracy:.2f}% ({correct}/{total})")
result
- emoset으로 한 경우 19%의 낮은 확률을 보여준다
Epoch [9/100], Loss: 0.0443
Test Accuracy after Epoch 9: 19.05%
Monet
# 1. 모델 및 프로세서 로드 model_name = "google/vit-base-patch16-224" model = AutoModelForImageClassification.from_pretrained(model_name, num_labels=6, ignore_mismatched_sizes=True) # 감정 클래스 수 processor = AutoImageProcessor.from_pretrained(model_name) # 로컬로 저장된 모델 가중치 로드 model_weights_path = hf_hub_download(repo_id="xodhks/ViT-emotion-LoRA", filename="model_monet_33.33.pth") model.load_state_dict(torch.load(model_weights_path, map_location='cpu'), strict=False) # 모델을 평가 모드로 설정 model.eval() # # 2. 데이터셋 로드 dataset = load_dataset("JANGJIWON/UGRP_sketchset_textbook") # 레이블 정보 확인 print("Checking label distribution...") print(f"Dataset label information: {dataset.features['label']}") # 전체 데이터셋 평가 correct = 0 total = len(dataset) # "train" 키가 필요하지 않습니다. print("\nEvaluating dataset...") # GPU 사용 여부 확인 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) for sample in dataset: image = sample['image'] label = sample['label'] # 이미지 전처리 inputs = processor(images=image, return_tensors="pt").to(device) # 모델 추론 with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits # 클래스에 대한 점수 predicted_idx = logits.argmax().item() # 가장 높은 점수를 가진 클래스 인덱스 if predicted_idx == label: correct += 1 # 정확도 계산 accuracy = correct / total * 100 print(f"\nAccuracy: {accuracy:.2f}% ({correct}/{total})")
Crawling
# 1. 모델 및 프로세서 로드 model_name = "google/vit-base-patch16-224" model = AutoModelForImageClassification.from_pretrained(model_name, num_labels=6, ignore_mismatched_sizes=True) processor = AutoImageProcessor.from_pretrained(model_name, use_fast=True) # use_fast=True로 설정 # 로컬로 저장된 모델 가중치 로드 model_weights_path = hf_hub_download(repo_id="xodhks/ViT-emotion-LoRA", filename="model_crawling_65.33.pth") model.load_state_dict(torch.load(model_weights_path, map_location='cpu', weights_only=True), strict=False) # weights_only=True 설정 # 모델을 평가 모드로 설정 model.eval() # 2. 데이터셋 로드 dataset = load_dataset("JANGJIWON/UGRP_sketchset_textbook", split="train") # 3. 데이터셋 평가 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) correct = 0 total = len(dataset) print("\nEvaluating dataset...") for sample in dataset: image = sample['image'] label = sample['label'] # 이미지 전처리 inputs = processor(images=image, return_tensors="pt").to(device) # 모델 추론 with torch.no_grad(): outputs = model(**inputs) logits = outputs.logits predicted_idx = logits.argmax().item() if predicted_idx == label: correct += 1 accuracy = correct / total * 100 print(f"\nAccuracy: {accuracy:.2f}% ({correct}/{total})")