当前位置:网站首页>解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(二)
解决方案:炼丹师养成计划 Pytorch+DeepLearning遇见的各种报错与踩坑避坑记录(二)
2022-07-26 22:46:00 【中杯可乐多加冰】
目录
- 问题1:pytorch下,对多维tensor进行缩放
- 问题2:'PIL.Image' has no attribute '****'
- 问题3:ImportError: cannot import name 'imread'
- 问题4:models/bird/netG.pth is a zip archive (did you mean to use torch.jit.load()?)
- 问题5:模型的保存与加载
- 问题6:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 226: illegal multibyte sequence
问题1:pytorch下,对多维tensor进行缩放
问题原因:有的时候想要把得到图像的尺寸过大过小,想要对生成的tensor张量进行缩放,同时要保持原有图像等比例。如现在有一个tensor,他的大小是:[64,3,256,256],64代表batch size,3表示通道数,256代表长和宽,想要将其缩小为32*32的尺寸。
解决方案:我们可以使用transform.resize(),但在试过后,发现使用torch.nn.functional.interpolate会更好用
首先说明其参数:torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None)
- input是我们的输入张量(Tensor)
- size是输出大小
- scale_factor (float or Tuple[float]) 指定输出为输入的多少倍数
- mode是上采样算法的选择:有’nearest’, ‘linear’, ‘bilinear’, ‘bicubic’ , ‘trilinear’和’area’,其中默认是’nearest’
- align_corners (bool, optional) 如果设置为True,则输入和输出张量由其角像素的中心点对齐,从而保留角像素处的值。如果设置为False,则输入和输出张量由它们的角像素的角点对齐,插值使用边界外值的边值填充
使用样例:
print(train_mask.size())
# train_mask尺寸本来是[64,3,256,256]
train_mask = torch.nn.functional.interpolate(train_mask, scale_factor=1 / 4, mode='bilinear',align_corners=False)
#变换之后,train_mask尺寸变为[64,3,64,64]
print(train_mask.size())
问题2:‘PIL.Image’ has no attribute ‘****’
问题原因:这个是因为系统已经安装的PIL太新了,有些属性在新版中已经被弃用
解决方案:改回原来的版本就可以:pip install pillow==4.1.1,安装老版本pillow,最后会告诉你这个版本已经过时了,不用管。
问题3:ImportError: cannot import name ‘imread’
问题原因:“scipy”库的版本过高
解决方案:改回原来的版本:pip install scipy==1.2.1
问题4:models/bird/netG.pth is a zip archive (did you mean to use torch.jit.load()?)
问题原因:原来训练后保存参数时所在的pytorch环境和现在加载参数的pytorch环境可能不同,版本不一致,导致当时保存的参数现在读不出来。
解决方案:先在1.x版本下加载模型,然后在保存为非zip格式的,即设置use_new_zipfile_serialization=False 就行了。
#torch_version==1.x
import torch
from models import net
checkpoint = 'xxx.pth'
model = net()
model.load_state_dict(torch.load(checkpoint))
model.eval()
torch.save(model.state_dict(), model_path, use_new_zipfile_serialization=False)
问题5:模型的保存与加载
问题描述:在实际运行当中,要注意每100轮epoch或者每50轮epoch要保存训练好的参数,以防不测(断电、断连、硬件故障、地震火灾等),这样下次可以直接加载该轮epoch的参数接着训练,就不用重头开始。
解决方案:
参数的保存:
torch.save(model.state_dict(), path)
参数的加载
model.load_state_dict(torch.load(path))
问题6:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 226: illegal multibyte sequence
问题原因:某个文件是gbk编码,其无法被解码。
解决方案:看看是不是自己在yml文件或者其他文件中写了中文(如在下图中 不小心在注释写了中文,导致解码失败),删除就好了
边栏推荐
- FTP service
- 为啥不建议使用Select *
- Application of load balancing
- Machine learning exercise 7 - K-means and PCA (principal component analysis)
- ERROR! MySQL is not running, but PID file exists
- Domain name analysis and DNS configuration installation
- Which securities company is better or safer for retail investors to open accounts
- MySQL存储过程函数
- Text three swordsman two
- Acwing 1057. stock trading IV
猜你喜欢

MySQL多表查询

Expect interaction free

R分数复现 R-precision评估指标定量 文本生成图像R分数定量实验全流程复现(R-precision)定量评价实验踩坑避坑流程

索引失效原理讲解及其常见情况

Codeforce problem 908 D. new year and arbitrary arrangement (probability DP)

25pxe efficient batch network installation

Virtualization technology KVM

Domain name analysis and DNS configuration installation

MySQL存储过程函数

RT-thread学习
随机推荐
使用ECS和OSS搭建个人网盘
How can smart people leave without offending others?
Atcoder D - increment of coins (probability DP)
Shell (13) Three Musketeers
RT-thread学习
Project | implement a high concurrency memory pool
虚拟化技术KVM
HarmonyOS图像处理应用开发实战直播笔记
索引失效原理讲解及其常见情况
Redis uses queues for consumption
PHP processing tree and infinite processing
iptables
Shell (10) array and bubble sort
MySQL index
MySQL installation
继承的详细介绍与理解,看了就懂
MySQL view
Regular expression gadget series
【CANN训练营】走进媒体数据处理(下)
ceph(分布式存储)