-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtensorflow_imagenet.js
60 lines (40 loc) · 1.71 KB
/
tensorflow_imagenet.js
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
var Jimp = require('jimp')
var Redis = require('ioredis')
var fs = require('fs')
const model_path = '../models/tensorflow/imagenet/resnet50.pb'
const script_path = '../models/tensorflow/imagenet/data_processing_script.txt'
const json_labels = fs.readFileSync('../data/imagenet_classes.json')
const labels = JSON.parse(json_labels)
const image_width = 224;
const image_height = 224;
async function load_model() {
let redis = new Redis({ parser: 'javascript' });
const model = fs.readFileSync(model_path, {'flag': 'r'})
const script = fs.readFileSync(script_path, {'flag': 'r'})
redis.call('AI.MODELSET', 'imagenet_model', 'TF', 'CPU',
'INPUTS', 'images', 'OUTPUTS', 'output', "BLOB", model)
redis.call('AI.SCRIPTSET', 'imagenet_script', 'CPU', "SOURCE", script)
}
async function run(filename) {
let redis = new Redis({ parser: 'javascript' });
let image = await Jimp.read(filename);
let input_image = image.cover(image_width, image_height);
let buffer = Buffer.from(input_image.bitmap.data);
redis.call('AI.TENSORSET', 'image1',
'UINT8', image_height, image_width, 4,
'BLOB', buffer)
redis.call('AI.SCRIPTRUN', 'imagenet_script', 'pre_process_4ch',
'INPUTS', 'image1', 'OUTPUTS', 'temp1')
redis.call('AI.MODELRUN', 'imagenet_model',
'INPUTS', 'temp1', 'OUTPUTS', 'temp2')
redis.call('AI.SCRIPTRUN', 'imagenet_script', 'post_process',
'INPUTS', 'temp2', 'OUTPUTS', 'out')
let out = await redis.call('AI.TENSORGET', 'out', 'VALUES')
let idx = out[2][0]
console.log(idx, labels[idx.toString()])
}
exports.load_model = load_model
exports.run = run
load_model()
const img_path = '../data/cat.jpg'
run(img_path)