Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
Added 2nd Model
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Mar 5, 2022
1 parent 4faf322 commit 37bde6a
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 12 deletions.
19 changes: 16 additions & 3 deletions food-busters-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { AzureFunction, Context, HttpRequest } from "@azure/functions";

import { checkVersion, getMLResult, makeRes, VersionRejectReason } from "./lib";
import {
checkVersion,
getMLLayerModel,
getMLResult,
makeRes,
VersionRejectReason,
} from "./lib";

const MinAppVersion = 306;
const MinWebVersion = 2;
const MinWebVersion = 4;

const index: AzureFunction = async (context: Context, req: HttpRequest) => {
const { body, headers } = req;
Expand All @@ -24,6 +30,11 @@ const index: AzureFunction = async (context: Context, req: HttpRequest) => {
return;
}

if (body.modelType != "P" && body.modelType != "U") {
context.res = makeRes(400, `Invalid Model Type: ${body.modelType}`);
return;
}

if (!body.image) {
context.res = makeRes(400, "Missing Image");
return;
Expand All @@ -36,7 +47,9 @@ const index: AzureFunction = async (context: Context, req: HttpRequest) => {

let result;
try {
result = await getMLResult(body.image);
if (body.modelType == "P") result = await getMLResult(body.image);

if (body.modelType == "U") result = await getMLLayerModel(body.image);
} catch (err) {
context.res = makeRes(500, `Error executing Model: ${err}`);
return;
Expand Down
66 changes: 59 additions & 7 deletions food-busters-ai/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,66 @@ const labelMap: { [key: number]: { name: string; nutrition: FoodNutrition } } =

// const mroot = "food-busters-ai/model";
let net: tf.GraphModel;
let lnet: tf.LayersModel;

export async function getMLResult(img: string) {
// const model = tf.io.fileSystem(`${mroot}/model.json`);
// const m1 = tf.io.fileSystem(`${mroot}/group1-shard1of3.bin`);
// const m2 = tf.io.fileSystem(`${mroot}/group1-shard2of3.bin`);
// const m3 = tf.io.fileSystem(`${mroot}/group1-shard3of3.bin`);
export async function getMLLayerModel(img: string) {
if (!lnet)
lnet = await tf.loadLayersModel(
"file://food-busters-ai/model-U/model.json"
);

const decodedImage = tf.node.decodeImage(
Buffer.from(img.replace(/^data:image\/.+;base64,/, ""), "base64"),
3
);

const transformed = tf.image
.resizeBilinear(decodedImage, [224, 224])
// stupid mistake
.cast("float32")
.div(255)
.expandDims(0);

const res = lnet.predict(transformed) as tf.Tensor;

// @ts-ignore
const arr = (await res.array())[0] as number[];

console.log(
`Omelet: ${arr[0] * 100}%, Chicken: ${arr[1] * 100}%, None: ${
arr[2] * 100
}%`
);

if (!net) net = await tf.loadGraphModel(process.env.ML_ENDPOINT as string);
// C++ Way lol
let max = arr[0];
let maxpos = 0;
for (let i = 1; i < arr.length - 1; i++) {
if (arr[i] > max) {
max = arr[i];
maxpos = i;
}
}

const result: MLResult = {
foodName: labelMap[maxpos + 1].name,
foodNutrition: labelMap[maxpos + 1].nutrition,
confidence: max,
version: Version,
};

tf.dispose(decodedImage);
tf.dispose(transformed);
tf.dispose(res);

return result;
}

export async function getMLResult(img: string) {
if (!net)
net = await tf.loadGraphModel(
"file://food-busters-ai/model/model.json"
);

const decodedImage = tf.node.decodeImage(
Buffer.from(img.replace(/^data:image\/.+;base64,/, ""), "base64"),
Expand All @@ -53,7 +105,7 @@ export async function getMLResult(img: string) {
foodName: labelMap[detected].name,
foodNutrition: labelMap[detected].nutrition,
// @ts-ignore
score: (await obj[2].array())[0][0],
confidence: (await obj[2].array())[0][0],
version: Version,
};

Expand Down
2 changes: 1 addition & 1 deletion food-busters-ai/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface MLResult {
foodName: string;
foodNutrition: FoodNutrition;
score: number;
confidence: number;
version: string;
}

Expand Down
Binary file added food-busters-ai/model-U/group1-shard1of6.bin
Binary file not shown.
Binary file added food-busters-ai/model-U/group1-shard2of6.bin
Binary file not shown.
Binary file added food-busters-ai/model-U/group1-shard3of6.bin
Binary file not shown.
Binary file added food-busters-ai/model-U/group1-shard4of6.bin
Binary file not shown.
Binary file added food-busters-ai/model-U/group1-shard5of6.bin
Binary file not shown.
Binary file added food-busters-ai/model-U/group1-shard6of6.bin
Binary file not shown.
1 change: 1 addition & 0 deletions food-busters-ai/model-U/model.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "food-busters-azure-functions",
"version": "1.0.2",
"version": "1.0.15",
"description": "Food Buster's Azure Functions",
"repository": "https://github.com/Food-Busters/azure-functions",
"author": "Leomotors",
Expand Down

0 comments on commit 37bde6a

Please sign in to comment.