CNN can't predict images outside the dataset

I am using celeba dataset to train my CNN face landmark detection model. Here is my model

class LandmarkModel:
    def __init__(self,inp_shape):
        self.model = models.Sequential()
        self.model.add(layers.Conv2D(16, (3, 3), activation='relu', input_shape=inp_shape))#l1
        self.model.add(layers.Conv2D(32,(3, 3), activation='relu'))
        self.model.add(layers.MaxPooling2D((2, 2)))
        self.model.add(layers.Conv2D(64,(3, 3), activation='relu'))
        self.model.add(layers.Flatten())
        self.model.add(layers.Dense(512))
        self.model.add(layers.Dense(10))

    def getModel(self):
        return self.model

I have trained my model for around 5k-6k images with loss of 0.1. When I use image from dataset that is outside of training sample I get correct prediction. But when I use my own clicked images predictions are completely off. I have clicked photos exactly like in dataset. I have also tried with downloaded celeb images still wrong predictions. What is the reason of this behavior?

Topic cnn convolutional-neural-network computer-vision deep-learning

Category Data Science


That could a lot of reasons for that. For example, images are sometimes represented with numbers in range 0-1 and sometimes in range 0-255 and it's very easy to mix these ranges for in-dataset / external as the model would fail silently without any warnings.

In general, if the same model gives you different results, then images are not exactly the same. I suggest you take the same image from your dataset and internet and compare the raw numbers on the input of the model. If results different, then input tensors should be different and you can check your preprocessing pipeline step by step to find the point of discrepancy.

The debugging tools like ipdb are usually very useful in such situations.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.