1. 程式人生 > >TensorFlow利用InceptionV3訓練新的影象分類模型

TensorFlow利用InceptionV3訓練新的影象分類模型

1. tensorflow編譯 、安裝

#CPU
bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
#GPU
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip install /tmp/tensorflow_pkg/tensorflow-*.whl

問題1:

ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory

請在.profile檔案中中新增如下:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda

2.利用Tensorflow訓練影象分類的模型

谷歌的Inceptionv3給出了儲存好的模型和訓練的程式碼,直接可以拿來訓練。
這裡採用遷移學習的方法。即前面的層的引數都不變,而只訓練最後一層的方法。最後一層是一個softmax分類器,這個分類器在原來的網路上是1000個輸出節點(ImageNet有1000個類),所以需要刪除網路的最後的一層,變為所需要的輸出節點數量,然後再進行訓練。

Tensorflow中採用的方法是這樣的:將自己的訓練集中的每張影象輸入網路,最後在瓶頸層(bottleneck),就是倒數第二層,會生成一個2048維度的特徵向量,將這個特徵儲存在一個txt檔案中,再用這個特徵來訓練softmax分類器。
具體的方法如下:

1.編譯和預處理

bazel buildtensorflow/examples/image_retraining:retrain

如果電腦比較新,建議用這個命令來編譯:

bazel build -c opt --copt=-mavx tensorflow/examples/image_retraining:retrain

後面一種在提取bottleneck特徵的時候比前面的一種快了10倍左右。

編譯完成後就可以使用了。但是建議還是改動一下tensorflow/examples/image_retraining目錄下的retrain的python指令碼,因為裡面的預設路徑是/tmp,這個資料夾一旦電腦關機所有資料都會清除。建議把裡面所有的這個路徑都改為另外的路徑。之後在該路徑下將自己的訓練資料集放好。

訓練資料集是有格式要求的:
a.資料集應該這樣設定,訓練集資料夾下放置多個子資料夾,每個子資料夾就是一個類,裡面包含該類的所有影象。
b.影象應該是jpg或者jpeg格式。

2.訓練

在設定好資料集後,執行

bazel-bin/tensorflow/examples/image_retraining/retrain --image_dir ~/XXX

image_dir ~/XXX是訓練資料集的路徑XXX是資料集的名稱。

這時就開始訓練了。訓練過程中會首先下載原來的Inception網路,儲存在ImageNet的資料夾下。
這裡寫圖片描述
第一個檔案是網路的圖結構,第二個檔案是一個測試影象,第三個是一個對映,從最後1000個節點中的每一個對映到一個編碼,第四個也是一個對映,是從編碼對映到人能夠識別的名詞,例如:節點的表示是第234個節點,而這個節點對映到的編碼是nb20003,這個編碼對映到的名詞是熊貓(僅舉例,數字和編碼以及名詞是隨意假設的)。這些對映關係和編碼是在ImageNet 2012測試集中定義的。

下載後開始提取每張訓練影象的bottleneck特徵。這個過程大概1s提取5張影象。在提取完成後就開始訓練。
開啟image_dir路徑,可以在下面發現多處了兩個檔案,分別是output.pb和output.txt。第一個檔案是訓練後的圖結構,第二個是從節點到名詞的對映,這裡不會給出中間的編碼對映——除非您自己定義一個對映關係。

接下來怎麼利用訓練好的模型來進行分類呢?首先還是回到下載下來的ImageNet資料夾中,執行tensorflow/model/ImageNet中的classify.py,則是對資料夾中的測試影象進性預測的結果。

這時,將訓練出的output_graph檔案放到該資料夾下,替換掉原有的圖檔案(可以把output_graph檔案重新命名為原來的圖檔名,這樣就不需要改動程式碼了)。再執行classfy.py檔案就可以用自己的模型來對影象進行分類了。給出的結果是Top5對應的節點數以及相應的概率。如果需要輸出名詞,需要自己定義對映關係。

但是這樣還是隻會對一張影象進行分類,我改動了classify_image的指令碼,讓它可以對多個檔案分類。如果您想自己嘗試改寫指令碼讓它可以對多個檔案分類,那可能會遇到這樣的情況:大概在預測10幾個檔案後,突然報錯,說圖的結構不能大於2G,這是因為每訓練一個圖,就會在圖中增加一個點,當增加到一個程度,圖的結構就會超過2G。這需要在每訓練一個圖片後重置圖,改寫的指令碼中已經克服了這個問題,但是這也使得分類的速度變慢,大概1.5s一張影象。這個指令碼會輸出top-2的準確率,如下:

修改後的classfy.py程式碼:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Simple image classification with Inception.

Run image classification with Inception trained on ImageNet 2012 Challenge data
set.

This program creates a graph from a saved GraphDef protocol buffer,
and runs inference on an input JPEG image. It outputs human readable
strings of the top 5 predictions along with their probabilities.

Change the --image_file argument to any jpg image to compute a
classification of that image.

Please see the tutorial and website for a detailed description of how
to use this script to perform image recognition.

https://tensorflow.org/tutorials/image_recognition/
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os.path
import re
import sys
import tarfile
import os

import numpy as np
from six.moves import urllib
import tensorflow as tf

FLAGS = tf.app.flags.FLAGS

# classify_image_graph_def.pb:
#   Binary representation of the GraphDef protocol buffer.
# imagenet_synset_to_human_label_map.txt:
#   Map from synset ID to a human readable string.
# imagenet_2012_challenge_label_map_proto.pbtxt:
#   Text representation of a protocol buffer mapping a label to synset ID.
tf.app.flags.DEFINE_string(
    'model_dir', '/tmp/imagenet',
    """Path to classify_image_graph_def.pb, """
    """imagenet_synset_to_human_label_map.txt, and """
    """imagenet_2012_challenge_label_map_proto.pbtxt.""")
tf.app.flags.DEFINE_string('image_file', '',
                           """Absolute path to image file.""")
tf.app.flags.DEFINE_integer('num_top_predictions', 2,
                            """Display this many predictions.""")

# pylint: disable=line-too-long
DATA_URL = 'http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz'
# pylint: enable=line-too-long


class NodeLookup(object):
  """Converts integer node ID's to human readable labels."""

  def __init__(self,
               label_lookup_path=None,
               uid_lookup_path=None):
    if not label_lookup_path:
      label_lookup_path = os.path.join(
          FLAGS.model_dir, 'imagenet_2012_challenge_label_map_proto.pbtxt')
    if not uid_lookup_path:
      uid_lookup_path = os.path.join(
          FLAGS.model_dir, 'imagenet_synset_to_human_label_map.txt')
    self.node_lookup = self.load(label_lookup_path, uid_lookup_path)

  def load(self, label_lookup_path, uid_lookup_path):
    """Loads a human readable English name for each softmax node.

    Args:
      label_lookup_path: string UID to integer node ID.
      uid_lookup_path: string UID to human-readable string.

    Returns:
      dict from integer node ID to human-readable string.
    """
    if not tf.gfile.Exists(uid_lookup_path):
      tf.logging.fatal('File does not exist %s', uid_lookup_path)
    if not tf.gfile.Exists(label_lookup_path):
      tf.logging.fatal('File does not exist %s', label_lookup_path)

    # Loads mapping from string UID to human-readable string
    proto_as_ascii_lines = tf.gfile.GFile(uid_lookup_path).readlines()
    uid_to_human = {}
    p = re.compile(r'[n\d]*[ \S,]*')
    for line in proto_as_ascii_lines:
      parsed_items = p.findall(line)
      uid = parsed_items[0]
      human_string = parsed_items[2]
      uid_to_human[uid] = human_string

    # Loads mapping from string UID to integer node ID.
    node_id_to_uid = {}
    proto_as_ascii = tf.gfile.GFile(label_lookup_path).readlines()
    for line in proto_as_ascii:
      if line.startswith('  target_class:'):
        target_class = int(line.split(': ')[1])
      if line.startswith('  target_class_string:'):
        target_class_string = line.split(': ')[1]
        node_id_to_uid[target_class] = target_class_string[1:-2]

    # Loads the final mapping of integer node ID to human-readable string
    node_id_to_name = {}
    for key, val in node_id_to_uid.items():
      if val not in uid_to_human:
        tf.logging.fatal('Failed to locate: %s', val)
      name = uid_to_human[val]
      node_id_to_name[key] = name

    return node_id_to_name

  def id_to_string(self, node_id):
    if node_id not in self.node_lookup:
      return ''
    return self.node_lookup[node_id]


def create_graph():
  """Creates a graph from saved GraphDef file and returns a saver."""
  # Creates graph from saved graph_def.pb.
  with tf.gfile.FastGFile(os.path.join(
      FLAGS.model_dir, 'classify_image_graph_def.pb'), 'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    _ = tf.import_graph_def(graph_def, name='')


def run_inference_on_image(image):
  """Runs inference on an image.

  Args:
    image: Image file name.

  Returns:
    Nothing
  """
  if not tf.gfile.Exists(image):
    tf.logging.fatal('File does not exist %s', image)
  image_data = tf.gfile.FastGFile(image, 'rb').read()

  # Creates graph from saved GraphDef.
  create_graph()

  with tf.Session() as sess:
    # Some useful tensors:
    # 'softmax:0': A tensor containing the normalized prediction across
    #   1000 labels.
    # 'pool_3:0': A tensor containing the next-to-last layer containing 2048
    #   float description of the image.
    # 'DecodeJpeg/contents:0': A tensor containing a string providing JPEG
    #   encoding of the image.
    # Runs the softmax tensor by feeding the image_data as input to the graph.
    softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})
    predictions = np.squeeze(predictions)

    # Creates node ID --> English string lookup.
    node_lookup = NodeLookup()

    top_k = predictions.argsort()[-FLAGS.num_top_predictions:][::-1]
    for node_id in top_k:
      score = predictions[node_id]
      print(node_id,end= '\t')
      print(score,end= '\t')
    print()

def main(_):
  for root, dirs, files in os.walk("/tmp/imagenet/picture", topdown=False):
    for name in files:
      print(name,end= '\t')
      image = (FLAGS.image_file if FLAGS.image_file else
                 os.path.join('/tmp/imagenet/picture',name))
      with tf.Graph().as_default():
         run_inference_on_image(image) 
         os.remove(image)

if __name__ == '__main__':
  tf.app.run()

相關推薦

TensorFlow利用InceptionV3訓練影象分類模型

1. tensorflow編譯 、安裝 #CPU bazel build -c opt //tensorflow/tools/pip_package:build_pip_package #GPU bazel build -c opt --config=cu

Tensorflow 的安裝和用InceptionV3訓練影象分類模型

Tensorflow的安裝 1.Tensorflow簡介 Tensorflow是一個谷歌釋出的人工智慧開發工具,於2015年年底開源。在開源之前一直是在谷歌內部使用,維護性比較好,裡面的很多工具也比較新。Tensorflow是採用C++和python寫成的,給的介面也是C+

tensorflow利用訓練模型進行目標檢測(一):預訓練模型的使用

err sync numpy sna sta porting trac git int32 一、運行樣例 官網鏈接:https://github.com/tensorflow/models/blob/master/research/object_detection/obje

tensorflow利用訓練模型進行目標檢測

一、安裝 首先系統中已經安裝了兩個版本的tensorflow,一個是通過keras安裝的, 一個是按照官網教程https://www.tensorflow.org/install/install_linux#InstallingNativePip使用Virtualenv 進行安裝的,第二個在根目錄下,做標記

tensorflow利用訓練模型進行目標檢測(二):將檢測結果存入mysql資料庫

mysql版本:5.7 ; 資料庫:rdshare;表captain_america3_sd用來記錄某幀是否被檢測。表captain_america3_d用來記錄檢測到的資料。 python模組,包部分內容參考http://www.runoob.com/python/python-modules.html&

tensorflow利用訓練模型進行目標檢測(四):檢測中的精度問題以及evaluation

一、tensorflow提供的evaluation Inference and evaluation on the Open Images dataset:https://github.com/tensorflow/models/blob/master/research/object_detection/g

[AI教程]TensorFlow入門:訓練卷積網路模型解決分類問題

介紹 1.相關包匯入 // An highlighted block import math import numpy as np import h5py import matplotlib.pyplot as plt import scipy from PI

實戰 | 一行命令訓練你的影象分類模型

1.專案介紹 這次給大家介紹一個很方便的訓練自己影象識別模型的一個程式。可以通過一行命令實現訓練自己的影象識別模型,並且訓練的速度很快,效果也不錯。 影象分類有三種訓練方式: 構建一個新的模型並從頭開始訓練,稱為scrach。 在已經訓練好的模型基礎上,修改模型的最後的全連線層,並重新訓

深度學習之影象分類模型AlexNet結構分析和tensorflow實現

在ImageNet上的影象分類challenge上,Hinton和他的學生Alex Krizhevsky提出的AlexNet網路結構模型贏得了2012屆的冠軍,重新整理了Image Classification的機率。因此,要研究CNN型別深度學習模型在影象分

tensorflow學習之訓練自己的CNN模型(簡單二分類

     本文借鑑已有cat-vs-dog模型,在此模型上進行修改。該模型可在以下網址下載,後續將對模型進行解析及進一步修改。https://download.csdn.net/download/twinkle_star1314/10414568。今天先對模型進行分析:一、模

深度學習之影象分類模型AlexNet解讀

版權宣告:本文為博主原創文章 https://blog.csdn.net/sunbaigui/article/details/39938097 在imagenet上的影象分類challenge上Alex提出的alexnet網路結構模型贏得了2012屆的冠軍。要研究CNN型別

文字分類——怎麼評價訓練出的分類模型

模型的評價就是對模型的準確性和覆蓋性的評價。 1. 當然最保險的方法是抽取大量預測例項,進行人工校對。原因是第1實際應用中沒有太多的標註測試集可用。第2當預測的例項類別分佈不均衡時,很可能導

深度學習核心技術實戰——影象分類模型

                                                                              影象分類模型1.LeNet-5: 每一個卷積核都會形成一個特徵圖,3個通道則是每個通道是不同的卷積核,但是最後是將三通道

使用caffe fine-tune一個單標籤影象分類模型

進行網路模型的訓練與測試(基於預訓練的模型)。 準備資料 這步主要是自己先把資料集劃分好,比如訓練集、驗證集和測試集各多少張,並把相應的圖片放到對應的資料夾下。一些針對影象本身的預處理,比如要進行資料增強,就可以在此步實現(資料增強在第2步資料轉換時也可以做,但是沒有自己手動的資料增強靈活)。除此之外

caffe 訓練自己的分類模型

學習caffe的最終目的,是可以利用自己的資料集,訓練模型,並解決實際問題。 所以在前面跑通了mnist和cifar-10例程的基礎上,嘗試訓練自己的模型,從頭到尾走一遍所有的流程。準備資料、訓練並得到模型,利用模型進行分類預測。 一、準備資料 1、在網上找了一些圖片,分

[caffe]深度學習之MSRA影象分類模型Deep Residual Network(深度殘差網路)解讀

一、簡介         MSRA的深度殘差網路在2015年ImageNet和COCO如下共5個領域取得第一名:ImageNet recognition, ImageNet detection, ImageNet localization, COCO detection,

[caffe]深度學習之影象分類模型VGG解讀

一、簡介 vgg和googlenet是2014年imagenet競賽的雙雄,這兩類模型結構有一個共同特點是go deeper。跟googlenet不同的是,vgg繼承了lenet以及alexnet的一些框架,尤其是跟alexnet框架非常像,vgg也是5個group的卷積、

c++調取inceptionv3網路實現影象分類

這個例子是在看tensorflow裡面的官網提供的例子裡面看到的,總體來說比較簡單,首先是模型下載,最好是用wget的方式下載,我用curl下載失敗: wget https://storage.googleapis.com/download.tensorflow.org/m

tensorflow如何繼續訓練之前儲存的模型

一:需重定義神經網路繼續訓練的方法1.訓練程式碼import numpy as np import tensorflow as tf x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 we

跟我上手深度學習: 五分鐘嘗試第一個深度學習(Caffe)訓練影象分類(詳細圖文步驟)

申請深度學習的開發環境 申請了使用者帳號後,進入“Supervessel Cloud”申請虛擬機器“Apply VM"。如下圖: 登陸之後,進入雲平臺的控制檯(dashboard),選擇介面頂部的“More services”,出現"Market Image"的選項