# MindSpore的VIT报错\[OneHot\] failed. OneHot: index values should not bigger than num classes: 100, but got: 100.

**1. 系统环境**

硬件环境\(Ascend/GPU/CPU\): Ascend/GPU/CPU

软件环境: MindSpore 版本: 2.0.0

执行模式（静态图/动态图）： 不限

Python 版本: 3.9

操作系统平台: linux

**2. 报错信息**

2.1 问题描述

使用Imagenet数据集，设置了100个类型的图片分类。

```
{'autoaugment': 1, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 100, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': -1, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
Please check the above information for the configurations 
standalone training 
######################################## 
{'auto_tune': 0, 
 'autoaugment': 1, 
 'aux_factor': 0.4, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 100, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_name': 'imagenet', 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'device_id': 0, 
 'device_num': 1, 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_engine': 'imagenet', 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': 0, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'local_rank': 0, 
 'logger': <LOGGER mindspore-benchmark (NOTSET)>, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'poly_power': 2, 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'seed': 1, 
 'split_point': 0.4, 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
######################################## 
get vit_config_path 
split_point=0.4 
============================================================ 
{'auto_tune': 0, 
 'autoaugment': 1, 
 'aux_factor': 0.4, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 100, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_name': 'imagenet', 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'device_id': 0, 
 'device_num': 1, 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_engine': 'imagenet', 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': 0, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'local_rank': 0, 
 'logger': <LOGGER mindspore-benchmark (NOTSET)>, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'poly_power': 2, 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'seed': 1, 
 'split_point': 0.4, 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
============================================================ 
```

**3. 根因分析**

imagenet数据集是class=100，但是0-100表示的是101个类，不是100个类，所以需要设置成0-99。

**4. 解决方案**

将代码中的'class\_num': 100,修改为'class\_num': 99.

```
{'autoaugment': 1, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 99, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': -1, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
Please check the above information for the configurations 
standalone training 
######################################## 
{'auto_tune': 0, 
 'autoaugment': 1, 
 'aux_factor': 0.4, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 99, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_name': 'imagenet', 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'device_id': 0, 
 'device_num': 1, 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_engine': 'imagenet', 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': 0, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'local_rank': 0, 
 'logger': <LOGGER mindspore-benchmark (NOTSET)>, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'poly_power': 2, 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'seed': 1, 
 'split_point': 0.4, 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
######################################## 
get vit_config_path 
split_point=0.4 
============================================================ 
{'auto_tune': 0, 
 'autoaugment': 1, 
 'aux_factor': 0.4, 
 'backbone': 'vit_base_patch32', 
 'batch_size': 8, 
 'beta1': 0.9, 
 'beta2': 0.999, 
 'class_num': 99, 
 'config_path': 'vit_patch32_imagenet2012_config_standalone.yml', 
 'crop_min': 0.05, 
 'dataset_name': 'imagenet', 
 'dataset_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/train', 
 'device_id': 0, 
 'device_num': 1, 
 'enable_modelarts': 0, 
 'eval_batch_size': 4, 
 'eval_engine': 'imagenet', 
 'eval_image_size': 224, 
 'eval_interval': 1, 
 'eval_num_workers': 2, 
 'eval_offset': 0, 
 'eval_path': '/data/zx_cv/vit/models-master/VIT/cache/datasets/imagenet_cifa/tiny-new/datasets/val', 
 'gc_flag': 0, 
 'interpolation': 'BILINEAR', 
 'keep_checkpoint_max': 3, 
 'label_smooth_factor': 0.1, 
 'local_rank': 0, 
 'logger': <LOGGER mindspore-benchmark (NOTSET)>, 
 'loss_name': 'ce_smooth_mixup', 
 'loss_scale': 1024, 
 'lr_decay_mode': 'cosine', 
 'lr_init': 0.001, 
 'lr_max': 0.00044375, 
 'lr_min': 1e-08, 
 'max_epoch': 300, 
 'mixup': 0.2, 
 'no_weight_decay_filter': 'beta,bias', 
 'open_profiler': 0, 
 'opt': 'adamw', 
 'poly_power': 2, 
 'pretrained': '', 
 'save_checkpoint': 1, 
 'save_checkpoint_epochs': 8, 
 'save_checkpoint_path': './outputs', 
 'seed': 1, 
 'split_point': 0.4, 
 'train_image_size': 224, 
 'train_num_workers': 1, 
 'use_label_smooth': 1, 
 'vit_config_path': 'src.vit.VitConfig', 
 'warmup_epochs': 0, 
 'weight_decay': 0.05} 
============================================================ 
```

