0

Hello I trained a model and the images were loading with:

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')

The question is how can I evaluate the model with test_datagen?

I triyed the following but dosent' work:

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]}')

I get this error:

Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).

Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
  • I think you should convert the `numpy` _x_ to tensor before evaluate. Check this out [how to convert numpy array to keras tensor](https://stackoverflow.com/questions/52816938/how-to-convert-numpy-array-to-keras-tensor). – mirzanahal Sep 05 '20 at 21:11

1 Answers1

0

Finnaly I use:

score = model.evaluate_generator(test_datagen, steps=STEP_SIZE_VALID)
print(f'Test loss: {score[0]} / Test accuracy: {score[1]}')
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84