Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 ImageDataGenerator流来自带有孙子文件夹的\u目录_Python_Tensorflow_Machine Learning_Keras_Dataset - Fatal编程技术网

Python ImageDataGenerator流来自带有孙子文件夹的\u目录

Python ImageDataGenerator流来自带有孙子文件夹的\u目录,python,tensorflow,machine-learning,keras,dataset,Python,Tensorflow,Machine Learning,Keras,Dataset,我有一个k-fold train数据集,但其结构有一个孙子文件夹,用于: /monkey / howler monkey - img1 - img2 / japanese macaque - img1 - img2 /dog / bulldog - img1 - img2 / Rottweiler - img1 -

我有一个k-fold train数据集,但其结构有一个孙子文件夹,用于:

/monkey
     / howler monkey
         - img1
         - img2
     / japanese macaque
         - img1
         - img2
/dog
     / bulldog
         - img1
         - img2
     / Rottweiler
         - img1
         - img2

在这种情况下,当我使用
ImageDataGenerator
flow\u from\u directory
时。找到了8个img,但该类有2个img,而不是4个img。我怎样才能得到4门课呢?

这个问题我已经问了很长时间了,我无法用
.flow\u从\u目录
直接找到答案。我所做的是使用数据帧中的
.flow\u。首先,我刚刚创建了一个带有图像路径及其相应标签的数据框(在您的例子中是howler monkey、japanese macque等)。制作此数据帧时,您不会在任何点实际加载图像

事情会是这样的:

images_paths_label = []
for root_class in os.listdir(root_folder):
   temp_class = os.path.join(root_folder, root_class)
   for class in os.listdir(temp_class):
       temp_subclass = os.path.join(temp_class, class)
       for image in os.listdir(temp_subclass):
           temp_img_path = os.path.join(temp_subclass, image)
           images_paths_label.append([temp_img_path, class])

df = pd.DataFrame(images_paths_label, columns = ['image_path', 'label'])

# Now the flow_from_dataframe part
generator = ImageDataGenerator(validation_split = 0.2)
train_generator = generator.flow_from_directory(df, directory = None, x_col = 'image_path', y_col = 'label', seed = 14,...)
  • 在使用代码段之前,请检查其缩进。我只是在这里输入stackoverflow
您给出的是
directory=None
,因为您在数据帧的
image\u path
列中放置了一个绝对路径。指定种子是因为默认情况下
shuffle=True
,它应该是True,因为您的数据帧样本是按类排序的。在这里设置种子,可以确保验证数据保持不变

这将使您全面了解如何在仍然使用生成器的情况下克服此问题。如果发现任何问题,请告诉我