Table of contents
0 Preface
Followed by the previous article: [01] google Colab tutorial running pytorch tensorboard on free GPU google Colaboratory
This time I want to talk about the actual combat of the Dataset class code, or operate it on google Colab (free GPU).
B station operation video: https://www.bilibili.com/video/BV1W8411W7d1/
1 Dataset download
Data that needs to be prepared in advance (multiple links are prepared to prevent failure):
1. Baidu Cloud, Ant and Bee/Lianshou Dataset: Link: https://pan.baidu.com/s/1jZoTmoFzaTLWh4lKBHVbEA Password: 5suq
2. Alibaba Cloud , https://www.aliyundrive.com/s/Ahspi5UMiaf
3, google cloud, https://drive.google.com/drive/folders/10BZPGiBGMgDSpI5jeLsUUViQ3uEST3b_?usp=sharing
2 codes
Entry: https://drive.google.com/drive/my-drive
code:
# Mount google drive
from google.colab import drive
drive.flush_and_unmount()
drive.mount('/content/drive', force_remount=False)
from torch.utils.data import Dataset
help(Dataset)
from torch.utils.data import Dataset
import os
import cv2 as cv
from PIL import Image
import matplotlib.pyplot as plt
class MyData(Dataset):
def __init__(self, root_dir, label_dir):
self.root_dir = root_dir
self.label_dir = label_dir
self.path = os.path.join(self.root_dir, self.label_dir)
self.img_path = os.listdir(self.path)
def __getitem__(self, index):
img_name = self.img_path[index]
img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
img = Image.open(img_item_path)
label = self.label_dir
return img,label
def __len__(self):
return len(self.img_path)
root_dir = './drive/MyDrive/AI/hymenoptera_data/train/'
ants_label_dir = "ants"
ants_dataset = MyData(root_dir,ants_label_dir)
img,label = ants_dataset[0]
plt.imshow(img)
plt.show()
label
root_dir = './drive/MyDrive/AI/hymenoptera_data/train/'
ants_label_dir = "bees"
ants_dataset = MyData(root_dir,ants_label_dir)
img,label = ants_dataset[0]
plt.imshow(img)
plt.show()
label
ls ./drive/MyDrive/AI/hymenoptera_data/train/
4 references
P6. Dataset class code combat: https://www.bilibili.com/video/BV1hE411t7RN?p=7