用PaddlePaddle实现图像分类-DenseNet(动态图版)

项目简介

本项目基于paddle 动态图实现了图像分类模型DenseNet ,建议使用GPU来运行本项目,静态图版本请查看:基于PaddlePaddle的图像分类-DenseNet,具体介绍如下:

下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

DenseNet

相比ResNet,DenseNet提出了一个更激进的密集连接机制:即互相连接所有的层,具体来说就是每个层都会接受其前面所有层作为其额外的输入。图1为DenseNet的密集连接机制。可以看到,在DenseNet中,每个层都会与前面所有层在channel维度上连接(concat)在一起(这里各个层的特征图大小是相同的,后面会有说明),并作为下一层的输入。对于一个L层的网络,DenseNet共包含L(L+1)/2个连接,相比ResNet,这是一种密集连接。而且DenseNet是直接concat来自不同层的特征图,这可以实现特征重用,提升效率,这一特点是DenseNet与ResNet最主要的区别,具体结构如图所示。

用PaddlePaddle实现图像分类-DenseNet(动态图版)
图1. DenseNet连接机制

结构对比(CNN, ResNet, DenseNet):

用PaddlePaddle实现图像分类-DenseNet(动态图版)

DenseNet结构图

用PaddlePaddle实现图像分类-DenseNet(动态图版)

DenseNet核心思想在于建立了不同层之间的连接关系,充分利用了feature,进一步减轻了梯度消失问题,加深网络不是问题,而且训练效果非常好。

In[1]

# 解压花朵数据集   
!cd data/data2815 && unzip -qo flower_photos.zip

In[2]

# 预处理数据,将其转化为标准格式。同时将数据拆分成两份,以便训练和计算预估准确率 
import codecs   
import os   
import random   
import shutil   
from PIL import Image   
   
train_ratio = 4.0/ 5   
   
all_file_dir = 'data/data2815'   
class_list = [c for c in os.listdir(all_file_dir) if os.path.isdir(os.path.join(all_file_dir, c)) and not c.endswith('Set') and not c.startswith('.')]   
class_list.sort() 
print(class_list)   
train_image_dir = os.path.join(all_file_dir, "trainImageSet")   
if not os.path.exists(train_image_dir):   
    os.makedirs(train_image_dir)   
       
eval_image_dir = os.path.join(all_file_dir, "evalImageSet")   
if not os.path.exists(eval_image_dir):   
    os.makedirs(eval_image_dir)   
   
train_file = codecs.open(os.path.join(all_file_dir, "train.txt"), 'w')   
eval_file = codecs.open(os.path.join(all_file_dir, "eval.txt"), 'w')   
   
with codecs.open(os.path.join(all_file_dir, "label_list.txt"), "w") as label_list:   
    label_id = 0   
    for class_dir in class_list:   
        label_list.write("{0}/t{1}/n".format(label_id, class_dir))   
        image_path_pre = os.path.join(all_file_dir, class_dir)   
        for file in os.listdir(image_path_pre):   
            try:   
                img = Image.open(os.path.join(image_path_pre, file))   
                if random.uniform(0, 1) <= train_ratio:   
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(train_image_dir, file))   
                    train_file.write("{0}/t{1}/n".format(os.path.join(train_image_dir, file), label_id))   
                else:   
                    shutil.copyfile(os.path.join(image_path_pre, file), os.path.join(eval_image_dir, file))   
                    eval_file.write("{0}/t{1}/n".format(os.path.join(eval_image_dir, file), label_id))   
            except Exception as e:   
                pass   
                # 存在一些文件打不开,此处需要稍作清洗   
        label_id += 1   
               
train_file.close()   
eval_file.close()
['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

In[7]

#模型训练
!python train.py
W0309 14:06:23.309630   331 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0309 14:06:23.314054   331 device_context.cc:245] device: 0, cuDNN Version: 7.3.
2020-03-09 14:06:26,455-INFO: Loss at epoch 0 step 0: [1.6306293], acc: [0.40625]
2020-03-09 14:06:26,455 - train.py[line:164] - INFO: Loss at epoch 0 step 0: [1.6306293], acc: [0.40625]
2020-03-09 14:07:00,967-INFO: Loss at epoch 0 step 40: [1.4798427], acc: [0.265625]
2020-03-09 14:07:00,967 - train.py[line:164] - INFO: Loss at epoch 0 step 40: [1.4798427], acc: [0.265625]
2020-03-09 14:07:25,247-INFO: model saved at epoch 0, best accuracy is 0.4166666666666667
2020-03-09 14:07:25,247 - train.py[line:173] - INFO: model saved at epoch 0, best accuracy is 0.4166666666666667
2020-03-09 14:07:26,168-INFO: Loss at epoch 1 step 0: [1.2032264], acc: [0.46875]
2020-03-09 14:07:26,168 - train.py[line:164] - INFO: Loss at epoch 1 step 0: [1.2032264], acc: [0.46875]
2020-03-09 14:08:01,188-INFO: Loss at epoch 1 step 40: [1.1422644], acc: [0.578125]
2020-03-09 14:08:01,188 - train.py[line:164] - INFO: Loss at epoch 1 step 40: [1.1422644], acc: [0.578125]
2020-03-09 14:08:26,341-INFO: model saved at epoch 1, best accuracy is 0.45694444444444443
2020-03-09 14:08:26,341 - train.py[line:173] - INFO: model saved at epoch 1, best accuracy is 0.45694444444444443
2020-03-09 14:08:27,338-INFO: Loss at epoch 2 step 0: [1.2546736], acc: [0.5625]
2020-03-09 14:08:27,338 - train.py[line:164] - INFO: Loss at epoch 2 step 0: [1.2546736], acc: [0.5625]
2020-03-09 14:09:02,312-INFO: Loss at epoch 2 step 40: [1.2519169], acc: [0.640625]
2020-03-09 14:09:02,312 - train.py[line:164] - INFO: Loss at epoch 2 step 40: [1.2519169], acc: [0.640625]
2020-03-09 14:09:26,235-INFO: model saved at epoch 2, best accuracy is 0.5520833333333334
2020-03-09 14:09:26,235 - train.py[line:173] - INFO: model saved at epoch 2, best accuracy is 0.5520833333333334
2020-03-09 14:09:27,184-INFO: Loss at epoch 3 step 0: [1.1887345], acc: [0.625]
2020-03-09 14:09:27,184 - train.py[line:164] - INFO: Loss at epoch 3 step 0: [1.1887345], acc: [0.625]
2020-03-09 14:10:01,888-INFO: Loss at epoch 3 step 40: [0.963113], acc: [0.609375]
2020-03-09 14:10:01,888 - train.py[line:164] - INFO: Loss at epoch 3 step 40: [0.963113], acc: [0.609375]
2020-03-09 14:10:25,868-INFO: Loss at epoch 4 step 0: [1.1661512], acc: [0.609375]
2020-03-09 14:10:25,868 - train.py[line:164] - INFO: Loss at epoch 4 step 0: [1.1661512], acc: [0.609375]
2020-03-09 14:11:00,529-INFO: Loss at epoch 4 step 40: [0.9893415], acc: [0.59375]
2020-03-09 14:11:00,529 - train.py[line:164] - INFO: Loss at epoch 4 step 40: [0.9893415], acc: [0.59375]
2020-03-09 14:11:24,229-INFO: model saved at epoch 4, best accuracy is 0.5930555555555556
2020-03-09 14:11:24,229 - train.py[line:173] - INFO: model saved at epoch 4, best accuracy is 0.5930555555555556
2020-03-09 14:11:25,270-INFO: Loss at epoch 5 step 0: [1.1620805], acc: [0.484375]
2020-03-09 14:11:25,270 - train.py[line:164] - INFO: Loss at epoch 5 step 0: [1.1620805], acc: [0.484375]
2020-03-09 14:12:00,006-INFO: Loss at epoch 5 step 40: [0.9655475], acc: [0.578125]
2020-03-09 14:12:00,006 - train.py[line:164] - INFO: Loss at epoch 5 step 40: [0.9655475], acc: [0.578125]
2020-03-09 14:12:24,136-INFO: Loss at epoch 6 step 0: [0.87590384], acc: [0.640625]
2020-03-09 14:12:24,136 - train.py[line:164] - INFO: Loss at epoch 6 step 0: [0.87590384], acc: [0.640625]
2020-03-09 14:12:59,071-INFO: Loss at epoch 6 step 40: [1.0555159], acc: [0.5625]
2020-03-09 14:12:59,071 - train.py[line:164] - INFO: Loss at epoch 6 step 40: [1.0555159], acc: [0.5625]
2020-03-09 14:13:22,416-INFO: Loss at epoch 7 step 0: [0.79678166], acc: [0.703125]
2020-03-09 14:13:22,416 - train.py[line:164] - INFO: Loss at epoch 7 step 0: [0.79678166], acc: [0.703125]
2020-03-09 14:13:56,924-INFO: Loss at epoch 7 step 40: [1.0299878], acc: [0.59375]
2020-03-09 14:13:56,924 - train.py[line:164] - INFO: Loss at epoch 7 step 40: [1.0299878], acc: [0.59375]
2020-03-09 14:14:21,666-INFO: Loss at epoch 8 step 0: [0.99066764], acc: [0.625]
2020-03-09 14:14:21,666 - train.py[line:164] - INFO: Loss at epoch 8 step 0: [0.99066764], acc: [0.625]
2020-03-09 14:14:55,127-INFO: Loss at epoch 8 step 40: [0.8330009], acc: [0.703125]
2020-03-09 14:14:55,127 - train.py[line:164] - INFO: Loss at epoch 8 step 40: [0.8330009], acc: [0.703125]
2020-03-09 14:15:18,747-INFO: model saved at epoch 8, best accuracy is 0.6583333333333333
2020-03-09 14:15:18,747 - train.py[line:173] - INFO: model saved at epoch 8, best accuracy is 0.6583333333333333
2020-03-09 14:15:19,664-INFO: Loss at epoch 9 step 0: [0.8782065], acc: [0.640625]
2020-03-09 14:15:19,664 - train.py[line:164] - INFO: Loss at epoch 9 step 0: [0.8782065], acc: [0.640625]
2020-03-09 14:15:54,402-INFO: Loss at epoch 9 step 40: [1.199759], acc: [0.46875]
2020-03-09 14:15:54,402 - train.py[line:164] - INFO: Loss at epoch 9 step 40: [1.199759], acc: [0.46875]
2020-03-09 14:16:17,738-INFO: Final loss: [0.7580707]
2020-03-09 14:16:17,738 - train.py[line:174] - INFO: Final loss: [0.7580707]

In[10]

#模型评估
!python eval.py
W0309 14:17:15.764678   520 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0309 14:17:15.769109   520 device_context.cc:245] device: 0, cuDNN Version: 7.3.
0.63352275

In[11]

#预测
!python infer.py
W0309 14:17:26.886791   580 device_context.cc:237] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.2, Runtime API Version: 9.0
W0309 14:17:26.890343   580 device_context.cc:245] device: 0, cuDNN Version: 7.3.
checkpoint loaded
image data/data2815/sunflowers/3840761441_7c648abf4d_n.jpg Infer result is: sunflowers

点击链接,使用AI Studio一键上手实践项目吧:https://aistudio.baidu.com/aistudio/projectdetail/205030 

下载安装命令

## CPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/cpu paddlepaddle

## GPU版本安装命令
pip install -f https://paddlepaddle.org.cn/pip/oschina/gpu paddlepaddle-gpu

>> 访问 PaddlePaddle 官网,了解更多相关内容

{{o.name}}


{{m.name}}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/opensource/92496.html

(0)
上一篇 2021年8月15日 08:19
下一篇 2021年8月15日 08:30

相关推荐

发表回复

登录后才能评论