TensorFlow中如何使用訓練好的模型識別貓狗圖片-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)TensorFlow中如何使用訓練好的模型識別貓狗圖片的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

10年積累的網(wǎng)站制作、網(wǎng)站設(shè)計經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有田林免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

本文邏輯:

  1. 我從網(wǎng)上下載了十幾張貓和狗的圖片,用于檢驗我們訓練好的模型。

  2. 處理我們下載的圖片

  3. 加載模型

  4. 將圖片輸入模型進行檢驗

代碼如下:

#coding=utf-8 
import tensorflow as tf 
from PIL import Image 
import matplotlib.pyplot as plt
import input_data 
import numpy as np
import model
import os 
#從指定目錄中選取一張圖片 
def get_one_image(train): 
  files = os.listdir(train)
  n = len(files)
  ind = np.random.randint(0,n)
  img_dir = os.path.join(train,files[ind]) 
  image = Image.open(img_dir) 
  plt.imshow(image)
  plt.show()
  image = image.resize([208, 208]) 
  image = np.array(image)
  return image 
def evaluate_one_image(): 
 #存放的是我從百度下載的貓狗圖片路徑
  train = '/Users/yangyibo/GitWork/pythonLean/AI/貓狗識別/testImg/' 
  image_array = get_one_image(train) 
  with tf.Graph().as_default(): 
    BATCH_SIZE = 1 # 因為只讀取一副圖片 所以batch 設(shè)置為1
    N_CLASSES = 2 # 2個輸出神經(jīng)元,[1,0] 或者 [0,1]貓和狗的概率
    # 轉(zhuǎn)化圖片格式
    image = tf.cast(image_array, tf.float32) 
    # 圖片標準化
    image = tf.image.per_image_standardization(image)
    # 圖片原來是三維的 [208, 208, 3] 重新定義圖片形狀 改為一個4D 四維的 tensor
    image = tf.reshape(image, [1, 208, 208, 3]) 
    logit = model.inference(image, BATCH_SIZE, N_CLASSES) 
    # 因為 inference 的返回沒有用激活函數(shù),所以在這里對結(jié)果用softmax 激活
    logit = tf.nn.softmax(logit) 
    # 用最原始的輸入數(shù)據(jù)的方式向模型輸入數(shù)據(jù) placeholder
    x = tf.placeholder(tf.float32, shape=[208, 208, 3]) 
    # 我門存放模型的路徑
    logs_train_dir = '/Users/yangyibo/GitWork/pythonLean/AI/貓狗識別/saveNet/'  
    # 定義saver 
    saver = tf.train.Saver() 
    with tf.Session() as sess: 
      print("從指定的路徑中加載模型。。。。")
      # 將模型加載到sess 中 
      ckpt = tf.train.get_checkpoint_state(logs_train_dir) 
      if ckpt and ckpt.model_checkpoint_path: 
        global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] 
        saver.restore(sess, ckpt.model_checkpoint_path) 
        print('模型加載成功, 訓練的步數(shù)為 %s' % global_step) 
      else: 
        print('模型加載失敗,,,文件沒有找到') 
      # 將圖片輸入到模型計算
      prediction = sess.run(logit, feed_dict={x: image_array})
      # 獲取輸出結(jié)果中大概率的索引
      max_index = np.argmax(prediction) 
      if max_index==0: 
        print('貓的概率 %.6f' %prediction[:, 0]) 
      else: 
        print('狗的概率 %.6f' %prediction[:, 1]) 
# 測試
evaluate_one_image()

/Users/yangyibo/GitWork/pythonLean/AI/貓狗識別/testImg/ 存放的是我從百度下載的貓狗圖片

TensorFlow中如何使用訓練好的模型識別貓狗圖片

執(zhí)行結(jié)果:

因為從testimg 中選取圖片是隨機的,所以每次執(zhí)行的結(jié)果不同

從指定的路徑中加載模型。。。。
模型加載成功, 訓練的步數(shù)為 11999
狗的概率 0.964047
[Finished in 6.8s]

感謝各位的閱讀!關(guān)于“TensorFlow中如何使用訓練好的模型識別貓狗圖片”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章題目:TensorFlow中如何使用訓練好的模型識別貓狗圖片-創(chuàng)新互聯(lián)
文章起源:http://bm7419.com/article8/dgdeip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、服務(wù)器托管定制開發(fā)、定制網(wǎng)站網(wǎng)站收錄、網(wǎng)站營銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設(shè)