1

I created a class called Gesture for a gesture recognition ML project I'm working on. I read in a dataset of gestures. For each item in the dataset, I made an instance of the Gesture class to store the values and added them all to a list. I stored that list as a .dat file so I don't have to reupload the dataset each time I work on this project (it takes a while to upload).

I added new methods to the Gesture class, but when I try to use them on the saved Gesture variables, I am met with an

 Attribute Error: 'Gesture' object has no attribute 'Reframe_Sequence' 

(Reframe_Sequence is the method I added after the data was stored and reloaded).

Must I re-store the dataset, or is there a way to get around this?

Here is some of the code so you get a better scope of the problem:

the class and the method:

class Gesture:
  def __init__(self, gesture, finger, subject, trial, sequence):
    self.gesture = gesture
    self.finger  = finger
    self.subject = subject
    self.trial = trial
    self.sequence = sequence 
    

  def Reframe_Sequence(self):
    # split each row by spaces, and save in an array
    # make an array that is len x 22 x 3
    # loop through each frame, then nested loop through, each with step of 3 and add the xyz to the joint
    s = self.sequence.to_numpy()
    n = s.size #number of frames
    arr2D = np.zeros(n, 66)
    arr3D = np.zeros(n,22,3)
    for i in range(n):
      arr2D[i] = s[i].split(" ")
    for i in range(n):
      for j in range(0, 66, 3):
        arr3D[i,j/3] = arr2D[i,j:j+3]
    self.sequence = arr3D
    return self.sequence

how it's loaded in and stored:

rootdir = '/content/drive/MyDrive/Colab Notebooks/HandGestureDataset_SHREC2017'

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
      word  = "skeletons_world.txt"
      if word in file:
        path = os.path.join(subdir, file)
        path_list = path.split("/")
        gesture = re.findall(r'\d+', path_list[6]) 
        finger = re.findall(r'\d+', path_list[7]) 
        subject= re.findall(r'\d+', path_list[8]) 
        trial= re.findall(r'\d+', path_list[9])
        sequence = pd.read_csv(path)
        g = Gesture(gesture, finger, subject, trial, sequence)
        Master_List.append(g)

pickle.dump(Master_List, open("master_list.dat", "wb"))
ML = pickle.load(open("/content/master_list.dat", "rb"))

Here is where the issue is when I try to use one of those saved objects

m = ML[5]
m.Reframe_Sequence()
k = m.sequence
print(k)

This is the error I get:

AttributeError Traceback (most recent call last) in ()

      1 m = ML[5]
----> 2 m.Reframe_Sequence()
      3 k = m.sequence
      4 print(k)

AttributeError: 'Gesture' object has no attribute 'Reframe_Sequence'

I added Reframe_Sequence() after I stored the objects. Must I re-store them?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Quill
  • 21
  • 2

1 Answers1

0

this seems to be a problem with pickle, which is why it is not recommended to store classes that contain methods, there are two ways around this, either you "rewire" the old class to the new class instance by changing the object __class__ or you can move the methods to another class, that will not be pickled and only pickle the dataclass, which is the recommended approach.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23