如何评估使用 flow_from_directory 加载数据的模型?

您好,我训练了一个模型,并且加载了图像:


batch_size = 16


# Data augmentation and preprocess

train_datagen = ImageDataGenerator(rescale=1./255,

    shear_range=0.2,

    zoom_range=0.2,

    horizontal_flip=True,

    validation_split=0.20) # set validation split


# Train dataset

train_generator = train_datagen.flow_from_directory(

    'PetImages/train',

    target_size=(244, 244),

    batch_size=batch_size,

    class_mode='binary',

    subset='training') # set as training data


# Validation dataset

validation_generator = train_datagen.flow_from_directory(

    'PetImages/train',

    target_size=(244, 244),

    batch_size=batch_size,

    class_mode='binary',

    subset='validation') # set as validation data


test_datagen = ImageDataGenerator(rescale=1./255)

# Test dataset

test_datagen = test_datagen.flow_from_directory(

    'PetImages/test')

问题是如何使用 test_datagen 评估模型?


我尝试了以下但没有工作:


x=[]

y=[]

test_datagen.reset()

for i in range(test_datagen.__len__()):

    a,b=test_datagen.next()

    x.append(a)

    y.append(b)

x=np.array(x)

y=np.array(y)

print(x.shape)

print(y.shape)


score = model.evaluate(x, y)

print(f'Test loss: {score[0]} / Test accuracy: {score[1]}')

我收到此错误:


无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。


叮当猫咪
浏览 78回答 1
1回答

胡子哥哥

最后我用:score = model.evaluate_generator(test_datagen, steps=STEP_SIZE_VALID)print(f'Test loss: {score[0]} / Test accuracy: {score[1]}')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python