TensorFlow on the VGG16 project

Source: Internet
Author: User
Tags crop image

Reprint Please specify link: http://www.cnblogs.com/SSSR/p/5630534.html

Examples in Tflearn training VGG16 project: https://github.com/tflearn/tflearn/blob/master/examples/images/vgg_network.py has not been tested successfully.

The next project is to use a model that has been trained by others to make predictions, and the test works very well.

Github:https://github.com/ry/tensorflow-vgg16 This project has been tested successfully, the effect is very good,

If there is a problem running in terminal in Ubuntu, you can resolve it by following the section below (to solve the problem of skimage reading pictures).

#coding: Utf-8Import skimageimport skimage.ioimport skimage.transforma=skimage.io.imread (' cat.jpg ')Import Pilimport NumPy as Npimport tensorflow as Tfsynset = [L.strip () for L in open ('/home/ubuntu/pythonproject/tensorflo W/tensorflow-vgg16/synset.txt '). ReadLines ()]def load_image (path): # Load image img = skimage.io.imread (path) #img1 =pil . Image.open ("/home/ubuntu/pythonproject/tensorflow/tensorflow-vgg16/pic/pig.jpg") #img =np.array (PIL. Image.open (path)) #imgx =np.array (img) #print type (IMGX), imgx.shape img = img/255.0 assert (0 <= img). All () and ( IMG <= 1.0). All () #print "Original Image Shape:", Img.shape # We crop Image from center Short_edge = min (img.shape[ : 2] yy = Int ((img.shape[0]-Short_edge)/2) xx = Int ((img.shape[1]-Short_edge)/2) crop_img = Img[yy:yy + Short _edge, Xx:xx + Short_edge] # Resize to 224, 224 resized_img = Skimage.transform.resize (crop_img, (224, 224)) return R ESIZED_IMG # returns the Top1 stringdef Print_prob (prob): #print prob print "prob shape", prob.shape pred = Np.argsort (prob) [:: -1] # Get top1 label TOP1 = synset[Pred[0]] #print "TOP1:", Top1 # Get top5 Label top5 = [Synset[pred[i]] for I in range (5)] #print "TOP5:", TOP5 Retu RN top1print u ' load model file ' with open ("/home/ubuntu/pythonproject/tensorflow/tensorflow-vgg16/vgg16.tfmodel", mode= ' RB ') As F:filecontent = F.read () print u ' Create diagram ' Graph_def = tf. Graphdef () graph_def. Parsefromstring (filecontent) images = Tf.placeholder ("float", [None, 224, 224, 3]) tf.import_graph_def (graph_def, input _map={"Images": Images}) print "Graph loaded from disk" graph = tf.get_default_graph () print u ' load picture ' #img =np.array (PIL. Image.open ("/home/ubuntu/pythonproject/tensorflow/tensorflow-vgg16/pic/pig.jpg")) #cat = load_image (path) print U ' Enter Sess to execute ' SESS=TF. Session () result=[]for i in [' cat.jpg ', ' airplane.jpg ', ' zebra.jpg ', ' pig.jpg ', ' 12.jpg ', ' 23.jpg ']: Img=load_image (' pic /' +i) init = Tf.initialize_all_variables () sess.run (init) print "variables initialized" batch = Img.reshape ((1, 224, 2 (3)) assert batch.shape = = (1, 224, 224, 3) feed_dict = {Images:batch} print U ' start execution ' prob_tensor = Graph.get_tensor_by_name ("import/prob:0") prob = Sess.run (Prob_tensor, feed_dict=feed_dict) prin T u ' output result ' #print_prob (prob[0]) result.append (Print_prob (prob[0])) print resultsess.close () "with TF. Session () as Sess:init = Tf.initialize_all_variables () sess.run (init) print "variables initialized" batch = Cat.resha PE ((1, 224, 224, 3)) assert batch.shape = = (1, 224, 224, 3) feed_dict = {Images:batch} print U ' start execution ' prob_tensor = Graph.get_tensor_by_name ("import/prob:0") prob = Sess.run (prob_tensor, feed_dict=feed_dict) print u ' output result ' print_prob ( Prob[0]) ""

  

TensorFlow on the VGG16 project

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.