how to use predict in tensorflow js - tensorflow

I made a model in python and exported it to tensorflow js.
The model is classifying facial images to emotions, and there should be 7 categories.
I made a tensor from the image and used predict like so:
const prediction = model.predict(imageTensor);
my prediction is :
Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 415,
"isDisposedInternal": false,
"kept": false,
"rankType": "2",
"scopeId": 197,
"shape": Array [
1,
7,
],
"size": 7,
"strides": Array [
7,
],
}
how do I extract the result from here?

Prediction is a tensor. If you want to access the data, you need to use prediction.dataSync()

found the answer, I was using typescript and it was needed:
const prediction = (model.predict(
imageTensor
) as tf.Tensor).dataSync();

Related

tf.dataSync() does not return tensor from BlazeFaceModel in a readable form

I am using BlazeFaceModel to detect faces before sending the faces to another model using Tensorflow.js
When I am using a custom model and trying to get the tensor output I used the code below and it worked at returning the tensors.
const returnTensors = true;
const faces = await blazeModel.estimateFaces(tensor, returnTensors);
if (faces !== null) {
// Download the tensors to view the shape
const face = faces.dataSync();
face.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});
}
But it throws the following error when applying on the tensor output from BlazeFaceModel:
faces.dataSync is not a function. (In 'faces.dataSync()', 'faces.dataSync' is undefined)
Output from console.log(faces)
Array [
Object {
"bottomRight": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 60793,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 116528,
"shape": Array [
2,
],
"size": 2,
"strides": Array [],
},
"landmarks": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 60795,
"isDisposedInternal": false,
"kept": false,
"rankType": "2",
"scopeId": 116532,
"shape": Array [
6,
2,
],
"size": 12,
"strides": Array [
2,
],
},
"probability": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 60785,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 116495,
"shape": Array [
1,
],
"size": 1,
"strides": Array [],
},
"topLeft": Tensor {
"dataId": Object {},
"dtype": "float32",
"id": 60792,
"isDisposedInternal": false,
"kept": false,
"rankType": "1",
"scopeId": 116526,
"shape": Array [
2,
],
"size": 2,
"strides": Array [],
},
},
]
faces is not a tensor. It is an array of json with key values where the values are tensor. If you would like to get all the tensors at once in an array, Object.values(faces[0]) can be used
tensors = Object.values(faces[0]) // array of tensor
tensors.map(t => t.dataSync()) // download the value of the tensor to a js array
// alternatively they can all be converted to a big tensor before using only once dataSync()

In tensorflow, is it possible to see another models build structure?

For example, if I load somebody else's model, this is what I see:
I want to get the code representation of this, for example:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
... etc
Not saying that the above is correct, but I want to know if there a way for me to physically reconstruct the model in code, including all activation functions?
I guess I can read the summary, but I don't know if I will be able to determine activation from it.
How would one do this?
If you have a model saved with complete architecture along with its training states. ie u have used something like this.
model.save('myfirstmodel.h5')
You can use
pprint(model.to_json())
pprint(model.to_yaml())
Output for json:
('{"class_name": "Sequential", "config": {"name": "sequential", "layers": '
'[{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 13], '
'"dtype": "float32", "sparse": false, "ragged": false, "name": "d1_input"}}, '
'{"class_name": "Dense", "config": {"name": "d1", "trainable": true, '
'"batch_input_shape": [null, 13], "dtype": "float32", "units": 4, '
'"activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": '
'"Ones", "config": {}}, "bias_initializer": {"class_name": "Zeros", "config": '
'{}}, "kernel_regularizer": null, "bias_regularizer": null, '
'"activity_regularizer": null, "kernel_constraint": null, "bias_constraint": '
'null}}, {"class_name": "Dense", "config": {"name": "d2", "trainable": true, '
'"dtype": "float32", "units": 6, "activation": "relu", "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}}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": '
'true, "dtype": "float32", "rate": 0.2, "noise_shape": null, "seed": null}}, '
'{"class_name": "Dense", "config": {"name": "out", "trainable": true, '
'"dtype": "float32", "units": 2, "activation": "sigmoid", "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}}]}, "keras_version": "2.4.0", "backend": "tensorflow"}')
However, if you have a frozen model in which your normal means dont work you can take a look at the structure of the model using netron.
It shows layer-wise architecture as well as which activation functions are used, parameter as well as its weights. You can download these weights as NumPy arrays.
You can use Netron in order to find the architecture of the model along with weights. Using this structural information you can rebuild your model.
See Link.
You get output like this:
You can use model.get_config() to get a dict of all the information you need to re-instantiate an identical model (at least in theory, since this depends on each layer in the model having a correct get_config method itself, which historically hasn't always been the case).
If your imported model is well-behaved, then you might even be able to create a clone model just by doing
new_model = tf.keras.Model.from_config(model.get_config())
This won't always work, so in general, you can just start with model.get_config() to inspect the details of a model.
The answer is NO. There is no way in tf or Keras to get code representation from model file or YAML or JSON. But yes, you can write a piece of code which will do this job. It should be pretty easy. If you save your model like the following:
model.to_yaml()
It saves your model configuration along with the activation functions. All you need to do it to go through the layers one by one and add the code representation of the layer. But loss function can be an issue for you.
Btw, if you want to retrain the saved model, you don't need the code structure. All you can is just load and train. Just don't freeze any layers. It will retrain and update all the path weights.

which algorithm is applicable to predict multiple output values using "machine learning" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to predict values four target numeric variables, using machine learning, I am very new to machine learning concepts,
Please help me out to create a model for the below mentioned dataset.
Please suggest which approach to use in order to predict multiple values.
I seriously don't know how to start and where to start and which algorithm to use.
Here is my input data set and output data set.
Input Dataset
// Input dataset
{
"width":1000,
"height":500
"objects": [
{"left": 27.76, "top": 27.5, "width":671, "height": 197},
{"left": 312.2, "top": 154.27, "width":499, "height": 452},
{"left": 707, "top":41.3, "width":1000, "height":714}
]
},
{
"width":1000,
"height":500
"objects": [
{"left": 30.12, "top": 37.5, "width":721, "height": 217},
{"left": 360.2, "top": 160.27, "width":530, "height": 520},
{"left": 720, "top":60, "width":1200, "height":814}
]
},
{
"width":1000,
"height":500
"objects": [
{"left": 35.12, "top": 40.2, "width":721, "height": 217},
{"left": 370.2, "top": 170.27, "width":540, "height": 530},
{"left": 800, "top":90, "width":1250, "height":910}
]
}
Output Dataset
{
"width":1000,
"height":500
"objects": [
{"left": 40.27, "top": 30, "width":671, "height": 197},
{"left": 370, "top": 160, "width":499, "height": 452},
{"left": 750, "top":50.13, "width":1000, "height":714}
]
},
{
"width":1000,
"height":500
"objects": [
{"left": 35.15, "top": 47.3, "width":721, "height": 217},
{"left": 410, "top": 190, "width":530, "height": 520},
{"left": 650, "top":90, "width":1200, "height":814}
]
},
{
"width":1000,
"height":500
"objects": [
{"left": 45.12, "top": 45, "width":721, "height": 217},
{"left": 390, "top": 185, "width":540, "height": 530},
{"left": 820, "top":100, "width":1250, "height":910}
]
}
Your question is too generic. So, I will take the liberty to answer it theoretically.
Let's say you are talking about prediction of a variable in the dataset. So, the first thing you need to do is to have a prepared dataset with all variables (conversion of categorical variables should be done) and derived variables can be added to dataset. Once the dataset is prepared, you need to create a training dataset and test dataset. On the training dataset you can create your model. Once the model is created you can evaluate the model using the test dataset to predict the variable you are interested in (eg Car Price in case of car dataset).
Now, some theoretical things: Basically, predictive analytics have 3 types of ML algorithms i.e Regression, Classification and Clustering. Depending on the need you need to select one of these. Regression is to predict a continous variable. Classification is to categorize the dataset by lables. Clustering is to identify unknown clusters.
In regression problem, there will be multiple Independent variables that will be used to predict the value of a dependent variable (Eg. Car price is predicted based mileage, car weight, height, length, horsepower etc). Here car price is a dependent variable and all other variables are Independent in nature.
Identify what you wish to do and then apply the concepts.

How to use Lucid Interpretability tools on a Tensorflow Detection model?

I want to use Lucid to analyze the feature extraction of a detection model I trained using the tensorflow Object Detection API on my own dataset. The model used is one from the Tensorflow Object Detection Zoo, namely faster_rcnn_resnet101.
I followed the Lucid tutorial to import my own model and saved a frozen graph of the model with the node /all_class_predictions_with_background as output_node.
I'm having trouble finding the input node of the graph to make Lucid run on it.
Furthermore I don't really think I have the right approach. Maybe I should first extract all the classification part of the detection model and freeze a new graph with only this part before going to Lucid.
Or maybe I should just import a resnet_101 classification model and copy/paste the correct weights from the detection model on it?
But I don't really know how to do those kind of things.
Can someone help me? I really want to try running Lucid on my detection network.
Yes, you should export an inference (frozen) graph to work with in Lucid.
I use the following script to export a graph from the training checkpoint files.
Useful information about the nodes in the exported file is logged to the console.
training_model="ssd_mnet_v2_ppn_512x288.config"
model_signature="eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14_200k"
# the specific checkpoint to export from
checkpoint_path="/TRAIN/models/model/train/model.ckpt-200000"
# directory to export into
output_path="/XYZ/graphs/${model_signature}"
# ensure these graph nodes are exported, and everything in between
additional_output_tensor_names="Preprocessor/sub,concat_1"
#
python export_inference_graph.py \
--input_type=image_tensor \
--pipeline_config_path /TRAIN/models/model/$training_model \
--trained_checkpoint_prefix=$checkpoint_path \
--output_directory=$output_path \
--additional_output_tensor_names=$additional_output_tensor_names
I found it convenient to make my own Lucid Model class, after reviewing the examples in the Lucid model zoo.
You have to examine your graph carefully as you need to specify the input node, and provide a list of layers that Lucid can work with.
from lucid.modelzoo.vision_base import Model, _layers_from_list_of_dicts
# the input node "Preprocessor/sub" is appropriate for image injection
class SSD_Mnet2_PPN( Model ):
def __init__(self, image_shape=None, graph_path=None, labels_path=None ):
self.model_path = graph_path
self.labels_path = labels_path
self.image_shape = image_shape
self.image_value_range = (-1, 1)
self.input_name = "Preprocessor/sub"
super().__init__()
# a hand-crafted list of layers - by inspection of the graph
SSD_Mnet2_PPN.layers = _layers_from_list_of_dicts(SSD_Mnet2_PPN, [
{ 'id': 0, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_2/add', 'depth': 24, 'shape': [ 1, 72, 128, 24 ], 'transform_id': 2 },
{ 'id': 2, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_5/add', 'depth': 32, 'shape': [ 1, 36, 64, 32 ], 'transform_id': 2 },
{ 'id': 5, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_9/add', 'depth': 64, 'shape': [ 1, 18, 32, 64 ], 'transform_id': 2 },
{ 'id': 7, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_12/add', 'depth': 96, 'shape': [ 1, 18, 32, 96 ], 'transform_id': 2 },
{ 'id': 9, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_15/add', 'depth': 160, 'shape': [ 1, 9, 16, 160 ], 'transform_id': 2 },
{ 'id': 11, 'tags': ['concat'], 'name': 'concat_1', 'depth': 13, 'shape': [ 1, 1212, 13 ], 'transform_id': 4 },
])
def model_for_version( version=None, path=None ):
if "320x180" in version:
return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 320, 180, 3 ] )
if "480x270" in version:
return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 480, 270, 3 ] )
if "512x288" in version:
return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 512, 288, 3 ] )
if "720x405" in version:
return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 720, 405, 3 ] )
raise ValueError( "No model for graph_version: {}".format( version ) )
Then you can write code as follows:
from lucid.optvis import render
model = model_for_version(
version = "eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14",
path = "/XYZ/graphs/eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14_200k/frozen_inference_graph.pb"
)
model.load_graphdef()
_ = render.render_vis( model, "FeatureExtractor/MobilenetV2/expanded_conv_15/add:17", thresholds=( 32, 256, 1024 ) )
Inevitably, one has to experiment quite a bit.

Python folium GeoJSON map not displaying

I'm trying to use a combination of geopandas, Pandas and Folium to create a polygon map that I can embed incorporate into a web page.
For some reason, it's not displaying.
The steps I've taken:
Grabbed a .shp from the UK's OS for Parliamentary boundaries.
I've then used geopandas to change the projection to epsg=4326 and then exported as GeoJSON which takes the following format:
{ "type": "Feature", "properties": { "PCON13CD": "E14000532", "PCON13CDO": "A03", "PCON13NM": "Altrincham and Sale West" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -2.313999519326579, 53.357408280545918 ], [ -2.313941776174758, 53.358341455420039 ], [ -2.31519699483377, 53.359035664493433 ], [ -2.317953152796459, 53.359102954309151 ], [ -2.319855973429864, 53.358581917200119 ],... ] ] ] } },...
Then what I'd like to do is mesh this with a dataframe of constituencies in the following format, dty:
constituency count
0 Burton 667
1 Cannock Chase 595
2 Cheltenham 22
3 Cheshire East 2
4 Congleton 1
5 Derbyshire Dales 1
6 East Staffordshire 4
import folium
mapf = folium.Map(width=700, height=370, tiles = "Stamen Toner", zoom_start=8, location= ["53.0219392","-2.1597434"])
mapf.geo_json(geo_path="geo_json_shape2.json",
data_out="data.json",
data=dty,
columns=["constituency","count"],
key_on="feature.properties.PCON13NM.geometry.type.Polygon",
fill_color='PuRd',
fill_opacity=0.7,
line_opacity=0.2,
reset="True")
The output from mapf looks like:
mapf.json_data
{'../../Crime_data/staffs_data92.json': [{'Burton': 667,
'Cannock Chase': 595,
'Cheltenham': 22,
'Cheshire East': 2,
'Congleton': 1,
'Derbyshire Dales': 1,
'East Staffordshire': 4,
'Lichfield': 438,
'Newcastle-under-Lyme': 543,
'North Warwickshire': 1,
'Shropshire': 17,
'South Staffordshire': 358,
'Stafford': 623,
'Staffordshire Moorlands': 359,
'Stoke-on-Trent Central': 1053,
'Stoke-on-Trent North': 921,
'Stoke-on-Trent South': 766,
'Stone': 270,
'Tamworth': 600,
'Walsall': 1}]}
Although the mapf.create_map() function successfully creates a map, the polygons don't render.
What debugging steps should I take?
#elksie5000, Try mplleaflet it is extremely straightforward.
pip install mplleaflet
in Jupyter/Ipython notebook:
import mplleaflet
ax = geopandas_df.plot(column='variable_to_plot', scheme='QUANTILES', k=9, colormap='YlOrRd')
mplleaflet.show(fig=ax.figure)