环境:Win8.1 TensorFlow1.0.1
软件:Anaconda3 (集成Python3及开发环境)
TensorFlow安装:pip install tensorflow (CPU版) pip install tensorflow-gpu (GPU版)
TFLearn安装:pip install tflearn
参考:
1. TFLearn: Deep learning library featuring a higher-level API for TensorFlow
2. Github: tflearn
1. 前言
在之间的博文中,我们介绍了深度学习的组成和发展,推动卷积神经网络快速发展的经典模型(AlexNet、VGG、Google Inception Net、ResNet),以及我们如何利用 TensorFlow 建立一个自己的简单 CNN 网络完成特定任务,例如 2层卷积层+2层全连接层+Softmax_CrossEntropy 完成 CIFAR-10 图像分类。随着计算资源平台的飞速发展,我们可以训练更深的网络解决更复杂问题和提高准确率,如 ResNet 使用 Residual Unit 成功训练152层深的神经网络,在 ILSVRC 2015 比赛中获得了冠军,取得3.57%的 top-5 错误率,同时参数量却比 VGGNet 低,效果非常突出。
当我们使用 TensorFlow 建立网络的一层 conv+pooling+lrn 时,可以采用最基本的定义:
# conv1
with tf.variable_scope('conv1') as scope:
kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64],
stddev=1e-4, wd=0.0)
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
bias = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(bias, name=scope.name)
_activation_summary(conv1)
# pool1
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding='SAME', name='pool1')
# norm1
norm1 = tf.nn.lrn(pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75,
name='norm1')
2. TFLearn
TFlearn is a modular and transparent deep learning library built on top of Tensorflow. It was designed to provide a higher-level API to TensorFlow in order to facilitate and speed-up experimentations, while remaining fully transparent and compatible with it.
TFLearn 是一个构建在 TensorFlow 之上的模块化和透明的深度学习库。它为 TensorFlow 提供高层次 API,目的是便于快速搭建试验环境,同时保持对 TensorFlow 的完全透明和兼容性。
它的特点:
Note: Latest TFLearn (v0.3) is only compatible with TensorFlow v1.0 and over.
安装:
To install TFLearn, the easiest way is to run
For the bleeding edge version (recommended):
pip install git+https://github.com/tflearn/tflearn.git
pip install tflearn
python setup.py install
我们可以在 tflearn/examples/images/alexnet.py 下学习如何用 TFLearn 实现 AlexNet 用于 Oxford 17 类鲜花 数据集分类任务。
回忆一下之前介绍用于 ImageNet 的 AlexNet 网络:
针对 Oxford 17类鲜花问题作出的修改:
直接运行 alexnet.py 文件将自动下载 Oxford 17 flowers 数据集进行训练:
提示:Oxford 17 flowers 是从 VGG官网 通过 TFLearn 内置 oxflower17.py 下载、解压、分类整理的,由于网络因素下载过程通常狗带,可以选择自行下载 17flowers.tgz 文件置于 17flowers 文件下,同时需要修改 oxflower17.py 下 maybe_download 函数:
def maybe_download(filename, source_url, work_directory):
if not os.path.exists(work_directory):
os.mkdir(work_directory)
filepath = os.path.join(work_directory, filename)
if not os.path.exists(filepath):
print("Downloading Oxford 17 category Flower Dataset, Please "
"wait...")
filepath, _ = urllib.request.urlretrieve(source_url + filename,
filepath, reporthook)
statinfo = os.stat(filepath)
print(('Succesfully downloaded', filename, statinfo.st_size, 'bytes.'))
# 将解压和分类整理函数置于if语句外,否则运行程序时检测不到上图16类文件图片
untar(filepath, work_directory)
build_class_directories(os.path.join(work_directory, 'jpg'))
return filepath
可在 TensorBoard 中查看训练过程的准确率、loss 值变化。
附上 alexnet.py 源码:
# -*- coding: utf-8 -*-
""" AlexNet.
Applying 'Alexnet' to Oxford's 17 Category Flower Dataset classification task.
References:
- Alex Krizhevsky, Ilya Sutskever & Geoffrey E. Hinton. ImageNet
Classification with Deep Convolutional Neural Networks. NIPS, 2012.
- 17 Category Flower Dataset. Maria-Elena Nilsback and Andrew Zisserman.
Links:
- [AlexNet Paper](http://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf)
- [Flower Dataset (17)](http://www.robots.ox.ac.uk/~vgg/data/flowers/17/)
"""
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
import tflearn.datasets.oxflower17 as oxflower17
X, Y = oxflower17.load_data(one_hot=True, resize_pics=(227, 227))
# Building 'AlexNet'
network = input_data(shape=[None, 227, 227, 3])
network = conv_2d(network, 96, 11, strides=4, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 256, 5, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 384, 3, activation='relu')
network = conv_2d(network, 256, 3, activation='relu')
network = max_pool_2d(network, 3, strides=2)
network = local_response_normalization(network)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 4096, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 17, activation='softmax')
network = regression(network, optimizer='momentum',
loss='categorical_crossentropy',
learning_rate=0.001)
# Training
model = tflearn.DNN(network, checkpoint_path='model_alexnet',
max_checkpoints=1, tensorboard_verbose=2)
model.fit(X, Y, n_epoch=1000, validation_set=0.1, shuffle=True,
show_metric=True, batch_size=64, snapshot_step=200,
snapshot_epoch=False, run_id='alexnet_oxflowers17')
4. 总结
使用 TFLearn 高层次 API 相比直接使用 TensorFlow 实现深度学习模型具有使用更简单、构建更快速、可视化更方便等特点,从此无需手动处理各个运算符之间的连接,解放了程序员们的生产力(心疼一秒=-=),提高了模型设计和优化效率。
另外,通过 TFLearn 还可以实现其他经典深度学习模型如 VGG、Inception、NIN、ResNet 等,对比原始论文学习,相信会更加高效。