function [one_feat_sps, weight_pool_info]=do_sp_pooling(one_feat_img, one_sp_info) img_size=size(one_feat_img); num_units=img_size(1)*img_size(2); dim=img_size(3); one_feat_img=reshape(one_feat_img, [num_units dim]); img_size_org=one_sp_info.img_size; pixel_ind_map=reshape([1: num_units], [img_size(1) img_size(2)]); pixel_ind_map_org=imresize(pixel_ind_map, img_size_org, 'nearest'); pixel_ind_sps=one_sp_info.pixel_ind_sps; num_sp=numel(pixel_ind_sps); weight_pool_info=zeros([num_sp, num_units], 'like', one_feat_img); for idx_sp=1:num_sp pixel_ind_sp_one=pixel_ind_sps{idx_sp}; ind_pixels_in_map=pixel_ind_map_org(pixel_ind_sp_one); [ind_units,~,uniqueIndex] = unique(ind_pixels_in_map); frequency = accumarray(uniqueIndex(:),1)./numel(ind_pixels_in_map); frequency=single(frequency); freq_one_sp=zeros(1, num_units, 'single'); freq_one_sp(ind_units)=frequency; weight_pool_info(idx_sp, :)=freq_one_sp; end one_feat_sps=weight_pool_info*one_feat_img; end将上述代码转换为pytorch代码,并可视化超像素的索引】
时间: 2023-08-15 10:05:57 浏览: 103
DAB.zip_DAB SPS_DAB-DPS_DPS DAB_sps_tobaccojai
5星 · 资源好评率100%
以下是将给定的 MATLAB 代码转换为 PyTorch 代码的过程:
```
import torch
import torch.nn.functional as F
import numpy as np
from scipy import ndimage
def do_sp_pooling(one_feat_img, one_sp_info):
img_size = one_feat_img.shape
num_units = img_size[0] * img_size[1]
dim = img_size[2]
one_feat_img = one_feat_img.reshape(num_units, dim)
img_size_org = one_sp_info['img_size']
pixel_ind_map = np.arange(num_units).reshape(img_size[0], img_size[1])
pixel_ind_map_org = ndimage.zoom(pixel_ind_map, [img_size_org[0]/img_size[0], img_size_org[1]/img_size[1]], order=0)
pixel_ind_sps = one_sp_info['pixel_ind_sps']
num_sp = len(pixel_ind_sps)
weight_pool_info = torch.zeros((num_sp, num_units), dtype=one_feat_img.dtype, device=one_feat_img.device)
for idx_sp in range(num_sp):
pixel_ind_sp_one = pixel_ind_sps[idx_sp]
ind_pixels_in_map = pixel_ind_map_org[pixel_ind_sp_one]
_, uniqueIndex = np.unique(ind_pixels_in_map, return_inverse=True)
frequency = np.bincount(uniqueIndex) / len(ind_pixels_in_map)
frequency = frequency.astype(one_feat_img.dtype)
freq_one_sp = torch.zeros(num_units, dtype=one_feat_img.dtype, device=one_feat_img.device)
freq_one_sp[ind_pixels_in_map] = torch.tensor(frequency, dtype=one_feat_img.dtype, device=one_feat_img.device)
weight_pool_info[idx_sp, :] = freq_one_sp
one_feat_sps = torch.mm(weight_pool_info, one_feat_img)
return one_feat_sps, weight_pool_info
```
这里我们使用了 PyTorch 库中的相关函数来实现对应功能,同时为了可视化超像素的索引,我们可以在函数中增加一些代码:
```
def do_sp_pooling(one_feat_img, one_sp_info):
img_size = one_feat_img.shape
num_units = img_size[0] * img_size[1]
dim = img_size[2]
one_feat_img = one_feat_img.reshape(num_units, dim)
img_size_org = one_sp_info['img_size']
pixel_ind_map = np.arange(num_units).reshape(img_size[0], img_size[1])
pixel_ind_map_org = ndimage.zoom(pixel_ind_map, [img_size_org[0]/img_size[0], img_size_org[1]/img_size[1]], order=0)
pixel_ind_sps = one_sp_info['pixel_ind_sps']
num_sp = len(pixel_ind_sps)
weight_pool_info = torch.zeros((num_sp, num_units), dtype=one_feat_img.dtype, device=one_feat_img.device)
for idx_sp in range(num_sp):
pixel_ind_sp_one = pixel_ind_sps[idx_sp]
ind_pixels_in_map = pixel_ind_map_org[pixel_ind_sp_one]
_, uniqueIndex = np.unique(ind_pixels_in_map, return_inverse=True)
frequency = np.bincount(uniqueIndex) / len(ind_pixels_in_map)
frequency = frequency.astype(one_feat_img.dtype)
freq_one_sp = torch.zeros(num_units, dtype=one_feat_img.dtype, device=one_feat_img.device)
freq_one_sp[ind_pixels_in_map] = torch.tensor(frequency, dtype=one_feat_img.dtype, device=one_feat_img.device)
weight_pool_info[idx_sp, :] = freq_one_sp
# 可视化超像素的索引
img_sp = np.zeros_like(pixel_ind_map_org)
img_sp[pixel_ind_sp_one//img_size[1], pixel_ind_sp_one%img_size[1]] = 1
img_sp = ndimage.binary_dilation(img_sp, iterations=1)
img_sp = np.where(img_sp, idx_sp+1, 0)
img_sp = ndimage.zoom(img_sp, [img_size[0]/img_size_org[0], img_size[1]/img_size_org[1]], order=0)
plt.imshow(img_sp, cmap='jet', alpha=0.3, vmin=0, vmax=num_sp)
one_feat_sps = torch.mm(weight_pool_info, one_feat_img)
return one_feat_sps, weight_pool_info
```
这里我们使用 matplotlib 库来绘制可视化结果,以 jet 颜色映射来表示超像素的索引。
阅读全文