• Tistory
    • 태그
    • 위치로그
    • 방명록
    • 관리자
    • 글쓰기
Carousel 01
Carousel 02
Previous Next

[Pytorch] Custom Dataloader를 사용하자

전공관련/Deep Learning 2019. 12. 23. 10:21




torch를 사용하다보면 단순 classification이면 기본 폴더로부터 읽어오는 dataloader를 써도 되지만 

새로운 작업을 하거나 classification 외의 다른 학습을 위해서는 custom dataloader가 필요한 경우가 있다.

 

필요한 기능에 따라 custum dataloader의 기본형을 정리 해 둘 예정!

 


1. 각 class 폴더 안에 이미지 데이터가 있는 경우

    (단, 이 경우 실제 class 이름(폴더명)을 가져오려면 폴더명 list에서 id번째 폴더명을 받아와야함)

class customDL(Dataset):
    def read_data_set(self):
        all_img_files = []
        all_labels = []

        class_names = os.walk(self.data_set_path).__next__()[1]

        for index, class_name in enumerate(class_names):
            label = index
            img_dir = os.path.join(self.data_set_path, class_name)
            img_files = os.walk(img_dir).__next__()[2]

            for img_file in img_files:
                img_file = os.path.join(img_dir, img_file)
                img = cv2.imread(img_file, flags=cv2.IMREAD_GRAYSCALE)
                if img is not None:
                    all_img_files.append(img_file)
                    all_labels.append(label)

        return all_img_files, all_labels, len(all_img_files), len(class_names)

    def __init__(self, folder_path, transform=None):
        self.data_set_path = folder_path
        self.image_files_path, self.labels, self.length, self.num_classes = self.read_data_set()
        self.transform = transform

    def __getitem__(self, idx):
        img_origin = cv2.imread(self.image_files_path[idx], flags=cv2.IMREAD_GRAYSCALE)

       # 필요한 연산 수행.

        id = self.labels[idx]

        return img_origin, id

    def __len__(self):
        return self.length

 

 

 

[추가 예정..]

 

 
저작자표시 (새창열림)

'전공관련 > Deep Learning' 카테고리의 다른 글

[Onnx] visual studio에서 onnxruntime을 설치 해 보자  (0) 2020.02.26
[Onnx] pytorch model을 onnx로 변환하여 사용하자  (1) 2020.02.26
[Pytorch] multiGPU 에서 학습한 모델을 singleGPU에서 사용하자2  (1) 2019.12.17
[Pytorch] pytorch 와 tensorboard를 같이 써보자.  (0) 2019.11.15
[Pytorch] multiGPU 에서 학습한 모델을 singleGPU에서 사용하자  (0) 2019.03.18
블로그 이미지

매직블럭

작은 지식들 그리고 기억 한조각

,

카테고리

  • 살다보니.. (449)
    • 주절거림 (3)
    • 취미생활 (36)
      • 지식과 지혜 (3)
      • 풍경이 되어 (4)
      • Memories (17)
      • 엥겔지수를 높여라 (2)
    • mathematics (6)
      • Matrix Computation (2)
      • RandomProcesses (3)
    • English.. (8)
    • Programming (147)
      • C, C++, MFC (51)
      • C# (1)
      • OpenCV (17)
      • Python (58)
      • Git, Docker (3)
      • Matlab (4)
      • Windows (3)
      • Kinect V2 (2)
      • 기타 etc. (8)
    • 전공관련 (80)
      • Algorithm (6)
      • Deep Learning (54)
      • 실습 프로그램 (4)
      • 주워들은 용어정리 (8)
      • 기타 etc. (8)
    • Computer (118)
      • Utility (21)
      • Windows (31)
      • Mac (4)
      • Ubuntu, Linux (58)
      • NAS (2)
      • Embedded, Mobile (2)
    • IT, Device (41)
      • 제품 사용기, 개봉기 (14)
      • 스마트 체험단 신청 (27)
    • Wish List (3)
    • TISTORY TIP (5)
    • 미분류. 수정중 (1)

태그목록

  • 포르투갈
  • 스마트체험단
  • 후쿠오카
  • 갤럭시노트3
  • portugal
  • matlab function
  • Computer Tip
  • 칼로리 대폭발
  • 매트랩
  • 딥러닝
  • matlab
  • 일본
  • 오봉자싸롱
  • ReadString
  • function
  • review
  • utility
  • Deep Learning
  • random variable
  • CStdioFile
  • 매트랩 함수
  • ColorMeRad
  • LIBSVM
  • Convolutional Neural Networks
  • DeepLearning
  • 크롬
  • DSLR
  • SVM
  • 에누리닷컴
  • 큐슈

달력

«   2025/06   »
일 월 화 수 목 금 토
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
06-15 04:16

LATEST FROM OUR BLOG

RSS 구독하기

BLOG VISITORS

  • Total :
  • Today :
  • Yesterday :

Copyright © 2015 Socialdev. All Rights Reserved.

티스토리툴바