日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TensorFlow基础笔记(5) VGGnet_test

發布時間:2024/4/17 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TensorFlow基础笔记(5) VGGnet_test 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考

http://blog.csdn.net/jsond/article/details/72667829

資源

1.相關的vgg模型下載網址

http://www.vlfeat.org/matconvnet/models/beta16/imagenet-vgg-verydeep-19.mat

2.ImageNet 1000種分類以及排列

https://github.com/sh1r0/caffe-Android-demo/blob/master/app/src/main/assets/synset_words.txt(如果下載單個txt格式不對的話就整包下載)

?

?

這里以E網絡為測試模型VGG19

#coding=utf-8 import numpy as np import scipy.misc import scipy.io as sio import tensorflow as tf import os##卷積層 def _conv_layer(input, weight, bias):conv = tf.nn.conv2d(input, tf.constant(weight), strides=(1, 1, 1, 1), padding='SAME')return tf.nn.bias_add(conv, bias)##池化層 def _pool_layer(input):return tf.nn.max_pool(input, ksize=(1, 2, 2, 1), strides=(1, 2, 2, 1), padding='SAME')##全鏈接層 def _fc_layer(input, weights, bias):shape = input.get_shape().as_list()dim = 1for d in shape[1:]:dim *= dx = tf.reshape(input, [-1, dim])fc = tf.nn.bias_add(tf.matmul(x, weights), bias)return fc##softmax輸出層 def _softmax_preds(input):preds = tf.nn.softmax(input, name='prediction')return preds##圖片處里前減去均值 def _preprocess(image, mean_pixel):return image - mean_pixel##加均值 顯示圖片 def _unprocess(image, mean_pixel):return image + mean_pixel##讀取圖片 并壓縮 def _get_img(src, img_size=False):img = scipy.misc.imread(src, mode='RGB')if not (len(img.shape) == 3 and img.shape[2] == 3):img = np.dstack((img, img, img))if img_size != False:img = scipy.misc.imresize(img, img_size)return img.astype(np.float32)##獲取名列表 def list_files(in_path):files = []for (dirpath, dirnames, filenames) in os.walk(in_path):# print("dirpath=%s, dirnames=%s, filenames=%s"%(dirpath, dirnames, filenames)) files.extend(filenames)breakreturn files##獲取文件路徑列表dir+filename def _get_files(img_dir):files = list_files(img_dir)return [os.path.join(img_dir, x) for x in files]##獲得圖片lable列表 def _get_allClassificationName(file_path):f = open(file_path, 'r')lines = f.readlines()f.close()return lines##構建cnn前向傳播網絡 def net(data, input_image):layers = ('conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1','conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2','conv3_1', 'relu3_1', 'conv3_2', 'relu3_2','conv3_3', 'relu3_3', 'conv3_4', 'relu3_4', 'pool3','conv4_1', 'relu4_1', 'conv4_2', 'relu4_2','conv4_3', 'relu4_3', 'conv4_4', 'relu4_4', 'pool4','conv5_1', 'relu5_1', 'conv5_2', 'relu5_2','conv5_3', 'relu5_3', 'conv5_4', 'relu5_4', 'pool5','fc6', 'relu6','fc7', 'relu7','fc8', 'softmax')weights = data['layers'][0]net = {}current = input_imagefor i, name in enumerate(layers):kind = name[:4]if kind == 'conv':kernels, bias = weights[i][0][0][0][0]kernels = np.transpose(kernels, (1, 0, 2, 3))bias = bias.reshape(-1)current = _conv_layer(current, kernels, bias)elif kind == 'relu':current = tf.nn.relu(current)elif kind == 'pool':current = _pool_layer(current)elif kind == 'soft':current = _softmax_preds(current)kind2 = name[:2]if kind2 == 'fc':kernels1, bias1 = weights[i][0][0][0][0]kernels1 = kernels1.reshape(-1, kernels1.shape[-1])bias1 = bias1.reshape(-1)current = _fc_layer(current, kernels1, bias1)net[name] = currentassert len(net) == len(layers)return net, mean_pixel, layersif __name__ == '__main__':imagenet_path = 'imagenet-vgg-verydeep-19.mat'image_dir = 'images/'data = sio.loadmat(imagenet_path) ##加載ImageNet mat模型mean = data['normalization'][0][0][0]mean_pixel = np.mean(mean, axis=(0, 1)) ##獲取圖片像素均值 lines = _get_allClassificationName('synset_words.txt') ##加載ImageNet mat標簽images = _get_files(image_dir) ##獲取圖片路徑列表 with tf.Session() as sess:for i, imgPath in enumerate(images):image = _get_img(imgPath, (224, 224, 3)); ##加載圖片并壓縮到標準格式=>224 224 image_pre = _preprocess(image, mean_pixel)# image_pre = image_pre.transpose((2, 0, 1))image_pre = np.expand_dims(image_pre, axis=0)image_preTensor = tf.convert_to_tensor(image_pre)image_preTensor = tf.to_float(image_preTensor)# Test pretrained modelnets, mean_pixel, layers = net(data, image_preTensor)preds = nets['softmax']predsSortIndex = np.argsort(-preds[0].eval())print('\n#####%s#######' % imgPath)for i in range(3): ##輸出前3種分類nIndex = predsSortIndexclassificationName = lines[nIndex[i]] ##分類名稱problity = preds[0][nIndex[i]] ##某一類型概率print('%d.ClassificationName=%s Problity=%f' % ((i + 1), classificationName, problity.eval()))sess.close()

?

分類結果

#####images/airplay.jpg####### 1.ClassificationName=n04228054 skiProblity=0.177715 2.ClassificationName=n04286575 spotlight, spotProblity=0.108483 3.ClassificationName=n04127249 safety pinProblity=0.026277#####images/bird.jpg####### 1.ClassificationName=n01608432 kiteProblity=0.096818 2.ClassificationName=n01833805 hummingbirdProblity=0.072687 3.ClassificationName=n02231487 walking stick, walkingstick, stick insectProblity=0.069186#####images/cat1.jpg####### 1.ClassificationName=n02123045 tabby, tabby catProblity=0.232015 2.ClassificationName=n02123159 tiger catProblity=0.094694 3.ClassificationName=n02124075 Egyptian catProblity=0.030673#####images/cat2.jpg####### 1.ClassificationName=n02123045 tabby, tabby catProblity=0.333797 2.ClassificationName=n02123159 tiger catProblity=0.164726 3.ClassificationName=n02124075 Egyptian catProblity=0.057272#####images/cat3.jpg####### 1.ClassificationName=n03887697 paper towelProblity=0.086723 2.ClassificationName=n02111889 Samoyed, SamoyedeProblity=0.055845 3.ClassificationName=n03131574 crib, cotProblity=0.052640#####images/dog1.jpg####### 1.ClassificationName=n02096585 Boston bull, Boston terrierProblity=0.429622 2.ClassificationName=n02108089 boxerProblity=0.199422 3.ClassificationName=n02093256 Staffordshire bullterrier, Staffordshire bull terrierProblity=0.093615#####images/dog2.jpg####### 1.ClassificationName=n02085936 Maltese dog, Maltese terrier, MalteseProblity=0.172208 2.ClassificationName=n03445777 golf ballProblity=0.139949 3.ClassificationName=n02259212 leafhopperProblity=0.118109#####images/lena.jpg####### 1.ClassificationName=n02869837 bonnet, poke bonnetProblity=0.130357 2.ClassificationName=n04356056 sunglasses, dark glasses, shadesProblity=0.066170 3.ClassificationName=n04355933 sunglassProblity=0.043199#####images/sky.jpg####### 1.ClassificationName=n03733281 maze, labyrinthProblity=0.711163 2.ClassificationName=n03065424 coil, spiral, volute, whorl, helixProblity=0.181123 3.ClassificationName=n04259630 sombreroProblity=0.010005

?

轉載于:https://www.cnblogs.com/adong7639/p/7652635.html

總結

以上是生活随笔為你收集整理的TensorFlow基础笔记(5) VGGnet_test的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。