Conversion of Datatypes in Python
Let’s take variable a, assign a specific value to it and check its datatype.
a = 100type(a)
You guessed it right. You get int as the output.
Let’s now check the size of the variable. We can use get that using getsizeof method from sys module of Python Standard Module.
getsizeof() method : Return the size of an object in bytes. The object can be any type of object.
Link : Click here
import syssys.getsizeof(a)
Output : 32
This means that it occupies 32 bytes in the memory
Similarly variable having float datatype occupies 24 bytes.
Ouput : 24
Let’s now go ahead with Numpy to get the size of Arrays in memory.
Output : in64 144
If we convert the above code into int32 datatype the memory size gets reduced.
We use the similar concept in Quantization of Neural Network models as well. Quantization for deep learning is the process of approximating a neural network that uses floating-point numbers by a neural network of low bit width numbers.
For a neural network, we need to get the best set of model parameters in the form of weights and bias. Quantization is very useful in deploying the ML models on the edge. It reduces both the memory requirement and computational cost of using neural networks.
The weights and bias values are considered in the form of 2D arrays in Numpy.
The following code picturizes the same.
We have float64 as the default datatype. Then it is converted to float32 and then to float16.
Generally the model parameters are in float32 datatype. The most commonly used conversion method is to convert it to float16.
Neural Compute Stick (VPU ) works only on FP16
The change in memory size is also reflected. The values inside each variable remains similar.
Output :
For float64 , the memory consumed in bytes is 32000120
For float32 , the memory consumed in bytes is 16000120
For float16 , the memory consumed in bytes is 8000120
The numbers highlighted in bold shows the memory size of each Numpy Array in bytes.
We can cross verify the same by saving every numpy array as .npy file type and check the size of it using du -sh <filename.npy> linux command
Checking the file size on Ubuntu
We get the size in bytes. Divide it by 1024 to get it in KB. Again divide it by 1024 to get it in MB