Loads MegEngine serialized object from the given URL. If the object is already present in ``model_dir``, it's deserialized and returned. If no ``model_dir`` is specified, it will be ``MGE_HOME/serialized``. Args: url: url to serialized object. model_dir: dir to cache ta
(url: str, model_dir=None)
| 219 | |
| 220 | |
| 221 | def load_serialized_obj_from_url(url: str, model_dir=None) -> Any: |
| 222 | """Loads MegEngine serialized object from the given URL. |
| 223 | |
| 224 | If the object is already present in ``model_dir``, it's deserialized and |
| 225 | returned. If no ``model_dir`` is specified, it will be ``MGE_HOME/serialized``. |
| 226 | |
| 227 | Args: |
| 228 | url: url to serialized object. |
| 229 | model_dir: dir to cache target serialized file. |
| 230 | |
| 231 | Returns: |
| 232 | loaded object. |
| 233 | """ |
| 234 | if model_dir is None: |
| 235 | model_dir = os.path.join(_get_megengine_home(), "serialized") |
| 236 | os.makedirs(model_dir, exist_ok=True) |
| 237 | |
| 238 | parts = urlparse(url) |
| 239 | filename = os.path.basename(parts.path) |
| 240 | |
| 241 | # use hash as prefix to avoid filename conflict from different urls |
| 242 | sha256 = hashlib.sha256() |
| 243 | sha256.update(url.encode()) |
| 244 | digest = sha256.hexdigest()[:6] |
| 245 | filename = digest + "_" + filename |
| 246 | |
| 247 | cached_file = os.path.join(model_dir, filename) |
| 248 | logger.info( |
| 249 | "load_serialized_obj_from_url: download to or using cached %s", cached_file |
| 250 | ) |
| 251 | if not os.path.exists(cached_file): |
| 252 | if is_distributed(): |
| 253 | logger.warning( |
| 254 | "Downloading serialized object in DISTRIBUTED mode\n" |
| 255 | " File may be downloaded multiple times. We recommend\n" |
| 256 | " users to download in single process first." |
| 257 | ) |
| 258 | download_from_url(url, cached_file) |
| 259 | |
| 260 | state_dict = _mge_load_serialized(cached_file) |
| 261 | return state_dict |
| 262 | |
| 263 | |
| 264 | class pretrained: |
no test coverage detected