Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 1x 1x 1x 2x 1x 1x 1x 1x 19x 15x 19x 1x 19x 19x 57x 19x 53x 53x 53x 53x 19x 15x 19x 15x 34x 53x 4x 1x 1x 1x 1x 21x 21x 21x 21x 21x 21x 1x 15x 15x 30x 15x 15x 15x 15x 1x 114x 114x 114x 1x 4x 2x 2x 2x 2x 1x 4x 4x 4x 4x 4x 4x 1x 1x 2x 2x 2x 2x 2x 2x | /** * File Name: model-loader.service * Version Number: v1.1 * Author: Tobias Bester * Project Name: Ninshiki * Organization: Software Sharks * User Manual: Refer to https://github.com/OrishaOrrie/SoftwareSharks/blob/master/Documentation/User%20Manual.pdf * Update History: * ------------------------------------------ * Date Author Description * 14/08/2018 Tobias Created service * ------------------------------------------ * Test Cases: model-loader.service.spec.ts * Functional Description: * Completes all actions related to the image prediction model, including loading the model and predicting images. */ /** * @ignore */I import { Injectable } from '@angular/core';E import * as tf from '@tensorflow/tfjs'; @Injectable({ prEovidedIn: 'root' }) export class ModelLoaderService { /** * The TensorFlowJS Model, which is loaded from a Google Cloud Storage bucket as a JSON file and weight shards */ public model: tf.Model = null; /** * Specifies the different models that can be used, including their name, the URL of the Google Storage bucket in which * they are called from, the number of classes that it can predict from, and whether the model has catalogue links, as is the * case with the Bramhope model. Also includes the classes JSON file with the model's corresponding class labels */ public modelType = [{ 'nIame': 'bramhope', 'url': 'https://storage.googleapis.com/testproject-ee885.appspot.com/mobilenet_model/model.json', 'classIJson': 'classes.json', 'numClIasses': 57, 'hasLinks': false }, { 'name': 'bramhope', 'url': 'https://storage.googleapis.com/testproject-ee885.appspot.com/bramhope_mobilenet_model/model.json', 'classJson': 'bramhope_classes.json', 'numClasses': 53, 'hasLinks': trEue }, { 'name': 'imagenet', 'url': 'https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json', 'classJson': 'imagenet_classes.json', 'numClasses': 1000, 'hasLinks': false }E ]; /** * An array used to store JSON objects related to the classes that were predicted. Includes class name, class likeliness, * and class catalogue links, if specified */ public resultPreds = []; /** * Determines which model is to be loaded and used to make predictions */ public modelNumber = 1; /** * Once this service is called, it checks if a model has been loaded */ constructor() { if (this.modelIsReady() === true) { console.log('Service has model ready'); } else { console.log('Service is stil loading model'); } } /** * Loads the TensorFlowJS model from the specified URL by using the tf.loadModel. It then warms up the model * by predicting on a blank image. This function only loads a model if one has not been loaded already */ async loadModel() { if (this.modelIsReady()) { console.log('Model is already loaded'); } else { try { this.model = await tf.loadModel(this.modelType[this.modelNumber].url); // Warm up model (this.model.predict(tf.zeros([1, 224, 224, 3])) as tf.Tensor<tf.Rank>).dispose(); console.log('Model Loaded from service!'); } catch (err) { console.error('Error obtained: ' + err); } } } /** * Checks if the model is ready to be used * @returns True if a TensorFlowJS model is loaded into memory. False if not */ modelIsReady() { console.log('Checking if the model is ready'); if (this.model == null) { return false; } else { return true; }I } /** * This is used to change the selected model. It does not load the model into memory but instead sets the model * in memory to null * @param modelNum Specifies which model is to be loaded */ changeModel(modelNum) { if (this.modelNumber === modelNum) { console.log('Model already loaded'); return; } this.modelNumber = modelNum; this.model = null; } E /** * Performs a set of TensorFlowJS operations that result in a list of predicted classes of an image * @param image HTMLImageElement containing the image to be predicted * @returns A list of predictions where each element is the predicted likeliness of the corresponding model class */ async predictImage(image) { const predictedClass = tf.tidy(() => { const raw = tf.fromPixels(image, 3); const cropped = this.cropImage(raw); // 224,224 is the required size for the MobileNet model const resized = tf.image.resizeBilinear(cropped, [224, 224]); const batchedImage = resized.expandDims(0); const img = batchedImage.toFloat().div(tf.scalar(127)).sub(tf.scalar(1)); const predictions = (this.model.predict(img) as tf.Tensor); return predictions; }); const classId = (await predictedClass.data()); // predictedClass.dispose(); return classId; } /** * Called by the predictImage function, this crops the raw image pixel data to a smaller * size so that it can be resized later * @param img The raw image pixel data as returned by tf.fromPixels * @returns The raw pixel data of the cropped image */E cropImage(img) { const size = Math.min(img.shape[0], img.shape[1]); const centerHeight = img.shape[0] / 2; const beginHeight = centerHeight - (size / 2); const centerWidth = img.shape[1] / 2; const beginWidth = centerWidth - (size / 2); return img.slice([beginHeight, beginWidth, 0], [size, size, 3]); } /** * Maps the predictions returned from the predictImage function to the corresponding class labels in * the classes JSON file. Then the classes are sorted by decreasing likeliness and the classes with a * likeliness lower than 0.001% are cut off * @param classPreds tf.Tensor.data The list of predictions returned by the predictImage function * @returns An array of JSON objects of the model classes and their associated prediction likeliness */ mapPredictions(classPreds) { console.log('Mapping predictions...'); const classesJson = require(`../imageupload/classes/${this.modelType[this.modelNumber].classJson}`); const numClasses = classPreds.length; this.resultPreds = []; const linkExists = this.modelHasLinks(); for (let i = 0; i < numClasses; i++) { // tslint:disable-next-line:triple-equals if (this.modelNumber == 2) { // Use if the classes json is in the plain format this.resultPreds[i] = {}; this.resultPreds[i].name = classesJson[i]; this.resultPreds[i].likeliness = (classPreds[i] * 100).toFixed(4); } else { // Used if the classes json is in the custom format this.resultPreds[i] = {}; this.resultPreds[i].id = classesJson.classes[i].id; this.resultPreds[i].first = classesJson.classes[i].first; this.resultPreds[i].name = classesJson.classes[i].name; this.resultPreds[i].likeliness = (classPreds[i] * 100).toFixed(4); if (linkExists) { this.resultPreds[i].link = classesJson.classes[i].link; } } } this.sortPreds(); this.processResultNames(); return this.resultPreds; } /** * Called by mapPredictions in order to sort the classes by likeliness */ public sortPreds() { this.resultPreds.sort((a, b) => { return b.likeliness - a.likeliness; }); } /** * Checks whether the selected model contains catalogue links * @returns True if the model has links, false if not */ public modelHasLinks() { return this.modelType[this.modelNumber].hasLinks; } /** * Formats the class labels to a more readable format and slices off classes with a likeliness lower * than 0.001% * @returns Sliced array of processed classes */ public processResultNames() { this.resultPreds.forEach((element, index) => { element.name = element.name.replace(/_/g, ' '); element.name = element.name.charAt(0).toUpperCase() + element.name.slice(1); if (element.likeliness < 0.001) { this.resultPreds = this.resultPreds.slice(0, index); } }); } } |