Trained model on cifar10 performs poorly on real images

38 Views Asked by At

So I'm trying to train a model using the CIFAR10 dataset.

The problem is that while the performance of the model on validation and test sets are good (about 95-96%), the model fails to predict images downloaded from the internet (preprocessed like with the inputs). I know that there are a lot of similar questions already, as a matter of fact, I’ve already tried implementing those suggestions but none works for me so far. I don’t know what I did wrong.

Here's my approach:

  • Data: test – 80%, validation – 10% (input of val_dataset in model.fit), test – 10% (input of model.evaluate)
  • Model (transfer learning): I use ResNet50 as the base model, use “imagenet” weights and freeze them.
    UpSampling2D((7,7)) to resize images from 32x32x3 to 224x224x3 --> ResNet50 --> Flatten --> Dense (relu) --> Dropout --> Dense(10, softmax).

Is it because the resolutions of images from this dataset is too low, while the resolutions of images downloaded from the internet are much higher? I don’t know how to describe this, but even after resizing the downloaded images to 32x32x3 to feed in model.predict, they still look kinda different from the images in the dataset?

An image from the dataset:
enter image description here

Downloaded image (original):
enter image description here

Downloaded image (resized to 32x32x3):
enter image description here

Is that the issue? If not, can you tell me what I did wrong, or what I could do so that my model can perform well on real life images?

Many thanks.

Update: How I downsample the downloaded images (after suggestions):

#loading image
dir = "image.jpg"
im_array2 = np.array(Image.open(dir))

IMG_SIZE = 32

#calculate the dimensions to rescale while still keeping aspect ratio
r = IMG_SIZE / im_array2.shape[0]
dim = (IMG_SIZE, int(im_array2.shape[1] * r))

test_image = np.floor(tf.image.resize(im_array2, dim)).astype(int)

#crop the image to size 32x32
test_image = tf.image.resize_with_crop_or_pad(test_image, IMG_SIZE, IMG_SIZE)

#convert to float32 because that is the input type that I set, expand_dims because the input takes image in batches
im = np.float32(test_image)
im_array2 = np.expand_dims(im, axis=0)

#Normalize the image array to fit the input setting
im_array2 = im_array2/255

New result:
enter image description here.

But the performance still isn't improved.

0

There are 0 best solutions below