【数据处理】如何在图片中随机采样

在图片中随机采样,而且不与已经标注的物体重合,啥也不说直接上代码:

"""
在图片随机采样宽为150,高为80的负样本
"""

import os
import json
import random

import cv2
import numpy as np

oimg_path = r"C:\Users\9ling\Desktop\YiLiuWuDataset_hardsample\original_dataset\yuedongguan_01"
ojson_path = r"C:\Users\9ling\Desktop\YiLiuWuDataset_hardsample\original_dataset\yuedongguan_01.json"

train_imgpath = r"C:\Users\9ling\Desktop\YiLiuWuDataset_hardsample\YiliuwuDataset\train\yuedongguan01"
train_jsonpath = r"C:\Users\9ling\Desktop\YiLiuWuDataset_hardsample\YiliuwuDataset\train\yuedongguan01\yuedongguan01.json"


def IoU(boxA, boxB):
    boxA = [int(x) for x in boxA]
    boxB = [int(x) for x in boxB]
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])
    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
    iou = interArea / float(boxAArea + boxBArea - interArea)
    return iou


json_list = {"labels": []}


def gain(ojson_path):
    jdata = json.load(open(ojson_path))["labels"]
    for item in jdata:
        sImg_path = os.path.join(oimg_path, item["filename"].split("\\")[-1])
        im = cv2.imdecode(np.fromfile(sImg_path), 1)
        height, width = im.shape[:-1]
        while True:
            rlist = []
            rx1 = random.choice(range(1, width - 150 - 1, 1))
            ry1 = random.choice(range(1, height - 80 - 1, 1))
            rx2 = min(rx1 + 150, width - 1)
            ry2 = min(ry1 + 80, height - 1)
            rlist.extend([rx1, ry1, rx2, ry2])
            sum_ious = 0
            for anno in item["annotations"]:
                alist = []
                ax1 = anno["x"]
                ay1 = anno["y"]
                ax2 = ax1 + anno["width"] - 1
                ay2 = ay1 + anno["height"] - 1
                alist.extend([ax1, ay1, ax2, ay2])
                if IoU(rlist, alist) == 0:
                    sum_ious += IoU(rlist, alist)
                    continue
                else:
                    sum_ious += IoU(rlist, alist)
                    break
            if sum_ious == 0:
                item["annotations"].append({"class": "negativesample01",
                                            "height": ry2 - ry1 + 1,
                                            "width": rx2 - rx1 + 1,
                                            "x": rx1,
                                            "y": ry1})
                break
        json_list["labels"].append(item)


gain(ojson_path)
json_test_str = json.dumps(json_list, indent=4)
with open(train_jsonpath, 'w') as json_file:
    json_file.write(json_test_str)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要加载VOC2012数据集并筛选出dog类的图片。可以使用torchvision的datasets模块来加载数据集,然后使用transforms模块进行数据增强。 ```python import torch import torchvision.transforms as transforms import torchvision.datasets as datasets # 数据增强 transform = transforms.Compose([ transforms.RandomResizedCrop(32), transforms.RandomHorizontalFlip(), transforms.ToTensor() ]) # 加载VOC2012数据集 train_dataset = datasets.VOCDetection(root='./data', year='2012', image_set='train', download=True, transform=transform) # 筛选dog类的图片 dog_dataset = [] for img, target in train_dataset: for obj in target['annotation']['object']: if obj['name'] == 'dog': dog_dataset.append(img) break ``` 接下来,我们需要对dog类的图片进行随机采样,并在每个采样图片挖掘具有判别性和频繁性的一类图片。这里我们使用LeNet作为挖掘算法。LeNet是一个经典的卷积神经网络,适用于处理小尺寸图像。 ```python import torch.nn as nn import torch.nn.functional as F # 定义LeNet模型 class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool1(F.relu(self.conv1(x))) x = self.pool2(F.relu(self.conv2(x))) x = x.view(-1, 16 * 5 * 5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x # 随机采样dog类的图片块 sampled_images = [] for dog_img in dog_dataset: h, w = dog_img.size[-2], dog_img.size[-1] x = torch.randint(0, w - 32, (10,)) y = torch.randint(0, h - 32, (10,)) for i in range(10): sampled_images.append(dog_img[:, :, y[i]:y[i]+32, x[i]:x[i]+32]) # 挖掘具有判别性和频繁性的一类图片 model = LeNet() model.train() optim = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) for epoch in range(10): for img in sampled_images: optim.zero_grad() output = model(img) loss = F.cross_entropy(output, torch.tensor([0])) loss.backward() optim.step() # 可视化挖掘结果 import matplotlib.pyplot as plt import numpy as np fig, axs = plt.subplots(2, 5) for i in range(2): for j in range(5): img = model.conv1.weight[i*5+j].detach().numpy() img = np.transpose(img, (1, 2, 0)) img = (img - np.min(img)) / (np.max(img) - np.min(img)) axs[i][j].imshow(img) plt.show() ``` 最后,我们可以将挖掘出的图片可视化。这里我们选择可视化LeNet模型的第一层卷积层的权重,因为这一层通常可以提取出输入图像的基本特征。 代码,我们使用matplotlib库将挖掘出的10张图片可视化。每张图片对应LeNet模型第一层的一个卷积核权重。可以看到,这些卷积核权重对应的是具有判别性和频繁性的一类图片的基本特征。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值