【问题标题】:CNN: What to do when labels are given by a mapCNN:当标签由地图给出时该怎么办
【发布时间】:2021-02-21 07:05:01
【问题描述】:

在为图像分类问题拟合卷积神经网络时,为了使用类似的函数

flow_from_directory()
image_dataset_from_directory()

Keras 期望火车数据以这种方式存储:

\data:
   \training
     \class_1
         "imag1.jpg"
         "imag2.jpg"
          ...
     \class_2
         "imag1.jpg"
         "imag2.jpg"
          ...
     ....
 

相反,我有一个数据集,其中所有图像都存储在一个文件夹中,还有一个 .json 文件,其中包含从文件名到标签的映射。类似的东西

{"18985.jpg": 0, "43358.jpg": 0, ... "13163.jpg": 1 ....}

有没有一种有效的方法来使用这个数据集?

【问题讨论】:

标签: python tensorflow image-processing keras


【解决方案1】:

我建议的解决方案是编写一个脚本来为您构建文件夹

第 1 步:打开 json,并获取唯一类别的列表

第 2 步:遍历唯一类别列表并在训练中创建一个文件夹

第 3 步:遍历 json,并将文件复制到正确的文件夹(您已经创建)

第 4 步:使用 image_dataset_from_directory 加载所有内容

另一个是使用 from_generator


import json 
  
# Opening JSON file 
f = open('data.json',) 
  
# returns JSON object as  
# a dictionary 
data = json.load(f) 

def gen():
for (image_path, label) in data.items():
   image = tf.keras.preprocessing.image.load_img(image_path)
   input_arr = keras.preprocessing.image.img_to_array(image)
   yield (input_arr, label)

dataset = tf.data.Dataset.from_generator(
     gen,
     (tf.float32, tf.float32),
     output_shapes=([32,256,256,3], [32,5]) # 5 is your number of categories

我个人会选择第一个^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2023-04-08
    相关资源
    最近更新 更多