Tensorflow使用slim工具(vgg16模型)实现图像分类与分割

本文介绍了如何在Tensorflow环境下,利用slim库下载VGG16模型,并进行图像分类和分割。首先,文章提到了所需的CUDA、cuDNN和Python版本,以及相关安装教程链接。接着,详细说明了如何下载模型和使用方法。在图像分类部分,文章展示了使用预训练模型对tiger.jpg进行分类的过程。最后,探讨了图像分割显示的应用,特别是在复杂场景中获取全图类别信息的需求。
摘要由CSDN通过智能技术生成

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)

    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值