Skip to content

Commit

Permalink
Export and interop update.
Browse files Browse the repository at this point in the history
- Separate calibration and export in helper function.
- Add a PTQ module in the Python API for calibration !
- Add qat sat example with the python API.
- Torch interop incomplete batch size are now handled.
- Add tiny ml networks to keras interop tests.
- Fix bug with multithread in `Histogram.cpp`
- Add tuto for stimuliprovider
- DeepNet name now follow the Python API convention
- Provider no longer load data to check data size.
  • Loading branch information
cmoineau committed May 13, 2022
2 parents f8cc080 + dde0a82 commit 24476fa
Show file tree
Hide file tree
Showing 41 changed files with 2,759 additions and 352 deletions.
Binary file added docs/_static/first_stimuli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/first_stimuli_fliped.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/_static/second_stimuli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/python_api/cells.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Cells are organize with the following logic :
.. figure:: ../_static/n2d2_cell_diagram.png
:alt: Cell class diagram

Block
~~~~~

.. autoclass:: n2d2.cells.Block
:members:
:inherited-members:


Sequence
~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/python_api/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ If you find an example not up to date, please consider leaving an issue here : h
.. toctree::
:maxdepth: 1

example/data_augmentation
example/performance_analysis
example/load_onnx
example/graph_manipulation
Expand Down
139 changes: 139 additions & 0 deletions docs/python_api/example/data_augmentation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
Data augmentation
=================

In this example, we will see how to use :py:class:`n2d2.provider.DataProvider` and :py:class:`n2d2.transform.Transformation` to load data and do some data augmentation.

You can find the full python script here :download:`data_augmentation.py</../python/examples/data_augmentation.py>`.

Preliminary
-----------


For this tutorial, we will use n2d2 for data augmentation, and numpy and matplotlib for the visualization.

We will create a method plot_tensor to save the generated images from an :py:class:`n2d2.Tensor`


.. code-block::
import n2d2
import matplotlib.pyplot as plt
def plot_tensor(tensor, path):
plt.imshow(tensor[0,0,:], cmap='gray', vmin=0, vmax=255)
plt.savefig(path)
Loading data
------------

We will begin by creating a :py:class:`n2d2.database.MNIST` driver to load the MNIST dataset.
We will then create a provider to get the images, we use a batch size of 1 to get only one image.


.. code-block::
database = n2d2.database.MNIST(data_path="/local/DATABASE/mnist", validation=0.1)
provider = n2d2.provider.DataProvider(database, [28, 28, 1], batch_size=1)
You can get the number of data per partition by using the method :py:meth:`n2d2.database.Database.get_partition_summary` which will print the paritionement of data.

.. code-block::
database.get_partition_summary()
**Output :**

.. testoutput::

Number of stimuli : 70000
Learn : 54000 stimuli (77.14%)
Test : 10000 stimuli (14.29%)
Validation : 6000 stimuli (8.57%)
Unpartitioned : 0 stimuli (0.0%)


To select which partition you want to read from you need to use the method :py:meth:`n2d2.provider.DataProvider.set_partition`

To read data from a :py:class:`n2d2.provider.DataProvider` you can use multiple methods.

You can use the methods :py:meth:`n2d2.provider.DataProvider.read_batch` or :py:meth:`n2d2.provider.DataProvider.read_random_batch`.


.. note::

Since :py:class:`n2d2.provider.DataProvider` is an `iterable`, so you can use the ``next()`` function or a for loop !

.. code-block::
# for loop example
for data in provider:
pass
# next example
data = next(provider)
For this tutorial we will use :py:meth:`n2d2.provider.DataProvider.read_batch` !

With this code we will get the first image and plot it :

.. code-block::
image = provider.read_batch(idx=0).to_numpy() * 255
plot_tensor(image, "first_stimuli.png")
.. figure:: /_static/first_stimuli.png
:alt: First stimuli of the MNIST dataset.

Data augmentation
-----------------

To do data augmentation with N2D2 we use :py:class:`n2d2.transform.Transformation`.
You can add transformation to provider with the method :py:meth:`n2d2.provider.DataProvider.add_on_the_fly_transformation` and :py:meth:`n2d2.provider.DataProvider.add_transformation`.

.. warning::

Since we already loaded the first image the method :py:meth:`n2d2.provider.DataProvider.add_transformation` would not apply the transformation to the image.

By using the transformation :py:class:`n2d2.transform.Flip` we will flip vertically our image.

.. code-block::
provider.add_on_the_fly_transformation(n2d2.transform.Flip(vertical_flip=True))
image = provider.read_batch(idx=0).to_numpy() * 255
plot_tensor(image, "first_stimuli_fliped.png")
.. figure:: /_static/first_stimuli_fliped.png
:alt: First stimuli of the MNIST dataset but flipped.

We will negate the first transformation with another :py:class:`n2d2.transform.Flip` which we will add with the method :py:meth:`n2d2.provider.DataProvider.add_transformation`.

.. code-block::
# negating the first transformation with another one
provider.add_transformation(n2d2.transform.Flip(vertical_flip=True))
image = provider.read_batch(idx=1).to_numpy() * 255
plot_tensor(image, "second_stimuli.png")
.. figure:: /_static/second_stimuli.png
:alt: Second stimuli of the MNIST dataset.


Getting labels
--------------

To get the labels

.. code-block::
print("Second stimuli label : ", provider.get_labels()[0])
**Output :**

.. testoutput::

Second stimuli label : 5
14 changes: 13 additions & 1 deletion docs/python_api/export.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ Example
DEEPNET_CELL,
nb_bits=8,
export_nb_stimuli_max=-1,
calibration=-1)
calibration=-1)
Export CPP TensorRT
-------------------

.. autofunction:: n2d2.export.export_tensor_rt

Example
^^^^^^^

.. code-block:: python
n2d2.export.export_tensor_rt(DEEPNET_CELL)
Loading

0 comments on commit 24476fa

Please sign in to comment.