load_image无法处理PNG/GIF
rock4you 发布于2019-10 浏览:1782 回复:0
0
收藏
最后编辑于2019-10

报错:AttributeError: 'NoneType' object has no attribute 'shape'

原因:

paddle.dataset.image.load_image(image)

无法处理PNG/GIF

 

代码如下:

my_train_list = "/home/aistudio/work/my_train_list.txt"
my_valid_list = "/home/aistudio/work/my_valid_list.txt"

文本内容:(文件名 tab 标签值)

/home/aistudio/data/data10954/cat_12_train/8GOkTtqw7E6IHZx4olYnhzvXLCiRsUfM.jpg	0
/home/aistudio/data/data10954/cat_12_train/hwQDH3VBabeFXISfjlWEmYicoyr6qK1p.jpg	0

 

1 #coding:utf-8
2 #自定义数据集
3 import paddle 
4 import paddle.fluid as fluid 
5 import numpy 
6 import sys 
7 from multiprocessing import cpu_count 
8
9 input_size_of_net = 224#神经网络定义的输入图片的大小
10 BATCH_SIZE=32
11
12
13 def train_mapper(sample): 
14    img, label = sample 
15    img = paddle.dataset.image.load_image(img) 
16    img = paddle.dataset.image.simple_transform(im=img,
17                                                resize_size = 256, 
18                                                crop_size =input_size_of_net, 
19                                                is_color = False, 
20                                                is_train = True)
    img = img.flatten().astype('float32')/255.0
    return img, label

def test_mapper(sample): 
    img,label = sample 
    img = paddle.dataset.image.load_image(img) 
    img = paddle.dataset.image.simple_transform(im=img, 
                                                resize_size=256,
                                                crop_size=input_size_of_net, 
                                                is_color = False, 
                                                is_train = False)
    img = img.flatten().astype('float32')/255.0 
    return img, label 

def train_r(train_list, buffered_size=256): 
    def reader(): 
        with open(train_list, 'r') as f: 
            lines = [line.strip() for line in f ]
            for line in lines: 
                img_path, label = line.strip().split('\t')
                yield img_path, int(label) 
    return paddle.reader.xmap_readers(train_mapper, reader,cpu_count(),buffered_size)  

def test_r(test_list, buffered_size = 256): 
    def reader(): 
        with open(test_list,'r') as f: 
            lines = [line.strip() for line in f]
            for line in lines: 
                img_path, label = line.strip().split('\t')
                yield img_path, int(label) 
    return paddle.reader.xmap_readers(test_mapper, reader, cpu_count(), buffered_size)


trainer_reader = train_r(train_list = my_train_list)
train_reader= paddle.batch(
                          paddle.reader.shuffle(
                              reader=trainer_reader,buf_size=300), 
                        batch_size=BATCH_SIZE)

tester_reader = test_r(test_list = my_valid_list)
test_reader = paddle.batch( tester_reader, batch_size=BATCH_SIZE)

temp_reader = paddle.batch(trainer_reader, batch_size=1) 
temp_data = next(temp_reader())
print(temp_data)
print("done.")

输出结果及报错信息:

[(array([0.08235294, 0.09019608, 0.08627451, ..., 0.02745098, 0.02352941,
       0.02745098], dtype=float32), 0)]
done.
Exception in thread Thread-8:
Traceback (most recent call last):
  File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/reader/decorator.py", line 303, in handle_worker
    r = mapper(sample)
  File "", line 20, in train_mapper
    is_train = True)
  File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/dataset/image.py", line 357, in simple_transform
    im = resize_short(im, resize_size)
  File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/dataset/image.py", line 215, in resize_short
    h, w = im.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
收藏
点赞
0
个赞
TOP
切换版块