How to get feature from pretrained model from params on GPU?


#1

For example I have ctx = gpu(0) now and I get the pre-trained model from

img_net = vision.inception_v3(pretrained=True, ctx=ctx)

and when I use feature = img_net.features(img) to get the feature it gives error

RuntimeError: Parameter 'inception30_conv0_weight' was not initialized on context cpu(0). It was only initialized on [gpu(0)].

However, features returns an Inception_v3 object but it does not accept ctx as a parameter,

  File "extract_image.py", line 37, in get_image_feature
    feature = img_net.features(img, ctx=ctx)
TypeError: __call__() got an unexpected keyword argument 'ctx'

so how could I get the feature I want with the parameters which are already saved in GPU? I suppose this way is faster than CPU


#2

Data and network weights must be in the same context. You need to call as_in_context() on your data:

feature = img_net.feature(img.as_in_context(gpu(0)))

#3

Perfectly solved my problem! Thanks