forked from ayberkydn/haxRL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncs.js
169 lines (150 loc) · 5.11 KB
/
funcs.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
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
var tensorToMatrix = a => math.matrix(Array.from(a.dataSync())).reshape(a.shape);
var tensorToArray = a => math.matrix(Array.from(a.dataSync())).reshape(a.shape)._data;
var matrixToTensor = a => dl.tensor(a._data);
var softmax = (scoresIn) => {
scores = Object.assign([], scoresIn);
let sum = 0;
for (let n = 0; n < scores.length; n++) {
scores[n] = Math.exp(scores[n]);
sum += scores[n];
}
for (let n = 0; n < scores.length; n++) {
scores[n] /= sum;
}
return scores;
};
var choice = (probs) => {
for (let n = 1; n < 3; n++) {
scores[n] += scores[n - 1];
}
let rand = Math.random();
if (rand < scores[0]) {
return 0;
} else if (rand < scores[1]) {
return 1;
} else {
return 2;
}
};
var arrayEqual = (array1, array2) => {
if (array1.length != array2.length) {
return false;
} else {
for (let n = 0; n < array1.length; n++) {
if (array1[n] != array2[n]) {
return false;
}
}
return true;
}
};
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
var ImageDataRGBA255ToImageTensorRGB1 = (imgData) => {
return dl.tidy(() => {
if (!(imgData instanceof ImageData)) {
throw `${imgData} Not an ImageData`;
}
return dl.fromPixels(imgData).div(dl.scalar(255));
});
};
var TensorRGB1ToImageDataRGBA255 = (tensor) => {
return dl.tidy(() => {
if (tensor.shape.length != 3 || tensor.shape[2] != 3) {
throw `Tensor of shape ${tensor.shape} is not an RGB tensor`;
}
let alpha = dl.ones([tensor.shape[0], tensor.shape[1], 1], 'float32');
let cat = tensor.concat(alpha, 2).mul(dl.scalar(255)).asType('int32');
var rgba255 = new ImageData(Uint8ClampedArray.from(cat.dataSync()), tensor.shape[1], tensor.shape[0]);
return rgba255;
});
};
var ImageTensorRGBToGray = (tensor) => {
return dl.tidy(() => {
if (tensor.shape.length != 3) {
throw `${tensor} is not an image tensor.`;
} else if (tensor.shape[2] != 3) {
throw `${tensor} is not an RGB tensor.`;
} else {
let gray = dl.mean(tensor, -1, true);
return gray;
}
});
};
var ImageTensor1To3Channel = (tensor) => {
return dl.tidy(() => {
if (tensor.shape.length != 3) {
throw `$Tensor of shape {tensor.shape} is not an image tensor.`;
} else if (tensor.shape[2] != 1) {
throw `Tensor of shape ${tensor.shape} is not an Grayscale tensor.`;
} else {
let rgb = dl.concat([tensor, tensor, tensor], -1);
return rgb;
}
});
};
var selectChannelFromImageTensor = (tensor, channel) => {
return dl.tidy(() => {
if (tensor.shape.length != 3) {
throw `Tensor of shape ${tensor.shape} is not an image tensor`;
} else if (channel != 0 && channel != 1 && channel != 2) {
throw `Invalid channel input: ${channel}`;
} else {
let imgTensorChannel = dl.slice(tensor, [0, 0, channel], [tensor.shape[0], tensor.shape[1], 1]);
return imgTensorChannel;
}
});
};
var scaleImageTensor = (tensor, scale) => {
return dl.tidy(() => {
if (scale > 0 && scale <= 1) {
scale = [scale, scale];
}
if (scale.length != 2) {
throw "Scale must be [height, width] format or must be number between 0 and 1.";
}
let imgHeight, imgWidth;
if (tensor.shape.length == 4) {
imgHeight = tensor.shape[1];
imgWidth = tensor.shape[2];
} else if (tensor.shape.length == 3) {
imgHeight = tensor.shape[0];
imgWidth = tensor.shape[1];
} else {
throw "Incompatible image tensor for scaling";
}
return tensor.resizeBilinear([Math.floor(imgHeight * scale[0]), Math.floor(imgWidth * scale[1])]);
});
};
var sampleImageFrom = (canvas, channel = 0, scale = [1, 1]) => {
return dl.tidy(() => {
let context = canvas.getContext("2d");
let img = context.getImageData(0, 0, cWidth, cHeight);
img = ImageDataRGBA255ToImageTensorRGB1(img);
img = scaleImageTensor(img, scale);
img = selectChannelFromImageTensor(img, channel);
return img;
});
};
var drawImageTensor = (tensor, canvas, dispose = true) => {
return dl.tidy(() => {
if (tensor.shape.length != 3) {
throw `Invalid tensor of shape ${tensor.shape}`;
} else {
let imgData;
if (tensor.shape[2] == 1) {
imgData = TensorRGB1ToImageDataRGBA255(ImageTensor1To3Channel(tensor));
} else if (tensor.shape[2] == 3) {
imgData = TensorRGB1ToImageDataRGBA255(tensor);
} else {
throw `Invalid tensor of shape ${tensor.shape}`;
}
let context = canvas.getContext("2d");
context.putImageData(imgData, 0, 0);
if (dispose == true) {
tensor.dispose();
}
}
});
};