How to run code using GPU?


#1

The code comes from Run an object detection model on your webcam but I changed to detect on the video.

The point is that I try to use
net = gcv.model_zoo.get_model(‘yolo3_darknet53_voc’, pretrained=True, ctx=mx.gpu(0))
net(x.as_in_context(mx.gpu(0))

and it shows
File “demo.py”, line 8, in
net(x.as_in_context(mx.gpu(0)))
NameError: name ‘x’ is not defined

import cv2
import gluoncv as gcv
import matplotlib.pyplot as plt
import mxnet as mx

# Load the model
net = gcv.model_zoo.get_model('yolo3_darknet53_voc', pretrained=True, ctx=mx.gpu(0))
net(x.as_in_context(mx.gpu(0))

cap = cv2.VideoCapture('test.avi')
axes = None

while(cap.isOpened()):

    # Load frame from the camera
    ret, frame = cap.read()

    # Image pre-processing
    frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
    rgb_nd, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)

    # Run frame through network
    class_IDs, scores, bounding_boxes = net(rgb_nd)

    for i in range(len(class_IDs[0])):
        if class_IDs[0][i].asscalar() != 14.:
            scores[0][i] = 0
            plt.cla()
            axes = gcv.utils.viz.plot_bbox(frame, bounding_boxes[0], scores[0], class_IDs[0], class_names=net.classes, ax=axes)
            #print('boundingBox====', bounding_boxes[0])

my configuration:
CUDA version is 9.2
cudnn-9.2 windows10
mxnet-cu92, install by pip
gluoncv-0.4.0b20190228

Thank you.


#2

Hi @emesssii,

You are correctly initializing the model parameter on the GPU (in a deferred way), but you’re trying to pass through a batch of data called x that hasn’t been defined, hence you’re getting the is not defined error. Skip that line entirely, and move rgb_nd to the gpu just before calling net inside the loop.

...

while(cap.isOpened()):

    # Load frame from the camera
    ret, frame = cap.read()

    # Image pre-processing
    frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8')
    rgb_nd, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=700)

    # Run frame through network
    rgb_nd = rgb_nd.as_in_context(mx.gpu(0))
    class_IDs, scores, bounding_boxes = net(rgb_nd)
    ...

You might also just want to set a ctx variable so you don’t have to keep referencing mx.gpu(0) multiple times.


#3

Hello @thomelane ,
Thanks a lot for your answer.

I tried mx.gpu(0) and yes GPU was used but it’s always 1%
my teacher told that it’s equal to not using.

In the Anaconda prompt show
[12:45:50] c:\jenkins\workspace\mxnet\mxnet\src\operator\nn\cudnn./cudnn_algoreg-inl.h:97: Running performance tests to find the best convolution algorithm, this can take a while… (setting env variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable)


#4

Just to confirm, you fixed your original issue?

As for why utilisation is low… in this example you’re just not giving your GPU enough to do. You’re only working with a single image at a time, and GPUs are optimised to work on things in parallel (e.g. a batch of data). You’d see much higher utilisation if you were training such a model, but you’re just running it for inference. You’re limited by other things, such as: getting data from the camera, applying the transforms and plotting the results.


#5

@thomelane

Yes, now It used GPU already and thanks for your explanation :smiley: