Python采用 torch模块+CUDA测试YOLO_v3_tutorial_from_scratch-master实时目标检测

github地址:https://github.com/qqwweee/keras-yolo3

Pytorch 中,如果直接从 cuda 中取数据,如 var_tensor.cuda().data.numpy(),

import torch

var_tensor = torch.FloatTensor(2,3)
if torch.cuda.is_available():  # 判断 GPU 是否可用
    var_tensor.cuda().data.numpy()

则会出现如下类似错误:

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

其应该 var_tensor.cuda().data.cpu().numpy().

wu@wu-X555LF:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> var_tensor = torch.FloatTensor(2,3)
>>> if torch.cuda.is_available():  # 判断 GPU 是否可用
...     var_tensor.cuda().data.numpy()
... 

Traceback (most recent call last):
  File "", line 2, in 
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
>>>  
>>> if torch.cuda.is_available():  # 判断 GPU 是否可用
...     print(var_tensor.cuda().data.cpu().numpy())
... 
[[-2.4259018e-32  4.5680929e-41 -2.4259018e-32]
 [ 4.5680929e-41  4.4841551e-44  0.0000000e+00]]
>>> 

 TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

报错原因:numpy不能读取CUDA tensor 需要将它转化为 CPU tensor。

所以如果想把CUDA tensor格式的数据改成numpy时,需要先将其转换成cpu float-tensor随后再转到numpy格式

例如:报错行:

tcls[index, best_n, g_y_center, g_x_center, np.array(target[index, t, 0])] = 1

修改后:

tcls[index, best_n, g_y_center, g_x_center, np.array(target[index, t, 0].cpu())] = 1

 

Pytroch 涉及到 Variable,Tensor 和 Numpy 间的转换比较多,还会涉及到 cuda 到 cpu的转换.


1. Variable 转 Numpy

import torch
from torch.autograd import Variable

var = Variable(torch.FloatTensor(2,3))
# var = tensor(1.00000e-03 *
#       [[ 1.1476,  0.0000,  0.0000],
#        [ 0.0000,  0.0000,  0.0000]])
var_numpy = var.data.numpy()
# array([[1.1476139e-03, 4.5816855e-41, 3.7984453e-37],
#        [0.0000000e+00, 4.4841551e-44, 0.0000000e+00]], dtype=float32)

1.2 Numpy 转 Variable

import torch
from torch.autograd import Variable
import numpy as np

var_numpy = np.random.randn(2, 3)
# array([[-0.27443182,  1.18369008, -0.24645608],
#        [-0.99800364,  0.58202014, -0.84904032]])
var = Variable(torch.from_numpy(var_numpy))
# tensor([[-0.2744,  1.1837, -0.2465],
#         [-0.9980,  0.5820, -0.8490]], dtype=torch.float64)

1.3 Tensor 转 Numpy

import torch

var_tensor = torch.FloatTensor(2,3)
# tensor(1.00000e-03 *
#       [[ 1.1476,  0.0000,  1.1476],
#        [ 0.0000,  0.0000,  0.0000]])
var_numpy = var_tensor.numpy()
# array([[1.1476139e-03, 4.5816855e-41, 1.1476139e-03],
#        [4.5816855e-41, 4.4841551e-44, 0.0000000e+00]], dtype=float32)

1.4 Numpy 转 Tensor

import torch
import numpy as np

var_numpy = np.ones()
var_tensor = torch.from_numpy(var_numpy)

1.5 .cuda()

Pytorch 可以将内存中的模型和数据复制到 GPU 显存中,进行 GPU 计算.

import torch

torch.cuda.device_count() # 计算可用 GPU 数量

var_tensor = torch.FloatTensor(2,3)
if torch.cuda.is_available():  # 判断 GPU 是否可用
    var_tensor = var_tensor.cuda() # .cuda(device_id) 指定 GPU 上
    # tensor(1.00000e-03 *
    #        [[ 1.1476,  0.0000,  1.1476],
    #         [ 0.0000,  0.0000,  0.0000]], device='cuda:0')

    # model = model.cuda()

1.6 .cpu()

Pytorch 中,如果直接从 cuda 中取数据,如 var_tensor.cuda().data.numpy(),

import torch

var_tensor = torch.FloatTensor(2,3)
if torch.cuda.is_available():  # 判断 GPU 是否可用
    var_tensor.cuda().data.numpy()

则会出现如下类似错误:

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

其应该 var_tensor.cuda().data.cpu().numpy().

import torch

var_tensor = torch.FloatTensor(2,3)
if torch.cuda.is_available():  # 判断 GPU 是否可用
    print(var_tensor.cuda().data.cpu().numpy())
    # array([[1.1476139e-03, 4.5816855e-41, 1.1476139e-03],
    #        [4.5816855e-41, 4.4841551e-44, 0.0000000e+00]], dtype=float32)
 

只要在模型和参数后加.cuda()即可将模型由cpu上的运算调到gpu上运算。
首先需要确定自己的pytorch版本能否进行gpu计算。
print (torch.cuda.is_available())
如果结果是True,则可以进行gpu计算,如果是False,就需要安装gpu版本的torch或是CUDA了。还可以通过
print (torch.cuda.device_count())
来识别可以使用的gpu个数。
wu@wu-X555LF:~$ python
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print (torch.cuda.is_available())
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'torch' is not defined
>>> import torch
>>> print (torch.cuda.is_available())
True
>>> import torch
>>> print (torch.cuda.device_count())
1
>>> 
RuntimeError: expected type torch.cuda.FloatTensor but got torch.FloatTensor 

意思是要求的目标类型是torch.cuda.FloatTensor,但是找到的数据类型是torch.FloatTensor,所以需要在数据类型后面加上.cuda()

 

调试好后,对11张照片的检测数据:

Python采用 torch模块+CUDA测试YOLO_v3_tutorial_from_scratch-master实时目标检测_第1张图片

wu@wu-X555LF:~/YOLO_v3_tutorial_from_scratch-master$ python detect.py
Loading network.....
Network successfully loaded
detect.py:124: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  prediction = model(Variable(batch, volatile = True), CUDA)
/usr/local/lib/python2.7/dist-packages/torch/nn/modules/upsampling.py:129: UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))
/usr/local/lib/python2.7/dist-packages/torch/nn/functional.py:2423: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
tensor([[ 27.8569,  25.5872, 403.7079, 338.0915,   0.7916,   0.9928,   0.0000],
        [ 59.0677,  21.8278, 412.1664, 341.9318,   0.7514,   0.9978,   0.0000],
        [ 21.3221,  36.3847, 409.6571, 382.4868,   0.9789,   0.9912,   0.0000],
        [ 50.8772,  28.8581, 417.2045, 389.4042,   0.9871,   0.9985,   0.0000],
        [ 68.7843, 193.3958, 217.9618, 310.4507,   0.9097,   0.9925,  16.0000],
        [ 61.3315, 188.5456, 222.4678, 311.8332,   0.7929,   0.9862,  16.0000],
        [ 15.2563,  54.2572, 411.7980, 408.0729,   0.7362,   0.8026,   0.0000],
        [ 54.1758,  58.4460, 412.5482, 402.1475,   0.8379,   0.9057,   0.0000]],
       device='cuda:0')
img1.jpg             predicted in  0.327 seconds
Objects Detected:    person dog
----------------------------------------------------------
tensor([[135.2782,  47.6728, 358.0490, 317.1448,   0.7971,   0.9998,  23.0000],
        [128.1152,  33.5257, 367.5708, 338.4106,   0.9968,   0.9998,  23.0000],
        [132.7019,  33.2024, 384.1996, 333.5505,   0.9584,   0.9996,  23.0000],
        [126.8189,  40.6234, 366.1057, 361.4030,   0.9984,   0.9992,  23.0000],
        [136.9678,  34.9452, 379.6520, 367.4395,   0.7453,   0.9991,  23.0000],
        [206.8016, 153.7453, 359.5540, 346.0767,   0.7613,   0.8244,  23.0000],
        [207.7154, 167.3423, 357.4759, 373.0155,   0.9987,   0.9467,  22.0000],
        [219.2162, 166.5169, 361.8505, 375.5834,   0.9886,   0.9686,  22.0000]],
       device='cuda:0')
giraffe.jpg          predicted in  0.311 seconds
Objects Detected:    zebra giraffe giraffe
----------------------------------------------------------
tensor([[134.2327, 291.7109, 224.3858, 405.3425,   0.7553,   0.9790,  56.0000],
        [210.5667, 225.6665, 260.5036, 339.7461,   0.6826,   0.9849,  56.0000],
        [213.4833, 231.0002, 258.8569, 361.4463,   0.8344,   0.9954,  56.0000],
        [  1.7477, 253.5884,  56.9126, 407.0457,   0.5734,   0.9109,  56.0000],
        [  1.9053, 269.1235,  56.5891, 414.2692,   0.5833,   0.9116,  56.0000],
        [136.3233, 287.8264, 209.5676, 407.5195,   0.7839,   0.9982,  56.0000],
        [141.7037, 286.0551, 213.6842, 410.0381,   0.6953,   0.9933,  56.0000],
        [135.9622, 292.6913, 207.6157, 415.9682,   0.7922,   0.9972,  56.0000],
        [142.8586, 291.1636, 213.9250, 420.5435,   0.7165,   0.9958,  56.0000]],
       device='cuda:0')
img4.jpg             predicted in  0.315 seconds
Objects Detected:    chair chair chair
----------------------------------------------------------
tensor([[257.3622, 114.1941, 396.3779, 325.9073,   0.9108,   0.9941,  17.0000],
        [117.9989,  92.8983, 185.8298, 367.7559,   0.9398,   1.0000,   0.0000],
        [251.6075, 134.6225, 384.9818, 340.5165,   0.9825,   0.9980,  17.0000],
        [256.6093, 134.8610, 395.6052, 337.2637,   0.9984,   0.9994,  17.0000],
        [ 38.1023, 256.5849, 133.5263, 343.9485,   0.9996,   0.9946,  16.0000],
        [ 45.4808, 256.3615, 152.4373, 344.7843,   0.7655,   0.9891,  16.0000],
        [122.6343,  84.8508, 180.0754, 356.1192,   0.9938,   0.9991,   0.0000],
        [122.9223,  84.3946, 179.9489, 379.0464,   0.9997,   1.0000,   0.0000],
        [122.0703, 103.4768, 180.9349, 380.2654,   0.9674,   0.9998,   0.0000]],
       device='cuda:0')
person.jpg           predicted in  0.313 seconds
Objects Detected:    person dog horse
----------------------------------------------------------
tensor([[ -5.0565,  -2.8967, 250.4529, 377.8716,   0.8012,   0.9959,   0.0000],
        [ 17.4476,   0.9274, 255.6442, 374.9525,   0.9433,   0.9989,   0.0000],
        [  4.0081,  17.3676, 246.1173, 401.0501,   0.9982,   0.9998,   0.0000],
        [ 19.6581,  19.9975, 251.0763, 398.3021,   0.9987,   1.0000,   0.0000],
        [ 20.1857,  19.4191, 322.2139, 398.0027,   0.9907,   0.9996,   0.0000],
        [202.8020,  40.4009, 359.5274, 397.7529,   0.8784,   0.9992,   0.0000],
        [219.4974,  39.6244, 367.0788, 397.1060,   0.5534,   0.9991,   0.0000],
        [ 18.1702,  45.6481, 257.3352, 416.0642,   0.7316,   0.9993,   0.0000],
        [202.5954,  79.1771, 360.6339, 404.0787,   0.9987,   1.0000,   0.0000],
        [182.9848,  69.9036, 374.6978, 411.2584,   0.9035,   0.9999,   0.0000],
        [216.0640,  72.5932, 368.9921, 411.4324,   0.9950,   1.0000,   0.0000],
        [193.6543,  67.6401, 396.2873, 411.4567,   0.7656,   0.9997,   0.0000],
        [384.7941, 272.7592, 413.6805, 391.6810,   0.9927,   0.9998,   0.0000],
        [382.7412, 274.8894, 414.2444, 389.7123,   0.9470,   0.9999,   0.0000],
        [387.1715, 275.6257, 414.7762, 389.0914,   0.9860,   0.9986,   0.0000],
        [386.7449, 270.2409, 414.4832, 393.2094,   0.7718,   0.9988,   0.0000],
        [385.1707, 282.0656, 413.3013, 393.4201,   0.7522,   0.9993,   0.0000],
        [382.9612, 279.7617, 414.2446, 394.8088,   0.6288,   0.9999,   0.0000],
        [388.4177, 280.3339, 414.0858, 395.7788,   0.9002,   0.9986,   0.0000]],
       device='cuda:0')
messi.jpg            predicted in  0.316 seconds
Objects Detected:    person person person
----------------------------------------------------------
tensor([[293.7799, 336.4542, 406.9822, 397.4315,   0.8936,   0.9718,   2.0000],
        [303.0222, 337.9891, 412.5237, 396.6951,   0.9989,   0.9939,   2.0000],
        [317.3988, 111.5369, 338.8864, 168.9404,   0.9511,   1.0000,   9.0000],
        [ 11.2130, 318.4292,  71.6485, 372.4577,   0.9965,   0.7276,   7.0000],
        [118.7127, 329.6002, 154.7238, 367.1014,   0.9338,   0.9842,   2.0000],
        [116.2954, 331.1614, 157.7737, 367.1437,   0.9949,   0.9768,   2.0000],
        [139.3013, 330.8665, 208.4683, 370.9378,   0.7299,   0.9881,   2.0000],
        [144.2358, 331.4480, 211.4829, 371.0599,   0.9652,   0.9835,   2.0000],
        [140.8486, 335.7671, 208.7370, 372.8831,   0.9789,   0.9925,   2.0000],
        [142.5337, 336.3735, 212.4799, 372.1794,   0.9960,   0.9945,   2.0000],
        [190.2717, 338.0269, 279.8693, 384.5337,   0.9998,   0.9996,   2.0000],
        [200.5816, 337.6032, 282.7402, 383.9203,   0.8820,   0.9985,   2.0000],
        [302.5593, 339.3743, 414.4839, 391.6807,   0.6504,   0.9671,   2.0000],
        [297.1130, 344.0286, 416.4875, 394.0561,   0.5854,   0.9789,   2.0000],
        [  1.2199, 333.5888,   9.6376, 349.7925,   0.6693,   0.9837,   2.0000],
        [ 72.4945, 334.8250,  95.2343, 357.4444,   0.6050,   0.9960,   2.0000],
        [ 70.1864, 333.8166,  96.5011, 357.6893,   0.9912,   0.9970,   2.0000],
        [ 88.0787, 334.5563, 117.7597, 360.5888,   0.8805,   0.9968,   2.0000],
        [ 89.5038, 334.4051, 122.5763, 360.9955,   0.9910,   0.9976,   2.0000],
        [112.1916, 330.5692, 154.6762, 365.4775,   0.9580,   0.9879,   2.0000],
        [118.0661, 331.0446, 157.9749, 365.8923,   0.9197,   0.9878,   2.0000]],
       device='cuda:0')
img3.jpg             predicted in  0.320 seconds
Objects Detected:    car car car car car car car truck traffic light
----------------------------------------------------------
tensor([[ 21.0674,  39.7363, 397.5462, 387.1755,   0.5021,   0.5120,  59.0000]],
       device='cuda:0')
scream.jpg           predicted in  0.310 seconds
Objects Detected:    bed
----------------------------------------------------------
tensor([[176.8712, 170.1540, 260.3937, 250.6774,   0.9937,   0.9944,   6.0000],
        [176.1150, 163.2453, 258.4796, 247.6239,   0.7567,   0.9998,   6.0000]],
       device='cuda:0')
img2.jpg             predicted in  0.310 seconds
Objects Detected:    train
----------------------------------------------------------
tensor([[ 50.6253,  40.9168, 348.1284, 332.8145,   0.7276,   0.5271,  14.0000],
        [ 49.3928,  73.7090, 327.6407, 357.0862,   0.9993,   0.9165,  14.0000],
        [ 93.9951,  92.6685, 310.1059, 335.0621,   0.6595,   0.9989,  14.0000],
        [ 59.4123,  72.1517, 340.7898, 359.2770,   0.9999,   0.9897,  14.0000],
        [ 70.3269,  70.3806, 390.7241, 359.8500,   0.6120,   0.8403,  14.0000],
        [ 44.9427,  82.5699, 331.3582, 374.2409,   0.9795,   0.8881,  14.0000],
        [ 60.4646,  86.0531, 347.6629, 370.0936,   0.9996,   0.9875,  14.0000]],
       device='cuda:0')
eagle.jpg            predicted in  0.312 seconds
Objects Detected:    bird
----------------------------------------------------------
tensor([[  1.3335, 152.8609,  89.4882, 272.8963,   0.5679,   0.9929,  17.0000],
        [127.1342, 146.5492, 243.4970, 283.1240,   0.9576,   0.9963,  17.0000],
        [122.6568, 142.6997, 246.2126, 291.3306,   0.9456,   0.9960,  17.0000],
        [143.7737, 142.0208, 252.6802, 275.4243,   0.8679,   0.9955,  17.0000],
        [138.8003, 135.1321, 261.6679, 289.7553,   0.8715,   0.9958,  17.0000],
        [235.4074, 168.3947, 322.5774, 272.2764,   0.9699,   0.9116,  17.0000],
        [ -1.4492, 161.4622,  93.1581, 301.8401,   0.6168,   0.9760,  17.0000],
        [ -4.2169, 147.7571,  96.4948, 328.6665,   0.8703,   0.9948,  17.0000],
        [  2.6034, 160.8760, 168.4490, 334.1104,   0.9981,   0.9962,  17.0000],
        [  3.0682, 160.0935, 205.8469, 331.7335,   0.9886,   0.9985,  17.0000],
        [127.2034, 155.9976, 238.2731, 298.0151,   0.8745,   0.9982,  17.0000],
        [114.2538, 149.7305, 243.5192, 305.7959,   0.6456,   0.9991,  17.0000],
        [234.0705, 175.7185, 324.3893, 281.6943,   0.9972,   0.9698,  17.0000],
        [  9.1950, 173.4842, 165.4161, 343.2495,   0.8890,   0.9917,  17.0000],
        [  0.9075, 153.4851,  80.3595, 281.9554,   0.5511,   0.9983,  17.0000],
        [234.4629, 162.6729, 323.7180, 280.5853,   0.8243,   0.9850,  17.0000],
        [235.9680, 169.9275, 321.7621, 285.9620,   0.9680,   0.9917,  17.0000]],
       device='cuda:0')
herd_of_horses.jpg   predicted in  0.315 seconds
Objects Detected:    horse horse horse horse
----------------------------------------------------------
tensor([[255.9501,  62.8628, 375.1920, 120.2392,   0.9988,   0.9408,   7.0000],
        [264.9454,  62.9573, 379.7973, 121.1892,   0.9732,   0.8827,   7.0000],
        [253.9948,  69.6495, 376.4342, 124.8832,   0.9126,   0.9245,   7.0000],
        [ 58.0845,  80.9899, 312.5435, 295.9547,   0.9144,   0.9984,   1.0000],
        [ 61.3942,  58.7391, 311.2639, 315.9876,   0.8781,   0.9970,   1.0000],
        [ 88.7801,  73.0497, 308.5988, 302.4539,   0.9284,   0.9962,   1.0000],
        [ 63.4914,  90.3646, 308.5248, 312.3206,   0.9907,   0.9991,   1.0000],
        [ 54.7179,  76.5711, 312.4187, 336.5705,   0.9864,   0.9980,   1.0000],
        [ 86.7887,  84.0716, 307.5977, 313.9231,   0.9862,   0.9996,   1.0000],
        [ 66.4632,  72.2634, 323.5919, 333.1773,   0.5819,   0.9987,   1.0000],
        [ 59.7621, 112.0806, 173.2027, 379.6900,   0.9217,   0.9821,  16.0000],
        [ 66.4913, 161.1039, 173.2828, 392.7958,   0.9999,   0.9980,  16.0000],
        [ 73.7838, 154.1201, 187.2080, 391.0830,   0.9672,   0.9930,  16.0000],
        [ 63.4730, 177.2014, 171.8198, 403.9184,   0.9882,   0.9784,  16.0000]],
       device='cuda:0')
dog.jpg              predicted in  0.317 seconds
Objects Detected:    bicycle truck dog
----------------------------------------------------------
SUMMARY
----------------------------------------------------------
Task                     : Time Taken (in seconds)
()
Reading addresses        : 0.000
Loading batch            : 0.102
Detection (11 images)    : 3.472
Output Processing        : 0.000
Drawing Boxes            : 0.066
Average time_per_img     : 0.331
----------------------------------------------------------

对实时视频的检测数据:

wu@wu-X555LF:~/YOLO_v3_tutorial_from_scratch-master$ python video.py 
Loading network.....
Network successfully loaded
video.py:116: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  output = model(Variable(img, volatile = True), CUDA)
/usr/local/lib/python2.7/dist-packages/torch/nn/modules/upsampling.py:129: UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))
/usr/local/lib/python2.7/dist-packages/torch/nn/functional.py:2423: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
FPS of the video is  2.74
tensor([[  3.6870,  23.1612, 371.0475, 395.5959,   0.9566,   0.9922,   0.0000],
        [ 41.6146,  27.0746, 383.0414, 390.1710,   0.9994,   0.9998,   0.0000],
        [ 64.7765,  25.8871, 401.2728, 393.3916,   0.9987,   0.9999,   0.0000]],
       device='cuda:0')
9.81083583832
FPS of the video is  2.75
tensor([[  1.7734,  25.3629, 372.4455, 398.2255,   0.9624,   0.9936,   0.0000],
        [ 40.5577,  26.9211, 387.9054, 395.8350,   0.9996,   0.9998,   0.0000],
        [ 56.3693,  28.1712, 408.1103, 396.1079,   0.9997,   0.9999,   0.0000],
        [113.0928,  20.1812, 413.0195, 405.6489,   0.8725,   0.9997,   0.0000],
        [ 22.5033,  49.6157, 401.2726, 404.1279,   0.8429,   0.9120,   0.0000],
        [ 53.8593,  46.9913, 414.7895, 406.4182,   0.7642,   0.9473,   0.0000]],
       device='cuda:0')
10.1276299953
FPS of the video is  2.76
tensor([[ -1.6435,  25.2875, 376.8335, 401.0244,   0.9680,   0.9914,   0.0000],
        [ 34.7869,  26.4240, 391.7931, 399.2069,   0.9997,   0.9997,   0.0000],
        [ 59.0089,  26.8073, 406.3557, 400.0825,   0.9996,   0.9999,   0.0000],
        [115.0690,  15.1473, 410.0022, 412.5994,   0.8145,   0.9996,   0.0000],
        [ 20.5124,  42.3972, 400.3126, 409.6206,   0.8868,   0.8691,   0.0000],
        [ 62.8611,  42.0554, 406.5467, 409.6780,   0.7169,   0.9564,   0.0000]],
       device='cuda:0')
10.4487998486
FPS of the video is  2.78
tensor([[ -3.6975,  26.4786, 380.1635, 400.3980,   0.9807,   0.9956,   0.0000],
        [ 27.8412,  31.0366, 399.1293, 396.3757,   0.9999,   0.9998,   0.0000],
        [ 50.0544,  30.1592, 413.5463, 398.7300,   0.9998,   0.9999,   0.0000],
        [106.7065,  17.7191, 419.5349, 411.0420,   0.7031,   0.9990,   0.0000],
        [ 15.6208,  45.2948, 406.8257, 406.3416,   0.9578,   0.9104,   0.0000],
        [ 53.9872,  44.9171, 413.4696, 406.6386,   0.8418,   0.9737,   0.0000]],
       device='cuda:0')
10.7669699192
FPS of the video is  2.79
tensor([[  4.0570,  25.2268, 368.1359, 398.6979,   0.9799,   0.9971,   0.0000],
        [ 38.8222,  28.9720, 388.1190, 392.3002,   0.9997,   0.9999,   0.0000],
        [ 53.0951,  25.6424, 409.0585, 396.2650,   0.9994,   0.9999,   0.0000],
        [ 31.1733,  50.4870, 393.1052, 403.1170,   0.9139,   0.9208,   0.0000],
        [ 56.8872,  52.1951, 404.6879, 401.5234,   0.8562,   0.9739,   0.0000]],
       device='cuda:0')
11.0870389938
FPS of the video is  2.80
tensor([[2.8185e+01, 1.4652e+01, 3.9003e+02, 3.5967e+02, 5.2198e-01, 6.4381e-01,
         0.0000e+00],
        [1.1302e-01, 2.1111e+01, 3.7283e+02, 3.9964e+02, 9.9544e-01, 9.9821e-01,
         0.0000e+00],
        [3.5803e+01, 2.4582e+01, 3.8964e+02, 3.9276e+02, 9.9972e-01, 9.9988e-01,
         0.0000e+00],
        [6.4918e+01, 2.1665e+01, 3.9736e+02, 3.9804e+02, 9.9805e-01, 9.9975e-01,
         0.0000e+00],
        [2.8584e+01, 4.7176e+01, 3.9294e+02, 4.0918e+02, 8.9274e-01, 8.4090e-01,
         0.0000e+00],
        [5.9524e+01, 4.6148e+01, 4.0308e+02, 4.0980e+02, 5.5456e-01, 8.8580e-01,
         0.0000e+00]], device='cuda:0')
11.4057018757
FPS of the video is  2.81
tensor([[ 13.2545,  21.3704, 356.7281, 402.2535,   0.9859,   0.9970,   0.0000],
        [ 48.2958,  28.5575, 372.9237, 397.6667,   0.9983,   0.9998,   0.0000],
        [ 87.8144,  24.8447, 377.8198, 403.2309,   0.9835,   0.9995,   0.0000],
        [ 37.2587,  42.4084, 385.2265, 413.8707,   0.9740,   0.9807,   0.0000],
        [ 81.7928,  38.9542, 386.5133, 416.3832,   0.8283,   0.9830,   0.0000]],
       device='cuda:0')
11.7239089012
FPS of the video is  2.83
tensor([[ 2.8957e+01,  1.3820e+01,  3.8741e+02,  3.5745e+02,  6.9316e-01,
          8.3713e-01,  0.0000e+00],
        [ 7.5038e+00,  2.1876e+01,  3.6616e+02,  3.9935e+02,  9.9749e-01,
          9.9936e-01,  0.0000e+00],
        [ 3.5406e+01,  2.8926e+01,  3.8802e+02,  3.9056e+02,  9.9982e-01,
          9.9997e-01,  0.0000e+00],
        [ 6.5351e+01,  2.1324e+01,  3.9743e+02,  3.9995e+02,  9.9614e-01,
          9.9993e-01,  0.0000e+00],
        [-3.1555e-01,  3.4308e+01,  3.6941e+02,  4.2110e+02,  6.3145e-01,
          9.1022e-01,  0.0000e+00],
        [ 3.3661e+01,  4.7638e+01,  3.9056e+02,  4.0904e+02,  9.7159e-01,
          9.8393e-01,  0.0000e+00],
        [ 6.2052e+01,  4.5810e+01,  3.9991e+02,  4.1152e+02,  7.2356e-01,
          9.6412e-01,  0.0000e+00]], device='cuda:0')
12.6779718399

^CTraceback (most recent call last):
  File "video.py", line 116, in 
    output = model(Variable(img, volatile = True), CUDA)
  File "/usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/wu/YOLO_v3_tutorial_from_scratch-master/darknet.py", line 218, in forward
    x = predict_transform(x, inp_dim, anchors, num_classes, CUDA)
  File "/home/wu/YOLO_v3_tutorial_from_scratch-master/util.py", line 82, in predict_transform
    x_offset = x_offset.cuda()
KeyboardInterrupt
[1]+  Killed                  python video.py
wu@wu-X555LF:~/YOLO_v3_tutorial_from_scratch-master$ 

Python采用 torch模块+CUDA测试YOLO_v3_tutorial_from_scratch-master实时目标检测_第2张图片

采用GPU实时视频检测的运算速度比CPU的运算速度快很多。

你可能感兴趣的:(python)