def test_Conv2D(use_bias, groups): x = nd.uniform(shape=(2, 2, 5, 5)) my_conv = MyConv(10, 3, 1, 1, in_channels=2, groups=groups, use_bias=use_bias) my_conv.initialize() ref_conv = Conv2D(10, 3, 1, 1, in_channels=2, groups=groups, use_bias=use_bias, bias_initializer=init.Constant(my_conv.bias.data()) if use_bias else 'zero', weight_initializer=init.Constant(my_conv.weight.data())) ref_conv.initialize() return (my_conv(x) - ref_conv(x)).abs().sum().asscalar()
def __init__(self, center, radius=0, decision=None, left=None, right=None): super(Node, self).__init__() center = nd.array(center) radius = nd.array(radius) with self.name_scope(): self.center = self.params.get('center', shape=center.shape[-1], init=init.Constant(center), grad_req='null') self.radius = self.params.get('radius', shape=1, init=init.Constant(radius), grad_req='null') self.collect_params().initialize() self.child = {'decision': decision, 'left': left, 'right': right}
def get_inits(X, ctx, lr, styles_Y): gen_img = GeneratedImage(X.shape) gen_img.initialize(init.Constant(X), ctx=ctx, force_reinit=True) trainer = gluon.Trainer(gen_img.collect_params(), 'adam', {'learning_rate': lr}) styles_Y_gram = [gram(Y) for Y in styles_Y] return gen_img(), styles_Y_gram, trainer
def get_net(model_name, context): if model_name == 'FCN': num_classes = 21 pretrained_net = vision.resnet18_v2(pretrained=True, ctx=context) net = gluon.nn.HybridSequential() for layer in pretrained_net.features[:-2]: net.add(layer) net.add( gluon.nn.Conv2D(num_classes, kernel_size=1), gluon.nn.Conv2DTranspose(num_classes, kernel_size=64, padding=16, strides=32)) net[-1].initialize(init.Constant( bilinear_kernel(num_classes, num_classes, 64)), ctx=context) net[-2].initialize(init=init.Xavier(), ctx=context) input_shape = (1, 3, 320, 480) label_shape = (1, 320, 480) loss_axis = 1 else: net = vision.get_model(model_name, classes=10) net.initialize(mx.init.Xavier(), ctx=context) input_shape = (1, 1, 224, 224) label_shape = 1 loss_axis = -1 return net, input_shape, label_shape, loss_axis
def get_inits(self, x, styles_y): gen_img = GeneratedImage(x.shape) gen_img.initialize(init.Constant(x), ctx=self.mx_ctx, force_reinit=True) trainer = gluon.Trainer(gen_img.collect_params(), 'adam', {'learning_rate': self.LR}) styles_y_gram = [self.gram(y) for y in styles_y] return gen_img(), styles_y_gram, trainer
def __init__(self, weight, bias, sharpness, tau, decision, side): super(Split, self).__init__() sharpness = nd.array(sharpness) weight = sharpness * nd.array(weight) bias = sharpness * nd.array(bias) tau = nd.array(tau) with self.name_scope(): self.tau = self.params.get('tau', shape=1, init=init.Constant(tau), grad_req='null') self.split = nn.Dense(units=1, in_units=weight.shape[-1], weight_initializer=init.Constant(weight), bias_initializer=init.Constant(bias)) self.collect_params().initialize() self.parent = {'decision': decision, 'side': side}
def __init__(self, lamda, r_init=1.0, dtype='float32', **kwargs): super().__init__(**kwargs) self._lamda = lamda self.R = self.params.get('R', shape=(1, ), init=init.Constant(r_init), dtype=dtype, allow_deferred_init=True)
def test_quantized_Conv2D(use_bias, groups): x = nd.uniform(shape=(2, 2, 5, 5)) my_conv = MyConv(10, 3, 1, 1, in_channels=2, groups=groups, use_bias=use_bias, input_dtype='uint8', weight_dtype='int8', quantized=True) my_conv.initialize() my_res = my_conv(x) ref_conv = Conv2D(10, 3, 1, 1, in_channels=2, groups=groups, use_bias=use_bias, bias_initializer=init.Constant(my_conv.bias.data()) if use_bias else 'zero', weight_initializer=init.Constant(my_conv.weight.data())) ref_conv.initialize() ref_res = ref_conv(x) sim_conv = ref_conv convert_conv2d = gen_conv2d_converter() convert_conv2d(sim_conv) qparams_init((sim_conv)) sim_res = sim_conv(x) return (((my_res - ref_res).abs() / ref_res.abs()).reshape(-1).sort()[-20:], ((my_res - sim_res).abs() / sim_res.abs()).reshape(-1).sort()[-20:])
def __init__(self, num_classes, embedding_size, alpha, lamda, lr_mult, **kwargs): super().__init__(weight=None, batch_axis=0, **kwargs) self._num_class = num_classes self._feature_dim = embedding_size self._alpha = alpha self._lamda = lamda self.mean = self.params.get('mean', shape=(num_classes, embedding_size), init=init.Xavier()) self.var = self.params.get('var', shape=(num_classes, embedding_size), init=init.Constant(1), lr_mult=lr_mult)
def se_initialize(net, param_files, ctx=cpu()): finished_params = [] """ Load parameters """ origin_params = [] for fname in param_files: origin_params.append(nd.load(fname)) """ Initialize parameters with snapshot-ensembled models """ for pname in origin_params[0].keys(): origins = nd.stack(*[pdict[pname] for pdict in origin_params]) dest = _get_param_by_name(net, pname) if len(dest.shape) == len(origins.shape): # print("Stack params:", dest.name) dest.initialize(init.Constant(origins), ctx=ctx) elif len(dest.shape) == len(origins.shape) - 1: # print("Reduced params:", dest.name) dest.initialize(init.Constant(origins.mean(axis=0)), ctx=ctx) else: raise ValueError() finished_params.append(dest.name) """ Initialize other parameters """ for p in net.collect_params().values(): if p.name not in finished_params: # print("Random params:", p.name) p.initialize()
def get_inits(X, device, lr): gen_img = GeneratedImage(X.shape) gen_img.initialize(init.Constant(X), ctx=device, force_reinit=True) trainer = gluon.Trainer(gen_img.collect_params(), 'adam', {'learning_rate': lr}) return gen_img(), trainer
def __init__(self, F, img_channel): super(LocalizationNetwork, self).__init__() self.F = F self.img_channel = img_channel self.conv = nn.HybridSequential() with self.conv.name_scope(): self.conv.add( nn.Conv2D(channels=64, kernel_size=3, stride=1, padding=1, use_bias=False, activation="relu"), nn.BatchNorm(), nn.MaxPool2D( pool_size=2, strides=2), # batch_size x 64 x I_height/2 x I_width/2 nn.Conv2D(channels=128, kernel_size=3, strides=1, padding=1, use_bias=False, activation="relu"), nn.BatchNorm(), nn.MaxPool2D( pool_size=2, strides=2), # batch_size x 128 x I_height/4 x I_width/4 nn.Conv2D(channels=256, kernel_size=3, strides=1, padding=1, use_bias=False, activation="relu"), nn.BatchNorm(), nn.MaxPool2D( pool_size=2, strides=2), # batch_size x 256 x I_height/8 x I_width/8 nn.Conv2D(channels=512, kernel_size=3, strides=1, padding=1, use_bias=False, activation="relu"), nn.BatchNorm(), nn.GlobalAvgPool2D(1) # batch_size x 512 ) self.localization_fc1 = nn.HybridSequential() with self.localization_fc1.name_scope(): self.localization_fc1.add(nn.Dense(256), nn.Activation('relu')) self.localization_fc2 = nn.Dense(self.F * 2) # Init fc2 in LocalizationNetwork self.localization_fc2.weight.initialize(init=init.Constant(0)) """ see RARE paper Fig. 6 (a) """ # ctrl_pts_x = np.linspace(-1.0, 1.0, int(F / 2)) # ctrl_pts_y_top = np.linspace(0.0, -1.0, num=int(F / 2)) # ctrl_pts_y_bottom = np.linspace(1.0, 0.0, num=int(F / 2)) # ctrl_pts_top = np.stack([ctrl_pts_x, ctrl_pts_y_top], axis=1) # ctrl_pts_bottom = np.stack([ctrl_pts_x, ctrl_pts_y_bottom], axis=1) # initial_bias = np.concatenate([ctrl_pts_top, ctrl_pts_bottom], axis=0) # self.localization_fc2.bias.data = torch.from_numpy(initial_bias).float().view(-1) self.localization_fc2.bias.initialize(init=MyInit(F))
net[0].bias.data(), net[0].weight.grad(), net[0].bias.grad(), ) # display all parameters collected with nn.Sequential.add() print(net.collect_params()) # display print(net.collect_params('.*weight')) print(net.collect_params('.*bias')) net.initialize(init=init.Normal(sigma=0.01), force_reinit=True) print(net[0].weight.data()) net.initialize(init=init.Constant(1), force_reinit=True) print(net[0].weight.data()) class MyInit(init.Initializer): def _init_weight(self, name, arr): print('Init', name, arr.shape) arr[:] = nd.random.uniform(low=-10, high=10, shape=arr.shape) arr *= arr.abs() > 5 net.initialize(init=MyInit(), force_reinit=True) print(net[0].weight.data()) net[0].weight.set_data(net[0].weight.data() + 1) print(net[0].weight.data())
from mxnet import gluon, image, init, nd from mxnet.gluon import data as gdata, loss as gloss, model_zoo, nn import numpy as np import sys from utils import * X = nd.arange(1, 17).reshape((1, 1, 4, 4)) K = nd.arange(1, 10).reshape((1, 1, 3, 3)) conv = nn.Conv2D(channels=1, kernel_size=3) conv.initialize(init.Constant(K)) print(conv(X)) print(K) # 下面我们将卷积核K改写成含有大量零元素的稀疏矩阵W,即权重矩阵。 # 权重矩阵的形状为(4, 16),其中的非零元素来自卷积核K中的元素。将输 # # 入X逐行连结,得到长度为16的向量。然后将W与向量化的X做矩阵乘法,得 # # 到长度为4的向量。对其变形后,我们可以得到和上面卷积运算相同的结果。 W, k = nd.zeros((4, 16)), nd.zeros(11) k[:3], k[4:7], k[8:] = K[0, 0, 0, :], K[0, 0, 1, :], K[0, 0, 2, :] W[0, 0:11], W[1, 1:12], W[2, 4:15], W[3, 5:16] = k, k, k, k nd.dot(W, X.reshape(16)).reshape((1, 1, 2, 2)), W print(nd.dot(W, X.reshape(16)).reshape((1, 1, 2, 2))) print(W) # 卷积的反向传播函数的实现可以看作将函数输入乘以转置后的权重矩阵, 而转置卷积 # 层正好交换了卷积层的前向计算函数与反向传播函数:转置卷积层的这两个函数可以 # 看作将函数输入向量分别乘以权重的转置和权重。
def build_model(self): self.model.initialize(init=init.Constant(0), force_reinit=True) for i, layer in enumerate(self.model): layer.weight.data()[:] = self.parameter[i * 2] layer.bias.data()[:] = self.parameter[i * 2 + 1]
#9.10-全卷机网络FCN #%matplotlib inline import d2lzh as d2l from mxnet import gluon, image, init, nd from mxnet.gluon import data as gdata, loss as gloss, model_zoo, nn import numpy as np import sys #9.10.1-转置卷积层 X = nd.arange(1, 17).reshape((1, 1, 4, 4)) K = nd.arange(1, 10).reshape((1, 1, 3, 3)) conv = nn.Conv2D(channels=1, kernel_size=3) conv.initialize(init.Constant(K)) conv(X), K #将卷积核改写成稀疏权重矩阵W W, k = nd.zeros((4, 16)), nd.zeros(11) k[:3], k[4:7], k[8:] = K[0, 0, 0, :], K[0, 0, 1, :], K[0, 0, 2, :] W[0, 0:11], W[1, 1:12], W[3, 5:16] = k, k, k nd.dot(W, X.reshape(16)).reshape((1, 1, 2, 2)), W conv = nn.Conv2D(10, kernel_size=4, padding=1, strides=2) conv.initialize() X = nd.random.uniform(shape=(1, 3, 64, 64)) Y = conv(X) Y.shape conv_trans = nn.Conv2DTranspose(3, kernel_size=4, padding=1, strides=2) conv_trans.initialize() conv_trans(Y).shape
batch_size, last_batch="discard", num_workers=num_workers) pretrained_net = model_zoo.vision.resnet18_v2(pretrained=True, root="../models") net = nn.HybridSequential() for layer in pretrained_net.features[:-2]: net.add(layer) net.add( nn.Conv2D(num_classes, kernel_size=1), nn.Conv2DTranspose(num_classes, kernel_size=64, padding=16, strides=32)) net[-2].initialize(init.Xavier()) net[-1].initialize( init.Constant(bilinear_kernel(num_classes, num_classes, 64))) loss = gloss.SoftmaxCrossEntropyLoss(axis=1) net.collect_params().reset_ctx(ctx) trainer = gluon.Trainer(net.collect_params(), "sgd", { "learning_rate": lr, "wd": wd }) gb.train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs) net.save_parameters("../models/fcn.params") # test_images, test_labels = read_voc_images("../data/VOC2012", is_train=False) # n = 4 # imgs = [] # for i in range(n):
def bilinear_kernel(in_channels, out_channels, kernel_size): factor = (kernel_size + 1) // 2 if kernel_size % 2 == 1: center = factor - 1 else: center = factor - 0.5 og = np.ogrid[:kernel_size, :kernel_size] filt = (1 - abs(og[0] - center) / factor) * \ (1 - abs(og[1] - center) / factor) weight = np.zeros((in_channels, out_channels, kernel_size, kernel_size), dtype='float32') weight[range(in_channels), range(out_channels), :, :] = filt return nd.array(weight) net[-1].initialize(init.Constant(bilinear_kernel(num_classes, num_classes, 64))) #net[-1].initialize(init=init.Xavier()) net[-2].initialize(init=init.Xavier()) # Read train and test data crop_size, batch_size, colormap2label = (320, 480), 4, nd.zeros(256**3) for i, cm in enumerate(d2l.VOC_COLORMAP): colormap2label[(cm[0] * 256 + cm[1]) * 256 + cm[2]] = i #voc_dir = d2l.download_voc_pascal(data_dir='C:/Users/mayao/Desktop/Python learning/MXnet/data') data_dir = 'C:/Users/mayao/Desktop/Python learning/MXnet/data' voc_dir = os.path.join(data_dir, 'VOCdevkit/VOC2012') num_workers = 0 if sys.platform.startswith('win32') else 4
pretrained_model = models.resnet18_v2(pretrained=True, ctx=ctx) model = nn.HybridSequential() # build the layer for layer in pretrained_model.features[:-2]: model.add(layer) with model.name_scope(): model.add( nn.Conv2D(args.num_classes, kernel_size=1), nn.Conv2DTranspose(args.num_classes, kernel_size=64, padding=16, strides=32)) model[-2].initialize(init=init.Xavier(), ctx=ctx) model[-1].initialize(init=init.Constant( utils.bilinear_kernel(args.num_classes, args.num_classes, 64)), ctx=ctx) # set the loss function loss = gluon.loss.SoftmaxCrossEntropyLoss(axis=1) model.collect_params().reset_ctx(ctx=ctx) trainer = gluon.Trainer( params=model.collect_params(), optimizer='sgd', optimizer_params={ 'learning_rate': args.init_lr, 'wd': args.weight_decay }, ) print("Start training...")
factor = (kernel_size + 1) // 2 if kernel_size % 2 == 1: center = factor - 1 else: center = factor - 0.5 og = (np.arange(kernel_size).reshape(-1, 1), np.arange(kernel_size).reshape(1, -1)) filt = (1 - np.abs(og[0] - center) / factor) * \ (1 - np.abs(og[1] - center) / factor) weight = np.zeros((in_channels, out_channels, kernel_size, kernel_size)) weight[range(in_channels), range(out_channels), :, :] = filt return np.array(weight) conv_trans = nn.Conv2DTranspose(3, kernel_size=4, padding=1, strides=2) conv_trans.initialize(init.Constant(bilinear_kernel(3, 3, 4))) img = image.imread('catdog.jpg') X = np.expand_dims(img.astype('float32').transpose(2, 0, 1), axis=0) / 255 Y = conv_trans(X) out_img = Y[0].transpose(1, 2, 0) d2l.set_figsize() print('input image shape:', img.shape) d2l.plt.imshow(img.asnumpy()) print('output image shape:', out_img.shape) d2l.plt.imshow(out_img.asnumpy()) W = bilinear_kernel(num_classes, num_classes, 64) net[-1].initialize(init.Constant(W)) net[-2].initialize(init=init.Xavier()) batch_size, crop_size = 32, (320, 480)