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 / 1.py
Created November 14, 2018 20:02
code_snippet_1 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
np.random.seed(44)
ia.seed(44)
def main():
for i in range(1, 191):
draw_single_sequential_images(str(i), "others", "others-aug")
for i in range(1, 191):
draw_single_sequential_images(str(i), "hits", "hits-aug")
for i in range(1, 191):
draw_single_sequential_images(str(i), "kicks", "kicks-aug")
def draw_single_sequential_images(filename, path, aug_path):
@Marina-Miranovich
Marina-Miranovich / code_snippet_2.js
Created November 14, 2018 20:37
code_snippet_2 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
import * as tf from '@tensorflow/tfjs';
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: 1, activation: 'sigmoid' }));
model.compile({
optimizer: tf.train.adam(1e-6),
loss: tf.losses.sigmoidCrossEntropy,
metrics: ['accuracy']
@Marina-Miranovich
Marina-Miranovich / code_snippet_3.js
Created November 14, 2018 20:42
code_snippet_3 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
export const loadModel = async () => {
const mn = new mobilenet.MobileNet(1, 1);
mn.path = `file://PATH/TO/model.json`;
await mn.load();
return (input): tf.Tensor1D =>
mn.infer(input, 'global_average_pooling2d_1')
.reshape([1024]);
};
@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)
@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_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_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_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_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));
});