|
| 1 | +Custom Neural Network Tutorial |
| 2 | +============================== |
| 3 | + |
| 4 | +Rock Paper Scissors Hand Pose Classification |
| 5 | +-------------------------------------------- |
| 6 | + |
| 7 | +This tutorial will show you how to train and test a PyTorch based custom neural network model made using DFFML. |
| 8 | +The dataset we will be using is the `rock-paper-scissors-dataset <http://www.laurencemoroney.com/rock-paper-scissors-dataset/>`_ |
| 9 | +which contains images of hands in Rock/Paper/Scissors poses, each image is a 300x300 RGB image. |
| 10 | + |
| 11 | +The model we'll be using is :ref:`PyTorchNeuralNetwork <plugin_model_dffml_model_pytorch_pytorchnet>` |
| 12 | +which is a part of ``dffml-model-pytorch``, a DFFML plugin which allows you to use PyTorch |
| 13 | +via DFFML. We can install it with ``pip``. We will also be using image loading from |
| 14 | +``dffml-config-image`` and YAML file loading from ``dffml-config-yaml`` for creating our neural network. |
| 15 | + |
| 16 | +.. code-block:: console |
| 17 | +
|
| 18 | + $ pip install -U dffml-model-pytorch dffml-config-image dffml-config-yaml |
| 19 | +
|
| 20 | +Download the dataset and verify with with ``sha384sum``. |
| 21 | + |
| 22 | +.. literalinclude:: /../examples/rockpaperscissors/dataset.sh |
| 23 | + |
| 24 | +.. code-block:: console |
| 25 | +
|
| 26 | + rps.zip: OK |
| 27 | + rps-test-set.zip: OK |
| 28 | + rps-validation.zip: OK |
| 29 | +
|
| 30 | +Extract the datasets. |
| 31 | + |
| 32 | +.. code-block:: console |
| 33 | +
|
| 34 | + $ unzip rps.zip |
| 35 | + $ unzip rps-test-set.zip |
| 36 | + $ unzip rps-validation.zip -d rps-predict |
| 37 | +
|
| 38 | +The dataset for training the model will be in the `rps` directory. |
| 39 | +The dataset for testing the model will be in the `rps-test-set` directory. |
| 40 | +The images we will be using for prediction on the neural network will be in the `rps-predict` directory. |
| 41 | + |
| 42 | +Now that we have our dataset ready, we can perform classification of the hand poses to predict whether |
| 43 | +it is rock, paper or scissors! |
| 44 | + |
| 45 | +We first create the neural network. |
| 46 | + |
| 47 | +The neural network can be created in 2 ways using DFFML: |
| 48 | + 1. By creating a dictionary of layers in YAML or JSON format passing the file via CLI (eg. @model.yaml). |
| 49 | + 2. By using the torch module to create the model and passing an instance of the network to the model config. |
| 50 | + |
| 51 | + |
| 52 | +Command Line |
| 53 | +------------ |
| 54 | + |
| 55 | +We first create a YAML file to define the neural network with all the information about the layers along with |
| 56 | +the forward method which is passed as list of layers under the model name key: |
| 57 | + |
| 58 | +**model.yaml** |
| 59 | + |
| 60 | +.. literalinclude:: /../examples/rockpaperscissors/model.yaml |
| 61 | + |
| 62 | +.. seealso:: |
| 63 | + Sequential layers can also be created by indenting the layers under a key! |
| 64 | + The layers definied inside the Sequential layer can be used again while defining the forward method in the |
| 65 | + following syntax: `- key1.layer2` |
| 66 | + |
| 67 | +.. Note:: |
| 68 | + If the forward method is not given in the YAML file, forward method is automatically created by appending |
| 69 | + the top level layers sequentially. |
| 70 | + |
| 71 | +Train the model. |
| 72 | + |
| 73 | +.. literalinclude:: /../examples/rockpaperscissors/train.sh |
| 74 | + |
| 75 | +.. code-block:: console |
| 76 | +
|
| 77 | + INFO:dffml.PyTorchNeuralNetworkContext:Training complete in 1m 42s |
| 78 | + INFO:dffml.PyTorchNeuralNetworkContext:Best Validation Accuracy: 1.000000 |
| 79 | +
|
| 80 | +Assess the model's accuracy. |
| 81 | + |
| 82 | +.. literalinclude:: /../examples/rockpaperscissors/accuracy.sh |
| 83 | + |
| 84 | +The output is: |
| 85 | + |
| 86 | +.. code-block:: console |
| 87 | +
|
| 88 | + 0.8763440860215054 |
| 89 | +
|
| 90 | +Predict with the trained model. |
| 91 | + |
| 92 | +.. literalinclude:: /../examples/rockpaperscissors/predict.sh |
| 93 | + |
| 94 | +Some of the Predictions: |
| 95 | + |
| 96 | +.. literalinclude:: /../examples/rockpaperscissors/output.txt |
| 97 | + |
| 98 | + |
| 99 | +Python API |
| 100 | +---------- |
| 101 | + |
| 102 | +.. literalinclude:: /../examples/rockpaperscissors/python_example.py |
| 103 | + |
| 104 | +The output will be as follows: |
| 105 | + |
| 106 | +.. literalinclude:: /../examples/rockpaperscissors/python_output.txt |
| 107 | + |
| 108 | +The model predicts the hand poses correctly with great confidence! |
0 commit comments