Skip to content

Commit

Permalink
Initial Javascript port
Browse files Browse the repository at this point in the history
  • Loading branch information
ebursztein committed Aug 24, 2023
1 parent bb9b890 commit d15ea29
Show file tree
Hide file tree
Showing 20 changed files with 5,086 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ release.sh
archive/
wandb/
experiments/

.DS_Store
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
Binary file added models/v1/tf/fingerprint.pb
Binary file not shown.
12 changes: 12 additions & 0 deletions models/v1/tf/keras_metadata.pb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

�7root"_tf_keras_network*�7{"name": "model_3", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": false, "class_name": "Functional", "config": {"name": "model_3", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 16, 24]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_2"}, "name": "input_2", "inbound_nodes": []}, {"class_name": "SpatialDropout1D", "config": {"name": "spatial_dropout1d_1", "trainable": true, "dtype": "float32", "rate": 0.0625, "noise_shape": null, "seed": null}, "name": "spatial_dropout1d_1", "inbound_nodes": [[["input_2", 0, 0, {}]]]}, {"class_name": "Flatten", "config": {"name": "flatten_1", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "name": "flatten_1", "inbound_nodes": [[["spatial_dropout1d_1", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_3", "inbound_nodes": [[["flatten_1", 0, 0, {}]]]}, {"class_name": "Activation", "config": {"name": "activation_2", "trainable": true, "dtype": "float32", "activation": "gelu"}, "name": "activation_2", "inbound_nodes": [[["dense_3", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_4", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_4", "inbound_nodes": [[["activation_2", 0, 0, {}]]]}, {"class_name": "Activation", "config": {"name": "activation_3", "trainable": true, "dtype": "float32", "activation": "gelu"}, "name": "activation_3", "inbound_nodes": [[["dense_4", 0, 0, {}]]]}, {"class_name": "Dense", "config": {"name": "dense_5", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_5", "inbound_nodes": [[["activation_3", 0, 0, {}]]]}, {"class_name": "Activation", "config": {"name": "tokenizer", "trainable": true, "dtype": "float32", "activation": "tanh"}, "name": "tokenizer", "inbound_nodes": [[["dense_5", 0, 0, {}]]]}], "input_layers": [["input_2", 0, 0]], "output_layers": [["tokenizer", 0, 0]]}, "shared_object_id": 15, "input_spec": [{"class_name": "InputSpec", "config": {"dtype": null, "shape": {"class_name": "__tuple__", "items": [null, 16, 24]}, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}}], "build_input_shape": {"class_name": "TensorShape", "items": [null, 16, 24]}, "is_graph_network": true, "full_save_spec": {"class_name": "__tuple__", "items": [[{"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 16, 24]}, "float32", null]}], {}]}, "save_spec": {"class_name": "TypeSpec", "type_spec": "tf.TensorSpec", "serialized": [{"class_name": "TensorShape", "items": [null, 16, 24]}, "float32", null]}, "keras_version": "2.11.0", "backend": "tensorflow", "model_config": {"class_name": "Functional", "config": {"name": "model_3", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 16, 24]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_2"}, "name": "input_2", "inbound_nodes": [], "shared_object_id": 0}, {"class_name": "SpatialDropout1D", "config": {"name": "spatial_dropout1d_1", "trainable": true, "dtype": "float32", "rate": 0.0625, "noise_shape": null, "seed": null}, "name": "spatial_dropout1d_1", "inbound_nodes": [[["input_2", 0, 0, {}]]], "shared_object_id": 1}, {"class_name": "Flatten", "config": {"name": "flatten_1", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "name": "flatten_1", "inbound_nodes": [[["spatial_dropout1d_1", 0, 0, {}]]], "shared_object_id": 2}, {"class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 3}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 4}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_3", "inbound_nodes": [[["flatten_1", 0, 0, {}]]], "shared_object_id": 5}, {"class_name": "Activation", "config": {"name": "activation_2", "trainable": true, "dtype": "float32", "activation": "gelu"}, "name": "activation_2", "inbound_nodes": [[["dense_3", 0, 0, {}]]], "shared_object_id": 6}, {"class_name": "Dense", "config": {"name": "dense_4", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_4", "inbound_nodes": [[["activation_2", 0, 0, {}]]], "shared_object_id": 9}, {"class_name": "Activation", "config": {"name": "activation_3", "trainable": true, "dtype": "float32", "activation": "gelu"}, "name": "activation_3", "inbound_nodes": [[["dense_4", 0, 0, {}]]], "shared_object_id": 10}, {"class_name": "Dense", "config": {"name": "dense_5", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 11}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 12}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "name": "dense_5", "inbound_nodes": [[["activation_3", 0, 0, {}]]], "shared_object_id": 13}, {"class_name": "Activation", "config": {"name": "tokenizer", "trainable": true, "dtype": "float32", "activation": "tanh"}, "name": "tokenizer", "inbound_nodes": [[["dense_5", 0, 0, {}]]], "shared_object_id": 14}], "input_layers": [["input_2", 0, 0]], "output_layers": [["tokenizer", 0, 0]]}}}2
� root.layer-0"_tf_keras_input_layer*�{"class_name": "InputLayer", "name": "input_2", "dtype": "float32", "sparse": false, "ragged": false, "batch_input_shape": {"class_name": "__tuple__", "items": [null, 16, 24]}, "config": {"batch_input_shape": {"class_name": "__tuple__", "items": [null, 16, 24]}, "dtype": "float32", "sparse": false, "ragged": false, "name": "input_2"}}2
� root.layer-1"_tf_keras_layer*�{"name": "spatial_dropout1d_1", "trainable": true, "expects_training_arg": true, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "SpatialDropout1D", "config": {"name": "spatial_dropout1d_1", "trainable": true, "dtype": "float32", "rate": 0.0625, "noise_shape": null, "seed": null}, "inbound_nodes": [[["input_2", 0, 0, {}]]], "shared_object_id": 1, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": 3, "max_ndim": null, "min_ndim": null, "axes": {}}, "shared_object_id": 17}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 16, 24]}}2
� root.layer-2"_tf_keras_layer*�{"name": "flatten_1", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Flatten", "config": {"name": "flatten_1", "trainable": true, "dtype": "float32", "data_format": "channels_last"}, "inbound_nodes": [[["spatial_dropout1d_1", 0, 0, {}]]], "shared_object_id": 2, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 1, "axes": {}}, "shared_object_id": 18}}2
�root.layer_with_weights-0"_tf_keras_layer*�{"name": "dense_3", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_3", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 3}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 4}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["flatten_1", 0, 0, {}]]], "shared_object_id": 5, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 384}}, "shared_object_id": 19}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 384]}}2
� root.layer-4"_tf_keras_layer*�{"name": "activation_2", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Activation", "config": {"name": "activation_2", "trainable": true, "dtype": "float32", "activation": "gelu"}, "inbound_nodes": [[["dense_3", 0, 0, {}]]], "shared_object_id": 6}2
�root.layer_with_weights-1"_tf_keras_layer*�{"name": "dense_4", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_4", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 7}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 8}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["activation_2", 0, 0, {}]]], "shared_object_id": 9, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 256}}, "shared_object_id": 20}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 256]}}2
� root.layer-6"_tf_keras_layer*�{"name": "activation_3", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Activation", "config": {"name": "activation_3", "trainable": true, "dtype": "float32", "activation": "gelu"}, "inbound_nodes": [[["dense_4", 0, 0, {}]]], "shared_object_id": 10}2
�root.layer_with_weights-2"_tf_keras_layer*�{"name": "dense_5", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Dense", "config": {"name": "dense_5", "trainable": true, "dtype": "float32", "units": 256, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}, "shared_object_id": 11}, "bias_initializer": {"class_name": "Zeros", "config": {}, "shared_object_id": 12}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}, "inbound_nodes": [[["activation_3", 0, 0, {}]]], "shared_object_id": 13, "input_spec": {"class_name": "InputSpec", "config": {"dtype": null, "shape": null, "ndim": null, "max_ndim": null, "min_ndim": 2, "axes": {"-1": 256}}, "shared_object_id": 21}, "build_input_shape": {"class_name": "TensorShape", "items": [null, 256]}}2
�  root.layer-8"_tf_keras_layer*�{"name": "tokenizer", "trainable": true, "expects_training_arg": false, "dtype": "float32", "batch_input_shape": null, "stateful": false, "must_restore_from_config": false, "preserve_input_structure_in_config": false, "autocast": true, "class_name": "Activation", "config": {"name": "tokenizer", "trainable": true, "dtype": "float32", "activation": "tanh"}, "inbound_nodes": [[["dense_5", 0, 0, {}]]], "shared_object_id": 14}2
��root.keras_api.metrics.0"_tf_keras_metric*�{"class_name": "Mean", "name": "loss", "dtype": "float32", "config": {"name": "loss", "dtype": "float32"}, "shared_object_id": 22}2
Binary file added models/v1/tf/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file added models/v1/tf/variables/variables.index
Binary file not shown.
40 changes: 0 additions & 40 deletions notebooks/hello_world.ipynb

This file was deleted.

66 changes: 0 additions & 66 deletions notebooks/tpu_tutorial.ipynb

This file was deleted.

9 changes: 9 additions & 0 deletions retvecjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## local install

to use the local copy.

```
npm i
npm link
npm run build
```
3 changes: 3 additions & 0 deletions retvecjs/export_to_tfjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# convert model to tfjs
# need to install tensorflowjs - pip install tensorflowjs
tensorflowjs_converter ../notebooks/demo_models/emotions.keras models/v1/emotions/
90 changes: 90 additions & 0 deletions retvecjs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<html>
<head>
<script async src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.10.0/dist/tf.min.js" crossorigin="anonymous"></script>
<style>
.loading {
display: none;
}

#prev {
display:flex;
justify-content: center;
place-items: center;
}
</style>
</head>
<body>
<input class="input" type="input" placeholder="password" id="prev">

<div id="result"></div>
</body>

<script type="module">
import RetVec from "./dist/retvec.js"
var model = null;


const modelPath = "retvecjs/deeppass/model.json"

window.onload = loading

// trigger the model.
let prev = document.getElementById('prev')

async function loading() {
prev.classList.toggle("loading")
await RetVec.init("../v1/model/model.json")
//load the model in init
model = await tf.loadGraphModel(modelPath);
console.debug('model loaded');

//unhide the input field
prev.classList.toggle("loading")
}



async function classify(e) {

let word = prev.value;

let inputs = RetVec.binarizer(word);

inputs = tf.expandDims(inputs, 0) // make it a batch
let res = await model.predict(inputs)
//console.log(res.dataSync())
//res = tf.tensor(res).dataSync();
let compromised_proba = Math.round(res[0].dataSync() * 100)
let zxvbn_buckets = res[1].dataSync()
let zxvbn_bucket = tf.argMax(zxvbn_buckets).dataSync()
let zxvbn_proba = Math.round(zxvbn_buckets[zxvbn_bucket] * 100)


//
//let zxvbn_proba = res[0][1][zxvbn_bucket]

//let npass = pnew.value;

/*
console.log(ppass, npass);
let pemb = await embed(ppass);
let nemb = await embed(npass);
console.log(nemb, pemb)
distance = pemb.matMul(pemb, transposeB=true);
console.log(distance);
distance = tf.sum(distance);
// distance = 1 - distance?
*/

let output = `compromised proba:${compromised_proba}<br>
zxvbn: ${zxvbn_bucket} (${zxvbn_proba})
`
result.innerHTML = output;

}
prev.onchange = classify


</script>

</html>
Loading

0 comments on commit d15ea29

Please sign in to comment.