Guest User

Untitled

a guest
Jun 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import numpy as np
  2. from sklearn.feature_extraction.image import extract_patches
  3.  
  4. def conv2d(inputs, filters):
  5. """
  6. Args:
  7. inputs (np.ndarray): NHWC
  8. filters (np.ndarray):
  9. with shape [filter_height, filter_width, in_channels, out_channels]
  10. """
  11.  
  12. kH, kW, inC, outC = filters.shape
  13. patches = extract_patches(images, (1, kH, kW, 1))
  14. patches = patches.reshape((*patches.shape[:3], -1))
  15.  
  16. kernel = filters.reshape((kH*kW*inC, outC))
  17.  
  18. return patches @ filters.reshape((kH*kW*inC, outC))
  19.  
  20. if __name__ == "__main__":
  21.  
  22. B = 4
  23. H, W = 11, 11
  24. inC = 3
  25. outC = 6
  26. kH, kW = 3,3
  27.  
  28. images = np.random.randint(0, 2, (B,H,W, inC))
  29. kernel = np.random.random((kH,kW,inC,outC))
  30.  
  31. h = conv2d(images, kernel)
Add Comment
Please, Sign In to add comment