<jupyter_start><jupyter_text>Food-recognizer computer vision modelling  A computer vision model trained on dataset from TensorFlow Datasets- Training data = 75750 images- Testing data = 25,250 images- Using mixed precision  Goals- The goal is to match or beat the `DeepFood Research Paper` - https://arxiv.org/abs/1606.05675- Researchers used a `Convolutional Neural Network`.- Trained the model for couple of days- The `top accuracy` achieved was `77.4%`- The research focused on Computer-Aided Dietary Assesment- It states the following concerns   - "Worldwide, in 2014, almost 2 billion adults, 18+, were overweight. Of these, over 600 million were obese.   - Accurately documenting dietary caloric intake is crucial to manage weight loss, but also presents challenges because most of the current methods for dietary assessment must rely on memory to recall foods eaten.   - The ultimate goal of the research is to develop a computer-aided techinical solutions to enhance and improve the accuracy of current measurements of dietary intake.   - The proposed system in this paper aims to improve the accuracy of dietary assessment by analyzing food images captured by mobile devices.   - The key techinique innovation in this paper is the deep learning based food image recognitiion algorithms. Substantial research has demonstrated that digital imaging accurately estimates dietary intake in many environments and it has many advantages over other methods.<jupyter_code># GPU for mixed precision and training
!nvidia-smi -L
# Importing 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import seaborn as sns
import tensorflow_datasets as tfds 
# Using helper functions for ease and simplicity
import os
if not os.path.exists("helper_functions.py"):
  print("Available")
  !wget https://raw.githubusercontent.com/hecshzye/food-or-not-model/main/helper_functions.py
else:
  print("already exists")  
from helper_functions import load_prepare_images, create_confusion_matrix, pred_plot, create_tensorboard_callback, plot_loss_curves, compare_history, unzip_data, walk_dir, calculate_results<jupyter_output><empty_output><jupyter_text>Importing dataset from tensorflow dataset<jupyter_code>datasets_list = tfds.list_builders()
print("food101" in datasets_list)
(train_data, test_data), ds_info = tfds.load(name="food101",
                                             split=["train", "validation"],
                                             shuffle_files=True,
                                             as_supervised=True,
                                             with_info=True)<jupyter_output>[1mDownloading and preparing dataset food101/2.0.0 (download: 4.65 GiB, generated: Unknown size, total: 4.65 GiB) to /root/tensorflow_datasets/food101/2.0.0...[0m<jupyter_text>Exploratory Data Analysis<jupyter_code>ds_info.features
class_names = ds_info.features["label"].names
class_names[:20]
# EDA in small sample size 
train_one_sample = train_data.take(1)
train_one_sample
# Output info
for image, label in train_one_sample:
  print(f"""
  image shape: {image.shape}
  dtype: {image.dtype}
  target class: {label}
  Class name: {class_names[label.numpy()]}
         """)
image
# Range
tf.reduce_min(image), tf.reduce_max(image)
# Sample plotting
plt.imshow(image)
plt.title(class_names[label.numpy()])
plt.axis(False);<jupyter_output><empty_output><jupyter_text>Nice 🤤 😋 🥺  Preprocessing and batching the dataset<jupyter_code># Function for preprocessing (unit8 to float32, faster computation)
def preprocess_img(image, label, img_shape=224):
  image = tf.image.resize(image, [img_shape, img_shape])
  return tf.cast(image, tf.float32), label
# Preprocessing sample image
preprocessed_img = preprocess_img(image, label)[0]
print(f" before:\n {image[:2]}...,\nShape: {image.shape},\nDatatype: {image.dtype}\n")
print(f" after:\n {preprocessed_img[:2]}...,\nShape: {preprocessed_img.shape}, \nDatatype: {preprocessed_img.dtype}")
# plotting the sample image
plt.imshow(preprocessed_img/255.)
plt.title(class_names[label])
plt.axis(False);
# Mapping preprocessed function to training data
train_data = train_data.map(map_func=preprocess_img, num_parallel_calls=tf.data.AUTOTUNE)
train_data = train_data.shuffle(buffer_size=1000).batch(batch_size=32).prefetch(buffer_size=tf.data.AUTOTUNE)
# Test data preprocessing
test_data = test_data.map(preprocess_img, num_parallel_calls=tf.data.AUTOTUNE)
test_data = test_data.batch(32).prefetch(tf.data.AUTOTUNE)

train_data, test_data<jupyter_output><empty_output><jupyter_text>Callbacks<jupyter_code>checkpoint_path = "model_checkpoints/cp.ckpt"
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                      monitor="val_accuracy",
                                                      save_best_only=True,
                                                      save_weights_only=True,
                                                      verbose=1)<jupyter_output><empty_output><jupyter_text>Using Mixed Precision Training<jupyter_code>from tensorflow.keras import mixed_precision
mixed_precision.set_global_policy(policy="mixed_float16")
mixed_precision.global_policy()<jupyter_output><empty_output><jupyter_text>Feature Exraction Modelling<jupyter_code>from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing

# Base model
input_shape = (224, 224, 3)
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
# Freezing the model layers
base_model.trainable = False 

# Functional model 
inputs = layers.Input(shape=input_shape, name="input_layer", dtype=tf.float16)
x = base_model(inputs, training=False)
x = layers.GlobalAveragePooling2D(name="pooling_layer")(x)
x = layers.Dense(len(class_names))(x)

# Activation of output layer serperation for float32 activation
outputs = layers.Activation("softmax", dtype=tf.float32, name="softmax_float32")(x)
model = tf.keras.Model(inputs, outputs)

# Compile
model.compile(loss="sparse_categorical_crossentropy",
              optimizer=tf.keras.optimizers.Adam(),
              metrics=["accuracy"])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup