Я строю классификатор изображений, используя тензорный поток для предтренированной модели поэтов. Моя модель обучена, и она работает хорошо. Однако при интеграции его в мое приложение я столкнулся с проблемой. Существующий код читает файл 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. Любая помощь приветствуется.