How To Detect Meat Adulteration Using Machine Learning

A beginner’s guide on building a simple meat adulteration detector using machine learning and image processing

Anjana M P
Bloggers Bay
4 min readAug 6, 2020

--

When it comes to the consumption of red meat, a lot of questions pop up in our minds. Does the consumption of red meat increase the risk of a heart attack? Is it good for the skin? Despite all these questions, the consumption of red meat among human beings has never been reduced and the only reason for that is the obvious taste of the meat.

Crispy mutton roast

But after the horsemeat scandal in Europe and the dog meat scandal in Vijayawada, India, we all have this doubt whether we are eating the right meat.

For decades now, the problem of red meat substitution has been in existence. Red meat substitution is done in butcher shops when particular meat (like mutton, beef,etc.) is expensive in a particular season, and so they get substituted with less expensive meat sometimes even meat of animals that we usually don’t intake.

We have had oblivious ears to the nasty truth of the meat scandal all these years hoping that we are eating the right meat we have ordered for.

Well, we no longer have to stay that way!!! What if I tell you that you can come to know whether you are eating the right meat with the help of your mobile? All that you would have to do would be to take a picture of the meat given to you from the butcher shop in your mobile camera via an app and you would come to know whether you are getting the right meat!!

Yes, we are on our way to achieving this dream into a mobile application. As of now, I have the backend working ready that I would be sharing with you.

Without much ado let’s jump into the coding part!! I have developed a supervised machine learning model to detect meat adulteration. We input the model with labeled images of both adulterated and unadulterated meat of different meat categories using which the model learns the difference between them. Hence, when we take a picture of the meat, the model classifies it into adulterated or unadulterated meat.

Tensorflow is used to categorize the input image as adulterated or unadulterated.

Images used for training and images to be tested should be in jpeg/jpg format with 100*100 dimensions which are the max size preset in the code. The images which adhere to those conditions are converted to 8-bit greyscale images (black and white) which are later layered and categorized.

In the training images folder, the images should be named as, ad.1.jpg, ad.2.jpg… similarly, the unadulterated images should be named as, unad.1.jpg, unad.2.jpg… as the class_names are set to be ‘ad’ and ‘unad’.

The dataset directory path is set to the respective train and test folders containing the images.

The size of the dataset is checked using the ‘.shape’ attribute and the naming convention is checked by printing its label. Here 0’s represent the adulterated meat images and 1’s represent the unadulterated meat images.

The same procedure is followed for the training dataset.

The test and train images are divided into 255 layers of shades.
The saved model is compiled for training, testing, and the accuracy metrics of the categorization model is determined.

The model is trained with 100 epochs (number of passes of the entire training dataset the machine learning program has completed) for better accuracy.

We can infer that the accuracy increases gradually from epoch to epoch and reaches up to 1.0(100%). We take the average of all epochs’ accuracy and compute the model’s accuracy.

Once all the epochs are completed, the accuracy of the model is found to be 82% which is a decent score for a Machine Learning, Image Processing model.

As we trained the model now we test the model for the images in the test dataset folder where the images are unlabelled, we use the model to predict their label and display (0 for ‘ad’ and 1 for ‘unad’).

On the printing predictions matrix, we can see SGD — optimizers value and categorical cross-entropy value.

Hurray!! You have got your meat adulteration detector model. Stay tuned for further updates on how to develop the front-end of the model and deploy it as a mobile application.

You can find the entire source code and dataset in my GitHub repository.

https://github.com/anjana999/Meat-Adulteration-Detector

Please star the repository if you like it!!

--

--

Anjana M P
Bloggers Bay

I am an enthusiastic software developer and am passionate about Machine Learning.