Skip to content

Trouble setting num_ranks != num_base_ranks #9

@yoxerao

Description

@yoxerao

Hey there!
Great paper and solution for ordinal regression.
I'm trying to use the code for aesthetic evaluation with 4 ranks; however, when if I try setting the num_base_ranks to 2, I get a mismatch error (shown below). From my understanding of the paper, it should be possible and possibly better to set num_base_ranks to a number below the number of actual ranks, correct?

My config:

runner_cfg:
  seed: 114514
  output_dir: results/aesthetic
  optimizer_and_scheduler_cfg:
    param_dict_cfg:
      lr_prompt_learner_context: 0.0001
      lr_prompt_learner_ranks: 0.0001
      lr_image_encoder: 0.0001
      lr_text_encoder: 0.0
      lr_logit_scale: 0.0
      staged_lr_image_encoder:
        new_layers: []
        base_lr_mult: 1.0
        new_lr_mult: 1.0
    optimizer_cfg:
      optimizer_name: radam
      lr: 0.0001
      weight_decay: 5.0e-4
    lr_scheduler_cfg:
      lr_scheduler_name: cosine
      max_epochs: 50
  load_weights_cfg:
    init_model_weights: null
    init_prompt_learner_weights: null
    init_image_encoder_weights: null
    init_text_encoder_weights: null
  model_cfg:
    type: OrdinalCLIP
    prompt_learner_cfg:
      type: RankPromptLearner
      num_ranks: 4 
      num_base_ranks: 2
      num_tokens_per_rank: 1
      num_context_tokens: 10
      rank_tokens_position: tail
      init_rank_path: OrdinalCLIP/data/AESTHETIC/classnames.txt
      init_context: "The aesthetic outcome of this breast cancer treatment is"
      rank_specific_context: false
      interpolation_type: linear
    text_encoder_name: RN50
    image_encoder_name: RN50
data_cfg:
  train_images_root: ""
  val_images_root: ""
  test_images_root: ""
  train_data_file: OrdinalCLIP/data/AESTHETIC/data_list/train.txt
  val_data_file: OrdinalCLIP/data/AESTHETIC/data_list/val.txt
  test_data_file: OrdinalCLIP/data/AESTHETIC/data_list/test.txt
  few_shot:
    num_shots: 0
  label_distributed_shift:
    num_topk_scaled_class: 0
    scale_factor: 1.0
  use_long_tail: false
  transforms_cfg:
    input_transforms:
      - random_resized_crop
      - random_hflip
      - normalize
    input_resize:
      - 256
      - 256
    input_size:
      - 224
      - 224
    pixel_mean:
      - 0.485
      - 0.456
      - 0.406
    pixel_std:
      - 0.229
      - 0.224
      - 0.225
  train_dataloder_cfg:
    num_workers: 8
    batch_size: 32
    shuffle: true
  eval_dataloder_cfg:
    num_workers: 8
    batch_size: 32
    shuffle: false
trainer_cfg:
  max_epochs: 50
  precision: 16
  accelerator: gpu
  devices: 1
  check_val_every_n_epoch: 1
  num_sanity_val_steps: 1
  fast_dev_run: false
test_only: false

my class names text file:

Poor
Fair
Good
Excellent

The error trace:

2025-03-24 18:52:37,695 - ordinalclip.runner.create_dataloaders - INFO - Train: (982, 8), Val: (210, 8), Test: (211, 8)
2025-03-24 18:52:37,754 - ordinalclip.utils.registry - INFO - Build obj: OrdinalCLIP
2025-03-24 18:52:37,755 - ordinalclip.models.ordinalclip - INFO - Building CLIP model...
2025-03-24 18:52:37,756 - ordinalclip.models.ordinalclip - INFO - Text backbone : RN50's counterpart.
2025-03-24 18:52:43,399 - ordinalclip.models.ordinalclip - INFO - Image backbone: RN50
2025-03-24 18:52:43,400 - ordinalclip.models.ordinalclip - INFO - CLIP Image encoder: RN50!
2025-03-24 18:52:43,503 - ordinalclip.models.ordinalclip - INFO - convert `clip_model` to float32. if need fp16 model, call `clip.model.convert_weights`
2025-03-24 18:52:43,503 - ordinalclip.utils.registry - INFO - Build obj: RankPromptLearner
2025-03-24 18:52:43,504 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - init context token
2025-03-24 18:52:43,504 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - init context: The aesthetic outcome of this breast cancer treatment is
2025-03-24 18:52:43,515 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - num_context_tokens: 10 -> 9
2025-03-24 18:52:43,596 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - rank_specific_context: False
2025-03-24 18:52:43,597 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - load init rank from: OrdinalCLIP/data/AESTHETIC/classnames.txt.
2025-03-24 18:52:43,601 - ordinalclip.models.prompt_leaners.rank_prompt_learner - INFO - num rank: 4:
	['Poor', 'Fair', 'Good', 'Excellent']
	['Poor', 'Fair', 'Good', 'Excellent']
Traceback (most recent call last):
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/models/prompt_leaners/rank_prompt_learner.py", line 58, in __init__
    rank_embeds, _num_tokens_per_rank = self.create_rank_embeds(
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/models/prompt_leaners/plain_prompt_learner.py", line 157, in create_rank_embeds
    raise ValueError(
ValueError: The length of rank_names is 4, which is not equal to num_ranks 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/models/ordinalclip.py", line 44, in __init__
    self.prompt_learner: PlainPromptLearner = PROMPT_LEARNERS.build(prompt_learner_cfg)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 210, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f"{obj_cls.__name__}: {e}, in {obj_cls}, args: {args.keys()}")
ValueError: RankPromptLearner: The length of rank_names is 4, which is not equal to num_ranks 2, in <class 'ordinalclip.models.prompt_leaners.rank_prompt_learner.RankPromptLearner'>, args: dict_keys(['num_ranks', 'num_base_ranks', 'num_tokens_per_rank', 'num_context_tokens', 'rank_tokens_position', 'init_rank_path', 'init_context', 'rank_specific_context', 'interpolation_type', 'clip_model'])

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "OrdinalCLIP/scripts/run_with_wandb.py", line 223, in <module>
    main()
  File "OrdinalCLIP/scripts/run_with_wandb.py", line 69, in main
    runner = PatchedRunner(**OmegaConf.to_container(cfg.runner_cfg))
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/runner/runner.py", line 36, in __init__
    self.module = MODELS.build(model_cfg)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 210, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/nas-ctm01/homes/fateixeira/tese/src/OrdinalCLIP/ordinalclip/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f"{obj_cls.__name__}: {e}, in {obj_cls}, args: {args.keys()}")
ValueError: OrdinalCLIP: RankPromptLearner: The length of rank_names is 4, which is not equal to num_ranks 2, in <class 'ordinalclip.models.prompt_leaners.rank_prompt_learner.RankPromptLearner'>, args: dict_keys(['num_ranks', 'num_base_ranks', 'num_tokens_per_rank', 'num_context_tokens', 'rank_tokens_position', 'init_rank_path', 'init_context', 'rank_specific_context', 'interpolation_type', 'clip_model']), in <class 'ordinalclip.models.ordinalclip.OrdinalCLIP'>, args: dict_keys(['prompt_learner_cfg', 'text_encoder_name', 'image_encoder_name'])

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