のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

fast.aiをgoogle colabで実行する方法

Python3のGPUありの設定で動作した。

共有ファイルを参照する方法を利用させてもらった。
qiita.com

!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

共有ファイルへのリンクをコピーする。
githubからfastaiをzipでダウンロードした。

#https://drive.google.com/open?id=1234567890123456789
id = '1234567890123456789'  # 共有リンクで取得した id= より後の部分
downloaded = drive.CreateFile({'id': id})
downloaded.GetContentFile('fastai.zip')
!unzip -o -qq fastai.zip

カレントディレクトリから、fastai-master>courses>dl1へ移動する

%cd ~
%pwd
%cd fastai-master
#!ls
%cd courses
#!ls
%cd dl1
!ls

catsdogs.zipを同様に共有ファイルからコピーしてきた。
fastai-master>courses>dl1>dataの中へコピーして、unzipした。

%cd ~
%pwd
%cd fastai-master
%cd courses
%cd dl1
!mkdir data
%cd data
%pwd

#https://drive.google.com/open?id=ABCDEFGHABCDEFGH
id2="ABCDEFGHABCDEFGH"# 共有リンクで取得した id= より後の部分
downloaded = drive.CreateFile({'id': id2})
downloaded.GetContentFile('dogscats.zip')

!unzip -o -qq dogscats.zip
!ls
%cd ..
!pip3 install http://download.pytorch.org/whl/cu80/torch-0.3.0.post4-cp36-cp36m-linux_x86_64.whl
!pip3 install torchvision
!pip install torchtext
!pip install bcolz
!pip install graphviz
!pip install sklearn_pandas
!pip install isoweek
!pip install pandas_summary
!pip install ipywidgets

AttributeError: module 'torch.nn' has no attribute 'functional'

                                                                                                                                                    • -

AttributeError Traceback (most recent call last)
in ()

        • > 1 from fastai.transforms import *

2 from fastai.conv_learner import *
3 from fastai.model import *
4 from fastai.dataset import *
5 from fastai.sgdr import *

/content/fastai-master/courses/dl1/fastai/transforms.py in ()
1 from .imports import *

        • > 2 from .layer_optimizer import *

3 from enum import IntEnum
4
5 def scale_min(im, targ, interpolation=cv2.INTER_AREA):

/content/fastai-master/courses/dl1/fastai/layer_optimizer.py in ()
1 from .imports import *

        • > 2 from .torch_imports import *

3 from .core import *
4
5 def opt_params(parm, lr, wd):

/content/fastai-master/courses/dl1/fastai/torch_imports.py in ()
1 import os

        • > 2 import torch, torchvision, torchtext

3 from torch import nn, cuda, backends, FloatTensor, LongTensor, optim
4 import torch.nn.functional as F
5 from torch.autograd import Variable

/usr/local/lib/python3.6/dist-packages/torchvision/__init__.py in ()

        • > 1 from torchvision import models

2 from torchvision import datasets
3 from torchvision import transforms
4 from torchvision import utils
5

/usr/local/lib/python3.6/dist-packages/torchvision/models/__init__.py in ()
3 from .vgg import *
4 from .squeezenet import *

        • > 5 from .inception import *

6 from .densenet import *

/usr/local/lib/python3.6/dist-packages/torchvision/models/inception.py in ()
1 import torch
2 import torch.nn as nn

        • > 3 import torch.nn.functional as F

4 import torch.utils.model_zoo as model_zoo
5

AttributeError: module 'torch.nn' has no attribute 'functional'