|
| 1 | +from .Core import Layer |
| 2 | +from .im2col import im2col_indices |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +class Dense(Layer): |
| 6 | + |
| 7 | + def __init__(self, n_in, n_out): |
| 8 | + super().__init__('dense_{}_{}'.format(n_in, n_out)) |
| 9 | + self.n_in = n_in |
| 10 | + self.n_out = n_out |
| 11 | + self.K = np.random.randn(n_out, n_in) |
| 12 | + self.bias = np.zeros(n_out) |
| 13 | + |
| 14 | + def forward(self, x): |
| 15 | + self.x = x |
| 16 | + w = self.K |
| 17 | + # the dense layer math: y = x*w |
| 18 | + y = w.dot(x.T) |
| 19 | + return y.T + self.bias.reshape((1, -1)) |
| 20 | + |
| 21 | + def load_from_torch(self, weights, bias): |
| 22 | + self.K = weights |
| 23 | + self.bias = bias |
| 24 | + |
| 25 | + |
| 26 | +class Conv2d(Layer): |
| 27 | + |
| 28 | + def __init__(self, C_in, C_out, K_s, Stride): |
| 29 | + """ |
| 30 | + Params: |
| 31 | + in channels, out channles, kernel size, stride |
| 32 | + """ |
| 33 | + super().__init__("conv_{}_{}x{}x{}".format(C_out, C_in, K_s, K_s)) |
| 34 | + self.c_in = C_in |
| 35 | + self.c_out = C_out |
| 36 | + self.k_s = K_s |
| 37 | + self.stride = Stride |
| 38 | + self.pad_size = int((K_s -1)/2) |
| 39 | + self.K = np.zeros((C_out, C_in, K_s, K_s)) |
| 40 | + self.bias = np.zeros(C_out) |
| 41 | + |
| 42 | + def forward(self, X): |
| 43 | + out = conv_forward(X, self.K, self.bias, stride=self.stride, padding=self.pad_size) |
| 44 | + return out |
| 45 | + |
| 46 | + |
| 47 | + def load_from_torch(self, weights, bias): |
| 48 | + self.K = weights |
| 49 | + self.bias = bias |
| 50 | + |
| 51 | + |
| 52 | +def conv_forward(X, W, b, stride=1, padding=1): |
| 53 | + cache = W, b, stride, padding |
| 54 | + n_filters, d_filter, h_filter, w_filter = W.shape |
| 55 | + n_x, d_x, h_x, w_x = X.shape |
| 56 | + h_out = (h_x - h_filter + 2 * padding) / stride + 1 |
| 57 | + w_out = (w_x - w_filter + 2 * padding) / stride + 1 |
| 58 | + |
| 59 | + if not h_out.is_integer() or not w_out.is_integer(): |
| 60 | + raise Exception('Invalid output dimension!') |
| 61 | + |
| 62 | + h_out, w_out = int(h_out), int(w_out) |
| 63 | + |
| 64 | + X_col = im2col_indices(X, h_filter, w_filter, padding=padding, stride=stride) |
| 65 | + W_col = W.reshape(n_filters, -1) |
| 66 | + |
| 67 | + out = W_col @ X_col |
| 68 | + out = (out.T + b[:]).T |
| 69 | + out = out.reshape(n_filters, h_out, w_out, n_x) |
| 70 | + out = out.transpose(3, 0, 1, 2) |
| 71 | + |
| 72 | + cache = (X, W, b, stride, padding, X_col) |
| 73 | + |
| 74 | + return out# , cache |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + # data = np.arange(16).reshape((1, 1, 4, 4)) |
| 81 | + data = np.random.randn(2, 3, 200, 200) |
| 82 | + |
| 83 | + |
| 84 | + import torch.nn as nn |
| 85 | + import torch |
| 86 | + from time import time |
| 87 | + |
| 88 | + conv_torch = nn.Conv2d(3, 5, 3, 1, 1) |
| 89 | + conv = Conv2d(3, 5, 3, 1) |
| 90 | + # conv_torch.weight = nn.Parameter(torch.FloatTensor(np.arange(9).reshape(1, 1, 3, 3))) |
| 91 | + # conv_torch.bias = nn.Parameter(torch.FloatTensor(np.zeros(1))) |
| 92 | + w = conv_torch.weight.detach().numpy() |
| 93 | + b = conv_torch.bias.detach().numpy() |
| 94 | + |
| 95 | + start = time() |
| 96 | + out_torch = conv_torch(torch.FloatTensor(data)).detach().numpy() |
| 97 | + print('torch time:', time() - start) |
| 98 | + |
| 99 | + conv.load_from_torch(w, b) |
| 100 | + start = time() |
| 101 | + out = conv(data) |
| 102 | + print('acw time:', time() - start) |
| 103 | + |
| 104 | + print('torch:', out_torch.shape) |
| 105 | + print('acw:', out.shape) |
| 106 | + print(np.sum(out-out_torch)) |
| 107 | + |
| 108 | + |
| 109 | + data = np.random.randn(2, 10) |
| 110 | + |
| 111 | + dense_torch = nn.Linear(10, 20) |
| 112 | + w = dense_torch.weight.detach().numpy() |
| 113 | + b = dense_torch.bias.detach().numpy() |
| 114 | + |
| 115 | + dense = Dense(10, 20) |
| 116 | + |
| 117 | + start = time() |
| 118 | + out_torch = dense_torch(torch.FloatTensor(data)).detach().numpy() |
| 119 | + print('torch time:', time() - start) |
| 120 | + |
| 121 | + dense.load_from_torch(w, b) |
| 122 | + start = time() |
| 123 | + out = dense(data) |
| 124 | + print('acw time:', time() - start) |
| 125 | + print(np.sum(out-out_torch)) |
0 commit comments