DGU-PoliceLab / System_Integration

This is PoliceLab 2.0 AI module repository.
GNU General Public License v3.0
1 stars 0 forks source link

HRI Emotion 모듈에서 오류 발생 #6

Closed qqaazz0222 closed 3 months ago

qqaazz0222 commented 3 months ago

얼굴 감지 가중치 불러올때 오류 발생

Resnet50_Final.pth

Downloading: "./_HAR/HRI/Models/Resnet50_Final.pth" to /root/.cache/torch/hub/checkpoints/Resnet50_Final.pth
Process Process-3:
Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/opt/conda/lib/python3.10/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/System_Integration/_HAR/HRI/emotion.py", line 35, in Emotion
    detector = face_detection.build_detector(args.face_detector, confidence_threshold=.5, nms_iou_threshold=.3)
  File "/System_Integration/_HAR/HRI/face_detection/build.py", line 34, in build_detector
    detector = build_from_cfg(args, DETECTOR_REGISTRY)
  File "/System_Integration/_HAR/HRI/face_detection/registry.py", line 73, in build_from_cfg
    return obj_cls(**args, **kwargs)
  File "/System_Integration/_HAR/HRI/face_detection/retinaface/detect.py", line 136, in __init__
    super().__init__("resnet50", *args, **kwargs)
  File "/System_Integration/_HAR/HRI/face_detection/retinaface/detect.py", line 35, in __init__
    state_dict = load_state_dict_from_url(
  File "/opt/conda/lib/python3.10/site-packages/torch/hub.py", line 766, in load_state_dict_from_url
    download_url_to_file(url, cached_file, hash_prefix, progress=progress)
  File "/opt/conda/lib/python3.10/site-packages/torch/hub.py", line 619, in download_url_to_file
    req = Request(url, headers={"User-Agent": "torch.hub"})
  File "/opt/conda/lib/python3.10/urllib/request.py", line 322, in __init__
    self.full_url = url
  File "/opt/conda/lib/python3.10/urllib/request.py", line 348, in full_url
    self._parse()
  File "/opt/conda/lib/python3.10/urllib/request.py", line 377, in _parse
    raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: './_HAR/HRI/Models/Resnet50_Final.pth'
qqaazz0222 commented 3 months ago

원인

_/System_Integration/HAR/HRI/emotion.py line:35

detector = face_detection.build_detector(args.face_detector, confidence_threshold=.5, nms_iou_threshold=.3)

위 파일에서 디텍터를 생성할 때, 모델 이름을 통해 torch hub에서 가중치 파일을 다운로드 설정된 다운로드 주소에 접근이 불가능함.

해결방법

__/opt/conda/lib/python3.10/site-packages/torch/hub.py func:load_state_dict_fromurl

def load_state_dict_from_url(
    url: str,
    model_dir: Optional[str] = None,
    map_location: MAP_LOCATION = None,
    progress: bool = True,
    check_hash: bool = False,
    file_name: Optional[str] = None,
    weights_only: bool = False,
) -> Dict[str, Any]:
    r"""Loads the Torch serialized object at the given URL.

    If downloaded file is a zip file, it will be automatically
    decompressed.

    If the object is already present in `model_dir`, it's deserialized and
    returned.
    The default value of ``model_dir`` is ``<hub_dir>/checkpoints`` where
    ``hub_dir`` is the directory returned by :func:`~torch.hub.get_dir`.

    Args:
        url (str): URL of the object to download
        model_dir (str, optional): directory in which to save the object
        map_location (optional): a function or a dict specifying how to remap storage locations (see torch.load)
        progress (bool, optional): whether or not to display a progress bar to stderr.
            Default: True
        check_hash(bool, optional): If True, the filename part of the URL should follow the naming convention
            ``filename-<sha256>.ext`` where ``<sha256>`` is the first eight or more
            digits of the SHA256 hash of the contents of the file. The hash is used to
            ensure unique names and to verify the contents of the file.
            Default: False
        file_name (str, optional): name for the downloaded file. Filename from ``url`` will be used if not set.
        weights_only(bool, optional): If True, only weights will be loaded and no complex pickled objects.
            Recommended for untrusted sources. See :func:`~torch.load` for more details.

    Example:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_HUB)
        >>> state_dict = torch.hub.load_state_dict_from_url('https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth')

    """
    # Issue warning to move data if old env is set
    if os.getenv('TORCH_MODEL_ZOO'):
        warnings.warn('TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME instead')

    if model_dir is None:
        hub_dir = get_dir()
        model_dir = os.path.join(hub_dir, 'checkpoints')

    try:
        os.makedirs(model_dir)
    except OSError as e:
        if e.errno == errno.EEXIST:
            # Directory already exists, ignore.
            pass
        else:
            # Unexpected OSError, re-raise.
            raise

    parts = urlparse(url)
    filename = os.path.basename(parts.path)
    if file_name is not None:
        filename = file_name
    cached_file = os.path.join(model_dir, filename)
    if not os.path.exists(cached_file): **<<< 이쪽을 활용**
        sys.stderr.write(f'Downloading: "{url}" to {cached_file}\n')
        hash_prefix = None
        if check_hash:
            r = HASH_REGEX.search(filename)  # r is Optional[Match[str]]
            hash_prefix = r.group(1) if r else None
        download_url_to_file(url, cached_file, hash_prefix, progress=progress)

    if _is_legacy_zip_format(cached_file):
        return _legacy_zip_load(cached_file, model_dir, map_location, weights_only)
    return torch.load(cached_file, map_location=map_location, weights_only=weights_only)

위 함수에서 캐시된 파일이 있으면, 다운로드하지 않고 캐시된 파일을 사용하는 것을 확인 캐시 경로에 Resnet50_Final.pth을 복사해 두는 것으로 문제 해결

setting.sh 수정

setting.sh에 아래 명령어 추가

cp /System_Integration/_HAR/HRI/models/Resnet50_Final.pth /root/.cache/torch/hub/checkpoints/