1. TypeError: object of type <class ‘numpy.float64’> cannot be safelyinterpreted as an integer
解决方法:
将numpy版本从1.18降至1.16,使用pip install numpy==1.16。参考链接
2. IndexError: list index out of range
改了很多都不对,一直想着是dataloader的问题,所以一直在看代码,网上关于这个代码很少,根据一位老哥的建议将dataloader(drop_last = True),还是报错。
在分析了数据集之后发现,在把dataset从本地导入到colab中之后,有一张图片莫名多上传了一张,所以整个index错误。
3. TypeError: ‘>’ not supported between instances of ‘builtin_function_or_method’ and ‘float’
源码: if random.random > 0.5:
修改为 if random.random() > 0.5:
4. 关于对PIL图片做rgb2hed之后的类型问题
img = PIL.Image.open('path/img.png')
img = rgb2hed(img) # 做了颜色反卷积之后的img会变成ndarray类型,在接下来要把它转换回PIL
# 强制类型转换,虽然也不知道为什么,但是如果不这样做就直接进行下一步的话会出现报错
img = img.astype(numpy.uint8)
img = torchvision.transforms.ToPILImage()(img)
5.RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 ‘target’
6. RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size([1, 1, 1000, 1000])
这个原因是因为在使用Crossentropyloss作为损失函数时,output=net(input)的output应该是[batchsize, n_class, height, weight],而label则是[batchsize, height, weight],label是单通道灰度图;BCELoss与CrossEntropyLoss都是用于分类问题。BCELoss是CrossEntropyLoss的一个特例,只用于二分类问题,而CrossEntropyLoss可以用于二分类,也可以用于多分类。
这里不能用torch.sequeeze(label),这样操作之后label.shape会变成([1000, 1000]),而应该使用label.reshape((1, 1000, 1000))
loss = self.losser(pred, mask.reshape((1,1000,1000)).long())
7. TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor
numpy不能读取CUDA tensor 需要将它转化为 CPU tensor
将predict.data.numpy()
改为predict.data.cpu().numpy()
即可
在这里贴上源代码:
def save_visialize(self, idx, img, mask, pred):
tensor2pil = transforms.ToPILImage()
img = img.reshape((-1, self.crop_scale, self.crop_scale)).cpu() #
mask = mask.reshape((-1, self.crop_scale, self.crop_scale)).cpu() #
pred = pred.reshape((-1, self.crop_scale, self.crop_scale)).cpu() #
img, mask, pred = tensor2pil(img).convert('RGB'), tensor2pil(mask), tensor2pil(pred)
img.save(os.path.join(self.visialize_path, idx + '_img.png'))
mask.save(os.path.join(self.visialize_path, idx + '_mask.png'))
pred.save(os.path.join(self.visialize_path, idx + '_pred.png'))
报错的原因在于使用transforms.ToPILImage()的时候需要把 tensor->numpy->PIL,所以如果不把图片做cpu()处理会报错。
8.TypeError: can’t pickle generator objects
源代码
def save_model(self, epoch, is_best = False):
state = {
'model': self.train_net.parameters()
'epoch': epoch,
'optimizer': self.optimizer,
'loss': self.best_loss
}
if is_best:
torch.save(state, os.path.join(self.save_path, 'best_checkpoint.pth'))
torch.save(state, os.path.join(self.save_path, 'checkpoint.pth'))
报错,查博客说是dict的问题,改进之后的代码:
def save_model(self, epoch, is_best = False):
state = {
'model': self.train_net.state_dict(), ######
'epoch': epoch,
'optimizer': self.optimizer,
'loss': self.best_loss
}
if is_best:
torch.save(state, os.path.join(self.save_path, 'best_checkpoint.pth'))
torch.save(state, os.path.join(self.save_path, 'checkpoint.pth'))
###在win10 cmd里安装遇到的一些问题
1.
ERROR: Could not install packages due to an EnvironmentError: [WinError 5] 拒绝访问。: ‘D:\anaconda\anaconda\Lib\site-packages\cv2\cv2.cp37-win_amd64.pyd’
解决方法:后面加上 --user
2.
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with ‘–trusted-host pypi.douban.com’.
ERROR: Could not find a version that satisfies the requirement cutecharts (from versions: none)
ERROR: No matching distribution found for cutecharts
解决方法: 后面加上 --trusted-host pypi.douban.com
3. pip临时使用国内镜像源
pip install xxx -i “http://…”
其中,比较常用的国内镜像包括:
(1)阿里云 http://mirrors.aliyun.com/pypi/simple/
(2)豆瓣http://pypi.douban.com/simple/
(3)清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
(4)中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
(5)华中科技大学http://pypi.hustunique.com/
--------------------------------
### MacOS命令行记录
1. python -m http.server 搭建一个简易web服务器
需要在电脑b上访问服务器a中的文件:在a上运行命令python -m http.server, 然后在b的浏览器中输入a的ip地址(比方说xxx.xx.xx.xx:8000)就可以访问a上的文件
2. scp传输文件
scp -i /Users//id_rsa.rsa file root@xxx.xx.xx.xx:/root/
scp -i /Users//id_rsa.rsa -r dirs root@xxx.xx.xx.xx:/root/
3. gcc版本转换
scl enable devtoolset-7 bash
可以写一个sh脚本,里面是需要每次开环境转换的东西,然后bash 脚本就好