When food meets AI: the Smart Recipe Project
Tags: machinelearning, ai, python, recipes
Published: 2020-07-27 06:35:35

#Part 2. Neither fish nor fowl? Classify it with the Smart Ingredient Classifier

![Musssel soup](https://dev-to-uploads.s3.amazonaws.com/i/p8mmrmathp81btcyjdyc.jpg)
_Mussel soup_

In the previous article, we extracted food entities (ingredients, quantities and units of measurement) from recipes. In this post, we classify the ingredient taxonomic class using the [BERT](https://arxiv.org/abs/1810.04805) model. In plain words, this means to classify Emmental as cheese, orange as a fruit, peas as a vegetable, and so on for each ingredient in recipes.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sncd6jfcx7u78pgar5p6.png)

##BERT in five points

Since its release in late 2018, BERT has positively changed the way to face NLP tasks, solving many challenging problems in the NLP field.
Under this view, one of the main problems in NLP consists of a lack of training data. To cope with this lack, the idea is to exploit a large amount of unannotated data for training general-purpose ​[language representation models​](https://en.wikipedia.org/wiki/Language_model), a process known as pre-training, and then fine-tuning these models on a smaller task-specific dataset.
Though this technique is not new (see [​word2vec](https://towardsdatascience.com/introduction-to-word-embedding-and-word2vec-652d0c2060fa) and [​GloVE](https://nlp.stanford.edu/projects/glove/) embeddings), we can say, BERT exploits it better. Why? Let’s find it out in five points:
1. It is built on a [​Transformer](https://medium.com/inside-machine-learning/what-is-a-transformer-d07dd1fbec04) architecture, a powerful state-of-the-art architecture, which applies an attention mechanism to understand relationships between tokens in a sentence.
2. It is deeply bidirectional since it takes into account the left and right contexts at the same time.
3. BERT is pre-trained on a large corpus of unlabeled text that allows to pick up the deeper and intimate understandings of how the language works.
4. BERT can be fine-tuned for different tasks by adding a few additional output layers. 
5. BERT is trained to perform:
- Masked Language Modelling: BERT has to predict randomly masked words.
- New sentence prediction: BERT tries to predict the next sentence in a sequence
of sentences.

##The Smart Recipe Project Taxonomy
To carry out the task, we designed a taxonomy, a model of classification for defining macro-categories and classifying the ingredients within them:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ki0hjielgwkmx16a45ag.png)

Such categorization is then used to tag the dataset that trains the model.

##BERT for ingredient taxonomic classification


For our task (ingredient taxonomic classification), the pre-trained BERT models have optimal performance. We chose the ​[bert-base-multilingual-cased](https://huggingface.co/bert-base-multilingual-cased) model and divided the classifier into two modules:
- **A training module**. We used [​Bert For Sequence Classification](https://huggingface.co/transformers/model_doc/bert.html) a basic Bert with a single linear layer at the top for classification. Both the pre-trained model and the untrained layer were trained on our data.
   
- **An applying module**. The applier takes the trained model and uses it to determine the taxonomic class of the ingredient in the recipe.
[You can find a more detailed version of the post on Medium.](https://medium.com/@condenastitaly/when-food-meets-ai-the-smart-recipe-project-6aa28b30e248?sk=201511e1d46f4b26e8fc24e0a21a6f7f)

--- 

**When Food meets AI: the Smart Recipe Project**
a series of 6 amazing articles

_Table of content_

[Part 1: Cleaning and manipulating food data](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-mg2)
[Part 1: A smart method for tagging your datasets](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-29bf)
[Part 2: NER for all tastes: extracting information from cooking recipes](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-2d6e)
[Part 2: Neither fish nor fowl? Classify it with the Smart Ingredient Classifier](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-21pg)
[Part 3: FoodGraph: a graph database to connect recipes and food data](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-27pm)
[Part 3. FoodGraph: Loading data and Querying the graph with SPARQL](https://dev.to/condenastitaly/when-food-meets-ai-the-smart-recipe-project-b3g)