(input, field_of_view, weights, dim, n_dims, pad_sz, pad_mode,
fw)
| 215 | |
| 216 | |
| 217 | def apply_weights(input, field_of_view, weights, dim, n_dims, pad_sz, pad_mode, |
| 218 | fw): |
| 219 | # for this operation we assume the resized dim is the first one. |
| 220 | # so we transpose and will transpose back after multiplying |
| 221 | tmp_input = fw_swapaxes(input, dim, 0, fw) |
| 222 | |
| 223 | # apply padding |
| 224 | tmp_input = fw_pad(tmp_input, fw, pad_sz, pad_mode) |
| 225 | |
| 226 | # field_of_view is a tensor of order 2: for each output (1d location |
| 227 | # along cur dim)- a list of 1d neighbors locations. |
| 228 | # note that this whole operations is applied to each dim separately, |
| 229 | # this is why it is all in 1d. |
| 230 | # neighbors = tmp_input[field_of_view] is a tensor of order image_dims+1: |
| 231 | # for each output pixel (this time indicated in all dims), these are the |
| 232 | # values of the neighbors in the 1d field of view. note that we only |
| 233 | # consider neighbors along the current dim, but such set exists for every |
| 234 | # multi-dim location, hence the final tensor order is image_dims+1. |
| 235 | neighbors = tmp_input[field_of_view] |
| 236 | |
| 237 | # weights is an order 2 tensor: for each output location along 1d- a list |
| 238 | # of weights matching the field of view. we augment it with ones, for |
| 239 | # broadcasting, so that when multiplies some tensor the weights affect |
| 240 | # only its first dim. |
| 241 | tmp_weights = fw.reshape(weights, (*weights.shape, * [1] * (n_dims - 1))) |
| 242 | |
| 243 | # now we simply multiply the weights with the neighbors, and then sum |
| 244 | # along the field of view, to get a single value per out pixel |
| 245 | tmp_output = (neighbors * tmp_weights).sum(1) |
| 246 | |
| 247 | # we transpose back the resized dim to its original position |
| 248 | return fw_swapaxes(tmp_output, 0, dim, fw) |
| 249 | |
| 250 | |
| 251 | def apply_convs(input, scale_factor, in_sz, out_sz, weights, dim, pad_sz, |
no test coverage detected