Skip to content

Instantly share code, notes, and snippets.

@kedevked
kedevked / mnist-mobilenet.js
Created March 8, 2019 20:36
using mobilenet for mnist classification
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const mnist = require('mnist');
global.fetch = require('node-fetch')
const NUM_CLASSES = 10;
async function loadModel() {
const loadedModel = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json')
// take whatever layer except last output
@kedevked
kedevked / loadmobilenet.js
Created March 8, 2019 21:33
load mobilenet model
const loadedModel = await tf.loadModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json')
// take whatever layer except last output
loadedModel.layers.forEach(layer => console.log(layer.name))
const layer = loadedModel.getLayer('conv_pw_13_relu')
return tf.model({ inputs: loadedModel.inputs, outputs: layer.output });
@kedevked
kedevked / mnist-sequential.js
Created March 8, 2019 21:38
build a sequential model for transfert learning
async function buildModel(featureExtractor, units) {
return tf.sequential({
layers: [
// Flattens the input to a vector so we can use it in a dense layer. While
// technically a layer, this only performs a reshape (and has no training
// parameters).
// slice so as not to take the batch size
tf.layers.flatten(
{ inputShape: featureExtractor.outputs[0].shape.slice(1) }),
// add all the layers of the model to train
@kedevked
kedevked / tfjs-webworker.html
Created March 14, 2019 02:32
use tensorflow.js in a web worker
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.2/dist/tf.min.js"></script>
<script>
const worker_function = () => {
onmessage = () => {
console.log('from web worker')
this.window = this
importScripts('https://cdn.jsdelivr.net/npm/setimmediate@1.0.5/setImmediate.min.js')
importScripts('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3')
@kedevked
kedevked / dog_breed.conv.py
Created April 28, 2019 17:09
convolutional layers for dog breed classifier
model.add(Dense(input_shape=[224, 224, 3], units=16))
model.add(Conv2D(filters = 16, kernel_size=2, strides=1, padding ='valid', activation='relu'))
model.add(MaxPooling2D(pool_size = (2), strides=(2), padding ='valid'))
model.add(Conv2D(filters = 32, kernel_size=2, strides=1, padding ='valid', activation='relu'))
model.add(MaxPooling2D(pool_size = (2), strides=(2), padding ='valid'))
model.add(Conv2D(filters = 64, kernel_size=2, strides=1, padding ='valid', activation='relu'))
model.add(MaxPooling2D(pool_size = (2), strides=(2), padding ='same'))
model.add(GlobalAveragePooling2D())
model.add(Dense(units=133, activation='softmax'))
@kedevked
kedevked / dog_breed_transfer.learning.py
Created April 28, 2019 17:26
transfer learning with inception v3
bottleneck_features = np.load('inception_v3_file.npz')
train_inception_v3 = bottleneck_features['train']
valid_inception_v3 = bottleneck_features['valid']
test_inception_v3 = bottleneck_features['test']
model = Sequential()
model.add(Dense(input_shape=train_inception_v3.shape[1:]), units=16))
model.add(Conv2D(filters = 16, kernel_size=2, strides=1, padding ='valid', activation='relu'))
model.add(MaxPooling2D(pool_size = (2), strides=(2), padding ='valid'))
@kedevked
kedevked / dog_breed_global_average_pooling.py
Created April 28, 2019 17:37
transfer learning with only GlobalAveragePooling layer
model = Sequential()
model.add(GlobalAveragePooling2D(train_inception_v3.shape[1:]))
model.add(Dense(133, activation='softmax'))
@kedevked
kedevked / dog_breed_flask.py
Created April 28, 2019 17:43
run flask without thread
if __name__ == "__main__":
app.run("0.0.0.0", port=5000, debug=False, threaded=False)
@kedevked
kedevked / dog_breed_flask_graph.py
Created April 28, 2019 18:08
use the same graph during all flask requests
app = Flask(__name__)
graph = tf.get_default_graph()
@app.route('/predict', methods=['POST', 'GET'])
@cross_origin()
def predict_():
'''
returns a flower prediction
'''
global graph
@kedevked
kedevked / drawingRectangle.html
Created February 12, 2021 18:09
drawing rectangle using tensorflow.js
<head>
<style>
body {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
canvas {