Forced self-isolation has stimulated many of us to remember our pet projects or just find a hobby for ourselves. Someone is fond of radio communication, someone develops cases for Raspberry. Well, someone is engaged in baking. But not simple, but with the involvement of machine learning.
Machine learning developer Sara Robinson decided to bake the perfect cupcake. But not by trial and error - our grandmothers did this, but with the help of technology. It all startedfrom taking 33 different recipes for cookies, pies and breads and building a TensorFlow model to analyze all of this data. The first goal was to understand why baked goods sometimes crumble a lot and how this can be avoided. But in the end, Sarah was able to get a recipe for the perfect cupcake, which is actually a cross between a cookie and a pie. And also - a recipe for a hybrid of bread and cookies.
From dataset to kitchen table
In December 2020, Sarah recruited her colleague, a Google employee named Dale Markovich, to the project. Together they developed a hybrid recipe. The resulting model made it possible to determine by the ingredients introduced what the result would be - cookies, pie or bread.
After everything worked out, the developers (it's a bit strange to use this term when applied to baking, right?) Decided to go further. The project was scaled up. This time, 600 recipes were selected for analysis. They have been carefully analyzed in order to identify the 16 most important ingredients that affect the texture and firmness of baked goods, plus, of course, taste.
These ingredients turned out to be:
- yeast,
- flour,
- sugar,
- eggs,
- fat (any oil),
- milk,
- baking soda,
- baking powder,
- Apple vinegar,
- buttermilk,
- banana,
- pumpkin puree,
- avocado,
- water,
- butter,
- salt.
The authors of the project, using the new model, compiled not only a list of ingredients, but also determined the correct proportions that help to create the perfect baked goods.
In addition, the model was able to independently determine the type of baked goods, separating
For each type of baked goods - biscuits, pie or bread, the model predicted the optimal amount and ratio of butter, sugar, yeast, and eggs. And the resulting model made it possible to get a recipe for hybrid dishes. Below is a photo of a cake and cookie hybrid using chocolate chips.
Sample code, model and running service
As for the TensorFlow model, the code is rather short. The Keras API was used for the model .
model = tf.keras.Sequential([
tf.keras.layers.Dense(16, input_shape=(num_ingredients,)),
tf.keras.layers.Dense(16, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
Using Python, the developers have created a function that converts the input ingredients first into the units of measurement familiar to baking lovers (cups, teaspoons, etc.), and then into percentages. This is what happens in the end.
def get_prediction(request):
data = request.get_json()
prescaled = dict(zip(columns, data))
scaled = scale_data(prescaled)
# Send scaled inputs to the model
prediction = predict_json('gcp-project-name', 'baking', scaled)
# Get the item with the highest confidence prediction
predicted_ind = np.argmax(prediction)
label_map = ['Bread', 'Cake', 'Cookies']
baked_prediction = label_map[predicted_ind]
confidence = str(round(prediction[predicted_ind] * 100))
if baked_prediction == 'Bread':
emoji = "It's bread!"
elif baked_prediction == 'Cake':
emoji = "It's cake!"
elif baked_prediction == 'Cookies':
emoji = "It's cookies!"
return "{} {}% confidence".format(emoji, confidence)
Well, the application itself, or rather a web service, is available here . So you can try it yourself.
Test tests
A hybrid of pie and cookies
Other developers became interested in the project, some decided to test the results of the model in practice. One of the recipes that we tested is "cookie pie".
The ingredients and preparation procedure were followed. For example, a model has shown that the optimum temperature for the added oil is 18.33 ° C. Of course, no one followed the tenths and hundredths of a degree, but the condition of 18 ° C was met.
The only thing that had to be changed was the size of the baking dish. A 6-inch form was required, but there was no one, so a 9-inch was used. In general, after the "cake-cookie" was baked, he was allowed to cool down a bit and tested. According to testers, the texture of the baked goods was unusual, but the taste was excellent. Everyone who tried this thing approved of it.
A hybrid of bread and cookies
The second tried and tested recipe is "baked goods". I had to tinker with him a little longer, but in the end everything worked out. The recipe proposed by the model is divided into two parts - actually, bread and cookies. The dough turned out to be much thicker than usually required for cookies, reminiscent of bread dough.
But in the end, everything turned out to be delicious, the texture resembled oatmeal cookies, although a little softer. Yeast was used in the recipe, so it looks a bit like bread.
Overall, the experiment turned out to be successful - machine learning and predictive model helped create unusual dishes that everyone who tried them liked.