Week 6 — DeepNutrition: Be Aware Of Your Macronutrients

ismet okatar
bbm406f19
Published in
5 min readJan 6, 2020

Facilitating the dietary assesment process by detecting foods from image

Hi everyone,

Today I want to talk about our adventure in dealing with Food101 Dataset. As we talked in our earlier blogs the dataset provided by vision lab at ETH Zurich consists of 101 classes and 101000 images, 1000 images for each class. Due to balance in the dataset we do not need to work with the balancing problem and looked for solutions in machine learning for our main problem. Our ultimate goal was to detect foods from images.

After a short research we decided to use CNN for our problem because of we are dealing with images. But then we encountered many CNN architectures each with various advantages and disadvantages. After a research in the area and looking to papers of research teams which dealed with the same problem we saw ResNet, VGGNet and GoogLeNet(Inception).

Then we implemented each of them (week5 for more information) and decided to use InceptionV3 model. We observed that using pretrained weights speeds up the process. So we used imagenet weights in each of our experiments. Then we have seen that there are many hyperparameters that may effect the accuracy of our model and decided to make experiments at each of them.

We have made our experiments on:

  1. Loss Functions
  2. Activation Methods
  3. Learning Rate
  4. Momentum of Learning Rate
  5. Batch Size
  6. Dropout
  7. Fine tuning

Due to lack of processing power and GPU, each epoch on Food101 dataset was taking 20–30 minutes. So we decided to use a subset of Food101 and we tested each parameter on the 11 class sub-dataset of Food101 as we said last week. 1000 pictures per class are splitted randomly into two parts 750 to train and 250 to test.

Learning Rate and Momentum and Epoch

First lets talk about different learning rates, selecting optimum learning rate is challenging because selecting low learning rate may results in long training process that could get stuck and if we select learning rate that is large it may result in learning optimal set of weights too fast or an unstable training process.To experience this we have tried different learning parameters to see this and as we see model with small learning rate still needs more epoch, where model with large learning rate learns weights too fast and overfits. So selecting optimal learning rate is very important for both accuracy and speed for our model.

Momentum is size of the steps taken towards the minimum by trying to jump from a local minima. The reason why we use momentum is is to avoid the algorithm getting stuck in a local minimum.We want to get to the lowest point.Model with momentum will allow it to avoid local minimums and make it more likely to find a global minimum for our case.Having momentum too high means model will be more likely to overshoot (the model goes through the local minimum but the momentum carries it back upwards). This will lead to longer learning times.So selecting right momentum value is important so we have tried different momentum values for our model.During our search we have found that if the momentum term is large then the learning rate should be kept smaller. So we kept our momentum high and learning rate small for our model and in theory helped us and practice gave the best results for our model.

Different learning rates, momentums and their accuracies are shown below.

Learning Rate: 0.0001, Momentum: 0.9, Epoch: 10, Train Accuracy: %86.93, Validation Accuracy: %91.34
Learning Rate: 0.0001, Momentum: 0.9, Epoch: 20, Train Accuracy: %94.74, Validation Accuracy: %94.30
Learning Rate: 0.0001, Momentum: 0, Epoch: 20, Train Accuracy: %0.7000, Validation Accuracy: %82.09

Learning Rate: 0.001, Momentum: 0.9, Epoch: 10, Train Accuracy: %98.13 , Validation Accuracy: %92.47
Learning Rate: 0.001, Momentum: 0.9, Epoch: 20, Train Accuracy: %99.43, Validation Accuracy: %94.23
Learning Rate: 0.001, Momentum: 0, Epoch: 20, Train Accuracy: %94.73, Validation Accuracy: %95.21

Learning Rate: 0.0005, Momentum: 0.9, Epoch: 10, Train Accuracy: %97.45, Validation Accuracy: %94.85
Learning Rate: 0.0005, Momentum: 0.9, Epoch: 20, Train Accuracy: %99.06, Validation Accuracy: %95.29
Learning Rate: 0.0005, Momentum: 0, Epoch: 20, Train Accuracy: %89.40, Validation Accuracy: %94.08

Learning Rate: 0.005, Momentum: 0.9, Epoch: 10, Train Accuracy: %94.58, Validation Accuracy: %86.51
Learning Rate: 0.005, Momentum: 0.9, Epoch: 20, Train Accuracy: %99.22, Validation Accuracy: %94.96

Activation Methods

We tested 4 different activation methods with 20 epoch, 0.5 dropout and categorical_crossentropy as loss function.

We can see that tanh function gave the best result in the activation functions while it hasnt even overfitted yet.

In our case;

Best Activation tanh

Loss Functions

We tested 4 different activation methods with 20 epoch, 0.5 dropout and relu as activation function.

Support vector machine based categorical_hinge funtion gave less accuracy then categorical_crossentropy in 20 epochs. But due to it hasnt overfitted yet it may gave better results for larger epochs.

Best Loss categorical_crossentropy

Size of Batch

Due to lack of GPU memory we trained our models at maximum 16 batches.
we also tried 4 and 8 batch sizes to observe the effect of the batch size to the accuracy.

In our case we can see a correlation between batch size and accuracy. The accuracy has increased when batch size has increased.

Best batch size 16

Dropout

Dropout is giving a probability of 0 (zero) to every each perceptron. For example if dropout is 0.9 it means every node can be 0 with %90 possibility.

We tried: 0.5, 0.8, 0.2, 0.005 and 0 dropout values.

We can see that not using Droput gave really good result which is near to 0.96

Best No Dropout

Fine tuning

We add a few layer to the end to fine-tune our model.

inception = InceptionV3(weights='imagenet', include_top=False)x = inception.outputx = GlobalAveragePooling2D()(x)# x = Flatten(name='flatten')(x)x = Dense(128,activation='relu')(x)x = Dropout(0.5)(x)

We can see that our approach havent gave any difference in that case..

To meet in another projects..

Ismet Okatar

Ali Kayadibi

Muhammed Aydogan

Week5 — https://medium.com/bbm406f19/week-5-deepnutrition-be-aware-of-your-macronutrients-f6f36dc070a0

Week4 — https://medium.com/bbm406f19/week-4-deepnutrition-be-aware-of-your-macronutrients-aaecce8b330e?

Week3 — https://medium.com/bbm406f19/week-3-deepnutrition-be-aware-of-your-macronutrients-e3cd3f01a9f7

Week2 — https://medium.com/bbm406f19/week-2-deepnutrition-be-aware-of-your-macronutrients-131dc0389e8

Week1 — https://medium.com/bbm406f19/deepnutrition-be-aware-of-your-macronutrients-c47497c423cc

--

--