From e0de909d62965c9d259ae515bb0135e49f93cbfe Mon Sep 17 00:00:00 2001 From: c Date: Thu, 19 Dec 2019 16:07:29 +0800 Subject: [PATCH] Revert "Refine kernal Random to w*h*16 kinds possiable." This reverts commit 675cd8567e8ba0643e8c63d60324ac84b5b1f218. --- FilterEvaluator/Evaluator.py | 1 + FilterEvaluator/EvaluatorUnsuper.py | 136 ++++++++++++-------- FilterEvaluator/bestweightEntropySearch.npy | Bin 416 -> 416 bytes FilterEvaluator/checkpointEntropySearch.pkl | Bin 1836 -> 1836 bytes 4 files changed, 80 insertions(+), 57 deletions(-) diff --git a/FilterEvaluator/Evaluator.py b/FilterEvaluator/Evaluator.py index bd13e97..937b9bd 100644 --- a/FilterEvaluator/Evaluator.py +++ b/FilterEvaluator/Evaluator.py @@ -16,6 +16,7 @@ import random import cv2 + CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/" print("Current Path :" + CurrentPath) diff --git a/FilterEvaluator/EvaluatorUnsuper.py b/FilterEvaluator/EvaluatorUnsuper.py index 4cd111d..608a878 100644 --- a/FilterEvaluator/EvaluatorUnsuper.py +++ b/FilterEvaluator/EvaluatorUnsuper.py @@ -16,7 +16,6 @@ import random import cv2 from tqdm import tqdm import threading -from itertools import combinations CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/" @@ -27,19 +26,6 @@ import Model as Model from tools import utils, Train, Loader - - - - -# iterable = combinations(range(72), 4) -# count = iterable.count() -# pool = tuple(iterable) -# n = len(pool) -# indices = sorted(random.sample(range(n), 2)) -# fdafda = tuple(pool[i] for i in indices) - - - def GetScore(netmodel,layer,SearchLayer,DataSet): netmodel.eval() sample = utils.SetDevice(torch.empty((SearchLayer.out_channels,0))) @@ -198,12 +184,10 @@ def UnsuperLearnFindBestWeight(netmodel, layer, weight, dataloader, databatchs=1 # search best weight from random data -# Test total sample size = stepsize * stepsize * interation -# Random kernel number = in_channels * kernel_size[0] * kernel_size[1] * 16 -def UnsuperLearnSearchBestWeight(netmodel, layer, dataloader, databatchs=128, stepsize=20, interation=1000): +def UnsuperLearnSearchBestWeight(netmodel, layer, dataloader, databatchs=128, stepsize=1000, interation=1000): interationbar = tqdm(total=interation) forwardlayer = layer -1 - samplesize = stepsize*stepsize + netmodel.eval() tl = netmodel.features[layer] outchannels = tl.out_channels @@ -227,55 +211,93 @@ def UnsuperLearnSearchBestWeight(netmodel, layer, dataloader, databatchs=128, st for i in range(outchannels): shift.append(1<0: + bitted = bittedset.pop() + hasdata = 1 + bittedLock.release() + if hasdata > 0: + entropys = [] + for i in range(len(indexs)): + histced = bitted[:,i].histc(256,0,255).type(torch.float32) + histced = histced[histced>0] + histced = histced/histced.sum() + entropy = (histced.log2()*histced).sum() + entropys.append(entropy.numpy()) + argmin = np.argmin(entropys) + + bestLock.acquire() + if entropys[argmin] < bestentropy[0]: + bestweight = newweight[indexs[argmin]] + bestentropy[0] = entropys[argmin] + print("finded better entropy") + bestLock.release() for j in range(interation): - # 400 8 4096 5 5 - output = outputs[indexs[j]] - # 102400 400 8 - reshaped = output.reshape(samplesize, outchannels, -1).permute(2, 0, 1) - # 400 8 - meaned = reshaped.mean(0) - # 102400 400 - # bitted = ((reshaped > meaned)* shift).sum(2).type(torch.float32).detach().cpu() - bitted = ((reshaped > meaned)* shift).sum(2).type(torch.float32) + newweightshape = list(newlayer.weight.data.shape) + newweightshape[0] = stepsize + newweight = np.random.uniform(-1.0,1.0,newweightshape).astype("float32") + newweight = newweight.reshape((-1,newweightshape[-1]*newweightshape[-2])) + newweight = np.swapaxes(newweight,0,1)-np.mean(newweight,-1) + newweight = np.swapaxes(newweight,0,1).reshape(newweightshape) - entropys = [] - for i in range(samplesize): - histced = bitted[:,i].histc(256,0,255).type(torch.float32) - histced = histced[histced>0] - histced = histced/histced.sum() - entropy = (histced.log2()*histced).sum() - entropys.append(entropy.detach().cpu().numpy()) - argmin = np.argmin(entropys) - if entropys[argmin] < bestentropy: - bestweight = newweight[indexs[j][argmin]] - bestentropy = entropys[argmin] + newlayer.weight.data=utils.SetDevice(torch.from_numpy(newweight)) + outputs = newlayer(datasnetout).transpose(0,1) + samplesize = stepsize*stepsize + indexs = np.random.randint(0,stepsize,samplesize*outchannels).reshape(samplesize,-1) + # 1000 8 4096 5 5 + outputs = outputs[indexs] + # 102400 1000 8 + reshaped = outputs.reshape(samplesize, outchannels, -1).permute(2, 0, 1) + # 1000 8 + meaned = reshaped.mean(0) + # 102400 1000 + bitted = ((reshaped > meaned)* shift).sum(2).type(torch.float32).detach().cpu() + + bittedLock.acquire() + bittedset.append(bitted) + bittedLock.release() + threadcpu = CPU() + threadcpu.start() + + # entropys = [] + # for i in range(len(indexs)): + # histced = bitted[:,i].histc(256,0,255).type(torch.float32) + # histced = histced[histced>0] + # histced = histced/histced.sum() + # entropy = (histced.log2()*histced).sum() + # entropys.append(entropy.detach().cpu().numpy()) + + # argmin = np.argmin(entropys) + # if entropys[argmin] < bestentropy: + # bestweight = newweight[indexs[argmin]] + # bestentropy = entropys[argmin] + interationbar.update(1) - interationbar.set_description("entropy:"+str(bestentropy)) + interationbar.set_description("left:"+str(len(bittedset))) + + while bittedset: + time.sleep(100) + interationbar.close() return bestweight + + def SetModelConvWeight(model, layer, weight): w = utils.SetDevice(torch.from_numpy(weight)) model.features[layer].weight.data = w diff --git a/FilterEvaluator/bestweightEntropySearch.npy b/FilterEvaluator/bestweightEntropySearch.npy index c9e7d788827196874fd5107fb73e37749004474b..5f64aa961998aca9c5e662970679899674b7125a 100644 GIT binary patch delta 297 zcmV+^0oMMY1E2$tfPcvyMnBN@4nG>zgTDA9n(LNqFK|j&tAHOOP4L{D5g};&}AwCG9n!Yf7f4ny_r#`IU89!<| zMZfuHeZF!zmpzVLl0HyEMLiO>9zQ-QM84i*20t9F?Y&`WFn_2Z#r(doioHAm z-u*s$^9()}kTbtaekVV2)#E-I<&i#{RW!fQLr=dxRu4Z->j*yV$z(s;y8w0wyn#RH*ag2ay$`;Sk#|38sEods vR#m@Odn~_*Eh51_6XwmnwE|DSDgZV=3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