Attribute error when loading pickle file in python jupyter notebook

I have created a config file with the following code for an object detection task and saved in the local disk.

# Create the config
C = Config()
C.use_horizontal_flips = horizontal_flips
C.use_vertical_flips = vertical_flips
C.rot_90 = rot_90
C.record_path = record_path
C.model_path = output_weight_path
C.num_rois = num_rois
C.base_net_weights = base_weight_path

with open(config_output_filename, 'wb') as config_f:
pickle.dump(C,config_f)

I am trying to load this pickle file in another jupyter notebook.

with open(config_output_filename, "rb") as f:
C = pickle.load(f)    
# turn off any data augmentation at test time
C.use_horizontal_flips = False
C.use_vertical_flips = False
C.rot_90 = False
print(C.num_rois)

But it gives me the following error.

------------------------------------------------------------------------ 

AttributeError             Traceback (most recent call 
last)
 ipython-input-20-c5528d5ef98b in module
  1 with open(config_output_filename, "rb") as f:
   ---- 2     C = pickle.load(f)

  AttributeError: Can't get attribute 'Config' on module '__main__'

But I was able to load the pickle file from the disk without errors in my previous attempts and applied the configurations on the test set.

Topic pickle object-detection jupyter keras python

Category Data Science


One option is to refactor the code to use a Python dictionary. A Python dictionary can be serialized as pickle or json.

config = {"use_horizontal_flips": True,
          "use_vertical_flips":   False,
        }

I guess the Config() is a class that you created. It will be needed again when you load a pickle file storing the config. Therefore, you need to import it in the jupyter notebook you use to load your pickle file.

Hope this could be helpful.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.