Converting a theano model built on GPU to CPU?

2019-05-02 08:50发布

I have some pickle files of deep learning models built on gpu. I'm trying to use them in production. But when i try to unpickle them on the server, i'm getting the following error.

Traceback (most recent call last):
File "score.py", line 30, in
model = (cPickle.load(file))
File "/usr/local/python2.7/lib/python2.7/site-packages/Theano-0.6.0-py2.7.egg/theano/sandbox/cuda/type.py", line 485, in CudaNdarray_unpickler
return cuda.CudaNdarray(npa)
AttributeError: ("'NoneType' object has no attribute 'CudaNdarray'", , (array([[ 0.011515 , 0.01171047, 0.10408644, ..., -0.0343636 ,
0.04944979, -0.06583775],
[-0.03771918, 0.080524 , -0.10609912, ..., 0.11019105,
-0.0570752 , 0.02100536],
[-0.03628891, -0.07109226, -0.00932018, ..., 0.04316209,
0.02817888, 0.05785328],
...,
[ 0.0703947 , -0.00172865, -0.05942701, ..., -0.00999349,
0.01624184, 0.09832744],
[-0.09029484, -0.11509365, -0.07193922, ..., 0.10658887,
0.17730837, 0.01104965],
[ 0.06659461, -0.02492988, 0.02271739, ..., -0.0646857 ,
0.03879852, 0.08779807]], dtype=float32),))

I checked for that cudaNdarray package in my local machine and it is not installed, but still i am able to unpickle them. But in the server, i am unable to. How do i make them to run on a server which doesnt have a GPU?

4条回答
闹够了就滚
2楼-- · 2019-05-02 09:27

There is a script in pylearn2 which may do what you need:

pylearn2/scripts/gpu_pkl_to_cpu_pkl.py

查看更多
冷血范
3楼-- · 2019-05-02 09:36

I solved this problem by just saving the parameters W & b, but not the whole model. You can save the parameters use this:http://deeplearning.net/software/theano/tutorial/loading_and_saving.html?highlight=saving%20load#robust-serialization This can save the CudaNdarray to numpy array. Then you need to read the params by numpy.load(), and finally convert the numpy array to tensorSharedVariable use theano.shared().

查看更多
神经病院院长
4楼-- · 2019-05-02 09:48

This works for me. Note: this doesn't work unless the following environment variable is set: export THEANO_FLAGS='device=cpu'

import os
from pylearn2.utils import serial
import pylearn2.config.yaml_parse as yaml_parse

if __name__=="__main__":

_, in_path, out_path = sys.argv
os.environ['THEANO_FLAGS']="device=cpu"

model = serial.load(in_path)

model2 = yaml_parse.load(model.yaml_src)
model2.set_param_values(model.get_param_values())

serial.save(out_path, model2)
查看更多
趁早两清
5楼-- · 2019-05-02 09:52

The related Theano code is here.

From there, it looks like there is an option config.experimental.unpickle_gpu_on_cpu which you could set and which would make CudaNdarray_unpickler return the underlying raw Numpy array.

查看更多
登录 后发表回答