mit-han-lab / once-for-all

[ICLR 2020] Once for All: Train One Network and Specialize it for Efficient Deployment
https://ofa.mit.edu/
MIT License
1.89k stars 333 forks source link

When I use ofa_resnet50 to Efficient Deployment in tutorial/ofa.ipynb, I met some errors. #40

Closed darrenzhang1007 closed 3 years ago

darrenzhang1007 commented 3 years ago
  1. first, I searched a network

    Searching with note10 constraint (25): 100%|██████████| 500/500 [00:09<00:00, 51.03it/s]Found best architecture on note10 with latency <= 25.00 ms in 9.84 seconds! It achieves 81.71% predicted accuracy with 24.73 ms latency on note10. Architecture of the searched sub-net: DyConv(O32, K3, S2) (DyConv(O32, K3, S1), Identity) DyConv(O64, K3, S1) max_pooling(ks=3, stride=2) (3x3_BottleneckConv_in->768->256_S1, avgpool_conv) (3x3_BottleneckConv_in->768->256_S1, Identity) (3x3_BottleneckConv_in->1536->256_S1, Identity) (3x3_BottleneckConv_in->768->256_S1, Identity) (3x3_BottleneckConv_in->2048->512_S2, avgpool_conv) (3x3_BottleneckConv_in->2048->512_S1, Identity) (3x3_BottleneckConv_in->3072->512_S1, Identity) (3x3_BottleneckConv_in->2048->512_S1, Identity) (3x3_BottleneckConv_in->6144->1024_S2, avgpool_conv) (3x3_BottleneckConv_in->3072->1024_S1, Identity) (3x3_BottleneckConv_in->4096->1024_S1, Identity) (3x3_BottleneckConv_in->6144->1024_S1, Identity) (3x3_BottleneckConv_in->4096->1024_S1, Identity) (3x3_BottleneckConv_in->4096->1024_S1, Identity) (3x3_BottleneckConv_in->8192->2048_S2, avgpool_conv) (3x3_BottleneckConv_in->6144->2048_S1, Identity) (3x3_BottleneckConv_in->12288->2048_S1, Identity) (3x3_BottleneckConv_in->12288->2048_S1, Identity) MyGlobalAvgPool2d(keep_dim=False) DyLinear(2048, 1000)

But, I think The middle dimension of the network searched is a bit untrustworthy

  1. When I wanted to evaluate this sub-model, I met this error

    Evaluating the sub-network with latency = 24.7 ms on note10 RuntimeError Traceback (most recent call last)

    in 6 _, net_config, latency = result 7 print('Evaluating the sub-network with latency = %.1f ms on %s' % (latency, target_hardware)) ----> 8 top1 = evaluate_ofa_subnet( 9 ofa_network, 10 imagenet_data_path, ~/桌面/once-for-all-master/ofa/tutorial/imagenet_eval_helper.py in evaluate_ofa_subnet(ofa_net, path, net_config, data_loader, batch_size, device) 18 assert len(net_config['ks']) == 20 and len(net_config['e']) == 20 and len(net_config['d']) == 5 19 ofa_net.set_active_subnet(ks=net_config['ks'], d=net_config['d'], e=net_config['e']) ---> 20 subnet = ofa_net.get_active_subnet().to(device) 21 calib_bn(subnet, path, net_config['r'][0], batch_size) 22 top1 = validate(subnet, path, net_config['r'][0], data_loader, batch_size, device) ~/桌面/once-for-all-master/ofa/imagenet_classification/elastic_nn/networks/ofa_resnets.py in get_active_subnet(self, preserve_weight) 226 active_idx = block_idx[:len(block_idx) - depth_param] 227 for idx in active_idx: --> 228 blocks.append(self.blocks[idx].get_active_subnet(input_channel, preserve_weight)) 229 input_channel = self.blocks[idx].active_out_channel 230 classifier = self.classifier.get_active_subnet(input_channel, preserve_weight) ~/桌面/once-for-all-master/ofa/imagenet_classification/elastic_nn/modules/dynamic_layers.py in get_active_subnet(self, in_channel, preserve_weight) 540 541 # copy weight from current layer --> 542 sub_layer.conv1.conv.weight.data.copy_( 543 self.conv1.conv.get_active_filter(self.active_middle_channels, in_channel).data) 544 copy_bn(sub_layer.conv1.bn, self.conv1.bn.bn)

RuntimeError: The size of tensor a (768) must match the size of tensor b (88) at non-singleton dimension 0

I guess that Do I need to modify the code for resnet50 network. Please tell me how to modify . Thanks a lot

han-cai commented 3 years ago

An example of how to use OFA-ResNet50 is provided at https://github.com/mit-han-lab/once-for-all/blob/master/tutorial/ofa_resnet50_example.ipynb