Example #1
0
class Cal_ce:
    def __init__(self, output_train_sub):
        self.ce = Cross_Entropy(num_class=10, num_bin=5)

    def ce_cal(self, each_out, label):
        each_out = each_out.reshape(-1, 1)
        return self.ce.KMeans_Cross_Entropy(each_out, label)
def featureSelector(X,Y):
    '''
    Select 50% features that has the lowest value of cross entropy
    input:
    X: output from max-pooling. Typically it has (numOfImages,pooled dimension, pooled dimension, K) size. e.g., (50000,14,14,41) at unit 1
    Y: labels corresponding to X
    output:
    selectedFeatures: selected 50% features 
    selectedFeaturesIndeces: indeces of the selected features 
    '''
    
    reshapedPooledX = X.reshape((len(X),-1))
    
    crossEntropyObj = Cross_Entropy(num_class = 10, num_bin = 10) 
    crossEntropyValStorage = np.zeros(reshapedPooledX.shape[-1])
    for storageIndex in range(reshapedPooledX.shape[-1]): #12 minutes for 100 images and 8036(14*14*41) dimension
        crossEntropyValStorage[storageIndex] = crossEntropyObj.KMeans_Cross_Entropy(reshapedPooledX[:,storageIndex].reshape(-1,1), Y)
    print("calculation is done \n")
    
    sortedIndex = np.argsort(crossEntropyValStorage) #return indeces that would be the indeces when the array is sorted
    numOfNs = int(sortedIndex.shape[0]/2) #select 50 persent of lowest values
    #numOfNs = int(1000) #Select 1000 lowest features 
    selectedFeaturesIndex = sortedIndex[0:numOfNs]
    
    
    numOfImgs = int(reshapedPooledX.shape[0]) #e.g.50000 images
    reducedFeatureImgs = np.zeros((numOfImgs,numOfNs))
    
    #Select features for each image
    for imgIndex in range(numOfImgs):
        for selectedFeatureCount in range(numOfNs):
            curr_selectedFeatureIndex = selectedFeaturesIndex[selectedFeatureCount]
            reducedFeatureImgs[imgIndex,selectedFeatureCount] = reshapedPooledX[imgIndex,curr_selectedFeatureIndex]
        
    return selectedFeaturesIndex, reducedFeatureImgs, numOfNs
Example #3
0
def cal_CE_layer(features, trainLabel):
    kernelNum = features.shape[1]
    #Compute the Cross_Entropy of Layer 1
    CE = []
    for i in range(kernelNum):
        tempData = features[:, i]
        tempData = tempData.reshape((len(tempData), -1))
        ce = Cross_Entropy(num_class=10, num_bin=10)
        ce_value = ce.KMeans_Cross_Entropy(tempData, trainLabel)
        CE.append(ce_value)
    return CE
def extract_feature(re_output, re_output_test, N, train_label):
    my_CrossEntropy = Cross_Entropy(num_class=10, num_bin=5)
    feat_ce = np.zeros(re_output.shape[-1])
    print("re_output.shape[-1]", re_output.shape[-1])
    print("re_output_test.shape[-1]", re_output_test.shape[-1])

    for k in range(re_output.shape[-1]):
        feat_ce[k] = my_CrossEntropy.KMeans_Cross_Entropy(
            re_output[:, k].reshape(-1, 1), train_label)
        # print(" --> KMeans cross entropy: %s" % str(feat_ce[k]))

    sorted_index = np.argsort(feat_ce)  # increasing
    final_output = np.zeros([re_output.shape[0], N], np.float32)
    print("re_output.shape[0]", re_output.shape[0])
    final_output_test = np.zeros([re_output_test.shape[0], N], np.float32)
    print("re_output_test.shape[0]", re_output_test.shape[0])

    for i in range(0, N):
        final_output[:, i] = re_output[:, sorted_index[i]]
        final_output_test[:, i] = re_output_test[:, sorted_index[i]]

    return final_output, final_output_test
Example #5
0
def feature_selection(layer1_o, layer2_o, layer3_o, Y):
    x_train1 = layer1_o.reshape(len(layer1_o), -1)
    x_train2 = layer2_o.reshape(len(layer2_o), -1)
    x_train3 = layer3_o.reshape(len(layer3_o), -1)
    input_list = [x_train1, x_train2, x_train3]
    res_list = []
    for i in range(3):
        print("Currently doing feature selection for hop {}".format(i+1))
        x_train = input_list[i]
        
        # Calculate loss for each features
        ce = Cross_Entropy(num_class=10, num_bin=5)
        feat_ce = np.zeros(x_train.shape[-1])
        start = time.time()
        for i in range(x_train.shape[-1]): #35280 features:
            if i % 1000 == 999:
                print("{}/{}".format(i+1, x_train.shape[1]))
            temp_vec = x_train1[:,i]
            feat_ce[i] = ce.KMeans_Cross_Entropy(x_train[:,i].reshape(-1,1), Y)
        end = time.time()
        print("time elapse: {}s".format(end-start))
        
        # Order the feature by its cross entropy loss
        feat_ce_tuplelist = []
        [feat_ce_tuplelist.append((feat_ce[i],i)) for i in range(x_train.shape[-1])]
        feat_ce_tuplelist.sort()
        
        # Select top 1000 features
        if len(feat_ce_tuplelist) > 1000:
            feat_ce_tuplelist_sel = feat_ce_tuplelist[0:1000]
            feat_num_sel = []
            [feat_num_sel.append(feat_ce_tuplelist_sel[i][1]) for i in range(1000)]
            x_train_sel = x_train[:,feat_num_sel]
            res_list.append(x_train_sel)
        else:
            res_list.append(x_train)
            
    return res_list[0], res_list[1], res_list[2]
    for i in range(0, 3):
        output[i] = skimage.measure.block_reduce(output[i], (1, 2, 2, 1),
                                                 np.max)

    # reshaping
    for i in range(0, 3):
        output[i] = output[i].reshape(
            10, output[i].shape[1] * output[i].shape[2] * output[i].shape[3])

    # ------------------ cross entropy ------------------ #
    ce = Cross_Entropy(num_class=10, num_bin=5)

    entropy = np.zeros(output[2].shape[1])
    rank = []
    for j in range(0, output[2].shape[1]):
        entropy[j] = ce.KMeans_Cross_Entropy(output[2][:, j].reshape(-1, 1),
                                             y_train[0:10])
        rank = np.argsort(-entropy)
    output[2] = output[2][:, rank[0:15]]

    # ------------------ LAG ------------------ #

    lag = LAG(encode='distance',
              num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
              alpha=10,
              learner=myLLSR(onehot=False))

    # ------------------ Module 3 ------------------#

    print('Time - %2d sec' % (time.time() - start_time))
    print("------- TRAINING FINISHED -------\n")

from cross_entropy import Cross_Entropy

X_reshape0 = output[0].reshape(len(output[0]), -1,output[0].shape[-1])
X_reshape1 = output[1].reshape(len(output[1]), -1,output[1].shape[-1])
X_reshape2 = output[2].reshape(len(output[2]), -1,output[2].shape[-1])

kce = Cross_Entropy(num_class=10, num_bin=5)

kce_fd0 = np.zeros(X_reshape0.shape[-1])
kce_fd1 = np.zeros(X_reshape1.shape[-1])
kce_fd2 = np.zeros(X_reshape2.shape[-1])

for k in range(X_reshape0.shape[-1]):
        kce_fd0[k] = kce.KMeans_Cross_Entropy(X_reshape0[:,:,k].reshape(X_reshape0.shape[0],-1), y_train[0:nti])
print('finished 0')

for k in range(X_reshape1.shape[-1]):
        kce_fd1[k] = kce.KMeans_Cross_Entropy(X_reshape1[:,:,k].reshape(X_reshape1.shape[0],-1), y_train[0:nti])
print('finished 1')
for k in range(X_reshape2.shape[-1]):
        kce_fd2[k] = kce.KMeans_Cross_Entropy(X_reshape2[:,:,k].reshape(X_reshape2.shape[0],-1), y_train[0:nti])
print('finished 2')  


fd0 = []
fd1 = []
fd2 = []
for k in range(X_reshape0.shape[-1]):
  fd0.append([k, kce_fd0[k]])
#Cross Entropy1
from cross_entropy import Cross_Entropy
temp1 = output_1.reshape((len(output_1), -1))
print("input feature shape is: %s" % str(temp1.shape))
fit_data_train, data_val, fit_label_train, label_val = train_test_split(
    data_train,
    label_train,
    test_size=0.8,
    random_state=0,
    stratify=label_train)
CE = Cross_Entropy(num_class=10, num_bin=5)
output1Feature = np.zeros((temp1.shape[-1]))

for i in range((temp1.shape[-1])):
    output1Feature[i] = CE.KMeans_Cross_Entropy(temp1[:, i].reshape(-1, 1),
                                                label_train)
    print("--> KMeans CE: %s" % str(output1Feature[i]))
print("------DONE-----\n")

#Cross Entropy2
from cross_entropy import Cross_Entropy
temp2 = output_2.reshape((len(output_2), -1))
print("input feature shape is: %s" % str(temp2.shape))
fit_data_train, data_val, fit_label_train, label_val = train_test_split(
    data_train,
    label_train,
    test_size=0.8,
    random_state=0,
    stratify=label_train)
CE = Cross_Entropy(num_class=10, num_bin=5)
output2Feature = np.zeros((temp2.shape[-1]))
Example #9
0
output_3 = np.concatenate((output1[2],output2[2],output3[2],output4[2],output5[2]))
print(output_1.shape, output_2.shape, output_3.shape)
# print(output[0].shape, output[1].shape, output[2].shape)
print("------- DONE -------\n")

#Cross Entropy1
from cross_entropy import Cross_Entropy
Xtrain1,Xtest1, Ytrain1 , Ytest1 = train_test_split(output_1, label_train, test_size = 0.9375, random_state = 0, stratify = label_train)
temp1 = Xtrain1.reshape((len(Xtrain1), -1))
print("input feature shape is: %s" %str(temp1.shape))
fit_data_train, data_val, fit_label_train, label_val = train_test_split(data_train, label_train, test_size = 0.8, random_state = 0, stratify = label_train) 
CE = Cross_Entropy(num_class = 10, num_bin = 5)
output1Feature = np.zeros((temp1.shape[-1]))

for i in range((temp1.shape[-1])):
  output1Feature[i] = CE.KMeans_Cross_Entropy(temp1[:,i].reshape(-1,1), Ytrain1)
  print("--> KMeans CE: %s"%str(output1Feature[i]))
print("------DONE-----\n")

#Cross Entropy2
from cross_entropy import Cross_Entropy
Xtrain2,Xtest2, Ytrain2 , Ytest2 = train_test_split(output_2, label_train, test_size = 0.9375, random_state = 0, stratify = label_train)
temp2 = Xtrain2.reshape((len(Xtrain2), -1))
print("input feature shape is: %s" %str(temp2.shape))
fit_data_train, data_val, fit_label_train, label_val = train_test_split(data_train, label_train, test_size = 0.8, random_state = 0, stratify = label_train) 
CE = Cross_Entropy(num_class = 10, num_bin = 5)
output2Feature = np.zeros((temp2.shape[-1]))

for i in range((temp2.shape[-1])):
  output2Feature[i] = CE.KMeans_Cross_Entropy(temp2[:,i].reshape(-1,1), Ytrain2)
  print("--> KMeans CE: %s"%str(output2Feature[i]))
Example #10
0
                               (num_train_later * 10, 32, 32, 3))

x_train_later = x_train_later.astype('float32')
x_train_later /= 255

output_train = p2.transform(x_train_later)

Ns = 1500
feature_set = []

for each in output_train:
    each = each.reshape(each.shape[0], -1)
    ce = Cross_Entropy(num_class=10, num_bin=5)
    feat_ce = np.zeros(each.shape[-1])
    for k in range(each.shape[-1]):
        feat_ce[k] = ce.KMeans_Cross_Entropy(each[:, k].reshape(-1, 1),
                                             y_train_later)
        # print(" --> KMeans ce: %s"%str(feat_ce[k]))
    feature_set.append(
        np.argpartition(feat_ce, np.min(
            (Ns, each.shape[-1] - 1)))[:np.min((Ns, each.shape[-1]))])
    print("------- DONE -------\n")

f = open('feature_set.pkl', 'wb')
pickle.dump(feature_set, f)
f.close()

print()
print('feature_set saved!')
print()

# f = open('feature_set.pkl', 'rb')