def get_crossover(keep_top_k, num_states, crossover_num, test_dict, untest_dict): print('crossover ......', flush=True) res = [] k = len(keep_top_k) iter = 0 max_iters = 10 * crossover_num while len(res) < crossover_num and iter < max_iters: id1, id2 = np.random.choice(k, 2, replace=False) p1 = keep_top_k[id1] p2 = keep_top_k[id2] mask = np.random.randint(low=0, high=2, size=(num_states + 1)).astype(np.float32) can = p1 * mask + p2 * (1.0 - mask) iter += 1 t_can = tuple(can[:-1]) model_for_flops = model_for_FLOPs.resnet50(can[:-1].astype( np.int)).cuda() flops = print_model_parm_flops(model_for_flops) if t_can in untest_dict.keys() or t_can in test_dict.keys( ) or flops > max_FLOPs or flops < min_FLOPs: continue can[-1] = flops res.append(can) untest_dict[t_can] = -1 if len(res) == crossover_num: break print('crossover_num = {}'.format(len(res)), flush=True) return res
def get_mutation(keep_top_k, num_states, mutation_num, m_prob, test_dict, untest_dict): print('mutation ......', flush=True) res = [] k = len(keep_top_k) iter = 0 max_iters = 10 while len(res) < mutation_num and iter < max_iters: ids = np.random.choice(k, mutation_num) select_seed = np.array([keep_top_k[id] for id in ids]) is_m = np.random.choice(np.arange(0, 2), (mutation_num, num_states + 1), p=[1 - m_prob, m_prob]) mu_val = np.random.choice(np.arange(1, len(channel_scale)), (mutation_num, num_states + 1)) * is_m select_list = ((select_seed + mu_val) % len(channel_scale)) iter += 1 for can in select_list: t_can = tuple(can[:-1]) model_for_flops = model_for_FLOPs.resnet50(can[:-1].astype( np.int)).cuda() flops = print_model_parm_flops(model_for_flops) if t_can in untest_dict.keys() or t_can in test_dict.keys( ) or flops > max_FLOPs or flops < min_FLOPs: continue can[-1] = flops res.append(can) untest_dict[t_can] = flops if len(res) == mutation_num: break print('mutation_num = {}'.format(len(res)), flush=True) return res
def random_can(num, num_states, test_dict, untest_dict): print('random select ........', flush=True) candidates = [] while (len(candidates)) < num: can = [] for i in range(len(stage_repeat)): can += [ np.random.randint(low=0, high=int(len(overall_channel_scale))) ] * stage_repeat[i] for i in range(num_states + 1): can += [np.random.randint(low=0, high=int(len(mid_channel_scale)))] #can = np.random.randint(low=0, high=len(overall_channel_scale), size=(num_states+1)).astype(np.float32) can = np.asarray(can).astype(np.float32) t_can = can[:-1] overall_scale_ids = t_can[:sum(stage_repeat)].astype(np.int) mid_scale_ids = t_can[sum(stage_repeat):].astype(np.int) model_for_flops = model_for_FLOPs.MobileNetV2(overall_scale_ids, mid_scale_ids).cuda() flops = print_model_parm_flops(model_for_flops) t_can = tuple(can[:-1]) if t_can in test_dict.keys() or t_can in untest_dict.keys( ) or flops > max_FLOPs: continue can[-1] = flops candidates.append(can) untest_dict[t_can] = -1 print('random_num = {}'.format(len(candidates)), flush=True) return candidates
def random_can(num, num_states, test_dict, untest_dict): print('random select ........', flush=True) candidates = [] while(len(candidates))<num: can = np.random.randint(low=0, high=len(channel_scale), size=(num_states+1)).astype(np.float32) t_can = tuple(can[:-1]) flops = print_model_parm_flops(model_for_flops, can[:-1].astype(np.int)) if t_can in test_dict.keys() or t_can in untest_dict.keys() or flops>max_FLOPs: continue can[-1] = flops candidates.append(can) untest_dict[t_can] = -1 print('random_num = {}'.format(len(candidates)), flush=True) return candidates
def get_crossover(keep_top_k, num_states, crossover_num, test_dict, untest_dict): print('crossover ......', flush=True) res = [] k = len(keep_top_k) iter = 0 max_iters = 10 * crossover_num while len(res) < crossover_num and iter < max_iters: id1, id2 = np.random.choice(k, 2, replace=False) p1 = keep_top_k[id1] p2 = keep_top_k[id2] mask = [] for i in range(len(stage_repeat)): mask += [np.random.randint(low=0, high=2)] * stage_repeat[i] for i in range(num_states + 1): mask += [np.random.randint(low=0, high=2)] #mask = np.random.randint(low=0, high=2, size=(num_states+1)).astype(np.float32) mask = np.array(mask) can = p1 * mask + p2 * (1.0 - mask) iter += 1 t_can = can[:-1] overall_scale_ids = t_can[:sum(stage_repeat)].astype(np.int) mid_scale_ids = t_can[sum(stage_repeat):].astype(np.int) model_for_flops = model_for_FLOPs.MobileNetV2(overall_scale_ids, mid_scale_ids).cuda() flops = print_model_parm_flops(model_for_flops) t_can = tuple(can[:-1]) if t_can in untest_dict.keys() or t_can in test_dict.keys( ) or flops > max_FLOPs: continue can[-1] = flops res.append(can) untest_dict[t_can] = -1 if len(res) == crossover_num: break print('crossover_num = {}'.format(len(res)), flush=True) return res
def get_mutation(keep_top_k, num_states, mutation_num, m_prob, test_dict, untest_dict): print('mutation ......', flush=True) res = [] k = len(keep_top_k) iter = 0 max_iters = 10 while len(res) < mutation_num and iter < max_iters: ids = np.random.choice(k, mutation_num) select_seed = np.array([keep_top_k[id] for id in ids]) is_m_part_a = np.random.choice(np.arange(0, 2), (mutation_num, len(stage_repeat)), p=[1 - m_prob, m_prob]) is_m_part_b = np.random.choice(np.arange(0, 2), (mutation_num, num_states + 1), p=[1 - m_prob, m_prob]) mu_val_part_a = np.random.choice( np.arange(1, len(overall_channel_scale)), (mutation_num, len(stage_repeat))) * is_m_part_a mu_val_part_b = np.random.choice( np.arange(1, len(mid_channel_scale)), (mutation_num, num_states + 1)) * is_m_part_b mu_val = np.zeros([mutation_num, sum(stage_repeat) + num_states + 1]) j = 0 for i in range(len(stage_repeat)): j_prev = j j = j_prev + stage_repeat[i] for p11 in range(j_prev, j): mu_val[:, p11] = mu_val_part_a[:, i] mu_val[:, sum(stage_repeat):] = mu_val_part_b #mu_val = np.random.choice(np.arange(1,len(channel_scale)), (mutation_num, num_states+1))*is_m select_list = np.zeros( [mutation_num, sum(stage_repeat) + num_states + 1]) select_list[:, :sum(stage_repeat)] = ( (select_seed + mu_val)[:, :sum(stage_repeat)] % len(overall_channel_scale)) select_list[:, sum(stage_repeat):] = ( (select_seed + mu_val)[:, sum(stage_repeat):] % len(mid_channel_scale)) iter += 1 for can in select_list: t_can = can[:-1] overall_scale_ids = t_can[:sum(stage_repeat)].astype(np.int) mid_scale_ids = t_can[sum(stage_repeat):].astype(np.int) model_for_flops = model_for_FLOPs.MobileNetV2( overall_scale_ids, mid_scale_ids).cuda() flops = print_model_parm_flops(model_for_flops) t_can = tuple(can[:-1]) if t_can in untest_dict.keys() or t_can in test_dict.keys( ) or flops > max_FLOPs: continue can[-1] = flops res.append(can) untest_dict[t_can] = flops if len(res) == mutation_num: break print('mutation_num = {}'.format(len(res)), flush=True) return res