Unable to run sample code on GPU


#1

Hi,

I’ve been trying to update the segmentation demo so I can run it on GPU. I’ve installed mxnet-cu91 to match my CUDA version and ha ev update the context used in teh FCN Pre-trained model

ctx = mx.gpu(0)

transform_fn = transforms.Compose([
** transforms.ToTensor(),**
** transforms.Normalize([.485, .456, .406], [.229, .224, .225])**
])
img = transform_fn(img)
img = img.expand_dims(0).as_in_context(ctx)
model = gluoncv.model_zoo.get_model(‘fcn_resnet50_voc’, pretrained=True, ctx=ctx)

The img context is gpu(0) (I’ve printed it out) but as soon as the attempt to load a model I get the error:

Failed to load Parameter 'fcn0_dilatedresnetv00_layers2_batchnorm11_runnung_var on [gpu[0]] because it was previous initialized on [cpu(0)]

Since I pass the correct context tinto the get_model call why is this having mixed contexts?


#2

I am not sure why you have such a problem, but if I run this script on CUDA-9.0 (I don’t have an access to 9.1) I don’t receive the exception you mention:

import mxnet as mx
import gluoncv

model = gluoncv.model_zoo.get_model("fcn_resnet50_voc", pretrained=True, ctx=mx.gpu(0))

I also don’t think it is related to CUDA version, because the exception looks like to be generated on MXNet level.

Can you check what your versions of MXNet and gluoncv are? Mine are 1.2.0 and 0.2.0 respectively. Maybe you need to update yours?

Can you tell me which versions do you use?
Can you try my minimal example and tell me if the problem is still there?


#3

Thanks, I was indeed still on a combination of mxnet-cu91 1.2.0 and gluoncv 0.1.0

After updating to 1.2.0 and gluoncv 0.2.0b20180618 the model loads without error on GPU.

However, a bigger issue is that the segmentation output is now incorrect. The output appears to be fixed to 480x480 and no longer matches the ouptut I had with 0.1.0

This happens on both cpu and gpu mode


#4

I have adjusted the code a little bit, so you could get the result of the same dimensions. Basically, I use MultiEvalModel from original test.py.

It works way slower (on my CPU it takes about a half a minute to generate an output), and it also requires to install the latest version of gluoncv with pip install gluoncv --pre.

Here is the code for this airplane example you have posted:

"""1. Getting Started with FCN Pre-trained Models
==============================================
This is a quick demo of using GluonCV FCN model.
"""
import mxnet as mx
from mxnet import image
from mxnet.gluon.data.vision import transforms
from gluoncv.model_zoo.segbase import *
import gluoncv
# using cpu
ctx = mx.cpu(0)


##############################################################################
# Prepare the image
# -----------------
#
# download the example image
url = 'https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/segmentation/voc_examples/4.jpg'
filename = 'example.jpg'
gluoncv.utils.download(url, filename)

##############################################################################
# load the image
img = image.imread(filename)

from matplotlib import pyplot as plt
plt.imshow(img.asnumpy())
plt.show()

##############################################################################
# normalize the image using dataset mean
transform_fn = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize([.485, .456, .406], [.229, .224, .225])
])
img = transform_fn(img).as_in_context(ctx)

##############################################################################
# Load the pre-trained model and make prediction
# ----------------------------------------------
#
# get pre-trained model
model = gluoncv.model_zoo.get_model('fcn_resnet50_voc', pretrained=True)
evaluator = MultiEvalModel(model, 21, ctx_list=ctx)

##############################################################################
# make prediction using single scale
output = evaluator(img)
predict = mx.nd.squeeze(mx.nd.argmax(output, 1)).asnumpy()

##############################################################################
# Add color pallete for visualization
from gluoncv.utils.viz import get_color_pallete
import matplotlib.image as mpimg
mask = get_color_pallete(predict, 'pascal_voc')
mask.save('output.png')

##############################################################################
# show the predicted mask
mmask = mpimg.imread('output.png')
plt.imshow(mmask)
plt.show()

Hope it works for you. If you want to do it in batches, then take a look into original test.py file mentioned above.


#5

Thanks,
That worked! The output mask is not consistent with the previous version though it is the correct dimensions. The performance is good running one of my Geforce GTX 1080Ti (I’ve yet to try using more than one)