# Densenet121 **Repository Path**: linesite/densenet121 ## Basic Information - **Project Name**: Densenet121 - **Description**: densenet121 - **Primary Language**: Python - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-12 - **Last Updated**: 2022-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Contents - [DenseNet-121 介绍](#DenseNet121-介绍) - [模型架构](#模型架构) - [数据集](#数据集) - [环境要求](#环境要求) - [快速开始](#快速开始) - [脚本介绍](#脚本介绍) - [脚本以及简单代码](#脚本以及简单代码) - [脚本参数](#脚本参数) - [训练步骤](#训练步骤) - [训练](#训练) - [评估步骤](#评估步骤) - [评估](#评估) - [模型介绍](#模型介绍) - [性能](#性能) - [评估性能](#评估性能) - [如何使用](#如何使用) - [教程](#教程) - [随机事件介绍](#随机事件介绍) - [ModelZoo 主页](#ModelZoo-主页) # [DenseNet-121 介绍](#contents) DenseNet,作为CVPR2017年的最佳论文,其通过前馈样式使每层网络与其他各层网络相连接。在传统的L层卷积神经网络中,共有L个连接,每个连接作用于当前网络层和其后的网络层。而在DenseNet中,一共有L(L+1)/2个连接。对于每一层网络层,其之前所有网络层的特征图都会作为此层的输入,而其自身的特征图会作为其后每一层网络层的输入。DenseNet拥有许多值得关注的优势,如大幅度减少了网络的参数量,加强特征传播,支持特征复用,同时也在一定程度上缓解了gradient vanishing问题的产生。DenseNet-121中的121指各个Dense Block中卷积层的深度。 [Paper]: Gao Huang, Zhuang Liu, Laurens van der Maaten, Kilian Q. Weinberger. "Densely Connected Convolutional Networks." In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2017: 4700-4708. # [模型架构](#contents) CNN网络一般要经过Pooling或者stride>1的Conv来降低特征图的大小,而DenseNet的密集连接方式需要特征图大小保持一致。为了解决这个问题,DenseNet网络中使用DenseBlock+Transition的结构,其中DenseBlock是包含很多层的模块,每个层的特征图大小相同,层与层之间采用密集连接方式。而Transition模块用来连接两个相邻的DenseBlock,并且通过Pooling使特征图大小降低。DenseNet-121网络结构共包含4个DenseBlock,各个DenseBlock之间通过Transition Layer连接。 # [数据集](#contents) Dataset used: [ImageNet]() Dataset size: ~125G, 1.2W colorful images in 1000 classes Train: 120G, 1.2W images Test: 5G, 50000 images Data format: RGB images. Note: Data will be processed in src/dataset_new.py # [环境要求](#contents) - 硬件(Ascend/GPU) - 需要准备具有Ascend或GPU处理能力的硬件环境. 如需使用Ascend,可以发送 [application form](https://obs-9be7.obs.cn-east-2.myhuaweicloud.com/file/other/Ascend%20Model%20Zoo%E4%BD%93%E9%AA%8C%E8%B5%84%E6%BA%90%E7%94%B3%E8%AF%B7%E8%A1%A8.docx) 到ascend@huawei.com。一旦批准,你就可以使用此资源 - 框架 - [MindSpore](https://www.mindspore.cn/install/en) - 如需获取更多信息,请查看如下链接: - [MindSpore Tutorials](https://www.mindspore.cn/tutorial/training/en/master/index.html) - [MindSpore Python API](https://www.mindspore.cn/doc/api_python/en/master/index.html) # [快速开始](#contents) 在通过官方网站安装MindSpore之后,你可以通过如下步骤开始训练以及评估: - runing on Ascend ```python # run training example python train.py --device_id device_id # run evaluation example python eval.py --device_id device_id ``` 我们默认使用ImageNet数据集 # [脚本介绍](#contents) ## [脚本以及简单代码](#contents) ``` ├── model_zoo ├── README.md // descriptions about all the models ├── DenseNet121 ├── README.md // descriptions about Densenet-121 ├── scripts │ ├── run_train.sh // shell script on Ascend │ ├── run_eval.sh // shell script on Ascend ├── src │ ├── dataset.py // creating dataset │ ├── config.py // parameter configuration │ ├── sampler.py │ ├── crossentropy.py │ ├──utils │ ├──logging.py // logging function │ ├──var_init.py // densenet variable init function │ ├── network │ ├──densenet.py // densenet architecture ├── train.py // training script ├── eval.py // evaluation script ``` ## [脚本参数](#contents) 训练以及评估的参数可以在config.py中设置 - config for DenseNet-121 dataset ```python 'nump_classes': 1000 # the number of classes in the dataset 'learning_rate': 0.002 # initial learning rate 'batch_size': 32 # training batch size 'epoch_size': 120 # total training epochs 'momentum': 0.9 # momentum 'buffer_size': 1000 # buffer size 'image_height': 227 # image height used as input to the model 'image_width': 227 # image width used as input to the model 'keep_checkpoint_max': 5 # only keep the last keep_checkpoint_max checkpoint 'save_checkpoint_steps': 50000 # save checkpoint per a number of steps 'save_checkpoint': True # whether saving the checkpoint 'save_checkpoint_path': "/home/nuaa/densenet/checkpoint_densenet-120_5004.ckpt" # the absolute full path to save the checkpoint file 'save_checkpoint_epochs': 4 'pretrained': False 'data_path': "/data/dataset/ImageNet/imagenet_original/" %the absolute full path of data ``` 如需获取更多信息,请查看`config.py`. ## [训练步骤](#contents) ### 训练 - running on Ascend ``` python train.py --device_id device_id ``` 训练时,训练过程中的epch和step以及此时的loss和精确度会呈现在终端上: ``` epoch: 1 step: 5004, loss is 3.641 epcoh: 2 step: 5004, loss is 3,540 ... ``` 此模型的checkpoint会在默认路径下存储 ## [评估步骤](#contents) ### 评估 - 在Ascend上使用ImageNet进行评估 在使用命令运行前,请检查用于评估的checkpoint的路径。请设置路径为到checkpoint的绝对路径,如 "/user/densenet/ckpt/ImageNet_ckt"。 ``` python eval.py --device_id device_id ``` 以上的python命令会在终端上运行,你可以在终端上查看此次评估的结果。测试集的精确度会以如下方式呈现: ``` Evaluation result:{acc(top1): 75.11628721190782 acc(top5): 92.28409757789159} ``` # [模型介绍](#contents) ## [性能](#contents) ### 评估性能 #### DenseNet-121 on ImageNet | Parameters | Ascend | -------------------------- | ----------------------------------------------------------- | Model Version | DenseNet-121 | Resource | Ascend: 8*Ascend 910 CPU: 192核 768GIB | uploaded Date | 10/19/2020 (month/day/year) | MindSpore Version | 0.5.0 | Dataset | ImageNet | Training Parameters | epoch=120, steps=5004, batch_size = 32, lr=0.002 | Optimizer | Momentum | Loss Function | Cross Entropy | outputs | probability | Loss | 0.917 | Speed | 8pc: 42.493 ms/step | Total time | 8pc: 452.99 mins | Scripts | [DenseNet121 script](https://gitee.com/linesite/densenet121/tree/master/script) | [DenseNet121 script](https://gitee.com/linesite/densenet121/tree/master/script) | ### Inference Performance #### DenseNet-121 on ImageNet | Parameters | Ascend | ------------------- | --------------------------- | Model Version | DenseNet-121 | Resource | Ascend: 8*Ascend 910 CPU: 192核 768GIB | Uploaded Date | 10/19/2020 (month/day/year) | MindSpore Version | 0.5.0 | Dataset | ImageNet | batch_size | 32 | outputs | probability | Accuracy | 8pc: TOP1:75.11% TOP5:92.88% ## [如何使用](#contents) ### 教程 如果你需要在不同硬件平台(如GPU,Ascend 910 或者 Ascend 310)使用训练好的模型,你可以参考这个 [Link](https://www.mindspore.cn/tutorial/training/en/master/advanced_use/migrate_3rd_scripts.html)。以下是一个简单例子的步骤介绍: - Running on Ascend ``` # Set context context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.device_target, save_graphs=False) context.set_auto_parallel_context(device_num=device_num,parallel_mode=ParallelMode.DATA_PARALLEL) init() # Load unseen dataset for inference train_dataset = classification_dataset(local_data_path+"/train", image_size=[227, 227],per_batch_size=32, max_epoch=120,rank=devid, group_size=device_num, num_parallel_workers=8) # Define model net = DenseNet121(class_num) opt = Momentum(net.trainable_params(), lr, momentum=0.9, weight_decay=1e-4, loss_scale=1024) loss_scale = FixedLossScaleManager(loss_scale_num, False) loss = CrossEntropy(smooth_factor=0.0,num_classes=1000) model = Model(net, amp_level="O3", keep_batchnorm_fp32=False, loss_fn=loss, optimizer=opt, loss_scale_manager=loss_scale, metrics={'acc'}) # Make predictions on the unseen dataset acc = model.eval(dataset) print("accuracy: ", acc) ``` # [随机事件介绍](#contents) 我们在train.py中设置了随机种子 # [ModelZoo 主页](#contents) 请查看官方网站 [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).