def alexnet_layers(n, MODEL_NAME): branchyNet = AlexNet().build(n) branchyNet.to_gpu() branchyNet.training() branchyNet.verbose = False # 在主网络上进行训练 main_loss, main_acc, main_time = utils.train(branchyNet, X_train, Y_train, main=True, batchsize=TRAIN_BATCHSIZE, num_epoch=TRAIN_NUM_EPOCHES) print("main_time:", main_time) # 在带有分支的网络上进行训练 branch_loss, branch_acc, branch_time = utils.train( branchyNet, X_train, Y_train, batchsize=TRAIN_BATCHSIZE, num_epoch=TRAIN_NUM_EPOCHES) print("branch_time:", branch_time) # 保存已经训练好的网络模型 with open(MODEL_PATH + MODEL_NAME, "wb") as f: dill.dump(branchyNet, f) # 加载已经保存好的模型 with open(MODEL_PATH + MODEL_NAME, "rb") as f: branchyNet = dill.load(f) # 网络模型测试,获取分支网络的准确率 branchyNet.testing() branchyNet.verbose = False branchyNet.to_gpu() # 退出点阈值设置 thresholdExits = [0.05, 0.01] branchyNet.thresholdExits = thresholdExits # 根据退出点阈值来获取分支网络的总体准确率 overacc, _, _, accbreakdowns, _ = utils.test(branchyNet, X_test, Y_test, batchsize=TEST_BATCHSIZE) return accbreakdowns
# 保存训练好的网络模型 with open(MODEL_NAME, "wb") as f: dill.dump(branchyNet, f) # 加载已保存好的模型 with open(MODEL_NAME, "rb") as f: branchyNet = dill.load(f) # 网络模型测试,获取主网络测试时间以及准确率 branchyNet.testing() branchyNet.verbose = False branchyNet.to_gpu() g_baseacc, g_basediff, _, _, _ = utils.test(branchyNet, X_test, Y_test, main=True, batchsize=TEST_BATCHSIZE) g_basediff = (g_basediff / float(len(Y_test))) * 1000. print("g_baseacc:", g_baseacc) print("g_basediff:", g_basediff) # 退出点阈值设置 thresholds = [ 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1., 2., 3., 5., 10. ] # 根据退出点阈值来获取代入单个退出点阈值的网络测试准确率、测试时间以及退出点样本数(带分支网络),最大信息熵 g_ts, g_accs, g_diffs, g_exits, g_entropies = utils.screen_branchy( branchyNet,
# Settings TEST_BATCHSIZE = 1 print '2. set network to inference mode' branchyNet.to_gpu() branchyNet.testing() branchyNet.verbose = False branchyNet.gpu = True # ren + # Test main g_baseacc, g_basediff, g_num_exits, g_accbreakdowns = utils.test(branchyNet, x_test, y_test, main=True, batchsize=TEST_BATCHSIZE) print 'main accuracy: ', g_baseacc g_basediff = (g_basediff / float(len(y_test))) * 1000. branchyNet.to_cpu() with open("_models/test_resnet56_cifar10_gpu_(g_baseacc).pkl", "w") as f: dill.dump({'g_baseacc': g_baseacc}, f) with open("_models/test_resnet56_cifar10_gpu_(g_basediff).pkl", "w") as f: dill.dump({'g_basediff': g_basediff}, f) with open("_models/test_resnet56_cifar10_gpu_(g_num_exits).pkl", "w") as f: dill.dump({'g_num_exits': g_num_exits}, f) with open("_models/test_resnet56_cifar10_gpu_(g_accbreakdowns).pkl", "w") as f: dill.dump({'g_accbreakdowns': g_accbreakdowns}, f)