bowang-lab / ECG-FM

An electrocardiogram analysis foundation model.
MIT License
92 stars 4 forks source link

tutorial inference question #2

Open jihyeheo opened 1 month ago

jihyeheo commented 1 month ago

hi? I'm trying to apply ecg-fm, but the process of importing physionet_finetune.pt doesn't work. 5. In load model! The mimic pt works well, but why is that?

root = os.getcwd() root model_pretrained = build_model_from_checkpoint( checkpoint_path=os.path.join(root, 'ckpts', 'physionet_finetuned.pt') ) model_pretrained

KadenMc commented 1 month ago

Are you on the latest versions of both ECG-FM and fairseq-signals repositories? If not, see if that fixes your issue.

Otherwise, please report back the error message you receive, as well as information about your virtual environment.

chenqi-li commented 3 weeks ago

Hi I am facing a similar issue, where I cannot load the physionet_finetuned.pt.

I have installed the latest ECG-FM and fairseq-signals. I tested with the code below.

import os
import pandas as pd
import torch

from fairseq_signals.utils.store import MemmapReader

# Manage directories
root = os.getcwd()
fairseq_signals_root = '/users/fairseq-signals'
fairseq_signals_root = fairseq_signals_root.rstrip('/')

# Download finetuned checkpoint
from huggingface_hub import hf_hub_download
_ = hf_hub_download(
    repo_id='wanglab/ecg-fm-preprint',
    filename='physionet_finetuned.pt',
    local_dir=os.path.join(root, 'ckpts'),
)
_ = hf_hub_download(
    repo_id='wanglab/ecg-fm-preprint',
    filename='physionet_finetuned.yaml',
    local_dir=os.path.join(root, 'ckpts'),
)

# Load model
from fairseq_signals.models import build_model_from_checkpoint
model_finetuned = build_model_from_checkpoint(
    checkpoint_path=os.path.join(root, 'ckpts/physionet_finetuned.pt')
)

I am getting the error

Traceback (most recent call last):
  File "/users/ECG-FM/inference_example.py", line 31, in <module>
    model_finetuned = build_model_from_checkpoint(
  File "/users/fairseq-signals/fairseq_signals/models/__init__.py", line 106, in build_model_from_checkpoint
    return build_model(model_cfg, task=None, from_checkpoint=True, checkpoint_path=checkpoint_path)
  File "/users/fairseq-signals/fairseq_signals/models/__init__.py", line 93, in build_model
    model_instance = model.build_model(cfg, task)
  File "/users/fairseq-signals/fairseq_signals/models/ecg_transformer.py", line 260, in build_model
    encoder = ECGTransformerModel.from_pretrained(cfg.model_path, cfg)
  File "/users/fairseq-signals/fairseq_signals/models/ecg_transformer.py", line 232, in from_pretrained
    model = super().from_pretrained(model_path, cfg, arg_overrides)
  File "/users/fairseq-signals/fairseq_signals/models/transformer.py", line 237, in from_pretrained
    return super().from_pretrained(model_path, cfg, arg_overrides, **kwargs)
  File "/users/fairseq-signals/fairseq_signals/models/pretraining_model.py", line 81, in from_pretrained
    model = task.build_model(args.model)
  File "/users/fairseq-signals/fairseq_signals/tasks/ecg_pretraining.py", line 380, in build_model
    model = super().build_model(model_cfg)
  File "/users/fairseq-signals/fairseq_signals/tasks/task.py", line 291, in build_model
    model = models.build_model(cfg, self, from_checkpoint)
  File "/users/fairseq-signals/fairseq_signals/models/__init__.py", line 93, in build_model
    model_instance = model.build_model(cfg, task)
  File "/users/fairseq-signals/fairseq_signals/models/ecg_transformer.py", line 260, in build_model
    encoder = ECGTransformerModel.from_pretrained(cfg.model_path, cfg)
  File "/users/fairseq-signals/fairseq_signals/models/ecg_transformer.py", line 232, in from_pretrained
    model = super().from_pretrained(model_path, cfg, arg_overrides)
  File "/users/fairseq-signals/fairseq_signals/models/transformer.py", line 237, in from_pretrained
    return super().from_pretrained(model_path, cfg, arg_overrides, **kwargs)
  File "/users/fairseq-signals/fairseq_signals/models/pretraining_model.py", line 63, in from_pretrained
    state = checkpoint_utils.load_checkpoint_to_cpu(model_path, arg_overrides)
  File "/users/fairseq-signals/fairseq_signals/utils/checkpoint_utils.py", line 267, in load_checkpoint_to_cpu
    with open(local_path, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '<REDACTED>'

In the physionet_finetuned.pt checkpoint, the model.model_path variable in the cfg is REDACTED, and the build_model_from_checkpoint function appears to use that variable?

doheon114 commented 2 weeks ago

It seems that the publisher of fairseq-signals has fixed the related issue. I was experiencing the same problem, but it now works smoothly. Try updating fairseq-signals to the latest commit and give it another shot.