def performance_summary(model, dummy_input, opt=None, prefix=""): try: df = distiller.model_performance_summary(model.module, dummy_input) except AttributeError: df = distiller.model_performance_summary(model, dummy_input) new_entry = { "Name": ["Total"], "MACs": [df["MACs"].sum()], } MAC_total = df["MACs"].sum() return MAC_total
def performance_summary(model, dummy_input, opt=None, prefix=""): try: df = distiller.model_performance_summary(model.module, dummy_input) except AttributeError: df = distiller.model_performance_summary(model, dummy_input) new_entry = { 'Name':['Total'], 'MACs':[df['MACs'].sum()], } MAC_total = df['MACs'].sum() return MAC_total
def get_experiment_performance_summary(chkpt_fname, dataset, arch, validate_fn): model = create_model(False, dataset, arch) model, compression_scheduler, start_epoch = apputils.load_checkpoint(model, chkpt_fname) dummy_input = get_dummy_input(dataset) perf_df = distiller.model_performance_summary(model, dummy_input, 1) total_macs = perf_df['MACs'].sum() top1, top5, vloss = validate_fn(model=model, epoch=-1) return total_macs, distiller.model_numel(model), top1
def test_sg_macs(): '''Compare the MACs of different modules as computed by a SummaryGraph and model summary.''' import common sg = create_graph('imagenet', 'mobilenet') assert sg model, _ = common.setup_test('mobilenet', 'imagenet', parallel=False) df_compute = distiller.model_performance_summary(model, distiller.get_dummy_input('imagenet')) modules_macs = df_compute.loc[:, ['Name', 'MACs']] for name, mod in model.named_modules(): if isinstance(mod, (nn.Conv2d, nn.Linear)): summary_macs = int(modules_macs.loc[modules_macs.Name == name].MACs) sg_macs = sg.find_op(name)['attrs']['MACs'] assert summary_macs == sg_macs
def test_compute_summary(): dataset = "cifar10" arch = "simplenet_cifar" model, _ = common.setup_test(arch, dataset, parallel=True) df_compute = distiller.model_performance_summary( model, distiller.get_dummy_input(dataset)) module_macs = df_compute.loc[:, 'MACs'].to_list() # [conv1, conv2, fc1, fc2, fc3] assert module_macs == [352800, 240000, 48000, 10080, 840] dataset = "imagenet" arch = "mobilenet" model, _ = common.setup_test(arch, dataset, parallel=True) df_compute = distiller.model_performance_summary( model, distiller.get_dummy_input(dataset)) module_macs = df_compute.loc[:, 'MACs'].to_list() expected_macs = [ 10838016, 3612672, 25690112, 1806336, 25690112, 3612672, 51380224, 903168, 25690112, 1806336, 51380224, 451584, 25690112, 903168, 51380224, 903168, 51380224, 903168, 51380224, 903168, 51380224, 903168, 51380224, 225792, 25690112, 451584, 51380224, 1024000 ] assert module_macs == expected_macs
def save_model_stats(model, save_path, name=None): if not os.path.exists(save_path): os.makedirs(save_path) sparsity_df, _ = distiller.weights_sparsity_summary(model, True) performance_df = distiller.model_performance_summary( model, torch.rand([1, 3, 352, 352]), 1) if name: sparsity_df.to_csv('{}/{}_{}.sparsity.csv'.format( save_path, model.name, name)) performance_df.to_csv('{}/{}_{}.performance.csv'.format( save_path, model.name, name)) else: sparsity_df.to_csv('{}/{}.sparsity.csv'.format(save_path, model.name)) performance_df.to_csv('{}/{}.performance.csv'.format( save_path, model.name))
def macs_display(model, macs_file, dummy_input): """ 分析网络的计算量MACs Arguments: model (class Net): 已训练好的模型 \n macs_file (str): 存储计算量结果的文件名称和位置 \n dummy_input (tensor): 和网络输入相同维度的dummy数据, for example, torch.FloatTensor(1, 3, 128, 128), [batch, channel, height, width] Examples: >>> from apputils.platform_summaries import * >>> macs_display(model, 'macs_file.xlsx', torch.FloatTensor(1, 3, 128, 128)) """ df_macs = distiller.model_performance_summary(model, dummy_input, 1) df_macs.to_csv(macs_file) netG_MACs = df_macs['MACs'].sum() msglogger.info("netG MACs: " + "{:,}".format(int(netG_MACs)))