2D目标检测中的IOU函数

2D目标检测中的IOU函数

代码如下(示例):

def calculate_iou(box1,box2):
    x1,y1,x2,y2 = box1  # xtl ,ytl ,xbr ,ybr
    x3,y3,x4,y4 = box2  # xtl ,ytl ,xbr ,ybr
    x_inter1 = max(x1,x3)
    y_inter1 = min(y1,y3)
    x_inter2 = min(x2,x4)
    y_inter2 = max(y2,y4)
    width_inter = abs(x_inter2 - x_inter1)
    height_inter = abs(y_inter2 - y_inter1)
    area_inter = width_inter * height_inter
    width_box1 = abs(x2 - x1)
    height_box1 = abs(y2 - y1)
    width_box2 = abs(x4 - x3)
    height_box2 = abs(y4 - y3)
    area_box1 = width_box1 * height_box1
    area_box2 = width_box2 * height_box2
    area_union = area_box2 + area_box1 - area_inter
    iou = area_inter / area_union
    return iou
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Faster R-CNN 进行交通标志检测的示例代码。这里使用 PyTorch 框架实现。 首先,我们需要定义 Faster R-CNN 模型的架构,这里使用 ResNet-50 作为特征提取器。我们还需要定义 RPN(Region Proposal Network)和 RoI(Region of Interest)池化层。 ```python import torch import torch.nn as nn import torchvision.models as models class FasterRCNN(nn.Module): def __init__(self, num_classes): super(FasterRCNN, self).__init__() # backbone network self.backbone = models.resnet50(pretrained=True) # RPN (Region Proposal Network) self.rpn = nn.Sequential( nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1), nn.ReLU(), nn.Conv2d(512, 3 * 2, kernel_size=1, stride=1, padding=0) ) # RoI (Region of Interest) Pooling self.roi_pool = nn.AdaptiveMaxPool2d((7, 7)) # classifier and regressor self.classifier = nn.Sequential( nn.Linear(2048 * 7 * 7, 4096), nn.ReLU(), nn.Linear(4096, 4096), nn.ReLU(), nn.Linear(4096, num_classes) ) self.regressor = nn.Sequential( nn.Linear(2048 * 7 * 7, 4096), nn.ReLU(), nn.Linear(4096, 4096), nn.ReLU(), nn.Linear(4096, num_classes * 4) ) def forward(self, x): features = self.backbone(x) rpn_output = self.rpn(features) # reshape RPN output rpn_output = rpn_output.permute(0, 2, 3, 1).contiguous().view(x.size(0), -1, 2) # RoI proposal proposals = self.proposal_generator(features, rpn_output) # RoI pooling rois = self.roi_pool(features, proposals) # classifier and regressor roi_features = rois.view(rois.size(0), -1) classifier_output = self.classifier(roi_features) regressor_output = self.regressor(roi_features) return classifier_output, regressor_output, proposals ``` 接下来,我们需要定义 RPN 和 RoI 池化层的前向传递函数。 ```python import torch.nn.functional as F from torch.autograd import Variable class RPN(nn.Module): def __init__(self, in_channels=512, num_anchors=3): super(RPN, self).__init__() self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1) self.cls_layer = nn.Conv2d(in_channels, num_anchors * 2, kernel_size=1, stride=1, padding=0) self.reg_layer = nn.Conv2d(in_channels, num_anchors * 4, kernel_size=1, stride=1, padding=0) self.anchor_scales = [8, 16, 32] def forward(self, x): batch_size = x.shape[0] feature_map = self.conv(x) cls_output = self.cls_layer(feature_map) reg_output = self.reg_layer(feature_map) cls_output = cls_output.permute(0, 2, 3, 1).contiguous().view(batch_size, -1, 2) reg_output = reg_output.permute(0, 2, 3, 1).contiguous().view(batch_size, -1, 4) return cls_output, reg_output class RoIPool(nn.Module): def __init__(self, output_size): super(RoIPool, self).__init__() self.output_size = output_size def forward(self, features, rois): num_rois = rois.shape[0] output = Variable(torch.zeros(num_rois, features.shape[1], self.output_size, self.output_size)) for i in range(num_rois): roi = rois[i] roi_x = int(round(roi[0].item())) roi_y = int(round(roi[1].item())) roi_w = int(round(roi[2].item() - roi[0].item())) roi_h = int(round(roi[3].item() - roi[1].item())) roi_feature = features[:, :, roi_y:roi_y+roi_h, roi_x:roi_x+roi_w] roi_feature = F.adaptive_max_pool2d(roi_feature, self.output_size) output[i] = roi_feature return output ``` 最后,我们可以使用上述定义的模型和函数进行交通标志检测。 ```python import torch.utils.data as data import torchvision.transforms as transforms import torchvision.datasets as datasets from PIL import Image class TrafficSignDataset(data.Dataset): def __init__(self, root): self.root = root self.transforms = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) self.img_paths = [] self.targets = [] with open(os.path.join(root, 'annotations.txt'), 'r') as f: for line in f.readlines(): img_path, x, y, w, h, label = line.strip().split(',') self.img_paths.append(os.path.join(root, img_path)) self.targets.append((int(x), int(y), int(w), int(h), int(label))) def __getitem__(self, index): img_path = self.img_paths[index] target = self.targets[index] img = Image.open(img_path).convert('RGB') img = self.transforms(img) return img, target def __len__(self): return len(self.img_paths) def collate_fn(batch): imgs = [] targets = [] for sample in batch: imgs.append(sample[0]) targets.append(sample[1]) return torch.stack(imgs, dim=0), targets def main(): # load dataset dataset = TrafficSignDataset('data/') dataloader = data.DataLoader(dataset, batch_size=4, shuffle=True, collate_fn=collate_fn) # create model model = FasterRCNN(num_classes=3) model.train() # define optimizer and loss function optimizer = torch.optim.Adam(model.parameters(), lr=0.001) criterion = nn.CrossEntropyLoss() # train model for epoch in range(10): for images, targets in dataloader: # move images and targets to GPU images = images.cuda() targets = [(torch.tensor([x, y, x+w, y+h]), label) for x, y, w, h, label in targets] targets = [t.cuda() for t in targets] # forward pass classifier_output, regressor_output, proposals = model(images) # calculate RPN loss rpn_cls_loss, rpn_reg_loss = calculate_rpn_loss(proposals, targets) rpn_loss = rpn_cls_loss + rpn_reg_loss # calculate RoI loss roi_cls_loss, roi_reg_loss = calculate_roi_loss(classifier_output, regressor_output, proposals, targets) roi_loss = roi_cls_loss + roi_reg_loss # calculate total loss loss = rpn_loss + roi_loss # backward pass optimizer.zero_grad() loss.backward() optimizer.step() print('Epoch: {} | RPN Loss: {:.4f} | RoI Loss: {:.4f} | Total Loss: {:.4f}'.format(epoch+1, rpn_loss.item(), roi_loss.item(), loss.item())) def calculate_rpn_loss(proposals, targets): rpn_cls_loss = 0 rpn_reg_loss = 0 for i in range(len(proposals)): proposal = proposals[i] target = targets[i] # calculate IoU between proposal and target iou = calculate_iou(proposal, target[0]) # calculate classification loss if iou >= 0.7: rpn_cls_loss += -torch.log(proposal[1]) elif iou < 0.3: rpn_cls_loss += -torch.log(1 - proposal[0]) # calculate regression loss if iou >= 0.5: rpn_reg_loss += smooth_l1_loss(proposal[0], target[0]) return rpn_cls_loss, rpn_reg_loss def calculate_roi_loss(classifier_output, regressor_output, proposals, targets): roi_cls_loss = 0 roi_reg_loss = 0 for i in range(len(proposals)): proposal = proposals[i] target = targets[i] # select positive and negative RoIs positive_indices = (proposal[:, 1] > proposal[:, 0]).nonzero().flatten() negative_indices = (proposal[:, 0] > proposal[:, 1]).nonzero().flatten() # calculate classification loss positive_cls_loss = -torch.log(classifier_output[i, positive_indices, target[1]]) negative_cls_loss = -torch.log(1 - classifier_output[i, negative_indices, target[1]]) roi_cls_loss += (positive_cls_loss.sum() + negative_cls_loss.sum()) / (len(positive_indices) + len(negative_indices)) # calculate regression loss positive_reg_loss = smooth_l1_loss(regressor_output[i, positive_indices, target[1] * 4:(target[1] + 1) * 4], target[0][positive_indices]) roi_reg_loss += positive_reg_loss.sum() / len(positive_indices) return roi_cls_loss, roi_reg_loss def calculate_iou(box1, box2): x1 = max(box1[0], box2[0]) y1 = max(box1[1], box2[1]) x2 = min(box1[2], box2[2]) y2 = min(box1[3], box2[3]) intersection = max(x2 - x1, 0) * max(y2 - y1, 0) area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) area2 = (box2[2] - box2[0]) * (box2[3] - box2[1]) union = area1 + area2 - intersection return intersection / union def smooth_l1_loss(input, target): diff = torch.abs(input - target) return torch.where(diff < 1, 0.5 * diff ** 2, diff - 0.5) if __name__ == '__main__': main() ``` 以上就是使用 Faster R-CNN 进行交通标志检测的示例代码。请注意,这只是一个简单的示例,实际应用可能需要对代码进行修改和调整,以适应不同的数据集和应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值