Are you looking for an end-to-end open-source machine learning and deep learning platform, and build a nice GUI for them? You can deliver enterprise-grade AI solutions easily by combining PyTorch and Python4Delphi library, inside Delphi and C++Builder.
PyTorch is an open-source machine learning library based on the torch
library, used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab (FAIR). It is free and open-source software released under the Modified BSD license. Although the Python interface is more polished and the primary focus of development, PyTorch also has a C++ interface.
A number of pieces of deep learning software are built on top of PyTorch, including Tesla Autopilot, Uber’s Pyro, HuggingFace’s Transformers, PyTorch Lightning, and Catalyst.
PyTorch provides two high-level features:
- Tensor computing (like NumPy) with strong acceleration via graphics processing units (GPU)
- Deep neural networks built on a tape-based automatic differentiation system
You can reuse your favorite Python packages such as NumPy, SciPy, and Cython to extend PyTorch when needed.
This post will guide you on how to run the PyTorch library and use Python for Delphi to display it in the Delphi Windows GUI app.
First, open and run our Python GUI using project Demo1
from Python4Delphi with RAD Studio. Then insert the script into the lower Memo
, click the Execute script
button, and get the result in the upper Memo. You can find the Demo1
source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Table of Contents
Demo
Let’s try to load and visualize the Fashion-MNIST dataset from TorchVision. Fashion-MNIST is a dataset of Zalando’s article images consisting of 60,000 training examples and 10,000 test examples. Each example comprises a 28×28 grayscale image and an associated label from one of 10 classes (0
: “T-Shirt
“, 1
: “Trouser
“, 2
: “Pullover
“, 3
: “Dress
“, 4
: “Coat
“, 5
: “Sandal
“, 6
: “Shirt
“, 7
: “Sneaker
“, 8
: “Bag
“, 9
: “Ankle Boot
“).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import torch from torch.utils.data import Dataset from torchvision import datasets from torchvision.transforms import ToTensor, Lambda import matplotlib.pyplot as plt training_data = datasets.FashionMNIST( root="data", train=True, download=True, transform=ToTensor() ) test_data = datasets.FashionMNIST( root="data", train=False, download=True, transform=ToTensor() ) labels_map = { 0: "T-Shirt", 1: "Trouser", 2: "Pullover", 3: "Dress", 4: "Coat", 5: "Sandal", 6: "Shirt", 7: "Sneaker", 8: "Bag", 9: "Ankle Boot", } figure = plt.figure(figsize=(8, 8)) cols, rows = 3, 3 for i in range(1, cols * rows + 1): sample_idx = torch.randint(len(training_data), size=(1,)).item() img, label = training_data[sample_idx] figure.add_subplot(rows, cols, i) plt.title(labels_map[label]) plt.axis("off") plt.imshow(img.squeeze(), cmap="gray") plt.show() |
Pretty cool, right?
As performed with TensorFlow in our previous post, the performance of PyTorch combined with Python4Delphi is much faster compared with regular Python!
See the long iterations of the exact same code, performed in IDE without integrations with Python4Delphi:
Congratulations, now you have learned how to run the PyTorch library and use Python for Delphi to display it in the Delphi Windows GUI app! Now you can try lots more sophisticated examples from these documentations using the PyTorch library and Python4Delphi.
Check out the PyTorch
library for Python and use it in your projects: https://pypi.org/project/torch/ and
Check out Python4Delphi
which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi
References & further readings
[1] PyTorch Documentation. (2023). Datasets & dataloaders. The PyTorch Foundation, The Linux Foundation. pytorch.org/tutorials/beginner/basics/data_tutorial.html
[2] PyTorch Documentation. (2023). Get started: Start locally. The PyTorch Foundation, The Linux Foundation. pytorch.org/get-started/locally
[3] Xiao, H., Rasul, K., & Vollgraf, R. (2017). Fashion-mnist: a novel image dataset for benchmarking machine learning algorithms. arXiv preprint arXiv:1708.07747.
[4] Zalando Research. (2022). Fashion-MNIST. GitHub repository. github.com/zalandoresearch/fashion-mnist