PyTorch is quickly becoming the hottest machine-learning. The following examples should get you started on basic PyTorch applications. It has several different applications, extending beyond its original purpose of developing machine learning algorithms. The following examples explore just of PyTorch's capabilities, including loading a pre-trained data-set, accessing GPU information, and parallel processing using a GPU.
Loading a Dataset
Machine learning algorithms can generate models that are then easily used by other applications. PyTorch, like other application interfaces, allows for quick loading of existing models. Though PyTorch comes with existing models, you may have to find models from other resources online. The following is an example of how to load the DarkNet dataset.
config_path='config/yolov3.cfg'
weights_path='config/yolov3.weights'
class_path='config/coco.names'
img_size=416
conf_thres=0.8
nms_thres=0.4
# load model and put into eval mode
model = Darknet(config_path, img_size=img_size)
model.load_weights(weights_path)
model.cuda()
model.eval()
Accessing the GPU
Accessing the GPU info is straightforward and well-defined under torch.cuda.
import torch #torch gives full access to
cuda.init() #initializer for CUDA
cuda.Device(0).name #returns the name of GPUs, 0 indicates the first of possibly many
Parallel Programming
Another powerful capability of PyTorch is the ease with which it grants a user access to parallel processing algorithms. Depending on the application, this can be a good alternative to using CUDA or some other GPGPU interface.
a = torch.DoubleTensor([1., 2.]) //create a tensor on the CPU
a = torch.FloatTensor([1., 2.]).cuda() //create a tensor on the GPU
Other Capabilities of PyTorch
PyTorch provides support for a host of different functions, including loading datasets from MNIST and ImageNet, as well as support for RNNs. These models are easy to load and can be applied to several different functions. Furthermore, the datatypes favor easy interfacing with OpenCV for a variety of programming applications.