def test_qparams_init(): # net = vgg16(pretrained=True) net = mobilenet1_0(pretrained=True) convert_model(net) qparams_init(net) input = nd.random.uniform(shape=(1, 3, 224, 224)) print(net(input))
def test_collect_qparams(): net = vgg16(pretrained=True) convert_model(net) qparams_init(net) print("collect_qparams: ", collect_qparams(net)) print("") print_all_qparams(net)
def test_quantized_mobilnet(): x = nd.uniform(shape=(1, 3, 224, 224)) from models.quantized_mobilenet import mobilenet1_0 as my_mobilenet my_net = my_mobilenet(pretrained=True) my_res = my_net(x) from gluoncv.model_zoo import mobilenet1_0 as ref_mobilenet ref_net = ref_mobilenet(pretrained=True) ref_res = ref_net(x) sim_net = ref_mobilenet(pretrained=True) convert_model(sim_net, exclude=[sim_net.features[0]]) qparams_init(sim_net) sim_res = sim_net(x) # print(">> Difference between my_quantized_mobilenet and simulated_mobilenet -- ") # print(">> ", ((my_res - sim_res).abs() / sim_res.abs()).reshape(-1).sort()[-100:]) return (my_res.argsort(is_ascend=False)[0, :20], ref_res.argsort(is_ascend=False)[0, :20], sim_res.argsort(is_ascend=False)[0, :20])
exclude_blocks = [] if opt.exclude_first_conv == 'true': exclude_blocks.extend([net.features[0], net.features[1]]) if model_name.startswith('mobilenetv2_'): exclude_blocks.append(net.output[0]) if model_name.startswith('cifar_resnet'): exclude_blocks.extend( [net.features[2][0].body[0], net.features[2][0].body[1]]) print('*' * 25 + ' Exclude blocks ' + '*' * 25) for b in exclude_blocks: print(b.name) print('*' * (25 * 2 + len(' Exclude blocks '))) print() convert.convert_model( net, exclude=exclude_blocks, convert_fn=convert_fn, ) # initialize for quantization parameters and reset context qparams_init(net) ctx = gpu(opt.use_gpu) if opt.use_gpu != -1 else cpu() net.collect_params().reset_ctx(ctx) # construct transformer if opt.dataset == 'imagenet': eval_transformer = T.Compose([ T.Resize(256, keep_ratio=True), T.CenterCrop(224), T.ToTensor(), T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
def test_merge_bn(): net = mobilenet1_0(pretrained=True) convert_model(net) qinit(net) _merge_bn(net)