前言:写于2019年1月2日:今天我有好好学习。希望以后做事情提前做,不要拖拉。
我所使用的代码和帮助文件来自这里:
https://github.com/mdbloice/Augmentor
一、安装python包
1、打开Anaconda Prompt
2、输入命令:pip install Augmentor
3、安装成功后,提示有新版本,此时输入命令:python -m pip install --upgrade pip
4、在python中测试是否安装成功:
输入 “import Augmentor”,果然发现没有这个包。
这时候我找到了这篇文章:
https://blog.csdn.net/sinat_23619409/article/details/79962518
我按照上图的办法,把当前Project下的解析器设置成Anaconda下的Python3.6。同时在这个Python3.6的设置界面发现了我刚才安装的Augmentor。
点击Augmentor,并点击右边的“+”。
但是这个时候import的时候还是报错。
我查了另一篇文章,说在pip的时候需要把pip的路径添加到系统的环境变量里去。
尝试解决办法1:
可能因为我是在Anaconda里安装的,我重新在Windows系统的cmd里安装试试看,然后把Pycharm的解析器也改成原始的Python3.7。
还是导入不进去。
尝试解决办法2:
把Anaconda里安装的包的路径加入系统的环境变量里去。
环境变量路径的写法:https://blog.csdn.net/myknotruby/article/details/78911452
我找到的环境变量路径为:D:\anaconda\Lib\site-packages\
手动加入到系统的环境变量的方法为:https://www.java.com/zh_CN/download/help/path.xml
尝试解决办法3:
用debug进行调试,看到底是哪一步出了问题。
这时候程序跳转到包的源代码内,发现源代码里这一步报错了:
报错原因是python3.7里没有这个叫做__builtin__的模块。
这个模块到底是什么呢?
经过查找发现,“因为在Python 3+中,__builtin__模块被命名为builtins”。
还是不行。
尝试解决办法4:
pip uninstall Augmentor
删掉重新安装,并且只安装最原始的版本,不进行更新。
参考文章:https://blog.csdn.net/blueheart20/article/details/80985132
还是不行
进展:直接在Anaconda Prompt下面,先输入python,再输入import Augmentor,没有报错,导入成功。
所以是Pycharm的问题!!!
所以赶紧换编辑器Spyder吧
解决办法:
(1)更换编辑器Spyder
(2)import Augmentor 这一行需要区分大小写!
二、阅读建议说明书并理解
完整的用户手册在这个网站:https://augmentor.readthedocs.io/en/master/
1、To begin, instantiate a Pipeline object that points to a directory on your file system:
初始化
import Augmentor
p=Augmentor.Pipeline("/path/to/images")
2、You can then add operations to the Pipeline object p as follows:
分配概率
p.rotate(probability=0.7,max_left_rotation=10,max_right_rotation=10)
p.zoom(probability=0.5,min_factor=1.1,max_factor=1.5)
Every function requires you to specify a probability, which is used to decide if an operation is applied to an image as it is passed through the augmentation pipeline.
3、Once you have created a pipeline, you can sample from it like so:
需要多少张照片
p.sample(10000)
4、Images can be passed through the pipeline in groups of two or more so that ground truth data can be identically augmented.
p=Augmentor.Pipeline("/path/to/images")
#Point to a directory containing ground truth data.
#Images with the same file names will be added as ground truth data
#and augmented in parallel to the original data.
p.ground_truth("/path/to/ground_truth_images")
#Add operations to the pipeline as normal:
p.rotate(probability=1,max_left_rotation=5,max_right_rotation=5)
p.flip_left_right(probability=0.5)
p.zoom_random(probability=0.5,percentage_area=0.8)
p.flip_top_bottom(probability=0.5)
p.sample(50)
5、Multiple Mask/Image Augmentation
这个我们用不到,我们只需要用到4就可以了。
三、开始写代码
1、直接复制上一段代码,希望先生成50个图,看图像的位置在哪里以及图长成什么呀。
2、但是一上来就报错了:在输入图片路径的那一行报错
“ (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape”
原因是文件夹路径不能直接复制粘贴,需要把“\”改成“/”
3、顺利运行,但是发现Console端显示:“0 ground truth image(s) found”
第一,去找图片在哪里生成:
Console端有提示:_2019/Week1/data_augmentation/original_image\output.
就是在Original_image里面的文件夹。
仔细去看果然有50个图,而且每个图都各不相同,但是大小和分辨率和类型都是和原图一样的。
4、需要解决的问题:为什么程序找不到groundtruth的图?
解决办法一:Groudtruth的文件夹有没有输入错误:
没有
检查的办法是把pipeline的文件夹改成Grountruth的文件夹。
解决办法二:移除Groundtruth文件夹里多余的内容。
因为groundtruth文件夹里还有多余的txt文件,我把它全部移除。
解决办法三:把groundtruth文件夹移动嗷originalimage 文件夹里面去。
这时候虽然能找到grounturth文件了,但是报错了“AttributeError: 'FileNotFoundError' object has no attribute 'message'”。
即使删掉output文件夹再跑一次还是会报错。
解决办法四:在注释行中找到原因了:
“#Point to a directory containing ground truth data.#Images with the same file names will be added as ground truth data#and augmented in parallel to the original data.”
grountruth与original_image的图像名字必须相同。
改图像名字中。。。
成功运行!。
新的original_image和groudtruth_image是保存在一起的。
50张跑成功了就去跑1000张,肯定也会成功的。
结束。output里有2000个项目。大概花了3分钟吧就跑完啦。
手动把他们分成原图和groundtruth图。
最后,检查原图和groundtruth图是否匹配。
打开第一个原图和第一个groundtruth图:非常匹配!
结束!
网友评论