Delphi Build An Artificial Intelligence Solutions With PyTorch Library In A Delphi Windows App

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
Build An Artificial Intelligence Solutions With PyTorch Library In A Delphi Windows App
By Muhammad Azizul Hakim June 11, 2021

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 Для просмотра ссылки Войди или Зарегистрируйся and Для просмотра ссылки Войди или Зарегистрируйся library, inside Для просмотра ссылки Войди или Зарегистрируйся.

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 button, and get the result in the upper Memo. You can find the Demo1 source on Для просмотра ссылки Войди или Зарегистрируйся. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.
1623417404467.png

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”).
Python:
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()
1623417469512.png
Pretty cool, right?

As performed with TensorFlow in our previous posts, 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:
1623417482729.png
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 Для просмотра ссылки Войди или Зарегистрируйся using the PyTorch library and Python4Delphi.

Check out the PyTorch library for Python and use it in your projects: Для просмотра ссылки Войди или Зарегистрируйся and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: Для просмотра ссылки Войди или Зарегистрируйся