1. 环境
cuda8.0+cudnn5.1+python2.7
关于tensorflow,cuda+cudnn等安装推荐教程:
http://blog.csdn.net/xierhacker/article/details/53035989
工具:tensorflow slim opencv numpy
参考:
https://github.com/tensorflow/models/tree/1630da3434974e9ad5a0b6d887ac716a97ce03d3/research/slim/
2. 下载vgg模型
from datasets import dataset_utils需要tensorflow/models/research/slim/datasets
整体思路就是“通过训练好的vgg_16模型进行图像分类
import sys
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
sys.path.append("/home/sxl/models/slim")
from datasets import dataset_utils
import tensorflow as tf
url = "http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz"
# 指定保存路径
checkpoints_dir = '/home/sxl/models/checkpoints'
if not tf.gfile.Exists(checkpoints_dir):
tf.gfile.MakeDirs(checkpoints_dir)
dataset_utils.download_and_uncompress_tarball(url, checkpoints_dir)
3. 图像分类
下载models/slim下的文件夹
此步注重的是局部类别,根据概率排名,很清楚地看出分类。
import sys
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
sys.path.append("/home/sxl/models/slim")
from matplotlib import pyplot as plt
import numpy as np
import cv2
import tensorflow as tf
from datasets import imagenet
from nets import vgg
from preprocessing import vgg_preprocessing
checkpoints_dir = '/home/sxl/models/checkpoints'
slim=tf.contrib.slim
image_size=vgg.vgg_16.default_image_size
with tf.Graph().as_default():
# Open specified url and load image as a string
# Decode string into matrix with intensity values
image = cv2.imread("/home/sxl/1214/tiger.jpg")
image=cv2.cvtColor(image, 4)
plt.imshow(image)
plt.suptitle("The tiger",
fontsize=14, fontweight='bold')
plt.axis('off')
plt.show()
# Resize the input image, preserving the aspect ratio
# and make a central crop of the resulted image.
# The crop will be of the size of the default image size of
# the network.
processed_image = vgg_preprocessing.preprocess_image(image,
image_size,
image_size,
is_training=False)
# Networks accept images in batches.
# The first dimension usually represents the batch size.
# In our case the batch size is one.
processed_images = tf.expand_dims(processed_image, 0)