ConvLearner -NameError: name 'to_gpu' is not defined

Hi all,

I’m trying to train my network for “Dog breeds” competition, but after running ConvLearner.pretrained, I get the error message: NameError: name ‘to_gpu’ is not defined.
Can somebody know how to solve this problem?

My code:

%reload_ext autoreload
%autoreload 2
%matplotlib inline
from fastai.imports import *
import pandas as pd
import numpy as np
import cv2

from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *

PATH = './'
sz = 224
arch = resnet34
bs = 10

label_file = f'{PATH}labels.csv'
n = len(list(open(label_file)))-1
val_idxs = get_cv_idxs(n)

data = ImageClassifierData.from_csv(path=PATH, folder='test', csv_fname=label_file,
                                    val_idxs=val_idxs, suffix='.jpg', tfms=tfms_from_model(arch, sz), bs=bs) 
learn = ConvLearner.pretrained(arch, data, precompute=True)

Error message:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-977ee74c5c67> in <module>
      6 data = ImageClassifierData.from_csv(path=PATH, folder='test', csv_fname=label_file,
      7                                     val_idxs=val_idxs, suffix='.jpg', tfms=tfms_from_model(arch, sz), bs=bs) 
----> 8 learn = ConvLearner.pretrained(arch, data, precompute=True)

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/conv_learner.py in pretrained(cls, f, data, ps, xtra_fc, xtra_cut, **kwargs)
     92     @classmethod
     93     def pretrained(cls, f, data, ps=None, xtra_fc=None, xtra_cut=0, **kwargs):
---> 94         models = ConvnetBuilder(f, data.c, data.is_multi, data.is_reg, ps=ps, xtra_fc=xtra_fc, xtra_cut=xtra_cut)
     95         return cls(data, models, **kwargs)
     96 

~/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/conv_learner.py in __init__(self, f, c, is_multi, is_reg, ps, xtra_fc, xtra_cut)
     46         fc_layers = self.get_fc_layers()
     47         self.n_fc = len(fc_layers)
---> 48         self.fc_model = to_gpu(nn.Sequential(*fc_layers))
     49         apply_init(self.fc_model, kaiming_normal)
     50         self.model = to_gpu(nn.Sequential(*(layers+fc_layers)))

NameError: name 'to_gpu' is not defined

and information about my env:
(Google Cloud Platform)

=== Software === 
python version  : 3.6.7
fastai version  : 1.0.36.post1
torch version   : 1.0.0
nvidia driver   : 410.72
torch cuda ver  : 9.0.176
torch cuda is   : available
torch cudnn ver : 7401
torch cudnn is  : enabled

=== Hardware === 
nvidia gpus     : 1
torch available : 1
  - gpu0        : 11441MB | Tesla K80

=== Environment === 
platform        : Linux-4.9.0-8-amd64-x86_64-with-debian-9.6
distro          : #1 SMP Debian 4.9.130-2 (2018-10-27)
conda env       : fastai
python          : /home/zaborowski/anaconda3/envs/fastai/bin/python
sys.path        : 
/home/zaborowski/anaconda3/envs/fastai/lib/python36.zip
/home/zaborowski/anaconda3/envs/fastai/lib/python3.6
/home/zaborowski/anaconda3/envs/fastai/lib/python3.6/lib-dynload
/home/zaborowski/anaconda3/envs/fastai/lib/python3.6/site-packages
/home/zaborowski/anaconda3/envs/fastai/lib/python3.6/site-packages/IPython/extensions

Thu Dec 13 09:24:55 2018       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.72       Driver Version: 410.72       CUDA Version: 10.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla K80           Off  | 00000000:00:04.0 Off |                    0 |
| N/A   69C    P8    32W / 149W |     11MiB / 11441MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

Can somebody help me with this?

You’re using a mix of v0.7 and v1, they can’t work together.

I’ve reinstalled my env for fast.ai v0.7 and it works.
Maybe after the first part of the course, I’ll switch to v1.0

Thank you for help!

You can enjoy both versions from the same instance. Just create an addition conda environment and Jupyter ipykernel. Switching is as easy as clicking the pull-down menu on Jupyter. There are tep-by-step instructions here: http://www.marychin.org/machinelearning/2018/12/13/my-ultimate-fast-ai-instance/

Thank you for advice!
After I learn how to use fastai v1.0, perhaps I’ll switch to this :slight_smile:
Now I need to learn how to use fastai v0.7 and DNN in general :slight_smile:

I join forums today, but I don’t find where to create a new topic after I activate the account.
If I need to wait for some hours?