我有形状为 (128, 128, 3) 的图像
应用此代码后:
person_segment = image_tensor * np.repeat(seg_tensor[..., np.newaxis], 3, axis=-1)
忽略没有细节的像素,并在分割中添加第三个通道以允许矩阵乘法。
代码有什么问题?
我尝试将图像和张量转换为 numpy 数组,并在应用点积之前存储 3D 分割。
这里是完整的代码:
image = Image.open(image_id)
transform = transforms.Compose([
transforms.PILToTensor()
])
image_tensor = transform(image).movedim(0,-1)
plt.imshow(image_tensor)
plt.show()
inputs = image_processor(images=image, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
list(logits.shape)
upsampled_logits = nn.functional.interpolate(
logits,
size=image.size[::-1],
mode='bilinear',
align_corners=False
)
seg_tensor = upsampled_logits.argmax(dim=1)[0]
plt.imshow(seg_tensor)
plt.show()
person_segment = image_tensor * np.repeat(seg_tensor[..., np.newaxis], 3, axis=-1)
plt.imshow(person_segment)
plt.show()
存储图像示例:
array([[160, 160, 160, ..., 162, 160, 158],
[163, 163, 163, ..., 164, 162, 160],
[164, 164, 164, ..., 166, 164, 162],
...,
[ 98, 78, 65, ..., 115, 114, 110],
[ 93, 61, 35, ..., 115, 117, 115],
[117, 72, 36, ..., 119, 116, 112]],
dtype=uint8)