This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = Sequential() | |
model.add(GlobalAveragePooling2D(train_inception_v3.shape[1:])) | |
model.add(Dense(133, activation='softmax')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == "__main__": | |
app.run("0.0.0.0", port=5000, debug=False, threaded=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = Flask(__name__) | |
graph = tf.get_default_graph() | |
@app.route('/predict', methods=['POST', 'GET']) | |
@cross_origin() | |
def predict_(): | |
''' | |
returns a flower prediction | |
''' | |
global graph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<style> | |
body { | |
display: flex; | |
flex-direction: row; | |
justify-content: center; | |
align-items: center; | |
} | |
canvas { |
OlderNewer