Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📖 Quick Start Pytorch Components & Workflows #177

Merged
merged 9 commits into from
Jul 27, 2022
Merged

Conversation

MFA-X-AI
Copy link
Member

@MFA-X-AI MFA-X-AI commented Jul 22, 2022

Description

With the upcoming 1,5 release, we're looking forward to improve the new user onboarding. This PR introduces a new set of pytorch components based on the official pytorch tutorials. Most of the components are designed to emulate commonly used modules such as torch utils Dataset and DataLoader, while the rest such as TorchModel are easily modifiable.

List of new components:

  1. LoadTorchVisionDataset
  2. TorchDataLoader
  3. TorchModel
  4. TrainTorchModel
  5. TestTorchModel
  6. SaveTorchModelState
  7. LoadTorchModelState
  8. TorchModelPredict
  9. Image2TorchTensor
  10. TorchModelPredictFromTensor

I have also created 2 workflows:

  • TorchTrainModel.xircuits: Trains a fashion mnist model, and saves the trained torch model state.
  • TrainPredictFromModel.xircuits: Loads a saved torch model state and performs inference.

image

All components are complete with their descriptions.

Due to it's specific use case, the previous pytorch components and workflows are migrated to it's own Xircuits project template - https://github.com/XpressAI/template-semantic_segmentation

References

#68

Pull Request Type

  • Xircuits Core (Jupyterlab Related changes)
  • Xircuits Canvas (Custom RD Related changes)
  • Xircuits Component Library
  • Testing Automation
  • Documentation
  • Others (Please Specify)

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Tests

  1. Setup

    1. Make venv
      python -m venv venv && source venv/Scripts/activate
    2. After extracting the zip file
      pip install xircuits*.whl
    3. To fetch the new torch components:
      xircuits-components --branch fahreza/new-torch
    4. pip install the new torch components
      pip install -r xai_components/xai_pytorch/requirements.txt
    5. Launch Xircuits:
      xircuits
  2. Workflow Tests

    1. Run TorchTrainModel.xircuits. Did it successfully run?
    2. Run TrainPredictFromModel.xircuits. Did it successfully run?
  3. Grammar Check
    Check that the new components have reasonable text descriptions.

Tested on?

  • Windows
  • Linux Ubuntu
  • Centos
  • Mac
  • Others (State here -> xxx )

Notes

The prediction isn't great since the model accuracy is only 60%. Feel free to add your own if it gives the correct output.

@MFA-X-AI MFA-X-AI requested a review from yuenherny July 25, 2022 01:45
@yuenherny
Copy link
Contributor

@MFA-X-AI Do you mean TorchPredictFromModel.xircuits instead of TrainPredictFromModel.xircuits?

image

@yuenherny
Copy link
Contributor

@MFA-X-AI Both examples TorchTrainModel.xircuits and TorchPredictFromModel.xircuits runs successfully and returns the following output, respectively:

======================================
__   __  ___                _ _
\ \  \ \/ (_)_ __ ___ _   _(_) |_ ___
 \ \  \  /| | '__/ __| | | | | __/ __|
 / /  /  \| | | | (__| |_| | | |_\__ \
/_/  /_/\_\_|_|  \___|\__,_|_|\__|___/

======================================

Xircuits is running...

D:\XpressAI-Repos\xircuits\venv\lib\site-packages\tqdm\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Executing: LoadTorchVisionDataset
Downloading FashionMNIST to D:\XpressAI-Repos\xircuits\data
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data\FashionMNIST\raw\train-images-idx3-ubyte.gz
100%|██████████| 26421880/26421880 [00:36<00:00, 731940.42it/s] 
Extracting data\FashionMNIST\raw\train-images-idx3-ubyte.gz to data\FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data\FashionMNIST\raw\train-labels-idx1-ubyte.gz
100%|██████████| 29515/29515 [00:00<00:00, 123685.05it/s]
Extracting data\FashionMNIST\raw\train-labels-idx1-ubyte.gz to data\FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data\FashionMNIST\raw\t10k-images-idx3-ubyte.gz
100%|██████████| 4422102/4422102 [00:02<00:00, 1529346.59it/s]
Extracting data\FashionMNIST\raw\t10k-images-idx3-ubyte.gz to data\FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data\FashionMNIST\raw\t10k-labels-idx1-ubyte.gz
100%|██████████| 5148/5148 [00:00<?, ?it/s]
Extracting data\FashionMNIST\raw\t10k-labels-idx1-ubyte.gz to data\FashionMNIST\raw


Executing: TorchDataLoader
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64

Executing: TorchModel
Using cpu device
NeuralNetwork(
  (flatten): Flatten(start_dim=1, end_dim=-1)
  (linear_relu_stack): Sequential(
    (0): Linear(in_features=784, out_features=512, bias=True)
    (1): ReLU()
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): ReLU()
    (4): Linear(in_features=512, out_features=10, bias=True)
  )
)

Executing: TrainTorchModel
Using cpu device

Epoch 1
-------------------------------
loss: 2.299560  [    0/60000]
loss: 2.291411  [ 6400/60000]
loss: 2.260286  [12800/60000]
loss: 2.255728  [19200/60000]
loss: 2.251524  [25600/60000]
loss: 2.203349  [32000/60000]
loss: 2.225282  [38400/60000]
loss: 2.177551  [44800/60000]
loss: 2.177159  [51200/60000]
loss: 2.142596  [57600/60000]

Epoch 2
-------------------------------
loss: 2.148282  [    0/60000]
loss: 2.137422  [ 6400/60000]
loss: 2.062230  [12800/60000]
loss: 2.084925  [19200/60000]
loss: 2.036381  [25600/60000]
loss: 1.968870  [32000/60000]
loss: 2.005548  [38400/60000]
loss: 1.909899  [44800/60000]
loss: 1.918659  [51200/60000]
loss: 1.851401  [57600/60000]

Epoch 3
-------------------------------
loss: 1.876785  [    0/60000]
loss: 1.844696  [ 6400/60000]
loss: 1.710771  [12800/60000]
loss: 1.766611  [19200/60000]
loss: 1.657800  [25600/60000]
loss: 1.613809  [32000/60000]
loss: 1.643544  [38400/60000]
loss: 1.535521  [44800/60000]
loss: 1.561128  [51200/60000]
loss: 1.464803  [57600/60000]

Executing: TestTorchModel
Using cpu device
Test Error: 
 Accuracy: 60.8%, Avg loss: 1.477669 


Executing: SaveTorchModelState
Saved PyTorch Model State to examples/TorchTrainModel.pth

Finish Executing

Then,

======================================
__   __  ___                _ _
\ \  \ \/ (_)_ __ ___ _   _(_) |_ ___
 \ \  \  /| | '__/ __| | | | | __/ __|
 / /  /  \| | | | (__| |_| | | |_\__ \
/_/  /_/\_\_|_|  \___|\__,_|_|\__|___/

======================================

Xircuits is running...

D:\XpressAI-Repos\xircuits\venv\lib\site-packages\tqdm\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Executing: Image2TorchTensor
Size of the Original image:  (1078, 1280)
Size of the image after resize:  (28, 28)
Size of the tensor:  torch.Size([1, 28, 28])

Executing: TorchModel
Using cpu device
NeuralNetwork(
  (flatten): Flatten(start_dim=1, end_dim=-1)
  (linear_relu_stack): Sequential(
    (0): Linear(in_features=784, out_features=512, bias=True)
    (1): ReLU()
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): ReLU()
    (4): Linear(in_features=512, out_features=10, bias=True)
  )
)

Executing: LoadTorchModelState
Using cpu device

Executing: TorchModelPredictFromTensor
Using cpu device
Predicted: "Coat"

Finish Executing

Looks fine, IMO!

Copy link
Contributor

@yuenherny yuenherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both items in Test runs successfully on my local - Python 3.10.5 in this case. Please do refer to my comment for more details.

@MFA-X-AI MFA-X-AI merged commit 0e75e37 into master Jul 27, 2022
@MFA-X-AI MFA-X-AI deleted the fahreza/new-torch branch July 27, 2022 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants