Created
October 2, 2020 16:21
-
-
Save loretoparisi/a610568cc254d9c6da73c6fdccb0f6c1 to your computer and use it in GitHub Desktop.
TFJS Test for EfficientDet Models
This file contains hidden or 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 fs = require('fs'); | |
const path = require('path'); | |
const log = require('@vladmandic/pilogger'); | |
const tf = require('@tensorflow/tfjs'); | |
const tfnode = require('@tensorflow/tfjs-node'); | |
function getTensorFromImage(path) { | |
if (!fs.existsSync(path)) return null; | |
const data = fs.readFileSync(path); | |
const tfimage = tfnode.node.decodeImage(data); | |
const expanded = tf.expandDims(tfimage, 0); | |
log.info('Image:', path, tfimage.size, 'bytes with shape:', expanded.shape); | |
tf.dispose(tfimage); | |
return expanded; | |
} | |
async function processSavedModel(image, modelPath) { | |
log.info('Loading saved model:', modelPath); | |
log.data('Model signature:', await tfnode.node.getMetaGraphsFromSavedModel(modelPath)) | |
const model = await tfnode.node.loadSavedModel(modelPath); | |
log.state('TensorFlow/JS Memory', tf.memory()); | |
try { | |
const res = await model.predict(image, {score: 0.1, iou: 0.1, topk: 20}); | |
log.data('Detected', res); | |
} catch (err) { | |
log.error(err.stack ? err.stack : err); | |
} | |
} | |
async function processGraphModel(image, modelPath) { | |
log.info('Loadeding layers model:', modelPath); | |
const model = await tf.loadGraphModel(`file://${path.join(__dirname, modelPath)}`); | |
log.info('Loaded model:', modelPath, model.artifacts.format, model.artifacts.generatedBy, model.artifacts.convertedBy); | |
log.data('Model signature:', model.executor._signature); | |
log.state('TensorFlow/JS Memory', tf.memory()); | |
try { | |
const res = await model.executeAsync(image); | |
log.data('Detected', res); | |
} catch (err) { | |
log.error(err.stack ? err.stack : err); | |
} | |
} | |
async function processLayersModel(image, modelPath) { | |
log.info('Loadeding layers model:', modelPath); | |
const model = await tf.loadLayersModel(`file://${path.join(__dirname, modelPath)}`); | |
log.data('Model signature:', model.executor._signature); | |
log.state('TensorFlow/JS Memory', tf.memory()); | |
try { | |
const res = await model.executeAsync(image); | |
log.data('Detected', res); | |
} catch (err) { | |
log.error(err.stack ? err.stack : err); | |
} | |
} | |
async function main() { | |
log.header(); | |
log.info('TensorFlow/JS Version', tf.version); | |
await tf.setBackend('tensorflow'); // 'tensorflow' is tfjs-node backend | |
await tf.enableProdMode(); | |
tf.ENV.set('DEBUG', false); | |
tf.ENV.set('WEBGL_FORCE_F16_TEXTURES', true); | |
await tf.ready(); | |
log.info('TensorFlow/JS Backend', tf.getBackend()); | |
log.info('TensorFlow/JS Flags', tf.ENV.flags); | |
const image = getTensorFromImage('test.jpg'); | |
// 1 - saved model from tfhub | |
// download: https://tfhub.dev/tensorflow/efficientdet/d0/1?tf-hub-format=compressed | |
// error: Session fail to run with error: Expects arg[0] to be uint8 but int32 is provided | |
// await processSavedModel(image, 'd0/tfhub'); | |
// 2 - saved model from tf model zoo (should be the same as (1), but file size does not match) | |
// download: http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz | |
// error: Session fail to run with error: Expects arg[0] to be uint8 but int32 is provided | |
// await processSavedModel(image, 'd0/zoo'); | |
// 3 - saved model converted from checkpoint from google's repository (there is no saved model) | |
// download: https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco2/efficientdet-d0.tar.gz | |
// convert ckpt to saved: model_inspect.py --runmode=saved_model --model_name=efficientdet-d0 --ckpt_path=ckpt --saved_model_dir saved | |
// error: Create kernel failed: Invalid argument: NodeDef mentions attr 'allowed_devices' not in Op<name=VarHandleOp | |
// await processSavedModel(image, 'd0/google'); | |
// 4 - graph model converted from (1): tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model . graph | |
// error: Error in matMul: inputs must have the same rank of at least 2, got ranks 5 and 2. | |
// await processGraphModel(image, 'd0/tfhub/graph/model.json'); | |
// 5 - graph model converted from (2): tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model . graph | |
// error: Error in matMul: inputs must have the same rank of at least 2, got ranks 5 and 2. | |
// await processGraphModel(image, 'd0/zoo/graph/model.json'); | |
// 6 - graph model converted from (3): tensorflowjs_converter --input_format=tf_saved_model --output_format=tfjs_graph_model . graph | |
// error: the input tensors shape does not match | |
// await processGraphModel(image, 'd0/google/graph/model.json'); | |
// 7 - graph model converted from (3): tensorflowjs_converter --input_format tf_frozen_model --output_format tfjs_graph_model --output_node_names=detections efficientdet-d0_frozen.pb frozen | |
// error: The shape of dict['image_files'] provided in model.execute(dict) must be [-1], but was [1,3268,5843,3] | |
// await processGraphModel(image, 'd0/google/frozen/model.json'); | |
// 8 - layers model converted from keras model from google (seems its weights only, not a full model) | |
// download: https://storage.googleapis.com/cloud-tpu-checkpoints/efficientdet/coco2/efficientdet-d0.h5 | |
// convert: tensorflowjs_converter --input_format keras ./efficientdet-d0.h5 out | |
// error: TypeError: Cannot read property 'model_config' of null | |
// await processLayersModel(image, 'd0/keras/layers/model.json'); | |
tf.dispose(image); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment