在初始图像分类器中更改label_img.py以接受numpy数组(opencv图像)而不是jpg文件

时间:2019-01-17 02:07:09

标签: tensorflow deep-learning classification

我正在使用张量流为诗人初始训练的模型构建图像分类器。我的模型已经过训练,效果很好。但是,将其集成到我的应用程序中时,我遇到了一个问题。现有代码读取jpg(或其他图像格式)文件,并创建一个适用于模型的张量。我想输入一个numpy数组(opencv图像),而不是从磁盘读取图像。

我已经尝试了convert_to_tensor(https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor),但是我无法在其余代码中使用结果。其他方法是从不建议使用的仓库中引用一个较旧的示例,该示例使用的是

tfImage = tf.gfile.FastGFile(imageFileWithPath,'rb')。read() 运行网络以获取预测 预测= sess.run(finalTensor,{'DecodeJpeg / contents:0':tfImage})

我在粘贴时已经压紧了压痕,但是您应该能够理解这个主意

def read_tensor_from_image_file(file_name, input_height=299, 
input_width=299,
            input_mean=0, input_std=255):
input_name = "file_reader"
output_name = "normalized"

if type(file_name) is str:
 file_reader = tf.read_file(file_name, input_name)
if file_name.endswith(".png"):
  image_reader = tf.image.decode_png(file_reader, channels = 3,
                                     name='png_reader')
elif file_name.endswith(".gif"):
  image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
                                                name='gif_reader'))
elif file_name.endswith(".bmp"):
  image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
else:
  image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
                                      name='jpeg_reader')
float_caster = tf.cast(image_reader, tf.float32)
dims_expander = tf.expand_dims(float_caster, 0);
resized = tf.image.resize_bilinear(dims_expander, [input_height, 
input_width])

elif type(file_name) is np.ndarray:
 tf_img = tf.convert_to_tensor(file_name)
 resized = tf.image.resize_bilinear(tf_img, [input_height, input_width])
 normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
sess = tf.Session()
result = sess.run(normalized)

return result

def classify(file_name,model_file = 
 "tf_files/retrained_graph.pb",label_file = 
 "tf_files/retrained_labels.txt",output_layer = "final_result"):



 input_height = 299
 input_width = 299
 input_mean = 128
 input_std = 128
 input_layer = "Mul"

 graph = load_graph(model_file)

 t = read_tensor_from_image_file(file_name,
                              input_height=input_height,
                              input_width=input_width,
                              input_mean=input_mean,
                              input_std=input_std)


 input_name = "import/" + input_layer
 output_name = "import/" + output_layer
 input_operation = graph.get_operation_by_name(input_name);
 output_operation = graph.get_operation_by_name(output_name);



 with tf.Session(graph=graph) as sess:
  start = time.time()
  results = sess.run(output_operation.outputs[0],
                  {input_operation.outputs[0]: t})
  end=time.time()
 results = np.squeeze(results)

top_k = results.argsort()[-5:][::-1]
labels = load_labels(label_file)

print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
template = "{} (score={:0.5f})"
for i in top_k:
 print(template.format(labels[i], results[i]))

上面的代码在给文件一个预期的路径时有效,但是我对read_tensor_from_image_file的修改使用了一个numpy数组会产生错误。我怀疑我需要替换't'来将其更改为张量运算符以进行output_operations。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

如果直接馈入形状正确的np数组以进行会话评估,则它应按预期运行。

在这里,我使用openCV对np数组进行整形/归一化,因此该函数应返回正确的形状:

import cv2

def read_tensor_from_image_file(file_name,
                                input_height=299,
                                input_width=299,
                                input_mean=0,
                                input_std=255):
    input_name = "file_reader"
    output_name = "normalized"

    if type(file_name) is str:
        file_reader = tf.read_file(file_name, input_name)
        if file_name.endswith(".png"):
            image_reader = tf.image.decode_png(file_reader, channels = 3,
                                               name='png_reader')
        elif file_name.endswith(".gif"):
            image_reader = tf.squeeze(tf.image.decode_gif(file_reader,
                                                          name='gif_reader'))
        elif file_name.endswith(".bmp"):
            image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
        else:
            image_reader = tf.image.decode_jpeg(file_reader, channels = 3,
                                                name='jpeg_reader')
        float_caster = tf.cast(image_reader, tf.float32)
        dims_expander = tf.expand_dims(float_caster, 0);
        resized = tf.image.resize_bilinear(dims_expander, [input_height,
                                                           input_width])
        normalized = tf.divide(tf.subtract(resized, [input_mean]), 
                               [input_std])
        sess = tf.Session()
        result = sess.run(normalized)

    elif type(file_name) is np.ndarray:
        resized = cv2.resize(file_name, (input_width, input_height),
                             interpolation=cv2.INTER_LINEAR)
        normalized = (resized - input_mean) / input_std
        result = normalized

    return result