TensorFlow uses Slim tool (VGG16 model) to realize image classification and segmentation

Source: Internet
Author: User
Tags nets

Contact TensorFlow Small white, online tutorials a lot, image classification should belong to a more classic example, especially Google pushed slim, but the online tutorial omitted many details will lead to run, after debugging finally ran out

The result is OK, share

My environment, cuda8.0+cudnn5.1+python2.7.

About TENSORFLOW,CUDA+CUDNN Installation Recommended Tutorials:

http://blog.csdn.net/xierhacker/article/details/53035989

The whole idea is to classify images by training good vgg_16 model,

Original:




STEP01 Output Result:

Probability 1.00 => [Lion, King of Beasts, Panthera leo]
5 things
Probability 0.00 => [Collie]
5 things
Probability 0.00 => [OX]
5 things
Probability 0.00 => [Baboon]
5 things
Probability 0.00 => [Chow, Chow Chow]

It's clear that the classification is more correct.

STEP02 Image Segmentation Display effect:




Tools: TensorFlow Slim OpenCV NumPy

STEP01: Download the VGG model,

Import Sys
Import OS

os.environ["cuda_visible_devices"] = ' 0 '
Sys.path.append ("/home/yc/models/slim")
from datasets Import Dataset_utils
Import TensorFlow as TF

url = "Http://download.tensorflow.org/models/vgg_16_2016_08_28.tar.gz"

# Specify Save Path
Checkpoints_dir = '/home/yc/models/checkpoints '

If not tf.gfile.Exists (Checkpoints_dir):
Tf.gfile.MakeDirs (Checkpoints_dir)

Dataset_utils.download_and_uncompress_tarball (URL, checkpoints_dir)

STEP02: Image classification from datasets, etc. belong to the folder under Download Models/slim, the code may not prompt, no relationship,

Import Sys
Import OS

os.environ["cuda_visible_devices"] = ' 0 '
Sys.path.append ("/home/yc/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/yc/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 the matrix with intensity values
Image = Cv2.imread ("/home/yc/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, the crop of the resulted image.
# The crop would 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 dimension usually represents the batch size.
# The batch size is one.
Processed_images = tf.expand_dims (processed_image, 0)

# Create The model, use the ' default ARG scope to configure
# The batch norm parameters. Arg_scope is a very conveniet
# feature of Slim Library--you can define default
# parameters for layers-like stride, padding etc.
With Slim.arg_scope (Vgg.vgg_arg_scope ()):
Logits, _ = Vgg.vgg_16 (Processed_images,
num_classes=1000,
Is_training=false)

# in order to get probabilities we apply Softmax on the output.
probabilities = Tf.nn.softmax (logits)

# Create A function that reads the network weights
# from the checkpoint file, you downloaded.
# We'll run it in session later.
INIT_FN = SLIM.ASSIGN_FROM_CHECKPOINT_FN (
Os.path.join (Checkpoints_dir, ' vgg_16.ckpt '),
Slim.get_model_variables (' vgg_16 '))

With TF. Session () as Sess:

# Load Weights
INIT_FN (Sess)

# We want to get predictions, image as NumPy matrix
# and resized and cropped piece that are actually
# Being fed to the network.
Network_input, probabilities = Sess.run ([processed_image,probabilities])
probabilities = probabilities[0, 0:]
Sorted_inds = [I[0] for I in sorted (enumerate (-probabilities),
Key=lambda x:x[1])]

# show the downloaded image


# Show the image this is actually being fed to the network
# The image is resized while preserving aspect ratio and then
# cropped. After that, the mean pixel value is subtracted from
# each pixel of that crop. We normalize the image to be between [-1, 1]
# to show the image.
Plt.imshow (Network_input/(Network_input.max ()-network_input.min ()))
Plt.suptitle ("resized, cropped and mean-centered input to Network",
Fontsize=14, fontweight= ' bold ')
Plt.axis (' off ')
Plt.show ()

names = Imagenet.create_readable_names_for_imagenet_labels ()
For I in range (5):
Print "5 things"
index = Sorted_inds[i]
# Now we print the top-5 predictions the network gives us with
# corresponding probabilities. Pay attention the index with
# class names is shifted by 1--the is because some networks
# were trained on 1000 classes and others on 1001. VGG-16 was trained
# on 1000 classes.
Print (' Probability%0.2f => [%s] '% (Probabilities[index], names[index+1])

res = Slim.get_model_variables ()

STEP03: Image Segmentation display (the last step is to focus on the local category, according to the probability ranking, very clear, etc. see the classification, in the complex scene, the need for the whole picture category, need to split and display)


Import Sys
Import OS

os.environ["cuda_visible_devices"] = ' 0 '
Sys.path.append ("/home/yc/models/slim")
From matplotlib import Pyplot as Plt
Import NumPy as NP
Import Cv2
Import TensorFlow as TF
Import Urllib2
from datasets Import imagenet
From Nets import Vgg
From preprocessing import vgg_preprocessing
Checkpoints_dir = '/home/yc/models/checkpoints '
Slim=tf.contrib.slim
Image_size=vgg.vgg_16.default_image_size

# Load the mean pixel values and the function
# that performs the subtraction
From preprocessing.vgg_preprocessing import (_mean_image_subtraction,
_r_mean, _g_mean, _b_mean)

# Function to nicely print segmentation results with
# Colorbar showing class names
def discrete_matshow (data, labels_names=[], title= ""):
print "Matshow Begin"

#get Discrete ColorMap
CMap = Plt.get_cmap (' Paired ', Np.max (data)-np.min (data) +1)
# set limits. 5 Outside True Range
Mat = plt.matshow (data,
Cmap=cmap,
Vmin = np.min (data)-.5,
Vmax = Np.max (data) +.5)
#tell the Colorbar to tick at integers
CAx = Plt.colorbar (Mat,
Ticks=np.arange (np.min (data), Np.max (data) +1))

# The names to be printed aside the Colorbar
If Labels_names:
Cax.ax.set_yticklabels (Labels_names)

If title:
Plt.suptitle (title, fontsize=14, fontweight= ' bold ')
Plt.show ()

With TF. Graph (). As_default ():

IMAGE01 = Cv2.imread ("/home/yc/1214/tiger.jpg")

Image=cv2.cvtcolor (image01,4)

# Convert image to float32 before subtracting the
# mean pixel value
Image_float = tf.to_float (image, Name= ' tofloat ')

# Subtract the mean pixel value from each pixel
Processed_image = _mean_image_subtraction (Image_float,
[_r_mean, _g_mean, _b_mean])

Input_image = tf.expand_dims (processed_image, 0)

With Slim.arg_scope (Vgg.vgg_arg_scope ()):

# Spatial_squeeze option enables to use network in a fully
# convolutional Manner
Logits, _ = Vgg.vgg_16 (Input_image,
num_classes=1000,
Is_training=false,
Spatial_squeeze=false)

# for each pixel we have predictions for each class
# Out of 1000. We need to pick the one with the highest
# probability. To being more precise, this are not probabilities,
# because we didn ' t apply Softmax. But If we pick a class
# with the highest value it is equivalent to picking
# The highest value after applying Softmax
pred = Tf.argmax (logits, dimension=3)

INIT_FN = SLIM.ASSIGN_FROM_CHECKPOINT_FN (
Os.path.join (Checkpoints_dir, ' vgg_16.ckpt '),
Slim.get_model_variables (' vgg_16 '))

With TF. Session () as Sess:
INIT_FN (Sess)
segmentation = Sess.run ([pred])

# Remove the ' the ' empty dimension
segmentation = Np.squeeze (segmentation)

# Let's get a unique predicted classes (from 0 to 1000) and
# relable The original predictions so classes are
# numerated starting from zero
unique_classes, relabeled_image = Np.unique (Segmentation,
Return_inverse=true)


Segmentation_size = Segmentation.shape
Print unique_classes
Relabeled_image = Relabeled_image.reshape (segmentation_size)

Labels_names = []
names = Imagenet.create_readable_names_for_imagenet_labels ()
For index, Current_class_number in Enumerate (unique_classes):

Labels_names.append (str (index) + ' + names[current_class_number+1])
Print Labels_names

Discrete_matshow (Data=relabeled_image, Labels_names=labels_names, title= "segmentation")



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.