【问题标题】:Product Classification loading image error in python产品分类在python中加载图像错误
【发布时间】:2020-10-11 04:42:15
【问题描述】:

最近我在做一个产品分类项目,我有一个预先分类的数据集“train”,其中包含与每个产品类别对应的 41 个文件夹,其 csv 文件列出了图像名称及其类别。

然后,我有另一个包含一堆未分类产品的“测试”数据集,该项目希望对这些图片进行分类并输出一个带有“名称”和“类别”的 csv 文件

我在这个项目中使用 google colab,在我成功加载并挂载所有文件并准备扫描训练后的图像后,我收到了一个错误,下面是我的代码

train_image = []

for i in tqdm(range(train.shape[0])):

    img = image.load_img('content/train/train/'+train[i].astype('str')+'.jpg', target_size=(28,28,1))

    img = image.img_to_array(img)

    img = img/255

    train_image.append(img)

X = np.array(train_image)

这是我得到的错误:

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
2 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 0

这就是我的培训目录在 google colab 中的样子

--content
  ↳train  
    ↳train  
      ↳0  
      ↳1  
      ....  
      ↳41  

如何消除该错误?

【问题讨论】:

  • 欢迎来到 SO;请注意,外部资源(教程等)的建议是 strictly off-topic 此处(已编辑问题以将其删除)。
  • @desertnaut 哦,好的,谢谢你的提醒,我真的是新来的

标签: python pandas machine-learning image-processing google-colaboratory


【解决方案1】:

好的,我知道了,是 load_image 的格式问题

img = image.load_img(r'train/train/' + str(train_df['category'][i]) + '/' + train_df['filename'][i], target_size=(28,28))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2018-12-25
    相关资源
    最近更新 更多