Skip to content

Instantly share code, notes, and snippets.

View Marina-Miranovich's full-sized avatar

Marina Miranovich Marina-Miranovich

View GitHub Profile
@Marina-Miranovich
Marina-Miranovich / code_snippet_13.js
Created November 14, 2018 21:06
code_snippet_13 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const [punch, kick, nothing] = Array.from((model.predict(
mobilenet(tf.fromPixels(scaled))
) as tf.Tensor1D).dataSync() as Float32Array);
const detect = (window as any).Detect;
if (nothing >= 0.4) return;
if (kick > punch && kick >= 0.35) {
detect.onKick();
return;
}
if (punch > kick && punch >= 0.35) detect.onPunch();
@Marina-Miranovich
Marina-Miranovich / code_snippet_12.js
Created November 14, 2018 21:04
code_snippet_12 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const model = tf.sequential();
model.add(tf.layers.inputLayer({ inputShape: [1024] }));
model.add(tf.layers.dense({ units: 1024, activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
await model.compile({
optimizer: tf.train.adam(1e-6),
loss: tf.losses.sigmoidCrossEntropy,
metrics: ['accuracy']
});
@Marina-Miranovich
Marina-Miranovich / code_snippet_11.js
Created November 14, 2018 21:03
code_snippet_11 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const punches = require('fs')
.readdirSync(Punches)
.filter(f => f.endsWith('.jpg'))
.map(f => `${Punches}/${f}`);
const kicks = require('fs')
.readdirSync(Kicks)
.filter(f => f.endsWith('.jpg'))
.map(f => `${Kicks}/${f}`);
const others = require('fs')
.readdirSync(Others)
@Marina-Miranovich
Marina-Miranovich / code_snippet_10.js
Created November 14, 2018 21:01
code_snippet_10 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const startInterval = (mobilenet, model) => () => {
setInterval(() => {
canvas.getContext('2d').drawImage(video, 0, 0);
grayscale(scale
.getContext('2d')
.drawImage(
canvas, 0, 0, canvas.width,
canvas.width / (ImageSize.Width / ImageSize.Height),
0, 0, ImageSize.Width, ImageSize.Height
));
@Marina-Miranovich
Marina-Miranovich / code_snippet_9.js
Created November 14, 2018 20:50
code_snippet_9 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
let mobilenet: (p: any) => tf.Tensor<tf.Rank>;
tf.loadModel('http://localhost:5000/model.json').then(model => {
mobileNet
.load()
.then((mn: any) => mobilenet = mobilenetInfer(mn))
.then(startInterval(mobilenet, model));
});
@Marina-Miranovich
Marina-Miranovich / code_snippet_8.js
Created November 14, 2018 20:49
code_snippet_8 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const grayscale = (canvas: HTMLCanvasElement) => {
const imageData = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
data[i] = avg;
data[i + 1] = avg;
data[i + 2] = avg;
}
canvas.getContext('2d').putImageData(imageData, 0, 0);
@Marina-Miranovich
Marina-Miranovich / code_snippet_7.js
Created November 14, 2018 20:48
code_snippet_7 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const video = document.getElementById('cam');
const Layer = 'global_average_pooling2d_1';
const mobilenetInfer = m => (p): tf.Tensor<tf.Rank> => m.infer(p, Layer);
const canvas = document.getElementById('canvas');
const scale = document.getElementById('crop');
const ImageSize = {
Width: 100,
Height: 56
};
navigator.mediaDevices
@Marina-Miranovich
Marina-Miranovich / code_snippet_6.js
Created November 14, 2018 20:46
code_snippet_6 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
await model.fit(xs, ys, {
epochs: Epochs,
batchSize: parseInt(((punches.length + others.length) * BatchSize).toFixed(0)),
callbacks: {
onBatchEnd: async (_, logs) => {
console.log('Cost: %s, accuracy: %s', logs.loss.toFixed(5), logs.acc.toFixed(5));
await tf.nextFrame();
}
}
});
@Marina-Miranovich
Marina-Miranovich / code_snippet_5.js
Last active November 14, 2018 20:46
code_snippet_5 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
export const readInput = img => imageToInput(readImage(img), TotalChannels);
const readImage = path => jpeg.decode(fs.readFileSync(path), true);
const imageToInput = image => {
const values = serializeImage(image);
return tf.tensor3d(values, [image.height, image.width, 3], 'int32');
};
const serializeImage = image => {
const totalPixels = image.width * image.height;
const result = new Int32Array(totalPixels * 3);
for (let i = 0; i < totalPixels; i++) {
@Marina-Miranovich
Marina-Miranovich / code_snippet_4.js
Created November 14, 2018 20:43
code_snippet_4 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
const punches = require('fs')
.readdirSync(Punches)
.filter(f => f.endsWith('.jpg'))
.map(f => `${Punches}/${f}`);
const others = require('fs')
.readdirSync(Others)
.filter(f => f.endsWith('.jpg'))
.map(f => `${Others}/${f}`);
const ys = tf.tensor1d(
new Array(punches.length).fill(1)