pytorch 若干小坑

来源:互联网 发布:贵阳大数据是什么 编辑:程序博客网 时间:2024/05/01 02:47

caffe灵活性还是比不上torch,想要快速建立新模型,torch是个不错的选择
#

batch size 设置略大则段错误

自己练手写的lenet和官方的lennet[https://github.com/pytorch/examples/blob/master/mnist/main.py]在CPU中运行时,设置稍微大一点的batch size(8以上),就会出现段错误,而且也没错误栈,所以无法定位,不是py层的问题

解决方法:
1.不要超过4(废话)
2.在GPU上跑(又一句废话)
不过目前就是这样,好像没别的办法了

ImageFolder

在使用 ImageFolder时,如果原始图片为单通道图像,该函数可能会把图像转换为3通道,查看torchvision/datasets/folder.py源码,有如下三个函数

def pil_loader(path):    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)    with open(path, 'rb') as f:        with Image.open(f) as img:            return img.convert('RGB')def accimage_loader(path):    import accimage    try:        return accimage.Image(path)    except IOError:        # Potentially a decoding problem, fall back to PIL.Image        return pil_loader(path)def default_loader(path):    from torchvision import get_image_backend    if get_image_backend() == 'accimage':        return accimage_loader(path)    else:        return pil_loader(path)

可知是default_loader调用accimage_loader失败,转而调用pil_loader时默认把所有图像转化为RGB了。那我们只要写一个自己的load函数给ImageFolder即可:

from PIL import Imagedef  readImg(path)    return Image.open(path)#...xx.ImageFolder(......,load=readImg)#设定自己的图片加载函数

——————————————————-

持续更新中。。。

原创粉丝点击