1
+ import os
2
+ os .environ ["CUDA_VISIBLE_DEVICES" ] = "1"
3
+
4
+ from legodnn .utils .dl .common .env import set_random_seed
5
+ set_random_seed (0 )
6
+
7
+ import sys
8
+ sys .setrecursionlimit (100000 )
9
+ import torch
10
+
11
+ from legodnn import BlockExtractor , BlockTrainer , ServerBlockProfiler , EdgeBlockProfiler , OptimalRuntime
12
+ from legodnn .gen_series_legodnn_models import gen_series_legodnn_models
13
+ from legodnn .block_detection .model_topology_extraction import topology_extraction
14
+ from legodnn .presets .auto_block_manager import AutoBlockManager
15
+ from legodnn .presets .common_detection_manager_1204_new import CommonDetectionManager
16
+ from legodnn .model_manager .common_model_manager import CommonModelManager
17
+ from legodnn .utils .common .file import experiments_model_file_path
18
+ from legodnn .utils .dl .common .model import get_module , set_module , get_model_size
19
+
20
+ from cv_task .datasets .image_classification .cifar_dataloader import CIFAR10Dataloader , CIFAR100Dataloader
21
+ from cv_task .image_classification .cifar .models import inceptionv3
22
+ from cv_task .image_classification .cifar .legodnn_configs import get_cifar100_train_config_200e
23
+ from cv_task .image_classification .class_tools import train_model , test_model
24
+
25
+ if __name__ == '__main__' :
26
+ cv_task = 'image_classification'
27
+ dataset_name = 'cifar100'
28
+ model_name = 'inceptionv3'
29
+ method = 'legodnn'
30
+ device = 'cuda'
31
+ compress_layer_max_ratio = 0.125
32
+ model_input_size = (1 , 3 , 32 , 32 )
33
+
34
+ block_sparsity = [0.0 , 0.2 , 0.4 , 0.6 , 0.8 ]
35
+ root_path = os .path .join ('results/legodnn' , cv_task , model_name + '_' + dataset_name + '_' + str (compress_layer_max_ratio ).replace ('.' , '-' ))
36
+
37
+ compressed_blocks_dir_path = root_path + '/compressed'
38
+ trained_blocks_dir_path = root_path + '/trained'
39
+ descendant_models_dir_path = root_path + '/descendant'
40
+ block_training_max_epoch = 65
41
+ test_sample_num = 100
42
+
43
+ checkpoint = 'data/models/resnet18/2021-10-20/22-09-22/resnet18.pth'
44
+ teacher_model = inceptionv3 (num_classes = 100 ).to (device )
45
+ teacher_model .load_state_dict (torch .load (checkpoint )['net' ])
46
+
47
+ print ('\033 [1;36m--------------------------------> BUILD LEGODNN GRAPH\033 [0m' )
48
+ model_graph = topology_extraction (teacher_model , model_input_size , device = device , mode = 'unpack' )
49
+ model_graph .print_ordered_node ()
50
+
51
+ print ('\033 [1;36m--------------------------------> START BLOCK DETECTION\033 [0m' )
52
+ detection_manager = CommonDetectionManager (model_graph , max_ratio = compress_layer_max_ratio )
53
+ detection_manager .detection_all_blocks ()
54
+ detection_manager .print_all_blocks ()
55
+
56
+ model_manager = CommonModelManager ()
57
+ block_manager = AutoBlockManager (block_sparsity , detection_manager , model_manager )
58
+
59
+ print ('\033 [1;36m--------------------------------> START BLOCK EXTRACTION\033 [0m' )
60
+ block_extractor = BlockExtractor (teacher_model , block_manager , compressed_blocks_dir_path , model_input_size , device )
61
+ block_extractor .extract_all_blocks ()
62
+
63
+ print ('\033 [1;36m--------------------------------> START BLOCK TRAIN\033 [0m' )
64
+ train_loader , test_loader = CIFAR100Dataloader ()
65
+ block_trainer = BlockTrainer (teacher_model , block_manager , model_manager , compressed_blocks_dir_path ,
66
+ trained_blocks_dir_path , block_training_max_epoch , train_loader , device = device )
67
+ block_trainer .train_all_blocks ()
68
+
69
+ server_block_profiler = ServerBlockProfiler (teacher_model , block_manager , model_manager ,
70
+ trained_blocks_dir_path , test_loader , model_input_size , device )
71
+ server_block_profiler .profile_all_blocks ()
72
+
73
+ edge_block_profiler = EdgeBlockProfiler (block_manager , model_manager , trained_blocks_dir_path ,
74
+ test_sample_num , model_input_size , device )
75
+ edge_block_profiler .profile_all_blocks ()
76
+
77
+ optimal_runtime = OptimalRuntime (trained_blocks_dir_path , model_input_size ,
78
+ block_manager , model_manager , device )
79
+ model_size_min = get_model_size (torch .load (os .path .join (compressed_blocks_dir_path , 'model_frame.pt' )))/ 1024 ** 2
80
+ model_size_max = get_model_size (teacher_model )/ 1024 ** 2 + 1
81
+ gen_series_legodnn_models (deadline = 100 , model_size_search_range = [model_size_min , model_size_max ], target_model_num = 100 , optimal_runtime = optimal_runtime , descendant_models_save_path = descendant_models_dir_path , device = device )
0 commit comments