2D人体姿态估计虽然已经比较成熟,但是面对遮挡问题仍然效果不佳,现阶段对于单张图片进行关键点检测时应对遮挡问题,通常在训练时采用数据增强,注意力机制以及人体骨架知识先验。
1.数据增强
1.1 随机遮挡
在训练时随机使用蒙版进行遮挡进行数据增强,通过使用遮挡人体的部分肢体进行训练,增强模型的泛化能力,这类的解决模型对于遮挡问题的方法相对而言效果不是很好,对于随机的蒙版遮挡,模型的学习能力较弱。
代码样例:
import torch
import numpy as np
import torchvision.transforms as transforms
from PIL import Image
class RandomErasing(object):
def __init__(self, p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=0):
self.p = p
self.scale = scale
self.ratio = ratio
self.value = value
def __call__(self, img):
if np.random.rand() > self.p:
return img
img = np.array(img)
for _ in range(np.random.randint(1, 3)):
area = img.shape[0] * img.shape[1]
target_area = np.random.uniform(self.scale[0], self.scale[1]) * area
aspect_ratio = np.random.uniform(self.ratio[0], self.ratio[1])
h = int(round(np.sqrt(target_area * aspect_ratio)))
w = int(round(np.sqrt(target_area / aspect_ratio)))
if w < img.shape[1] and h < img.shape[0]:
x = np.random.randint(0, img.shape[1] - w + 1)
y = np.random.randint(0, img.shape[0] - h + 1)
img[y:y + h, x:x + w, :] = self.value
return Image.fromarray(img)
transform = transforms.Compose([
RandomErasing(p=0.5)
])
# Example usage:
img = Image.open('example.jpg')
transformed_img = transform(img)
transformed_img.show()
1.2贴图方法
贴图进行数据增强,一般是采用贴人体的肢体或者是躯干,模拟拥挤人群中人员相互遮挡的问题,这种模拟遮挡方式比较真实更加符合现实的实际情况。
from PIL import Image, ImageDraw
def apply_texture(img_path, mask_path, output_path, position=(0, 0)):
img = Image.open(img_path)
mask = Image.open(mask_path).convert('RGBA')
# Resize mask to fit the image
mask = mask.resize(img.size)
# Apply mask to the image
img.paste(mask, position, mask)
img.save(output_path)
# Example usage:
apply_texture('person.jpg', 'mask.png', 'person_with_mask.jpg')
1.3生成网络的方式
通过生成图片中背景部分进行遮挡,这种遮挡方式相对于上面方式更加符合现实的实际情况,遮挡后然后进行训练。
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, ReLU, Add
from tensorflow.keras.models import Model
def build_generator():
inputs = Input(shape=(256, 256, 3))
# Simple generator model
x = Conv2D(64, (7, 7), padding='same')(inputs)
x = ReLU()(x)
x = Conv2D(128, (5, 5), padding='same')(x)
x = ReLU()(x)
x = Conv2D(3, (7, 7), padding='same')(x)
outputs = Add()([inputs, x])
model = Model(inputs, outputs)
return model
# Example usage:
generator = build_generator()
generator.compile(optimizer='adam', loss='mse')
简单实现,根据实际情况修改
数据增强方式根据自己的任务进行选择。
2.使用注意力机制
解决遮挡问题时也可以使用注意力机制方法,使得网络可以更加关注主要目标的信息进行学习。
3.利用先验知识
假设进行人体姿态估计时可以使用人体相关骨骼点的相对位置进行补全,比如对称的点相对位置根据已有的信息进行估计补全。