Spaces:
Runtime error
Runtime error
File size: 10,402 Bytes
da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe fb140f6 da48dbe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
import torch
import torch.nn as nn
import torch.nn.functional as F
def iuvmap_clean(U_uv, V_uv, Index_UV, AnnIndex=None):
Index_UV_max = torch.argmax(Index_UV, dim=1).float()
recon_Index_UV = []
for i in range(Index_UV.size(1)):
if i == 0:
recon_Index_UV_i = torch.min(
F.threshold(Index_UV_max + 1, 0.5, 0), -F.threshold(-Index_UV_max - 1, -1.5, 0)
)
else:
recon_Index_UV_i = torch.min(
F.threshold(Index_UV_max, i - 0.5, 0), -F.threshold(-Index_UV_max, -i - 0.5, 0)
) / float(i)
recon_Index_UV.append(recon_Index_UV_i)
recon_Index_UV = torch.stack(recon_Index_UV, dim=1)
if AnnIndex is None:
recon_Ann_Index = None
else:
AnnIndex_max = torch.argmax(AnnIndex, dim=1).float()
recon_Ann_Index = []
for i in range(AnnIndex.size(1)):
if i == 0:
recon_Ann_Index_i = torch.min(
F.threshold(AnnIndex_max + 1, 0.5, 0), -F.threshold(-AnnIndex_max - 1, -1.5, 0)
)
else:
recon_Ann_Index_i = torch.min(
F.threshold(AnnIndex_max, i - 0.5, 0), -F.threshold(-AnnIndex_max, -i - 0.5, 0)
) / float(i)
recon_Ann_Index.append(recon_Ann_Index_i)
recon_Ann_Index = torch.stack(recon_Ann_Index, dim=1)
recon_U = recon_Index_UV * U_uv
recon_V = recon_Index_UV * V_uv
return recon_U, recon_V, recon_Index_UV, recon_Ann_Index
def iuv_map2img(U_uv, V_uv, Index_UV, AnnIndex=None, uv_rois=None, ind_mapping=None, n_part=24):
device_id = U_uv.get_device()
batch_size = U_uv.size(0)
K = U_uv.size(1)
heatmap_size = U_uv.size(2)
Index_UV_max = torch.argmax(Index_UV, dim=1)
if AnnIndex is None:
Index_UV_max = Index_UV_max.to(torch.int64)
else:
AnnIndex_max = torch.argmax(AnnIndex, dim=1)
Index_UV_max = Index_UV_max * (AnnIndex_max > 0).to(torch.int64)
outputs = []
for batch_id in range(batch_size):
output = torch.zeros([3, U_uv.size(2), U_uv.size(3)], dtype=torch.float32).cuda(device_id)
output[0] = Index_UV_max[batch_id].to(torch.float32)
if ind_mapping is None:
output[0] /= float(K - 1)
else:
for ind in range(len(ind_mapping)):
output[0][output[0] == ind] = ind_mapping[ind] * (1. / n_part)
for part_id in range(0, K):
CurrentU = U_uv[batch_id, part_id]
CurrentV = V_uv[batch_id, part_id]
output[1,
Index_UV_max[batch_id] == part_id] = CurrentU[Index_UV_max[batch_id] == part_id]
output[2,
Index_UV_max[batch_id] == part_id] = CurrentV[Index_UV_max[batch_id] == part_id]
if uv_rois is None:
outputs.append(output.unsqueeze(0))
else:
roi_fg = uv_rois[batch_id][1:]
# x1 = roi_fg[0]
# x2 = roi_fg[2]
# y1 = roi_fg[1]
# y2 = roi_fg[3]
w = roi_fg[2] - roi_fg[0]
h = roi_fg[3] - roi_fg[1]
aspect_ratio = float(w) / h
if aspect_ratio < 1:
new_size = [heatmap_size, max(int(heatmap_size * aspect_ratio), 1)]
output = F.interpolate(output.unsqueeze(0), size=new_size, mode='nearest')
paddingleft = int(0.5 * (heatmap_size - new_size[1]))
output = F.pad(
output, pad=(paddingleft, heatmap_size - new_size[1] - paddingleft, 0, 0)
)
else:
new_size = [max(int(heatmap_size / aspect_ratio), 1), heatmap_size]
output = F.interpolate(output.unsqueeze(0), size=new_size, mode='nearest')
paddingtop = int(0.5 * (heatmap_size - new_size[0]))
output = F.pad(
output, pad=(0, 0, paddingtop, heatmap_size - new_size[0] - paddingtop)
)
outputs.append(output)
return torch.cat(outputs, dim=0)
def iuv_img2map(uvimages, uv_rois=None, new_size=None, n_part=24):
device_id = uvimages.get_device()
batch_size = uvimages.size(0)
uvimg_size = uvimages.size(-1)
Index2mask = [
[0], [1, 2], [3], [4], [5], [6], [7, 9], [8, 10], [11, 13], [12, 14], [15, 17], [16, 18],
[19, 21], [20, 22], [23, 24]
]
part_ind = torch.round(uvimages[:, 0, :, :] * n_part)
part_u = uvimages[:, 1, :, :]
part_v = uvimages[:, 2, :, :]
recon_U = []
recon_V = []
recon_Index_UV = []
recon_Ann_Index = []
for i in range(n_part + 1):
if i == 0:
recon_Index_UV_i = torch.min(
F.threshold(part_ind + 1, 0.5, 0), -F.threshold(-part_ind - 1, -1.5, 0)
)
else:
recon_Index_UV_i = torch.min(
F.threshold(part_ind, i - 0.5, 0), -F.threshold(-part_ind, -i - 0.5, 0)
) / float(i)
recon_U_i = recon_Index_UV_i * part_u
recon_V_i = recon_Index_UV_i * part_v
recon_Index_UV.append(recon_Index_UV_i)
recon_U.append(recon_U_i)
recon_V.append(recon_V_i)
for i in range(len(Index2mask)):
if len(Index2mask[i]) == 1:
recon_Ann_Index_i = recon_Index_UV[Index2mask[i][0]]
elif len(Index2mask[i]) == 2:
p_ind0 = Index2mask[i][0]
p_ind1 = Index2mask[i][1]
# recon_Ann_Index[:, i, :, :] = torch.where(recon_Index_UV[:, p_ind0, :, :] > 0.5, recon_Index_UV[:, p_ind0, :, :], recon_Index_UV[:, p_ind1, :, :])
# recon_Ann_Index[:, i, :, :] = torch.eq(part_ind, p_ind0) | torch.eq(part_ind, p_ind1)
recon_Ann_Index_i = recon_Index_UV[p_ind0] + recon_Index_UV[p_ind1]
recon_Ann_Index.append(recon_Ann_Index_i)
recon_U = torch.stack(recon_U, dim=1)
recon_V = torch.stack(recon_V, dim=1)
recon_Index_UV = torch.stack(recon_Index_UV, dim=1)
recon_Ann_Index = torch.stack(recon_Ann_Index, dim=1)
if uv_rois is None:
return recon_U, recon_V, recon_Index_UV, recon_Ann_Index
recon_U_roi = []
recon_V_roi = []
recon_Index_UV_roi = []
recon_Ann_Index_roi = []
if new_size is None:
M = uvimg_size
else:
M = new_size
for i in range(batch_size):
roi_fg = uv_rois[i][1:]
# x1 = roi_fg[0]
# x2 = roi_fg[2]
# y1 = roi_fg[1]
# y2 = roi_fg[3]
w = roi_fg[2] - roi_fg[0]
h = roi_fg[3] - roi_fg[1]
aspect_ratio = float(w) / h
if aspect_ratio < 1:
w_size = max(int(uvimg_size * aspect_ratio), 1)
w_margin = int((uvimg_size - w_size) / 2)
recon_U_roi_i = recon_U[i, :, :, w_margin:w_margin + w_size]
recon_V_roi_i = recon_V[i, :, :, w_margin:w_margin + w_size]
recon_Index_UV_roi_i = recon_Index_UV[i, :, :, w_margin:w_margin + w_size]
recon_Ann_Index_roi_i = recon_Ann_Index[i, :, :, w_margin:w_margin + w_size]
else:
h_size = max(int(uvimg_size / aspect_ratio), 1)
h_margin = int((uvimg_size - h_size) / 2)
recon_U_roi_i = recon_U[i, :, h_margin:h_margin + h_size, :]
recon_V_roi_i = recon_V[i, :, h_margin:h_margin + h_size, :]
recon_Index_UV_roi_i = recon_Index_UV[i, :, h_margin:h_margin + h_size, :]
recon_Ann_Index_roi_i = recon_Ann_Index[i, :, h_margin:h_margin + h_size, :]
recon_U_roi_i = F.interpolate(recon_U_roi_i.unsqueeze(0), size=(M, M), mode='nearest')
recon_V_roi_i = F.interpolate(recon_V_roi_i.unsqueeze(0), size=(M, M), mode='nearest')
recon_Index_UV_roi_i = F.interpolate(
recon_Index_UV_roi_i.unsqueeze(0), size=(M, M), mode='nearest'
)
recon_Ann_Index_roi_i = F.interpolate(
recon_Ann_Index_roi_i.unsqueeze(0), size=(M, M), mode='nearest'
)
recon_U_roi.append(recon_U_roi_i)
recon_V_roi.append(recon_V_roi_i)
recon_Index_UV_roi.append(recon_Index_UV_roi_i)
recon_Ann_Index_roi.append(recon_Ann_Index_roi_i)
recon_U_roi = torch.cat(recon_U_roi, dim=0)
recon_V_roi = torch.cat(recon_V_roi, dim=0)
recon_Index_UV_roi = torch.cat(recon_Index_UV_roi, dim=0)
recon_Ann_Index_roi = torch.cat(recon_Ann_Index_roi, dim=0)
return recon_U_roi, recon_V_roi, recon_Index_UV_roi, recon_Ann_Index_roi
def seg_img2map(segimages, uv_rois=None, new_size=None, n_part=24):
device_id = segimages.get_device()
batch_size = segimages.size(0)
uvimg_size = segimages.size(-1)
part_ind = torch.round(segimages[:, 0, :, :] * n_part)
recon_Index_UV = []
for i in range(n_part + 1):
if i == 0:
recon_Index_UV_i = torch.min(
F.threshold(part_ind + 1, 0.5, 0), -F.threshold(-part_ind - 1, -1.5, 0)
)
else:
recon_Index_UV_i = torch.min(
F.threshold(part_ind, i - 0.5, 0), -F.threshold(-part_ind, -i - 0.5, 0)
) / float(i)
recon_Index_UV.append(recon_Index_UV_i)
recon_Index_UV = torch.stack(recon_Index_UV, dim=1)
if uv_rois is None:
return None, None, recon_Index_UV, None
recon_Index_UV_roi = []
if new_size is None:
M = uvimg_size
else:
M = new_size
for i in range(batch_size):
roi_fg = uv_rois[i][1:]
# x1 = roi_fg[0]
# x2 = roi_fg[2]
# y1 = roi_fg[1]
# y2 = roi_fg[3]
w = roi_fg[2] - roi_fg[0]
h = roi_fg[3] - roi_fg[1]
aspect_ratio = float(w) / h
if aspect_ratio < 1:
w_size = max(int(uvimg_size * aspect_ratio), 1)
w_margin = int((uvimg_size - w_size) / 2)
recon_Index_UV_roi_i = recon_Index_UV[i, :, :, w_margin:w_margin + w_size]
else:
h_size = max(int(uvimg_size / aspect_ratio), 1)
h_margin = int((uvimg_size - h_size) / 2)
recon_Index_UV_roi_i = recon_Index_UV[i, :, h_margin:h_margin + h_size, :]
recon_Index_UV_roi_i = F.interpolate(
recon_Index_UV_roi_i.unsqueeze(0), size=(M, M), mode='nearest'
)
recon_Index_UV_roi.append(recon_Index_UV_roi_i)
recon_Index_UV_roi = torch.cat(recon_Index_UV_roi, dim=0)
return None, None, recon_Index_UV_roi, None
|