util.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # coding: utf-8
  2. import numpy as np
  3. def smooth_curve(x):
  4. """用于使损失函数的图形变圆滑
  5. 参考:http://glowingpython.blogspot.jp/2012/02/convolution-with-numpy.html
  6. """
  7. window_len = 11
  8. s = np.r_[x[window_len-1:0:-1], x, x[-1:-window_len:-1]]
  9. w = np.kaiser(window_len, 2)
  10. y = np.convolve(w/w.sum(), s, mode='valid')
  11. return y[5:len(y)-5]
  12. def shuffle_dataset(x, t):
  13. """打乱数据集
  14. Parameters
  15. ----------
  16. x : 训练数据
  17. t : 监督数据
  18. Returns
  19. -------
  20. x, t : 打乱的训练数据和监督数据
  21. """
  22. permutation = np.random.permutation(x.shape[0])
  23. x = x[permutation,:] if x.ndim == 2 else x[permutation,:,:,:]
  24. t = t[permutation]
  25. return x, t
  26. def conv_output_size(input_size, filter_size, stride=1, pad=0):
  27. return (input_size + 2*pad - filter_size) / stride + 1
  28. def im2col(input_data, filter_h, filter_w, stride=1, pad=0):
  29. """
  30. Parameters
  31. ----------
  32. input_data : 由(数据量, 通道, 高, 长)的4维数组构成的输入数据
  33. filter_h : 滤波器的高
  34. filter_w : 滤波器的长
  35. stride : 步幅
  36. pad : 填充
  37. Returns
  38. -------
  39. col : 2维数组
  40. """
  41. N, C, H, W = input_data.shape
  42. out_h = (H + 2*pad - filter_h)//stride + 1
  43. out_w = (W + 2*pad - filter_w)//stride + 1
  44. img = np.pad(input_data, [(0,0), (0,0), (pad, pad), (pad, pad)], 'constant')
  45. col = np.zeros((N, C, filter_h, filter_w, out_h, out_w))
  46. for y in range(filter_h):
  47. y_max = y + stride*out_h
  48. for x in range(filter_w):
  49. x_max = x + stride*out_w
  50. col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]
  51. col = col.transpose(0, 4, 5, 1, 2, 3).reshape(N*out_h*out_w, -1)
  52. return col
  53. def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
  54. """
  55. Parameters
  56. ----------
  57. col :
  58. input_shape : 输入数据的形状(例:(10, 1, 28, 28))
  59. filter_h :
  60. filter_w
  61. stride
  62. pad
  63. Returns
  64. -------
  65. """
  66. N, C, H, W = input_shape
  67. out_h = (H + 2*pad - filter_h)//stride + 1
  68. out_w = (W + 2*pad - filter_w)//stride + 1
  69. col = col.reshape(N, out_h, out_w, C, filter_h, filter_w).transpose(0, 3, 4, 5, 1, 2)
  70. img = np.zeros((N, C, H + 2*pad + stride - 1, W + 2*pad + stride - 1))
  71. for y in range(filter_h):
  72. y_max = y + stride*out_h
  73. for x in range(filter_w):
  74. x_max = x + stride*out_w
  75. img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]
  76. return img[:, :, pad:H + pad, pad:W + pad]