Platform: Kaggle Kernels

If I’m on a competition where it’s not allowed internet connection on kernels, how can i use a pre-trained fast-ai resnet34 cnn?

yeah, ‘1.0.45’

you can upload your pre-trained AWD LSTM as a dataset in kaggle, then in your kaggle kernel you can “Add Data” and select your dataset.

I tried using this approach. But the learner fails to load the pretrained model, even thought that’s the one it downloaded itself a few minutes before.

The notebook describes all the steps and errors occurred. Do you know what’s wrong there?

I update the kernels every weekend so any updates pushed out mid-week cause some breakage now and then.
Sorry for the issues, I’ll try to update them mid-week :slight_smile:

learn = create_cnn(data, models.resnet34, metrics=error_rate, pretrained=False, model_dir=str(models_path.absolute())) in your notebook.
this was how i did it.
model_path = “/kaggle/input/resnet34/resnet34”
i copied my dowloaded resnet34 into kaggle/working/models/
!cp -r /kaggle/input/resnet34/ /kaggle/working/models/
initialised my model
model = create_cnn(data, models.resnet34, metrics=accuracy, path=".", pretrained=False, model_dir=’/kaggle/working/models/resnet34/resnet34’)
then you can fit with that

@init_27
Kaggle kernel allows us to run !pip install fastai==1.0.46 to install the latest version fine, but when load the library, the latest is not used.

see this proof in Kaggle

Thank you for the suggestion. I don’t quite get how it’s different from my solution, except for the missing step of loading the copied model. As far as my understanding of the process goes, it only creates a new network with resnet34 architecture and randomly initialized weights. It doesn’t use the copied file in any way.

I’ve updated the notebook with your example. It supports my guess that the weights from the resnet34.pth weren’t used. I might have messed up with the directories somehow, though. Could you please tell the output of the following commands?

# What's in your datasource directory
!ls -lah /kaggle/input/resnet34/
# The working directory should have the content of /kaggle/input/resnet34/ I guess
# Why is it /kaggle/working/models/resnet34/resnet34 you're using for your model_dir?
# Is there any special file that create_cnn can read?
!ls -lah /kaggle/working/models/
!ls -lah /kaggle/working/models/resnet34
!ls -lah /kaggle/working/models/resnet34/resnet34

And just to be sure, did you compare the results of using the model from dataset, as you suggest, to the ones from loading it (on another machine maybe)?

I have a few beginner questions, as doing the course using Kernels doesn’t seem to be working for me.

Most importantly, how can you actually save your work? I’m trying to do lesson 1, and have been trying for about 2 days to get through it. It’s taking between up to 40 minutes to train a model, and sometimes this is long enough for the Kernel to stop responding/time out. No error message, just stops responding completely. When I reload the kernel, I would like to be able to load the model so I don’t have to spend 60+ minutes going through the notebook. Unfortunately, I can’t figure out how to load the data, as the saved models don’t seem to persist if I have to refresh the page. Is this even possible with kernels?

I also tried looking into exporting the pth files after saving the model, but in order to do this, it looks like you have to ‘commit’ the kernel - but this process takes over 90 minutes, and requires running the notebook start to finish, with no promise that it won’t time out, so I unfortunately haven’t been able to save my work this way.

I assume I’m just not understanding the workflow I should be using to work using kernels. Can someone provide me some guidance? As it is, I can’t really even get through the first lesson, as it is taking so long, and I don’t have a way of saving progress. Do other platforms have similar limitations?

1 Like

Hi,

You can use Google Colab for fast ai. Please check this link to get started
https://course.fast.ai/start_colab.html

Same question for Lesson 2 – I’m wanting to pull the weights and labels.csv for a dataset I spent time cleaning-up.

The timeout is a known problem of the Kaggle Kernels if you do not commit. But if you commit, it should not timeout and you should be able to export your model as pth files.

1 Like

Where is this export you speak of :wink:

learn.save should work…

As near as I can tell, learn.save is just dropping output onto ephemeral storage. Once the Kaggle Kernal session expires so do any files created during the session.

@Steve It depends on the path of the model… If it’s in the main directory it should be under the output tab after committing. Please see this kernel and this kernel as examples.

People have asked so many times on how to do this I ought to have a dedicated kaggle kernel and a post here to demonstrate…

1 Like

@ilovescience I can see the .pth files being created in the /kaggle/working/models directory while running through the notebook. I can commit the notebook and view it in Kernels --> My Work --> Output. I can click on the notebook and see the Notebook, Data, Log, and Comments links on the left-hand rail but under Data I only see the Source Data not the .pth files.

I also see Data Files under Output but just see a message that, “You haven’t created any kernels yet.” when I select Data Files. I’ve been poking at this for awhile now and need a break. Thank you for your assistance!!

I think the questions raised are mostly around how to save your model and how to upload your dataset.

  1. Saving gives error:
    Kernels load datasets from ../input/ this is read only storage. If we create a learner from here and try learn.save(), the function tries to save in the same folder as the dataset. Hence the error.

Fix: Set the path in learn.save() accordingly.

  1. I don’t see my kernel outputs:

Too see the outputs, you need to “commit” the kernel, which will run the nb from end to end. Thus creating your output files.

  1. Uploading custom datasets:

You will need to create custom dataset by uploading it to kaggle datasets and then linking it to your kernel. Follow the kaggle kernels tutorial to understand this.

Hope these solve the pain points.
Please let me know if I’ve missed anything.

I’ve created a FAQ section above with these points.

this is beautiful and super useful!