为什么从flow_from_directory tensorflow得到一个空序列?



我尝试为我的CNN加载测试图像。我得到了测试图像的路径,如果我打印出它的长度,我看到我成功地得到了图像。问题是,如果我想从test_data_gen得到图像,我得到一个空序列。这是文件夹的结构。

下面是我的代码:
# This command only in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import numpy as np
import matplotlib.pyplot as plt
# Get project files
!wget https://cdn.freecodecamp.org/project-data/cats-and-dogs/cats_and_dogs.zip
!unzip cats_and_dogs.zip
PATH = 'cats_and_dogs'
train_dir = os.path.join(PATH, 'train')
validation_dir = os.path.join(PATH, 'validation')
test_dir = os.path.join(PATH, 'test')
# Get number of files in each directory. The train and validation directories
# each have the subdirecories "dogs" and "cats".
total_train = sum([len(files) for r, d, files in os.walk(train_dir)])
total_val = sum([len(files) for r, d, files in os.walk(validation_dir)])
total_test = len(os.listdir(test_dir))
# Variables for pre-processing and training.
batch_size = 128
epochs = 15
IMG_HEIGHT = 150
IMG_WIDTH = 150
test_image_generator = ImageDataGenerator(rescale=1./255)
e, subset=None, interpolation="nearest")
test_data_gen = test_image_generator.flow_from_directory(test_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH), 
color_mode='rgb', 
classes=None, 
class_mode='categorical', 
batch_size=128, 
shuffle='False', 
seed=None, 
save_to_dir=None, 
save_prefix='', 
save_format='png', 
follow_links=False, 
subset=None, 
interpolation="nearest")

在您的test_data_gen中,您需要将class_mode从categorical设置为None以仅返回图像,因为测试目录仅包含图像而不是类明智的文件夹结构。下面是修改后的代码:

test_image_generator = ImageDataGenerator(rescale=1./255)
e, subset=None, interpolation="nearest")
test_data_gen = test_image_generator.flow_from_directory(test_dir,
target_size=(IMG_HEIGHT, IMG_WIDTH), 
color_mode='rgb', 
classes=None, 
class_mode=None, 
batch_size=128, 
shuffle='False', 
seed=None, 
save_to_dir=None, 
save_prefix='', 
save_format='png', 
follow_links=False, 
subset=None, 
interpolation="nearest")

相关内容

  • 没有找到相关文章

最新更新