def __init__(self, height, width, HidSize, OutSize, OPIUM_type, \ genWeights_type, q = 200, act_fun = tanh): print ">>> Initialing RF-ELM model <<<" smtic = time.time() InSize = height*width if genWeights_type != 'rf': raise Exception("Wrong model called") ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) self.RandomWeight = 2*random.rand(self.HidSize, self.InSize) - 1 F = zeros((self.HidSize, self.InSize)) for i in range(self.HidSize): uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) while size < q: uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) Fi = zeros((height, width)) Fi[uli:bri+1, ulj:brj+1] = 1 F[i, :] = Fi.flatten() self.RandomWeight *= F self.RandomWeight /= sqrt(sum(power(self.RandomWeight, 2), axis=1)).reshape(self.HidSize, 1) smtoc = time.time() print "RF-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic)
def run(): for dataset in datasets: print('INICIANDO VALIDAÇÃO PARA O %s' % dataset.name) hit_sum = [] data_manager = DataManager(dataset) data_manager.load_data(categorical=False) for i in range(1, 2): x_TRAIN, y_TRAIN, x_VALIDATION, y_VALIDATION = data_manager.split_train_test_5fold( data_manager.X, data_manager.Y) for fold in range(0, 2): rbf = ELM(number_of_hidden=1000) rbf.fit(x_TRAIN[fold], y_TRAIN[fold]) hit_sum.append( rbf.evaluate(x_VALIDATION[fold], y_VALIDATION[fold])) rmse = np.power(hit_sum, 0.5) print('MSE: %.25f' % np.average(hit_sum)) print('MSE Std: %.25f' % np.std(hit_sum)) print('RMSE: %.25f' % np.average(rmse)) print('R0.0MSE Std: %.25f' % np.std(rmse))
def __init__(self, InSize, HidSize, OutSize, OPIUM_type, \ genWeights_type, act_fun = np.tanh, H=1, stochastic=False,\ binaryTrain=False, binaryTest=True): if not binaryTrain and not binaryTest: raise Exception("Wrong model called, please call ELM but NOT BELM") print ">>> Initialing B-ELM model <<<" smtic = time.time() ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) self.H = H self.binaryTrain = binaryTrain self.binaryTest = binaryTest self.stochastic = stochastic self.LinearWeight = np.random.uniform(-self.H, self.H, size=(self.OutSize, self.HidSize)) self.Wb = self.binarization(self.LinearWeight) smtoc = time.time() print "B-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic) if binaryTrain: print ">>>Training in binary fassion" else: print ">>>Training in real value fassion" if binaryTest: print ">>>Test in binary fassion" else: print ">>>Test in real value fassion"
def cross_validation(): for dataset in datasets: print('INICIANDO VALIDAÇÃO PARA O %s' % dataset.name) hit_sum_test = [] data_manager = DataManager(dataset) data_manager.load_data(categorical=False) for index in range(0, 20): x_TRAIN, y_TRAIN, x_VALIDATION, y_VALIDATION = data_manager.split_train_test_5fold( data_manager.X, data_manager.Y) hit_sum = [] for fold in range(0, 5): rbf = ELM(number_of_hidden=1000) rbf.fit(x_TRAIN[fold], y_TRAIN[fold]) hit_sum.append( rbf.evaluate(x_VALIDATION[fold], y_VALIDATION[fold])) hit_sum_test.append(np.average(hit_sum)) print('Accuracy: %.2f' % np.average(hit_sum_test)) print('Min: %.2f' % np.min(hit_sum_test)) print('Max: %.2f' % np.max(hit_sum_test)) print('Stand De: %.2f%%' % np.std(hit_sum_test))
class ELMrecogPic_conv(object): def __init__(self): self.patcharm = 28 self.newELM = ELM(InSize = self.patcharm**2, HidSize=(self.patcharm**2)*10, OutSize=11) self.newELM.load('HYBRID') #self.newELM.load('rfciwelm') def runtest(self, numDigits, method = 'line', canvasSize0 = [], paddingMode = 0, edgeSize = 0): fltSize = 28 halfSize = 14 if method == 'rand': imgcanvas = MNISTCanvasImage('MNIST', canvasSize0) self.testcanvas0, labeltrue = imgcanvas.generateImage(numDigits) elif method == 'line': testcanvas_temp, labeltrue, canvasSize_info = lineMNIST(numDigits, edgeSize) if paddingMode == 1: self.testcanvas0 = zeros(canvasSize0) idx1 = random.randint(0, canvasSize0[0]-canvasSize_info[0]-1) idx2 = random.randint(0, canvasSize0[1]-canvasSize_info[1]-1) self.testcanvas0[idx1:idx1+canvasSize_info[0], idx2:idx2+canvasSize_info[1]] = testcanvas_temp else: self.testcanvas0 = testcanvas_temp canvasSize0 = canvasSize_info ## zeropadding testcanvas = zeropadding(self.testcanvas0, halfSize, halfSize) canvasSize = array(canvasSize0) + fltSize ## convolving part tot_count = (canvasSize[0]-fltSize)*(canvasSize[1]-fltSize) labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) print "> Convolving in progress ..." for y in range(halfSize, canvasSize[1]-halfSize): for x in range(halfSize, canvasSize[0]-halfSize): if ((y-halfSize)*(canvasSize[0]-fltSize)+x-halfSize+1)%500 == 0: print "> Current convolving index is %d ( %d in total )..."\ %((y-halfSize)*(canvasSize[0]-fltSize)+x-halfSize+1, tot_count) item_temp = testcanvas[x-halfSize:x+halfSize, y-halfSize:y+halfSize] labelhatpatch = self.newELM.recall(item_temp) p = distCal(labelhatpatch) #labelmatrix[(canvasSize[0]-fltSize)*(y-halfSize)+x-halfSize, :] = reshape(labelhatpatch,10) labelmatrix[x-halfSize, y-halfSize] = p return testcanvas, labelmatrix, labeltrue
def __init__(self): self.patcharm = [576, 900, 1296] #1296 #900 self.Outsize = 34 self.dim = [20, 24, 28] self.newELM_20 = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize) self.newELM_24 = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize) self.newELM_28 = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize) self.newELM_20.load('C:\\dataspace\\weights\\harbour34_20_basic') self.newELM_24.load('C:\\dataspace\\weights\\harbour34_24_basic') self.newELM_28.load('C:\\dataspace\\weights\\harbour34_28_basic') self.hog_20 = cv2.HOGDescriptor((self.dim[0], self.dim[0]), (8,8), (4,4), (4,4), 9) self.hog_24 = cv2.HOGDescriptor((self.dim[1], self.dim[1]), (8,8), (4,4), (4,4), 9) self.hog_28 = cv2.HOGDescriptor((self.dim[2], self.dim[2]), (8,8), (4,4), (4,4), 9)
def __init__(self, Num, Method, IMGsize = (100, 100)): self.patcharm = (28, 28) self.newELM = ELM(InSize = self.patcharm[0]**2, HidSize=(self.patcharm[0]**2)*20, OutSize=10, \ OPIUM_type='basic', genWeights_type ='dec') self.newELM.load('WellTrainedELMWeights') #self.newELM.load('rfciwelm') self.eqfactor = loadtxt('MNISTstatistics_factor.dat') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.label_max = None self.label_mean = None self.numDigits = Num self.labeltrue = zeros(Num) self.labelhat = zeros(Num) self.method = Method self.canvasSize0 = IMGsize self.entrylist = [] self.ave_act = [0.09, 0.1] self.peak_act = [0.3, 0.25, 0.2] self.entries = len(self.ave_act)* len(self.peak_act)
def __init__(self): self.patcharm = [576, 900, 1296] self.Outsize = 34 self.dim = [20, 24, 28] self.newELM_20 = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize) self.newELM_24 = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize) self.newELM_28 = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize) self.newELM_20.load('C:\\dataspace\\weights\\harbour34_20') self.newELM_24.load('C:\\dataspace\\weights\\harbour34_24') self.newELM_28.load('C:\\dataspace\\weights\\harbour34_28') self.hog_20 = cv2.HOGDescriptor((self.dim[0], self.dim[0]), (8,8), (4,4), (4,4), 9) self.hog_24 = cv2.HOGDescriptor((self.dim[1], self.dim[1]), (8,8), (4,4), (4,4), 9) self.hog_28 = cv2.HOGDescriptor((self.dim[2], self.dim[2]), (8,8), (4,4), (4,4), 9) self.stepsize = 4 self.centroidindex = None
def train_model(): data = load_data() (x_train, y_train), (x_test, y_test) = prepare_data_for_tooc(data) num_neurons = 2000 model = ELM(784, num_neurons, 10, sigmoid_activation) model.train(x_train, y_train) # evaluate training accuracy train_accuracy = 0 for s in range(x_train.shape[0]): output = model.get_output(x_train[s, :]) if np.argmax(output) == np.argmax(y_train[s, :]): train_accuracy += 1 print(train_accuracy / x_train.shape[0], flush=True) # evaluate test accuracy test_accuracy = 0 for s in range(x_test.shape[0]): output = model.get_output(x_test[s, :]) if np.argmax(output) == np.argmax(y_test[s, :]): test_accuracy += 1 print(test_accuracy / x_test.shape[0], flush=True)
def decision_surface(): for dataset in datasets: print('INICIANDO o plot da superfície de decisão PARA O %s' % dataset.value) data_manager = DataManager(dataset) data_manager.load_data() x_TRAIN, y_TRAIN, x_VALIDATION, y_VALIDATION = data_manager.split_train_test_5fold( data_manager.X, data_manager.Y) validation_perceptron = ELM(number_of_hidden=4)
def __init__(self, lane = 'v'): self.patcharm = 28 self.Outsize = 10 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*10, self.Outsize) self.newELM.load('C:\\dataspace\\weights\\') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.raw_labelmatrix = None self.Lane = lane
def __init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, data, act_fun = tanh): print ">>> Initialing C-ELM model <<<" smtic = time.time() if genWeights_type != 'c': raise Exception("Wrong model called") ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) N = data.shape[0] self.RandomWeight = zeros((self.HidSize, self.InSize)) self.b = zeros((self.HidSize, 1)) for i in range(self.HidSize): d1 = data[random.randint(0, N), :] d2 = data[random.randint(0, N), :] d = d1 + d2 self.RandomWeight[i,:] = d/sqrt(sum(power(d, 2))) self.b[i] = random.uniform(0, 1) smtoc = time.time() print "C-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic)
def __init__(self, lane = 'h'): self.patcharm = 28 self.Outsize = 11 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*10, self.Outsize) # load pre-trained weights here! self.newELM.load('C:\\dataspace\\weights\\MNIdata') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix0 = None self.labelmatrix = None self.Lane = lane
def run_elm_cancer(): acs = [] att = [x for x in range(10)] d = ut.get_cancer().to_numpy() print('======== ELM BREAST CANCER ========') for i in range(20): elm = ELM(8, 2) acs.append(realization_elm(elm, d, att)) print('Realização: {0}, Acurácia: {1}%'.format(i, acs[i])) print('Acurácia após 20 realizações: {0}%, Desvio padrão: {1}'.format( calc_accuracy(acs), np.std(acs)))
def run_elm_dermatology(): acs = [] att = [x for x in range(34)] d = ut.get_dermatology().to_numpy() print('======== ELM DERMATOLOGY ========') for i in range(20): elm = ELM(30, 6) acs.append(realization_elm(elm, d, att)) print('Realização: {0}, Acurácia: {1}%'.format(i, acs[i])) print('Acurácia após 20 realizações: {0}%, Desvio padrão: {1}'.format( calc_accuracy(acs), np.std(acs)))
def run_elm_vertebral(): acs = [] att = [0, 1, 2, 3, 4, 5] d = ut.get_column_data().to_numpy() print('======== ELM VERTEBRAL ========') for i in range(20): elm = ELM(16, 3) acs.append(realization_elm(elm, d, att)) print('Realização: {0}, Acurácia: {1}%'.format(i, acs[i])) print('Acurácia após 20 realizações: {0}%, Desvio padrão: {1}'.format( calc_accuracy(acs), np.std(acs)))
def run_elm_iris(): acs = [] att = [0, 1, 2, 3] d = ut.get_iris_data().to_numpy() print('======== ELM IRIS ========') for i in range(20): elm = ELM(18, 3) acs.append(realization_elm(elm, d, att)) print('Realização: {0}, Acurácia: {1}%'.format(i, acs[i])) print('Acurácia após 20 realizações: {0}%, Desvio padrão: {1}'.format( calc_accuracy(acs), np.std(acs)))
def __init__(self, lane = 'v'): self.patcharm = 28 self.outSize = 10 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*20, self.outSize) self.newELM.load('WellTrainedELMWeights') #self.eqfactor = loadtxt('MNISTstatistics_factor.dat') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.raw_labelmatrix = None self.Lane = lane
def run_elm_xor(): acs = [] att = [0, 1] d = ut.get_xor().to_numpy() print('======== ELM XOR ========') for i in range(20): elm = ELM(8, 2) acs.append(realization_elm(elm, d, att, xor=True)) print('Realização: {0}, Acurácia: {1}%'.format(i, acs[i])) print('Acurácia após 20 realizações: {0}%, Desvio padrão: {1}'.format( calc_accuracy(acs), np.std(acs)))
def __init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, data, label, act_fun = tanh): print ">>> Initialing CIW-ELM model <<<" smtic = time.time() if genWeights_type != 'ciw': raise Exception("Wrong model called") ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) data = self.__normalizeTest(data) N, _ = label.shape self.RandomWeight = zeros((self.HidSize, self.InSize)) l = label.argmax(axis=1) start = 0 end = 0 for i in range(self.OutSize): start = end index = l == i Ni = sum(index) Mi = int(self.HidSize*float(Ni)/N) end += Mi if i == self.OutSize-1: Mi += self.HidSize-end end = self.HidSize Ri = random.randint(2, size = (Mi, Ni)) Ri[Ri == 0] = -1 Di = data[index] Wi = dot(Ri, Di) self.RandomWeight[start:end, :] = Wi self.RandomWeight /= sqrt(sum(power(self.RandomWeight, 2), axis=1)).reshape(self.HidSize, 1) smtoc = time.time() print "CIW-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic)
def __init__(self): self.patcharm = [576, 900, 1296] #1296 #900 self.Outsize = [34, 2] self.dim = [20, 24, 28] self.newELM_20 = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize[0]) self.newELM_24 = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize[0]) self.newELM_28 = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize[0]) self.newELM_20_binary = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize[1]) self.newELM_24_binary = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize[1]) self.newELM_28_binary = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize[1]) self.newELM_20.load('/home/houyimeng/dataspace/weights/harbour34_20') self.newELM_24.load('/home/houyimeng/dataspace/weights/harbour34_24') self.newELM_28.load('/home/houyimeng/dataspace/weights/harbour34_28') self.newELM_20_binary.load('/home/houyimeng/dataspace/weights/harbour2_20') self.newELM_24_binary.load('/home/houyimeng/dataspace/weights/harbour2_24') self.newELM_28_binary.load('/home/houyimeng/dataspace/weights/harbour2_28') self.hog_20 = cv2.HOGDescriptor((self.dim[0], self.dim[0]), (8,8), (4,4), (4,4), 9) self.hog_24 = cv2.HOGDescriptor((self.dim[1], self.dim[1]), (8,8), (4,4), (4,4), 9) self.hog_28 = cv2.HOGDescriptor((self.dim[2], self.dim[2]), (8,8), (4,4), (4,4), 9)
elm.trainModel(train_data, train_label) elm.save(r"D:\workspace\Data\Data Synthesis\synthesis\synthesis\weights\elm1") elm.testModel(test_data, test_label) """ # Train on real data train_data , train_label, test_data, test_label = loadData('REAL\greyscale', percent = 1) feature_dim = train_data.shape[1] label_dim = train_label.shape[1] train_data = normalizeData(train_data) test_data = normalizeData(test_data) elm = ELM(feature_dim, feature_dim*10, label_dim, 'lite', 'dec') elm.trainModel(train_data, train_label) elm.save(r"D:\workspace\Data\Data Synthesis\synthesis\synthesis\weights\elmReal") elm.testModel(test_data, test_label) """ # Train on synthetic data and fun-tune on real data from numpy import concatenate train_data , train_label, test_data, test_label = loadData('Real', percent = 1) data = concatenate((train_data, test_data), axis=0) label = concatenate((train_label, test_label), axis=0) feature_dim = data.shape[1] label_dim = label.shape[1] data = normalizeData(data)
# -*- coding: utf-8 -*- """ Created on Tue Mar 15 17:52:11 2016 @author: zz """ from util import * from ELM import ELM train_data, train_label, test_data, test_label = loadData(0.1) feature_dim = train_data.shape[1] label_dim = train_label.shape[1] elm = ELM(feature_dim, feature_dim*10, label_dim, 'lite', 'dec') elm.trainModel(train_data, train_label) #elm.save(r"D:\workspace\Data\ELM\weights\elm") elm.testModel(test_data, test_label)
class ELMrecogPic_beta(object): def __init__(self, lane = 'h'): self.patcharm = 28 self.Outsize = 11 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*10, self.Outsize) # load pre-trained weights here! self.newELM.load('C:\\dataspace\\weights\\MNIdata') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix0 = None self.labelmatrix = None self.Lane = lane def runtest(self, numDigits): fltSize = self.patcharm halfSize = fltSize/2 datasource = 'MNIST' # 'MNIST', 'synthesis' or 'others' if datasource == 'MNIST': self.testcanvas0, labeltrue, canvasSize0 = lineMNIST(numDigits) elif datasource == 'others': labeltrue = zeros((numDigits)) path = 'C:\\comingdata\\others\\' dirs = os.listdir(path) for item in dirs: img = cv2.imread(path+item) #img = img[int(img.size[0]/5):int(img.size[0]/3),:int(img.size[1]/2))) SIZE = ( img.size[0], img.size[1] ) #SIZE = ( int(img.size[0]*1.6), int(img.size[1]*1.6) ) img = cv2.resize(img, (0,0), fx=1.4, fy=1.4) temp_canvas0 = array(img.getdata())/255 self.testcanvas0 = reshape( temp_canvas0, (SIZE[1], SIZE[0])) canvasSize0 = array([SIZE[1], SIZE[0]]) else: self.testcanvas0, labeltrue, canvasSize0 = lineIMAGE(numDigits, source=datasource) ## zeropadding testcanvas = zeropadding(self.testcanvas0, halfSize, halfSize) canvasSize = array(canvasSize0) + fltSize ## convolving canvas tot_count = (canvasSize[0]-fltSize)*(canvasSize[1]-fltSize) self.labelmatrix0 = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) print "> Convolving in progress ..." for x in range(halfSize, canvasSize[0]-halfSize): for y in range(halfSize, canvasSize[1]-halfSize): if ( (x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize) %500 == 0: print "> Current convolving index is %d ( %d in total )..."%((x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize+1, tot_count) item_temp = testcanvas[x-halfSize:x+halfSize, y-halfSize:y+halfSize] labelhatpatch = self.newELM.recall(item_temp) self.labelmatrix0[x-halfSize,y-halfSize] = distCal(labelhatpatch) # use a mass flt to flt the result self.labelmatrix = massflt(self.labelmatrix0) #self.labelmatrix = self.labelmatrix0 ## clustering clust_temp = where(self.labelmatrix != -1) datamatrix = vstack((clust_temp[0], clust_temp[1])) Con, Iter = True, 0 separateWidth = self.patcharm*3/4 while Con and Iter < (numDigits * 10): print "> Clustering entry No.",Iter+1, "..." clusters, centroids = kmean(datamatrix.T, numDigits) Con = False for i in range(len(centroids)): for j in range(len(centroids)): # bound detection if i != j and absolute(centroids[i][0] - centroids[j][0]) <= separateWidth and \ absolute(centroids[i][1] - centroids[j][1]) <= separateWidth : Con = True Iter += 1 if Iter < numDigits * 10: print "Clustering Succeed!" else: print "Clustering failed, latest entry picked" centroids = array(centroids) labelhat0 = zeros(numDigits) countnum = zeros((self.Outsize, numDigits)) # decicsion making if clusters != []: for i in range(len(clusters)): for j in range(len(clusters[i])): temp = clusters[i][j] distfactor = absolute(temp[0]-centroids[i,0]) + absolute(temp[1]-centroids[i,1]) + 0.01 # euclidean distance countnum [self.labelmatrix[temp[0],temp[1]],i] += 1/distfactor # equalization labelhat0[i] = countnum[:,i].argmax() else: print "No data generated" labelhat0 = -1 if self.Lane == 'h': centroidindex = argsort(centroids[:,1]) elif self.Lane == 'v': centroidindex = argsort(centroids[:,0]) labelhat = array(labelhat0[centroidindex], dtype='int') self.truecolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.hatcolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) displaysize = 2 # visualize the centroids and their labels for i in range(len(centroids)): self.truecolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labeltrue[i] for i in range(len(centroids)): self.hatcolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labelhat[i] print "True labels are:", labeltrue print "Hat labels are:", labelhat return self.labelmatrix def visualize(self): if self.Lane == 'h': fig = plt.figure() fig.add_subplot(4,1,1) plt.imshow(self.testcanvas0) fig.add_subplot(4,1,2) plt.imshow(self.labelmatrix0) fig.add_subplot(4,1,3) plt.imshow(self.labelmatrix) fig.add_subplot(4,1,4) plt.imshow(self.hatcolormatrix) elif self.Lane == 'v': fig = plt.figure() fig.add_subplot(1,4,1) plt.imshow(self.testcanvas0) fig.add_subplot(1,4,2) plt.imshow(self.labelmatrix0) fig.add_subplot(1,4,3) plt.imshow(self.labelmatrix) fig.add_subplot(1,4,4) plt.imshow(self.hatcolormatrix)
class ELMrecogPic_multiscale(object): def __init__(self, Num, Method, IMGsize = (100, 100)): self.patcharm = (28, 28) self.newELM = ELM(InSize = self.patcharm[0]**2, HidSize=(self.patcharm[0]**2)*20, OutSize=10, \ OPIUM_type='basic', genWeights_type ='dec') self.newELM.load('WellTrainedELMWeights') #self.newELM.load('rfciwelm') self.eqfactor = loadtxt('MNISTstatistics_factor.dat') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.label_max = None self.label_mean = None self.numDigits = Num self.labeltrue = zeros(Num) self.labelhat = zeros(Num) self.method = Method self.canvasSize0 = IMGsize self.entrylist = [] self.ave_act = [0.09, 0.1] self.peak_act = [0.3, 0.25, 0.2] self.entries = len(self.ave_act)* len(self.peak_act) def convolve(self): stepSize = 1 edgeSize = 0 paddingMode = 0 halfSize = [int(self.patcharm[0]/2), int(self.patcharm[1]/2)] if self.method == 'rand': imgcanvas = MNISTCanvasImage('MNIST', self.canvasSize0) self.testcanvas0, self.labeltrue = imgcanvas.generateImage(self.numDigits) elif self.method == 'line': testcanvas_temp, self.labeltrue, canvasSize_info = lineMNIST(self.numDigits, edgeSize) if paddingMode == 1: self.testcanvas0 = zeros(self.canvasSize0) idx1 = random.randint(0, self.canvasSize0[0]-canvasSize_info[0]-1) idx2 = random.randint(0, self.canvasSize0[1]-canvasSize_info[1]-1) self.testcanvas0[idx1:idx1+canvasSize_info[0], idx2:idx2+canvasSize_info[1]] = testcanvas_temp else: self.testcanvas0 = testcanvas_temp self.canvasSize0 = canvasSize_info ## zeropadding testcanvas = zeropadding(self.testcanvas0, halfSize[0], halfSize[1]) canvasSize = array(self.canvasSize0) + array(self.patcharm) ## convolving part tot_count = (canvasSize[0]-halfSize[0]*2)*(canvasSize[1]-halfSize[1]*2) self.labelmatrix = zeros(((canvasSize[0]-halfSize[0]*2)/stepSize, (canvasSize[1]-halfSize[1]*2)/stepSize)) self.label_max = zeros(((canvasSize[0]-halfSize[0]*2)/stepSize, (canvasSize[1]-halfSize[1]*2)/stepSize)) self.label_mean = zeros(((canvasSize[0]-halfSize[0]*2)/stepSize, (canvasSize[1]-halfSize[1]*2)/stepSize)) print "> Convolving in progress ..." for x in range(halfSize[0], canvasSize[0]-halfSize[0]): for y in range(halfSize[1], canvasSize[1]-halfSize[1]): if ( (x-halfSize[0])*(canvasSize[1]-halfSize[1]*2)+y-halfSize[1]+1 ) %500 == 0: print "> Current convolving index is %d ( %d in total )..."%\ ( (x-halfSize[0])*(canvasSize[1]-halfSize[1]*2)+y-halfSize[1]+1, tot_count) if x%stepSize == 0 and y%stepSize == 0: item_temp = testcanvas[x-halfSize[0]:x+halfSize[0], y-halfSize[1]:y+halfSize[1]] labelhatpatch = self.newELM.recall(item_temp) self.labelmatrix[(x-halfSize[0])/stepSize, (y-halfSize[1])/stepSize] = distCal(labelhatpatch) self.label_max[(x-halfSize[0])/stepSize, (y-halfSize[1])/stepSize] = max(labelhatpatch) self.label_mean[(x-halfSize[0])/stepSize, (y-halfSize[1])/stepSize] = mean(labelhatpatch) def testOne(self, scale_idx): _labelmatrix_temp = zeros(self.labelmatrix.shape) _labelmatrix_temp = self.labelmatrix labelhat0 = zeros(self.numDigits) countnum = zeros((10, self.numDigits)) print "Current entry :", scale_idx+1 for idx in range(_labelmatrix_temp.shape[0]): for idy in range(_labelmatrix_temp.shape[1]): if self.peak_act[int(scale_idx/len(self.ave_act))] * self.label_max[idx, idy] \ >= self.label_mean[idx, idy] >= self.ave_act[scale_idx%len(self.ave_act)]: _labelmatrix_temp[idx,idy] = _labelmatrix_temp[idx,idy] else: _labelmatrix_temp[idx,idy] = -1 ## clustering clust_temp = where(_labelmatrix_temp != -1) datamatrix = vstack((clust_temp[0], clust_temp[1])) Con, Iter = True, 0 while Con and Iter < (self.numDigits * 10): print "> Clustering entry No.",Iter+1, "..." clusters, centroids = kmean(datamatrix.T, self.numDigits) Con = False for i in range(len(centroids)): for j in range(len(centroids)): # bound detection if i != j and absolute(centroids[i][0] - centroids[j][0]) <= int(self.patcharm[0]/2) and \ absolute(centroids[i][1] - centroids[j][1]) <= int(self.patcharm[1]/2) : Con = True Iter += 1 if Iter < self.numDigits * 10: self.entrylist.append(scale_idx) print "Clustering Succeed!" else: print "Clustering failed, latest entry picked" centroids = array(centroids) # decision making if clusters != []: for i in range(len(clusters)): for j in range(len(clusters[i])): temp = clusters[i][j] distfactor = (temp[0]-centroids[i,0])**2+(temp[1]-centroids[i,1])**2 + 0.01 # euclidean distance countnum [_labelmatrix_temp[temp[0],temp[1]],i] += 1/distfactor/self.eqfactor[self.labelmatrix[temp[0],temp[1]]] # equalization labelhat0[i] = countnum[:,i].argmax() else: print "No labels are tapped" labelhat0 = -1*ones(10) return labelhat0 def testLoop(self): result_multi = zeros((self.entries, self.numDigits)) for ety in range(self.entries): result_multi[ety,:] = self.testOne(ety) # voting for i1 in range(self.numDigits): combining_temp = zeros(10) for j1 in self.entrylist: combining_temp[result_multi[j1,i1]] += 1 print combining_temp self.labelhat[i1] = combining_temp.argmax() print 'True label:', self.labeltrue print 'Hat label:', array(self.labelhat,dtype='int') def visualize(self): plt.figure(1) plt.imshow(self.testcanvas0) plt.figure(2) plt.imshow(self.labelmatrix) plt.figure(3) plt.imshow(self.truecolormatrix) plt.figure(4) plt.imshow(self.hatcolormatrix)
def __init__(self, height, width, HidSize, OutSize, OPIUM_type, \ genWeights_type, data, label, q = 200, act_fun = np.tanh, H=1,\ randomPrec=3, actPrec=3, linearPrec=3, callPrec=3, fixedTrain = True, fixedTest=True): if genWeights_type != 'rf-ciw': raise Exception("Wrong model called") print ">>> Initialing FPGA-RF-CIW-ELM model <<<" smtic = time.time() InSize = height*width ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) self.H = H self.randomPrec = randomPrec self.actPrec = actPrec self.linearPrec = linearPrec self.callPrec = callPrec self.fixedTrain = fixedTrain self.fixedTest = fixedTest #self.LinearWeight = np.random.uniform(-self.H, self.H, size=(self.OutSize, self.HidSize)) #data = self.__normalizeTest(data) N = label.shape[0] self.RandomWeight = np.zeros((self.HidSize, self.InSize)) l = label.argmax(axis=1) start = 0 end = 0 for i in range(self.OutSize): start = end index = l == i Ni = np.sum(index) Mi = int(self.HidSize*float(Ni)/N) end += Mi if i == self.OutSize-1: Mi += self.HidSize-end end = self.HidSize Ri = np.random.randint(2, size = (Mi, Ni)) Ri[Ri == 0] = -1 Di = data[index] Wi = np.dot(Ri, Di) self.RandomWeight[start:end, :] = Wi F = np.zeros((self.HidSize, self.InSize)) for i in range(self.HidSize): uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) while size < q: uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) Fi = np.zeros((height, width)) Fi[uli:bri+1, ulj:brj+1] = 1 F[i, :] = Fi.flatten() self.RandomWeight *= F self.RandomWeight /= np.sqrt(np.sum(np.power(self.RandomWeight, 2), \ axis=1)).reshape(self.HidSize, 1) if self.randomPrec == 0: print ">>>Binary random weights" self.RandomWeight = self.binarization(self.RandomWeight) else: print ">>>Fixed point precise random weights: ", self.randomPrec, "bits representation" self.RandomWeight = around(self.RandomWeight, N_bits=self.randomPrec) if self.linearPrec == 0: print ">>>Binary linear weights" self.Wb = self.binarization(self.LinearWeight) else: print ">>>Fixed point precise linear weights: ", self.linearPrec, "bits representation" self.Wb = around(self.LinearWeight, N_bits=self.linearPrec) if self.fixedTrain: print ">>>Fixed point precise linear weight will be used in training\ phase: ", self.linearPrec, "bits representation" else: print ">>>Real value linear weights will be used in training phase" if self.fixedTest: print ">>>Linear weights will be fixed point precise weights \ in testing phase: ", self.linearPrec, "bits representation" else: print ">>>Linear weights will be real value weights in testing phase" smtoc = time.time() print "FPGA-RF-CIW-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic)
class ELMrecogPic_v2(object): def __init__(self): self.patcharm = [576, 900, 1296] self.Outsize = 34 self.dim = [20, 24, 28] self.newELM_20 = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize) self.newELM_24 = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize) self.newELM_28 = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize) self.newELM_20.load('C:\\dataspace\\weights\\harbour34_20') self.newELM_24.load('C:\\dataspace\\weights\\harbour34_24') self.newELM_28.load('C:\\dataspace\\weights\\harbour34_28') self.hog_20 = cv2.HOGDescriptor((self.dim[0], self.dim[0]), (8,8), (4,4), (4,4), 9) self.hog_24 = cv2.HOGDescriptor((self.dim[1], self.dim[1]), (8,8), (4,4), (4,4), 9) self.hog_28 = cv2.HOGDescriptor((self.dim[2], self.dim[2]), (8,8), (4,4), (4,4), 9) self.stepsize = 4 self.centroidindex = None def runtest(self, path = 'C:\\dataspace\\harbour\\canvas\\'): ############################################################################### # load image & prep #crop_rg = [0.05, 0.95, 0.05, 0.5] scaleFactor = 0.5 dirs = os.listdir(path) rand_idx = random.randint(len(dirs)) img = cv2.imread(path+dirs[rand_idx], 0) #img = img[int(img.shape[0]*crop_rg[2]):int(img.shape[0]*crop_rg[3]), int(img.shape[1]*crop_rg[0]):int(img.shape[1]*crop_rg[1])] self.testcanvas = cv2.resize(img, (0,0), fx=scaleFactor, fy=scaleFactor) print "> Grabing a random canvas for testing ... " print '> Image SIZE:', self.testcanvas.shape print '> Step SIZE:', self.stepsize ############################################################################### ## convolving part 1 fltSize_20 = self.dim[0] halfSize_20 = self.dim[0]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_20, halfSize_20) canvasSize = array(self.testcanvas.shape) + fltSize_20 self.labelmatrix_20 = -1*ones(( canvasSize[0]-fltSize_20, canvasSize[1]-fltSize_20)) self.confidence_20 = -1*ones(( canvasSize[0]-fltSize_20, canvasSize[1]-fltSize_20)) print "> Convolving part 1 in progress ..., WindowSize = (20,20)" for y in range(halfSize_20, canvasSize[0]-halfSize_20): for x in range(halfSize_20, canvasSize[1]-halfSize_20): if x%self.stepsize == 0 and y%self.stepsize == 0: item_temp = testcanvas[y-halfSize_20:y+halfSize_20, x-halfSize_20:x+halfSize_20] temp_img = uint8(item_temp) hist = self.hog_20.compute(temp_img) labelhatpatch = self.newELM_20.recall(hist) label_val, conf = distCal(labelhatpatch) self.confidence_20[y-halfSize_20, x-halfSize_20] = conf self.labelmatrix_20[y-halfSize_20, x-halfSize_20] = label_val ############################################################################### ## convolving part 2 fltSize_24 = self.dim[1] halfSize_24 = self.dim[1]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_24, halfSize_24) canvasSize = array(self.testcanvas.shape) + fltSize_24 ## convolving part 1 self.labelmatrix_24 = -1*ones(( canvasSize[0]-fltSize_24, canvasSize[1]-fltSize_24)) self.confidence_24 = -1*ones(( canvasSize[0]-fltSize_24, canvasSize[1]-fltSize_24)) print "> Convolving part 2 in progress ..., WindowSize = (24,24)" for y in range(halfSize_24, canvasSize[0]-halfSize_24): for x in range(halfSize_24, canvasSize[1]-halfSize_24): if x%self.stepsize == 0 and y%self.stepsize == 0: item_temp = testcanvas[y-halfSize_24:y+halfSize_24, x-halfSize_24:x+halfSize_24] temp_img = uint8(item_temp) hist = self.hog_24.compute(temp_img) labelhatpatch = self.newELM_24.recall(hist) label_val, conf = distCal(labelhatpatch) self.confidence_24[y-halfSize_24, x-halfSize_24] = conf self.labelmatrix_24[y-halfSize_24, x-halfSize_24] = label_val ############################################################################### ## convolving part 3 fltSize_28 = self.dim[2] halfSize_28 = self.dim[2]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_28, halfSize_28) canvasSize = array(self.testcanvas.shape) + fltSize_28 ## convolving part 1 self.labelmatrix_28 = -1*ones(( canvasSize[0]-fltSize_28, canvasSize[1]-fltSize_28)) self.confidence_28 = -1*ones(( canvasSize[0]-fltSize_28, canvasSize[1]-fltSize_28)) print "> Convolving part 3 in progress ..., WindowSize = (28,28)" for y in range(halfSize_28, canvasSize[0]-halfSize_28): for x in range(halfSize_28, canvasSize[1]-halfSize_28): if x%self.stepsize == 0 and y%self.stepsize == 0: item_temp = testcanvas[y-halfSize_28:y+halfSize_28, x-halfSize_28:x+halfSize_28] temp_img = uint8(item_temp) hist = self.hog_28.compute(temp_img) labelhatpatch = self.newELM_28.recall(hist) label_val, conf = distCal(labelhatpatch) self.confidence_28[y-halfSize_28, x-halfSize_28] = conf self.labelmatrix_28[y-halfSize_28, x-halfSize_28] = label_val ################################################################################ # get several possible centers clust_temp_20 = where(self.labelmatrix_20 != -1) clust_temp_24 = where(self.labelmatrix_24 != -1) clust_temp_28 = where(self.labelmatrix_28 != -1) len_clust = array([len(clust_temp_20), len(clust_temp_24), len(clust_temp_28)]) self.main_kernel = len_clust.argmax() clust_temp_x = hstack((clust_temp_20[0], clust_temp_24[0], clust_temp_28[0])) clust_temp_y = hstack((clust_temp_20[1], clust_temp_24[1], clust_temp_28[1])) data_coor = vstack((clust_temp_x, clust_temp_y)) center = ones(data_coor.shape[1]) numDigits = 2 for i in range(1, data_coor.shape[1]): coor = [data_coor[0,i], data_coor[1,i]] dist = zeros(i) for j in range(0, i): dist[j] = sqrt((coor[0] - data_coor[0, j])**2 + (coor[1] - data_coor[1, j])**2) if min(dist) <= halfSize_28: center[i] = center[dist.argmin()] else: center[i] = numDigits numDigits += 1 ############################################################################## # make decision self.labelhat = zeros(numDigits-1) self.centroids = zeros((2, numDigits-1)) n_cout = 0 for i in set(center): clusters_idx = find(center, i) k_val = 10*ones(len(center)) for k in clusters_idx: if k < len(clust_temp_20[0]): k_val[k] = self.confidence_20[data_coor[0, k], data_coor[1, k]] elif len(clust_temp_20[0]) <= k < len(clust_temp_20[0]) + len(clust_temp_24[0]): k_val[k] = self.confidence_24[data_coor[0, k], data_coor[1, k]] else: k_val[k] = self.confidence_28[data_coor[0, k], data_coor[1, k]] self.centroids[0, n_cout] = data_coor[0, k_val.argmin()] self.centroids[1, n_cout] = data_coor[1, k_val.argmin()] if k_val.argmin() < len(clust_temp_20[0]): self.labelhat[n_cout] = self.labelmatrix_20[self.centroids[0, n_cout], self.centroids[1, n_cout]] elif len(clust_temp_20[0]) <= k_val.argmin() < len(clust_temp_20[0]) + len(clust_temp_24[0]): self.labelhat[n_cout] = self.labelmatrix_24[self.centroids[0, n_cout], self.centroids[1, n_cout]] else: self.labelhat[n_cout] = self.labelmatrix_28[self.centroids[0, n_cout], self.centroids[1, n_cout]] n_cout += 1 data_var1 = std(self.centroids[0,:]) data_var2 = std(self.centroids[1,:]) if data_var1 > data_var2: self.centroidindex = argsort(self.centroids[0,:]) self.lane_flg = 'v' else: self.centroidindex = argsort(self.centroids[1,:]) self.lane_flg = 'h' n_start = 1 for i in self.centroidindex: print "Prediction", n_start ,", Coordinates=", "(", int(self.centroids[0, i]), ',', int(self.centroids[1, i]), \ "), Label ->", codetable(int(self.labelhat[i])) n_start += 1 return self.labelhat def savePATCH(self): dir_name = 'C:\\dataspace\\Recognition\\' n_start = 1 kearm = self.dim[self.main_kernel]/2 for ii in self.centroidindex: x_cor, y_cor = self.centroids[0,ii], self.centroids[1,ii] if x_cor < kearm: img_coor = self.testcanvas[:int(x_cor+kearm), \ int(y_cor-kearm):int(y_cor+kearm) ] elif y_cor < kearm: img_coor = self.testcanvas[int(x_cor-kearm):int(x_cor+kearm),\ :int(y_cor+kearm) ] elif x_cor > self.testcanvas.shape[0] + kearm: img_coor = self.testcanvas[int(x_cor-kearm):, \ int(y_cor-kearm):int(y_cor+kearm) ] elif y_cor > self.testcanvas.shape[1] + kearm: img_coor = self.testcanvas[int(x_cor-kearm):int(x_cor+kearm), \ :int(y_cor+kearm) ] elif x_cor > self.testcanvas.shape[0] + kearm and y_cor > self.testcanvas.shape[1] + kearm: img_coor = self.testcanvas[int(x_cor-kearm):, int(y_cor-kearm):] elif x_cor < kearm and y_cor < kearm: img_coor = self.testcanvas[:int(x_cor+kearm), :int(y_cor+kearm)] else: img_coor = self.testcanvas[int(x_cor-kearm):int(x_cor+kearm), \ int(y_cor-kearm):int(y_cor+kearm) ] file_name = 'IMG_'+str(n_start)+'_LABEL_'+str(codetable(self.labelhat[ii]))+'.jpg' cv2.imwrite( os.path.join(dir_name, file_name), img_coor) n_start += 1 def visualize(self): fig = plt.figure() if self.lane_flg == 'v': fig.add_subplot(1,4,1) plt.imshow(self.testcanvas) fig.add_subplot(1,4,2) plt.imshow(self.labelmatrix_20) fig.add_subplot(1,4,3) plt.imshow(self.labelmatrix_24) fig.add_subplot(1,4,4) plt.imshow(self.labelmatrix_28) else: fig.add_subplot(4,1,1) plt.imshow(self.testcanvas) fig.add_subplot(4,1,2) plt.imshow(self.labelmatrix_20) fig.add_subplot(4,1,3) plt.imshow(self.labelmatrix_24) fig.add_subplot(4,1,4) plt.imshow(self.labelmatrix_28)
def __init__(self): self.patcharm = 28 self.newELM = ELM(InSize = self.patcharm**2, HidSize=(self.patcharm**2)*10, OutSize=11) self.newELM.load('HYBRID')
class ELMrecogPic(object): def __init__(self, lane = 'v'): self.patcharm = 28 self.outSize = 10 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*20, self.outSize) self.newELM.load('WellTrainedELMWeights') #self.eqfactor = loadtxt('MNISTstatistics_factor.dat') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.raw_labelmatrix = None self.Lane = lane def runtest(self, numDigits, method = 'line', canvasSize0 = [], paddingMode = 0, edgeSize = 0): fltSize = 28 halfSize = 14 displaysize = 5 DigitLane = 10 norm_flag = 0 if method == 'rand': imgcanvas = MNISTCanvasImage('MNIST', canvasSize0) self.testcanvas0, labeltrue = imgcanvas.generateImage(numDigits) elif method == 'line': testcanvas_temp, labeltrue, canvasSize_info = lineMNIST(numDigits, edgeSize, DigitLane, self.Lane, norm_flag) if paddingMode == 1: self.testcanvas0 = zeros(canvasSize0) idx1 = random.randint(0, canvasSize0[0]-canvasSize_info[0]-1) idx2 = random.randint(0, canvasSize0[1]-canvasSize_info[1]-1) self.testcanvas0[idx1:idx1+canvasSize_info[0], idx2:idx2+canvasSize_info[1]] = testcanvas_temp else: self.testcanvas0 = testcanvas_temp canvasSize0 = canvasSize_info ## zeropadding testcanvas = zeropadding(self.testcanvas0, halfSize, halfSize) canvasSize = array(canvasSize0) + fltSize ## convolving part tot_count = (canvasSize[0]-fltSize)*(canvasSize[1]-fltSize) self.labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.raw_labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) print "> Convolving in progress ..." for x in range(halfSize, canvasSize[0]-halfSize): for y in range(halfSize, canvasSize[1]-halfSize): if ( (x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize) %500 == 0: print "> Current convolving index is %d ( %d in total )..."%((x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize+1, tot_count) item_temp = testcanvas[x-halfSize:x+halfSize, y-halfSize:y+halfSize] labelhatpatch = self.newELM.recall(item_temp) self.raw_labelmatrix[x-halfSize,y-halfSize] = labelhatpatch.argmax() self.labelmatrix[x-halfSize,y-halfSize] = fltdot(labelhatpatch) ## clustering clust_temp = where(self.labelmatrix != -1) datamatrix = vstack((clust_temp[0], clust_temp[1])) Con, Iter = True, 0 while Con and Iter < (numDigits * 10): print "> Clustering entry No.",Iter+1, "..." clusters, centroids = kmean(datamatrix.T, numDigits) Con = False for i in range(len(centroids)): for j in range(len(centroids)): # bound detection if i != j and absolute(centroids[i][0] - centroids[j][0]) <= halfSize and \ absolute(centroids[i][1] - centroids[j][1]) <= halfSize : Con = True Iter += 1 if Iter < numDigits * 10: print "Clustering Succeed!" else: print "Clustering failed, latest entry picked" centroids = array(centroids) labelhat0 = zeros(numDigits) countnum = zeros((10,numDigits)) # decicsion making if clusters != []: for i in range(len(clusters)): for j in range(len(clusters[i])): temp = clusters[i][j] distfactor = absolute(temp[0]-centroids[i,0]) + absolute(temp[1]-centroids[i,1]) + 0.01 # euclidean distance countnum [self.labelmatrix[temp[0],temp[1]],i] += 1/distfactor#/self.eqfactor[self.labelmatrix[temp[0],temp[1]]] # equalization labelhat0[i] = countnum[:,i].argmax() else: print "No data generated" labelhat0 = -1 if self.Lane == 'h': centroidindex = argsort(centroids[:,1]) elif self.Lane == 'v': centroidindex = argsort(centroids[:,0]) labelhat = array(labelhat0[centroidindex], dtype='int') self.truecolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.hatcolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) # visualize the centroids and their labels for i in range(len(centroids)): self.truecolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labeltrue[i] for i in range(len(centroids)): self.hatcolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labelhat[i] print "True labels are:", labeltrue print "Hat labels are:", labelhat ''' if (labeltrue == labelhat).tolist().count(False) == len(labeltrue): return 0 else: return (labeltrue == labelhat).tolist().count(False) ''' def visualize(self): if self.Lane == 'h': fig = plt.figure() fig.add_subplot(5,1,1) plt.imshow(self.testcanvas0) fig.add_subplot(5,1,2) plt.imshow(self.raw_labelmatrix) fig.add_subplot(5,1,3) plt.imshow(self.labelmatrix) fig.add_subplot(5,1,4) plt.imshow(self.truecolormatrix) fig.add_subplot(5,1,5) plt.imshow(self.hatcolormatrix) elif self.Lane == 'v': fig = plt.figure() fig.add_subplot(1,5,1) plt.imshow(self.testcanvas0) fig.add_subplot(1,5,2) plt.imshow(self.raw_labelmatrix) fig.add_subplot(1,5,3) plt.imshow(self.labelmatrix) fig.add_subplot(1,5,4) plt.imshow(self.truecolormatrix) fig.add_subplot(1,5,5) plt.imshow(self.hatcolormatrix)
def test(data_set_name, particle_num, max_iter, pretrain=False): if data_set_name == 'WIL': feature_dimension, hidden_size, class_num = WIL_FEATURE_DIMENSION, WIL_HIDDEN_SIZE, WIL_CLASS_NUM cor = 'c' # classification activate_output = True norm_train_data, norm_test_data = WIL_load_data() else: feature_dimension, hidden_size, class_num = BLE_FEATURE_DIMENSION, BLE_HIDDEN_SIZE, BLE_LABEL_DIMENSION cor = 'r' # regression activate_output = False norm_train_data, norm_test_data = BLE_load_data() n_rows, n_cols = cal_wb_mat_size(feature_dimension, hidden_size, class_num) train_data, train_label = norm_train_data[:, : feature_dimension], norm_train_data[:, feature_dimension:] test_data, test_label = norm_test_data[:, : feature_dimension], norm_test_data[:, feature_dimension:] pretrained_wb_mats = None elm_acc_arr = None if pretrain: pretrained_wb_mats = np.empty( (n_rows, n_cols, PRETRAINED_PARTICLE_NUM)) # train elm(s) elm_acc_arr = np.empty((0, 2)) for i in range(PRETRAINED_PARTICLE_NUM): elm = ELM(input_num=feature_dimension, hidden_num=hidden_size, output_num=class_num, cor=cor) elm.fit(train_data, train_label) elm_train_acc = elm.test_acc(data=train_data, label=train_label) elm_test_acc = elm.test_acc(data=test_data, label=test_label) elm_acc_arr = np.append(elm_acc_arr, [[elm_train_acc, elm_test_acc]], axis=0) if cor == 'c': print( 'pre-training ELM %02d, train acc = %.2f%%, test acc = %.2f%%' % (i, elm_train_acc * 100, elm_test_acc * 100)) else: print( 'pre-training ELM %02d, train mean error = %.2f, test mean error = %.2f' % (i, elm_train_acc, elm_test_acc)) pretrained_wb_mats[:, :, i] = encode_pretrained_weights( elm.input_weights, elm.bias_list, elm.output_weights, [0] * class_num) # print total avg/max/min train/test acc for elm(s) if cor == 'c': print('train acc -- avg: %.2f%%, max: %.2f%%, min: %.2f%%' % (mean(elm_acc_arr[:, 0]) * 100, max(elm_acc_arr[:, 0]) * 100, min(elm_acc_arr[:, 0]) * 100)) print('test acc -- avg: %.2f%%, max: %.2f%%, min: %.2f%%' % (mean(elm_acc_arr[:, 1] * 100), max(elm_acc_arr[:, 1]) * 100, min(elm_acc_arr[:, 1]) * 100)) else: print('train mean error -- avg: %.2f, max: %.2f, min: %.2f' % (mean(elm_acc_arr[:, 0]), max( elm_acc_arr[:, 0]), min(elm_acc_arr[:, 0]))) print('test mean error -- avg: %.2f, max: %.2f, min: %.2f' % (mean(elm_acc_arr[:, 1]), max( elm_acc_arr[:, 1]), min(elm_acc_arr[:, 1]))) # create FPG (with pretrained_wb_mats if it is not None) fpg = FPG(particle_num=particle_num, input_num=feature_dimension, hidden_num=hidden_size, output_num=class_num, activate_output=activate_output, cor=cor, pretrained_wb_mats=pretrained_wb_mats) # train fpg gbest_particle = fpg.fit(norm_train_data, max_iter=max_iter, test_data_list=norm_test_data) train_acc = fpg.test_acc(norm_train_data, particle=gbest_particle) test_acc = fpg.test_acc(norm_test_data, particle=gbest_particle) if cor == 'c': print('train acc = %.3f%%' % (train_acc * 100)) print('test acc = %.3f%%' % (test_acc * 100)) else: print('train mean error = %.3f' % train_acc) print('test mean error = %.3f' % test_acc) train_result = copy.deepcopy(fpg.history) train_result[ 'elm_acc_arr'] = elm_acc_arr if pretrain else None # add pretrain elm acc info return train_result
import numpy as np from tensorflow.python.keras import datasets from ELM import ELM (X_train, Y_train), (X_test, Y_test) = datasets.mnist.load_data() print(X_train.shape, Y_train.shape, X_test.shape, Y_test.shape) x = ELM(500) x.fit(X_train, Y_train) y = x.predict(X_test) correct = 0 total = y.shape[0] for i in range(total): predicted = np.argmax(y[i]) test = np.argmax(Y_test[i]) correct = correct + (1 if predicted == test else 0) print('Accuracy: {:f}'.format(correct / total))
class ELMrecogPic_real_where(object): def __init__(self): self.patcharm = [576, 900, 1296] #1296 #900 self.Outsize = [34, 2] self.dim = [20, 24, 28] self.newELM_20 = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize[0]) self.newELM_24 = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize[0]) self.newELM_28 = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize[0]) self.newELM_20_binary = ELM(self.patcharm[0], self.patcharm[0]*10, self.Outsize[1]) self.newELM_24_binary = ELM(self.patcharm[1], self.patcharm[1]*10, self.Outsize[1]) self.newELM_28_binary = ELM(self.patcharm[2], self.patcharm[2]*10, self.Outsize[1]) self.newELM_20.load('/home/houyimeng/dataspace/weights/harbour34_20') self.newELM_24.load('/home/houyimeng/dataspace/weights/harbour34_24') self.newELM_28.load('/home/houyimeng/dataspace/weights/harbour34_28') self.newELM_20_binary.load('/home/houyimeng/dataspace/weights/harbour2_20') self.newELM_24_binary.load('/home/houyimeng/dataspace/weights/harbour2_24') self.newELM_28_binary.load('/home/houyimeng/dataspace/weights/harbour2_28') self.hog_20 = cv2.HOGDescriptor((self.dim[0], self.dim[0]), (8,8), (4,4), (4,4), 9) self.hog_24 = cv2.HOGDescriptor((self.dim[1], self.dim[1]), (8,8), (4,4), (4,4), 9) self.hog_28 = cv2.HOGDescriptor((self.dim[2], self.dim[2]), (8,8), (4,4), (4,4), 9) def runtest(self): # load tested image crop_rg = [0.5, 0.6, 0.25, 0.8] scaleFactor = 0.6 path = '/home/houyimeng/dataspace/images/canvas/' dirs = os.listdir(path) for item in dirs: img = cv2.imread(path+item, 0) img = img[int(img.shape[0]*crop_rg[2]):int(img.shape[0]*crop_rg[3]), int(img.shape[1]*crop_rg[0]):int(img.shape[1]*crop_rg[1])] self.testcanvas = cv2.resize(img, (0,0), fx=scaleFactor, fy=scaleFactor) print "The size of the testcanvas:", self.testcanvas.shape cv2.imwrite('greyscale.png', img) stepsize = 2 ############################################################################### fltSize_20 = self.dim[0] halfSize_20 = self.dim[0]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_20, halfSize_20) canvasSize = array(self.testcanvas.shape) + fltSize_20 ## convolving part 1 tot_count = (canvasSize[0]-fltSize_20)*(canvasSize[1]-fltSize_20) labelmatrix_20_raw = -1*ones(( canvasSize[0]-fltSize_20, canvasSize[1]-fltSize_20)) n_cout = 0 print "> Convolving part 1 in progress ..., WindowSize = (20,20)" for y in range(halfSize_20, canvasSize[0]-halfSize_20): for x in range(halfSize_20, canvasSize[1]-halfSize_20): n_cout += 1 if n_cout%2000 == 0: print "> Current convolving index is %d ( %d in total )..."%(n_cout, tot_count) if x%stepsize == 0 and y%stepsize == 0: item_temp = testcanvas[y-halfSize_20:y+halfSize_20, x-halfSize_20:x+halfSize_20] temp_img = uint8(item_temp) hist = self.hog_20.compute(temp_img) labelhatpatch = self.newELM_20_binary.recall(hist) label_val = distCal_bin(labelhatpatch) if label_val == 1: labelhatpatch_34 = self.newELM_20.recall(hist) label_val_34 = distCal_normal(labelhatpatch_34) labelmatrix_20_raw[y-halfSize_20, x-halfSize_20] = label_val_34 else: labelmatrix_20_raw[y-halfSize_20, x-halfSize_20] = label_val ############################################################################### ## convolving part 2 fltSize_24 = self.dim[1] halfSize_24 = self.dim[1]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_24, halfSize_24) canvasSize = array(self.testcanvas.shape) + fltSize_24 ## convolving part 1 tot_count = (canvasSize[0]-fltSize_24)*(canvasSize[1]-fltSize_24) labelmatrix_24_raw = -1*ones(( canvasSize[0]-fltSize_24, canvasSize[1]-fltSize_24)) n_cout = 0 print "> Convolving part 2 in progress ..., WindowSize = (24,24)" for y in range(halfSize_24, canvasSize[0]-halfSize_24): for x in range(halfSize_24, canvasSize[1]-halfSize_24): n_cout += 1 if n_cout%2000 == 0: print "> Current convolving index is %d ( %d in total )..."%(n_cout, tot_count) if x%stepsize == 0 and y%stepsize == 0: item_temp = testcanvas[y-halfSize_24:y+halfSize_24, x-halfSize_24:x+halfSize_24] temp_img = uint8(item_temp) hist = self.hog_24.compute(temp_img) labelhatpatch = self.newELM_24_binary.recall(hist) label_val = distCal_bin(labelhatpatch) if label_val == 1: labelhatpatch_34 = self.newELM_24.recall(hist) label_val_34 = distCal_normal(labelhatpatch_34) labelmatrix_24_raw[y-halfSize_24, x-halfSize_24] = label_val_34 else: labelmatrix_24_raw[y-halfSize_24, x-halfSize_24] = label_val ############################################################################### ## convolving part 3 fltSize_28 = self.dim[2] halfSize_28 = self.dim[2]/2 ## zeropadding testcanvas = zeropadding(self.testcanvas, halfSize_28, halfSize_28) canvasSize = array(self.testcanvas.shape) + fltSize_28 ## convolving part 1 tot_count = (canvasSize[0]-fltSize_28)*(canvasSize[1]-fltSize_28) labelmatrix_28_raw = -1*ones(( canvasSize[0]-fltSize_28, canvasSize[1]-fltSize_28)) n_cout = 0 print "> Convolving part 3 in progress ..., WindowSize = (28,28)" for y in range(halfSize_28, canvasSize[0]-halfSize_28): for x in range(halfSize_28, canvasSize[1]-halfSize_28): n_cout += 1 if n_cout%2000 == 0: print "> Current convolving index is %d ( %d in total )..."%(n_cout, tot_count) if x%stepsize == 0 and y%stepsize == 0: item_temp = testcanvas[y-halfSize_28:y+halfSize_28, x-halfSize_28:x+halfSize_28] temp_img = uint8(item_temp) hist = self.hog_28.compute(temp_img) labelhatpatch = self.newELM_28_binary.recall(hist) label_val = distCal_bin(labelhatpatch) if label_val == 1: labelhatpatch_34 = self.newELM_28.recall(hist) label_val_34 = distCal_normal(labelhatpatch_34) labelmatrix_28_raw[y-halfSize_28, x-halfSize_28] = label_val_34 else: labelmatrix_28_raw[y-halfSize_28, x-halfSize_28] = label_val ################################################################################ # get several possible centers self.labelmatrix_20 = massflt(labelmatrix_20_raw, 23) self.labelmatrix_24 = massflt(labelmatrix_24_raw, 23) self.labelmatrix_28 = massflt(labelmatrix_28_raw, 23) return self.labelmatrix_20, self.labelmatrix_24, self.labelmatrix_28, self.testcanvas ############################################################################# def visualize(self): plt.figure(1) plt.imshow(self.testcanvas) plt.figure(2) plt.imshow(self.labelmatrix_20) plt.figure(3) plt.imshow(self.labelmatrix_24) plt.figure(4) plt.imshow(self.labelmatrix_28)
def __init__(self, height, width, HidSize, OutSize, OPIUM_type, \ genWeights_type, data, q = 200, act_fun = np.tanh, H=1,\ randomPrec=0, actPrec=1, linearPrec=0, callPrec=1, fixedTrain = True, fixedTest=True): if genWeights_type != 'rf-c': raise Exception("Wrong model called") print ">>> Initialing FPGA-RF-C-ELM model <<<" smtic = time.time() InSize = height*width ELM.__init__(self, InSize, HidSize, OutSize, OPIUM_type, genWeights_type, act_fun) self.H = H self.randomPrec = randomPrec self.actPrec = actPrec self.linearPrec = linearPrec self.callPrec = callPrec self.fixedTrain = fixedTrain self.fixedTest = fixedTest #self.LinearWeight = np.random.uniform(-self.H, self.H, size=(self.OutSize, self.HidSize)) #data = self.__normalizeTest(data) N = data.shape[0] self.RandomWeight = np.zeros((self.HidSize, self.InSize)) self.b = np.zeros((self.HidSize, 1)) for i in range(self.HidSize): d1 = data[np.random.randint(0, N), :] d2 = data[np.random.randint(0, N), :] d = d1 + d2 self.RandomWeight[i,:] = d/np.sqrt(np.sum(np.power(d, 2))) self.b[i] = np.random.uniform(0, 1) F = np.zeros((self.HidSize, self.InSize)) for i in range(self.HidSize): uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) while size < q: uli, ulj, bri, brj = self.__genIndex(height, width, 3) size = (bri + 1 - uli)*(brj + 1 - ulj) Fi = np.zeros((height, width)) Fi[uli:bri+1, ulj:brj+1] = 1 F[i, :] = Fi.flatten() self.RandomWeight *= F self.RandomWeight /= np.sqrt(np.sum(np.power(self.RandomWeight, 2), \ axis=1)).reshape(self.HidSize, 1) if self.randomPrec == 0: print ">>>Binary random weights" self.RandomWeight = self.binarization(self.RandomWeight) self.b = self.binarization(self.b) else: print ">>>Fixed point precise random weights: ", self.randomPrec, "points after zero" self.RandomWeight = around(self.RandomWeight, N_bits=self.randomPrec) self.b = around(self.b, N_bits=self.randomPrec) if self.linearPrec == 0: print ">>>Binary linear weights" self.Wb = self.binarization(self.LinearWeight) else: print ">>>Fixed point precise linear weights: ", self.linearPrec, "points after zero" self.Wb = around(self.LinearWeight, N_bits=self.linearPrec) if self.fixedTrain: print ">>>Fixed point precise linear weight will be used in training\ phase: ", self.linearPrec, "points after zero" else: print ">>>Real value linear weights will be used in training phase" if self.fixedTest: print ">>>Linear weights will be fixed point precise weights \ in testing phase: ", self.linearPrec, "points after zero" else: print ">>>Linear weights will be real value weights in testing phase" smtoc = time.time() print "FPGA-RF-C-ELM Initialization complete, time cost: %3.2f seconds"%(smtoc-smtic)
class ELMrecogPic_beta2(object): def __init__(self, lane = 'v'): self.patcharm = 28 self.Outsize = 10 self.newELM = ELM(self.patcharm**2, (self.patcharm**2)*10, self.Outsize) self.newELM.load('C:\\dataspace\\weights\\') self.truecolormatrix = None self.hatcolormatrix = None self.testcanvas0 = None self.labelmatrix = None self.raw_labelmatrix = None self.Lane = lane def runtest(self, numDigits): fltSize = 28 halfSize = 14 datasource = 'binary' # 'synthesis' or others if datasource == 'MNIST': self.testcanvas0, labeltrue, canvasSize0 = lineMNIST(numDigits) else: self.testcanvas0, labeltrue, canvasSize0 = lineIMAGE(numDigits, datasource) ## zeropadding testcanvas = zeropadding(self.testcanvas0, halfSize, halfSize) canvasSize = array(canvasSize0) + fltSize ## convolving part tot_count = (canvasSize[0]-fltSize)*(canvasSize[1]-fltSize) self.labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.raw_labelmatrix = zeros((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) print "> Convolving in progress ..." for x in range(halfSize, canvasSize[0]-halfSize): for y in range(halfSize, canvasSize[1]-halfSize): if ( (x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize) %500 == 0: print "> Current convolving index is %d ( %d in total )..."%((x-halfSize)*(canvasSize[1]-fltSize)+y-halfSize+1, tot_count) item_temp = testcanvas[x-halfSize:x+halfSize, y-halfSize:y+halfSize] labelhatpatch = self.newELM.recall(item_temp) self.labelmatrix[x-halfSize,y-halfSize] = distCal(labelhatpatch) ## clustering clust_temp = where(self.labelmatrix != -1) datamatrix = vstack((clust_temp[0], clust_temp[1])) Con, Iter = True, 0 separateWidth = 21 while Con and Iter < (numDigits * 10): print "> Clustering entry No.",Iter+1, "..." clusters, centroids = kmean(datamatrix.T, numDigits) Con = False for i in range(len(centroids)): for j in range(len(centroids)): # bound detection if i != j and absolute(centroids[i][0] - centroids[j][0]) <= separateWidth and \ absolute(centroids[i][1] - centroids[j][1]) <= separateWidth : Con = True Iter += 1 if Iter < numDigits * 10: print "Clustering Succeed!" else: print "Clustering failed, latest entry picked" centroids = array(centroids) labelhat0 = zeros(numDigits) countnum = zeros((self.Outsize, numDigits)) # decicsion making if clusters != []: for i in range(len(clusters)): for j in range(len(clusters[i])): temp = clusters[i][j] distfactor = absolute(temp[0]-centroids[i,0]) + absolute(temp[1]-centroids[i,1]) + 0.01 # euclidean distance countnum [self.labelmatrix[temp[0],temp[1]],i] += 1/distfactor # equalization labelhat0[i] = countnum[:,i].argmax() else: print "No data generated" labelhat0 = -1 if self.Lane == 'h': centroidindex = argsort(centroids[:,1]) elif self.Lane == 'v': centroidindex = argsort(centroids[:,0]) labelhat = array(labelhat0[centroidindex], dtype='int') self.truecolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) self.hatcolormatrix = (-1)*ones((canvasSize[0]-fltSize, canvasSize[1]-fltSize)) displaysize = 2 # visualize the centroids and their labels for i in range(len(centroids)): self.truecolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labeltrue[i] for i in range(len(centroids)): self.hatcolormatrix [round(centroids[i][0])-displaysize:round(centroids[i][0])+displaysize, \ round(centroids[i][1])-displaysize:round(centroids[i][1])+displaysize ] = labelhat[i] print "True labels are:", labeltrue print "Hat labels are:", labelhat return self.labelmatrix def visualize(self): if self.Lane == 'h': fig = plt.figure() fig.add_subplot(4,1,1) plt.imshow(self.testcanvas0) fig.add_subplot(4,1,2) plt.imshow(self.labelmatrix0) fig.add_subplot(4,1,3) plt.imshow(self.truematrix) fig.add_subplot(4,1,4) plt.imshow(self.hatcolormatrix) elif self.Lane == 'v': fig = plt.figure() fig.add_subplot(1,4,1) plt.imshow(self.testcanvas0) fig.add_subplot(1,4,2) plt.imshow(self.labelmatrix) fig.add_subplot(1,4,3) plt.imshow(self.truecolormatrix) fig.add_subplot(1,4,4) plt.imshow(self.hatcolormatrix)