0

I'm trying to get code from this repo running, which means I have to make this get_data function work:

def get_data(self, pickle_path, aug_flag=True):
    with open(pickle_path + self.image_filename, 'rb') as f:
        images = pickle.load(f)   # <--------- THIS line is the problem
        images = np.array(images)
        print('images: ', images.shape)
    # do more things here

But it gives me the error ValueError: unsupported pickle protocol: 3 so I found advice here, where they recommend a different protocol: pickle.dump(images, f, protocol=2)

def get_data(self, pickle_path, aug_flag=True):
    with open(pickle_path + self.image_filename, 'rb') as f:
        pickle.dump(images, f, protocol=2)   # still bad
        images = np.array(images)
        print('images: ', images.shape)
    # do more things here

However, that gives me the error UnboundLocalError: local variable 'images' referenced before assignment. Is there a way I can fix this, specifically for StackGAN/misc/datasets.py?

Nathan majicvr.com
  • 950
  • 2
  • 11
  • 31
  • just do `other_images = np.array(images)` – Joran Beasley Mar 30 '18 at 22:28
  • The advice you're trying to follow is telling you how to change the code that *generates* the file in the first place, such that it creates a file that Pickle library implementations that only support protocol-2 can load. It's not telling you how to *load* a file created with the wrong format. – Charles Duffy Mar 30 '18 at 22:28

1 Answers1

0

I found the answer here:

def get_data(self, pickle_path, aug_flag=True):
    with open(pickle_path + self.image_filename, 'rb') as f:
        pickle.dump(pickle.load(sys.stdin), sys.stdout, 2)  # <----- *****
        images = np.array(images)
        print('images: ', images.shape)
    # do more things here

StackGAN supplies files pickled in Python3, and I needed to convert those to a Python2 protocol

Nathan majicvr.com
  • 950
  • 2
  • 11
  • 31