天天看點

【ReID】【代碼注釋】讀資料/吐資料 deep-person-reid/dataset_loader.py

源碼URL:

https://github.com/michuanhaohao/deep-person-reid/blob/master/dataset_loader.py

讀資料/吐資料,讀前39行源碼并注釋

from __future__ import print_function, absolute_import
import os
from PIL import Image
import numpy as np
import os.path as osp

import torch  # 需要重構pytorch的dataloader函數
from torch.utils.data import Dataset



def read_image(img_path):  # 定義讀圖檔函數,傳img_path進去
    got_img = False  # 定義标志位,判斷是否讀到圖檔以便報錯

    # 如果圖檔有問題?如果網絡通信斷了?如果因為各種原因沒讀到圖檔?
    if not os.path.exists(img_path):  # path是否存在
        raise IOError("{} dose not exist".format(img_path))  # 不存在則報錯"img_path dose not exist"
    while not got_img:  # 若沒有讀到圖檔,while循環
        try:  # 嘗試讀圖檔
            img = Image.open(img_path).convert('RGB')  # 函數核心,打開圖檔,轉RGB
            got_img = True  # 讀到圖檔,标志位變為True
        except IOError:  # 否則出現異常
            print("IOError incurred when reading '{}'. Will redo. Don't worry. Just chill.".format(img_path))  # 報錯
            pass  # 循環跳出
    return img  # 傳回img


class ImageDataset(Dataset):  # 旨在重構pytorch的dataset,将Dataset作為參數放入,繼承Dataset的類
    def __init__(self, dataset, transform=None):  # 傳入dataset,目前不考慮資料增廣
        self.dataset = dataset  # 定義dataset
        self.transform = transform  # 要用到,先定義為類的屬性

    def __len__(self):  # 在直接傳回dataset的長度,傳回圖檔數量
        return len(self.dataset)

    def __getitem__(self, index):
        img_path, pid, camid = self.dataset[index]  # 結合data_manager的dataset,得到三個參數
        img = read_image(img_path)  # 用剛剛的function把img讀出來
        if self.transform is not None:  # 如果有transform
            img = self.transform  # 執行
        return img, pid, camid  # 直接傳回img和pid,camid




if __name__ == '__main__':
    import data_manager
    dataset = data_manager.init_img_dataset(root='F:/Market-1501/Market-1501-v15.09.15', name='market1501')
    train_loader = ImageDataset(dataset.train)
    from IPython import embed
    embed()
    """
    In [1]: for batch_id, (img, pid, camid) in enumerate(train_lodaer):
       ...: 	break
       ...: 
    In [2]: img
    Out[2]: <PIL.Image.Image image mode=RGB size=64x128 at 0x16C128F5A58>
    In [3]: img.save('aaaa.jpg')
    """
           

儲存圖像aaaa.jpg結果檢視:

【ReID】【代碼注釋】讀資料/吐資料 deep-person-reid/dataset_loader.py

繼續閱讀