- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试用 C++ 编写一个使用 YOLO 模型的推理程序。我搜索了一些关于darknet的信息,但是它必须使用.cfg文件来导入模型结构(这对我来说有点太复杂了......),因此我想用tensorflow来做程序。
(我的模型权重从.hdf5(在Python中使用)转换为.pb(在C++中使用))
我发现了一些用python编写的示例,看起来他们在推理过程之前做了一些工作... Source
def yolo_eval(yolo_outputs,
anchors,
num_classes,
image_shape,
max_boxes=50,
score_threshold=.6,
iou_threshold=.5):
"""Evaluate YOLO model on given input and return filtered boxes."""
num_layers = len(yolo_outputs)
anchor_mask = [[6,7,8], [3,4,5], [0,1,2]] if num_layers==3 else [[3,4,5], [1,2,3]] # default setting
input_shape = K.shape(yolo_outputs[0])[1:3] * 32
boxes = []
box_scores = []
for l in range(num_layers):
_boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l],
anchors[anchor_mask[l]], num_classes, input_shape, image_shape)
boxes.append(_boxes)
box_scores.append(_box_scores)
boxes = K.concatenate(boxes, axis=0)
box_scores = K.concatenate(box_scores, axis=0)
mask = box_scores >= score_threshold
max_boxes_tensor = K.constant(max_boxes, dtype='int32')
boxes_ = []
scores_ = []
classes_ = []
for c in range(num_classes):
# TODO: use keras backend instead of tf.
class_boxes = tf.boolean_mask(boxes, mask[:, c])
class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c])
nms_index = tf.image.non_max_suppression(
class_boxes, class_box_scores, max_boxes_tensor, iou_threshold=iou_threshold)
class_boxes = K.gather(class_boxes, nms_index)
class_box_scores = K.gather(class_box_scores, nms_index)
classes = K.ones_like(class_box_scores, 'int32') * c
boxes_.append(class_boxes)
scores_.append(class_box_scores)
classes_.append(classes)
boxes_ = K.concatenate(boxes_, axis=0)
scores_ = K.concatenate(scores_, axis=0)
classes_ = K.concatenate(classes_, axis=0)
return boxes_, scores_, classes_
我已经打印出了返回值它看起来像这样
boxes-> Tensor("concat_11:0", shape=(?, 4), dtype=float32)
scores-> Tensor("concat_12:0", shape=(?,), dtype=float32)
classes-> Tensor("concat_13:0", shape=(?,), dtype=int32)
我的YOLO模型(.hdf5)的原始输出是(我通过打印model.output得到这个)
tf.Tensor 'conv2d_59_1/BiasAdd:0' shape=(?, ?, ?, 21) dtype=float32
tf.Tensor 'conv2d_67_1/BiasAdd:0' shape=(?, ?, ?, 21) dtype=float32
tf.Tensor 'conv2d_75_1/BiasAdd:0' shape=(?, ?, ?, 21) dtype=float32
Python代码的推理部分是
out_boxes, out_scores, out_classes = sess.run(
[boxes, scores, classes],
feed_dict={
yolo_model.input: image_data,
input_image_shape: [image.size[1], image.size[0]],
K.learning_phase(): 0
})
与Python版本的推理代码相比,C++ 部分是... ( Reference )
int main()
{
string image = "test.jpg";
string graph = "yolo_weight.pb";
string labels = "coco.names";
int32 input_width = 416;
int32 input_height = 416;
float input_mean = 0;
float input_std = 255;
string input_layer = "input_1:0";
std::vector<std::string> output_layer = {"conv2d_59/BiasAdd:0", "conv2d_67/BiasAdd:0", "conv2d_75/BiasAdd:0" };
std::unique_ptr<tensorflow::Session> session;
string graph_path = tensorflow::io::JoinPath(root_dir, graph);
Status load_graph_status = LoadGraph(graph_path, &session);
std::vector<Tensor> resized_tensors;
string image_path = tensorflow::io::JoinPath(root_dir, image);
Status read_tensor_status = ReadTensorFromImageFile(image_path, input_height, input_width,
input_mean, input_std, &resized_tensors);
Tensor inpTensor = Tensor(DT_FLOAT, TensorShape({ 1, input_height, input_width, 3 }));
std::vector<Tensor> outputs;
cv::Mat srcImage = cv::imread(image);
cv::resize(srcImage, srcImage, cv::Size(input_width, input_height));
srcImage.convertTo(srcImage, CV_32FC3);
srcImage = srcImage / 255;
string ty = type2str(srcImage.type());
float *p = (&inpTensor)->flat<float>().data();
cv::Mat tensorMat(input_height, input_width, CV_32FC3, p);
srcImage.convertTo(tensorMat, CV_32FC3);
Status run_status = session->Run({{ input_layer, inpTensor }}, { output_layer }, {}, &outputs);
int cc = 1;
auto output_detection_class = outputs[0].tensor<float, 4>();
std::cout << "detection scores" << std::endl;
std::cout << "typeid(output_detection_scoreclass).name->" << typeid(output_detection_class).name() << std::endl;
for (int i = 0; i < 13; ++i)
{
for (int j = 0; j < 13; ++j)
{
for (int k = 0; k < 21; ++k)
{
// using (index_1, index_2, index_3) to access the element in a tensor
printf("i->%d, j->%d, k->%d\t", i, j, k);
std::cout << output_detection_class(1, i, j, k) << "\t";
cc += 1;
if (cc % 4 == 0)
{
std::cout << "\n";
}
}
}
std::cout << std::endl;
}
return 0;
}
C++版本推断部分的输出为
outputs.size()-> 3
outputs[0].shape()-> [1,13,13,21]
outputs[1].shape()-> [1,26,26,21]
outputs[2].shape()-> [1,52,52,21]
但是我得到的输出非常奇怪......
(outputs[0]的输出值看起来不像分数、类别或坐标中的任何一个......)
所以我想知道是不是因为我错过了推理之前用 python 编写的部分?或者我使用了错误的方式来获取输出数据?
我检查了一些相关的问题和答案...
1.Yolo v3 model output clarification with keras
2.Convert YoloV3 output to coordinates of bounding box, label and confidence
3.How to access tensorflow::Tensor C++
但是我还是不知道怎么做:(
我还找到了repo这可能会有所帮助,我看了一下它的yolo.cpp,但是它的模型输出张量的形状和我的不一样,我不确定是否可以直接修改代码,它的输出张量是
tf.Tensor 'import/output:0' shape=(?, 735) dtype = float32
感谢任何帮助或建议...
最佳答案
如果您仍在为此苦苦挣扎,我不知道您在哪里将 Sigmoid 和 Exp 应用于输出层值。
您可以查看这篇论文,其中描述了如何处理输出。
https://medium.com/analytics-vidhya/yolo-v3-theory-explained-33100f6d193
关于python - 如何使用 C++ 正确使用 tensorflow 从 YOLO 模型获取输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59677170/
我想用 Yolo 建立一个数据库,这是我第一次使用深度学习 如何为 Yolo 构建数据库并对其进行训练? 如何获得分类的权重? 对于刚接触深度学习的人来说是不是太难了? 最佳答案 是的,您可以轻松做到
我在互联网上搜索过,但发现的信息很少,我不明白 yolo 的 .cfg 中每个变量/值代表什么文件。所以我希望你们中的一些人可以提供帮助,我认为我不是唯一遇到这个问题的人,所以如果有人知道 2 或 3
我正在用 yolor 训练自定义数据集。我成功地运行了一次,但过了一段时间后,我无法很好地运行它。 我注意到的第一个错误是在训练部分: Traceback (most recent call last
我试图了解 Yolo4 的架构。 它由主干、颈部、密集预测和稀疏预测组成。 知道 Yolo 3 已经有 Backbone ,Yolo 4 是采用 Yolo 3 的所有架构,包括其 Backbone 还
I'm trying to train a yolov8 nano model with the following py script:我正试图用以下py脚本训练一个yolov8纳米模型:
我关注了this microsoft tutorial没有问题。但我想将模型更改为 yolo v3 或 v4。我从 onnx/models 获取 YOLOv4 onnx 模型并能够获得 yolov4
我关注了this microsoft tutorial没有问题。但我想将模型更改为 yolo v3 或 v4。我从 onnx/models 获取 YOLOv4 onnx 模型并能够获得 yolov4
我正在尝试在 Yolo Darknet 中使用 class ->2、filter(21) 训练我的自定义对象,收集了大约 5000 张大小 (1106x620) 的图像,设置学习率 .01 批处理是
我正在使用来自 AlexeyAB's fork of Darknet 的 YOLOv3 和 YOLOv3-Tiny .我知道图像大小必须是 32 的倍数。而且 batch除以 subdivisions
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
感谢您阅读我的问题。我使用谷歌colab。我需要 yad2k 库。也许这不支持pip。 有人成功安装yad2k吗? ImportError Traceback(最近一次调用最后一次) 在 () 13
我正在使用 YOLO 进行机器学习项目。我正在按照找到的指南创建自己的数据集 here (在如何训练(检测您的自定义对象)部分)。对于边界框,我需要知道我想在给定图片中训练 YOLO 的每个对象的 [
我在 coursera 中完成了深度学习特化的 YOLO 作业,它按照作业要求预测了 test.jpg 上的框。但后来我从官方 darknet 网站下载了 yolo.weights 并将其转换为 .h
我目前正在主分支 (https://github.com/AlexeyAB/darknet) 上使用 AlexeyAB 的 linux 版本在 Darknet YOLO 上进行训练, map /图表不
我想在暗网 YOLOv3 上获取边界框坐标(如 xmin、xmax、ymin、ymax)我尝试了一些方法,但是当我在更改代码后编译程序时,我看不到任何差异。 如何找到 Darknette 的边界框坐标
当我阅读 YOLO 论文时,它说它使用 K-means 制作 anchor 框。然而,当我看到实现这个的代码时,它似乎修复了 anchor 大小,如下所示。我希望你描述它的确切含义或指出我对此的误解。
目前我正在使用一种名为“Yolov2”的深度学习模型进行对象检测,我想用它来提取文本并将其保存在磁盘中,但我不知道该怎么做,如果有人知道更多关于那个,请给我建议 我使用 Tensorflow 谢谢 最
我有多个图像图表,所有这些图表都包含作为字母数字字符的标签,而不仅仅是文本标签本身。我希望我的 YOLO 模型能够识别其中存在的所有数字和字母数字字符。 我如何训练我的 YOLO 模型来做同样的事情。
我正在尝试为 YOLO/Darknet 中的一些图像手动创建标签文件,我需要为边界框填写一些值。 来自 YOLO 网站(https://pjreddie.com/darknet/yolo/): Now
我正在尝试在 Darknet YOLO v2 中训练自定义对象分类器 https://pjreddie.com/darknet/yolo/ 我收集了一个图像数据集,其中大多数尺寸为 6000 x 40
我是一名优秀的程序员,十分优秀!