def test_active_subnet_with_config_and_save_subnet_result():
    # config 에 따라 top1 acc 를 뽑을 수 있음
    # N_network_try = 100

    # exmple net_config
    net_config = {'wid': None,
                  'ks': [7, 5, 5, 3, 7, 5, 3, 5, 7, 5, 3, 7, 5, 7, 7, 3, 5, 3, 7, 5],
                  'e': [4, 4, 6, 3, 3, 4, 4, 4, 6, 6, 4, 3, 6, 3, 3, 3, 6, 6, 6, 3],
                  'd': [2, 3, 3, 4, 4],
                  'r': [208]}

    # results = []
    arch_manager = ArchManager()

    with torch.no_grad():
        # for frame in range(N_network_try):
        ofa_network = ofa_net('ofa_mbv3_d234_e346_k357_w1.2', pretrained=True)
        torch.cuda.empty_cache()
        net_config['r'] = [random.choice(arch_manager.resolutions)]

        for i in range(len(net_config['e'])):
            net_config['ks'][i] = random.choice(arch_manager.kernel_sizes)
            net_config['e'][i] = random.choice(arch_manager.expand_ratios)

        for i in range(len(net_config['d'])):
            net_config['d'][i] = random.choice(arch_manager.depths)

        ofa_network.set_active_subnet(ks=net_config['ks'], d=net_config['d'], e=net_config['e'])

        imagenet_path = "../once-for-all/tutorial/.imagenet_fake/"
        data_loader = get_data_loader(imagenet_path)
        # print(ofa_network.module_str)

        start = time.time()
        top1 = evaluAate_ofa_subnet(
            ofa_network,
            imagenet_path,
            net_config,
            data_loader,
            batch_size=250,
            device='cuda' if torch.cuda.is_available() else 'cpu')
        # device = f'cuda:{random.choice([0, 1, 2, 3])}' if torch.cuda.is_available() else 'cpu')
        latency = time.time() - start

        # results.append({**net_config, 'acc': top1, 'latency': latency})
        # del ofa_network

        # save
        with open(f'../results/acc_{top1}_latency_{latency}.pickle', 'wb') as f:
            pickle.dump({**net_config, 'acc': top1, 'latency': latency}, f, pickle.HIGHEST_PROTOCOL)
示例#2
0
#os.environ['CUDA_VISIBLE_DEVICES'] = '0'
cuda_available = torch.cuda.is_available()
if cuda_available:
    torch.backends.cudnn.enabled = True
    torch.backends.cudnn.benchmark = True
    torch.cuda.manual_seed(random_seed)
    print('Using GPU.')
else:
    print('Using CPU.')

# %% [markdown]
# Good! Now you have successfully configured the environment! It's time to import the **OFA network** for the following experiments.
# The OFA network used in this tutorial is built upon MobileNetV3 with width multiplier 1.2, supporting elastic depth (2, 3, 4) per stage, elastic expand ratio (3, 4, 6), and elastic kernel size (3, 5 7) per block.

# %%
ofa_network = ofa_net('ofa_mbv3_d234_e346_k357_w1.2', pretrained=True)
print('The OFA Network is ready.')

# %% [markdown]
# Now, let's build the ImageNet dataset and the corresponding dataloader. Notice that **if you're using the CPU,
# we will skip ImageNet evaluation by default** since it will be very slow.
# If you are using the GPU, in case you don't have the full dataset,
# we will download a subset of ImageNet which contains 2,000 images (~250M) for testing.
# If you do have the full ImageNet dataset on your machine, just specify it in `imagenet_data_path` and the downloading script will be skipped.

# %%
if cuda_available:
    # path to the ImageNet dataset
    print("Please input the path to the ImageNet dataset.\n")
    imagenet_data_path = './imagenet_1k'  #input()
示例#3
0
                        'ofa_mbv3_d234_e346_k357_w1.2',
                        'ofa_proxyless_d234_e346_k357_w1.3'
                    ],
                    help='OFA networks')

args = parser.parse_args()
if args.gpu == 'all':
    device_list = range(torch.cuda.device_count())
    args.gpu = ','.join(str(_) for _ in device_list)
else:
    device_list = [int(_) for _ in args.gpu.split(',')]
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
args.batch_size = args.batch_size * max(len(device_list), 1)
ImagenetDataProvider.DEFAULT_PATH = args.path

ofa_network = ofa_net(args.net, pretrained=True)
run_config = ImagenetRunConfig(test_batch_size=args.batch_size,
                               n_worker=args.workers)
""" Randomly sample a sub-network, 
    you can also manually set the sub-network using: 
        ofa_network.set_active_subnet(ks=7, e=6, d=4) 
"""
ofa_network.sample_active_subnet()
subnet = ofa_network.get_active_subnet(preserve_weight=True)
""" Test sampled subnet 
"""
run_manager = RunManager('.tmp/eval_subnet', subnet, run_config, init=False)
# assign image size: 128, 132, ..., 224
run_config.data_provider.assign_active_img_size(224)
run_manager.reset_running_statistics(net=subnet)
示例#4
0
def quantization(title='optimize', model_name='', file_path=''):

    data_dir = args.data_dir
    quant_mode = args.quant_mode
    finetune = args.fast_finetune
    deploy = args.deploy
    batch_size = args.batch_size
    subset_len = args.subset_len
    if quant_mode != 'test' and deploy:
        deploy = False
        print(
            r'Warning: Exporting xmodel needs to be done in quantization test mode, turn off it in this running!'
        )
    if deploy and (batch_size != 1 or subset_len != 1):
        print(
            r'Warning: Exporting xmodel needs batch size to be 1 and only 1 iteration of inference, change them automatically!'
        )
        batch_size = 1
        subset_len = 1

    # model = resnet50().cpu()
    ofa_network = ofa_net('ofa_resnet50', pretrained=True)
    with open(args.model_dir + 'net_config_' + model_name + '.pickle',
              'rb') as handle:
        arch_dict = pickle.load(handle)
    ofa_network.set_active_subnet(**arch_dict)
    model = ofa_network.get_active_subnet()

    model.load_state_dict(torch.load(file_path))

    size = args.image_size
    input = torch.randn([batch_size, 3, size, size])
    if quant_mode == 'float':
        quant_model = model
    else:
        ## new api
        ####################################################################################
        quantizer = torch_quantizer(quant_mode, model, (input), device=device)

        quant_model = quantizer.quant_model
        #####################################################################################

    # to get loss value after evaluation
    loss_fn = torch.nn.CrossEntropyLoss().to(device)

    val_loader, _ = load_data(subset_len=subset_len,
                              train=False,
                              batch_size=batch_size,
                              sample_method='random',
                              data_dir=data_dir,
                              model_name=model_name)

    # fast finetune model or load finetuned parameter before test
    if finetune == True:
        ft_loader, _ = load_data(subset_len=1024,
                                 train=False,
                                 batch_size=batch_size,
                                 sample_method=None,
                                 data_dir=data_dir,
                                 model_name=model_name)
        if quant_mode == 'calib':
            quantizer.fast_finetune(evaluate,
                                    (quant_model, ft_loader, loss_fn))
        elif quant_mode == 'test':
            quantizer.load_ft_param()

    # record  modules float model accuracy
    # add modules float model accuracy here
    acc_org1 = 0.0
    acc_org5 = 0.0
    loss_org = 0.0

    #register_modification_hooks(model_gen, train=False)
    acc1_gen, acc5_gen, loss_gen = evaluate(quant_model, val_loader, loss_fn)

    # logging accuracy
    print('loss: %g' % (loss_gen))
    print('top-1 / top-5 accuracy: %g / %g' % (acc1_gen, acc5_gen))

    # handle quantization result
    if quant_mode == 'calib':
        quantizer.export_quant_config()
    if deploy:
        quantizer.export_xmodel(deploy_check=False)
示例#5
0
random.seed(random_seed)
np.random.seed(random_seed)
torch.manual_seed(random_seed)
print('Successfully imported all packages and configured random seed to %d!' %
      random_seed)
#os.environ['CUDA_VISIBLE_DEVICES'] = '0'
cuda_available = torch.cuda.is_available()
if cuda_available:
    torch.backends.cudnn.enabled = True
    torch.backends.cudnn.benchmark = True
    torch.cuda.manual_seed(random_seed)
    print('Using GPU.')
else:
    print('Using CPU.')
# ofa_network = ofa_net('ofa_mbv3_d234_e346_k357_w1.2', pretrained=True)
ofa_network = ofa_net('ofa_resnet50', pretrained=True)
print('The OFA Network is ready.')
if cuda_available:
    # path to the ImageNet dataset
    print("Please input the path to the ImageNet dataset.\n")
    imagenet_data_path = "/gdata/ImageNet2012"

    print(os.path.isdir(imagenet_data_path))
    # if 'imagenet_data_path' is empty, download a subset of ImageNet containing 2000 images (~250M) for test
    # if not os.path.isdir(imagenet_data_path):
    #     os.makedirs(imagenet_data_path, exist_ok=True)
    #     download_url('https://hanlab.mit.edu/files/OnceForAll/ofa_cvpr_tutorial/imagenet_1k.zip', model_dir='data')
    #     ! cd data && unzip imagenet_1k 1>/dev/null && cd ..
    #     ! cp -r data/imagenet_1k/* $imagenet_data_path
    #     ! rm -rf data
    #     print('%s is empty. Download a subset of ImageNet for test.' % imagenet_data_path)
示例#6
0
	def make_db(self):
		self.ofa_network = ofa_net('ofa_mbv3_d234_e346_k357_w1.0', pretrained=True)
		self.run_config = ImagenetRunConfig(test_batch_size=self.args.batch_size,
		                                    n_worker=20)
		database = []
		st_time = time.time()
		f = open(f'{self.path}/txt_{self.index}.txt', 'w')
		for dn in range(10000):
			best_pp = -1
			best_info = None
			dls = None
			with torch.no_grad():
				if self.model_name == 'generator':
					for i in range(10):
						net_setting = self.ofa_network.sample_active_subnet()
						subnet = self.ofa_network.get_active_subnet(preserve_weight=True)
						if i == 0:
							run_manager = RunManager('.tmp/eval_subnet', self.args, subnet,
							                         self.run_config, init=False, pp=self.predictor)
							self.run_config.data_provider.assign_active_img_size(224)
							dls = {j: copy.deepcopy(run_manager.data_loader) for j in range(1, 10)}
						else:
							run_manager = RunManager('.tmp/eval_subnet', self.args, subnet,
							                         self.run_config,
							                         init=False, data_loader=dls[i], pp=self.predictor)
							run_manager.reset_running_statistics(net=subnet)
						
						loss, (top1, top5), pred_acc \
							= run_manager.validate(net=subnet, net_setting=net_setting)
						
						if best_pp < pred_acc:
							best_pp = pred_acc
							print('[%d] class=%d,\t loss=%.5f,\t top1=%.1f,\t top5=%.1f' % (
								dn, len(run_manager.cls_lst), loss, top1, top5))
							info_dict = {'loss': loss,
							             'top1': top1,
							             'top5': top5,
							             'net': net_setting,
							             'class': run_manager.cls_lst,
							             'params': run_manager.net_info['params'],
							             'flops': run_manager.net_info['flops'],
							             'test_transform': run_manager.test_transform
							             }
							best_info = info_dict
				elif self.model_name == 'predictor':
					net_setting = self.ofa_network.sample_active_subnet()
					subnet = self.ofa_network.get_active_subnet(preserve_weight=True)
					run_manager = RunManager('.tmp/eval_subnet', self.args, subnet, self.run_config, init=False)
					self.run_config.data_provider.assign_active_img_size(224)
					run_manager.reset_running_statistics(net=subnet)
					
					loss, (top1, top5), _ = run_manager.validate(net=subnet)
					print('[%d] class=%d,\t loss=%.5f,\t top1=%.1f,\t top5=%.1f' % (
						dn, len(run_manager.cls_lst), loss, top1, top5))
					best_info = {'loss': loss,
					             'top1': top1,
					             'top5': top5,
					             'net': net_setting,
					             'class': run_manager.cls_lst,
					             'params': run_manager.net_info['params'],
					             'flops': run_manager.net_info['flops'],
					             'test_transform': run_manager.test_transform
					             }
				database.append(best_info)
				if (len(database)) % 10 == 0:
					msg = f'{(time.time() - st_time) / 60.0:0.2f}(min) save {len(database)} database, {self.index} id'
					print(msg)
					f.write(msg + '\n')
					f.flush()
					torch.save(database, f'{self.path}/database_{self.index}.pt')