1.建立Dataset
(1)實作概覽
(2)程式碼
Step0: Load module/package
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
from torchvision.io import read_image
import numpy as np
import os
import pandas as pd
import torch
prjt_path = r"D:\JPNB\learning_AIdea2022"
train_data_path = str(prjt_path + r"\train_data")
output_path = str(prjt_path + r"\output")
Step1: 為準心圖片建立標籤對照表: tag_locCoor_label_crop.csv
# 將有準心資料的標籤撈出來
df_Y = pd.read_csv(output_path + r"\tag_locCoor_Y.csv") # 有準心圖片
df_all_label = pd.read_csv(prjt_path + r"\tag_locCoor_label.csv", encoding = "Big5") # 所有圖片的標籤
df_all_label = df_all_label[['Img', 'label']]
# 串上圖片其他資訊
crop_img_labels = df_Y.merge(df_all_label, on="Img", how="left")
print(crop_img_labels.shape, df_Y.shape) # (19705, 9) (19705, 8)
# crop_img_labels.groupby("label").size().sort_values(ascending=True) # 19705張圖片
tmp_cropimg_name_list = [] # 19616張圖片,89張圖片在裁切過程錯誤,所以並非 19705張圖片
path = str(output_path + r"\crop_img")
for img_name in os.listdir(path):
img_name = img_name.replace("crop_", "")
tmp_cropimg_name_list.append(img_name)
final_crop_label_df = crop_img_labels[crop_img_labels["Img"].isin(tmp_cropimg_name_list)]
print(final_crop_label_df.shape) # (19616, 9)
final_crop_label_df.to_csv(prjt_path + r"\tag_locCoor_label_crop.csv")
Step2: 客製化類別CustomImageDataset()
class CustomImageDataset(Dataset):
def __init__(self, annotations_file, img_dir, height, width, transform=None, target_transform=None):
self.img_labels = pd.read_csv(annotations_file)[["label"]]
self.img_name = pd.read_csv(annotations_file)[["Img"]]
self.img_name = "crop_"+self.img_name
self.img_dir = img_dir
self.transform = transform
self.target_transform = target_transform
self.height = height # Debug
self.width = width
def __len__(self):
return len(self.img_labels)
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.img_name.iloc[idx, 0])
image = read_image(img_path)
resize = transforms.Resize([self.width, self.height]) # Debug: resize img to let all images in same size.
image = resize(image)
label = self.img_labels.iloc[idx, 0] # Debug: 要修改成[idx, 0]才是取數值,不會把欄位名稱一起誤抓
#print("label:", label)
if self.transform:
image = self.transform(image)
if self.target_transform:
label = self.target_transform(label)
return image, label
Step3: 實例化類別
dataset = CustomImageDataset(annotations_file=str(prjt_path + r"\tag_locCoor_label_crop.csv"),
img_dir=str(output_path + r"\crop_img"), height = 190, width=190)
# pick 1000th img data in dataset
first_data = dataset[10000]
features, labels = first_data
print(features, labels)
print(len(dataset))
心得小語:
今天差點趕不上發文,在23:30總算Debug完畢開始振筆疾書寫文章,豪開心呀~!!
昨天追劇看太晚,《何以笙簫默》的何以琛實在太有魅力了~ 但今天一整天,真的首次體會到年過25後體力明顯下滑QQ,下班後直接昏睡一個小時補眠。這周末得好好努力啦,當然也是要繼續追劇調劑身心啦!!!
今日工時: 50mins*2
相信自己是成功的秘訣
You have to BELIEVE IN YOURSELF. That’s the secret of success.