使用移动网络和移动网络检测奇异物体(dnn)opencv-python?

发布于 2025-01-14 07:16:07 字数 1708 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-01-21 07:16:07

简短回答:是的,您可以使用简单的逻辑运算来检查类 ID,然后仅显示属于该类的边界框(如果您不想使用单个输出层重新训练模型)

但是,要正确回答问题并使用最佳实际场景;让我们检查一下单类和多类对象检测之间的区别以及是否值得重新训练。

在模型架构方面,只有不同的层才是全连接层(当然只有当你使用相同的主干网进行特征提取时)。单类目标检测器只有回归层集,而多类目标检测模型有回归层+softmax分类器来检测和分类多类标签。

那么您应该使用哪一个呢?

在某些情况下(即使您试图检测单个类别),不同的对象看起来彼此非常相似,将它们分成多个类别并在多类别检测器中训练它们将减少为您的类别提供误报结果的机会想要欲望。您可能会认为这种情况就像您现在不知道猫是什么一样,并且由于它们与狗相似,因此您很容易将猫误认为是狗。但如果你现在知道猫和狗是分开的,那么将它们误认为彼此的可能性就很低。

几乎所有时候,深度学习问题都没有通用的解决方案。但现在了解它们背​​后的理论将引导您走向成功。

如果您想将该模型 (ssd_mobilenet_v3) 转换为单类对象检测器,您需要做的就是 删除全连接层,添加你自己的层,然后训练为平常

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Input, Flatten, Dense, Dropout
from tensorflow.keras.models import Model

# 1 for single-class detection
NUMBER_OF_CLASSES = 1

# Include_top False means you exclude FC (Fully Connected) Layers of the model
model = MobileNetV2(include_top=False, input_tensor=Input(150, 150, 3))

# This freezes all layers of the MobileNet to not update it during training process
model.trainable = False

# We flatten the output layer so we can add our own (With 1 class)
flatten = model.output
flatten = Flatten()(flatten)

# construct a fully-connected layer header to output the predicted
# bounding box coordinates
bboxHead = Dense(128, activation="relu")(flatten)
bboxHead = Dense(64, activation="relu")(bboxHead)
bboxHead = Dense(32, activation="relu")(bboxHead)
bboxHead = Dense(4, activation="sigmoid",
                 name="bounding_box")(bboxHead)

# construct a second fully-connected layer head, this one to predict
# the class label
softmaxHead = Dense(512, activation="relu")(flatten)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(512, activation="relu")(softmaxHead)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(NUMBER_OF_CLASSES, activation="softmax",
                    name="class_label")(softmaxHead)

model = Model(
    inputs=model.input,
    outputs=(bboxHead, softmaxHead)
)

## Training........

Short Answer: Yes, you can use a simple logical operation to check class ID then display only bounding boxes belonging to that class (If you don't want to retrain the model with single output layer)

However, to correctly answer the question and use the best practical scenario; let's check what's the difference between single class and multiclass object detection is and if it is worth to retrain it.

In terms of model architecture, only different layer is the fully connected layer (Of course only if you're using the same backbone for feature extraction.) Single-class object detectors only have a regression layer set, whereas multi-class object detection models have regression layer + softmax classifier to detect and classify multi class labels.

So which one you should use?

In some cases (even if you're trying to detect single class) where different objects look very similar to eachother, seperating them into multiple classes and training them in multi-class detector will reduce the chances to give false positive result for the class you want to desire. You may think this case like you do not now what cat is and since they're similar to dogs, you can easily mistake a cat to a dog. But if you now what cat and dog are seperately, the chances are low to mistake them for eachother.

Almost all the time, there is no general solution to a deep learning problem. But nowing the theory behind them will lead you to success.

If you want to convert that model (ssd_mobilenet_v3) to single-class object detector, all you need to do is remove fully connected layer, add your own then train as usual;

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Input, Flatten, Dense, Dropout
from tensorflow.keras.models import Model

# 1 for single-class detection
NUMBER_OF_CLASSES = 1

# Include_top False means you exclude FC (Fully Connected) Layers of the model
model = MobileNetV2(include_top=False, input_tensor=Input(150, 150, 3))

# This freezes all layers of the MobileNet to not update it during training process
model.trainable = False

# We flatten the output layer so we can add our own (With 1 class)
flatten = model.output
flatten = Flatten()(flatten)

# construct a fully-connected layer header to output the predicted
# bounding box coordinates
bboxHead = Dense(128, activation="relu")(flatten)
bboxHead = Dense(64, activation="relu")(bboxHead)
bboxHead = Dense(32, activation="relu")(bboxHead)
bboxHead = Dense(4, activation="sigmoid",
                 name="bounding_box")(bboxHead)

# construct a second fully-connected layer head, this one to predict
# the class label
softmaxHead = Dense(512, activation="relu")(flatten)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(512, activation="relu")(softmaxHead)
softmaxHead = Dropout(0.5)(softmaxHead)
softmaxHead = Dense(NUMBER_OF_CLASSES, activation="softmax",
                    name="class_label")(softmaxHead)

model = Model(
    inputs=model.input,
    outputs=(bboxHead, softmaxHead)
)

## Training........
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文