|
PyTorch はディープラーニングを実装する際に用いられるディープラーニング用ライブラリのPython APIの一つです。もともとは、Torch7と呼ばれるLua言語で書かれたライブラリでした。PyTorchは、このTorch7とPreferred Networks社のChainerをベースに2017年2月に作られたPython用ライブラリです。
ディープラーニング用の代表的なライブラリとしてはCaffe、TensorFlow、Chainerなどが有名です。KerasはTensorFlowやCaffeを使用しやすくするためのwrapperとなります。Chainerは日本のPreferred Networks社が開発したライブラリですが、国際的認知度ではマイナーな印象です。PyTorch は後発にもかかわらず急速に発達して、広範に活用され始めています。
PyTorchの利点はDefine by Run(動的計算グラフ)と呼ばれる特徴です。Define by Runは入力データのサイズや次元数に合わせてニューラルネットワークの形や計算方法を変更することができます。
一方で、TensorFlowの特徴はDefine and Run(静的計算グラフ)と呼ばれます。Define and Runではニューラルネットワークの計算方法をはじめに決めてしまうため、入力データの次元がデータごとに異なる状況に対応しづらいという特徴があります。
Keras+Tensorflowは見やすく簡易で、非常に簡単にネットワークを作成できるので、人工知能の専門家以外の人たちにとって、使いやすい必須の道具となっています。他方で、PyTorchは、Define by Runという特徴ゆえに、AIを開発する専門家に必須のアイテムになりつつあります。
このページでは非専門家の人たちを想定しますが、PyTorchはPython APIなので、Pythonの基礎的な知識を持っている人を想定します。なお、anaconda等でPythonのモジュール一式がインストールされていることも前提とします。deep-learningの理論的な説明は、例えば、3層パーセプトロン(MLP)や畳み込みニューラルネットワーク(CNN)の説明は、Deep Learningと人工知能のページにありますので、併せて読むと理解が深まると思います。
Last updated: 2018.6.16
conda install pytorch torchvision -c pytorch
from __future__ import print_function import torch x=torch.Tensor(5,3) print(x)
tensor([[ 0.0000e+00, -2.5244e-29, -4.7749e+03], [-8.5920e+09, 6.2369e-30, 1.4013e-45], [ 6.2836e-30, 1.4013e-45, 6.2836e-30], [ 1.4013e-45, 6.2778e-30, 1.4013e-45], [ 6.2821e-30, 1.4013e-45, 3.8576e-32]])
x = torch.empty(5, 3) print(x)
tensor([[ 0.0000e+00, -2.5244e-29, 0.0000e+00],
[-2.5244e-29, 5.6052e-45, 1.7180e+25],
[ 5.7738e+16, 4.5912e-41, 0.0000e+00],
[ 0.0000e+00, 0.0000e+00, 1.0951e+21],
[-5.7835e+03, -1.5849e+29, -5.785
x = torch.rand(5, 3) print(x)
tensor([[ 0.2561, 0.4928, 0.4191],
[ 0.0556, 0.4421, 0.0621],
[ 0.8881, 0.8851, 0.2572],
[ 0.0567, 0.2491, 0.2788],
[ 0.3648, 0.2398, 0.8449]])
y=torch.rand(5,3) print(x+y)
tensor([[ 0.9846, 1.3548, 0.9620],
[ 0.1496, 1.2262, 0.4178],
[ 1.2757, 1.3802, 0.8955],
[ 0.7388, 0.2780, 1.1868],
[ 0.9870, 0.7609, 1.4777]])
print(torch.add(x, y))
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 input image channel, 6 output channels, 5x5 square convolution
# kernel
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
# If the size is a square you can only specify a single number
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # all dimensions except the batch dimension
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
Net( (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1)) (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1)) (fc1): Linear(in_features=400, out_features=120, bias=True) (fc2): Linear(in_features=120, out_features=84, bias=True) (fc3): Linear(in_features=84, out_features=10, bias=True) )
params = list(net.parameters()) print(len(params)) print(params[0].size()) # conv1's .weight OUT: 10 torch.Size([6, 1, 5, 5])
wget https://pjreddie.com/media/files/yolov3.weights
python detect.py -h
python detect.py --images imgs --det det
Loading network..... Network successfully loaded dog.jpg predicted in 1.781 seconds Objects Detected: bicycle truck dog ---------------------------------------------------------- eagle.jpg predicted in 1.751 seconds Objects Detected: bird ---------------------------------------------------------- giraffe.jpg predicted in 1.784 seconds Objects Detected: zebra giraffe giraffe ---------------------------------------------------------- herd_of_horses.jpg predicted in 1.755 seconds Objects Detected: horse horse horse horse ---------------------------------------------------------- img1.jpg predicted in 1.847 seconds Objects Detected: person dog ---------------------------------------------------------- img2.jpg predicted in 1.842 seconds Objects Detected: train ---------------------------------------------------------- img3.jpg predicted in 1.817 seconds Objects Detected: car car car car car car car truck traffic light ---------------------------------------------------------- img4.jpg predicted in 1.768 seconds Objects Detected: chair chair chair clock ---------------------------------------------------------- messi.jpg predicted in 1.812 seconds Objects Detected: person person person sports ball ---------------------------------------------------------- person.jpg predicted in 1.850 seconds Objects Detected: person dog horse ---------------------------------------------------------- SUMMARY ---------------------------------------------------------- Task : Time Taken (in seconds) Reading addresses : 0.001 Loading batch : 3.065 Detection (11 images) : 20.594 Output Processing : 0.000 Drawing Boxes : 0.190 Average time_per_img : 2.168 ----------------------------------------------------------

$ python cam_demo.py FPS of the video is 0.24 FPS of the video is 0.42 FPS of the video is 0.56 FPS of the video is 0.69 FPS of the video is 0.78 ------