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
| def convert_conv_to_fc(input, filter_height=3, filter_width=3, stride=1, padding=0): input_padded = np.pad(input, ((0, 0), (0, 0), (padding, padding), (padding, padding)), 'constant', constant_values=(0, 0)) # [N, C, H, W] num, depth, height, width = input_padded.shape[:4]
res = [] for k in range(num): i = 0 while i < height: j = 0 while j < width: arr = input_padded[k, :, i:i + filter_height, j:j + filter_width] res.append(arr.flatten()) j += stride if (j + filter_width) > width: break i += stride if (i + filter_height) > height: break
return np.array(res)
def deconvert_fc_to_conv(input, output_shape, filter_height=3, filter_width=3, stride=2, padding=0, isstinct=False): output = np.zeros(output_shape) output_padded = np.pad(output, ((0, 0), (0, 0), (padding, padding), (padding, padding)), 'constant', constant_values=(0, 0)) # [N, C, H, W] num, depth, height, width = output_padded.shape[:4]
number = 0 for k in range(num): i = 0 while i < height: j = 0 while j < width: if isstinct: output_padded[k, :, i:i + filter_height, j:j + filter_width] = \ input[number].reshape(depth, filter_height, filter_width) else: output_padded[k, :, i:i + filter_height, j:j + filter_width] += \ input[number].reshape(depth, filter_height, filter_width) j += stride number += 1 if (j + filter_width) > width: break i += stride if (i + filter_height) > height: break
if padding == 0: return output_padded
return output_padded[:, :, padding:-padding, padding:-padding]
|