赞
踩
手把手教你在win10下搭建pytorch GPU环境(Anaconda+Pycharm) - 老潇的摸鱼日记 - 博客园 (cnblogs.com)https://www.cnblogs.com/victorxiao/p/13512258.html【扫盲】PyTorch环境配置_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1m4411m7uZ?spm_id_from=333.999.0.0
cuda安装成功:
接下来把cudnn的bin、include、lib 这三个文件夹直接复制到cuda里面的cudnn文件夹中:
接下来配置环境变量:
Anaconda prompt下安装:
创建虚拟环境:
conda create --name pytorch_gpu python=3.7
激活虚拟环境pytorch_gpu:
conda activate pytorch_gpu
添加清华镜像源:
i 编辑 Esc :wq退出并保存
- channels:
- - defaults
- show_channel_urls: true
- default_channels:
- - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- custom_channels:
- conda-forge: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- msys2: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- bioconda: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- menpo: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- pytorch: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
- simpleitk: http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
我的
cudatookit = 10.2 (windows 装的是11.3)
- conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- 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/r
-
- conda config --set show_channel_urls yes
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
NVIDIA RTX A5000对应cuda11.0
- # CUDA 11.0
- conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=11.0 -c pytorch
问题:cuda版本太旧,显卡太新,cuda得11以上,此处选择11.3
pip 安装cuda11.3版本pytorch的命令
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
如果提示:
则安装10.2版本 cudatookit=10.2
结果如下:
可能遇到:
可能遇到cuda版本和pillow版本不一致的问题,将pillow从9.0.1更新到9.1.0就好了:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade pillow
超出内存,修改如下:
数据集:
修改train.py中的num_workers=0
如果cuda显存不足:减小批处理大小
训练的过程中他会生成一个记录每一次epoch的结果(results20220402),包括有动态调优的学习率,会发现,损失基本上是越来越小的,IOU是越来越大的,代表预测的图像和本身标注的掩码图的相似度是越来越高的,最后会记录一个最佳的模型参数,作为最终的结果去预测。
接下来做预测,右图为测试图像的标注好的图像,左图为预测后的图像,大的血管还是能够得到保留,一些细小的血管学习效率不好,网络性能有待提升。
数据集:
选择好Prompt终端的虚拟环境:
注意修改以下:
将预测批量输出到output_predict文件夹下:
- import os
- import json
-
- import torch
- from PIL import Image
- from torchvision import transforms
- import matplotlib.pyplot as plt
-
- from vit_model import vit_base_patch16_224_in21k as create_model
-
- def readimg():
- path_predict = 'F:\\python\\visiontr\\test_image'
- path_predict_list = []
- for i in os.listdir(path_predict):
- path_img = os.path.join(i)
- # path_img = os.path.join(path_predict,i)
- path_predict_list.append(path_img)
- return path_predict_list
- # abc = readimg()
- # print(abc)
-
- def main(img_name):
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
-
- data_transform = transforms.Compose(
- [transforms.Resize(256),
- transforms.CenterCrop(224),
- transforms.ToTensor(),
- transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
-
- # load image
- img_path = 'F:\\python\\visiontr\\test_image\\' + img_name
- # print(img_path)
- assert os.path.exists(img_path), "file: '{}' dose not exist.".format(img_path)
- img = Image.open(img_path)
- plt.imshow(img)
- # [N, C, H, W]
- img = data_transform(img)
- # expand batch dimension
- img = torch.unsqueeze(img, dim=0)
-
- # read class_indict
-
- json_path = 'F:\\python\\visiontr\\class_indices.json'
- # json_path = './class_indices.json'
- assert os.path.exists(json_path), "file: '{}' dose not exist.".format(json_path)
-
- json_file = open(json_path, "r")
- class_indict = json.load(json_file)
-
- # create model
- model = create_model(num_classes=3, has_logits=False).to(device)
- # load model weights
- model_weight_path = "F:\\python\\visiontr\\weights\\model-9.pth"
- # model_weight_path = "./weights/model-9.pth"
- model.load_state_dict(torch.load(model_weight_path, map_location=device))
- model.eval()
- with torch.no_grad():
- # predict class
- output = torch.squeeze(model(img.to(device))).cpu()
- predict = torch.softmax(output, dim=0)
- predict_cla = torch.argmax(predict).numpy()
-
- print_res = "class: {} prob: {:.3}".format(class_indict[str(predict_cla)],
- predict[predict_cla].numpy())
- plt.title(print_res)
- for i in range(len(predict)):
- print("class: {:10} prob: {:.3}".format(class_indict[str(i)],
- predict[i].numpy()))
- output_path = r'./output_predict'
- if not os.path.exists(output_path):
- os.mkdir(output_path)
- plt.savefig('./output_predict/' + img_name)
- # plt.show()
-
-
- if __name__ == '__main__':
- abc = readimg()
- for i in range(len(abc)):
- main(abc[i])
结果如下:
数据集:
训练:
修改生成的权重:
如果显卡算力太低且pytorch版本过高则:
换用cpu进行训练:
改为
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。