pytorch Fast.ai:ModuleAttributeError:'Sequential'对象没有属性'fine_tune'

rks48beu  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(212)

当我使用fast.ai时,T遇到了这个问题,下面是我的代码:

from fastai.vision.all import *
from fastai.text.all import *
from fastai.collab import *
from fastai.tabular.all import *

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

path = untar_data(URLs.PETS)/'images'

def is_cat(x): return x[0].isupper()

dls = ImageDataLoaders.from_name_func(
    path, get_image_files(path), valid_pct=0.2, seed=42,
    label_func=is_cat, item_tfms=Resize(224))

learn = cnn_learner(dls, resnet34, metrics=error_rate).to(device)

learn.fine_tune(1)

字符串
它显示:“ModuleAttributeError:'Sequential' object has no attribute 'fine_tune'”

noj0wjuj

noj0wjuj1#

fastai将为您选择GPU(如果可用)。
我复制了你的问题,并在Learner中删除了.to(device)调用,从而消除了错误:

learn = cnn_learner(dls, resnet34, metrics=error_rate)
learn.fine_tune(1)

字符串

nuypyhwy

nuypyhwy2#

我能够重现这个错误,并通过在文件顶部添加from fastai.schedule.callback import fine_tune行来解决它。
下面是fastai库中fine_tune方法的代码:https://github.com/fastai/fastai/blob/b273fbb32d075ef1d6fd372687b5f56564cead9a/fastai/callback/schedule.py#L161

# %% ../../nbs/14_callback.schedule.ipynb 60
@patch
@delegates(Learner.fit_one_cycle)
def fine_tune(self:Learner, epochs, base_lr=2e-3, freeze_epochs=1, lr_mult=100,
              pct_start=0.3, div=5.0, **kwargs):
    "Fine tune with `Learner.freeze` for `freeze_epochs`, then with `Learner.unfreeze` for `epochs`, using discriminative LR."
    self.freeze()
    self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
    base_lr /= 2
    self.unfreeze()
    self.fit_one_cycle(epochs, slice(base_lr/lr_mult, base_lr), pct_start=pct_start, div=div, **kwargs)

字符串
注意,我称它为“方法”,但它并没有在类上定义。想知道这是怎么回事吗?
看到那个@patch装饰器了吗
查看fastcore库中patch函数的定义:https://github.com/fastai/fastcore/blob/master/nbs/01_basics.ipynb
这让我想到了我所期望的解决方案。你只需要导入模块,用方法修补类。

相关问题