pytorch学习笔记2

首先如果遇到conda找不到包,pip老是超时的情况建议添加一下镜像源

conda的

python 复制代码
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# reference
# https://mirror.tuna.tsinghua.edu.cn/help/anaconda/

cuda最好还是像下面链接里的用10.0

https://blog.csdn.net/zzq060143/article/details/88042075

安装pytorch的时候,主要是把官网指令后面的-c pytorch删掉,-c pytorch的意思是,安装下载的channel强制为pytorch官网的channel。所以需要删除才能走清华镜像的channel。

pip的话用这个命令

python 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

添加清华镜像

写python可以使用这个python console,是ipython内核,可以交互式运行Python语句(jupyter也是这个内核)

写代码时

上边部分写代码

下面部分进行实时调试

一个简单的Dataset类

python 复制代码
import os
from PIL import Image
from torch.utils.data import Dataset

class MyDateSet(Dataset):
    def __init__(self,root_dir,lable_dir):#提供全局变量,root_dir通常为train文件的地址,lable_dir提供标签
        self.root_dir=root_dir
        self.lable_dir=lable_dir
        self.path=os.path.join(root_dir,lable_dir)
        self.img_path=os.listdir(self.path)


    def __getitem__(self, idx):
        img_name=self.img_path[idx]
        img_item_name=os.path.join(self.root_dir,self.lable_dir,img_name)
        img=Image.open(img_item_name)
        lable=self.lable_dir
        return img,lable

    def __len__(self):
        return len(self.img_path)

root_dir=r'hymenoptera_data/train'
ants_lable_dir=r'ants'
bees_lable_dir=r'bees'
ants_dataset=MyDateSet(root_dir,ants_lable_dir)
bees_dataset=MyDateSet(root_dir,bees_lable_dir)

train_dataset=ants_dataset+bees_dataset#对两个数据集进行拼接
相关推荐
宵时待雨5 分钟前
数据结构(初阶)笔记归纳2:顺序表的实现
c语言·数据结构·笔记·算法
木木木一5 分钟前
Rust学习记录--C10 泛型,Trait,生命周期
python·学习·rust
Moonquakes54011 分钟前
嵌入式基础学习笔记(51)
笔记·单片机·学习
musenh16 分钟前
spring学习1
java·学习·spring
棒棒的皮皮24 分钟前
【深度学习】YOLO 模型核心部署格式全解析(PyTorch/ONNX/TensorRT/TFLite)
pytorch·深度学习·yolo·计算机视觉
Engineer邓祥浩28 分钟前
设计模式学习(12) 23-10 外观模式
学习·设计模式·外观模式
专注于大数据技术栈31 分钟前
java学习--Vector
java·学习
宵时待雨35 分钟前
STM32笔记归纳1:STM32的基本信息与引脚分布
笔记·stm32·嵌入式硬件
_叶小格_43 分钟前
ansible自动化入门基础
运维·笔记·学习·自动化·ansible
im_AMBER1 小时前
前端 + agent 开发学习路线
前端·学习·agent