concept MobileNet in category deep learning

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.
Due to this broad reach, the web browser is a logical choice for deploying deep-learning models, as long as the kinds of data the models expect are available in the browser. But what kinds of data are available in the browser? The answer is, many! Take, for example, the most popular applications of deep learning: classifying and detecting objects in images and videos, transcribing speech, translating languages, and analyzing text content. Web browsers are equipped with arguably the most comprehensive technologies and APIs for presenting (and, with user permission, for capturing) textual, image, audio, and video data. As a result, powerful machine-learning models can be directly used in the browser, for example, with TensorFlow.js and straightforward conversion processes. In the later chapters of this book, we will cover many concrete examples of deploying deep-learning models in the browser. For example, once you have captured images from a webcam, you can use TensorFlow.js to run MobileNet to label objects, run YOLO2 to put bounding boxes around detected objects, run Lipnet to do lipreading, or run a CNN-LSTM network to apply captions to images.
On the second point, the core idea of transfer learning is reusing previous training results. Through learning from a very large dataset, the original neural network has become very good at extracting useful features from the original input data. These features will be useful for the new task as long as the new data in the transfer-learning task is not too different from the original data. Researchers have assembled very large datasets for common machine-learning domains. In computer vision, there is ImageNet,[2] which contains millions of labeled images from about a thousand categories. Deep-learning researchers have trained deep convnets using the ImageNet dataset, including ResNet, Inception, and MobileNet (the last of which we will soon lay our hands on). Due to the large number and diversity of the images in ImageNet, convnets trained on it are good feature extractors for general types of images. These feature extractors will be useful for working with small datasets like those in the aforementioned scenarios, but training such effective feature extractors is impossible with small datasets like those. Opportunities for transfer learning exist in other domains as well. For example, in natural language processing, people have trained word embeddings (that is, vector representation of all common words in a language) on large text corpora consisting of billions of words. These embeddings are useful for language-understanding tasks where much smaller text datasets are available. Without further ado, let’s see how transfer learning works in practice through an example.
Figure 5.5. Transfer learning can be divided into three types according to whether the output shape and activation of the new model are the same as or different from those of the original model. Case A: the output shape and the activation function of the new model match those of the base model. The transfer of the MNIST model onto new digits in section 5.1.1 is an example of this type of transfer learning. Case B: the new model has the same activation type as the base model because the original task and the new task are of the same type (for example, both are multiclass classification). However, the output shapes are different (for instance, the new task involves a different number of classes). Examples of this type of transfer learning can be found in section 5.1.2 (controlling a video game in the style of Pac-ManTM 4 through a webcam) and 5.1.3 (recognizing a new set of spoken words). Case C: the new task is of a different type from the original one (such as regression versus classification). The object-detection model based on MobileNet is an example of this type.
![]()
The code in listing 5.3 (from webcam-transfer-learning/index.js) is responsible for loading the base model. In particular, we load a version of MobileNet that can run efficiently in TensorFlow.js. Info box 5.1 describes how this model is converted from the Keras deep-learning library in Python. As soon as the model is loaded, we use the getLayer() method to get hold of one of its layers. getLayer() allows you to specify a layer by its name ('conv_pw_13_relu' in this case). You may recall another way to access a model’s layers from section 2.4.2—that is, by indexing into the model’s layers attribute, which holds all the model’s layers as a JavaScript array. This approach is easy to use only when the model consists of a small number of layers. The MobileNet model we are dealing with here has 93 layers, which makes that approach fragile (for example, what if more layers get added to the model in the future?). Therefore, the name-based getLayer() approach is more reliable, if we assume the authors of MobileNet will keep the names of the key layers unchanged when they release new versions of the model.
Listing 5.3. Loading MobileNet and creating a “truncated” model from it
async function loadTruncatedMobileNet() { const mobilenet = await tf.loadLayersModel( #1 'https://storage.googleapis.com/' + #1 'tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json'); #1 const layer = mobilenet.getLayer( #2 'conv_pw_13_relu'); #2 return tf.model({ #3 inputs: mobilenet.inputs, #3 outputs: layer.output #3 }); #3 }
The second input (yPred) is the model’s output prediction, with exactly the same shape as yTrue. Of the five dimensions along the second axis of yTrue (the five columns, if we view it as a matrix), the first one is a 0–1 indicator for the shape of the target object (0 for triangle and 1 for rectangle). This is determined by how the data is synthesized (see simple-object-detection/synthetic_images.js). The remaining four columns are the target object’s bounding box—that is, its left, right, top, and bottom values—each of which ranges from 0 to CANVAS_SIZE (224). The number 224 is the height and width of the input images and comes from the input image size to MobileNet, which our model is based on.