def ResNext_bottleneck_B(split, bottom, dim_in, features, stride): dim_out = features * 4 dim_branch_out = int(math.floor(features * (opt.baseWidth / 64))) branch_container = list() for i in range(opt.cardinalty): # torch#75: s:add(Convolution(nInputPlane,d,1,1,1,1,0,0)) scale1, relu1 = conv_BN_scale_relu(split, bottom, dim_branch_out, 1, 1, 0) # torch#76: s:add(Convolution(d,d,3,3,stride,stride,1,1)) # NOTE: I don't think stride is needed, as we need to keep dim the same scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, stride, 1) branch_container.append(relu2) comb = L.Concat(*branch_container, in_place=True) # torch#96: s:add(Convolution(D*C,n*4,1,1,1,1,0,0)) scale_comb, relu_comb = conv_BN_scale_relu(split, comb, dim_out, 1, 1, 0) if dim_in == dim_out: scale0 = bottom relu0 = L.ReLU(scale0, in_place=True) else: scale0, relu0 = conv_BN_scale_relu(split, bottom, dim_out, 1, stride, 0) return L.Eltwise(relu_comb, relu0, operation=P.Eltwise.SUM)
def decompose(split, bottom, dim_in, dim_branch_out, cardinality): branch_container = list() for i in range(cardinality): # torch#75: s:add(Convolution(nInputPlane,d,1,1,1,1,0,0)) scale1, relu1 = conv_BN_scale_relu(split, bottom, dim_branch_out, 1, 1, 0); # torch#76: s:add(Convolution(d,d,3,3,stride,stride,1,1)) # NOTE: I don't think stride is needed, as we need to keep dim the same scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) branch_container.append(relu2) comb = L.Concat(*branch_container, in_place=True) return comb
def expand_dim_n_bottleneck_B(split, bottom, dim_in, features, stride): dim_branch_out = int(math.floor(features * (opt.baseWidth / 64))) dim_out = dim_in * 4 branch_container = list() for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, bottom, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, stride, 1) branch_container.append(relu2) comb = L.Concat(*branch_container, in_place=True) scale_comb, relu_comb = conv_BN_scale_relu(split, comb, dim_out, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, bottom, dim_out, 1, stride, 0) return L.Eltwise(relu_comb, relu_i, operation=P.Eltwise.SUM)
def ResNext(split, n): if split == "train": data, labels = L.Data(source=TRAIN_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28, mirror=True)) elif split == "test": data, labels = L.Data(source=TEST_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28)) scale, result = conv_BN_scale_relu( split, data, nout=64, ks=3, stride=1, pad=1) #NOT CHANGE dim_out 64, 64x{28x28} features = 64 t = expand_dim_n_bottleneck_B(split, result, features, features, 1) #64->256, 256x{28x28} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #256->256, 256x{28x28} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #256->256, 256x{28x28} features = 128 t = expand_dim_n_bottleneck_B(split, t, features * 2, features, 2) #256->512, 512x{14x14} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #512->512, 512x{14x14} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #512->512, 512x{14x14} features = 256 t = expand_dim_n_bottleneck_B(split, t, features * 2, features, 2) #512->1024, 1024x{7x7} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #1024->1024, 1024x{7x7} t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #1024->1024, 1024x{7x7} #pool = L.Pooling(result, pool = P.Pooling.AVE, global_pooling = True, kernel_size = 8, stride = 1) pool = L.Pooling(t, pool=P.Pooling.AVE, global_pooling=True) IP = L.InnerProduct(pool, num_output=10, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant')) acc = L.Accuracy(IP, labels) loss = L.SoftmaxWithLoss(IP, labels) return to_proto(acc, loss)
def ResNext(split, n): DATAPATH_PREFIX = "../CIFAR10/" TRAIN_FILE = DATAPATH_PREFIX + "cifar10_train_lmdb" TEST_FILE = DATAPATH_PREFIX + "cifar10_test_lmdb" MEAN_FILE = DATAPATH_PREFIX + "mean.binaryproto" if split == "train": data, labels = L.Data(source=TRAIN_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28, mirror=True)) elif split == "test": data, labels = L.Data(source=TEST_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28)) scale, result = conv_BN_scale_relu(split, data, nout=64, ks=3, stride=1, pad=1) # Conv2 t = n_bottleneck_layer_naive(split, result, ResNext_bottleneck_B, 64, n, 1) # Attention all three stride is 1 # Conv3 t = n_bottleneck_layer_naive( split, t, ResNext_bottleneck_B, 128, n, 2) # Attention 1st stride is 2, the 2nd and 3rd is 1 # Conv4 r = n_bottleneck_layer_naive( split, t, ResNext_bottleneck_B, 256, n, 2) # Attention 1st stride is 2, the 2nd and 3rd is 1 #pool = L.Pooling(result, pool = P.Pooling.AVE, global_pooling = True, kernel_size = 8, stride = 1) pool = L.Pooling(r, pool=P.Pooling.AVE, global_pooling=True) IP = L.InnerProduct(pool, num_output=int(opt.dataset[5:]), weight_filler=dict(type='xavier'), bias_filler=dict(type='constant')) acc = L.Accuracy(IP, labels) loss = L.SoftmaxWithLoss(IP, labels) return to_proto(acc, loss)
def ResNext(split, n): DATAPATH_PREFIX = "../CIFAR10/" TRAIN_FILE = DATAPATH_PREFIX + "cifar10_train_lmdb" TEST_FILE = DATAPATH_PREFIX + "cifar10_test_lmdb" MEAN_FILE = DATAPATH_PREFIX + "mean.binaryproto" if split == "train": data, labels = L.Data(source=TRAIN_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28, mirror=True)) elif split == "test": data, labels = L.Data(source=TEST_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28)) scale, result = conv_BN_scale_relu( split, data, nout=64, ks=3, stride=1, pad=1) #NOT CHANGE dim_out 64, 64x{28x28} features = 64 t = expand_dim_n_bottleneck_B(split, result, features, features, 1) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) features = 128 t = expand_dim_n_bottleneck_B(split, t, features * 2, features, 2) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) features = 256 t = expand_dim_n_bottleneck_B(split, t, features * 2, features, 2) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) t = same_dim_n_bottlneck_B(split, t, features * 4, features, 1) #pool = L.Pooling(result, pool = P.Pooling.AVE, global_pooling = True, kernel_size = 8, stride = 1) pool = L.Pooling(t, pool=P.Pooling.AVE, global_pooling=True) IP = L.InnerProduct(pool, num_output=10, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant')) acc = L.Accuracy(IP, labels) loss = L.SoftmaxWithLoss(IP, labels) return to_proto(acc, loss)
def ResNext(split, n): if split == "train": data, labels = L.Data(source=TRAIN_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28, mirror=True)) elif split == "test": data, labels = L.Data(source=TEST_FILE, backend=P.Data.LMDB, batch_size=opt.batch_size, ntop=2, transform_param=dict(mean_file=MEAN_FILE, crop_size=28)) scale, result = conv_BN_scale_relu( split, data, nout=64, ks=3, stride=1, pad=1) #NOT CHANGE dim_out 64, 64x{28x28} # Block 1 # The first bottleneck bc = [] features = 64 dim_branch_out = int(math.floor(features * (opt.baseWidth / 64.0))) print("features: ", features, "dim_branch_out: ", dim_branch_out) for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, result, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 256, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, result, 256, 1, 1, 0) rf1 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The second bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf1, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 256, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf1, 256, 1, 1, 0) rf2 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The third bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf2, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 256, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf2, 256, 1, 1, 0) rf3 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 4th bttleneck bc = [] features = 128 dim_branch_out = int(math.floor(features * (opt.baseWidth / 64.0))) print("features: ", features, "dim_branch_out: ", dim_branch_out) for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf3, dim_branch_out, 1, 2, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 512, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf3, 512, 1, 2, 0) rf4 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 5th bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf4, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 512, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf4, 512, 1, 1, 0) rf5 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 6th bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf5, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 512, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf5, 512, 1, 1, 0) rf6 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 7th bttleneck bc = [] features = 256 dim_branch_out = int(math.floor(features * (opt.baseWidth / 64.0))) print("features: ", features, "dim_branch_out: ", dim_branch_out) for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf6, dim_branch_out, 1, 2, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 1024, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf6, 1024, 1, 2, 0) rf7 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 8th bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf7, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 1024, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf7, 1024, 1, 1, 0) rf8 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) # The 9th bottleneck bc = [] for i in range(opt.cardinalty): scale1, relu1 = conv_BN_scale_relu(split, rf8, dim_branch_out, 1, 1, 0) scale2, relu2 = conv_BN_scale_relu(split, relu1, dim_branch_out, 3, 1, 1) bc.append(relu2) comb = L.Concat(*bc) sf, rf = conv_BN_scale_relu(split, comb, 1024, 1, 1, 0) scale_i, relu_i = conv_BN_scale_relu(split, rf8, 1024, 1, 1, 0) rf9 = L.Eltwise(rf, relu_i, operation=P.Eltwise.SUM) #pool = L.Pooling(result, pool = P.Pooling.AVE, global_pooling = True, kernel_size = 8, stride = 1) pool = L.Pooling(rf9, pool=P.Pooling.AVE, global_pooling=True) IP = L.InnerProduct(pool, num_output=10, weight_filler=dict(type='xavier'), bias_filler=dict(type='constant')) acc = L.Accuracy(IP, labels) loss = L.SoftmaxWithLoss(IP, labels) return to_proto(acc, loss)