import numpy as np
import numba
@numba.jit(nopython=False, parallel=True)
def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
N, C, H, W = input_data.shape
out_h = (H + 2*pad - filter_h)//stride + 1
out_w = (W + 2*pad - filter_w)//stride + 1
img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))
for y in range(filter_h):
y_max = y + stride*out_h
for x in range(filter_w):
x_max = x + stride*out_w
col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]
col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)
return col
img = np.arange(1,65).reshape(2,2,4,4)
print(img)
col = im2col(img, filter_h=1, filter_w=1)
print(col)
'''
[[[[ 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]]]]
[[ 1. 2. 5. 6. 17. 18. 21. 22.]
[ 2. 3. 6. 7. 18. 19. 22. 23.]
[ 3. 4. 7. 8. 19. 20. 23. 24.]
[ 5. 6. 9. 10. 21. 22. 25. 26.]
[ 6. 7. 10. 11. 22. 23. 26. 27.]
[ 7. 8. 11. 12. 23. 24. 27. 28.]
[ 9. 10. 13. 14. 25. 26. 29. 30.]
[10. 11. 14. 15. 26. 27. 30. 31.]
[11. 12. 15. 16. 27. 28. 31. 32.]
[33. 34. 37. 38. 49. 50. 53. 54.]
[34. 35. 38. 39. 50. 51. 54. 55.]
[35. 36. 39. 40. 51. 52. 55. 56.]
[37. 38. 41. 42. 53. 54. 57. 58.]
[38. 39. 42. 43. 54. 55. 58. 59.]
[39. 40. 43. 44. 55. 56. 59. 60.]
[41. 42. 45. 46. 57. 58. 61. 62.]
[42. 43. 46. 47. 58. 59. 62. 63.]
[43. 44. 47. 48. 59. 60. 63. 64.]]
'''
class Convolution:
def __init__(self, W, b, stride=1, pad=0):
self.W = W
self.b = b
self.stride = stride
self.pad = pad
def forward(self, x):
FN, C, FH, FW = self.W.shape
N, C, H, W = x.shape
out_h = int(1 + (H + 2*self.pad - FH) / self.stride)
out_w = int(1 + (W + 2*self.pad - FW) / self.stride)
col = im2col(x, FH, FW, self.stride, self.pad)
col_W = self.W.reshape(FN, -1).T
out = np.dot(col, col_W) + self.b
out = out.reshape(N, out_h, out_w, -1).transpose(0, 3, 1, 2)
return out