I want to train a model to recognize different category of food (example: rice, burger, apple, pizza, orange,... )

After the first training, I realized that the model is detecting other object as food. (example: hand -> fish, phone -> Chocolate, person -> candies... )

I get a very low loss because the testing dataset and validation must have at least a pictures of food. But when it comes to picture of object other than food, the model fails. How do label the dataset in way that the model will not do any detection if there is no food on the picture?


1 Answer 1


From how you've phrased your question I'm going to assume you've jumped in without much structured training in data science, so I'll answer at a fairly high level.

This is an inherent problem with image classification in that if your final layer only has food classes then whatever you feed in will always be classified as a type of food regardless of what is actually in the image.

There are a few techniques you could try. The simplest and fastest being using a pre-built classifier to screen the input data for the presence of "food" and then only using your own classifier to determine what type of food it is. Any of the open source imageNet networks would be a good step here - if that finds something with a food label then use your classifier to identify the category. This means you won't need to retrain although you'll be at the mercy of any errors in the pre-trained category classifier, which is outside of your control. Here's a good how to guide: https://www.learnopencv.com/keras-tutorial-using-pre-trained-imagenet-models/

Another option is to add a negative class to your data set consisting of non-food items that you label "other". This is probably trickier as you'd need to cover all the non-food categories that your network will see and ensure that it doesn't learn the background.

Take a look at your final level and make a call about whether to accept the top result or not. You're forcing a choice and these values may give a clue as to the level of error in the prediction.

  • $\begingroup$ thanks for your reply. Could you please explain why should I ensure that "it doesn't learn the background" ? $\endgroup$
    – TSR
    Jul 31, 2018 at 14:03
  • $\begingroup$ You want your network to learn about the objects and not the background. If your "other" set has something common in the background then your may find that your other class picks up on that instead, even if there is food in the image. For a very trivial example, lets say that all your food classes were on white plates, and all your "other" class had darker backgrounds, your network might learn that dark equalled "other" and white meant food so you would get food on dark plates falsely labelled as other and potentially images that had pale discs being classified as food. $\endgroup$
    – Yssybyl
    Jul 31, 2018 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .