Skip to content

Issue with latest_weights_file_path() function #37

Open
@Xie-yx

Description

@Xie-yx

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions