Calculation operations with the parameters of a TFLite quantized model - tensorflow

I am trying to implement image classification in hardware using the quantized Mobilenetv2 model taken from here. To do that, I first need to reproduce the inference process from the beginning to the end to make sure I understand the calculations/operations that are performed on the data.
The first target is the Conv fuction. I can see how it is being calculated, but there are several arguments that are passed to this function which I would like to know how they are produced: output_offset, output_multiplier,output_shift, output_activation_min, output_activation_max. I cannot find the previous function that calls the Conv() function with these parameters. This would hopefully give me an insight of how these arguments are generated. Could someone point me to the right line of the source code?
Another gap in the sourcecode is at the interpreter.invoke() function. I wish to track and see what happens next, but can not find the soursecode that implements the invoke() function. The help would be greatly appreciated!

If you want to know how the conv reference code is used you can read the code for the conv operator.
The python interpreter uses swig to call the C++ intepreter.
Hope this helps.

Related

Where can I find exactly how Tensorflow does matrix multiplication?

For example, I want to do a matrix multiplication, and in doing so, I use the tf.matmul operation inside the tensorflow. And, i want to optimize matrix mulptiplication in tf. However, I cannot reach where the matrix Multiplication is made exactly in tf_matmul. Is there any people who can help me to do this ?
We need to do some code tracing to figure out what is being called and what is happening
1) tensorflow.python.ops.math_ops called via tf.matmul
2) tf.matmul returns either a sparse_matmul (which calls gen_math_ops.sparse_matmul) or gen_math_ops.batch_mat_mul
3) The gen_math_ops script is automatically generated but the underlying code is math_ops.cc
All the best!

Pytorch register_hook to Keras implementation

Im trying to implement the following project into Tensorflow/Keras.
https://github.com/jacobgil/pytorch-pruning
Im having a hard time understanding what register_hook does? It can be found in finetune.py, row 66.
x.register_hook(self.compute_rank)
I've searched for clear explanations regarding this function and tried to find Keras-equivalents, without any luck. Do you have answers to these questions?
First things first, here's the documentation:
http://pytorch.org/docs/master/autograd.html#torch.autograd.Variable.register_hook
This allows you to register a method to a Variable that is called whenever the Variable's .grad is updated, i.e. in a backward pass, and takes the grad as input. The method can return a Variable that would replace the original .grad or None if you just want to read the gradients to do something else.
If you update the gradients this way, the nodes further down in the compute graph see the new updated gradient in the backward pass and will have their respective gradients calculated with the updated value.
I'm not a Tensorflow expert, but the RegisterGradient decorators (documentation) seem to be able to do the same, for an example see this answer.

Custom op backward

I am writing a custom op, and I got stucked when writing the backward part.
When I call out_grad[0].asnumpy() or do any manipulation of the out_grad, the program crash without any error message.
I tried fill the in_grad with zeros, the program run smoothly, but I need the grad to flow backward.
def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
self.assign(in_grad[0], req[0], 0)
self.assign(in_grad[1], req[1], 0)
What's going wrong here?
Custom Operator in MXNet show us how to define a loss function using custom op. The loss op is very special because it doesn't need grad to be flow into.
But in my situation, I need grad to flow into my op. So, the function below should return the dependency instead of empty as in loss op.
def declare_backward_dependency(self, out_grad, in_data, out_data):
return [out_grad[0]]
In my opinion, the dependency is some variable which the gradient should be delievered to.
Have you tried to follow the tutorial here for developing a
Custom Operator in MXNet.
If that does not help, provide your full code of the Custom operator along with some sample data and a simple model with which this issue can be easily reproduced.

How can I plot a histogram of discrete distribution on tensorboard?

I'm using tensorboard (tensorflow 1.1.0) to show the result of my CNN classifier.
I added some output vector as tf.summary.histogram in order to show the counts of output in each bin, but tensorboard seems to automatically compute interpolation and show them as (somehow) smoothed distribution
(and therefore I can not find the exact counts for the bins).
Could someone tell me how can I avoid the interpolation and show usual histograms using bars?
I not sure that there is easy way to do it.
I very unsure in below text, correct me if I wrong.
From this file https://github.com/tensorflow/tensorboard/blob/master/tensorboard/plugins/histogram/vz_histogram_timeseries/index.html it seems that histogram comes to tensorboard in double values.
Summary op uses either histogram from https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/python/ops/histogram_ops.py (1) or https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/core/lib/histogram/histogram.cc (2)
I suppose that it uses 2nd because here https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/python/summary/summary.py#L189 it calls function from generated file. In my package code in this generated file there is another function call:
result = _op_def_lib.apply_op("HistogramSummary", tag=tag, values=values,
name=name)
I have grep all repo and seems like there is no other python code which define something with "HistogramSummary", so it seems like it's really defined here https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/core/kernels/summary_op.cc and this code uses code mentioned above (2).
So, it seems to me that histogram which is used now is buried deep inside of framework and I not sure that it's easy to rewrite it.
In this page there is email for support https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/summary . I suppose that it's better to contact this person or make issue on github.

Has anyone managed to make Asynchronous advantage actor critic work with Mujoco experiments?

I'm using an open source version of a3c implementation in Tensorflow which works reasonably well for atari 2600 experiments. However, when I modify the network for Mujoco, as outlined in the paper, the network refuses to learn anything meaningful. Has anyone managed to make any open source implementations of a3c work with continuous domain problems, for example mujoco?
I have done a continuous action of Pendulum and it works well.
Firstly, you will build your neural network and output mean (mu) and standard deviation (sigma) for selecting an action.
The essential part of the continuous action is to include a normal distribution. I'm using tensorflow, so the code is looks like:
normal_dist = tf.contrib.distributions.Normal(mu, sigma)
log_prob = normal_dist.log_prob(action)
exp_v = log_prob * td_error
entropy = normal_dist.entropy() # encourage exploration
exp_v = tf.reduce_sum(0.01 * entropy + exp_v)
actor_loss = -exp_v
When you wanna sample an action, use the function tensorflow gives:
sampled_action = normal_dist.sample(1)
The full code of Pendulum can be found in my Github. https://github.com/MorvanZhou/tutorials/blob/master/Reinforcement_learning_TUT/10_A3C/A3C_continuous_action.py
I was hung up on this for a long time, hopefully this helps someone in my shoes:
Advantage Actor-critic in discrete spaces is easy: if your actor does better than you expect, increase the probability of doing that move. If it does worse, decrease it.
In continuous spaces though, how do you do this? The entire vector your policy function outputs is your move -- if you are on-policy, and you do better than expected, there's no way of saying "let's output that action even more!" because you're already outputting exactly that vector.
That's where Morvan's answer comes into play. Instead of outputting just an action, you output a mean and a std-dev for each output-feature. To choose an action, you pass your inputs in to create a mean/stddev for each output-feature, and then sample each feature from this normal distribution.
If you do well, you adjust the weights of your policy network to change the mean/stddev to encourage this action. If you do poorly, you do the opposite.