-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMPLTest.py
96 lines (74 loc) · 2.39 KB
/
SMPLTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#%%
VIBEFOLDER = r'F:\Git2\VIBE\data\vibe_data/'
filename = VIBEFOLDER + 'SMPL_NEUTRAL.pkl'
import pickle
#import chumpy
with open(filename, 'rb') as f:
params = pickle.load(f,encoding="latin1")
print(params)
sd = params['shapedirs']
print(type(sd))
print(sd.x)
#%%
# %load_ext autoreload
# %autoreload 2
# %matplotlib inline
import numpy as np
import pickle
VIBEFOLDER = r'F:\Git2\VIBE\data\vibe_data/'
filename = VIBEFOLDER + 'SMPL_NEUTRAL.pkl'
class Dummy:
pass
class MyUnpickler(pickle._Unpickler):
def find_class(self, module, name):
print(module, name)
#return super().find_class(module, name)
try:
return super().find_class(module, name)
#except AttributeError:
except Exception:
#return super().find_class("numpy", "ndarray")
return Dummy
with open(filename, 'rb') as f:
params = MyUnpickler(f,encoding="latin1").load()
sd = params['shapedirs']
print(type(sd))
print(sd.x)
#%%
import sys
import pickle
class MyUnpickler(pickle._Unpickler):
def find_class(self, module, name): # from the pickle module code but with a try
# Subclasses may override this. # we are doing it right now...
try:
if self.proto < 3 and self.fix_imports:
if (module, name) in _compat_pickle.NAME_MAPPING:
module, name = _compat_pickle.NAME_MAPPING[(module, name)]
elif module in _compat_pickle.IMPORT_MAPPING:
module = _compat_pickle.IMPORT_MAPPING[module]
__import__(module, level=0)
if self.proto >= 4:
return _getattribute(sys.modules[module], name)[0]
else:
return getattr(sys.modules[module], name)
except AttributeError:
return Dummy
# edit: as per Ben suggestion an even simpler subclass can be used
# instead of the above
class MyUnpickler2(pickle._Unpickler):
def find_class(self, module, name):
try:
return super().find_class(module, name)
except AttributeError:
return Dummy
class C:
pass
c1 = C()
with open('data1.dat', 'wb') as f:
pickle.dump(c1,f)
del C # simulate the missing class
#%%
with open(VIBEFOLDER + 'SMPL_NEUTRAL.pkl', 'rb') as f:
unpickler = MyUnpickler(f) # or MyUnpickler2(f)
c1 = unpickler.load()
print(c1) # got a Dummy object because of missing class