Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CNN:当地图给出标签时该怎么办_Python_Tensorflow_Image Processing_Keras - Fatal编程技术网

Python CNN:当地图给出标签时该怎么办

Python CNN:当地图给出标签时该怎么办,python,tensorflow,image-processing,keras,Python,Tensorflow,Image Processing,Keras,在为图像分类问题拟合卷积神经网络时,为了使用以下函数 flow_from_directory() image_dataset_from_directory() Keras希望列车数据以这种方式存储: \data: \training \class_1 "imag1.jpg" "imag2.jpg" ... \class_2 "imag1.jpg&

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

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 ....}

是否有一种有效的方法来使用此数据集?

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

步骤1:打开json,并获得一个独特类别的列表

第2步:迭代唯一类别列表,并在training下创建一个文件夹

步骤3:迭代json,并将文件复制到正确的文件夹(您已经创建了该文件夹)

步骤4:从目录中使用图像\u数据集\u加载所有内容

另一种方法是从发电机使用


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

我个人会选择第一个^^ ^

你想这样使用这些文件,还是想从目录expect中将其转换为配置映像\u dataset\u?你可以使用dataframe@AlexandreCatalano,两种方法都很好我尝试了一些方法告诉我是否有用:)