1、解决pycharm配置pytorch问题:import _ssl # if we can‘t import it, let the error propagateImportError: DLL load faile_import _ssl # if we can't import it, let the error-优快云博客
2、两个函数
dir(torch):显示torch下所有“分隔区”
help(torch.cuda.is_available):显示该函数的用法
一、加载数据
获取数据及其label并编号
Dataset
打包数据:为后面的网络提供不同的数据形式
Dataloader
二、代码
(1)获取数据
from torch.utils.data import Dataset
from PIL import Image #读取图片
import os #用来获得所有图片的地址
class MyData(Dataset): #定义一个从Dataset继承的类
def __init__(self, root_dir, label_dir): #初始化类 self指定了该类MyData中的全局变量 root_dir为dataset/train label_dir为ants
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, idx): #idx是索引
img_name = self.img_path[idx]
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 = "dataset/train"
ants_label_dir = "ants"
bees_label_dir = "bees"
ants_dataset = MyData(root_dir, ants_label_dir)
bees_dataset = MyData(root_dir, bees_label_dir)
train_dataset = ants_dataset + bees_dataset #合并数据集
(2)根据图片名生成对应label的txt文件,并写入label
import os
root_dir = "dataset/train"
target_dir = "ants_image"
img_path = os.listdir(os.path.join(root_dir, target_dir))
label = target_dir.split('_')[0] #分为ants和image,取ants
out_dir = "ants_label"
for i in img_path:
file_name = i.split('.jpg')[0]
with open(os.path.join(root_dir, out_dir, "{}.txt".format(file_name)), 'w') as f:
f.write(label)