def __init__(self, X, labels): data, label = L.Data([X, labels], "data", batch_size=100)() conv1 = L.Conv(data, "conv1", dim_out=20, kernel=5) pool1 = L.Pool(conv1, "pool1", pool=L.Pool.MAX, kernel=2, stride=2) conv2 = L.Conv(pool1, "conv2", dim_out=50, kernel=5) pool2 = L.Pool(conv2, "pool2", pool=L.Pool.MAX, kernel=2, stride=2) fc3 = L.FC(pool2, "fc3", dim_out=500) relu3 = L.ReLU(fc3, "relu3") pred = L.FC(relu3, "pred", dim_out=10) loss = L.SoftmaxWithLoss(pred, "loss", label=label) # Net Instance self.net = mobula.Net() # Set Loss Layer self.net.set_loss(loss)
#Y = np.eye(10)[labels.ravel()] print ("Read OK", n) Xmean = np.mean(X, 0) np.save("xmean.npy", Xmean) # Subtract mean and normalize X = (X - Xmean) / 255.0 # transfer the shape of X to NCHW # N, C, H, W = n, 1, 28, 28 X.resize((n, 1, 28, 28)) # LeNet-5 data, label = L.Data([X, labels], "data", batch_size = 100) conv1 = L.Conv(data, "conv1", dim_out = 20, kernel = 5) pool1 = L.Pool(conv1, "pool1", pool = L.Pool.MAX, kernel = 2, stride = 2) relu1 = L.ReLU(pool1, "relu1") conv2 = L.Conv(relu1, "conv2", dim_out = 50, kernel = 5) pool2 = L.Pool(conv2, "pool2", pool = L.Pool.MAX, kernel = 2, stride = 2) relu2 = L.ReLU(pool2, "relu2") fc3 = L.FC(relu2, "fc3", dim_out = 500) relu3 = L.ReLU(fc3, "relu3") pred = L.FC(relu3, "pred", dim_out = 10) loss = L.SoftmaxWithLoss(pred, "loss", label = label) # Net Instance net = mobula.Net() # Set Loss Layer net.set_loss(loss)
target_size = (42, 42) im = imread("./mobula.png") # Reshape im = imresize(im, target_size) # TO GRAY im = im[:, :, 0] * 0.299 + im[:, :, 1] * 0.587 + im[:, :, 2] * 0.114 h, w = im.shape t = 1 Y = im.reshape((1, h, w, t)).transpose((0, 3, 1, 2)) X = np.random.random((1, t, h, w)) - 0.5 data, label = L.Data([X, Y]) conv = L.Conv(data, dim_out=42, kernel=3, pad=1) relu = L.ReLU(conv) convt = L.ConvT(relu, dim_out=t, kernel=3, pad=1) relu2 = L.ReLU(convt) loss = L.MSE(relu2, label=label) # Net Instance net = mobula.Net() # Set Loss Layer net.set_loss(loss) # Set Solver net.set_solver(S.Momentum()) # Learning Rate net.lr = 2e-6
def go_conv(stride, pad): X = np.random.random((2, 4, 10, 10)) * 100 N, C, H, W = X.shape K = 3 D = 2 FW = np.random.random((D, C, K * K)) * 10 F = FW.reshape((D, C, K, K)) data = L.Data(X) conv = L.Conv(data, kernel=K, pad=pad, stride=stride, dim_out=D) pad_h = pad_w = pad kernel_h = kernel_w = K XH = H XW = W NH = (XH + pad_h * 2 - kernel_h) // stride + 1 NW = (XW + pad_w * 2 - kernel_w) // stride + 1 data.reshape() conv.reshape() conv.W = FW conv.b = np.random.random(conv.b.shape) * 10 ty = np.zeros((N, D, NH, NW)) for i in range(N): x = X[i] x = np.pad(x, ((0, 0), (pad_h, pad_h), (pad_w, pad_w)), "constant") for d in range(D): f = F[d] # 4, 3, 3 for h in range(NH): for w in range(NW): th = h * stride tw = w * stride a = x[:, th:th + K, tw:tw + K] e = np.sum(a * f) ty[i, d, h, w] = e ty[i] += conv.b.reshape((D, 1, 1)) conv.forward() print(np.max(np.abs(conv.Y - ty)), conv.Y.shape) assert np.allclose(conv.Y, ty) conv.dY = np.random.random(conv.Y.shape) * 50 # test backward, dX, dW, db conv.backward() db = np.sum(conv.dY, (0, 2, 3)).reshape(conv.b.shape) dF = np.zeros((D, C, K, K)) dX = np.zeros(X.shape) # sample for i in range(N): x = X[i] x = np.pad(x, ((0, 0), (pad_h, pad_h), (pad_w, pad_w)), "constant") # output for d in range(D): # channels for c in range(C): for h in range(NH): for w in range(NW): # ty[i,d,h,w] for fh in range(K): for fw in range(K): # ty[i,d,h,w] += sum ~ F[d, c, fh, fw] * X[i, c, h*s + fh, w*s + fw] ph = h * stride + fh pw = w * stride + fw dF[d, c, fh, fw] += conv.dY[i, d, h, w] * x[c, ph, pw] oh = ph - pad_h ow = pw - pad_w if oh >= 0 and ow >= 0 and oh < H and ow < W: dX[i, c, oh, ow] += conv.dY[i, d, h, w] * F[d, c, fh, fw] assert np.allclose(conv.db, db.reshape(conv.db.shape)) assert np.allclose(conv.dW, dF.reshape(conv.dW.shape)) assert np.allclose(conv.dX, dX)