Welcome to Jiyuniverse

[주절주절] 이미지 라벨링 후 데이터셋을 분리한 파일을 삭제해버렸다. 본문

ML, DL

[주절주절] 이미지 라벨링 후 데이터셋을 분리한 파일을 삭제해버렸다.

JJiiyun 2025. 2. 11. 18:45

하하하하하.

labels 폴더가 다 날아갔었다. 

처음 데이터셋에서 다시 같은 코드를 돌리는 게 내키지 않아서,

images 내부 폴더 각각의 파일명과 비교해서 같으면 labels 내부 폴더에 저장하는 코드를 작성했다.

 

def collect_same_file(origin_list, labels) : # origin_list = images_test
  origin_list_set = {os.path.splitext(os.path.basename(img))[0] for img in origin_list}
  dest_list = [label for label in labels if os.path.splitext(os.path.basename(label))[0] in origin_list_set]
  return dest_list
  
def move_files(dest_dir, dest_list): # dest_list = labels_test ('/content/dataset/data/syn_00361.txt'와 같은 파일 경로.)
    os.makedirs(dest_dir, exist_ok=True)
    for file_path in dest_list:
    if os.path.exists(file_path):  # 파일이 존재하는지 확인
        dest_path = os.path.join(dest_dir, os.path.basename(file_path))  # 대상 경로 설정
        shutil.move(file_path, dest_path)
        print(f"Moved: {file_path} → {dest_path}")
    else:
        print(f"{file_path} 파일이 존재하지 않습니다.")
      
      
images_train = glob.glob(f"{images_path}/train/*.png")
images_val = glob.glob(f"{images_path}/val/*.png")

move_files("/content/drive/MyDrive/comento/data/labels/train/", collect_same_file(images_train, labels))
move_files("/content/drive/MyDrive/comento/data/labels/val/", collect_same_file(images_val, labels))

 

 

 

더 오래 걸렸다. ^^...~!!!!

 

 

 

이 짤이 생각났다. 그래도 좋은 경험이었다. list comprehension을 쓰는 게 익숙해졌달까?..?

실무에서는 더 큰 데이터를 다루어야 하니까 이렇게 해결하는 게 효율적인 방식이었을지도 몰라. 자기위로하며... 글을 올립니다

'ML, DL' 카테고리의 다른 글

[pytorch] squeeze(), unsqueeze() 함수  (0) 2025.02.07
[pytorch] torch.rand() 함수, 텐서 크기  (0) 2025.02.06