邂逅的光阴 2024-04-28 18:36 采纳率: 0%
浏览 5

(标签-python|关键词-for)

问题描述:在执行python run_mindformer.py --config ./configs/clip/run_clip_vit_b_32_zero_shot_image_classification_cifar100.yaml --run_mode eval --eval_dataset_dir /home/ma-user/work/mindformers/data/cifar-100-python命令,评估时候出现2024-04-28 18:17:06,086 - mindformers[mindformers/models/modeling_utils.py:1437] - INFO - model built, but weights is unloaded, since the config has no checkpoint_name_or_path attribute or checkpoint_name_or_path is None.结果中Top1 Accuracy = {'Top1 Accuracy': 0.0135},准确率才0.0135,所以我认为可能是预训练权重未加载上,下面是我的配置文件:

seed: 0
run_mode: 'eval'
output_dir: './output' # path to save checkpoint/strategy
load_checkpoint: ''
src_strategy_path_or_dir: ''
auto_trans_ckpt: False  # If true, auto transform load_checkpoint to load in distributed model
only_save_strategy: False
resume_training: False

# context
context:
  mode: 0 #0--Graph Mode; 1--Pynative Mode
  device_target: "Ascend"
  enable_graph_kernel: False
  graph_kernel_flags: "--opt_level=0"
  max_call_depth: 10000
  save_graphs: False
  device_id: 0

# aicc
remote_save_url: "Please input obs url on AICC platform."

# runner
runner_config:
  epochs: 30
  batch_size: 32
  image_size: 224
  sink_mode: False
  sink_size: 2

# parallel
use_parallel: False
parallel:
  parallel_mode: 0 # 0-data parallel, 1-semi-auto parallel, 2-auto parallel, 3-hybrid parallel
  gradients_mean: True
  enable_alltoall: False
  full_batch: False
  search_mode: "sharding_propagation"
  enable_parallel_optimizer: False
  strategy_ckpt_save_file: "./ckpt_strategy.ckpt"
parallel_config:
  data_parallel: 1
  model_parallel: 1
  expert_parallel: 1
  pipeline_stage: 1
  micro_batch_num: 1
  gradient_aggregation_group: 4
micro_batch_interleave_num: 1

# recompute
recompute_config:
  recompute: False
  parallel_optimizer_comm_recompute: False
  mp_comm_recompute: True
  recompute_slice_activation: False

# autotune
auto_tune: False
filepath_prefix: './autotune'
autotune_per_step: 10

# profile
profile: False
profile_start_step: 1
profile_stop_step: 10
init_start_profile: False
profile_communication: False
profile_memory: True

# Trainer
trainer:
  type: ZeroShotImageClassificationTrainer
  model_name: 'clip_vit_b_32'

# eval dataset
eval_dataset:  &eval_dataset
  data_loader:
    type: Cifar100DataLoader
    dataset_dir: 'cifar-100-python'
    column_names: ["image", "text", "label"]
    stage: 'test'
    fine_label: True
    shuffle: False
    hypothesis_template: "a picture of {}"
  tokenizer:
    type: CLIPTokenizer
    pad_token: '!'
  text_transforms:
    type: TokenizerForward
    max_length: 77
    padding: "max_length"
  transforms:
    - type: ToPIL
    - type: Resize
      size: 224
      interpolation: 'linear'
    - type: CenterCrop
      size: 224
    - type: ToTensor
    - type: Normalize
      mean: [0.48145466, 0.4578275, 0.40821073]
      std: [0.26862954, 0.26130258, 0.27577711]
      is_hwc: False
  num_parallel_workers: 8
  python_multiprocessing: False
  drop_remainder: False
  batch_size: 32
  repeat: 1
  numa_enable: False
  prefetch_size: 30
  seed: 2022
eval_dataset_task:
  type: ZeroShotImageClassificationDataset
  dataset_config: *eval_dataset

# model
model:
  arch:
    type: CLIPModel
  model_config:
    type: CLIPConfig
    text_config:
      type: CLIPTextConfig
      vocab_size: 49408
      hidden_size: 512
      intermediate_size: 2048
      num_hidden_layers: 12
      num_attention_heads: 8
      max_position_embeddings: 77
      hidden_act: "quick_gelu"
      attention_dropout: 0.0
      drop_out: 0.0
      initializer_range: 0.02
      initializer_factor: 1.0
    vision_config:
      type: CLIPVisionConfig
      hidden_size: 768
      intermediate_size: 3072
      num_hidden_layers: 12
      num_attention_heads: 12
      image_size: 224
      patch_size: 32
      hidden_act: "quick_gelu"
      dropout: 0.0
      attention_dropout: 0.0
      initializer_range: 0.02
      initializer_factor: 1.0
    dtype: float16  # type of tensors
    checkpoint_name_or_path: clip_vit_b_32   # the loaded model type
    projection_dim: 512  # feature dims
    logit_scale_init_value: 2.6592

# lr sechdule
layer_scale: False
layer_decay: 0.65

# optimizer
optimizer:
  type: adamw
  weight_decay: 0.001
  learning_rate: 0.00001
lr_scale: False
lr_scale_factor: 256

# callbacks
callbacks:
  - type: MFLossMonitor
  - type: CheckpointMointor
    prefix: "mindformers"
    save_checkpoint_steps: 100
    integrated_save: True
    async_save: False
  - type: ObsMonitor
eval_callbacks:
  - type: ObsMonitor

# metric
metric:
  type: 'Accuracy'

# processor
processor:
  type: CLIPProcessor
  image_processor:
    type: CLIPImageProcessor
    image_resolution: 224  # input image size
  tokenizer:
    type: CLIPTokenizer
    pad_token: '!' # corresponding token id is 0
top_k: 3
save_file: 'results.txt'
#save_file: '/home/ma-user/work/mindformers/results.txt'


我应该怎么解决该问题,而且预训练的时候生成的权重一个为mindformers_rank_0_1-network.ckpt,另一个为mindformers_rank_0_14-40_7.ckpt(这部分权重文件有三个)

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-28 18:36
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的信息,看起来模型确实没有加载预训练权重,因为配置中的checkpoint_name_or_path属性设置为空字符串。这导致了模型虽然构建完成,但没有加载任何预训练权重,因此在评估时表现不佳。

    要解决这个问题,你需要确保将正确的预训练模型路径添加到checkpoint_name_or_path字段。您有两个预训练权重文件,一个是mindformers_rank_0_1-network.ckpt,另一个是mindformers_rank_0_14-40_7.ckpt。由于这两个文件看起来像是多进程训练产生的多个部分,你需要合并它们,然后再指定合并后的权重文件作为checkpoint_name_or_path

    首先,我们需要合并这些分片文件。您可以使用MindSpore的工具merge_pdt_files.py来进行这个操作。假设这两个文件位于同一个目录下,您可以运行以下Python脚本进行合并:

    import os
    from mindspore.communication.management import get_rank, get_group_size
    
    def merge_files(file_prefix, output_file):
        rank = get_rank()
        group_size = get_group_size()
    
        if rank == 0:
            with open(output_file, 'wb') as fout:
                for i in range(group_size):
                    file_to_merge = f"{file_prefix}_{rank}_{i}.ckpt"
                    with open(file_to_merge, 'rb') as fin:
                        fout.write(fin.read())
        else:
            pass
    
    if __name__ == "__main__":
        file_prefix = "mindformers_rank_0_"  # 前缀
        output_file = "merged_model.ckpt"  # 合并后文件名
        merge_files(file_prefix, output_file)
    

    运行上述代码后,将会得到一个名为merged_model.ckpt的文件,这是两个分片文件合并的结果。然后,更新您的配置文件,将checkpoint_name_or_path设置为这个合并后的文件路径,例如:

    model:
      ...
      checkpoint_name_or_path: merged_model.ckpt  # 替换为合并后的文件路径
      ...
    

    保存更改后的配置文件,然后重新运行评估命令:

    python run_mindformer.py --config ./configs/clip/run_clip_vit_b_32_zero_shot_image_classification_cifar100.yaml --run_mode eval --eval_dataset_dir /home/ma-user/work/mindformers/data/cifar-100-python
    

    这样应该可以正确加载预训练权重,并提高模型在CIFAR-100上的评估性能。如果仍然有问题,可能需要检查其他配置参数,比如数据集处理、模型结构或优化器设置是否合适。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月28日

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表