Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving general artifacts #153

Closed
kfirgoldberg opened this issue Jun 24, 2020 · 8 comments
Closed

Saving general artifacts #153

kfirgoldberg opened this issue Jun 24, 2020 · 8 comments
Labels
Feature Request Feature Request - Support w/ :+1: reaction

Comments

@kfirgoldberg
Copy link

Hi,
I'd like to save an object of a class I implemented as an artifact.
According to the documentation, it seems like something I am supposed to be able to do, but when I try I get an error:
"Artifact type <my_class> not supported" :(
Any ideas?

Thanks!

@bmartinn
Copy link
Member

Hi @kfir99,
Wow that sounds quite cool!
Currently the upload_artifact method supports files (either as string or Path) or a set of predefined objects (Numpy,Pandas PIL Image etc.)

This is very interesting use case, could you elaborate on how it is used ?
Also, how would you do that (i.e. serialize the object), would it be to json it? pickle it?

@kfirgoldberg
Copy link
Author

I'll elaborate:
I use gluoncv which is based on mxnet, and I have a "classification_network" class which includes a gluoncv network and some other information (synsets, etc...) which is the class I'm trying to save.
I usually pickle it to save locally, but any serialization method is fine for me.
Ideally I'd like to use this to log the model after every epoch - have the object of the classification_network class along with its accuracy and the epoch number

@bmartinn
Copy link
Member

bmartinn commented Jun 24, 2020

@kfir99 , let me see if I understand you correctly

  1. Log the classification_network network.
  2. Make sure that the output model is "connected" with the classification_network object, in order to have full visibility and traceability on the model weights, and how they were trained.

Regarding (1) you could do:

import pickle
with open('temp_object.pkl', 'wb') as f:
    pickle.dump(my_object, f)

task.upload_artifact('classification_network', artifact_object='temp_object.pkl', delete_after_upload=True)

Regrading (2), you can manually upload a model file (which will be automatically connected with the experiment) :

from trains import OutputModel
OutputModel(framework='mxnet').update_weights('mxmodel.bin')

What do you think?

EDIT:
Maybe we should have a flag like auto_pickle=False, and if set to True it will pickle the object and upload it
Maybe something like:

task.upload_model('my object', my_object, auto_pickle=True)

Then from another script we could do the following that will automatically load the object from the pickle file:

previous_task = Task.get_task(task_id='abcq123')
my_object = previous_task.artifacts['my object'].get()

It actually sounds like a cool feature to add :)

@kfirgoldberg
Copy link
Author

Thanks for your response!
The feature you suggested sounds very useful :)
I tried implementing your suggestions, (2) worked, but for (1) I got the same "artifact not supported" error, this time for <type 'str'>.
Am I missing something?

@bmartinn
Copy link
Member

The feature you suggested sounds very useful :)

@kfir99 , awesome we are in agreement, we will add this feature 🚀

(1) I got the same "artifact not supported"

Notice that (1) passes a string (or a Path object) , My guess is that you passed a link to a file that was not there (or maybe it was a relative path etc).
You can verify with:

from pathlib2 import Path
import pickle
with open('temp_object.pkl', 'wb') as f:
    pickle.dump(my_object, f)

pickle_file = Path('temp_object.pkl')
assert pickle_file.exists()
task.upload_artifact('classification_network', artifact_object=pickle_file, delete_after_upload=True)

@bmartinn bmartinn added the Feature Request Feature Request - Support w/ :+1: reaction label Jun 24, 2020
@kfirgoldberg
Copy link
Author

Changing the path from a string to a Path object worked :)
Thank you very much for your help!

allegroai-git pushed a commit that referenced this issue Jul 1, 2020
Fix multiple argparse calls before task.init
Add mark_started force argument #159
@bmartinn
Copy link
Member

bmartinn commented Jul 7, 2020

Hi @kfir99
You will be happy to hear that there is now support for auto pickling artifacts, and yes this is all thanks to you :)

task.upload_artifact('MyObject', artifact_object=object() auto_pickle=True)
# and later from another process
my_object = task.artifacts['MyObject'].get()

Install the latest RC to enjoy the new feature

pip install trains==0.15.2rc0

@kfirgoldberg
Copy link
Author

Thank you very much, I'm excited to try it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Feature Request - Support w/ :+1: reaction
Projects
None yet
Development

No branches or pull requests

2 participants