魔改检测模型RFBNet用作分类的实验,含pytorch代码

RFBNet是一个比较经典的图像分割模型,该模型使用了空洞卷积、多分支融合和残差的思路。

论文地址:https://arxiv.org/pdf/1711.07767.pdf

其核心模块的示意图如下:

因为我本身的研究方向只有分类和分割,不做检测。

所以,我尝试把这个模块融入到分类模型中。

一方面是好奇,是否能提升分类结果,其次也是作为pytorch代码的日常训练。

实验思路很简单,首先图像输入vgg19的前两个stage,后接一个RFB模块,最后接全局池化和FC层。

代码:

class RFBNet(nn.Module):

    def __init__(self, RFBBlock, num_classes=6):
        super(RFBNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 64, 3, 1, 1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.Conv2d(64, 64, 3, 1, 1),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(2,2)
            )
        self.layer2 = nn.Sequential(
            nn.Conv2d(64, 128, 3, 1, 1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.Conv2d(128, 128, 3, 1, 1),
            nn.BatchNorm2d(128),
            nn.ReLU(),
            nn.MaxPool2d(2,2)
            )
        self.layer3 = self._make_layer(RFBBlock,128,512)
        self.gap1 = nn.Sequential(nn.AdaptiveAvgPool2d(1))
        self.fc1 = nn.Sequential(
            nn.Flatten(),
            nn.Linear(512,6),)

    def forward(self,x):
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.gap1(x)
        out = self.fc1(x)
        return out
        
    def _make_layer(self,block,in_planes, out_planes):
        layer = block(in_planes, out_planes)
        return nn.Sequential(layer)

RFB模块的代码如下:

class RFBBlock(nn.Module):

    def __init__(self, in_planes, out_planes, stride=1, scale = 0.1, visual = 1):
        super(RFBBlock, self).__init__()
        self.scale = scale
        self.out_channels = out_planes
        inter_planes = in_planes // 8
        self.branch0 = nn.Sequential(
                BasicConv(in_planes, 2*inter_planes, kernel_size=1, stride=stride),
                BasicConv(2*inter_planes, 2*inter_planes, kernel_size=3, stride=1, padding=visual, dilation=visual, relu=False)
                )
        self.branch1 = nn.Sequential(
                BasicConv(in_planes, inter_planes, kernel_size=1, stride=1),
                BasicConv(inter_planes, 2*inter_planes, kernel_size=(3,3), stride=stride, padding=(1,1)),
                BasicConv(2*inter_planes, 2*inter_planes, kernel_size=3, stride=1, padding=visual+1, dilation=visual+1, relu=False)
                )
        self.branch2 = nn.Sequential(
                BasicConv(in_planes, inter_planes, kernel_size=1, stride=1),
                BasicConv(inter_planes, (inter_planes//2)*3, kernel_size=3, stride=1, padding=1),
                BasicConv((inter_planes//2)*3, 2*inter_planes, kernel_size=3, stride=stride, padding=1),
                BasicConv(2*inter_planes, 2*inter_planes, kernel_size=3, stride=1, padding=2*visual+1, dilation=2*visual+1, relu=False)
                )

        self.ConvLinear = BasicConv(6*inter_planes, out_planes, kernel_size=1, stride=1, relu=False)
        self.shortcut = BasicConv(in_planes, out_planes, kernel_size=1, stride=stride, relu=False)
        self.relu = nn.ReLU(inplace=False)

    def forward(self,x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)

        out = torch.cat((x0,x1,x2),1)
        out = self.ConvLinear(out)
        short = self.shortcut(x)
        out = out*self.scale + short
        out = self.relu(out)

        return out

这么简单一改之后,模型的实验结果比VGG19和resnet都强,但是这种级别的创新想发论文是不可能的,只能发表在csdn上(笑

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个基于openpose模型的用于检测坐姿的pytorch代码,该代码使用pytorch实现,需要安装pytorch、opencv-python和OpenPose模型。 ```python import cv2 import torch import numpy as np # 加载OpenPose模型 net = cv2.dnn.readNetFromTensorflow("path/to/openpose.pb") # 定义坐姿检测函数 def detect_pose(image): # 图像预处理 input_blob = cv2.dnn.blobFromImage(image, 1.0 / 255, (368, 368), (0, 0, 0), swapRB=False, crop=False) net.setInput(input_blob) # 获取OpenPose输出 output = net.forward() # 解析OpenPose输出 points = [] for i in range(18): prob_map = output[0, i, :, :] prob_map = cv2.resize(prob_map, (image.shape[1], image.shape[0])) _, confidence, _, point = cv2.minMaxLoc(prob_map) if confidence > 0.1: points.append((int(point[0]), int(point[1]))) else: points.append(None) # 计算坐姿得分 score = 0 if points[8] is not None and points[11] is not None and points[1] is not None: hip_height = points[8][1] + points[11][1] - points[1][1] hip_width = abs(points[8][0] - points[11][0]) if hip_width > 0: score = hip_height / hip_width return score, points # 测试坐姿检测函数 if __name__ == "__main__": image = cv2.imread("path/to/image.jpg") score, points = detect_pose(image) print("坐姿得分:", score) for i, point in enumerate(points): if point is not None: cv2.circle(image, point, 3, (0, 0, 255), -1) cv2.imshow("image", image) cv2.waitKey(0) ``` 注意,上述代码中的OpenPose模型需要下载并放置在指定的路径中。你可以从OpenPose的官方网站下载预训练模型,也可以使用第三方提供的预训练模型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝海渔夫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值