2016-06-29 18 views
10

TLDR: Không thể tìm ra cách sử dụng tính năng khởi tạo lại V3 cho nhiều dự đoán hình ảnh.Chỉnh sửa tensorflow inceptionV3 retraining-example.py cho nhiều phân loại

Xin chào mọi người :) Tôi đã dành một vài ngày tìm kiếm nhiều bài đăng trên stackoverflow và tài liệu, nhưng tôi không thể tìm thấy câu trả lời cho câu hỏi này. Rất nhiều sẽ đánh giá cao sự giúp đỡ về điều này!

tôi đã đào tạo lại một mô hình tensorflow inceptionV3 trên hình ảnh mới, và nó có thể làm việc trên hình ảnh mới bằng cách làm theo các hướng dẫn tại https://www.tensorflow.org/versions/r0.9/how_tos/image_retraining/index.html và sử dụng các lệnh sau:

bazel build tensorflow/examples/label_image:label_image && \ 
bazel-bin/tensorflow/examples/label_image/label_image \ 
--graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt \ 
--output_layer=final_result \ 
--image= IMAGE_DIRECTORY_TO_CLASSIFY 

Tuy nhiên, tôi cần phải phân loại nhiều hình ảnh (như một tập dữ liệu), và nghiêm túc bị mắc kẹt về cách làm như vậy. Tôi đã tìm thấy ví dụ sau tại

https://github.com/eldor4do/Tensorflow-Examples/blob/master/retraining-example.py

về cách sử dụng các mô hình đào tạo lại, nhưng một lần nữa, nó là rất thưa thớt trên chi tiết về làm thế nào để sửa đổi nó cho nhiều phân loại.

Từ những gì tôi đã thu thập từ hướng dẫn MNIST, tôi cần phải nhập feed_dict vào đối tượng sess.run(), nhưng đã bị kẹt ở đó vì tôi không thể hiểu cách triển khai nó trong ngữ cảnh này.

Bất kỳ hỗ trợ nào sẽ được đánh giá rất cao! :)

EDIT:

Chạy script Styrke với một số sửa đổi, tôi đã nhận này

[email protected]:~/git$ python tensorflowMassPred.py I 
     tensorflow/stream_executor/dso_loader.cc:108] successfully opened 
     CUDA library libcublas.so locally I 
     tensorflow/stream_executor/dso_loader.cc:108] successfully opened 
     CUDA library libcudnn.so locally I 
     tensorflow/stream_executor/dso_loader.cc:108] successfully opened 
     CUDA library libcufft.so locally I 
     tensorflow/stream_executor/dso_loader.cc:108] successfully opened 
     CUDA library libcuda.so locally I 
     tensorflow/stream_executor/dso_loader.cc:108] successfully opened 
     CUDA library libcurand.so locally 
     /home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1197: 
     VisibleDeprecationWarning: converting an array with ndim > 0 to an 
     index will result in an error in the future 
     result_shape.insert(dim, 1) I 
     tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:924] successful 
     NUMA node read from SysFS had negative value (-1), but there must be 
     at least one NUMA node, so returning NUMA node zero I 
     tensorflow/core/common_runtime/gpu/gpu_init.cc:102] Found device 0 
     with properties: name: GeForce GTX 660 major: 3 minor: 0 
     memoryClockRate (GHz) 1.0975 pciBusID 0000:01:00.0 Total memory: 
     2.00GiB Free memory: 1.78GiB I tensorflow/core/common_runtime/gpu/gpu_init.cc:126] DMA: 0 I 
     tensorflow/core/common_runtime/gpu/gpu_init.cc:136] 0: Y I 
     tensorflow/core/common_runtime/gpu/gpu_device.cc:806] Creating 
     TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 660, pci 
     bus id: 0000:01:00.0) W tensorflow/core/framework/op_def_util.cc:332] 
     Op BatchNormWithGlobalNormalization is deprecated. It will cease to 
     work in GraphDef version 9. Use tf.nn.batch_normalization(). E 
     tensorflow/core/common_runtime/executor.cc:334] Executor failed to 
     create kernel. Invalid argument: NodeDef mentions attr 'T' not in 
     Op<name=MaxPool; signature=input:float -> output:float; 
     attr=ksize:list(int),min=4; attr=strides:list(int),min=4; 
     attr=padding:string,allowed=["SAME", "VALID"]; 
     attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]>; 
     NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3, 
     3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency) 
     [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3, 
     3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]] 
     Traceback (most recent call last): File 
     "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 715, in _do_call 
      return fn(*args) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 697, in _run_fn 
      status, run_metadata) File "/home/waffle/anaconda3/lib/python3.5/contextlib.py", line 66, in 
     __exit__ 
      next(self.gen) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors.py", 
     line 450, in raise_exception_on_not_ok_status 
      pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors.InvalidArgumentError: NodeDef 
     mentions attr 'T' not in Op<name=MaxPool; signature=input:float -> 
     output:float; attr=ksize:list(int),min=4; 
     attr=strides:list(int),min=4; attr=padding:string,allowed=["SAME", 
     "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC", 
     "NCHW"]>; NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", 
     ksize=[1, 3, 3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency) 
     [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3, 
     3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]] 

     During handling of the above exception, another exception occurred: 

     Traceback (most recent call last): File "tensorflowMassPred.py", 
     line 116, in <module> 
      run_inference_on_image() File "tensorflowMassPred.py", line 98, in run_inference_on_image 
      {'DecodeJpeg/contents:0': image_data}) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 372, in run 
      run_metadata_ptr) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 636, in _run 
      feed_dict_string, options, run_metadata) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 708, in _do_run 
      target_list, options, run_metadata) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py", 
     line 728, in _do_call 
      raise type(e)(node_def, op, message) tensorflow.python.framework.errors.InvalidArgumentError: NodeDef 
     mentions attr 'T' not in Op<name=MaxPool; signature=input:float -> 
     output:float; attr=ksize:list(int),min=4; 
     attr=strides:list(int),min=4; attr=padding:string,allowed=["SAME", 
     "VALID"]; attr=data_format:string,default="NHWC",allowed=["NHWC", 
     "NCHW"]>; NodeDef: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", 
     ksize=[1, 3, 3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency) 
     [[Node: pool = MaxPool[T=DT_FLOAT, data_format="NHWC", ksize=[1, 3, 
     3, 1], padding="VALID", strides=[1, 2, 2, 1], 
     _device="/job:localhost/replica:0/task:0/gpu:0"](pool/control_dependency)]] 
     Caused by op 'pool', defined at: File "tensorflowMassPred.py", line 
     116, in <module> 
      run_inference_on_image() File "tensorflowMassPred.py", line 87, in run_inference_on_image 
      create_graph() File "tensorflowMassPred.py", line 68, in create_graph 
      _ = tf.import_graph_def(graph_def, name='') File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", 
     line 274, in import_graph_def 
      op_def=op_def) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", 
     line 2260, in create_op 
      original_op=self._default_original_op, op_def=op_def) File "/home/waffle/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", 
     line 1230, in __init__ 
      self._traceback = _extract_stack() 

Đây là kịch bản: một số chức năng sẽ bị xóa.

import os 
import numpy as np 
import tensorflow as tf 
os.chdir('tensorflow/') #if need to run in the tensorflow directory 
import csv,os 
import pandas as pd 
import glob 

imagePath = '../_images_processed/test' 
modelFullPath = '/tmp/output_graph.pb' 
labelsFullPath = '/tmp/output_labels.txt' 

# FILE NAME TO SAVE TO. 
SAVE_TO_CSV = 'tensorflowPred.csv' 


def makeCSV(): 
    global SAVE_TO_CSV 
    with open(SAVE_TO_CSV,'w') as f: 
     writer = csv.writer(f) 
     writer.writerow(['id','label']) 


def makeUniqueDic(): 
    global SAVE_TO_CSV 
    df = pd.read_csv(SAVE_TO_CSV) 
    doneID = df['id'] 
    unique = doneID.unique() 
    uniqueDic = {str(key):'' for key in unique} #for faster lookup 
    return uniqueDic 


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(modelFullPath, 'rb') as f: 
     graph_def = tf.GraphDef() 
     graph_def.ParseFromString(f.read()) 
     _ = tf.import_graph_def(graph_def, name='') 


def run_inference_on_image(): 
    answer = [] 
    global imagePath 
    if not tf.gfile.IsDirectory(imagePath): 
     tf.logging.fatal('imagePath directory does not exist %s', imagePath) 
     return answer 

    if not os.path.exists(SAVE_TO_CSV): 
     makeCSV() 

    files = glob.glob(imagePath+'/*.jpg') 
    uniqueDic = makeUniqueDic()   
    # Get a list of all files in imagePath directory 
    #image_list = tf.gfile.ListDirectory(imagePath) 

    # Creates graph from saved GraphDef. 
    create_graph() 

    with tf.Session() as sess: 

     softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') 

     for pic in files: 
      name = getNamePicture(pic) 
      if name not in uniqueDic: 
       image_data = tf.gfile.FastGFile(pic, 'rb').read() 
       predictions = sess.run(softmax_tensor, 
            {'DecodeJpeg/contents:0': image_data}) 
       predictions = np.squeeze(predictions) 

       top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions 
       f = open(labelsFullPath, 'rb') 
       lines = f.readlines() 
       labels = [str(w).replace("\n", "") for w in lines] 
#   for node_id in top_k: 
#    human_string = labels[node_id] 
#    score = predictions[node_id] 
#    print('%s (score = %.5f)' % (human_string, score)) 
       pred = labels[top_k[0]] 
       with open(SAVE_TO_CSV,'a') as f: 
        writer = csv.writer(f) 
        writer.writerow([name,pred]) 
    return answer 

if __name__ == '__main__': 
    run_inference_on_image() 

Trả lời

4

Vì vậy, nhìn vào kịch bản liên kết của bạn:

with tf.Session() as sess: 

    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) 

    top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions 

Trong đoạn này, image_data là hình ảnh mới mà bạn muốn để nuôi với mô hình, đó là tải một vài dòng trước đó:

image_data = tf.gfile.FastGFile(imagePath, 'rb').read() 

Vì vậy, bản năng của tôi sẽ thay đổi run_inference_on_image để chấp nhận imagePath làm thông số và sử dụng os.listdiros.path.join để làm điều đó trên mỗi hình ảnh trong tập dữ liệu của bạn.

+0

Xin chào, cảm ơn bạn đã trả lời! Tôi xin lỗi, tôi không hiểu ý bạn là gì. Bạn đang nói để tải tất cả các bức ảnh, sau đó loop sess.run()? – Wboy

+0

Đó là một cách tuyệt vời để đi về nó! : D – struct

+0

Thật không may, đó không phải là những gì tôi đang tìm kiếm. Nó giống như các hàng phù hợp từng cái một trong một trình phân loại bình thường (như xgboost) và cực kỳ chậm (mất 31 giờ cho hình ảnh 8k). Tôi đang tìm một giải pháp cung cấp toàn bộ hình ảnh X vào feed_dict và vào bộ phân loại và xuất các dự đoán cho tất cả các hình ảnh tại một lần chụp. – Wboy

5

Dữ liệu jpeg thô dường như được cấp trực tiếp cho hoạt động decode_jpeg, chỉ mất một hình ảnh làm đầu vào tại một thời điểm. Để xử lý nhiều hơn một hình ảnh tại một thời điểm, bạn có thể cần phải xác định thêm decode_jpeg ops. Nếu có thể làm điều đó thì tôi hiện không biết làm thế nào.

Điều tốt nhất tiếp theo, dễ dàng, có lẽ là để phân loại tất cả các hình ảnh từng người một bên trong một vòng lặp TensorFlow. Bằng cách này, bạn sẽ ít nhất tránh tải lại biểu đồ và bắt đầu một phiên TF mới cho mỗi hình ảnh mà bạn muốn phân loại, cả hai đều có thể mất khá nhiều thời gian nếu bạn phải làm điều đó rất nhiều.

Ở đây tôi đã thay đổi định nghĩa của hàm run_inference_on_image() để phân loại tất cả hình ảnh trong thư mục được chỉ định bởi biến số imagePath.Tôi chưa thử nghiệm mã này, vì vậy có thể có những vấn đề nhỏ cần được sửa.

def run_inference_on_image(): 
    answer = [] 

    if not tf.gfile.IsDirectory(imagePath): 
     tf.logging.fatal('imagePath directory does not exist %s', imagePath) 
     return answer 

    # Get a list of all files in imagePath directory 
    image_list = tf.gfile.ListDirectory(imagePath) 

    # Creates graph from saved GraphDef. 
    create_graph() 

    with tf.Session() as sess: 

     softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') 

     for i in image_list: 
      image_data = tf.gfile.FastGFile(i, 'rb').read() 
      predictions = sess.run(softmax_tensor, 
            {'DecodeJpeg/contents:0': image_data}) 
      predictions = np.squeeze(predictions) 

      top_k = predictions.argsort()[-5:][::-1] # Getting top 5 predictions 
      f = open(labelsFullPath, 'rb') 
      lines = f.readlines() 
      labels = [str(w).replace("\n", "") for w in lines] 
      for node_id in top_k: 
       human_string = labels[node_id] 
       score = predictions[node_id] 
       print('%s (score = %.5f)' % (human_string, score)) 

      answer.append(labels[top_k[0]]) 
    return answer 
+0

Xin chào, Cảm ơn bạn rất nhiều vì đã trả lời - Tôi gặp một số sự cố khi chạy tập lệnh này, tôi đã cập nhật câu trả lời của mình. Nó sẽ là tuyệt vời nếu bạn có thể giúp một lần nữa! :) – Wboy

+0

@Wboy Có một vấn đề [TensorFlow] (https://github.com/tensorflow/tensorflow/issues/1528) có vẻ liên quan đến vấn đề mới của bạn. – Styrke

+0

Chưa giải quyết xong, nhưng tôi sẽ trao cho bạn tiền thưởng trước khi kết thúc :) – Wboy

0

Tôi gặp vấn đề tương tự. Tôi đã làm theo tất cả các giải pháp có thể và cuối cùng tìm ra một giải pháp phù hợp với tôi. Lỗi này xảy ra khi phiên bản Tensorflow được sử dụng để đào tạo lại mô hình khác với phiên bản mà nó đang được sử dụng.

Giải pháp là cập nhật Tensorflow lên phiên bản mới nhất. Vì tôi đã sử dụng pip để cài đặt Tensorflow, tôi chỉ phải chạy lệnh sau:

sudo pip install tensorflow --upgrade 

Và nó hoạt động hoàn hảo.

Các vấn đề liên quan