def load_variables(chkpoint, base_dir=BASE_DIR): manifest_path = os.path.join(base_dir, chkpoint, "manifest.json") if not os.path.exists(manifest_path): print( 'Weights for checkpoint %s are not downloaded. Downloading to %s ...' % (chkpoint, base_dir)) from posenet.converter.wget import download download(chkpoint, base_dir) assert os.path.exists(manifest_path) manifest = open(manifest_path) variables = json.load(manifest) manifest.close() state_dict = {} for x in variables: torch_name = to_torch_name(x) if not torch_name: continue filename = variables[x]["filename"] byte = open(os.path.join(base_dir, chkpoint, filename), 'rb').read() fmt = str(int(len(byte) / struct.calcsize('f'))) + 'f' d = struct.unpack(fmt, byte) d = np.array(d, dtype=np.float32) shape = variables[x]["shape"] if len(shape) == 4: tpt = (2, 3, 0, 1) if 'depthwise' in filename else (3, 2, 0, 1) d = np.reshape(d, shape).transpose(tpt) state_dict[torch_name] = torch.Tensor(d) return state_dict
def load_variables(chkpoint, base_dir=BASE_DIR): manifest_path = os.path.join(base_dir, chkpoint, "manifest.json") if not os.path.exists(manifest_path): print( 'Weights for checkpoint %s are not downloaded. Downloading to %s ...' % (chkpoint, base_dir)) from posenet.converter.wget import download download(chkpoint, base_dir) assert os.path.exists(manifest_path) f = open(manifest_path) variables = json.load(f) f.close() # with tf.variable_scope(None, 'MobilenetV1'): for x in variables: filename = variables[x]["filename"] byte = open(os.path.join(base_dir, chkpoint, filename), 'rb').read() fmt = str(int(len(byte) / struct.calcsize('f'))) + 'f' d = struct.unpack(fmt, byte) d = tf.cast(d, tf.float32) d = tf.reshape(d, variables[x]["shape"]) variables[x]["x"] = tf.Variable(d, name=x) return variables