Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with latest_weights_file_path() function #37

Open
Xie-yx opened this issue Mar 7, 2025 · 0 comments
Open

Issue with latest_weights_file_path() function #37

Xie-yx opened this issue Mar 7, 2025 · 0 comments

Comments

@Xie-yx
Copy link

Xie-yx commented Mar 7, 2025

Hi @hkproj ,

I found an issue with the latest_weights_file_path() function in config.py.

The original code uses weights_files.sort() to sort the files and selects the last one as the latest file. However, sort() uses lexicographical (dictionary) order, which can lead to incorrect results in some cases.

Here is the original code:

def latest_weights_file_path(config):
    model_folder = f"{config['datasource']}_{config['model_folder']}"
    model_filename = f"{config['model_basename']}*"
    weights_files = list(Path(model_folder).glob(model_filename))
    if len(weights_files) == 0:
        return None
    weights_files.sort()
    return str(weights_files[-1])

A corrected version, suggested by ChatGPT, is as follows:

def latest_weights_file_path(config):
    model_folder = f"{config['datasource']}_{config['model_folder']}"
    model_filename = f"{config['model_basename']}*.pt"  # 确保匹配 `.pt` 文件
    weights_files = list(Path(model_folder).glob(model_filename))
    
    if not weights_files:
        return None

    def extract_number(f):
        match = re.search(r'_(\d+)\.pt$', f.name)
        return int(match.group(1)) if match else -1

    latest_file = max(weights_files, key=extract_number) 
    return str(latest_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant