How to change Yolov5 tflite model output to float32? - yolov5

Here You can see the StatefulPartitionedCall:3 gives the int32 but I need to have it all in float32, Is there any way to get 4 float32 tflite models from Yolov5?

You can use default quantization (Float32) of TFLIte converter to get float32 . You can also force output type to float32 using below command.
converter.inference_input_type = tf.float32
converter.inference_output_type = tf.float32
Attached relevant threads for reference.
Ref 1

Related

using special function such as __add__ in cython cdef classes

I am wanting to create a cython object that can has convenient operations such as addition multiplication and comparisons. But when I compile such classes they all seem to have a lot of python overhead.
A simple example:
%%cython -a
cdef class Pair:
cdef public:
int a
int b
def __init__(self, int a, int b):
self.a = a
self.b = b
def __add__(self, Pair other):
return Pair(self.a + other.a, self.b + other.b)
p1 = Pair(1, 2)
p2 = Pair(3, 4)
p3 = p1+p2
print(p3.a, p3.b)
But I end up getting quite large readouts from the annotated compiler
It seems like the __add__ function is converting objects from python floats to cython doubles and doing a bunch of type checking. Am I doing something wrong?
There's likely a couple of issues:
I'm assuming that you're using Cython 0.29.x (and not the newer Cython 3 alpha). See https://cython.readthedocs.io/en/stable/src/userguide/special_methods.html#arithmetic-methods
This means that you can’t rely on the first parameter of these methods being “self” or being the right type, and you should test the types of both operands before deciding what to do
It is likely treating self as untyped and thus accessing a and b as Python attributes.
The Cython 3 alpha treats special methods differently (see https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html#arithmetic-methods) so you could also consider upgrading to that.
Although the call to __init__ has C typed arguements it's still a Python call so you can't avoid boxing and unboxing the arguments to Python ints. You could avoid this call and do something like:
cdef Pair res = Pair.__new__()
res.a = ... # direct assignment to attribute

get weights for each layer of the model using OpenVINO API

I'd like to get the tensor of weights and biases for each layer of the network using the C++/Python API on the OpenVINO framework. I found this solution but unfortunately it uses an older API (release 2019 R3.1) that is no longer supported. For example, the class CNNNetReader does not exist in the latest OpenVINO. Does anyone know how to achieve this in new releases? (2020+)
Thanks!
Can you try something like this? First you need to read-in a model, the iterate over the nodes in the graph and filter the Constants (I'm making a copy here because the nodes are held as shared pointers). Then each Constant node can be transformed into a vector of values if you're looking for that. To do that you can use the templated method cast_vector<T> and you can figure out which type to use by checking what the Constant holds (using get_element_type).
const auto model = core.read_model("path");
std::vector<std::shared_ptr<ov::op::v0::Constant>> weights;
const auto ops = model->get_ops();
std::copy_if(std::begin(ops), std::end(ops), std::back_inserter(weights), [](const std::shared_ptr<ov::Node>& op) {
return std::dynamic_pointer_cast<ov::op::v0::Constant>(op) != nullptr;
});
for (const auto& w : weights) {
if (w->get_element_type() == ov::element::Type_t::f32) {
const std::vector<float> floats = w->cast_vector<float>();
} else if (w->get_element_type() == ov::element::Type_t::i32) {
const std::vector<int32_t> ints = w->cast_vector<int32_t>();
} else if (...) {
...
}
}
Use the class Core and the method read_model to read the model. Next, use the methods in the class Model to get the detail for each layer.
import openvino.runtime as ov
ie = ov.Core()
network = ie.read_model('face-detection-adas-0001.xml')
# print(network.get_ops())
# print(network.get_ordered_ops())
# print(network.get_output_op(0))
# print(network.get_result())
Refer to OpenVINO Python API and OpenVINO Runtime C++ API for more information.

Google OR Tools, "returned a result with an exception set" Error on my server

I am using ORTools for path optimization. When I try my code in localhost it returns the expected result. However, when I try it on my DigitalOcean App it gives an error message:" returned a result with an exception set". Can anyone help me to find a solution ?
Here is the TraceBack code from Postman if it is useful:
The above exception ('numpy.float64' object cannot be interpreted as an integer) was the direct cause of the following exception:
Local vars
/workspace/.heroku/python/lib/python3.10/site-packages/ortools/constraint_solver/pywrapcp.py, line 5730, in GetArcCostForVehicle
return _pywrapcp.RoutingModel_CostVar(self)
def GetArcCostForVehicle(self, from_index: "int64_t", to_index: "int64_t", vehicle: "int64_t") -> "int64_t":
r"""
Returns the cost of the transit arc between two nodes for a given vehicle.
Input are variable indices of node. This returns 0 if vehicle < 0.
"""
return _pywrapcp.RoutingModel_GetArcCostForVehicle(self, from_index, to_index, vehicle) …
Please check that all your transit callbacks are returning an int type.
In your code you must have registered a transit callback:
# Create and register a transit callback.
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
then you have passed this transit_callback_index to setup the arc cost evaluator function.
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
The point is, the function distance_callback() will be call internally by the C++ library (python is just a wrapper on top of the C++ library). The C++ expect a C++ int64_t returned by the distance_callback() wrapper function (i.e. the python-C++ bridge need an int from the python method to be able to convert it to a C++ int64_t).
If your method return a numpy.float64 (e.g. because you r distance matrix data are an numpy array full of floating point values) then everything blow up thus the error message...

thrust transform defining custom binary function

I am trying to write a custom function to carry out sum. I followed this question Cuda Thrust Custom function to take reference.Here is how I have defined my functor
struct hashElem
{
int freq;
int error;
};
//basically this function adds some value to to the error field of each element
struct hashErrorAdd{
const int error;
hashErrorAdd(int _error): error(_error){}
__host__ __device__
struct hashElem operator()(const hashElem& o1,const int& o2)
{
struct hashElem o3;
o3.freq = o1.freq;
o3.error = o1.error + (NUM_OF_HASH_TABLE-o2)*error; //NUM_OF_HASH_TABLE is a constant
return o3;
}
};
struct hashElem freqError[SIZE_OF_HASH_TABLE*NUM_OF_HASH_TABLE];
int count[SIZE_OF_HASH_TABLE*NUM_OF_HASH_TABLE];
thrust::device_ptr<struct hashElem> d_freqError(freqError);
thrust::device_ptr<int> d_count(count);
thrust::transform(thrust::device,d_freqError,d_freqError+new_length,d_count,hashErrorAdd(perThreadLoad)); //new_length is a constant
This code on compilation gives the following error:
error: function "hashErrorAdd::operator()" cannot be called with the given argument list
argument types are: (hashElem)
object type is: hashErrorAdd
Please can anybody explain to me why I am getting this error? and how I can resolve it. Please comment in case I am not able to explain the problem clearly. Thankyou.
It appears that you want to pass two input vectors to thrust::transform and then do an in-place transform (i.e. no output vector is specified).
There is no such incarnation of thrust::transform
Since you have passed:
thrust::transform(vector_first, vector_last, vector_first, operator);
The closest matching prototype is a version of transform that takes one input vector and creates one output vector. In that case, you would need to pass a unary op that takes the input vector type (hashElem) only as an argument, and returns a type appropriate for the output vector, which is int in this case, i.e. as you have written it (not as your intent). Your operator() does not do that, and it cannot be called with the arguments that thrust is expecting to pass to it.
As I see it, you have a couple options:
You could switch to the version of transform that takes two input vectors and produces one output vector, and create a binary op as functor.
You could zip together your two input vectors, and do an in-place transform if that is what you want. Your functor would then be a unary op, but it would take as argument whatever tuple was created from dereferencing the input vector, and it would have to return or modify the same kind of tuple.
As an aside, your method of creating device pointers directly from host arrays looks broken to me. You may wish to review the thrust quick start guide.

Why can't cython memory views be pickled?

I have a cython module that uses memoryview arrays, that is...
double[:,:] foo
I want to run this module in parallel using multiprocessing. However I get the error:
PicklingError: Can't pickle <type 'tile_class._memoryviewslice'>: attribute lookup tile_class._memoryviewslice failed
Why can't I pickle a memory view and what can I do about it.
Maybe passing the actual array instead of the memory view can solve your problem.
If you want to execute a function in parallel, all of it parameters have to be picklable if i recall correctly. At least that is the case with python multiprocessing. So you could pass the array to the function and create the memoryview inside your function.
def some_function(matrix_as_array):
cdef double[:,:] matrix = matrix_as_array
...
I don't know if this helps you, but I encountered a similar problem. I use a memoryview as an attribute in a cdef class. I had to write my own __reduce__ and __setstate__ methods to correctly unpickle instances of my class. Pickling the memory view as an array by using numpy.asarray and restoring it in __setstate__ worked for me. A reduced version of my code:
import numpy as np
cdef class Foo:
cdef double[:,:] matrix
def __init__(self, matrix):
'''Assign a passed array to the typed memory view.'''
self.matrix = matrix
def __reduce__(self):
'''Define how instances of Foo are pickled.'''
d=dict()
d['matrix'] = np.asarray(self.matrix)
return (Foo, (d['matrix'],), d)
def __setstate__(self, d):
'''Define how instances of Foo are restored.'''
self.matrix = d['matrix']
Note that __reduce__ returns a tuple consisting of a callable (Foo), a tuple of parameters for that callable (i.e. what is needed to create a 'new' Foo instance, in this case the saved matrix) and the dictionary with all values needed to restore the instance.