I'll pay $25 to whoever helps me fix this FastAI problem

Hi,

I’ll pay $25 to whoever helps me fix my issue. I’d prefer to use Venmo for payment but we can discuss other options. To earn the $25, your solution has to get my GAN learner working in my web app (without retraining it).

When I try to export my GAN learner for inference, I get an exception: AttributeError: Can’t pickle local object ‘gan_loss_from_func.._loss_G’

If I try to save the learner (rather than export) then load it from another module, I get the following exception when I try to load the pth file (despite loading it in exactly the same way as in my notebook):

RuntimeError: Error(s) in loading state_dict for GANModule:
Missing key(s) in state_dict: “generator.layers.0.0.weight”, “generator.layers.0.1.weight”,… [lots more]
Unexpected key(s) in state_dict: “opt_func”, “loss_func”, “metrics”, “true_wd”, “bn_wd”, “wd”, “train_bn”, “model_dir”, “callback_fns”, “cb_state”, “model”, “data”, “cls”.

Thanks,

Kris

The issue is at some point your loss function included lambas. To export it properly make it a def function instead

Re your export, save has a bunch of attributes to it, it’s not just the model. To do that you should do torch.save(learn.model) if you just want the raw PyTorch, however fixing the lambda will let you do learn.export properly.

Or, to get it from save, if m is a torch.load instance it’s inside m[‘model’]

How can I fix the lambda issue without retraining the model?

My generator loss function is MSELossFlat()
My critic loss function is AdaptiveLoss(nn.BCEWithLogitsLoss())

I’m sorry if it wasn’t clear, but the solution has to allow me to use the model I’ve already trained. It took me a month to train it.