Cannot load model trained by fastai1

Hi,

I have trained and built a GAN to colorise black and white photos by fastai1, it was working okay and I was able to build a web application for it.
But I got errors when I tried to load the models (pkl and pth) using fastai2. Is there a way to load models trained by fastai 1, or should I train it again using fastai2?

Thanks

You’d need to save the weights and the exact model, and then load those weights in, however this is learn.save(). fastai2 is not backwards compatible with fastai1

1 Like

Thanks!
I have one more question, I am trying to use FeatureLoss mentioned in lesson7 fastai v3 course.
But I cannot from fastai.core import * and from fastai.callbacks import hook_outputs using fastai2
I was trying to use the code from DeOldify. Thanks

from fastai import *
from fastai.core import *
from fastai.torch_core import *
from fastai.callbacks import hook_outputs
import torchvision.models as models
    

class FeatureLoss(nn.Module):
        def __init__(self, layer_wgts=[20, 70, 10]):
            super().__init__()

            self.m_feat = models.vgg16_bn(True).features.cuda().eval()
            requires_grad(self.m_feat, False)
            blocks = [
                i - 1
                for i, o in enumerate(children(self.m_feat))
                if isinstance(o, nn.MaxPool2d)
            ]
            layer_ids = blocks[2:5]
            self.loss_features = [self.m_feat[i] for i in layer_ids]
            self.hooks = hook_outputs(self.loss_features, detach=False)
            self.wgts = layer_wgts
            self.metric_names = ['pixel'] + [f'feat_{i}' for i in range(len(layer_ids))]
            self.base_loss = F.l1_loss

        def _make_features(self, x, clone=False):
            self.m_feat(x)
            return [(o.clone() if clone else o) for o in self.hooks.stored]

        def forward(self, input, target):
            out_feat = self._make_features(target, clone=True)
            in_feat = self._make_features(input)
            self.feat_losses = [self.base_loss(input, target)]
            self.feat_losses += [
                self.base_loss(f_in, f_out) * w
                for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)
            ]

            self.metrics = dict(zip(self.metric_names, self.feat_losses))
            return sum(self.feat_losses)

        def __del__(self):
            self.hooks.remove()

use

from fastai.callbacks.hook import hook_outputs