-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
66 lines (58 loc) · 2.22 KB
/
utils.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
61
62
63
64
65
66
const tf = require('@tensorflow/tfjs-node');
const sleep = function (milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
}
while (currentDate - date < milliseconds);
}
const modelToDict = async function (model) {
const weightsByLayer = {}
for (const layer of model.layers) {
if (!(layer.name.startsWith("average_pooling") || layer.name.startsWith("flatten"))) {
const layerWeight = await layer.getWeights()
const layerWeightData = layerWeight.map(async (weight) => {
return await weight.data()
})
const layerWeightShape = layerWeight.map(async (weight) => {
return await weight.shape
})
const jsonData = await Promise.all(layerWeightData)
const jsonShape = await Promise.all(layerWeightShape)
weightsByLayer[layer.name] = { "data": jsonData, "shape": jsonShape }
}
}
return weightsByLayer
}
const dictToModel = function (model, parsedJson) {
for (const layer of model.layers) {
const tempLayerJson = parsedJson[layer.name]
if (layer.name.startsWith("conv2d")) {
layer.setWeights([tf.tensor4d(Object.values(tempLayerJson["data"][0]).map(element => element), tempLayerJson["shape"][0]),
tf.tensor(Object.values(tempLayerJson["data"][1]).map(element => element), tempLayerJson["shape"][1])])
}
else if (layer.name.startsWith("dense")) {
layer.setWeights([tf.tensor2d(Object.values(tempLayerJson["data"][0]).map(element => element), tempLayerJson["shape"][0]),
tf.tensor(Object.values(tempLayerJson["data"][1]).map(element => element), tempLayerJson["shape"][1])])
}
}
}
const dictSum = function (a, b) {
Object.entries(a).forEach(([key, val]) => {
a[key] = val + b[key]
})
}
const dictDivide = function (a, b) {
Object.entries(a).forEach(([key, val]) => {
a[key] = val / b
})
}
const delay = async function (ms, state = null) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(state), ms);
})
}
module.exports = {
sleep, modelToDict, dictToModel, dictSum, dictDivide, delay
}