├── .idea └── vcs.xml ├── Autocoder.py ├── Bilinear_feature.py ├── CNN_facial_keypoint_detection_ex.ipynb ├── DataToolBox.py ├── Ensemble.py ├── FisherLDA.py ├── GAN ├── DCGAN.py ├── WGAN.py └── starGAN.py ├── Imbalance ├── DataPreprocessing.py └── SMOTE.py ├── KerasLayer ├── QAoutputBlock.py ├── context2query_attention.py ├── gate_attention.py ├── layer_dropout.py ├── multihead_attention.py ├── multihead_attention_bysu.py ├── position_embedding.py ├── position_embedding_bysu.py ├── simple_attention.py ├── simple_attention_permute.py └── very_simple_attention.py ├── MatMHKS.py ├── NetModel ├── C3D.py ├── CNN+RNN.py ├── Hourglass.py ├── MoblieNetV2.py ├── QANet_keras.py ├── QANet_tensorflow │ ├── QANet_model.py │ ├── layers.py │ ├── test.py │ ├── train.py │ └── util.py ├── SAN.py ├── SAN │ ├── SAN_layers.py │ ├── SAN_model.py │ └── train_SAN.py ├── SegNet.py └── SoundNet.py ├── README.md ├── generator ├── generate_arrays_from_file.py ├── generator_for_imbalance.py └── image_reformance_generator.py ├── keras_api ├── ExponentialMovingAverage.py └── keras2tf.py ├── latex ├── bayes_analysis │ ├── bayesiantests.py │ └── main.py ├── build_latex_table.ipynb └── get_best_results.ipynb ├── loss_function ├── ArcFace_loss.py ├── center_loss.py ├── contrastive_loss.py ├── focal_loss.py ├── huber_loss.py ├── island_loss.py ├── spearman_loss.py ├── sphere_loss.py ├── tensorflow_loss_test.ipynb └── triplet_loss.py ├── metrics ├── calccc.py └── tensorflow_keras_metrics.py ├── multi_pooling.py ├── multi_thread.py ├── soft_attention ├── keras_version │ ├── attention_model.png │ ├── attention_model.py │ ├── attention_weights_model.py │ └── f_weights.png └── tf_version │ └── soft_attention_LSTM.ipynb ├── tensorflow_api ├── multi_gpu_training.py ├── read_record.py ├── save_record.py └── training_validating_for_tf.py └── xgb_lgb └── xgbtest.py /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Autocoder.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.models import * 3 | 4 | #hid_node is a list 5 | #if the last_activation_func is sigmoid,x must be minmax normalized 6 | def autoencoder(x,hid_node,activation_func='relu',last_activation_func='sigmoid',batch_size=64,epochs=150): 7 | input_dim=x.shape[1] 8 | input_data = Input(shape=(input_dim,)) 9 | # Encoder 10 | encoder_out = Dense(hid_node[0], activation=activation_func)(input_data) 11 | for i in range(1,len(hid_node)): 12 | encoder_out = Dense(hid_node[i], activation=activation_func)(encoder_out) 13 | #Decoder 14 | for i in range(len(hid_node)-2,-1,-1): 15 | encoder_out = Dense(hid_node[i], activation=activation_func)(encoder_out) 16 | decoder_out = Dense(input_dim, activation=last_activation_func)(encoder_out) 17 | #Model 18 | encoder = Model(inputs=input_data, outputs=encoder_out) 19 | encoder_decoder=Model(inputs=input_data, outputs=decoder_out) 20 | encoder_decoder.compile(loss='mse', optimizer='adam') 21 | encoder_decoder.fit(x=x, y=x, batch_size=batch_size, epochs=epochs,shuffle=True) 22 | 23 | return encoder,encoder_decoder 24 | 25 | 26 | -------------------------------------------------------------------------------- /Bilinear_feature.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | #feature1:n1*n2,feature2:n1*n3,return bilinear_feature:n1*(n2*n3) 4 | def bilinear_fun(feature1,feature2,method='out_product'): 5 | bilinear_feature=np.zeros((feature1.shape[0],feature1.shape[1]*feature2.shape[1])) 6 | if method=='out_product': 7 | for i in range(feature1.shape[0]): 8 | f1=np.reshape(feature1[i,:],(-1,1)) 9 | f2=np.reshape(feature2[i,:],(1,-1)) 10 | bilinear_feature[i,:]=np.reshape(np.dot(f1,f2),(1,-1))[0] 11 | else: 12 | print('Can\'t do '+method) 13 | return bilinear_feature -------------------------------------------------------------------------------- /DataToolBox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | from sklearn.metrics import roc_auc_score 4 | 5 | #minmax normalization 6 | def minmax_norm(X): 7 | return (X-np.min(X))/(np.max(X)-np.min(X)) 8 | 9 | #z-score normalization 10 | def zscore_norm(X): 11 | return (X-np.mean(X))/np.std(X) 12 | 13 | #计算acc 14 | def get_acc(real_label,predict_label): 15 | return len(np.nonzero(real_label-predict_label==0)[0])/len(real_label) 16 | 17 | #计算auc 18 | def get_auc(real_label, scores): 19 | return roc_auc_score(real_label, scores) 20 | 21 | #计算macc,返回tp,tn,macc 22 | def get_macc(real_label, predict_label): 23 | n1 = len(np.nonzero(real_label == 1)[0]) 24 | n2 = len(np.nonzero(real_label == 0)[0]) 25 | tp_temp = sum(predict_label[np.nonzero(real_label == 1)[0]] == 1) 26 | tn_temp = sum(predict_label[np.nonzero(real_label == 0)[0]] == 0) 27 | tp = tp_temp / n1 28 | tn = tn_temp / n2 29 | m_acc = (tp + tn) / 2 30 | return tp,tn,m_acc 31 | 32 | #计算f1score 33 | #默认少数类为1 34 | def f1score(real_label, predict_label,min_label=1): 35 | n1 = len(np.nonzero(real_label == min_label)[0]) 36 | tp_temp = sum(predict_label[np.nonzero(real_label == min_label)[0]] == min_label) 37 | recall=tp_temp/n1 38 | precision=tp_temp/len(np.nonzero(predict_label==min_label)[0]) 39 | return 2*(recall*precision)/(recall+precision) 40 | 41 | # 二分类,返回[多数类,少数类,多数类类标,少数类类标] 可返回index 42 | def divide_data(data, label,return_index=False): 43 | labels= np.unique(label) 44 | n1 = len(np.nonzero(label == labels[0])[0]) 45 | n2 = len(np.nonzero(label == labels[1])[0]) 46 | # 判断少数类的类标号 47 | if n1 > n2: 48 | less_label = labels[1] 49 | much_label = labels[0] 50 | else: 51 | less_label = labels[0] 52 | much_label = labels[1] 53 | if return_index: 54 | data_much_index = np.nonzero(label != less_label)[0] 55 | data_less_index = np.nonzero(label == less_label)[0] 56 | return data_much_index, data_less_index, much_label, less_label 57 | 58 | # 分离多数类和少数类 59 | data_much = data[np.nonzero(label!=less_label)[0],:] 60 | data_less = data[np.nonzero(label==less_label)[0],:] 61 | return data_much,data_less,much_label,less_label 62 | 63 | # 随机下采样Random UnderSampling 64 | def RUS(data_much, data_less, much_label, less_label): 65 | less_num = data_less.shape[0] 66 | much_num = data_much.shape[0] 67 | # 保留所有少数类 68 | train_data_temp = data_less 69 | train_label_temp=np.ones((1,less_num))*less_label 70 | # 多数类随机下采样 71 | index=np.arange(much_num) 72 | np.random.shuffle(index) 73 | train_data_temp=np.vstack((train_data_temp,data_much[index[0:less_num],:])) 74 | train_label_temp=np.hstack((train_label_temp,np.ones((1,less_num))*much_label)) 75 | 76 | return train_data_temp,train_label_temp 77 | 78 | # 随机上采样Random OverSampling 79 | def ROS(data_much, data_less, much_label, less_label): 80 | less_num = data_less.shape[0] 81 | much_num = data_much.shape[0] 82 | # 保留所有多数类 83 | train_data_temp = data_much 84 | train_label_temp=np.ones((1,much_num))*much_label 85 | # 少数类有放回复制 86 | index=np.array(pd.DataFrame.sample(pd.DataFrame(np.arange(less_num)),replace=True,n=much_num)).T[0] 87 | train_data_temp=np.vstack((train_data_temp,data_less[index,:])) 88 | train_label_temp=np.hstack((train_label_temp,np.ones((1,much_num))*less_label)) 89 | 90 | return train_data_temp,train_label_temp 91 | 92 | #PAC-Bayes bund 93 | #2分类 94 | def PAC_Bayes(real_label,pred,rou=0.05): 95 | m = pred.shape[0] 96 | KL = pred 97 | KL[:, 0] = -1 * (1 - real_label) * np.log(KL[:, 0] + 0.00001) 98 | KL[:, 1] = -1 * real_label * np.log(KL[:, 1] + 0.00001) 99 | pac = (np.sum(KL) + np.log((m + 1) / rou)) / m 100 | return pac 101 | 102 | def cross_entropy_loss(real_label,pred): 103 | KL = pred 104 | KL[:, 0] = -1 * (1 - real_label) * np.log(KL[:, 0] + 0.00001) 105 | KL[:, 1] = -1 * real_label * np.log(KL[:, 1] + 0.00001) 106 | return np.sum(KL)/KL.shape[0] -------------------------------------------------------------------------------- /Ensemble.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.model_selection import StratifiedKFold 3 | from sklearn.linear_model import LogisticRegression 4 | 5 | def stacking_proba(clf,X_train,y,X_test,nfolds=5,random_seed=2017,return_score=False, 6 | shuffle=True,metric='acc',clf_name='UnKnown'): 7 | folds = StratifiedKFold(n_splits=nfolds, shuffle=shuffle, random_state=random_seed) 8 | folds.get_n_splits(X_train,y) 9 | #return stacking_proba for train set 10 | train_stacking_proba=np.zeros((X_train.shape[0],np.unique(y).shape[0])) 11 | score=0 12 | for i,(train_index, validate_index) in enumerate(folds.split(X_train, y)): 13 | # print(str(clf_name)+" folds:"+str(i+1)+"/"+str(nfolds)) 14 | X_train_fold=X_train[train_index,:] 15 | y_train_fold=y[train_index] 16 | X_validate_fold=X_train[validate_index,:] 17 | y_validate_fold=y[validate_index] 18 | clf.fit(X_train_fold,y_train_fold) 19 | fold_preds=clf.predict_proba(X_validate_fold) 20 | train_stacking_proba[validate_index,:]=fold_preds 21 | #validation 22 | fold_preds_a = np.argmax(fold_preds, axis=1) 23 | fold_score=len(np.nonzero(y_validate_fold - fold_preds_a == 0)[0]) / len(y_validate_fold) 24 | # print('validate '+metric+":"+str(fold_score)) 25 | score+=fold_score 26 | score/=nfolds 27 | #return stacking_proba for test set 28 | clf.fit(X_train,y) 29 | test_stacking_proba=clf.predict_proba(X_test) 30 | 31 | if np.unique(y).shape[0] == 2: # when binary classification only return positive class proba 32 | train_stacking_proba=train_stacking_proba[:,1] 33 | test_stacking_proba=test_stacking_proba[:,1] 34 | if return_score: 35 | return train_stacking_proba,test_stacking_proba,score 36 | else: 37 | return train_stacking_proba,test_stacking_proba 38 | 39 | #clfs=list[clf1,clf2,clf3,...] 40 | #clfs_name=list[name1,name2,...] 41 | def stacking(clfs,X_train,y,X_test,nfolds=5,stage=1,random_seed=2017,shuffle=True,clfs_name=None,final_clf=None,C=1): 42 | X_train=np.nan_to_num(X_train) 43 | X_test=np.nan_to_num(X_test) 44 | column_num = len(np.unique(y)) 45 | if column_num==2: 46 | column_num=1# binary classification only needs one class proba 47 | for s in range(stage): 48 | print('stage:'+str(s+1)+'/'+str(stage)) 49 | X_train_stack=[];X_test_stack=[] 50 | for i in range(len(clfs)): 51 | [train_stacking_proba,test_stacking_proba]=stacking_proba(clfs[i],X_train,y,X_test,nfolds, 52 | random_seed,shuffle=shuffle,clf_name=clfs_name[i]) 53 | X_train_stack.append(np.reshape(train_stacking_proba,(train_stacking_proba.shape[0],column_num))) 54 | X_test_stack.append(np.reshape(test_stacking_proba,(test_stacking_proba.shape[0],column_num))) 55 | X_train=np.concatenate(tuple(X_train_stack),axis=1) 56 | X_test=np.concatenate(tuple(X_test_stack),axis=1) 57 | 58 | #final_clf is LogisticRegression as default 59 | if final_clf == None: 60 | final_clf=LogisticRegression(C=C,class_weight='balanced',penalty='l2') 61 | final_clf.fit(X_train, y) 62 | final_label=final_clf.predict(X_test) 63 | final_proba=final_clf.predict_proba(X_test) 64 | return final_label,final_proba 65 | 66 | #voting (all weights are the same) 67 | def vote_ensemble(clfs,X_train,y,X_test): 68 | confidence = np.zeros((X_test.shape[0], len(np.unique(y)))) 69 | for clf in clfs: 70 | clf.fit(X_train,y) 71 | confidence+=(clf.predict_proba(X_test)) 72 | confidence/=len(clfs) 73 | return np.argmax(confidence,axis=0),confidence 74 | 75 | #adaBoost return 'clf_coef' as the weights of classifications and 'data_coef' as the weights of samples 76 | def AdaBoost(clf,validation_data,validation_label,sample_weights=None,metric='acc'): 77 | if sample_weights==None: 78 | sample_weights=np.ones(validation_data.shape[0])/validation_data.shape[0] 79 | predict_label=clf.predict(validation_data) 80 | e=sample_weights*(validation_label-predict_label) 81 | clf_coef=0.5*np.log((1-e)/e) 82 | Z=np.sum(sample_weights*np.exp(-1*clf_coef*validation_label*predict_label)) 83 | data_coef=(sample_weights*np.exp(-1*clf_coef*validation_label*predict_label))/Z 84 | 85 | return clf_coef,data_coef 86 | 87 | 88 | -------------------------------------------------------------------------------- /FisherLDA.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.base import BaseEstimator 3 | 4 | #traditional fisher with different b for binary 5 | #the default b is (n1m1+n2m2)/n1+n2 6 | class Fisher(BaseEstimator): 7 | def __init__(self, intercept=(1,2)): 8 | self.intercept_=intercept 9 | 10 | def fit(self, X, y): 11 | labels=np.unique(y) 12 | if len(labels)>2: 13 | print('just for binary') 14 | return 15 | X0=X[np.where(y==labels[0]),:][0] 16 | X1=X[np.where(y==labels[1]),:][0] 17 | self.m0=np.mean(X0,axis=0) 18 | self.m1=np.mean(X1,axis=0) 19 | self.Sw=np.dot(np.transpose(X0-self.m0),X0-self.m0)+np.dot(np.transpose(X1-self.m1),X1-self.m1) 20 | self.W=np.dot(np.linalg.pinv(self.Sw),np.transpose(self.m0-self.m1)) 21 | 22 | #calculate for b 23 | self.mx0=np.dot(self.m0,self.W) 24 | self.mx1=np.dot(self.m1,self.W) 25 | self.b=-1*(min(self.mx0,self.mx1)+abs(self.mx0-self.mx1)*(self.intercept_[0]/self.intercept_[1])) 26 | 27 | return self 28 | 29 | def get_params(self, deep=True): 30 | return [self.W,self.b] 31 | 32 | def decision_function(self,X): 33 | return np.dot(X,self.W)+self.b 34 | 35 | def predict_proba(self,X): 36 | prob = self.decision_function(X) 37 | prob *= -1 38 | np.exp(prob, prob) 39 | prob += 1 40 | np.reciprocal(prob, prob) 41 | 42 | return np.column_stack([prob, 1 - prob]) 43 | 44 | def predict(self,X): 45 | return np.argmax(self.predict_proba(X),axis=1) 46 | 47 | -------------------------------------------------------------------------------- /GAN/DCGAN.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.models import * 3 | from keras.optimizers import * 4 | import os 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | from skimage import io,transform 8 | 9 | class DCGAN(): 10 | def __init__(self,image_size=(64,64,1),random_vector_size=100): 11 | self.image_size=image_size 12 | self.random_vector_size=random_vector_size 13 | 14 | # discriminator 15 | self.discriminator = self.Discriminator() 16 | self.optimizer = RMSprop(lr=0.0002, decay=6e-8) 17 | self.discriminator.compile(loss='binary_crossentropy', optimizer=self.optimizer, metrics=['accuracy']) 18 | 19 | # generator 20 | self.generator = self.Generator() 21 | 22 | # adversarial 23 | self.adversarial = Sequential() 24 | self.adversarial.add(self.generator) 25 | self.adversarial.add(self.discriminator) 26 | self.optimizer = RMSprop(lr=0.0001, decay=3e-8) 27 | self.adversarial.compile(loss='binary_crossentropy', optimizer=self.optimizer, metrics=['accuracy']) 28 | 29 | def fit(self,path,batch_size=64,epochs=10000,draw=False,shuffle=True): 30 | train_list=np.array(os.listdir(path)) 31 | train_index = np.arange(len(train_list)) 32 | self.path=path 33 | self.batch_size=batch_size 34 | self.epochs=epochs 35 | for i in range(epochs): 36 | print('epochs:'+str(i + 1) + '/' + str(epochs)) 37 | d_acc=0 38 | g_acc=0 39 | if shuffle: 40 | np.random.shuffle(train_index) 41 | for j in range(int(len(train_index)/batch_size)): 42 | temp_index=train_index[j*batch_size:min((j+1)*batch_size,len(train_index)-1)] 43 | images_train=self.train_generator(train_list,temp_index) 44 | noise = np.random.uniform(-1.0, 1.0, size=[batch_size, 100]) 45 | images_fake = self.generator.predict(noise) 46 | x = np.concatenate((images_train, images_fake), axis=0) 47 | y = np.ones([2 * batch_size, 1]) 48 | y[batch_size:, :] = 0 49 | d_loss_acc=self.discriminator.train_on_batch(x, y) 50 | y = np.ones([batch_size, 1]) 51 | noise = np.random.uniform(-1.0, 1.0, size=[batch_size, 100]) 52 | g_loss_acc=self.adversarial.train_on_batch(noise, y) 53 | d_acc+=d_loss_acc[1] * 100 54 | g_acc+=g_loss_acc[1] * 100 55 | print("\rsteps:%d/%d discriminnator_loss/acc:[%.8f,%.2f%%], generator_loss/acc:[%.8f,%.2f%%]" % ( 56 | j + 1, int(len(train_index) / batch_size), d_loss_acc[0], d_acc/(j+1), g_loss_acc[0], 57 | g_acc/(j+1)), end=' ', flush=True) 58 | if draw and (i+1)%5==0: 59 | self.plot_images(path='fake',step=i+1) 60 | print('\n') 61 | return self 62 | 63 | def train_generator(self,train_list,temp_index): 64 | train_list_batch=train_list[temp_index] 65 | images_train=[] 66 | for s in train_list_batch: 67 | img = io.imread(self.path + "/" + s,as_grey=True) 68 | img = transform.resize(img, (self.image_size[0],self.image_size[1]),mode='reflect') 69 | img*=255. 70 | img=self.preprocess_input(img) 71 | images_train.append(img) 72 | images_train=np.reshape(images_train, (self.batch_size, self.image_size[0], self.image_size[1], self.image_size[2])) 73 | return images_train 74 | 75 | def preprocess_input(self,x): 76 | x = x/255. 77 | x = x*2. 78 | x = x-1. 79 | return x 80 | 81 | def preprocess_output(self,x): 82 | x = x+1. 83 | x = x/2. 84 | x = x*255. 85 | return x 86 | 87 | def plot_images(self, samples=25, noise=None, step=0, image_size=(64, 64, 1), path=None,show=False): 88 | if noise is None: 89 | noise = np.random.uniform(-1.0, 1.0, size=[samples, 100]) 90 | filename = "fake_epoches" + str(step) + ".png" 91 | images = self.preprocess_output(self.generator.predict(noise)) 92 | plt.figure(figsize=(8, 8)) 93 | for i in range(images.shape[0]): 94 | plt.subplot(5, 5, i + 1) 95 | image = images[i, :, :, :] 96 | if image_size[2]==1: 97 | image = np.reshape(image, [image_size[0], image_size[1]]) 98 | plt.imshow(image, cmap='gray') 99 | else: 100 | image = np.reshape(image, [image_size[0],image_size[1],image_size[2]]) 101 | plt.imshow(image) 102 | plt.axis('off') 103 | plt.tight_layout() 104 | if path != None: 105 | filename = path + '/' + filename 106 | if show: 107 | plt.show() 108 | plt.savefig(filename) 109 | plt.close('all') 110 | 111 | def Discriminator(self): 112 | input = Input(shape=(self.image_size[0], self.image_size[1], self.image_size[2])) 113 | x = Conv2D(32, (3, 3), strides=2, padding='same')(input) 114 | x = LeakyReLU(alpha=0.2)(x) 115 | x = Dropout(0.3)(x) 116 | 117 | x = Conv2D(64, (3, 3), strides=2, padding='same')(x) 118 | x = LeakyReLU(alpha=0.2)(x) 119 | x = Dropout(0.3)(x) 120 | 121 | x = Conv2D(128, (3, 3), strides=2, padding='same')(x) 122 | x = LeakyReLU(alpha=0.2)(x) 123 | x = Dropout(0.3)(x) 124 | 125 | x = Conv2D(256, (3, 3), strides=2, padding='same')(x) 126 | x = LeakyReLU(alpha=0.2)(x) 127 | x = Dropout(0.3)(x) 128 | 129 | x = Flatten()(x) 130 | output = Dense(1, activation='sigmoid')(x) 131 | 132 | return Model(inputs=input, outputs=output, name='Discriminator') 133 | 134 | def Generator(self): 135 | dim = int(self.image_size[0] / 16) 136 | origin_channel = self.image_size[2] 137 | fake_input = Input(shape=(self.random_vector_size,)) 138 | x = Dense(dim * dim * 256)(fake_input) 139 | x = BatchNormalization(momentum=0.9)(x) 140 | x = Activation('relu')(x) 141 | x = Reshape((dim, dim, 256))(x) 142 | 143 | x = UpSampling2D()(x) 144 | x = Conv2DTranspose(128, (3, 3), padding='same')(x) 145 | x = BatchNormalization(momentum=0.9)(x) 146 | x = Activation('relu')(x) 147 | 148 | x = UpSampling2D()(x) 149 | x = Conv2DTranspose(64, (3, 3), padding='same')(x) 150 | x = BatchNormalization(momentum=0.9)(x) 151 | x = Activation('relu')(x) 152 | 153 | x = UpSampling2D()(x) 154 | x = Conv2DTranspose(32, (3, 3), padding='same')(x) 155 | x = BatchNormalization(momentum=0.9)(x) 156 | x = Activation('relu')(x) 157 | 158 | x = UpSampling2D()(x) 159 | x = Conv2DTranspose(origin_channel, (3, 3), padding='same')(x) 160 | output = Activation('tanh')(x) 161 | 162 | return Model(inputs=fake_input, outputs=output, name='Generator') -------------------------------------------------------------------------------- /GAN/WGAN.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.models import * 3 | from keras.optimizers import * 4 | import keras.backend as K 5 | import os 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | from skimage import io,transform 9 | from keras.initializers import RandomNormal 10 | #plt.switch_backend('agg') 11 | conv_init = RandomNormal(0, 0.02) 12 | 13 | class WGAN(): 14 | # loss_function:'gradient_penalty' or 'clip' 15 | def __init__(self, image_size=(64, 64, 1), random_vector_size=100, diters=5, D_lr=0.0002, G_lr=0.0002, loss_function='gradient_penalty', clamp=0.01, lamda=10): 16 | self.image_size = image_size 17 | self.random_vector_size = random_vector_size 18 | self.diters = diters 19 | self.loss_function = loss_function 20 | 21 | # discriminator 22 | self.discriminator = self.Discriminator() 23 | # generator 24 | self.generator = self.Generator() 25 | 26 | self.D_train, self.G_train, self.D_clamp = self.WGAN_train(loss_function, D_lr=D_lr, G_lr=G_lr, clamp=clamp, 27 | lamda=lamda) 28 | 29 | def WGAN_train(self, loss_function, D_lr, G_lr, clamp, lamda): 30 | assert loss_function=='gradient_penalty' or loss_function=='clip' 31 | 32 | x_real = Input(shape=self.image_size) 33 | fake_vectors = Input(shape=(self.random_vector_size,)) 34 | x_fake = self.generator(fake_vectors) 35 | loss_real = K.mean(self.discriminator(x_real)) 36 | loss_fake = K.mean(self.discriminator(x_fake)) 37 | 38 | # loss for generator 39 | loss = -loss_fake 40 | training_updates = RMSprop(lr=G_lr).get_updates(loss, self.generator.trainable_weights) 41 | G_train = K.function([fake_vectors], [loss], training_updates) 42 | 43 | # clip step 44 | if loss_function == 'clip': 45 | # loss for discriminator 46 | loss = loss_fake - loss_real 47 | training_updates = RMSprop(lr=D_lr).get_updates(loss, self.discriminator.trainable_weights) 48 | D_train = K.function([x_real, fake_vectors], [loss_real, loss_fake], training_updates) 49 | 50 | # clip 51 | clamp_lower, clamp_upper = clamp * -1., clamp 52 | weights_clip = [K.update(x, K.clip(x, clamp_lower, clamp_upper)) for x in self.discriminator.trainable_weights] 53 | D_clamp = K.function([], [], weights_clip) 54 | 55 | return D_train, G_train, D_clamp 56 | 57 | # gradient penalty step 58 | else: 59 | # loss for discriminator 60 | e = K.placeholder(shape=(None, 1, 1, 1)) 61 | x_mixed = Input(shape=self.image_size, tensor=e * x_real + (1 - e) * x_fake) 62 | x_mixed_gradient = K.gradients(self.discriminator(x_mixed), [x_mixed])[0] 63 | x_mixed_gradient_norm = K.sqrt(K.sum(K.square(x_mixed_gradient), axis=[1, 2, 3])) # not norm in batch_size 64 | gradient_penalty = K.mean(K.square(x_mixed_gradient_norm - 1)) 65 | loss = loss_fake - loss_real + lamda * gradient_penalty 66 | training_updates = RMSprop(lr=D_lr).get_updates(loss, self.discriminator.trainable_weights) 67 | D_train = K.function([x_real, fake_vectors, e], [loss_real, loss_fake], training_updates) 68 | 69 | return D_train, G_train, None 70 | 71 | def fit(self, path, batch_size=64, epochs=10000, draw=False, shuffle=True): 72 | train_list = np.array(os.listdir(path)) 73 | train_index = np.arange(len(train_list)) 74 | self.path = path 75 | self.batch_size = batch_size 76 | self.epochs = epochs 77 | gen_iterations = 0 78 | batches = len(train_index) // batch_size 79 | for i in range(epochs): 80 | loss_real_all=0 81 | loss_fake_all=0 82 | loss_D_all=0 83 | j = 0 84 | print('epochs:' + str(i + 1) + '/' + str(epochs)) 85 | d_print = '\r[steps:%d/%d (diters:%d/%d)] loss_real: %.6f, loss_fake: %.6f, loss_D: %.6f' % ( 86 | j, batches, 0, 0, loss_real_all, loss_fake_all, loss_D_all) 87 | g_print = ' [gen_iterations:%d] loss_G: %.6f' % (gen_iterations, 0) 88 | if shuffle: 89 | np.random.shuffle(train_index) 90 | while j < batches: 91 | if gen_iterations < 25 or gen_iterations % 200 == 0: 92 | diters = 100 93 | else: 94 | diters = self.diters 95 | q = 0 96 | # train discriminator 97 | while q < diters and j < batches: 98 | temp_index = train_index[j * batch_size:min((j + 1) * batch_size, len(train_index) - 1)] 99 | j += 1;q += 1 100 | x_real = self.train_generator(train_list, temp_index) 101 | fake_vectors = np.random.normal(size=[batch_size, self.random_vector_size]) 102 | if self.loss_function=='gradient_penalty': 103 | e = np.random.uniform(size=(batch_size, 1, 1, 1)) 104 | loss_real, loss_fake = self.D_train([x_real,fake_vectors,e]) 105 | else: 106 | self.D_clamp([]) 107 | loss_real, loss_fake = self.D_train([x_real, fake_vectors]) 108 | loss_real_all += loss_real 109 | loss_fake_all += loss_fake 110 | loss_D_all += (loss_fake - loss_real) 111 | d_print = '\r[steps:%d/%d (diters:%d/%d)] loss_real: %.6f, loss_fake: %.6f, loss_D: %.6f' % ( 112 | j, batches, q, diters, loss_real_all/j, loss_fake_all/j, loss_D_all/j) 113 | print(d_print+g_print, end=' ', flush=True) 114 | 115 | # plot fake image 116 | if draw and gen_iterations % 200 == 0: 117 | self.plot_images(path='fake',step=gen_iterations) 118 | 119 | # train generator 120 | gen_iterations += 1 121 | fake_vectors = np.random.normal(size=[batch_size, self.random_vector_size]) 122 | [loss_G] = self.G_train([fake_vectors]) 123 | g_print = ' [gen_iterations:%d] loss_G: %.6f' % (gen_iterations, loss_G) 124 | print(d_print + g_print, end=' ', flush=True) 125 | print('\n') 126 | return self 127 | 128 | def train_generator(self,train_list,temp_index): 129 | train_list_batch=train_list[temp_index] 130 | images_train=[] 131 | for s in train_list_batch: 132 | img = io.imread(self.path + "/" + s, as_grey=True) 133 | img = transform.resize(img, (self.image_size[0], self.image_size[1]), mode='reflect') 134 | img *= 255. 135 | img = self.preprocess_input(img) 136 | images_train.append(img) 137 | images_train=np.reshape(images_train, (self.batch_size, self.image_size[0], self.image_size[1], self.image_size[2])) 138 | return images_train 139 | 140 | def preprocess_input(self,x): 141 | x = x / 255. 142 | x = x * 2. 143 | x = x - 1. 144 | return x 145 | 146 | def preprocess_output(self, x): 147 | x = x + 1. 148 | x = x / 2. 149 | x = x * 255. 150 | return x 151 | 152 | def plot_images(self, samples=25, noise=None, step=0, image_size=(64, 64, 1), path=None,show=False): 153 | if noise is None: 154 | noise = np.random.normal(size=[samples, self.random_vector_size]) 155 | filename = "fake_epoches" + str(step) + ".png" 156 | images = self.preprocess_output(self.generator.predict(noise)) 157 | plt.figure(figsize=(8, 8)) 158 | for i in range(images.shape[0]): 159 | plt.subplot(5, 5, i + 1) 160 | image = images[i, :, :, :] 161 | if image_size[2]==1: 162 | image = np.reshape(image, [image_size[0], image_size[1]]) 163 | plt.imshow(image, cmap='gray') 164 | else: 165 | image = np.reshape(image, [image_size[0],image_size[1],image_size[2]]) 166 | plt.imshow(image) 167 | plt.axis('off') 168 | plt.tight_layout() 169 | if path != None: 170 | filename = path + '/' + filename 171 | if show: 172 | plt.show() 173 | plt.savefig(filename) 174 | plt.close('all') 175 | 176 | def Discriminator(self): 177 | input = Input(shape=(self.image_size[0], self.image_size[1], self.image_size[2])) 178 | 179 | # 32*32 180 | x = Conv2D(32, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(input) 181 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 182 | x = LeakyReLU(alpha=0.2)(x) 183 | 184 | # 16*16 185 | x = Conv2D(64, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 186 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 187 | x = LeakyReLU(alpha=0.2)(x) 188 | 189 | # 8*8 190 | x = Conv2D(128, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 191 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 192 | x = LeakyReLU(alpha=0.2)(x) 193 | 194 | # 4*4 195 | x = Conv2D(256, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 196 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 197 | x = LeakyReLU(alpha=0.2)(x) 198 | 199 | # 4*4*1 conv filters with valid padding to get 1-d output, which is used to instead of the sigmoid 200 | x = Conv2D(1, (4, 4), strides=1, padding='valid', kernel_initializer=conv_init, use_bias=False)(x) 201 | output = Flatten()(x) 202 | 203 | return Model(inputs=input, outputs=output, name='Discriminator') 204 | 205 | def Generator(self): 206 | origin_channel = self.image_size[2] 207 | fake_input = Input(shape=(self.random_vector_size,)) 208 | x = Reshape((1, 1, self.random_vector_size))(fake_input) 209 | 210 | # 4*4 211 | x = Conv2DTranspose(256, (4, 4), strides=1, padding='valid', kernel_initializer=conv_init, use_bias=False)(x) 212 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 213 | x = Activation('relu')(x) 214 | 215 | # 8*8 216 | x = Conv2DTranspose(128, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 217 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 218 | x = Activation('relu')(x) 219 | 220 | # 16*16 221 | x = Conv2DTranspose(64, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 222 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 223 | x = Activation('relu')(x) 224 | 225 | # 32*32 226 | x = Conv2DTranspose(32, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 227 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x,training=1) 228 | x = Activation('relu')(x) 229 | 230 | # 64*64 231 | x = Conv2DTranspose(origin_channel, (3, 3), strides=2, padding='same', kernel_initializer=conv_init, use_bias=False)(x) 232 | output = Activation('tanh')(x) 233 | 234 | return Model(inputs=fake_input, outputs=output, name='Generator') -------------------------------------------------------------------------------- /GAN/starGAN.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | from keras.layers import * 4 | from keras.models import * 5 | from keras.optimizers import * 6 | import keras.backend as K 7 | import matplotlib.pyplot as plt 8 | from skimage import io,transform 9 | from keras.initializers import RandomNormal 10 | # plt.switch_backend('agg') 11 | # conv_init = RandomNormal(0, 0.02) 12 | 13 | class StarGAN(): 14 | def __init__(self, image_size=(128, 128, 3), n_class=5, repeat_num=6, diters=5, D_lr=0.0002, G_lr=0.0002, lamda_gp=10, lamda_cls=1, lamda_rec=10): 15 | self.image_size = image_size 16 | self.repeat_num = repeat_num 17 | self.diters = diters 18 | self.n_class = n_class 19 | self.D_lr = D_lr 20 | self.G_lr = G_lr 21 | self.lamda_gp = lamda_gp 22 | self.lamda_cls = lamda_cls 23 | self.lamda_rec = lamda_rec 24 | 25 | # discriminator 26 | self.discriminator = self.Discriminator() 27 | # generator 28 | self.generator = self.Generator() 29 | 30 | def save_weights(self, path): 31 | if not os.path.exists(path): 32 | os.mkdir(path) 33 | self.discriminator.save_weights(path + '/discriminator_weights.h5') 34 | self.generator.save_weights(path + '/generator_weights.h5') 35 | 36 | def load_weights(self, path): 37 | self.discriminator.load_weights(path + '/discriminator_weights.h5') 38 | self.generator.load_weights(path + '/generator_weights.h5') 39 | 40 | def starGAN_train(self, D_lr, G_lr, lamda_gp, lamda_cls, lamda_rec): 41 | 42 | x_real = Input(shape=self.image_size) 43 | label_real = Input(shape=(self.n_class,)) 44 | label_fake = Input(shape=(self.n_class,)) 45 | label_real_matrix = Input(shape=(self.image_size[0],self.image_size[1],self.n_class)) 46 | label_fake_matrix = Input(shape=(self.image_size[0],self.image_size[1],self.n_class)) 47 | x_fake = self.generator([x_real, label_fake_matrix]) 48 | 49 | # loss for discriminator 50 | d_out_src_real, d_out_cls_real = self.discriminator(x_real) 51 | d_loss_real = -K.mean(d_out_src_real) 52 | d_loss_cls = K.mean(K.categorical_crossentropy(label_real, d_out_cls_real)) 53 | # cal acc 54 | label_sub = d_out_cls_real - label_real 55 | c1 = 1 + K.min(label_sub, axis=1) # label为1的最小置信度 56 | c2 = K.max(label_sub, axis=1) # label为0的最大置信度 57 | d_acc = K.mean(K.cast(K.greater(c1 - c2, 0), K.floatx())) # 如果label为1的最小置信度大于label为0的最大置信度,则正确,否则错误 58 | # label_pred = K.cast(K.greater(K.clip(d_out_cls_real, 0, 1), 0.5), K.floatx()) 59 | # d_acc = 1 - K.mean(K.clip(K.sum(K.abs(label_real - label_pred), axis=1), 0, 1)) 60 | d_out_src_fake, d_out_cls_fake = self.discriminator(x_fake) 61 | d_loss_fake = K.mean(d_out_src_fake) 62 | 63 | # gradient penalty 64 | e = K.placeholder(shape=(None, 1, 1, 1)) 65 | x_mixed = Input(shape=self.image_size, tensor=e * x_real + (1 - e) * x_fake) 66 | x_mixed_gradient = K.gradients(self.discriminator(x_mixed), [x_mixed])[0] 67 | x_mixed_gradient_norm = K.sqrt(K.sum(K.square(x_mixed_gradient), axis=[1, 2, 3])) # not norm in batch_size 68 | gradient_penalty = K.mean(K.square(x_mixed_gradient_norm - 1)) 69 | 70 | d_loss = d_loss_real + d_loss_fake + lamda_gp * gradient_penalty + lamda_cls * d_loss_cls 71 | d_training_updates = RMSprop(lr=D_lr).get_updates(d_loss, self.discriminator.trainable_weights) 72 | D_train = K.function([x_real, label_real, label_real_matrix, label_fake, label_fake_matrix, e], [d_loss, d_acc], d_training_updates) 73 | 74 | # loss for generator 75 | x_rec = self.generator([x_fake, label_real_matrix]) 76 | g_out_src_fake, g_out_cls_fake = self.discriminator(x_fake) 77 | g_loss_fake = -K.mean(g_out_src_fake) 78 | g_loss_rec = K.mean(K.abs(x_real - x_rec)) 79 | g_loss_cls = K.mean(K.categorical_crossentropy(label_fake, g_out_cls_fake)) 80 | 81 | g_loss = g_loss_fake + lamda_rec * g_loss_rec + lamda_cls * g_loss_cls 82 | g_training_updates = RMSprop(lr=G_lr).get_updates(g_loss, self.generator.trainable_weights) 83 | G_train = K.function([x_real, label_real, label_real_matrix, label_fake, label_fake_matrix], [g_loss], g_training_updates) 84 | 85 | return D_train, G_train 86 | 87 | def fit(self, X_path, y, batch_size=16, epochs=20, draw=False, shuffle=True): 88 | # X_path:path array[string], y:one-hot array[array[int]] 89 | train_index = np.arange(len(X_path)) 90 | self.batch_size = batch_size 91 | self.epochs = epochs 92 | gen_iterations = 0 93 | batches = len(train_index) // batch_size 94 | D_train, G_train = self.starGAN_train(self.D_lr, self.G_lr, self.lamda_gp, self.lamda_cls, self.lamda_rec) 95 | for i in range(epochs): 96 | # train step 97 | if epochs-i<10: 98 | D_train, G_train = self.starGAN_train(self.D_lr*((epochs-i)/10), self.G_lr*((epochs-i)/10), self.lamda_gp, self.lamda_cls, 99 | self.lamda_rec) 100 | d_loss_all = 0 101 | d_acc_all = 0 102 | g_loss_all = 0 103 | j = 0 104 | g = 0 105 | print('epochs:' + str(i + 1) + '/' + str(epochs)) 106 | d_print = '\r[steps:%d/%d (diters:%d/%d)] d_loss: %.6f, d_acc: %.4f' % (j, batches, 0, 0, d_loss_all, d_acc_all) 107 | g_print = ' [gen_iterations:%d] g_loss: %.6f' % (gen_iterations, g_loss_all) 108 | if shuffle: 109 | np.random.shuffle(train_index) 110 | while j < batches: 111 | q = 0 112 | # train discriminator 113 | while q < self.diters and j < batches: 114 | temp_index = train_index[j * batch_size:min((j + 1) * batch_size, len(train_index) - 1)] 115 | j += 1;q += 1 116 | x_real,label_real,label_fake = self.train_generator(X_path, y, temp_index) 117 | label_real_matrix=self.label2matrix(label_real) 118 | label_fake_matrix=self.label2matrix(label_fake) 119 | e = np.random.uniform(size=(batch_size, 1, 1, 1)) 120 | [d_loss,d_acc] = D_train([x_real, label_real, label_real_matrix, label_fake, label_fake_matrix, e]) 121 | d_loss_all += d_loss 122 | d_acc_all += d_acc 123 | d_print = '\r[steps:%d/%d (diters:%d/%d)] d_loss: %.6f, d_acc: %.4f' % (j, batches, q, self.diters, d_loss_all/j, d_acc_all/j) 124 | print(d_print+g_print, end=' ', flush=True) 125 | 126 | # plot fake image 127 | if draw and gen_iterations % 200 == 0: 128 | self.plot_images(X_path=X_path, y=y, path='fake',step=gen_iterations) 129 | 130 | # train generator 131 | gen_iterations += 1 132 | g += 1 133 | [g_loss] = G_train([x_real, label_real, label_real_matrix, label_fake, label_fake_matrix]) 134 | g_loss_all += g_loss 135 | g_print = ' [gen_iterations:%d] g_loss: %.6f' % (gen_iterations, g_loss_all/g) 136 | print(d_print + g_print, end=' ', flush=True) 137 | print('\n') 138 | return self 139 | 140 | def train_generator(self, X_path, y, temp_index): 141 | X_path=np.array(X_path) 142 | train_list_batch=X_path[temp_index] 143 | label_real=y[temp_index,:] 144 | images_train=[] 145 | for s in train_list_batch: 146 | img = io.imread(s) 147 | img = self.preprocess_input(img) 148 | images_train.append(img) 149 | images_train=np.reshape(images_train, (len(temp_index), self.image_size[0], self.image_size[1], self.image_size[2])) 150 | 151 | # get label_fake 152 | random_index=np.random.choice(np.arange(y.shape[0]),len(temp_index)) 153 | label_fake=y[random_index,:] 154 | 155 | return images_train,label_real,label_fake 156 | 157 | def label2matrix(self,y): 158 | # label转矩阵 batch*n_class->batch*n_class*w*h,如[0,1,0]转为3个h*w矩阵——全零矩阵[0],全一矩阵[1],全零矩阵[0] 159 | y_matrix=np.zeros((y.shape[0],self.image_size[0],self.image_size[1],y.shape[1])) 160 | for i in range(y.shape[0]): 161 | for j in range(y.shape[1]): 162 | if y[i,j]==1: 163 | y_matrix[i,:,:,j]=1 164 | return y_matrix 165 | 166 | def preprocess_input(self,x): 167 | x = x / 255. 168 | x = x * 2. 169 | x = x - 1. 170 | return x 171 | 172 | def preprocess_output(self, x): 173 | x = x + 1. 174 | x = x / 2. 175 | # x = x * 255. 176 | return x 177 | 178 | def plot_images(self, X_path, y, samples=5, step=0, path=None,show=False): 179 | filename = "gen_steps" + str(step) + ".png" 180 | temp_index = np.random.choice(np.arange(y.shape[0]), samples, replace=False) 181 | x_real, label_real, _ = self.train_generator(X_path, y, temp_index) 182 | label_blond_hair = label_real 183 | label_blond_hair[:,0]=0 184 | label_blond_hair[:,1]=1 185 | label_blond_hair[:,2]=0 186 | label_male=label_real 187 | label_male[:,3]=1 188 | label_old=label_real 189 | label_old[:,4]=0 190 | # generate fake image 191 | x_blond_hair=self.generator.predict([x_real,self.label2matrix(label_blond_hair)]) 192 | x_male=self.generator.predict([x_real,self.label2matrix(label_male)]) 193 | x_old=self.generator.predict([x_real,self.label2matrix(label_old)]) 194 | 195 | plt.figure(figsize=(10, 10)) 196 | for i in range(x_real.shape[0]): 197 | #plot origin 198 | plt.subplot(samples, 4, i * 4 + 1) 199 | plt.imshow(self.preprocess_output(x_real[i, :, :, :])) 200 | plt.title('origin') 201 | plt.axis('off') 202 | 203 | plt.subplot(samples, 4, i * 4 + 2) 204 | plt.imshow(self.preprocess_output(x_blond_hair[i, :, :, :])) 205 | plt.title('blond hair') 206 | plt.axis('off') 207 | 208 | plt.subplot(samples, 4, i * 4 + 3) 209 | plt.imshow(self.preprocess_output(x_male[i, :, :, :])) 210 | plt.title('male') 211 | plt.axis('off') 212 | 213 | plt.subplot(samples, 4, i * 4 + 4) 214 | plt.imshow(self.preprocess_output(x_old[i, :, :, :])) 215 | plt.title('old') 216 | plt.axis('off') 217 | plt.tight_layout() 218 | if path != None: 219 | filename = path + '/' + filename 220 | if show: 221 | plt.show() 222 | plt.savefig(filename) 223 | plt.close('all') 224 | 225 | def ResidualBlock(self, x, filters): 226 | # Skip layer 227 | shortcut = Conv2D(filters, (1, 1), padding='same')(x) 228 | 229 | # Residual block 230 | x = Conv2D(filters, (3, 3), padding='same', use_bias=False)(x) 231 | x = BatchNormalization()(x, training=1) 232 | x = Activation('relu')(x) 233 | x = Conv2D(filters, (3, 3), padding='same', use_bias=False)(x) 234 | x = BatchNormalization()(x, training=1) 235 | x = add([x, shortcut]) 236 | 237 | return x 238 | 239 | def Discriminator(self): 240 | input = Input(shape=(self.image_size[0], self.image_size[1], self.image_size[2])) 241 | 242 | x = Conv2D(64, (4, 4), strides=2, padding='same')(input) 243 | x = LeakyReLU(alpha=0.01)(x) 244 | 245 | k_size=self.image_size[0]//2 246 | filters=128 247 | for i in range(self.repeat_num-3): 248 | x = Conv2D(filters, (3, 3), strides=2, padding='same')(x) 249 | filters*=2 250 | k_size//=2 251 | x = LeakyReLU(alpha=0.2)(x) 252 | 253 | # k_size*k_size*1 conv filters with valid padding to get 1-d output, which is used to instead of the sigmoid 254 | output_x = Conv2D(1, (k_size, k_size), strides=1, padding='valid', use_bias=False)(x) 255 | output_y = Conv2D(self.n_class, (k_size, k_size), strides=1, padding='valid', use_bias=False)(x) 256 | output_x = Flatten()(output_x) 257 | output_y = Flatten()(output_y) 258 | 259 | return Model(inputs=input, outputs=[output_x,output_y], name='Discriminator') 260 | 261 | def Generator(self): 262 | origin_channel = self.image_size[2] 263 | x_real = Input(shape=(self.image_size[0], self.image_size[1], origin_channel)) 264 | label_fake = Input(shape=(self.image_size[0], self.image_size[1], self.n_class)) 265 | x = Concatenate(axis=-1)([x_real,label_fake]) 266 | 267 | x = Conv2D(64, (7, 7), strides=1, padding='same', use_bias=False)(x) 268 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x, training=1) 269 | x = Activation('relu')(x) 270 | 271 | # down-sampling 272 | x = Conv2D(128, (4, 4), strides=2, padding='same', use_bias=False)(x) 273 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x, training=1) 274 | x = Activation('relu')(x) 275 | 276 | x = Conv2D(256, (4, 4), strides=2, padding='same', use_bias=False)(x) 277 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x, training=1) 278 | x = Activation('relu')(x) 279 | 280 | # Bottleneck 281 | for i in range(self.repeat_num): 282 | x = self.ResidualBlock(x, 256) 283 | 284 | # up-sampling 285 | x = Conv2DTranspose(128, (4, 4), strides=2, padding='same', use_bias=False)(x) 286 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x, training=1) 287 | x = Activation('relu')(x) 288 | 289 | x = Conv2DTranspose(64, (4, 4), strides=2, padding='same', use_bias=False)(x) 290 | x = BatchNormalization(momentum=0.9, epsilon=1e-5)(x, training=1) 291 | x = Activation('relu')(x) 292 | 293 | x = Conv2D(origin_channel, (7, 7), strides=1, padding='same', use_bias=False)(x) 294 | output = Activation('tanh')(x) 295 | 296 | return Model(inputs=[x_real,label_fake], outputs=output, name='Generator') -------------------------------------------------------------------------------- /Imbalance/DataPreprocessing.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def MajorityAndMinority(y): 4 | labels = np.unique(y) 5 | if len(np.where(y == labels[0])[0]) >= len(np.where(y == labels[1])[0]): 6 | majority_label = labels[0] 7 | majority_num = len(np.where(y == labels[0])[0]) 8 | minority_label = labels[1] 9 | minority_num = y.shape[0] - majority_num 10 | else: 11 | majority_label = labels[1] 12 | majority_num = len(np.where(y == labels[1])[0]) 13 | minority_label = labels[0] 14 | minority_num = y.shape[0] - majority_num 15 | return majority_label,majority_num,minority_label,minority_num 16 | 17 | #binary 18 | #ratio: majority'=(majority-minority)*ratio+minoirty 19 | def RandomUnderSampling(X,y,ratio=0,random_seed=2,replace=False): 20 | np.random.seed(random_seed) 21 | majority_label, majority_num, minority_label, minority_num=MajorityAndMinority(y) 22 | majority_X=X[y==majority_label,:] 23 | minority_X=X[y==minority_label,:] 24 | random_index=np.random.choice(np.arange(majority_num),int(minority_num+(majority_num-minority_num)*ratio),replace=replace) 25 | majority_X=majority_X[random_index,:] 26 | final_X=np.concatenate((minority_X,majority_X),axis=0) 27 | final_y=np.ones(final_X.shape[0]) 28 | final_y[0:minority_num]=final_y[0:minority_num] * minority_label 29 | final_y[minority_num:] = final_y[minority_num:] * majority_label 30 | 31 | return final_X,final_y -------------------------------------------------------------------------------- /Imbalance/SMOTE.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from sklearn.neighbors import NearestNeighbors 3 | 4 | def smote_interpolate(X, y, k=5, rate=1.0): 5 | ''' 6 | :param k: neighbor 7 | :param X: 8 | :param y: 9 | :param rate: N(X_minority)/N(X_majority) 10 | :return: X_smote, y_smote 11 | ''' 12 | # get minority/majority labels 13 | unique_labels = np.unique(y) 14 | assert len(unique_labels)==2 15 | num0 = len(np.where(y == unique_labels[0])[0]) 16 | num1 = len(np.where(y == unique_labels[1])[0]) 17 | if num0 > num1: 18 | y_minority = unique_labels[1] 19 | n_minority = num1 20 | n_majority = num0 21 | else: 22 | y_minority = unique_labels[0] 23 | n_minority = num0 24 | n_majority = num1 25 | 26 | # get R smote_samples for each minority 27 | R = round((rate * n_majority - n_minority) / n_minority) 28 | if R == 0: 29 | R += 1 30 | 31 | KNN = NearestNeighbors(n_neighbors=k).fit(X) 32 | minority_ind = np.where(y == y_minority)[0] 33 | k_neighbors = KNN.kneighbors(X[minority_ind,::], n_neighbors=k, return_distance=False) 34 | X_add = [] 35 | y_add = [] 36 | for i in range(k_neighbors.shape[0]): 37 | target_neighbor = np.random.choice(k_neighbors[i,:], size=R, replace=True) 38 | for t in target_neighbor: 39 | X_add.append(X[minority_ind[i],::] + (np.random.rand()*(X[t,::] - X[minority_ind[i],::]))) 40 | y_add.append(y_minority) 41 | X_add = np.array(X_add) 42 | y_add = np.array(y_add) 43 | X_smote = np.concatenate((X, X_add), axis=0) 44 | y_smote = np.concatenate((y, y_add)) 45 | 46 | return X_smote, y_smote -------------------------------------------------------------------------------- /KerasLayer/QAoutputBlock.py: -------------------------------------------------------------------------------- 1 | # ! -*- coding: utf-8 -*- 2 | from keras.engine.topology import Layer 3 | from keras.regularizers import * 4 | import tensorflow as tf 5 | import keras.backend as K 6 | 7 | class QAoutputBlock(Layer): 8 | def __init__(self, ans_limit=30, **kwargs): 9 | self.ans_limit = ans_limit 10 | super(QAoutputBlock, self).__init__(**kwargs) 11 | 12 | def build(self, input_shape): 13 | super(QAoutputBlock, self).build(input_shape) 14 | 15 | def call(self, x, mask=None): 16 | x1 ,x2 = x 17 | outer = tf.matmul(tf.expand_dims(x1, axis=2), tf.expand_dims(x2, axis=1)) 18 | outer = tf.matrix_band_part(outer, 0, self.ans_limit) 19 | output1 = tf.reshape(tf.cast(tf.argmax(tf.reduce_max(outer, axis=2), axis=1), tf.float32),(-1,1)) 20 | output2 = tf.reshape(tf.cast(tf.argmax(tf.reduce_max(outer, axis=1), axis=1), tf.float32),(-1,1)) 21 | 22 | return [output1, output2] 23 | 24 | def compute_output_shape(self, input_shape): 25 | return [(input_shape[0][0],1), (input_shape[0][0],1)] -------------------------------------------------------------------------------- /KerasLayer/context2query_attention.py: -------------------------------------------------------------------------------- 1 | from keras import backend as K 2 | from keras.engine.topology import Layer 3 | from keras.regularizers import * 4 | import tensorflow as tf 5 | 6 | class context2query_attention(Layer): 7 | 8 | def __init__(self, output_dim, cont_limit, ques_limit, dropout, **kwargs): 9 | self.output_dim=output_dim 10 | self.cont_limit = cont_limit 11 | self.ques_limit = ques_limit 12 | self.dropout = dropout 13 | super(context2query_attention, self).__init__(**kwargs) 14 | 15 | def build(self, input_shape): 16 | # input_shape: [(None, 400, 128), (None, 50, 128)] 17 | self.W0 = self.add_weight(name='W0', 18 | shape=(input_shape[0][-1], 1), 19 | initializer='glorot_uniform', 20 | regularizer=l2(3e-7), 21 | trainable=True) 22 | self.W1 = self.add_weight(name='W1', 23 | shape=(input_shape[1][-1], 1), 24 | initializer='glorot_uniform', 25 | regularizer=l2(3e-7), 26 | trainable=True) 27 | self.W2 = self.add_weight(name='W2', 28 | shape=(1, 1, input_shape[0][-1]), 29 | initializer='glorot_uniform', 30 | regularizer=l2(3e-7), 31 | trainable=True) 32 | self.bias = self.add_weight(name='linear_bias', 33 | shape=(input_shape[1][1],), 34 | initializer='zero', 35 | regularizer=l2(3e-7), 36 | trainable=True) 37 | super(context2query_attention, self).build(input_shape) 38 | 39 | def Mask(self, inputs, seq_len, axis=1, time_dim=1, mode='mul'): 40 | if seq_len == None: 41 | return inputs 42 | else: 43 | seq_len=K.cast(seq_len,tf.int32) 44 | mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[time_dim]) 45 | mask = 1 - K.cumsum(mask, 1) 46 | mask = K.expand_dims(mask, axis) 47 | if mode == 'mul': 48 | return inputs * mask 49 | if mode == 'add': 50 | return inputs - (1 - mask) * 1e12 51 | 52 | def call(self, x, mask=None): 53 | x_cont, x_ques, cont_len, ques_len = x 54 | 55 | # get similarity matrix S 56 | subres0 = K.tile(K.dot(x_cont, self.W0), [1, 1, self.ques_limit]) 57 | subres1 = K.tile(K.permute_dimensions(K.dot(x_ques, self.W1), pattern=(0, 2, 1)), [1, self.cont_limit, 1]) 58 | subres2 = K.batch_dot(x_cont * self.W2, K.permute_dimensions(x_ques, pattern=(0, 2, 1))) 59 | S = subres0 + subres1 + subres2 60 | S += self.bias 61 | 62 | S_ = tf.nn.softmax(self.Mask(S, ques_len, axis=1, time_dim=2, mode='add')) 63 | S_T = K.permute_dimensions(tf.nn.softmax(self.Mask(S, cont_len, axis=2, time_dim=1, mode='add'), axis=1), (0, 2, 1)) 64 | c2q = tf.matmul(S_, x_ques) 65 | q2c = tf.matmul(tf.matmul(S_, S_T), x_cont) 66 | result = K.concatenate([x_cont, c2q, x_cont * c2q, x_cont * q2c], axis=-1) 67 | 68 | return result 69 | 70 | def compute_output_shape(self, input_shape): 71 | return (input_shape[0][0], input_shape[0][1], self.output_dim) -------------------------------------------------------------------------------- /KerasLayer/gate_attention.py: -------------------------------------------------------------------------------- 1 | # ! -*- coding: utf-8 -*- 2 | from keras.engine.topology import Layer 3 | from keras.regularizers import * 4 | import tensorflow as tf 5 | import keras.backend as K 6 | 7 | class GateAttention(Layer): 8 | def __init__(self, filters, dropout=0.0, regularizer=l2(3e-7), **kwargs): 9 | self.filters = filters 10 | self.dropout = dropout 11 | self.regularizer = regularizer 12 | super(GateAttention, self).__init__(**kwargs) 13 | 14 | def build(self, input_shape): 15 | self.WC = self.add_weight(name='WC', 16 | shape=(input_shape[0][-1], self.filters), 17 | regularizer=self.regularizer, 18 | initializer='glorot_uniform', 19 | trainable=True) 20 | self.WQ = self.add_weight(name='WQ', 21 | shape=(input_shape[1][-1], self.filters), 22 | regularizer=self.regularizer, 23 | initializer='glorot_uniform', 24 | trainable=True) 25 | self.V = self.add_weight(name='V', 26 | shape=(2 * input_shape[1][-1], self.filters), 27 | regularizer=self.regularizer, 28 | initializer='glorot_uniform', 29 | trainable=True) 30 | super(GateAttention, self).build(input_shape) 31 | 32 | def mask_logits(self, inputs, mask, clen, mask_value=-1e12): 33 | shapes = [x if x != None else -1 for x in inputs.shape.as_list()] 34 | mask = K.cast(mask, tf.int32) 35 | mask = K.one_hot(mask[:, 0], shapes[-1]) 36 | mask = 1 - K.cumsum(mask, 1) 37 | mask = tf.cast(mask, tf.float32) 38 | mask = tf.tile(tf.expand_dims(mask, axis=1), [1, clen, 1]) 39 | return inputs + mask_value * (1 - mask) 40 | 41 | def call(self, x, mask=None): 42 | x_cont, x_ques, ques_len = x 43 | input_shape_ = x_cont.shape.as_list() 44 | x_cont_ = tf.nn.relu(K.dot(x_cont, self.WC)) 45 | x_ques_ = tf.nn.relu(K.dot(x_ques, self.WQ)) 46 | logits = tf.matmul(x_cont_, x_ques_, transpose_b=True) / (self.filters ** 0.5) 47 | logits = self.mask_logits(logits, ques_len, clen=input_shape_[1]) 48 | logits = tf.nn.softmax(logits) 49 | C = tf.matmul(logits, x_ques) 50 | res = tf.concat([x_cont, C], axis=2) 51 | gate = tf.nn.sigmoid(K.dot(res, self.V)) 52 | return gate 53 | 54 | def compute_output_shape(self, input_shape): 55 | return input_shape[0] -------------------------------------------------------------------------------- /KerasLayer/layer_dropout.py: -------------------------------------------------------------------------------- 1 | # ! -*- coding: utf-8 -*- 2 | from keras.engine.topology import Layer 3 | import tensorflow as tf 4 | import keras.backend as K 5 | 6 | class LayerDropout(Layer): 7 | def __init__(self, dropout = 0.0, **kwargs): 8 | self.dropout = dropout 9 | super(LayerDropout, self).__init__(**kwargs) 10 | 11 | def build(self, input_shape): 12 | super(LayerDropout, self).build(input_shape) 13 | 14 | def call(self, x, mask=None, training=None): 15 | x, residual = x 16 | pred = tf.random_uniform([]) < self.dropout 17 | x_train = tf.cond(pred, lambda: residual, lambda: tf.nn.dropout(x, 1.0 - self.dropout) + residual) 18 | x_test = x + residual 19 | return K.in_train_phase(x_train, x_test, training=training) 20 | 21 | def compute_output_shape(self, input_shape): 22 | return input_shape -------------------------------------------------------------------------------- /KerasLayer/multihead_attention.py: -------------------------------------------------------------------------------- 1 | # ! -*- coding: utf-8 -*- 2 | 3 | from keras import backend as K 4 | from keras.engine.topology import Layer 5 | import tensorflow as tf 6 | 7 | class Attention(Layer): 8 | def __init__(self, units, num_heads, dropout=0.0, bias=True, **kwargs): 9 | self.units = units 10 | self.num_heads = num_heads 11 | self.dropout = dropout 12 | self.bias = bias 13 | super(Attention, self).__init__(**kwargs) 14 | 15 | def build(self, input_shape): 16 | if self.bias: 17 | self.b = self.add_weight(name='bias', 18 | shape=(input_shape[0][-2],), 19 | initializer='zero') 20 | super(Attention, self).build(input_shape) 21 | 22 | def split_last_dimension(self, x, n): 23 | old_shape = x.get_shape().dims 24 | last = old_shape[-1] 25 | new_shape = old_shape[:-1] + [n] + [last // n if last else None] 26 | ret = tf.reshape(x, tf.concat([tf.shape(x)[:-1], [n, -1]], 0)) 27 | ret.set_shape(new_shape) 28 | return tf.transpose(ret, [0, 2, 1, 3]) 29 | 30 | def mask_logits(self, inputs, mask, mask_value=-1e12): 31 | shapes = [x if x != None else -1 for x in inputs.shape.as_list()] 32 | mask = K.cast(mask, tf.int32) 33 | mask = K.one_hot(mask[:, 0], shapes[-1]) 34 | mask = 1 - K.cumsum(mask, 1) 35 | mask = tf.cast(mask, tf.float32) 36 | mask = tf.reshape(mask, [shapes[0], 1, 1, shapes[-1]]) 37 | return inputs + mask_value * (1 - mask) 38 | 39 | def dot_product_attention(self, x, seq_len=None, dropout=0.1, training=None): 40 | q, k, v = x 41 | logits = tf.matmul(q, k, transpose_b=True) 42 | if self.bias: 43 | logits += self.b 44 | if seq_len is not None: 45 | logits = self.mask_logits(logits, seq_len) 46 | weights = tf.nn.softmax(logits, name="attention_weights") 47 | weights = K.in_train_phase(K.dropout(weights, dropout), weights, training=training) 48 | x = tf.matmul(weights, v) 49 | return x 50 | 51 | def combine_last_two_dimensions(self, x): 52 | old_shape = x.get_shape().dims 53 | a, b = old_shape[-2:] 54 | new_shape = old_shape[:-2] + [a * b if a and b else None] 55 | ret = tf.reshape(x, tf.concat([tf.shape(x)[:-2], [-1]], 0)) 56 | ret.set_shape(new_shape) 57 | return ret 58 | 59 | def call(self, x, mask=None, training=None): 60 | memory, query, seq_len = x 61 | Q = self.split_last_dimension(query, self.num_heads) 62 | memory = tf.split(memory, 2, axis=2) 63 | K = self.split_last_dimension(memory[0], self.num_heads) 64 | V = self.split_last_dimension(memory[1], self.num_heads) 65 | 66 | key_depth_per_head = self.units // self.num_heads 67 | Q *= (key_depth_per_head ** -0.5) 68 | x = self.dot_product_attention([Q, K, V], seq_len, dropout=self.dropout, training=training) 69 | x = self.combine_last_two_dimensions(tf.transpose(x, [0,2,1,3])) 70 | 71 | return x 72 | 73 | def compute_output_shape(self, input_shape): 74 | return input_shape[1] -------------------------------------------------------------------------------- /KerasLayer/multihead_attention_bysu.py: -------------------------------------------------------------------------------- 1 | # ! -*- coding: utf-8 -*- 2 | 3 | from keras import backend as K 4 | from keras.engine.topology import Layer 5 | 6 | class Attention(Layer): 7 | def __init__(self, nb_head, size_per_head, **kwargs): 8 | self.nb_head = nb_head 9 | self.size_per_head = size_per_head 10 | self.output_dim = nb_head * size_per_head 11 | super(Attention, self).__init__(**kwargs) 12 | 13 | def build(self, input_shape): 14 | self.WQ = self.add_weight(name='WQ', 15 | shape=(input_shape[0][-1], self.output_dim), 16 | initializer='glorot_uniform', 17 | trainable=True) 18 | self.WK = self.add_weight(name='WK', 19 | shape=(input_shape[1][-1], self.output_dim), 20 | initializer='glorot_uniform', 21 | trainable=True) 22 | self.WV = self.add_weight(name='WV', 23 | shape=(input_shape[2][-1], self.output_dim), 24 | initializer='glorot_uniform', 25 | trainable=True) 26 | super(Attention, self).build(input_shape) 27 | 28 | def Mask(self, inputs, seq_len, mode='mul'): 29 | if seq_len == None: 30 | return inputs 31 | else: 32 | mask = K.one_hot(seq_len[:, 0], K.shape(inputs)[1]) 33 | mask = 1 - K.cumsum(mask, 1) 34 | for _ in range(len(inputs.shape) - 2): 35 | mask = K.expand_dims(mask, 2) 36 | if mode == 'mul': 37 | return inputs * mask 38 | if mode == 'add': 39 | return inputs - (1 - mask) * 1e12 40 | 41 | def call(self, x, mask=None): 42 | # 如果只传入Q_seq,K_seq,V_seq,那么就不做Mask 43 | # 如果同时传入Q_seq,K_seq,V_seq,Q_len,V_len,那么对多余部分做Mask 44 | if len(x) == 3: 45 | Q_seq, K_seq, V_seq = x 46 | Q_len, V_len = None, None 47 | elif len(x) == 5: 48 | Q_seq, K_seq, V_seq, Q_len, V_len = x 49 | Q_len = K.cast(Q_len, 'int32') 50 | V_len = K.cast(V_len, 'int32') 51 | else: 52 | print('wrong input!') 53 | return x 54 | # 对Q、K、V做线性变换 55 | Q_seq = K.dot(Q_seq, self.WQ) 56 | Q_seq = K.reshape(Q_seq, (-1, K.shape(Q_seq)[1], self.nb_head, self.size_per_head)) 57 | Q_seq = K.permute_dimensions(Q_seq, (0, 2, 1, 3)) 58 | K_seq = K.dot(K_seq, self.WK) 59 | K_seq = K.reshape(K_seq, (-1, K.shape(K_seq)[1], self.nb_head, self.size_per_head)) 60 | K_seq = K.permute_dimensions(K_seq, (0, 2, 1, 3)) 61 | V_seq = K.dot(V_seq, self.WV) 62 | V_seq = K.reshape(V_seq, (-1, K.shape(V_seq)[1], self.nb_head, self.size_per_head)) 63 | V_seq = K.permute_dimensions(V_seq, (0, 2, 1, 3)) 64 | # 计算内积,然后mask,然后softmax 65 | A = K.batch_dot(Q_seq, K_seq, axes=[3, 3]) 66 | A = K.permute_dimensions(A, (0, 3, 2, 1)) 67 | A = self.Mask(A, V_len, 'add') 68 | A = K.permute_dimensions(A, (0, 3, 2, 1)) 69 | A = K.softmax(A) 70 | # 输出并mask 71 | O_seq = K.batch_dot(A, V_seq, axes=[3, 2]) 72 | O_seq = K.permute_dimensions(O_seq, (0, 2, 1, 3)) 73 | O_seq = K.reshape(O_seq, (-1, K.shape(O_seq)[1], self.output_dim)) 74 | O_seq = self.Mask(O_seq, Q_len, 'mul') 75 | return O_seq 76 | 77 | def compute_output_shape(self, input_shape): 78 | return (input_shape[0][0], input_shape[0][1], self.output_dim) -------------------------------------------------------------------------------- /KerasLayer/position_embedding.py: -------------------------------------------------------------------------------- 1 | from keras.engine.topology import Layer 2 | import tensorflow as tf 3 | import math 4 | 5 | 6 | class Position_Embedding(Layer): 7 | def __init__(self, min_timescale=1.0, max_timescale=1.0e4, **kwargs): 8 | self.min_timescale = min_timescale 9 | self.max_timescale = max_timescale 10 | super(Position_Embedding, self).__init__(**kwargs) 11 | 12 | def get_timing_signal_1d(self, length, channels): 13 | position = tf.to_float(tf.range(length)) 14 | num_timescales = channels // 2 15 | log_timescale_increment = (math.log(float(self.max_timescale) / float(self.min_timescale)) / (tf.to_float(num_timescales) - 1)) 16 | inv_timescales = self.min_timescale * tf.exp(tf.to_float(tf.range(num_timescales)) * -log_timescale_increment) 17 | scaled_time = tf.expand_dims(position, 1) * tf.expand_dims(inv_timescales, 0) 18 | signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1) 19 | signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]]) 20 | signal = tf.reshape(signal, [1, length, channels]) 21 | return signal 22 | 23 | def add_timing_signal_1d(self, x): 24 | length = tf.shape(x)[1] 25 | channels = tf.shape(x)[2] 26 | signal = self.get_timing_signal_1d(length, channels) 27 | return x + signal 28 | 29 | def call(self, x, mask=None): 30 | return self.add_timing_signal_1d(x) 31 | 32 | def compute_output_shape(self, input_shape): 33 | return input_shape -------------------------------------------------------------------------------- /KerasLayer/position_embedding_bysu.py: -------------------------------------------------------------------------------- 1 | from keras import backend as K 2 | from keras.engine.topology import Layer 3 | 4 | 5 | class Position_Embedding(Layer): 6 | 7 | def __init__(self, size=None, mode='sum', **kwargs): 8 | self.size = size # 必须为偶数 9 | self.mode = mode 10 | super(Position_Embedding, self).__init__(**kwargs) 11 | 12 | def call(self, x, mask=None): 13 | if (self.size == None) or (self.mode == 'sum'): 14 | self.size = int(x.shape[-1]) 15 | batch_size, seq_len = K.shape(x)[0], K.shape(x)[1] 16 | position_j = 1. / K.pow(10000., 2 * K.arange(self.size / 2, dtype='float32') / self.size) 17 | position_j = K.expand_dims(position_j, 0) 18 | position_i = K.cumsum(K.ones_like(x[:, :, 0]), 1) - 1 # K.arange不支持变长,只好用这种方法生成 19 | position_i = K.expand_dims(position_i, 2) 20 | position_ij = K.dot(position_i, position_j) 21 | position_ij = K.concatenate([K.cos(position_ij), K.sin(position_ij)], 2) 22 | if self.mode == 'sum': 23 | return position_ij + x 24 | elif self.mode == 'concat': 25 | return K.concatenate([position_ij, x], 2) 26 | 27 | def compute_output_shape(self, input_shape): 28 | if self.mode == 'sum': 29 | return input_shape 30 | elif self.mode == 'concat': 31 | return (input_shape[0], input_shape[1], input_shape[2] + self.size) -------------------------------------------------------------------------------- /KerasLayer/simple_attention.py: -------------------------------------------------------------------------------- 1 | from keras import initializers, regularizers, constraints 2 | import keras.backend as K 3 | from keras.engine.topology import Layer 4 | 5 | class Attention(Layer): 6 | def __init__(self, timesteps, attention_size, bias=True, 7 | W_regularizer=regularizers.l1(0.01), W_constraint=None, 8 | U_regularizer=regularizers.l1(0.01), U_constraint=None, 9 | **kwargs): 10 | self.supports_masking = True 11 | self.init = initializers.get('glorot_uniform') 12 | self.bias = bias 13 | self.timesteps = timesteps 14 | self.attention_size = attention_size 15 | self.W_regularizer = regularizers.get(W_regularizer) 16 | self.W_constraint = constraints.get(W_constraint) 17 | self.U_regularizer = regularizers.get(U_regularizer) 18 | self.U_constraint = constraints.get(U_constraint) 19 | 20 | super(Attention, self).__init__(**kwargs) 21 | 22 | def build(self, input_shape): 23 | assert len(input_shape) == 3 24 | 25 | self.features_dim = input_shape[-1] 26 | self.W = self.add_weight((self.features_dim, self.attention_size), 27 | initializer=self.init, 28 | regularizer=self.W_regularizer, 29 | constraint=self.W_constraint, 30 | name='{}_W'.format(self.name)) 31 | 32 | self.U = self.add_weight((self.attention_size, 1), 33 | initializer=self.init, 34 | regularizer=self.U_regularizer, 35 | constraint=self.U_constraint, 36 | name='{}_U'.format(self.name)) 37 | 38 | if self.bias: 39 | self.b = self.add_weight((self.attention_size,), 40 | initializer='zero', 41 | name='{}_b'.format(self.name)) 42 | else: 43 | self.b = None 44 | 45 | self.built = True 46 | 47 | def compute_mask(self, input, input_mask=None): 48 | return None 49 | 50 | def call(self, x, mask=None): 51 | e = K.dot(x, self.W) 52 | if self.bias: 53 | e += self.b 54 | e = K.tanh(e) 55 | e = K.reshape(K.dot(e, self.U), (-1, self.timesteps)) 56 | a = K.exp(e) 57 | if mask is not None: 58 | a *= K.cast(mask, K.floatx()) 59 | a_weights = a / K.cast(K.sum(a, axis=-1, keepdims=True) + K.epsilon(), K.floatx()) 60 | weighted_output = x * K.expand_dims(a_weights, axis=-1) 61 | 62 | return [K.mean(weighted_output, axis=1), a_weights] 63 | 64 | def compute_output_shape(self, input_shape): 65 | return [(input_shape[0], self.features_dim), (input_shape[0], self.timesteps)] -------------------------------------------------------------------------------- /KerasLayer/simple_attention_permute.py: -------------------------------------------------------------------------------- 1 | from keras import initializers, regularizers, constraints 2 | import keras.backend as K 3 | from keras.engine.topology import Layer 4 | 5 | class Attention(Layer): 6 | def __init__(self, timesteps, bias=True, simple=False, 7 | W_regularizer=None, W_constraint=None, 8 | V_regularizer=None, V_constraint=None, 9 | **kwargs): 10 | self.supports_masking = True 11 | self.init = initializers.get('glorot_uniform') 12 | self.bias = bias 13 | self.timesteps = timesteps 14 | self.simple = simple 15 | self.W_regularizer = regularizers.get(W_regularizer) 16 | self.W_constraint = constraints.get(W_constraint) 17 | self.V_regularizer = regularizers.get(V_regularizer) 18 | self.V_constraint = constraints.get(V_constraint) 19 | 20 | super(Attention, self).__init__(**kwargs) 21 | 22 | def build(self, input_shape): 23 | assert len(input_shape) == 3 24 | 25 | self.features_dim = input_shape[-1] 26 | self.W = self.add_weight((self.timesteps, self.timesteps), 27 | initializer=self.init, 28 | regularizer=self.W_regularizer, 29 | constraint=self.W_constraint, 30 | name='{}_W'.format(self.name)) 31 | if not self.simple: 32 | self.V = self.add_weight((self.features_dim, 1), 33 | initializer=self.init, 34 | regularizer=self.V_regularizer, 35 | constraint=self.V_constraint, 36 | name='{}_V'.format(self.name)) 37 | 38 | if self.bias: 39 | self.b = self.add_weight((self.timesteps,), 40 | initializer='zero', 41 | name='{}_b'.format(self.name)) 42 | else: 43 | self.b = None 44 | 45 | self.built = True 46 | 47 | def compute_mask(self, input, input_mask=None): 48 | return None 49 | 50 | def call(self, x, mask=None): 51 | x_transpose = K.permute_dimensions(x, (0,2,1)) 52 | e = K.dot(x_transpose, self.W) 53 | if self.bias: 54 | e += self.b 55 | e = K.tanh(e) 56 | if not self.simple: 57 | e = K.permute_dimensions(e, (0,2,1)) 58 | e = K.reshape(K.dot(e, self.V), (-1, self.timesteps)) 59 | else: 60 | e = K.mean(e, axis=1) 61 | a = K.exp(e) 62 | if mask is not None: 63 | a *= K.cast(mask, K.floatx()) 64 | a_weights = a / K.cast(K.sum(a, axis=-1, keepdims=True) + K.epsilon(), K.floatx()) 65 | weighted_output = x * K.expand_dims(a_weights, axis=-1) 66 | 67 | return [K.sum(weighted_output, axis=1), a_weights] 68 | 69 | def compute_output_shape(self, input_shape): 70 | return [(input_shape[0], self.features_dim), (input_shape[0], self.timesteps)] -------------------------------------------------------------------------------- /KerasLayer/very_simple_attention.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.optimizers import * 3 | 4 | def VerySimpleAttention(inputs,timesteps,reduce_mean=True): 5 | a = Lambda(lambda x:K.permute_dimensions(x,(0,2,1)))(inputs) 6 | a = Dense(timesteps, activation='softmax', name='attention_W')(a) 7 | a = Lambda(lambda x: K.mean(x, axis=1), name='dim_reduction')(a) 8 | a = RepeatVector(inputs.shape[-1])(a) 9 | a_probs = Lambda(lambda x:K.permute_dimensions(x,(0,2,1)))(a) 10 | a_weights= Lambda(lambda x:tf.reduce_mean(x,axis=-1), name='attention_weights')(a_probs) 11 | x = Multiply(name='attention_mul')([inputs, a_probs]) 12 | if reduce_mean: 13 | x = Lambda(lambda x:tf.reduce_mean(x,axis=1))(x) 14 | else: 15 | x = Flatten()(x) 16 | return x, a_weights 17 | -------------------------------------------------------------------------------- /MatMHKS.py: -------------------------------------------------------------------------------- 1 | from sklearn.base import BaseEstimator 2 | import numpy as np 3 | import math 4 | from numpy import linalg as LA 5 | 6 | class MatMHKS(BaseEstimator): 7 | def __init__(self, penalty='l2', C=1.0, matrix_type=None,class_weight=None, random_seed=None, 8 | feature_shuffle=False, max_iter=100,v0=1,b0=10**(-6),eta=0.99,min_step=0.0001, 9 | multi_class='ovr',verbose=0): 10 | self.penalty = penalty 11 | self.C = C 12 | self.matrix_type = matrix_type 13 | self.random_seed = random_seed 14 | self.feature_shuffle = feature_shuffle 15 | self.class_weight = class_weight 16 | self.max_iter = max_iter 17 | self.v0 = v0 18 | self.b0 = b0 19 | self.eta = eta 20 | self.min_step = min_step 21 | self.multi_class = multi_class 22 | self.verbose = verbose 23 | 24 | def reshape(self,X): 25 | X_matrix=[] 26 | for i in range(X.shape[0]): 27 | x = np.reshape(X[i, :], (self.matrix_type[0], self.matrix_type[1]),'C') 28 | x = np.concatenate((x, np.zeros((x.shape[0], 1))), axis=1) 29 | x = np.asmatrix(np.concatenate((x, np.zeros((1, x.shape[1]))), axis=0)) 30 | x[-1, -1] = 1 31 | X_matrix.append(x) 32 | return X_matrix 33 | 34 | def get_mat_e(self,X_matrix,y,u,v,b): 35 | E = np.asmatrix(np.zeros((len(X_matrix), 1))) 36 | for i in range(len(X_matrix)): 37 | x=X_matrix[i] 38 | E[i]=y[i]*u.T*x*v 39 | return E-self.I-b 40 | 41 | def get_mat_u(self,X_matrix,y,S,v,b): 42 | self.Z = np.asmatrix(np.zeros((len(X_matrix), S.shape[0]))) 43 | for i in range(len(X_matrix)): 44 | x = X_matrix[i] 45 | self.Z[i, :] = y[i] * (x*v).T 46 | S=np.sum(S,axis=1) 47 | u=np.linalg.pinv(self.C * S * S.T + self.Z.T * self.Z) * self.Z.T * (self.I + b) 48 | return u 49 | 50 | def get_mat_v(self,X_matrix,y,S,u,b): 51 | if type(self.pinv) != np.matrix: 52 | self.Y = np.asmatrix(np.zeros((len(X_matrix), S.shape[1]))) 53 | for i in range(len(X_matrix)): 54 | x = X_matrix[i] 55 | self.Y[i, :] = y[i] * (u.T * x) 56 | S = np.sum(S, axis=0) 57 | self.pinv=np.linalg.pinv(self.C * S.T * S + self.Y.T * self.Y) 58 | return self.pinv * self.Y.T * (self.I + b) 59 | 60 | def fun(self,X,y): 61 | u=np.asmatrix(np.zeros((self.matrix_type[0]+1,1))) 62 | v=np.asmatrix(np.ones((self.matrix_type[1]+1,1))*self.v0) 63 | b=np.asmatrix(np.ones((X.shape[0],1))*self.b0) 64 | self.I=np.asmatrix(np.ones((X.shape[0],1))) 65 | S=np.asmatrix(np.ones((self.matrix_type[0]+1,self.matrix_type[1]+1))) 66 | S[:,-1]=0;S[-1,:]=0 67 | X_matrix=self.reshape(X) 68 | iter=1 69 | self.pinv=None 70 | while iter < self.max_iter: 71 | if iter == 1: 72 | u = self.get_mat_u(X_matrix, y, S, v, b) 73 | v = self.get_mat_v(X_matrix, y, S, u, b) 74 | e = self.get_mat_e(X_matrix, y, u, v, b) 75 | b_next = b + self.eta * (e + abs(e)) 76 | if LA.norm(b_next - b, 2) < self.min_step: 77 | break 78 | b = b_next 79 | iter += 1 80 | # print(iter) 81 | return u,v 82 | 83 | def get_params(self, deep=False): 84 | """Get parameter.s""" 85 | params = super(BaseEstimator, self).get_params(deep=deep) 86 | if isinstance(self.kwargs, dict): # if kwargs is a dict, update params accordingly 87 | params.update(self.kwargs) 88 | if params['missing'] is np.nan: 89 | params['missing'] = None # sklearn doesn't handle nan. see #4725 90 | if not params.get('eval_metric', True): 91 | del params['eval_metric'] # don't give as None param to Booster 92 | return params 93 | 94 | def shuffle(self,X): 95 | if self.random_seed==None: 96 | self.random_seed=2 97 | np.random.seed(self.random_seed) 98 | colum_shuffle=np.arange(X.shape[1]) 99 | np.random.shuffle(colum_shuffle) 100 | return X[:,colum_shuffle] 101 | 102 | def fit(self, X, y): 103 | if self.matrix_type==None: 104 | self.matrix_type=(1,X.shape[1]) 105 | labels=np.unique(y) 106 | self.real_class=labels #save real classes 107 | self.n_class = len(labels) 108 | y=y-min(labels) 109 | labels = labels - min(labels) 110 | self.u={} 111 | self.v={} 112 | if self.feature_shuffle==True: 113 | X=self.shuffle(X) 114 | if self.n_class==2: 115 | y_temp = np.zeros(y.shape[0]) 116 | y_temp[np.where(y==0)] = 1 117 | y_temp[np.where(y!=0)] = -1 118 | self.u[0], self.v[0] = self.fun(X, y_temp) 119 | else: 120 | #ovo or ovr 121 | if self.multi_class=='ovr': 122 | for positive in labels: 123 | y_temp=np.zeros(y.shape[0]) 124 | y_temp[np.where(y==positive)[0]]=1 125 | y_temp[np.where(y!=positive)[0]]=-1 126 | self.u[positive],self.v[positive]=self.fun(X,y_temp) 127 | 128 | elif self.multi_class=='ovo': 129 | for c1 in range(len(labels)-1): 130 | for c2 in range(c1+1,len(labels)): 131 | X_c1=X[np.where(y==c1)[0],:] 132 | X_c2=X[np.where(y==c2)[0],:] 133 | X_temp=np.concatenate((X_c1,X_c2),axis=0) 134 | y_temp=np.ones(X_temp.shape[0]) 135 | y_temp[X_c1.shape[0]:]=-1 136 | self.u[(c1,c2)],self.v[(c1,c2)]=self.fun(X_temp,y_temp) 137 | return self 138 | 139 | def softmax(self,X): 140 | prob=np.zeros((len(X),self.n_class)) 141 | for i in range(len(X)): 142 | for i_class in range(self.n_class): 143 | prob[i,i_class]=math.exp(self.u[i_class].T*X[i]*self.v[i_class]) 144 | prob[i,:]=prob[i,:]/sum(prob[i,:]) 145 | return prob 146 | 147 | def sigmoid(self,X): 148 | prob=np.zeros((len(X),2)) 149 | for i in range(len(X)): 150 | if self.multi_class=='ovr': 151 | prob[i, 0] = 1 / (1 + math.exp(-1 * (self.u[0].T * X[i] * self.v[0]))) 152 | else: 153 | prob[i, 0] = 1 / (1 + math.exp(-1 * (self.u[(0,1)].T * X[i] * self.v[(0,1)]))) 154 | prob[:,1]=1-prob[:,0] 155 | return prob 156 | 157 | def sigmoid_ovo(self,X): 158 | prob=np.zeros((len(X),self.n_class)) 159 | for i in range(len(X)): 160 | for k in self.u.keys(): 161 | prob_temp=(1 / (1 + math.exp(-1 * (self.u[k].T * X[i] * self.v[k])))) 162 | prob[i,k[0]]+=prob_temp 163 | prob[i,k[1]]+=(1-prob_temp) 164 | prob[i, :] = prob[i, :] / sum(prob[i, :]) 165 | return prob 166 | 167 | def predict_proba(self,X): 168 | if self.feature_shuffle==True: 169 | X=self.shuffle(X) 170 | X_matrix = self.reshape(X) 171 | if self.n_class==2: 172 | prob=self.sigmoid(X_matrix) 173 | else: 174 | if self.multi_class=='ovr': 175 | prob=self.softmax(X_matrix) 176 | else: 177 | prob=self.sigmoid_ovo(X_matrix) 178 | return prob 179 | 180 | def predict(self,X): 181 | prob=self.predict_proba(X) 182 | labels=np.zeros(X.shape[0]) 183 | for i in range(X.shape[0]): 184 | labels[i]=np.argmax(prob[i,:]) 185 | 186 | return labels+min(self.real_class) -------------------------------------------------------------------------------- /NetModel/C3D.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.models import * 3 | 4 | def model(input_shape=(16, 112, 112, 3)): 5 | inputs = Input(shape=input_shape) 6 | x = Convolution3D(64, 3, 3, 3, activation='relu', 7 | border_mode='same', name='conv1', 8 | input_shape=input_shape)(inputs) 9 | x = MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), 10 | border_mode='valid', name='pool1')(x) 11 | # 2nd layer group 12 | x = Convolution3D(128, 3, 3, 3, activation='relu', 13 | border_mode='same', name='conv2')(x) 14 | x = MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), 15 | border_mode='valid', name='pool2')(x) 16 | # 3rd layer group 17 | x = Convolution3D(256, 3, 3, 3, activation='relu', 18 | border_mode='same', name='conv3a')(x) 19 | x = Convolution3D(256, 3, 3, 3, activation='relu', 20 | border_mode='same', name='conv3b')(x) 21 | x = MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), 22 | border_mode='valid', name='pool3')(x) 23 | # 4th layer group 24 | x = Convolution3D(512, 3, 3, 3, activation='relu', 25 | border_mode='same', name='conv4a')(x) 26 | x = Convolution3D(512, 3, 3, 3, activation='relu', 27 | border_mode='same', name='conv4b')(x) 28 | x = MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), 29 | border_mode='valid', name='pool4')(x) 30 | # 5th layer group 31 | x = Convolution3D(512, 3, 3, 3, activation='relu', 32 | border_mode='same', name='conv5a')(x) 33 | x = Convolution3D(512, 3, 3, 3, activation='relu', 34 | border_mode='same', name='conv5b')(x) 35 | x = ZeroPadding3D(padding=((0, 0), (0, 1), (0, 1)), name='zeropad5')(x) 36 | x = MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), 37 | border_mode='valid', name='pool5')(x) 38 | x = Flatten()(x) 39 | # FC layers group 40 | x = Dense(4096, activation='relu', name='fc6')(x) 41 | x = Dropout(0.5)(x) 42 | x = Dense(4096, activation='relu', name='fc7')(x) 43 | x = Dropout(0.5)(x) 44 | 45 | a = Dense(1, activation='sigmoid', name='arousal')(x) 46 | v = Dense(1, activation='tanh', name='valence')(x) 47 | 48 | model = Model(inputs, [a,v], name='C3D') 49 | 50 | return model 51 | 52 | c3d_model=model() 53 | print(c3d_model.summary()) 54 | 55 | import numpy as np 56 | X=np.random.random((1000,16,112,112,3)) 57 | y=np.random.random((1000,2)) 58 | y[:,1]=(y[:,1]*2)-1 59 | c3d_model.compile(loss='mse',optimizer='adam') 60 | c3d_model.fit(X,[y[:,0],y[:,1]]) -------------------------------------------------------------------------------- /NetModel/CNN+RNN.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import * 3 | from keras.layers import * 4 | from keras.optimizers import * 5 | from keras_vggface.vggface import VGGFace 6 | 7 | def model(vgg_model, input_shape=(224,224,3), CNN_output_dim=512, timesteps=8, unit=256, ac='sigmoid'): 8 | inputs = Input((timesteps, input_shape[0], input_shape[1], input_shape[2])) 9 | masking_len = Input((timesteps,CNN_output_dim)) 10 | print(inputs) 11 | x = Lambda(lambda x:tf.unstack(x,axis=1),name='unstack')(inputs) 12 | print(x) 13 | xs=[] 14 | for xi in x: 15 | xs.append(vgg_model(xi)) 16 | x = Lambda(lambda x:tf.stack(x,axis=1),name='stack')(xs) 17 | x = Lambda(lambda x: x[0] * x[1])([x, masking_len]) 18 | x = Masking(mask_value=0)(x) 19 | x = LSTM(unit, return_sequences=True,name='LSTM')(x) 20 | if ac == 'tanh': 21 | output = Dense(1, activation='tanh',name='output1')(x) 22 | elif ac == 'tanh+sigmoid': 23 | x1 = Dense(1, activation='sigmoid',name='output1')(x) 24 | x2 = Dense(1, activation='tanh',name='output2')(x) 25 | output = [x1, x2] 26 | else: 27 | output = Dense(1, activation='sigmoid',name='output1')(x) 28 | 29 | return Model(inputs=[inputs,masking_len], outputs=output) 30 | 31 | 32 | VGGFace_model = VGGFace(model='vgg16', weights=None, include_top=False, input_shape=(224,224,3), pooling='avg') 33 | model=model(VGGFace_model) 34 | # model.summary() -------------------------------------------------------------------------------- /NetModel/Hourglass.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.models import * 3 | from keras import layers 4 | from keras.optimizers import * 5 | from skimage import io 6 | 7 | # heatmaps 8 | def makeGaussian(height, width, sigma = 2, center=None): 9 | x = np.arange(0, width, 1, float) 10 | y = np.arange(0, height, 1, float)[:, np.newaxis] 11 | if center is None: 12 | x0 = width // 2 13 | y0 = height // 2 14 | else: 15 | x0 = center[0] 16 | y0 = center[1] 17 | return np.exp(-4*np.log(2) * ((x-x0)**2 + (y-y0)**2) / sigma**2) 18 | 19 | # xrange 有效像素点范围 20 | def get_heatmaps(labels, size=(64, 64), xrange=1): 21 | life_x = labels[0:5] 22 | life_y = labels[5:10] 23 | int_x = labels[10:15] 24 | int_y = labels[15:20] 25 | aff_x = labels[20:25] 26 | aff_y = labels[25:30] 27 | finger_x = labels[30:35] 28 | finger_y = labels[35:40] 29 | x_all=np.concatenate((life_x,int_x,aff_x,finger_x)) 30 | y_all=np.concatenate((life_y,int_y,aff_y,finger_y)) 31 | heatmaps=np.zeros((size[0],size[0],len(x_all))) 32 | for i in range(len(x_all)): 33 | x=int(x_all[i]*size[1]) 34 | y=int(y_all[i]*size[0]) 35 | heatmap=makeGaussian(size[0], size[1], sigma = 2, center=[x,y]) 36 | heatmaps[:,:,i]=heatmap 37 | 38 | return heatmaps 39 | 40 | # generator to heatmaps 41 | def generator2heatmaps(file_list, label_list, batch_size, shuffle=True, random_seed=None): 42 | while True: 43 | if shuffle: 44 | if random_seed != None: 45 | random_seed += 1 46 | np.random.seed(random_seed) 47 | index = np.arange(file_list.shape[0]) 48 | np.random.shuffle(index) 49 | file_list = file_list[index] 50 | label_list = label_list[index] 51 | count = 0 52 | x, y = [], [] 53 | for i, path in enumerate(file_list): 54 | img = io.imread(path) 55 | img = np.array(img) 56 | x_temp = img 57 | y_temp = get_heatmaps(label_list[i, :]) 58 | count += 1 59 | x.append(x_temp) 60 | y.append(y_temp) 61 | if count % batch_size == 0 and count != 0: 62 | x = np.array(x) 63 | x = x.reshape(batch_size, 128, 128, 1).astype("float32") 64 | y = np.array(y) 65 | yield x, [y, y, y, y, y, y] 66 | x, y = [], [] 67 | 68 | def preprocess_numpy_input(x): 69 | x /= 127.5 70 | x -= 1. 71 | return x 72 | 73 | def Residual(x, filters): 74 | # Skip layer 75 | shortcut = Conv2D(filters, (1, 1), padding='same')(x) 76 | 77 | # Residual block 78 | x = BatchNormalization()(x) 79 | x = Activation('relu')(x) 80 | x = Conv2D(int(filters / 2), (1, 1), padding='same', use_bias=False)(x) 81 | x = BatchNormalization()(x) 82 | x = Activation('relu')(x) 83 | x = Conv2D(int(filters / 2), (3, 3), padding='same', use_bias=False)(x) 84 | x = BatchNormalization()(x) 85 | x = Activation('relu')(x) 86 | x = Conv2D(filters, (1, 1), padding='same', use_bias=False)(x) 87 | x = layers.add([x, shortcut]) 88 | 89 | return x 90 | 91 | def Hourglass(x, level, module, filters): 92 | # up layer 93 | for i in range(module): 94 | x = Residual(x, filters) 95 | up = x 96 | 97 | # low layer 98 | low = MaxPooling2D()(x) 99 | for i in range(module): 100 | low = Residual(low, filters) 101 | if level>1: 102 | low = Hourglass(low, level-1, module, filters) 103 | else: 104 | for i in range(module): 105 | low = Residual(low, filters) 106 | for i in range(module): 107 | low = Residual(low, filters) 108 | low = UpSampling2D()(low) 109 | x = layers.add([up, low]) 110 | 111 | return x 112 | 113 | def model(input_shape=(256, 256, 1), labels=20, nstack=6, level=4, module=1, filters=256, preprocess=True): 114 | img_input = Input(shape=input_shape) 115 | 116 | if preprocess: 117 | x = Lambda(preprocess_numpy_input)(img_input) 118 | else: 119 | x = img_input 120 | 121 | # 256*256 122 | x = Conv2D(64, (7, 7), strides=2, padding='same', use_bias=False)(x) 123 | # 128*128 124 | x = BatchNormalization()(x) 125 | x = Activation('relu')(x) 126 | x = Residual(x, int(filters/2)) 127 | x = MaxPooling2D()(x) 128 | # 64*64 129 | x = Residual(x, int(filters/2)) 130 | middle_x = Residual(x, filters) 131 | outputs=[] 132 | 133 | for i in range(nstack): 134 | x = Hourglass(middle_x, level, module, filters) 135 | for j in range(module): 136 | x = Residual(x, filters) 137 | x = Conv2D(filters, (1, 1), padding='same', use_bias=False)(x) 138 | x = BatchNormalization()(x) 139 | x = Activation('relu')(x) 140 | temp_output = Conv2D(labels, (1, 1), padding='same', name='nstack_'+str(i+1))(x) 141 | outputs.append(temp_output) 142 | 143 | if i < nstack-1: 144 | x = Conv2D(filters, (1, 1), padding='same')(x) 145 | temp_output = Conv2D(filters, (1, 1), padding='same')(temp_output) 146 | middle_x = layers.add([middle_x, x, temp_output]) 147 | 148 | # Create model. 149 | model = Model(img_input, outputs, name='hourglass') 150 | 151 | return model 152 | 153 | # test test 154 | model=model((64,64,3),labels=10,preprocess=False) 155 | optimizer = Adam(lr=0.0003, beta_1=0.9, beta_2=0.999, epsilon=1e-08) 156 | model.compile(loss='mse', optimizer=optimizer) 157 | X_train=np.random.random((1000,64,64,3)) 158 | y=np.random.random((1000,16,16,10)) 159 | model.fit(X_train,[y,y,y,y,y,y],verbose=1) -------------------------------------------------------------------------------- /NetModel/MoblieNetV2.py: -------------------------------------------------------------------------------- 1 | from keras.models import Model 2 | from keras.layers import Input, Conv2D, GlobalAveragePooling2D, Dropout 3 | from keras.layers import Activation, BatchNormalization, add, Reshape 4 | from keras.applications.mobilenet import relu6, DepthwiseConv2D 5 | from keras.utils.vis_utils import plot_model 6 | 7 | from keras import backend as K 8 | 9 | 10 | def _conv_block(inputs, filters, kernel, strides): 11 | """Convolution Block 12 | This function defines a 2D convolution operation with BN and relu6. 13 | # Arguments 14 | inputs: Tensor, input tensor of conv layer. 15 | filters: Integer, the dimensionality of the output space. 16 | kernel: An integer or tuple/list of 2 integers, specifying the 17 | width and height of the 2D convolution window. 18 | strides: An integer or tuple/list of 2 integers, 19 | specifying the strides of the convolution along the width and height. 20 | Can be a single integer to specify the same value for 21 | all spatial dimensions. 22 | # Returns 23 | Output tensor. 24 | """ 25 | 26 | channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 27 | 28 | x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs) 29 | x = BatchNormalization(axis=channel_axis)(x) 30 | return Activation(relu6)(x) 31 | 32 | 33 | def _bottleneck(inputs, filters, kernel, t, s, r=False): 34 | """Bottleneck 35 | This function defines a basic bottleneck structure. 36 | # Arguments 37 | inputs: Tensor, input tensor of conv layer. 38 | filters: Integer, the dimensionality of the output space. 39 | kernel: An integer or tuple/list of 2 integers, specifying the 40 | width and height of the 2D convolution window. 41 | t: Integer, expansion factor. 42 | t is always applied to the input size. 43 | s: An integer or tuple/list of 2 integers,specifying the strides 44 | of the convolution along the width and height.Can be a single 45 | integer to specify the same value for all spatial dimensions. 46 | r: Boolean, Whether to use the residuals. 47 | # Returns 48 | Output tensor. 49 | """ 50 | 51 | channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 52 | tchannel = K.int_shape(inputs)[channel_axis] * t 53 | 54 | x = _conv_block(inputs, tchannel, (1, 1), (1, 1)) 55 | 56 | x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x) 57 | x = BatchNormalization(axis=channel_axis)(x) 58 | x = Activation(relu6)(x) 59 | 60 | x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x) 61 | x = BatchNormalization(axis=channel_axis)(x) 62 | 63 | if r: 64 | x = add([x, inputs]) 65 | return x 66 | 67 | 68 | def _inverted_residual_block(inputs, filters, kernel, t, strides, n): 69 | """Inverted Residual Block 70 | This function defines a sequence of 1 or more identical layers. 71 | # Arguments 72 | inputs: Tensor, input tensor of conv layer. 73 | filters: Integer, the dimensionality of the output space. 74 | kernel: An integer or tuple/list of 2 integers, specifying the 75 | width and height of the 2D convolution window. 76 | t: Integer, expansion factor. 77 | t is always applied to the input size. 78 | s: An integer or tuple/list of 2 integers,specifying the strides 79 | of the convolution along the width and height.Can be a single 80 | integer to specify the same value for all spatial dimensions. 81 | n: Integer, layer repeat times. 82 | # Returns 83 | Output tensor. 84 | """ 85 | 86 | x = _bottleneck(inputs, filters, kernel, t, strides) 87 | 88 | for i in range(1, n): 89 | x = _bottleneck(x, filters, kernel, t, 1, True) 90 | 91 | return x 92 | 93 | 94 | def MobileNetv2(input_shape, k): 95 | """MobileNetv2 96 | This function defines a MobileNetv2 architectures. 97 | # Arguments 98 | input_shape: An integer or tuple/list of 3 integers, shape 99 | of input tensor. 100 | k: Integer, layer repeat times. 101 | # Returns 102 | MobileNetv2 model. 103 | """ 104 | 105 | inputs = Input(shape=input_shape) 106 | x = _conv_block(inputs, 32, (3, 3), strides=(2, 2)) 107 | 108 | x = _inverted_residual_block(x, 16, (3, 3), t=1, strides=1, n=1) 109 | x = _inverted_residual_block(x, 24, (3, 3), t=6, strides=2, n=2) 110 | x = _inverted_residual_block(x, 32, (3, 3), t=6, strides=2, n=3) 111 | x = _inverted_residual_block(x, 64, (3, 3), t=6, strides=2, n=4) 112 | x = _inverted_residual_block(x, 96, (3, 3), t=6, strides=1, n=3) 113 | x = _inverted_residual_block(x, 160, (3, 3), t=6, strides=2, n=3) 114 | x = _inverted_residual_block(x, 320, (3, 3), t=6, strides=1, n=1) 115 | 116 | x = _conv_block(x, 1280, (1, 1), strides=(1, 1)) 117 | x = GlobalAveragePooling2D()(x) 118 | x = Reshape((1, 1, 1280))(x) 119 | x = Dropout(0.3, name='Dropout')(x) 120 | x = Conv2D(k, (1, 1), padding='same')(x) 121 | 122 | x = Activation('softmax', name='softmax')(x) 123 | output = Reshape((k,))(x) 124 | 125 | return Model(inputs, output) 126 | 127 | if __name__ == '__main__': 128 | model = MobileNetv2((224, 224, 3), 1000) 129 | model.summary() -------------------------------------------------------------------------------- /NetModel/QANet_keras.py: -------------------------------------------------------------------------------- 1 | from keras.layers import * 2 | from keras.regularizers import * 3 | from keras.models import * 4 | from KerasLayer.context2query_attention import context2query_attention 5 | from KerasLayer.multihead_attention import Attention as MultiHeadAttention 6 | from KerasLayer.position_embedding import Position_Embedding as PositionEmbedding 7 | from KerasLayer.QAoutputBlock import QAoutputBlock 8 | from keras.optimizers import * 9 | from keras.callbacks import * 10 | from KerasLayer.layer_dropout import LayerDropout 11 | from keras.initializers import * 12 | K.square() 13 | regularizer = l2(3e-7) 14 | VarianceScaling(scale=1.0, mode='fan_in', distribution='normal', seed=2018) 15 | 16 | def mask_logits(inputs, mask, mask_value=-1e12, axis=1, time_dim=1): 17 | mask = K.cast(mask, tf.int32) 18 | mask = K.one_hot(mask[:, 0], K.shape(inputs)[time_dim]) 19 | mask = 1 - K.cumsum(mask, 1) 20 | mask = tf.cast(mask, tf.float32) 21 | if axis != 0: 22 | mask = tf.expand_dims(mask, axis) 23 | return inputs + mask_value * (1 - mask) 24 | 25 | def highway(highway_layers, x, num_layers=2, dropout=0.0): 26 | # reduce dim 27 | x = highway_layers[0](x) 28 | for i in range(num_layers): 29 | T = highway_layers[i * 2 + 1](x) 30 | H = highway_layers[i * 2 + 2](x) 31 | H = Dropout(dropout)(H) 32 | x = Lambda(lambda v: v[0] * v[1] + v[2] * (1 - v[1]))([H, T, x]) 33 | return x 34 | 35 | def conv_block(conv_layers, x, num_conv=4, dropout=0.0, l=1., L=1.): 36 | x = Lambda(lambda v: K.expand_dims(v, axis=2))(x) 37 | for i in range(num_conv): 38 | residual = x 39 | x = BatchNormalization()(x) 40 | x = Dropout(dropout)(x) 41 | x = conv_layers[i][0](x) 42 | x = conv_layers[i][1](x) 43 | x = LayerDropout(dropout * (l / L))([x, residual]) 44 | x = Lambda(lambda v: tf.squeeze(v, axis=2))(x) 45 | return x 46 | 47 | def attention_block(attention_layer, x, seq_len, dropout=0.0, l=1., L=1.): 48 | residual = x 49 | x = BatchNormalization()(x) 50 | x = Dropout(dropout)(x) 51 | x1 = attention_layer[0](x) 52 | x2 = attention_layer[1](x) 53 | x = attention_layer[2]([x1,x2,seq_len]) 54 | x = LayerDropout(dropout * (l / L))([x, residual]) 55 | return x 56 | 57 | def feed_forward_block(FeedForward_layers, x, dropout=0.0, l=1., L=1.): 58 | residual = x 59 | x = BatchNormalization()(x) 60 | x = Dropout(dropout)(x) 61 | x = FeedForward_layers[0](x) 62 | x = FeedForward_layers[1](x) 63 | x = LayerDropout(dropout * (l / L))([x, residual]) 64 | return x 65 | 66 | def QANet(word_dim=300, char_dim=64, cont_limit=400, ques_limit=50, char_limit=16, word_mat=None, char_mat=None, 67 | char_input_size=1000, filters=128, num_head=8, dropout=0.1, ans_limit=30): 68 | # Input Embedding Layer 69 | contw_input = Input((cont_limit,)) 70 | quesw_input = Input((ques_limit,)) 71 | contc_input = Input((cont_limit, char_limit)) 72 | quesc_input = Input((ques_limit, char_limit)) 73 | 74 | # get mask 75 | c_mask = Lambda(lambda x: tf.cast(x, tf.bool))(contw_input) 76 | q_mask = Lambda(lambda x: tf.cast(x, tf.bool))(quesw_input) 77 | cont_len = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(tf.cast(x, tf.int32), axis=1), axis=1))(c_mask) 78 | ques_len = Lambda(lambda x: tf.expand_dims(tf.reduce_sum(tf.cast(x, tf.int32), axis=1), axis=1))(q_mask) 79 | 80 | # embedding word 81 | WordEmbedding = Embedding(word_mat.shape[0], word_dim, weights=[word_mat], mask_zero=False, trainable=False) 82 | xw_cont = WordEmbedding(contw_input) 83 | xw_ques = WordEmbedding(quesw_input) 84 | 85 | # embedding char 86 | CharEmbedding = Embedding(char_input_size, char_dim, weights=[char_mat], input_length=char_limit, mask_zero=False, 87 | name='char_embedding') 88 | xc_cont = CharEmbedding(contc_input) 89 | xc_ques = CharEmbedding(quesc_input) 90 | char_conv = Conv1D(filters, 5, activation='relu', kernel_regularizer=regularizer, name='char_conv') 91 | xc_cont = Lambda(lambda x: tf.reshape(x, (-1, char_limit, char_dim)))(xc_cont) 92 | xc_ques = Lambda(lambda x: tf.reshape(x, (-1, char_limit, char_dim)))(xc_ques) 93 | xc_cont = char_conv(xc_cont) 94 | xc_ques = char_conv(xc_ques) 95 | xc_cont = GlobalMaxPooling1D()(xc_cont) 96 | xc_ques = GlobalMaxPooling1D()(xc_ques) 97 | xc_cont = Lambda(lambda x: tf.reshape(x, (-1, cont_limit, filters)))(xc_cont) 98 | xc_ques = Lambda(lambda x: tf.reshape(x, (-1, ques_limit, filters)))(xc_ques) 99 | 100 | # highwayNet 101 | x_cont = Concatenate()([xw_cont, xc_cont]) 102 | x_ques = Concatenate()([xw_ques, xc_ques]) 103 | 104 | # highway shared layers 105 | highway_layers = [Conv1D(filters, 1, kernel_regularizer=regularizer)] 106 | for i in range(2): 107 | highway_layers.append(Conv1D(filters, 1, kernel_regularizer=regularizer, activation='sigmoid')) 108 | highway_layers.append(Conv1D(filters, 1, kernel_regularizer=regularizer, activation='linear')) 109 | x_cont = highway(highway_layers, x_cont, num_layers=2, dropout=dropout) 110 | x_ques = highway(highway_layers, x_ques, num_layers=2, dropout=dropout) 111 | 112 | # build shared layers 113 | # shared convs 114 | DepthwiseConv_share_1 = [] 115 | for i in range(4): 116 | DepthwiseConv_share_1.append([DepthwiseConv2D((7, 1), activation='relu', kernel_regularizer=regularizer, 117 | padding='same', depth_multiplier=1), 118 | Conv2D(filters, 1, padding='same', kernel_regularizer=regularizer)]) 119 | # shared attention 120 | head_size = filters // num_head 121 | SelfAttention_share_1 = [Conv1D(2 * filters, 1, kernel_regularizer=regularizer), 122 | Conv1D(filters, 1, kernel_regularizer=regularizer), 123 | MultiHeadAttention(filters, num_head, dropout=0.1, bias=False)] 124 | # shared feed-forward 125 | FeedForward_share_1 = [] 126 | FeedForward_share_1.append(Conv1D(filters, 1, kernel_regularizer=regularizer, activation='relu')) 127 | FeedForward_share_1.append(Conv1D(filters, 1, kernel_regularizer=regularizer, activation='linear')) 128 | 129 | # context part 130 | x_cont = PositionEmbedding()(x_cont) 131 | x_cont = conv_block(DepthwiseConv_share_1, x_cont, 4, dropout) 132 | x_cont = attention_block(SelfAttention_share_1, x_cont, cont_len, dropout) 133 | x_cont = feed_forward_block(FeedForward_share_1, x_cont, dropout) 134 | 135 | # question part 136 | x_ques = PositionEmbedding()(x_ques) 137 | x_ques = conv_block(DepthwiseConv_share_1, x_ques, 4, dropout) 138 | x_ques = attention_block(SelfAttention_share_1, x_ques, ques_len, dropout) 139 | x_ques = feed_forward_block(FeedForward_share_1, x_ques, dropout) 140 | 141 | # Context_to_Query_Attention_Layer 142 | x = context2query_attention(512, cont_limit, ques_limit, dropout)([x_cont, x_ques, cont_len, ques_len]) 143 | x = Conv1D(filters, 1, kernel_regularizer=regularizer, activation='linear')(x) 144 | 145 | # Model_Encoder_Layer 146 | # shared layers 147 | DepthwiseConv_share_2 = [] 148 | SelfAttention_share_2 = [] 149 | FeedForward_share_2 = [] 150 | for i in range(7): 151 | DepthwiseConv_share_2_temp = [] 152 | for i in range(2): 153 | DepthwiseConv_share_2_temp.append([DepthwiseConv2D((5, 1), activation='relu', 154 | kernel_regularizer=regularizer, padding='same', 155 | depth_multiplier=1), 156 | Conv2D(filters, 1, padding='same', kernel_regularizer=regularizer)]) 157 | DepthwiseConv_share_2.append(DepthwiseConv_share_2_temp) 158 | SelfAttention_share_2.append([Conv1D(2 * filters, 1, kernel_regularizer=regularizer), 159 | Conv1D(filters, 1, kernel_regularizer=regularizer), 160 | MultiHeadAttention(filters, num_head, dropout=0.1, bias=True)]) 161 | FeedForward_share_2.append([Conv1D(filters, 1, kernel_regularizer=regularizer, activation='relu'), 162 | Conv1D(filters, 1, kernel_regularizer=regularizer, activation='linear')]) 163 | 164 | outputs = [x] 165 | for i in range(3): 166 | x = outputs[-1] 167 | for j in range(7): 168 | x = PositionEmbedding()(x) 169 | x = conv_block(DepthwiseConv_share_2[j], x, 2, dropout, l=j, L=7) 170 | x = attention_block(SelfAttention_share_2[j], x, cont_len, dropout, l=j, L=7) 171 | x = feed_forward_block(FeedForward_share_2[j], x, dropout, l=j, L=7) 172 | outputs.append(x) 173 | 174 | # Output_Layer 175 | x_start = Concatenate()([outputs[1], outputs[2]]) 176 | x_start = Conv1D(1, 1, kernel_regularizer=regularizer, activation='linear')(x_start) 177 | x_start = Lambda(lambda x: tf.squeeze(x, axis=-1))(x_start) 178 | x_start = Lambda(lambda x: mask_logits(x[0], x[1], axis=0, time_dim=1))([x_start, cont_len]) 179 | x_start = Lambda(lambda x: K.softmax(x), name='start')(x_start) 180 | 181 | x_end = Concatenate()([outputs[1], outputs[3]]) 182 | x_end = Conv1D(1, 1, kernel_regularizer=regularizer, activation='linear')(x_end) 183 | x_end = Lambda(lambda x: tf.squeeze(x, axis=-1))(x_end) 184 | x_end = Lambda(lambda x: mask_logits(x[0], x[1], axis=0, time_dim=1))([x_end, cont_len]) 185 | x_end = Lambda(lambda x: K.softmax(x), name='end')(x_end) 186 | 187 | x_start_fin, x_end_fin = QAoutputBlock(ans_limit)([x_start,x_end]) 188 | return Model(inputs=[contw_input, quesw_input, contc_input, quesc_input], outputs=[x_start, x_end, x_start_fin, x_end_fin]) 189 | 190 | embedding_matrix = np.random.random((10000,300)) 191 | embedding_matrix_char = np.random.random((1000,64)) 192 | model=QANet(word_mat=embedding_matrix,char_mat=embedding_matrix_char) 193 | # model.summary() 194 | 195 | optimizer=Adam(lr=0.001,beta_1=0.8,beta_2=0.999,epsilon=1e-7) 196 | model.compile(optimizer=optimizer, loss=['categorical_crossentropy','categorical_crossentropy','mae','mae'], loss_weights=[1, 1, 0, 0]) 197 | # 198 | # # call backs 199 | # class LRSetting(Callback): 200 | # def on_batch_begin(self, batch, logs=None): 201 | # lr = min(0.001, 0.001 / np.log(999.) * np.log(batch + 1)) 202 | # K.set_value(self.model.optimizer.lr, lr) 203 | # lr_setting = LRSetting() 204 | # check_point = ModelCheckpoint('model/QANetv02.h5', monitor='val_loss', verbose=0, save_best_only=True,save_weights_only=True, mode='auto', period=1) 205 | # early_stop = EarlyStopping(monitor='val_loss', patience=10, verbose=1, mode='auto') 206 | # 207 | # load data 208 | char_dim=64 209 | cont_limit=400 210 | ques_limit=50 211 | char_limit=16 212 | 213 | context_word = np.random.randint(0, 10000, (300, cont_limit)) 214 | question_word = np.random.randint(0, 10000, (300, ques_limit)) 215 | context_char = np.random.randint(0, 96, (300, cont_limit, char_limit)) 216 | question_char = np.random.randint(0, 96, (300, ques_limit, char_limit)) 217 | start_label = np.random.randint(0, 2, (300, cont_limit)) 218 | end_label = np.random.randint(0, 2, (300, cont_limit)) 219 | start_label_fin = np.argmax(start_label,axis=-1) 220 | end_label_fin = np.argmax(end_label,axis=-1) 221 | 222 | model.fit([context_word,question_word,context_char,question_char],[start_label, end_label, start_label_fin, end_label_fin],batch_size=8) -------------------------------------------------------------------------------- /NetModel/QANet_tensorflow/QANet_model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import load_model 3 | import tensorflow_hub as hub 4 | import keras.backend as K 5 | from layers import regularizer, residual_block, highway, conv, mask_logits, optimized_trilinear_for_attention, \ 6 | total_params 7 | 8 | 9 | class Model(object): 10 | def __init__(self, config, word_mat=None, char_mat=None, test=False, use_elmo=False, use_cove=False): 11 | 12 | # hyper-parameter 13 | self.char_dim = config['char_dim'] 14 | self.cont_limit = config['cont_limit'] if not test else 1000 15 | self.ques_limit = config['ques_limit'] if not test else 50 16 | self.char_limit = config['char_limit'] 17 | self.ans_limit = config['ans_limit'] 18 | self.filters = config['filters'] 19 | self.num_heads = config['num_heads'] 20 | self.batch_size = config['batch_size'] 21 | self.l2_norm = config['l2_norm'] 22 | self.decay = config['decay'] 23 | self.learning_rate = config['learning_rate'] 24 | self.grad_clip = config['grad_clip'] 25 | self.use_elmo = use_elmo 26 | self.use_cove = use_cove 27 | self.dropout = tf.placeholder_with_default(0.0, (), name="dropout") 28 | 29 | # embedding layer 30 | self.word_mat = tf.get_variable("word_mat", initializer=tf.constant(word_mat, dtype=tf.float32), 31 | trainable=False) 32 | self.char_mat = tf.get_variable("char_mat", initializer=tf.constant(char_mat, dtype=tf.float32), trainable=True) 33 | 34 | # input tensor 35 | self.contw_input_ = tf.placeholder(tf.int32, [None, self.cont_limit], "context_word") 36 | self.quesw_input_ = tf.placeholder(tf.int32, [None, self.ques_limit], "question_word") 37 | self.contc_input_ = tf.placeholder(tf.int32, [None, self.cont_limit, self.char_limit], "context_char") 38 | self.quesc_input_ = tf.placeholder(tf.int32, [None, self.ques_limit, self.char_limit], "question_char") 39 | self.y_start_ = tf.placeholder(tf.int32, [None, self.cont_limit + 1], "answer_start_index") 40 | self.y_end_ = tf.placeholder(tf.int32, [None, self.cont_limit + 1], "answer_end_index") 41 | self.contw_strings = tf.placeholder(tf.string, [None, self.cont_limit], 'contw_strings') 42 | self.quesw_strings = tf.placeholder(tf.string, [None, self.ques_limit], 'quesw_strings') 43 | 44 | self.c_mask = tf.cast(self.contw_input_, tf.bool) 45 | self.q_mask = tf.cast(self.quesw_input_, tf.bool) 46 | self.cont_len = tf.reduce_sum(tf.cast(self.c_mask, tf.int32), axis=1) 47 | self.ques_len = tf.reduce_sum(tf.cast(self.q_mask, tf.int32), axis=1) 48 | if self.use_elmo: 49 | elmo = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True) 50 | self.cont_elmo = elmo(inputs={"tokens": self.contw_strings, "sequence_len": self.cont_len}, 51 | signature="tokens", as_dict=True)["elmo"] 52 | self.ques_elmo = elmo(inputs={"tokens": self.quesw_strings, "sequence_len": self.ques_len}, 53 | signature="tokens", as_dict=True)["elmo"] 54 | 55 | # if self.use_cove: 56 | # self.cove_model = load_model('Keras_CoVe_V2.h5') 57 | # self.cove_model.trainable = False 58 | 59 | # slice for maxlen in each batch 60 | self.c_maxlen = tf.reduce_max(self.cont_len) 61 | self.q_maxlen = tf.reduce_max(self.ques_len) 62 | 63 | self.contw_input = tf.slice(self.contw_input_, [0, 0], [-1, self.c_maxlen]) 64 | self.quesw_input = tf.slice(self.quesw_input_, [0, 0], [-1, self.q_maxlen]) 65 | self.c_mask = tf.slice(self.c_mask, [0, 0], [-1, self.c_maxlen]) 66 | self.q_mask = tf.slice(self.q_mask, [0, 0], [-1, self.q_maxlen]) 67 | self.contc_input = tf.slice(self.contc_input_, [0, 0, 0], [-1, self.c_maxlen, self.char_limit]) 68 | self.quesc_input = tf.slice(self.quesc_input_, [0, 0, 0], [-1, self.q_maxlen, self.char_limit]) 69 | self.y_start = tf.slice(self.y_start_, [0, 0], [-1, self.c_maxlen + 1]) 70 | self.y_end = tf.slice(self.y_end_, [0, 0], [-1, self.c_maxlen + 1]) 71 | if self.use_elmo: 72 | self.cont_elmo = tf.slice(self.cont_elmo, [0, 0, 0], [-1, self.c_maxlen, 1024]) 73 | self.ques_elmo = tf.slice(self.ques_elmo, [0, 0, 0], [-1, self.q_maxlen, 1024]) 74 | 75 | # init model & complie 76 | self.build_model() 77 | total_params() 78 | self.complie() 79 | 80 | def build_model(self): 81 | PL, QL, CL, d, dc, nh = self.c_maxlen, self.q_maxlen, self.char_limit, self.filters, self.char_dim, self.num_heads 82 | 83 | with tf.variable_scope("Input_Embedding_Layer"): 84 | ch_emb = tf.reshape(tf.nn.embedding_lookup(self.char_mat, self.contc_input), [-1, CL, dc]) 85 | qh_emb = tf.reshape(tf.nn.embedding_lookup(self.char_mat, self.quesc_input), [-1, CL, dc]) 86 | ch_emb = tf.nn.dropout(ch_emb, 1.0 - 0.5 * self.dropout) 87 | qh_emb = tf.nn.dropout(qh_emb, 1.0 - 0.5 * self.dropout) 88 | 89 | # Bidaf style conv-highway encoder 90 | ch_emb = conv(ch_emb, d, bias=True, activation=tf.nn.relu, kernel_size=5, name="char_conv", reuse=None) 91 | qh_emb = conv(qh_emb, d, bias=True, activation=tf.nn.relu, kernel_size=5, name="char_conv", reuse=True) 92 | 93 | ch_emb = tf.reduce_max(ch_emb, axis=1) 94 | qh_emb = tf.reduce_max(qh_emb, axis=1) 95 | 96 | ch_emb = tf.reshape(ch_emb, [-1, PL, ch_emb.shape[-1]]) 97 | qh_emb = tf.reshape(qh_emb, [-1, QL, ch_emb.shape[-1]]) 98 | 99 | c_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.contw_input), 1.0 - self.dropout) 100 | q_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.quesw_input), 1.0 - self.dropout) 101 | 102 | # if self.use_cove: 103 | # c_emb_cove = self.cove_model(c_emb) 104 | # q_emb_cove = self.cove_model(q_emb) 105 | # c_emb = tf.concat([c_emb, c_emb_cove], axis=-1) 106 | # q_emb = tf.concat([q_emb, q_emb_cove], axis=-1) 107 | 108 | c_emb = tf.concat([c_emb, ch_emb], axis=2) 109 | q_emb = tf.concat([q_emb, qh_emb], axis=2) 110 | 111 | if self.use_elmo: 112 | c_emb = tf.concat([c_emb, self.cont_elmo], axis=-1) 113 | q_emb = tf.concat([q_emb, self.ques_elmo], axis=-1) 114 | 115 | c_emb = highway(c_emb, size=d, scope="highway", dropout=self.dropout, reuse=None) 116 | q_emb = highway(q_emb, size=d, scope="highway", dropout=self.dropout, reuse=True) 117 | 118 | with tf.variable_scope("Embedding_Encoder_Layer"): 119 | c = residual_block(c_emb, 120 | num_blocks=1, 121 | num_conv_layers=4, 122 | kernel_size=7, 123 | mask=self.c_mask, 124 | num_filters=d, 125 | num_heads=nh, 126 | seq_len=self.cont_len, 127 | scope="Encoder_Residual_Block", 128 | bias=False, 129 | dropout=self.dropout) 130 | q = residual_block(q_emb, 131 | num_blocks=1, 132 | num_conv_layers=4, 133 | kernel_size=7, 134 | mask=self.q_mask, 135 | num_filters=d, 136 | num_heads=nh, 137 | seq_len=self.ques_len, 138 | scope="Encoder_Residual_Block", 139 | reuse=True, 140 | bias=False, 141 | dropout=self.dropout) 142 | 143 | with tf.variable_scope("Context_to_Query_Attention_Layer"): 144 | S = optimized_trilinear_for_attention([c, q], self.c_maxlen, self.q_maxlen, 145 | input_keep_prob=1.0 - self.dropout) 146 | mask_q = tf.expand_dims(self.q_mask, 1) 147 | S_ = tf.nn.softmax(mask_logits(S, mask=mask_q)) 148 | mask_c = tf.expand_dims(self.c_mask, 2) 149 | S_T = tf.transpose(tf.nn.softmax(mask_logits(S, mask=mask_c), dim=1), (0, 2, 1)) 150 | c2q = tf.matmul(S_, q) 151 | q2c = tf.matmul(tf.matmul(S_, S_T), c) 152 | attention_outputs = [c, c2q, c * c2q, c * q2c] 153 | 154 | with tf.variable_scope("Model_Encoder_Layer"): 155 | attention_inputs = tf.concat(attention_outputs, axis=-1) 156 | enc = [conv(attention_inputs, d, name="input_projection")] 157 | for i in range(3): 158 | if i % 2 == 0: # dropout every 2 blocks 159 | enc[i] = tf.nn.dropout(enc[i], 1.0 - self.dropout) 160 | enc.append(residual_block(enc[i], 161 | num_blocks=7, 162 | num_conv_layers=2, 163 | kernel_size=5, 164 | mask=self.c_mask, 165 | num_filters=d, 166 | num_heads=nh, 167 | seq_len=self.cont_len, 168 | scope="Model_Encoder", 169 | bias=False, 170 | reuse=True if i > 0 else None, 171 | dropout=self.dropout)) 172 | 173 | with tf.variable_scope("Output_Layer"): 174 | start_logits = tf.concat([enc[1], enc[2]], axis=-1) 175 | end_logits = tf.concat([enc[1], enc[3]], axis=-1) 176 | if self.use_elmo: 177 | start_logits = tf.concat((start_logits, self.cont_elmo), axis=-1) 178 | end_logits = tf.concat((end_logits, self.cont_elmo), axis=-1) 179 | 180 | start_logits = tf.squeeze(conv(start_logits, 1, bias=False, name="start_pointer"), -1) 181 | end_logits = tf.squeeze(conv(end_logits, 1, bias=False, name="end_pointer"), -1) 182 | unanswer_bias = tf.get_variable("unanswer_bias", [1], 183 | regularizer=tf.contrib.layers.l2_regularizer(scale=3e-7), 184 | initializer=tf.zeros_initializer()) 185 | unanswer_bias = tf.reshape(tf.tile(unanswer_bias, [self.batch_size]), [-1, 1]) 186 | self.logits1 = tf.concat((unanswer_bias, mask_logits(start_logits, mask=self.c_mask)), axis=-1) 187 | self.logits2 = tf.concat((unanswer_bias, mask_logits(end_logits, mask=self.c_mask)), axis=-1) 188 | start_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits1, labels=self.y_start) 189 | end_loss = tf.nn.softmax_cross_entropy_with_logits(logits=self.logits2, labels=self.y_end) 190 | self.loss = tf.reduce_mean(start_loss + end_loss) 191 | if self.l2_norm is not None: 192 | variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) 193 | l2_loss = tf.contrib.layers.apply_regularization(regularizer, variables) 194 | self.loss += l2_loss 195 | 196 | # output 197 | outer = tf.matmul(tf.expand_dims(tf.nn.softmax(self.logits1), axis=2), 198 | tf.expand_dims(tf.nn.softmax(self.logits2), axis=1)) 199 | outer = tf.matrix_band_part(outer, 0, self.ans_limit) 200 | self.output1 = tf.argmax(tf.reduce_max(outer, axis=2), axis=1) - 1 201 | self.output2 = tf.argmax(tf.reduce_max(outer, axis=1), axis=1) - 1 202 | 203 | if self.decay is not None: 204 | self.var_ema = tf.train.ExponentialMovingAverage(self.decay) 205 | ema_op = self.var_ema.apply(tf.trainable_variables()) 206 | with tf.control_dependencies([ema_op]): 207 | self.loss = tf.identity(self.loss) 208 | self.assign_vars = [] 209 | for var in tf.global_variables(): 210 | v = self.var_ema.average(var) 211 | if v is not None: 212 | self.assign_vars.append(tf.assign(var, v)) 213 | 214 | def complie(self): 215 | self.global_step = tf.get_variable('global_step', shape=[], dtype=tf.int32, 216 | initializer=tf.constant_initializer(0), trainable=False) 217 | self.lr = tf.minimum(self.learning_rate, 218 | 0.001 / tf.log(999.) * tf.log(tf.cast(self.global_step, tf.float32) + 1)) 219 | self.opt = tf.train.AdamOptimizer(learning_rate=self.lr, beta1=0.8, beta2=0.999, epsilon=1e-7) 220 | grads = self.opt.compute_gradients(self.loss) 221 | gradients, variables = zip(*grads) 222 | capped_grads, _ = tf.clip_by_global_norm(gradients, self.grad_clip) 223 | self.train_op = self.opt.apply_gradients(zip(capped_grads, variables), global_step=self.global_step) 224 | -------------------------------------------------------------------------------- /NetModel/QANet_tensorflow/test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import QANet_model 3 | import QANet_model_alter 4 | import tensorflow as tf 5 | import util 6 | import json 7 | import os 8 | 9 | os.environ["CUDA_VISIBLE_DEVICES"] = '3' 10 | 11 | 12 | def training_shuffle(data, seed=None): 13 | if seed is not None: 14 | np.random.seed(seed) 15 | index = np.arange(data[0].shape[0]) 16 | np.random.shuffle(index) 17 | for i, d in enumerate(data): 18 | data[i] = data[i][index, ::] 19 | return data 20 | 21 | 22 | def next_batch(data, batch_size, iteration): 23 | data_temp = [] 24 | start_index = iteration * batch_size 25 | if iteration == (data[0].shape[0] // batch_size) - 1: 26 | end_index = -1 27 | else: 28 | end_index = (iteration + 1) * batch_size 29 | for i, d in enumerate(data): 30 | if end_index != -1: 31 | data_temp.append(data[i][start_index: end_index, ::]) 32 | else: 33 | data_temp.append(data[i][-1*batch_size:, ::]) 34 | return data_temp 35 | 36 | 37 | # load testset 38 | test_context_word=np.load('dataset/test_contw_input.npy') 39 | test_question_word=np.load('dataset/test_quesw_input.npy') 40 | test_context_char=np.load('dataset/test_contc_input.npy') 41 | test_question_char=np.load('dataset/test_quesc_input.npy') 42 | test_start_label=np.load('dataset/test_y_start.npy') 43 | test_end_label=np.load('dataset/test_y_end.npy') 44 | test_qid=np.load('dataset/test_qid.npy').astype(np.int32) 45 | context_string = np.load('dataset/test_contw_strings.npy') 46 | ques_string = np.load('dataset/test_quesw_strings.npy') 47 | with open('dataset/test_eval.json', "r") as fh: 48 | eval_file = json.load(fh) 49 | 50 | # load embedding matrix 51 | word_mat = np.load('dataset/word_emb_mat.npy') 52 | char_mat = np.load('dataset/char_emb_mat.npy') 53 | 54 | test_set = [test_context_word, test_question_word, test_context_char, test_question_char, context_string, ques_string, test_start_label, test_end_label] 55 | 56 | config = { 57 | 'char_dim': 64, 58 | 'cont_limit': 400, 59 | 'ques_limit': 50, 60 | 'char_limit': 16, 61 | 'ans_limit': 30, 62 | 'filters': 128, 63 | 'num_heads': 1, 64 | 'dropout': 0.1, 65 | 'l2_norm': 3e-7, 66 | 'decay': 0.9999, 67 | 'learning_rate': 1e-3, 68 | 'grad_clip': 5.0, 69 | 'batch_size': 32, 70 | 'epoch': 30, 71 | 'path': 'QANetV201' 72 | } 73 | 74 | model = QANet_model.Model(config, word_mat=word_mat, char_mat=char_mat) 75 | sess_config = tf.ConfigProto(allow_soft_placement=True) 76 | sess_config.gpu_options.allow_growth = True 77 | 78 | with tf.Session(config=sess_config) as sess: 79 | sess.run(tf.global_variables_initializer()) 80 | saver = tf.train.Saver() 81 | saver.restore(sess, tf.train.latest_checkpoint(os.path.join('model',str(config['path'])+'/'))) 82 | if config['decay'] < 1.0: 83 | sess.run(model.assign_vars) 84 | n_batch_test = test_context_word.shape[0] // config['batch_size'] 85 | 86 | # test step 87 | sum_loss_val = 0 88 | y1s = [] 89 | y2s = [] 90 | last_test_str = '\r' 91 | for i in range(n_batch_test): 92 | contw_input, quesw_input, contc_input, quesc_input, contw_string, quesw_string, y_start, y_end \ 93 | = next_batch(test_set, config['batch_size'], i) 94 | loss_value, y1, y2 = sess.run([model.loss, model.output1, model.output2], 95 | feed_dict={model.contw_input_: contw_input, model.quesw_input_: quesw_input, 96 | model.contc_input_: contc_input, model.quesc_input_: quesc_input, 97 | model.contw_strings: contw_string, model.quesw_strings: quesw_string, 98 | model.y_start_: y_start, model.y_end_: y_end}) 99 | y1s.append(y1) 100 | y2s.append(y2) 101 | sum_loss_val += loss_value 102 | last_test_str = "\r[test:%d/%d] -loss: %.4f" % (i + 1, n_batch_test, sum_loss_val / (i + 1)) 103 | print(last_test_str, end=' ', flush=True) 104 | y1s = np.concatenate(y1s) 105 | y2s = np.concatenate(y2s) 106 | answer_dict, _, noanswer_num = util.convert_tokens(eval_file, test_qid.tolist(), y1s.tolist(), y2s.tolist()) 107 | metrics = util.evaluate(eval_file, answer_dict) 108 | print(last_test_str, 109 | " -EM: %.2f%%, -F1: %.2f%% -Noanswer: %d" % (metrics['exact_match'], metrics['f1'], noanswer_num), end=' ', 110 | flush=True) 111 | print('\n') -------------------------------------------------------------------------------- /NetModel/QANet_tensorflow/train.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import pandas as pd 3 | import QANet_model 4 | import tensorflow as tf 5 | import util 6 | import json 7 | import os 8 | import pickle 9 | import time 10 | 11 | os.environ["CUDA_VISIBLE_DEVICES"] = '3' 12 | 13 | 14 | def training_shuffle(data, seed=None): 15 | if seed is not None: 16 | np.random.seed(seed) 17 | index = np.arange(data[0].shape[0]) 18 | np.random.shuffle(index) 19 | for i, d in enumerate(data): 20 | if len(d.shape)>1: 21 | data[i] = data[i][index, ::] 22 | else: 23 | data[i] = data[i][index] 24 | return data 25 | 26 | 27 | def next_batch(data, batch_size, iteration): 28 | data_temp = [] 29 | start_index = iteration * batch_size 30 | if iteration == (data[0].shape[0] // batch_size) - 1: 31 | end_index = -1 32 | else: 33 | end_index = (iteration + 1) * batch_size 34 | for i, d in enumerate(data): 35 | if end_index != -1: 36 | if len(d.shape) > 1: 37 | data_temp.append(data[i][start_index: end_index, ::]) 38 | else: 39 | data_temp.append(data[i][start_index: end_index]) 40 | else: 41 | if len(d.shape) > 1: 42 | data_temp.append(data[i][-1 * batch_size:, ::]) 43 | else: 44 | data_temp.append(data[i][-1 * batch_size:]) 45 | return data_temp 46 | 47 | def cal_ETA(t_start, i, n_batch): 48 | t_temp = time.time() 49 | t_avg = float(int(t_temp) - int(t_start)) / float(i + 1) 50 | if n_batch - i - 1 > 0: 51 | return int((n_batch - i - 1) * t_avg) 52 | else: 53 | return int(t_temp)-int(t_start) 54 | 55 | 56 | # load trainset 57 | context_word = np.load('dataset/train_contw_input.npy').astype(np.int32) 58 | question_word = np.load('dataset/train_quesw_input.npy').astype(np.int32) 59 | context_char = np.load('dataset/train_contc_input.npy').astype(np.int32) 60 | question_char = np.load('dataset/train_quesc_input.npy').astype(np.int32) 61 | start_label = np.load('dataset/train_y_start.npy').astype(np.int32) 62 | end_label = np.load('dataset/train_y_end.npy').astype(np.int32) 63 | context_string = np.load('dataset/train_contw_strings.npy') 64 | ques_string = np.load('dataset/train_quesw_strings.npy') 65 | 66 | # load valset 67 | val_context_word = np.load('dataset/test_contw_input.npy').astype(np.int32) 68 | val_question_word = np.load('dataset/test_quesw_input.npy').astype(np.int32) 69 | val_context_char = np.load('dataset/test_contc_input.npy').astype(np.int32) 70 | val_question_char = np.load('dataset/test_quesc_input.npy').astype(np.int32) 71 | val_start_label = np.load('dataset/test_y_start.npy').astype(np.int32) 72 | val_end_label = np.load('dataset/test_y_end.npy').astype(np.int32) 73 | val_qid = np.load('dataset/test_qid.npy').astype(np.int32) 74 | val_context_string = np.load('dataset/test_contw_strings.npy') 75 | val_ques_string = np.load('dataset/test_quesw_strings.npy') 76 | 77 | with open('dataset/test_eval.json', "r") as fh: 78 | eval_file = json.load(fh) 79 | 80 | # load embedding matrix 81 | word_mat = np.load('dataset/word_emb_mat.npy') 82 | char_mat = np.load('dataset/char_emb_mat.npy') 83 | 84 | train_set = [context_word, question_word, context_char, question_char, context_string, ques_string, start_label, end_label] 85 | val_set = [val_context_word, val_question_word, val_context_char, val_question_char, val_context_string, val_ques_string, val_start_label, val_end_label] 86 | 87 | config = { 88 | 'char_dim': 64, 89 | 'cont_limit': 400, 90 | 'ques_limit': 50, 91 | 'char_limit': 16, 92 | 'ans_limit': 50, 93 | 'filters': 128, 94 | 'num_heads': 1, 95 | 'dropout': 0.1, 96 | 'l2_norm': 3e-7, 97 | 'decay': 0.9999, 98 | 'learning_rate': 1e-3, 99 | 'grad_clip': 5.0, 100 | 'batch_size': 32, 101 | 'epoch': 25, 102 | 'path': 'QANetV201' 103 | } 104 | 105 | model = QANet_model.Model(config, word_mat=word_mat, char_mat=char_mat,use_elmo=True) 106 | sess_config = tf.ConfigProto(allow_soft_placement=True) 107 | sess_config.gpu_options.allow_growth = True 108 | 109 | best_f1 = 0 110 | best_em = 0 111 | f1s = [] 112 | ems = [] 113 | 114 | with tf.Session(config=sess_config) as sess: 115 | if not os.path.exists(os.path.join('model',config['path'])): 116 | os.mkdir(os.path.join('model',config['path'])) 117 | sess.run(tf.global_variables_initializer()) 118 | saver = tf.train.Saver() 119 | n_batch = context_word.shape[0] // config['batch_size'] 120 | n_batch_val = val_context_word.shape[0] // config['batch_size'] 121 | for epoch in range(config['epoch']): 122 | train_set = training_shuffle(train_set) 123 | t_start = time.time() 124 | last_train_str = "\r" 125 | # training step 126 | sum_loss = 0 127 | for i in range(n_batch): 128 | contw_input, quesw_input, contc_input, quesc_input, contw_string, quesw_string, y_start, y_end \ 129 | = next_batch(train_set, config['batch_size'], i) 130 | loss_value, _ = sess.run([model.loss, model.train_op], 131 | feed_dict={model.contw_input_: contw_input, model.quesw_input_: quesw_input, 132 | model.contc_input_: contc_input, model.quesc_input_: quesc_input, 133 | model.contw_strings: contw_string, model.quesw_strings: quesw_string, 134 | model.y_start_: y_start, model.y_end_: y_end, 135 | model.dropout: config['dropout']}) 136 | sum_loss += loss_value 137 | last_train_str = "\r[epoch:%d/%d, steps:%d/%d] -ETA: %ds -loss: %.4f" % ( 138 | epoch + 1, config['epoch'], i + 1, n_batch, cal_ETA(t_start, i, n_batch), sum_loss / (i + 1)) 139 | print(last_train_str, end=' ', flush=True) 140 | 141 | # validating step 142 | sum_loss_val = 0 143 | y1s = [] 144 | y2s = [] 145 | last_val_str = "\r" 146 | for i in range(n_batch_val): 147 | contw_input, quesw_input, contc_input, quesc_input, contw_string, quesw_string, y_start, y_end \ 148 | = next_batch(val_set, config['batch_size'], i) 149 | loss_value, y1, y2 = sess.run([model.loss, model.output1, model.output2], 150 | feed_dict={model.contw_input_: contw_input, model.quesw_input_: quesw_input, 151 | model.contc_input_: contc_input, model.quesc_input_: quesc_input, 152 | model.contw_strings: contw_string, model.quesw_strings: quesw_string, 153 | model.y_start_: y_start, model.y_end_: y_end}) 154 | y1s.append(y1) 155 | y2s.append(y2) 156 | sum_loss_val += loss_value 157 | last_val_str = last_train_str + " [validate:%d/%d] -loss: %.4f" % ( 158 | i + 1, n_batch_val, sum_loss_val / (i + 1)) 159 | print(last_val_str, end=' ', flush=True) 160 | y1s = np.concatenate(y1s) 161 | y2s = np.concatenate(y2s) 162 | answer_dict, _, noanswer_num = util.convert_tokens(eval_file, val_qid.tolist(), y1s.tolist(), y2s.tolist()) 163 | metrics = util.evaluate(eval_file, answer_dict) 164 | ems.append(metrics['exact_match']) 165 | f1s.append(metrics['f1']) 166 | if metrics['f1'] > best_f1: 167 | best_f1 = metrics['f1'] 168 | saver.save(sess, os.path.join('model',config['path'], 'model.ckpt'), global_step=(epoch+1) * n_batch) 169 | print(last_val_str, " -EM: %.2f%%, -F1: %.2f%% -Noanswer: %d" % (metrics['exact_match'], metrics['f1'], noanswer_num), end=' ', flush=True) 170 | print('\n') 171 | 172 | result = pd.DataFrame([ems, f1s], index=['em', 'f1']).transpose() 173 | result.to_csv('log/result.csv', index=None) 174 | 175 | saver.save(sess, os.path.join('model',config['path'], 'model.ckpt'), global_step=config['epoch'] * n_batch) 176 | 177 | 178 | -------------------------------------------------------------------------------- /NetModel/QANet_tensorflow/util.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import re 3 | from collections import Counter 4 | import string 5 | 6 | ''' 7 | This file is taken and modified from R-Net by HKUST-KnowComp 8 | https://github.com/HKUST-KnowComp/R-Net 9 | ''' 10 | 11 | 12 | def get_record_parser(config, is_test=False): 13 | def parse(example): 14 | para_limit = config.test_para_limit if is_test else config.para_limit 15 | ques_limit = config.test_ques_limit if is_test else config.ques_limit 16 | char_limit = config.char_limit 17 | features = tf.parse_single_example(example, 18 | features={ 19 | "context_idxs": tf.FixedLenFeature([], tf.string), 20 | "ques_idxs": tf.FixedLenFeature([], tf.string), 21 | "context_char_idxs": tf.FixedLenFeature([], tf.string), 22 | "ques_char_idxs": tf.FixedLenFeature([], tf.string), 23 | "y1": tf.FixedLenFeature([], tf.string), 24 | "y2": tf.FixedLenFeature([], tf.string), 25 | "id": tf.FixedLenFeature([], tf.int64) 26 | }) 27 | context_idxs = tf.reshape(tf.decode_raw( 28 | features["context_idxs"], tf.int32), [para_limit]) 29 | ques_idxs = tf.reshape(tf.decode_raw( 30 | features["ques_idxs"], tf.int32), [ques_limit]) 31 | context_char_idxs = tf.reshape(tf.decode_raw( 32 | features["context_char_idxs"], tf.int32), [para_limit, char_limit]) 33 | ques_char_idxs = tf.reshape(tf.decode_raw( 34 | features["ques_char_idxs"], tf.int32), [ques_limit, char_limit]) 35 | y1 = tf.reshape(tf.decode_raw( 36 | features["y1"], tf.float32), [para_limit]) 37 | y2 = tf.reshape(tf.decode_raw( 38 | features["y2"], tf.float32), [para_limit]) 39 | qa_id = features["id"] 40 | return context_idxs, ques_idxs, context_char_idxs, ques_char_idxs, y1, y2, qa_id 41 | return parse 42 | 43 | 44 | def get_batch_dataset(record_file, parser, config): 45 | num_threads = tf.constant(config.num_threads, dtype=tf.int32) 46 | dataset = tf.data.TFRecordDataset(record_file).map( 47 | parser, num_parallel_calls=num_threads).shuffle(config.capacity).repeat() 48 | if config.is_bucket: 49 | buckets = [tf.constant(num) for num in range(*config.bucket_range)] 50 | 51 | def key_func(context_idxs, ques_idxs, context_char_idxs, ques_char_idxs, y1, y2, qa_id): 52 | c_len = tf.reduce_sum( 53 | tf.cast(tf.cast(context_idxs, tf.bool), tf.int32)) 54 | t = tf.clip_by_value(buckets, 0, c_len) 55 | return tf.argmax(t) 56 | 57 | def reduce_func(key, elements): 58 | return elements.batch(config.batch_size) 59 | 60 | dataset = dataset.apply(tf.contrib.data.group_by_window( 61 | key_func, reduce_func, window_size=5 * config.batch_size)).shuffle(len(buckets) * 25) 62 | else: 63 | dataset = dataset.batch(config.batch_size) 64 | return dataset 65 | 66 | 67 | def get_dataset(record_file, parser, config): 68 | num_threads = tf.constant(config.num_threads, dtype=tf.int32) 69 | dataset = tf.data.TFRecordDataset(record_file).map( 70 | parser, num_parallel_calls=num_threads).repeat().batch(config.batch_size) 71 | return dataset 72 | 73 | def convert_tokens(eval_file, qa_id, pp1, pp2, unanswer_id=-1): 74 | answer_dict = {} 75 | remapped_dict = {} 76 | noanswer_num = 0 77 | for qid, p1, p2 in zip(qa_id, pp1, pp2): 78 | context = eval_file[str(qid)]["context"] 79 | spans = eval_file[str(qid)]["spans"] 80 | uuid = eval_file[str(qid)]["uuid"] 81 | if p1==unanswer_id or p2==unanswer_id or p1>=len(spans) or p2>=len(spans): # prediction has no answer 82 | noanswer_num+=1 83 | answer_dict[str(qid)]='unanswerable' 84 | else: 85 | start_idx = spans[p1][0] 86 | end_idx = spans[p2][1] 87 | answer_dict[str(qid)] = context[start_idx: end_idx] 88 | remapped_dict[uuid] = context[start_idx: end_idx] 89 | return answer_dict, remapped_dict, noanswer_num 90 | 91 | 92 | def evaluate(eval_file, answer_dict): 93 | f1 = exact_match = total = 0 94 | for key, value in answer_dict.items(): 95 | total += 1 96 | ground_truths = eval_file[key]["answers"] 97 | prediction = value 98 | if len(ground_truths)==0: # ground truth has no answer 99 | if prediction=='unanswerable': 100 | exact_match+=1 101 | f1+=1 102 | else: 103 | exact_match += metric_max_over_ground_truths(exact_match_score, prediction, ground_truths) 104 | f1 += metric_max_over_ground_truths(f1_score,prediction, ground_truths) 105 | exact_match = 100.0 * exact_match / total 106 | f1 = 100.0 * f1 / total 107 | return {'exact_match': exact_match, 'f1': f1} 108 | 109 | 110 | def normalize_answer(s): 111 | 112 | def remove_articles(text): 113 | return re.sub(r'\b(a|an|the)\b', ' ', text) 114 | 115 | def white_space_fix(text): 116 | return ' '.join(text.split()) 117 | 118 | def remove_punc(text): 119 | exclude = set(string.punctuation) 120 | return ''.join(ch for ch in text if ch not in exclude) 121 | 122 | def lower(text): 123 | return text.lower() 124 | 125 | return white_space_fix(remove_articles(remove_punc(lower(s)))) 126 | 127 | 128 | def f1_score(prediction, ground_truth): 129 | prediction_tokens = normalize_answer(prediction).split() 130 | ground_truth_tokens = normalize_answer(ground_truth).split() 131 | common = Counter(prediction_tokens) & Counter(ground_truth_tokens) 132 | num_same = sum(common.values()) 133 | if num_same == 0: 134 | return 0 135 | precision = 1.0 * num_same / len(prediction_tokens) 136 | recall = 1.0 * num_same / len(ground_truth_tokens) 137 | f1 = (2 * precision * recall) / (precision + recall) 138 | return f1 139 | 140 | 141 | def exact_match_score(prediction, ground_truth): 142 | return (normalize_answer(prediction) == normalize_answer(ground_truth)) 143 | 144 | 145 | def metric_max_over_ground_truths(metric_fn, prediction, ground_truths): 146 | scores_for_ground_truths = [] 147 | for ground_truth in ground_truths: 148 | score = metric_fn(prediction, ground_truth) 149 | scores_for_ground_truths.append(score) 150 | return max(scores_for_ground_truths) 151 | -------------------------------------------------------------------------------- /NetModel/SAN.py: -------------------------------------------------------------------------------- 1 | # stochastic answer network in tensorflow and tensor2tensor 2 | import tensorflow as tf 3 | from tensor2tensor.layers.common_layers import conv1d, dense 4 | 5 | regularizer = tf.contrib.layers.l2_regularizer(scale = 3e-7) 6 | initializer = tf.contrib.layers.variance_scaling_initializer(factor=1.0, mode='FAN_AVG', uniform=True, dtype=tf.float32) 7 | 8 | def exp_mask(inputs, mask, mask_value=-1e30): 9 | mask = tf.cast(mask, tf.float32) 10 | return inputs + mask_value * (1 - mask) 11 | 12 | def linear_sum_attention(x, mask, dropout): 13 | alpha = tf.squeeze(conv1d(x, 1, 1, kernel_initializer=initializer, kernel_regularizer=regularizer), axis=-1) # [bs, c_len] 14 | alpha = exp_mask(alpha, mask) # [bs, c_len] 15 | alpha = tf.expand_dims(tf.nn.softmax(alpha), axis=1) # [bs, 1, c_len] 16 | x = tf.squeeze(tf.matmul(alpha, x), axis=1) # [bs, dim] 17 | x = tf.nn.dropout(x, 1.0 - dropout) 18 | return x 19 | 20 | def output_attention(c, q, filters=128 * 3, name=None): 21 | q = tf.expand_dims( 22 | dense(q, filters, name=name, reuse=tf.AUTO_REUSE, kernel_initializer=initializer, kernel_regularizer=regularizer), 23 | axis=-1) # [bs, dim, 1] 24 | cq = tf.squeeze(tf.matmul(c, q), axis=-1) # [bs, c_len] 25 | return cq 26 | 27 | def GRUCell(x, h, hidden_size=128, filters=256): 28 | with tf.variable_scope('Output_GRU_Cell', reuse=tf.AUTO_REUSE): 29 | gate_kernel = tf.get_variable("gate_kernel", 30 | shape=(hidden_size + filters, 2 * hidden_size), 31 | dtype=tf.float32, 32 | initializer=initializer, 33 | regularizer=regularizer) 34 | gate_bias = tf.get_variable("gates_bias", 35 | shape=(2 * hidden_size), 36 | dtype=tf.float32, 37 | regularizer=regularizer) 38 | candidate_kernel = tf.get_variable("candidate_kernel", 39 | shape=(hidden_size + filters, hidden_size), 40 | dtype=tf.float32, 41 | initializer=initializer, 42 | regularizer=regularizer) 43 | candidate_bias = tf.get_variable("candidate_bias", 44 | shape=(hidden_size), 45 | dtype=tf.float32, 46 | regularizer=regularizer) 47 | 48 | gate_inputs = tf.matmul(tf.concat([x, h], 1), gate_kernel) + gate_bias 49 | value = tf.nn.sigmoid(gate_inputs) 50 | r, u = tf.split(value=value, num_or_size_splits=2, axis=1) 51 | r_state = r * h 52 | candidate = tf.matmul(tf.concat([x, r_state], 1), candidate_kernel) + candidate_bias 53 | c = tf.nn.tanh(candidate) 54 | new_h = u * h + (1 - u) * c 55 | return new_h 56 | 57 | def SAN(cont, ques, q_mask, name, num_turn=5, hidden_size=128, filters=128*3, dropout=0.3): 58 | with tf.variable_scope(name): 59 | ques_mem = linear_sum_attention(ques, q_mask, dropout) 60 | start_scores_list = [] 61 | end_scores_list = [] 62 | for turn in range(num_turn): 63 | st_scores = output_attention(cont, ques_mem, filters=filters, name='start_output') # [bs, c_len] 64 | start_scores_list.append(st_scores) 65 | end_scores = output_attention(cont, ques_mem, filters=filters, name='end_output') # [bs, c_len] 66 | end_scores_list.append(end_scores) 67 | x = tf.squeeze(tf.matmul(tf.expand_dims(tf.nn.softmax(end_scores), axis=1), cont), axis=1) # [bs, 1, c_len]->[bs, 1, dim]->[bs, dim] 68 | ques_mem = tf.nn.dropout(ques_mem, 1.0 - dropout) 69 | ques_mem = GRUCell(x, ques_mem, hidden_size=hidden_size, filters=filters) # [bs, c_len] 70 | 71 | return start_scores_list, end_scores_list -------------------------------------------------------------------------------- /NetModel/SAN/SAN_layers.py: -------------------------------------------------------------------------------- 1 | # stochastic answer network in tensorflow and tensor2tensor 2 | import tensorflow as tf 3 | from tensorflow.contrib.keras import layers 4 | from tensorflow.contrib.layers import variance_scaling_initializer, l2_regularizer 5 | from tensor2tensor.layers.common_layers import conv1d, dense, layer_norm 6 | 7 | initializer = lambda: variance_scaling_initializer(factor=1.0, mode='FAN_AVG', uniform=True, dtype=tf.float32) 8 | initializer_relu = lambda: variance_scaling_initializer(factor=2.0, mode='FAN_IN', uniform=False, dtype=tf.float32) 9 | regularizer = l2_regularizer(scale=3e-7) 10 | 11 | def exp_mask(inputs, mask, mask_value=-1e30): 12 | mask = tf.cast(mask, tf.float32) 13 | return inputs + mask_value * (1 - mask) 14 | 15 | def GRUCell(x, h, hidden_size, filters): 16 | with tf.variable_scope('Output_GRU_Cell', reuse=tf.AUTO_REUSE): 17 | gate_kernel = tf.get_variable("gate_kernel", 18 | shape=(hidden_size + filters, 2 * hidden_size), 19 | dtype=tf.float32, 20 | initializer=initializer(), 21 | regularizer=regularizer) 22 | gate_bias = tf.get_variable("gates_bias", 23 | shape=(2 * hidden_size), 24 | dtype=tf.float32, 25 | regularizer=regularizer) 26 | candidate_kernel = tf.get_variable("candidate_kernel", 27 | shape=(hidden_size + filters, hidden_size), 28 | dtype=tf.float32, 29 | initializer=initializer(), 30 | regularizer=regularizer) 31 | candidate_bias = tf.get_variable("candidate_bias", 32 | shape=(hidden_size), 33 | dtype=tf.float32, 34 | regularizer=regularizer) 35 | 36 | gate_inputs = tf.matmul(tf.concat([x, h], axis=-1), gate_kernel) + gate_bias # [bs, f+h]*[bs, f+h, 2h] 37 | value = tf.nn.sigmoid(gate_inputs) 38 | r, u = tf.split(value=value, num_or_size_splits=2, axis=1) # [bs, h]*2 39 | r_state = r * h # [bs, h] 40 | candidate = tf.matmul(tf.concat([x, r_state], axis=-1), candidate_kernel) + candidate_bias # [bs, f+h]*[bs,f+h, h] 41 | c = tf.nn.tanh(candidate) 42 | new_h = u * h + (1 - u) * c # [bs, h] 43 | return new_h 44 | 45 | def BilinearAttention(c, q, c_mask, filters, name, norm=False): 46 | q = tf.expand_dims(dense(q, filters, name=name, reuse=tf.AUTO_REUSE, 47 | kernel_initializer=initializer(), kernel_regularizer=regularizer),axis=-1) # [bs, dim, 1] 48 | if norm: 49 | q = layer_norm(q) 50 | cq = tf.squeeze(tf.matmul(c, q), axis=-1) # [bs, c_len, dim] * [bs, dim, 1] -> [bs, c_len] 51 | cq = exp_mask(cq, c_mask) 52 | return cq 53 | 54 | def SAN(c_mem, q_mem, c_mask, filters, hidden_size, num_turn, name, dropout, type='last'): 55 | with tf.variable_scope(name): 56 | start_scores_list = [] 57 | end_scores_list = [] 58 | for turn in range(num_turn): 59 | st_scores = BilinearAttention(c_mem, q_mem, c_mask, filters, name='start_output') # [bs, c_len] 60 | start_scores_list.append(st_scores) 61 | end_scores = BilinearAttention(c_mem, q_mem, c_mask, filters, name='end_output') # [bs, c_len] 62 | end_scores_list.append(end_scores) 63 | x = tf.squeeze(tf.matmul(tf.expand_dims(tf.nn.softmax(end_scores), axis=1), c_mem), axis=1) # [bs, 1, c_len] * [bs, c_len, f]->[bs, 1, f]->[bs, f] 64 | q_mem = tf.nn.dropout(q_mem, 1.0 - dropout) # [bs, h] 65 | q_mem = GRUCell(x, q_mem, hidden_size=hidden_size, filters=filters) # [bs, h] 66 | 67 | if type=='last': 68 | return start_scores_list[-1], end_scores_list[-1] 69 | else: 70 | return start_scores_list, end_scores_list 71 | 72 | def CharCNN(x, char_limit, char_dim, filters, maxlen, kernel_size=5, name='char_conv'): 73 | x = tf.reshape(x, [-1, char_limit, char_dim]) 74 | x = tf.nn.relu(conv1d(x, filters, kernel_size=kernel_size, name=name, padding='same', 75 | kernel_initializer=initializer_relu(), kernel_regularizer=regularizer)) 76 | x = tf.reduce_max(x, axis=1) 77 | x = tf.reshape(x, [-1, maxlen, filters]) 78 | return x 79 | 80 | def FeedForward(x, filters, dropout, name): 81 | with tf.variable_scope(name): 82 | x = tf.nn.relu(conv1d(x, filters, kernel_size=1, padding='same', name='FFN_1', 83 | kernel_initializer=initializer_relu(), kernel_regularizer=regularizer)) 84 | x = conv1d(x, filters, kernel_size=1, padding='same', name = "FFN_2", 85 | kernel_initializer=initializer(), kernel_regularizer=regularizer) 86 | x = tf.nn.dropout(x, 1 - dropout) 87 | return x 88 | 89 | # keras based 90 | def BiLSTM_keras(x=None, filters=256, dropout=0.0, name='BiLSTM', return_sequences=True): 91 | BiLSTM_Layer = layers.Bidirectional(layers.LSTM(filters, return_sequences=return_sequences), 92 | merge_mode='concat', name=name) 93 | if x is None: 94 | return BiLSTM_Layer 95 | else: 96 | x = BiLSTM_Layer(x) 97 | x = tf.nn.dropout(x, 1 - dropout) 98 | return x 99 | 100 | # danamicLstm 101 | # from tensorflow.contrib.rnn import LSTMCell 102 | # from tensorflow.contrib.rnn import MultiRNNCell 103 | # def BiLSTM(x, x_length, filters, dropout=0.0, name='BiLSTM'): 104 | # lstm_fw_cell = MultiRNNCell([LSTMCell(filters)]) 105 | # lstm_bw_cell = MultiRNNCell([LSTMCell(filters)]) 106 | # outputs, _ = tf.nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, x, 107 | # sequence_length=x_length, dtype=tf.float32, scope=name) 108 | # outputs = tf.concat(outputs, axis=-1) 109 | # outputs = tf.nn.dropout(outputs, 1 - dropout) 110 | # return outputs 111 | 112 | # cudnnLSTM 113 | from tensorflow.contrib.cudnn_rnn import CudnnLSTM 114 | def BiLSTM(x, filters, dropout=0.0, name='BiLSTM'): 115 | cudnn_lstm = CudnnLSTM(1, filters, direction='bidirectional', name=name) 116 | x, _ = cudnn_lstm(x) 117 | x = tf.nn.dropout(x, 1 - dropout) 118 | return x 119 | 120 | def Dense(x, unit, norm=True, dropout=0.0): 121 | x = dense(x, unit, kernel_initializer=initializer(), kernel_regularizer=regularizer) 122 | if norm: 123 | x = layer_norm(x) 124 | x = tf.nn.dropout(x, 1 - dropout) 125 | if unit==1: 126 | x = tf.squeeze(x, axis=-1) 127 | return x 128 | 129 | def DotProductProject(x1, x2, filters, dropout): 130 | x1 = tf.nn.dropout(x1, 1 - dropout) # [bs, c_len, dim] 131 | x2 = tf.nn.dropout(x2, 1 - dropout) # [bs, q_len, dim] 132 | x1 = conv1d(x1, filters, kernel_size=1, padding='same', name='conv', reuse=tf.AUTO_REUSE, 133 | kernel_initializer=initializer_relu(), kernel_regularizer=regularizer) # [bs, c_len, filters] 134 | x1 = tf.nn.relu(layer_norm(x1)) 135 | x2 = conv1d(x2, filters, kernel_size=1, padding='same', name='conv', reuse=tf.AUTO_REUSE, 136 | kernel_initializer=initializer_relu(), kernel_regularizer=regularizer) # [bs, q_len, filters] 137 | x2 = tf.nn.relu(layer_norm(x2)) 138 | S = tf.matmul(x1, x2, transpose_b=True) # [bs, c_len, q_len] 139 | return S 140 | 141 | def SimilarityMatrix(x1, x2, filters, type='dot_product_project', dropout=0.0): 142 | if type=='dot_product_project': 143 | S = DotProductProject(x1, x2, filters, dropout) 144 | else: 145 | raise NotImplementedError 146 | return S 147 | 148 | def AttentionLayer(c, q, v, q_mask, filters, dropout): 149 | S = SimilarityMatrix(c, q, filters, dropout=dropout) # [bs, c_len, q_len] 150 | q_mask = tf.expand_dims(q_mask, axis=1) # [bs, q_len] -> [bs, 1, q_len] 151 | S = tf.nn.softmax(exp_mask(S, q_mask)) 152 | S = tf.nn.dropout(S, 1 - dropout) 153 | x = tf.matmul(S, v) 154 | return x 155 | 156 | def DeepAttentionLayers(c, q, v, q_mask, filters, dropout, name='DeepAttentionLayers'): 157 | att_outputs = [] 158 | for i in range(len(v)): 159 | with tf.variable_scope(name+'_layer'+str(i+1)): 160 | x = AttentionLayer(c, q, v[i], q_mask, filters, dropout) 161 | att_outputs.append(x) 162 | att_outputs = tf.concat(att_outputs, axis=-1) 163 | att_outputs = tf.nn.dropout(att_outputs, 1 - dropout) 164 | return att_outputs 165 | 166 | def SumAttention(x, mask, dropout): 167 | x = tf.nn.dropout(x, 1 - dropout) 168 | alpha = tf.squeeze(conv1d(x, 1, 1, kernel_initializer=initializer(), name='sum_conv', 169 | kernel_regularizer=regularizer), axis=-1) # [bs, c_len] 170 | alpha = exp_mask(alpha, mask) # [bs, c_len] 171 | alpha = tf.expand_dims(tf.nn.softmax(alpha), axis=1) # [bs, 1, c_len] 172 | x = tf.squeeze(tf.matmul(alpha, x), axis=1) # x:[bs, c_len, dim] -> [bs, dim] 173 | return x 174 | 175 | def total_params(exclude=None): 176 | total_parameters = 0 177 | if exclude is not None: 178 | trainable_variables = list(set(tf.trainable_variables())^set(tf.trainable_variables(exclude))) 179 | else: 180 | trainable_variables = tf.trainable_variables() 181 | for variable in trainable_variables: 182 | shape = variable.get_shape() 183 | variable_parametes = 1 184 | try: 185 | for dim in shape: 186 | variable_parametes *= dim.value 187 | total_parameters += variable_parametes 188 | except: 189 | print(shape,'cudnn weights is unknown') 190 | print("Total number of trainable parameters: {}".format(total_parameters)) -------------------------------------------------------------------------------- /NetModel/SegNet.py: -------------------------------------------------------------------------------- 1 | # used for DeMesh 2 | from keras.models import * 3 | from keras.layers import * 4 | 5 | def SegNet(image_size=(220, 178, 3)): 6 | inputs = Input(image_size) 7 | mask = Input(image_size) 8 | 9 | # encoder 10 | x = Conv2D(64, 3, padding='same')(inputs) 11 | x = BatchNormalization()(x) 12 | x = Activation('relu')(x) 13 | x = MaxPooling2D()(x) 14 | 15 | x = Conv2D(128, 3, padding='same')(x) 16 | x = BatchNormalization()(x) 17 | x = Activation('relu')(x) 18 | x = MaxPooling2D()(x) 19 | 20 | x = Conv2D(256, 3, padding='same')(x) 21 | x = BatchNormalization()(x) 22 | x = Activation('relu')(x) 23 | x = MaxPooling2D()(x) 24 | 25 | x = Conv2D(512, 3, padding='same')(x) 26 | x = BatchNormalization()(x) 27 | x = Activation('relu')(x) 28 | x = MaxPooling2D()(x) 29 | 30 | # decoder 31 | x = UpSampling2D()(x) 32 | x = Conv2D(512, 3, padding='same')(x) 33 | x = BatchNormalization()(x) 34 | 35 | x = UpSampling2D()(x) 36 | x = ZeroPadding2D((1, 0))(x) 37 | x = Conv2D(256, 3, padding='same')(x) 38 | x = BatchNormalization()(x) 39 | 40 | x = UpSampling2D()(x) 41 | x = Conv2D(128, 3, padding='same')(x) 42 | x = BatchNormalization()(x) 43 | 44 | x = UpSampling2D()(x) 45 | x = ZeroPadding2D((2,1))(x) 46 | x = Conv2D(64, 3, padding='same')(x) 47 | x = BatchNormalization()(x) 48 | 49 | x1 = Conv2D(3, 1, activation='sigmoid')(x) 50 | x2 = Multiply()([mask, x1]) 51 | 52 | return Model(inputs=[inputs, mask], outputs=[x1, x2]) 53 | 54 | model=SegNet() 55 | model.summary() -------------------------------------------------------------------------------- /NetModel/SoundNet.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import * 3 | from keras.layers import * 4 | from keras.optimizers import * 5 | from keras import applications 6 | 7 | def model(timesteps=600000, output_type='pred'): 8 | inputs = Input((timesteps,)) 9 | x = Reshape((timesteps, 1))(inputs) 10 | x = Conv1D(filters=16, kernel_size=64, strides=2, name='conv1')(x) 11 | x = BatchNormalization(name='bn1')(x) 12 | x = Activation('relu')(x) 13 | x = MaxPooling1D(pool_size=8)(x) 14 | 15 | x = Conv1D(filters=32, kernel_size=32, strides=2, name='conv2')(x) 16 | x = BatchNormalization(name='bn2')(x) 17 | x = Activation('relu')(x) 18 | x = MaxPooling1D(pool_size=8)(x) 19 | 20 | x = Conv1D(filters=64, kernel_size=16, strides=2, name='conv3')(x) 21 | x = BatchNormalization(name='bn3')(x) 22 | x = Activation('relu')(x) 23 | x = Conv1D(filters=128, kernel_size=8, strides=2, name='conv4')(x) 24 | x = BatchNormalization(name='bn4')(x) 25 | x = Activation('relu')(x) 26 | x = Conv1D(filters=256, kernel_size=4, strides=2, name='conv5')(x) 27 | x = BatchNormalization(name='bn5')(x) 28 | x = Activation('relu')(x) 29 | x = MaxPooling1D(pool_size=4)(x) 30 | 31 | if output_type=='feat': 32 | output = GlobalAveragePooling1D()(x) 33 | else: 34 | x = Conv1D(filters=512, kernel_size=4, strides=2, name='conv6')(x) 35 | x = BatchNormalization(name='bn6')(x) 36 | x = Activation('relu')(x) 37 | 38 | x = Conv1D(filters=1024, kernel_size=4, strides=2, name='conv7')(x) 39 | x = BatchNormalization(name='bn7')(x) 40 | x = Activation('relu')(x) 41 | x = Dropout(0.3)(x) 42 | 43 | x1 = Conv1D(1, kernel_size=16, activation='sigmoid', name='last_conv_1')(x) 44 | x1 = Reshape((1,))(x1) 45 | x2 = Conv1D(1, kernel_size=16, activation='tanh', name='last_conv_2')(x) 46 | x2 = Reshape((1,))(x2) 47 | output = [x1,x2] 48 | 49 | return Model(inputs=inputs, outputs=output) 50 | 51 | model=model(output_type='feat') 52 | model.summary() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Machine-Learning-Toolbox 2 | 3 | ## 1.DataToolBox 4 | ### (1) Metric: 5 | acc=get_acc(real_label,predict_label) 6 | 7 | auc=get_auc(real_label, scores) 8 | 9 | [tpr,tnr,macc]=get_macc(real_label, predict_label) 10 | 11 | ### (2) Re-sampling: 12 | [data_much,data_less,much_label,less_label]=divide_data(data, label) 13 | 14 | Random Under-Sampling: 15 | [train_data_temp,train_label_temp]=RUS(data_much, data_less, much_label, less_label) 16 | 17 | Random Over-Sampling: 18 | [train_data_temp,train_label_temp]=ROS(data_much, data_less, much_label, less_label) 19 | 20 | ## 2.Ensemble 21 | ### (1) Stacking 22 | stacking(clfs,X_train,y,X_test,nfolds=5,stage=1,random_seed=2017,shuffle=True,clfs_name=None,final_clf=None) 23 | 24 | ## 3. MatMHKS 25 | A matrix based linear classifier 26 | 27 | clf=MatMHKS(penalty='l2', C=1.0, matrix_type=None,class_weight=None, max_iter=100,u0=0.5,b0=10**(-6),eta=0.99,min_step=0.0001,multi_class='ovr', verbose=0) 28 | 29 | clf.fit(X,y) 30 | 31 | clf.predict(X) 32 | 33 | clf.predict_proba(X) 34 | 35 | ## 4.generator 36 | generator for keras/tensorflow (enhanced,imbalanced data) 37 | 38 | ## 5.metrics 39 | tpr tnr for keras/tensorflow 40 | 41 | ## 6.loss 42 | 43 | ### (1) focal loss 44 | 45 | ### (2) center loss 46 | 47 | ### (3) triplet loss 48 | 49 | ### (4) island loss 50 | -------------------------------------------------------------------------------- /generator/generate_arrays_from_file.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from keras import utils 3 | import os 4 | import h5py 5 | from skimage import io 6 | def generate_arrays_from_file(path): 7 | while True: 8 | f = open(path) 9 | batch_size=64 10 | count=0 11 | x=[] 12 | y=[] 13 | for line in f: 14 | xs = line.split(',') 15 | xs=list(map(lambda x:float(x),xs)) 16 | x_temp=np.array(xs[0:-1]) 17 | y_temp=int(xs[-1]) 18 | count+=1 19 | x.append(x_temp) 20 | y.append(y_temp) 21 | if count==batch_size: 22 | x=np.concatenate(x,axis=0) 23 | x=np.reshape(x,(batch_size,60,40,1)) 24 | y=utils.to_categorical(np.array(y),5) 25 | yield x,y 26 | x = [] 27 | y = [] 28 | count=0 29 | f.close() 30 | 31 | #for autoencoder 32 | def generate_pics_from_file(path): 33 | while True: 34 | batch_size=64 35 | count=0 36 | x=[] 37 | for pic in os.listdir(path): 38 | img = io.imread(path+"/"+pic) 39 | img= cv.resize(img,(32,32)) 40 | x.append(img) 41 | count+=1 42 | if count==batch_size: 43 | x=np.array(x) 44 | x = np.reshape(x,(batch_size, 32, 32, 3)) 45 | yield x, x 46 | x = [] 47 | count = 0 48 | 49 | def generate_for_lung(file_list, label_list, batch_size): 50 | while True: 51 | count = 0 52 | x = [] 53 | y = [] 54 | for i,path in enumerate(file_list): 55 | file=h5py.File(path, 'r') 56 | x_temp = np.array(file['data']) 57 | x_temp = np.resize(x_temp,(128,128,128)) 58 | file.close() 59 | y_temp = int(label_list[i]) 60 | count += 1 61 | x.append(x_temp) 62 | y.append(y_temp) 63 | if count == batch_size: 64 | x = np.concatenate(x) 65 | x = np.reshape(x,(batch_size,128,128,128)) 66 | y = np.array(y) 67 | yield x, y 68 | x = [] 69 | y = [] 70 | count = 0 71 | 72 | #生成器generator for image with shuffle 73 | def generate_for_kp(file_list, label_list, batch_size, shuffle=True, random_seed=None): 74 | while True: 75 | #洗牌 76 | if shuffle: 77 | if random_seed!=None: 78 | random_seed+=1 79 | np.random.seed(random_seed) 80 | index=np.arange(file_list.shape[0]) 81 | np.random.shuffle(index) 82 | file_list=file_list[index] 83 | label_list=label_list[index] 84 | count = 0 85 | x, y = [], [] 86 | for i,path in enumerate(file_list): 87 | img=io.imread(path) 88 | img = np.array(img) 89 | x_temp=img/255.0 90 | y_temp=label_list[i,:] 91 | count += 1 92 | x.append(x_temp) 93 | y.append(y_temp) 94 | if count % batch_size == 0 and count != 0: 95 | x = np.array(x) 96 | x = x.reshape(batch_size, 96, 96, 3).astype("float32") 97 | y = np.array(y) 98 | yield x, y 99 | x, y = [], [] 100 | 101 | 102 | def generate_for_kp_test(file_list, batch_size): 103 | while True: 104 | count = 0 105 | x = [] 106 | for path in file_list: 107 | img = io.imread(path) 108 | img = np.array(img) 109 | x_temp = img / 255.0 110 | count += 1 111 | x.append(x_temp) 112 | if count % batch_size == 0 and count != 0: 113 | x = np.array(x) 114 | x = x.reshape(batch_size, 96, 96, 1).astype("float32") 115 | yield x 116 | x = [] 117 | 118 | # heatmaps 119 | # range 有效像素点范围 120 | def get_heatmaps(labels, size=(56, 56), range=1): 121 | heatmaps=[] 122 | life_x = labels[0:5] 123 | life_y = labels[5:10] 124 | int_x = labels[10:15] 125 | int_y = labels[15:20] 126 | aff_x = labels[20:25] 127 | aff_y = labels[25:30] 128 | finger_x = labels[30:35] 129 | finger_y = labels[35:40] 130 | x_all=np.concatenate((life_x,int_x,aff_x,finger_x)) 131 | y_all=np.concatenate((life_y,int_y,aff_y,finger_y)) 132 | for i in range(len(x_all)): 133 | heatmap=np.zeros(size) 134 | x=x_all[i]*size[0] 135 | y=y_all[i]*size[1] 136 | heatmap[max(0,x-range):min(size[0],x+range),max(0,y-range):min(size[1],y+range)]=1 137 | heatmaps.append(heatmap) 138 | 139 | heatmaps=np.reshape(heatmaps,(size[0],size[1],len(x_all))) 140 | 141 | return heatmaps 142 | 143 | # generator to heatmaps 144 | def generator2heatmaps(file_list, label_list, batch_size, shuffle=True, random_seed=None): 145 | while True: 146 | if shuffle: 147 | if random_seed != None: 148 | random_seed += 1 149 | np.random.seed(random_seed) 150 | index = np.arange(file_list.shape[0]) 151 | np.random.shuffle(index) 152 | file_list = file_list[index] 153 | label_list = label_list[index] 154 | count = 0 155 | x, y = [], [] 156 | for i, path in enumerate(file_list): 157 | img = io.imread(path) 158 | img = np.array(img) 159 | x_temp = img 160 | y_temp = get_heatmaps(label_list[i, :]) 161 | count += 1 162 | x.append(x_temp) 163 | y.append(y_temp) 164 | if count % batch_size == 0 and count != 0: 165 | x = np.array(x) 166 | x = x.reshape(batch_size, 128, 128, 1).astype("float32") 167 | y = np.array(y) 168 | yield x, [y, y, y, y, y, y] 169 | x, y = [], [] -------------------------------------------------------------------------------- /generator/generator_for_imbalance.py: -------------------------------------------------------------------------------- 1 | from skimage import io 2 | import numpy as np 3 | import keras 4 | 5 | # 生成器generator for imbalanced data,每个batch中样本等量 6 | def generator_train(file_list, label_list, batch_size, shuffle=True, random_seed=None, imbalance=True): 7 | while True: 8 | if shuffle: 9 | if random_seed!=None: 10 | random_seed+=1 11 | np.random.seed(random_seed) 12 | index=np.arange(file_list.shape[0]) 13 | np.random.shuffle(index) 14 | file_list=file_list[index] 15 | label_list=label_list[index] 16 | count = 0 17 | label_true=keras.utils.to_categorical(np.array(label_list)) 18 | x, y = [], [] 19 | if imbalance: # batch内样本不同类别平衡 20 | labels=np.unique(label_list) 21 | index_list=[] 22 | max_num=0 23 | for l in labels: 24 | index_list.append(np.where(label_list==l)[0]) 25 | if len(np.where(label_list==l)[0])>max_num: 26 | max_num=len(np.where(label_list==l)[0]) 27 | for i in range(max_num): # i保持循环到最多的类结束为止 28 | for j in range(len(index_list)): # 不同类别各取一个 29 | if i%len(index_list[j])==0 and i!=0: # 当这个类别已经循环了一遍了 30 | np.random.shuffle(index_list[j]) # 需要洗牌这个类别 31 | x_temp=io.imread(file_list[index_list[j][i%len(index_list[j])]]) 32 | y_temp=label_true[index_list[j][i%len(index_list[j])],:] 33 | count+=1 34 | x.append(x_temp) 35 | y.append(y_temp) 36 | if count % batch_size == 0 and count != 0: 37 | x = np.array(x) 38 | x = x.reshape(batch_size, 128, 128, 3) 39 | y = np.array(y) 40 | yield x, y 41 | x, y = [], [] 42 | else: # for validation 43 | for i,path in enumerate(file_list): 44 | x_temp = io.imread(path) 45 | y_temp=label_true[i,:] 46 | count += 1 47 | x.append(x_temp) 48 | y.append(y_temp) 49 | if count % batch_size == 0 and count != 0: 50 | x = np.array(x) 51 | x = x.reshape(batch_size, 128, 128, 3) 52 | y = np.array(y) 53 | yield x, y 54 | x, y = [], [] -------------------------------------------------------------------------------- /generator/image_reformance_generator.py: -------------------------------------------------------------------------------- 1 | from skimage import io,transform 2 | import numpy as np 3 | from keras.preprocessing import image 4 | 5 | #平移图片x,同时平移关键点y 6 | def shift(x, y=None, w_limit=0., h_limit=0., row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.): 7 | wshift = np.random.uniform(-1*w_limit, w_limit) 8 | hshift = np.random.uniform(-1*h_limit, h_limit) 9 | h, w = x.shape[row_axis], x.shape[col_axis] #读取图片的高和宽 10 | tx = hshift * h #高偏移大小,若不偏移可设为0,若向上偏移设为正数 11 | ty = wshift * w #宽偏移大小,若不偏移可设为0,若向左偏移设为正数 12 | translation_matrix = np.array([[1, 0, tx], 13 | [0, 1, ty], 14 | [0, 0, 1]]) 15 | transform_matrix = translation_matrix 16 | x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval) 17 | 18 | if y is not None: 19 | transform_matrix[0:-1, -1] *= -1 20 | all_x = np.concatenate((y[0:5], y[10:15], y[20:25], y[30:35])) 21 | all_y = np.concatenate((y[5:10], y[15:20], y[25:30], y[35:40])) 22 | all_x = all_x.reshape((1, -1)) 23 | all_y = all_y.reshape((1, -1)) 24 | all_xy1 = np.concatenate((all_y, all_x, np.ones((1, all_x.shape[1]))), axis=0) 25 | y_1 = np.dot(transform_matrix, all_xy1)[0:-1, :] 26 | y[0:5] = y_1[1, 0:5] 27 | y[5:10] = y_1[0, 0:5] 28 | y[10:15] = y_1[1, 5:10] 29 | y[15:20] = y_1[0, 5:10] 30 | y[20:25] = y_1[1, 10:15] 31 | y[25:30] = y_1[0, 10:15] 32 | y[30:35] = y_1[1, 15:20] 33 | y[35:40] = y_1[0, 15:20] 34 | 35 | return x, y 36 | 37 | #旋转图片x,同时旋转关键点y 38 | def rotate(x, y=None, rotate_limit=0, row_axis=0, col_axis=1, channel_axis=2, fill_mode='nearest', cval=0.): 39 | theta = np.pi / 180 * np.random.uniform(-1 * rotate_limit, rotate_limit) # 逆时针旋转角度 40 | rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0], 41 | [np.sin(theta), np.cos(theta), 0], 42 | [0, 0, 1]]) 43 | h, w = x.shape[row_axis], x.shape[col_axis] 44 | transform_matrix = image.transform_matrix_offset_center(rotation_matrix, h, w) 45 | x = image.apply_transform(x, transform_matrix, channel_axis, fill_mode, cval) 46 | 47 | if y is not None: 48 | all_x = np.concatenate((y[0:5], y[10:15], y[20:25], y[30:35])) 49 | all_y = np.concatenate((y[5:10], y[15:20], y[25:30], y[35:40])) 50 | all_x = all_x.reshape((1, -1)) 51 | all_y = all_y.reshape((1, -1)) 52 | all_xy1 = np.concatenate((all_x, all_y, np.ones((1, all_x.shape[1]))), axis=0) 53 | y_1 = np.dot(transform_matrix, all_xy1)[0:-1, :] 54 | y[0:5] = y_1[0, 0:5] 55 | y[5:10] = y_1[1, 0:5] 56 | y[10:15] = y_1[0, 5:10] 57 | y[15:20] = y_1[1, 5:10] 58 | y[20:25] = y_1[0, 10:15] 59 | y[25:30] = y_1[1, 10:15] 60 | y[30:35] = y_1[0, 15:20] 61 | y[35:40] = y_1[1, 15:20] 62 | 63 | return x, y 64 | 65 | 66 | # 90°,180°,270°旋转图片x,同时旋转关键点y(!!!!!!y尺寸为1:1且范围为0:1!!!!!) 67 | def rotate90n(x, y=None): 68 | theta = np.random.choice(np.arange(0, 4), 1) * 90 69 | x = transform.rotate(x, theta) 70 | if theta == 180: 71 | y = 1 - y 72 | elif theta == 90: 73 | y_t = y[0:5].copy() 74 | y[0:5] = y[5:10] 75 | y[5:10] = 1 - y_t 76 | 77 | y_t = y[10:15].copy() 78 | y[10:15] = y[15:20] 79 | y[15:20] = 1 - y_t 80 | 81 | y_t = y[20:25].copy() 82 | y[20:25] = y[25:30] 83 | y[25:30] = 1 - y_t 84 | 85 | y_t = y[30:35].copy() 86 | y[30:35] = y[35:40] 87 | y[35:40] = 1 - y_t 88 | elif theta == 270: 89 | y_t = y[0:5].copy() 90 | y[0:5] = 1 - y[5:10] 91 | y[5:10] = y_t 92 | 93 | y_t = y[10:15].copy() 94 | y[10:15] = 1 - y[15:20] 95 | y[15:20] = y_t 96 | 97 | y_t = y[20:25].copy() 98 | y[20:25] = 1 - y[25:30] 99 | y[25:30] = y_t 100 | 101 | y_t = y[30:35].copy() 102 | y[30:35] = 1 - y[35:40] 103 | y[35:40] = y_t 104 | 105 | return x, y 106 | 107 | #带数据增强(平移,旋转)的生成器generator 108 | def generate_reformance_kp(file_list, label_list, batch_size, shuffle=True, random_seed=None): 109 | while True: 110 | if shuffle: 111 | if random_seed!=None: 112 | random_seed+=1 113 | np.random.seed(random_seed) 114 | index=np.arange(file_list.shape[0]) 115 | np.random.shuffle(index) 116 | file_list=file_list[index] 117 | label_list=label_list[index] 118 | count = 0 119 | x, y = [], [] 120 | for i,path in enumerate(file_list): 121 | img=io.imread(path) 122 | y_temp=label_list[i,:]*224. 123 | x_temp = np.reshape(img, (224, 224, 1)) 124 | x_temp, y_temp = rotate(x_temp, y_temp, 45)#旋转 125 | x_temp, y_temp = shift(x_temp, y_temp, 0.15, 0.15)#平移 126 | y_temp/=224. 127 | x_temp = x_temp.reshape((224,224)) 128 | count += 1 129 | x.append(x_temp) 130 | y.append(y_temp) 131 | if count % batch_size == 0 and count != 0: 132 | x = np.array(x) 133 | x = x.reshape(batch_size, 224, 224, 1).astype("float32") 134 | y = np.array(y) 135 | yield x, y 136 | x, y = [], [] -------------------------------------------------------------------------------- /keras_api/ExponentialMovingAverage.py: -------------------------------------------------------------------------------- 1 | # changed from https://github.com/alno/kaggle-allstate-claims-severity/blob/master/keras_util.py by ewrfcas 2 | 3 | from keras import backend as K 4 | from keras.models import load_model 5 | from tqdm import tqdm 6 | 7 | def ExponentialMovingAverage_TrainBegin(model): 8 | # run when training begins 9 | # ema_trainable_weights_vals save the latest weights of model with ema 10 | ema_trainable_weights_vals ={} 11 | for weight in tqdm(model.trainable_weights): 12 | ema_trainable_weights_vals[weight.name]=K.get_value(weight) 13 | return ema_trainable_weights_vals 14 | 15 | def ExponentialMovingAverage_BatchEnd(model, ema_trainable_weights_vals, decay=0.999): 16 | # run when each batch ends 17 | for weight in model.trainable_weights: 18 | old_val = ema_trainable_weights_vals[weight.name] 19 | ema_trainable_weights_vals[weight.name] = decay * old_val + (1.0 - decay) * K.get_value(weight) 20 | return ema_trainable_weights_vals 21 | 22 | def ExponentialMovingAverage_EpochEnd(model, ema_trainable_weights_vals): 23 | # run when each epoch ends, generate model with ema to evaluating 24 | for weight in tqdm(model.trainable_weights): 25 | K.set_value(weight, ema_trainable_weights_vals[weight.name]) -------------------------------------------------------------------------------- /keras_api/keras2tf.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | from tensorflow.python.framework import graph_util 4 | from keras.models import load_model 5 | 6 | with tf.Session() as sess: 7 | model=load_model('models/Hourglass_modelsV3_epoch7.h5') 8 | with tf.gfile.FastGFile('models/Hourglass_tf.pb','wb') as f: 9 | graph_def = sess.graph.as_graph_def() 10 | output_nodes = [] 11 | input_nodes = [] 12 | for ot in model.outputs: 13 | output_nodes.append(ot.op.name) 14 | for it in model.inputs: 15 | input_nodes.append(it.op.name) 16 | print('inputs:',input_nodes) 17 | print('outputs:',output_nodes) 18 | output_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, output_nodes) 19 | f.write(output_graph_def.SerializeToString()) 20 | 21 | with tf.gfile.FastGFile('models/Hourglass_tf.pb','rb') as f: 22 | intput_graph_def=tf.GraphDef() 23 | intput_graph_def.ParseFromString(f.read()) 24 | with tf.Graph().as_default() as p_graph: 25 | tf.import_graph_def(intput_graph_def, name="") 26 | 27 | # for i in p_graph.get_operations(): 28 | # if 'input' in i.name: 29 | # print(i.name) 30 | 31 | inputs=p_graph.get_tensor_by_name('input_1:0') 32 | outputs=p_graph.get_tensor_by_name('nstack_2/Tanh:0') 33 | 34 | with tf.Session(graph=p_graph) as sess: 35 | img = (np.random.random((1, 224, 176, 3)) - 0.5) * 2 36 | y_pred=sess.run(outputs,feed_dict={inputs:img}) 37 | y_pred=np.squeeze(y_pred,0) 38 | print(y_pred.shape) -------------------------------------------------------------------------------- /latex/bayes_analysis/main.py: -------------------------------------------------------------------------------- 1 | import bayesiantests as bt 2 | import pandas as pd 3 | import numpy as np 4 | import seaborn as sb 5 | import matplotlib.pyplot as plt 6 | from tqdm import tqdm 7 | #使用csv文件路径作为输入,要求csv文件格式为第一行算法名称,二至末行为多个数据集某一评价指标的结果,输出的是各个算法性能从优到劣的排序 8 | #使用论文Time for a Change: a Tutorial for Comparing Multiple Classi ers Through Bayesian Analysis中的贝叶斯signrank性能评价方法及原始函数,github地址 9 | # https://github.com/BayesianTestsML/tutorial/ 10 | 11 | def wrap_function(path,rope=0.01,verbose=False,names=('left_classifier', 'right_classifier')): 12 | x=pd.read_csv(path) 13 | x.dropna(axis=0, how='all', inplace=True) 14 | x.dropna(axis=1, how='all', inplace=True) 15 | try: 16 | names=x.columns.values 17 | n_classifier=len(names) 18 | except: 19 | print('Wrong input type,the algorithm takes DataFrame as standard input') 20 | if n_classifier==2: 21 | left_bigger,equal,right_bigger=bt.signrank(np.array(x),rope=rope,verbose=verbose,names=names) 22 | else: 23 | result_line=np.zeros((n_classifier,)) 24 | result_matrix=np.zeros((n_classifier,n_classifier)) 25 | for i in tqdm(range(0,n_classifier)): 26 | for j in range(0,n_classifier): 27 | if i==j: 28 | continue 29 | else: 30 | x_t=x.iloc[:,[i,j]] 31 | names=x_t.columns.values 32 | left_bigger, equal, right_bigger = bt.signrank(np.array(x_t), rope=rope, verbose=verbose, names=names) 33 | if max([left_bigger,equal,right_bigger])==left_bigger: 34 | result_line[i]+=1 35 | result_matrix[i][j]=left_bigger 36 | result=pd.DataFrame(result_line,index=x.columns.values) 37 | result = result.sort_values(by=[0], ascending=False) 38 | result = result.index.values 39 | result_matrix=pd.DataFrame(result_matrix,index=x.columns.values,columns=x.columns.values) 40 | for al in result: 41 | print(al + ' ', end=' ') 42 | return result_matrix 43 | 44 | path='./data/total_report_macc.csv' 45 | matrix=wrap_function(path,verbose=True,rope=0.01) 46 | matrix.to_csv('./result_matrix.csv') 47 | 48 | sb.heatmap(matrix,annot=True,linewidths=0.5, cmap='YlGnBu') 49 | plt.xticks(rotation=90) 50 | plt.yticks(rotation=360) 51 | plt.savefig("bayes_heatmap_macc.png") 52 | plt.show() -------------------------------------------------------------------------------- /latex/get_best_results.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "100%|██████████| 64/64 [00:01<00:00, 41.53it/s]\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import pandas as pd\n", 18 | "from glob import glob\n", 19 | "import numpy as np\n", 20 | "from tqdm import tqdm\n", 21 | "\n", 22 | "datasets=['ecoli_0_vs_1','abalone19','glass-0-4_vs_5','haberman','segment0','pima','glass-0-1-4-6_vs_2','yeast5']\n", 23 | "files = ['results/result_cart/'+d+'.txt' for d in datasets]\n", 24 | "\n", 25 | "df_all=[]\n", 26 | "names=[]\n", 27 | "for f in tqdm(files):\n", 28 | " name=f.split('/')[-1].split('.')[0]\n", 29 | " df = pd.read_table(f, encoding='gbk', header=None)\n", 30 | " best_macc = float(df.iloc[-2, :].values[0].split(',')[0].split(':')[-1])\n", 31 | " best_std = 0\n", 32 | " for i in range(df.shape[0]):\n", 33 | " s = df.iloc[i,:].values[0]\n", 34 | " if 'Setting' in s:\n", 35 | " maccs=[]\n", 36 | " elif 'The average mean-accuracy is' in s:\n", 37 | " if float(s.split('±')[0].split(': ')[-1])==best_macc:\n", 38 | " best_std = np.std(maccs)\n", 39 | " elif 'tp' in s and 'tn' in s and 'm-acc' in s and 'auc' in s:\n", 40 | " maccs.append(float(s.split(' auc:')[0].split('m-acc:')[-1]))\n", 41 | " df_all.append([best_macc, best_std])\n", 42 | " names.append(name)\n", 43 | " \n", 44 | "df_all = pd.DataFrame(df_all, index=names, columns=['macc','std'])\n", 45 | "df_all.to_csv('report/ECUBoost_CART_report.csv')\n", 46 | " \n", 47 | " \n", 48 | "# df=pd.read_table(files[0], encoding='gbk',header=None)\n", 49 | "# print(float(df.iloc[-2,:].values[0].split(',')[0].split(':')[-1]))\n", 50 | "# df.head()" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": null, 56 | "metadata": { 57 | "collapsed": true 58 | }, 59 | "outputs": [], 60 | "source": [ 61 | "# used for SVM/BEBS\n", 62 | "import pandas as pd\n", 63 | "from glob import glob\n", 64 | "import numpy as np\n", 65 | "from tqdm import tqdm\n", 66 | "\n", 67 | "datasets=['ecoli_0_vs_1', 'glass1', 'wisconsin', 'pima', 'glass0', 'yeast1', 'haberman', 'vehicle2', 'vehicle1', 'vehicle3', 'vehicle0', 'ecoli1',\n", 68 | " 'new_thyroid1', 'new_thyroid2', 'ecoli2', 'segment0', 'glass6', 'yeast3', 'ecoli3', 'page_blocks0', 'ecoli-0-3-4_vs_5', 'ecoli-0-2-3-4_vs_5',\n", 69 | " 'yeast-0-3-5-9_vs_7-8', 'ecoli-0-4-6_vs_5', 'yeast-0-2-5-6_vs_3-7-8-9', 'yeast-0-2-5-7-9_vs_3-6-8', 'ecoli-0-3-4-6_vs_5', 'ecoli-0-3-4-7_vs_5-6',\n", 70 | " 'ecoli-0-1_vs_2-3-5', 'yeast_2_vs_4', 'ecoli-0-6-7_vs_3-5', 'glass-0-4_vs_5', 'ecoli-0-2-6-7_vs_3-5', 'glass-0-1-5_vs_2', 'yeast_0_5_6_7_9_vs_4',\n", 71 | " 'vowel0', 'ecoli-0-6-7_vs_5', 'ecoli-0-1-4-7_vs_2-3-5-6', 'glass_0_1_6_vs_2', 'ecoli-0-1_vs_5', 'led7digit-0-2-4-5-6-7-8-9_vs_1', 'glass-0-6_vs_5',\n", 72 | " 'glass-0-1-4-6_vs_2', 'glass2', 'ecoli-0-1-4-7_vs_5-6', 'cleveland-0_vs_4', 'ecoli-0-1-4-6_vs_5', 'shuttle_c0_vs_c4', 'yeast_1_vs_7', 'ecoli4',\n", 73 | " 'glass4', 'page_blocks_1_3_vs_4', 'abalone9_18', 'glass_0_1_6_vs_5', 'yeast_1_4_5_8_vs_7', 'yeast_2_vs_8', 'glass5', 'shuttle_c2_vs_c4', 'yeast4',\n", 74 | " 'yeast_1_2_8_9_vs_7', 'yeast5', 'yeast6', 'ecoli_0_1_3_7_vs_2_6', 'abalone19']\n", 75 | "files = ['results/BEBS_result/'+d+'.txt' for d in datasets]\n", 76 | "\n", 77 | "df_all=[]\n", 78 | "names=[]\n", 79 | "for f in tqdm(files):\n", 80 | " name=f.split('/')[-1].split('.')[0]\n", 81 | " df = pd.read_table(f, encoding='gbk', header=None)\n", 82 | " best_macc = float(df.iloc[-1, :].values[0].split(',')[0].split(':')[-1])\n", 83 | " best_std = 0\n", 84 | " for i in range(df.shape[0]):\n", 85 | " s = df.iloc[i,:].values[0]\n", 86 | " if 'SVM:' in s:\n", 87 | " maccs=[]\n", 88 | " elif 'The average mean-accuracy is' in s:\n", 89 | " if float(s.split('±')[0].split(': ')[-1])==best_macc:\n", 90 | " best_std = np.std(maccs)\n", 91 | " elif 'tp' in s and 'tn' in s and 'm-acc' in s:\n", 92 | " maccs.append(float(s.split('m-acc:')[-1]))\n", 93 | " df_all.append([best_macc, best_std])\n", 94 | " names.append(name)\n", 95 | " \n", 96 | "df_all = pd.DataFrame(df_all, index=names, columns=['macc','std'])\n", 97 | "df_all.to_csv('report/BEBS_CART_report.csv')\n", 98 | " \n", 99 | " \n", 100 | "# df=pd.read_table(files[0], encoding='gbk',header=None)\n", 101 | "# print(float(df.iloc[-2,:].values[0].split(',')[0].split(':')[-1]))\n", 102 | "# df.head()" 103 | ] 104 | } 105 | ], 106 | "metadata": { 107 | "kernelspec": { 108 | "display_name": "Python 3", 109 | "language": "python", 110 | "name": "python3" 111 | }, 112 | "language_info": { 113 | "codemirror_mode": { 114 | "name": "ipython", 115 | "version": 3 116 | }, 117 | "file_extension": ".py", 118 | "mimetype": "text/x-python", 119 | "name": "python", 120 | "nbconvert_exporter": "python", 121 | "pygments_lexer": "ipython3", 122 | "version": "3.6.3" 123 | } 124 | }, 125 | "nbformat": 4, 126 | "nbformat_minor": 1 127 | } 128 | -------------------------------------------------------------------------------- /loss_function/ArcFace_loss.py: -------------------------------------------------------------------------------- 1 | from keras import initializers 2 | import keras.backend as K 3 | from keras.engine.topology import Layer 4 | import math 5 | import tensorflow as tf 6 | 7 | class ArcFaceLoss(Layer): 8 | def __init__(self, class_num, s=64, m=0.5, **kwargs): 9 | self.init = initializers.get('glorot_uniform') 10 | self.class_num = class_num 11 | self.s = s 12 | self.m = m 13 | super(ArcFaceLoss, self).__init__(**kwargs) 14 | 15 | def build(self, input_shape): 16 | assert len(input_shape[0]) == 2 and len(input_shape[1]) == 2 17 | self.W = self.add_weight((input_shape[0][-1], self.class_num), initializer=self.init, 18 | name='{}_W'.format(self.name)) 19 | super(ArcFaceLoss, self).build(input_shape) 20 | 21 | def call(self, inputs, mask=None): 22 | cos_m = math.cos(self.m) 23 | sin_m = math.sin(self.m) 24 | mm = sin_m * self.m 25 | threshold = math.cos(math.pi - self.m) 26 | # inputs: 27 | # x: features, y_mask: 1-D or one-hot label works as mask 28 | x = inputs[0] 29 | y_mask = inputs[1] 30 | if y_mask.shape[-1]==1: 31 | y_mask = K.cast(y_mask, tf.int32) 32 | y_mask = K.reshape(K.one_hot(y_mask, self.class_num),(-1, self.class_num)) 33 | 34 | # feature norm 35 | x = K.l2_normalize(x, axis=1) 36 | # weights norm 37 | self.W = K.l2_normalize(self.W, axis=0) 38 | 39 | # cos(theta+m) 40 | cos_theta = K.dot(x, self.W) 41 | cos_theta2 = K.square(cos_theta) 42 | sin_theta2 = 1. - cos_theta2 43 | sin_theta = K.sqrt(sin_theta2 + K.epsilon()) 44 | cos_tm = self.s * ((cos_theta * cos_m) - (sin_theta * sin_m)) 45 | 46 | # this condition controls the theta+m should in range [0, pi] 47 | # 0<=theta+m<=pi 48 | # -m<=theta<=pi-m 49 | cond_v = cos_theta - threshold 50 | cond = K.cast(K.relu(cond_v), dtype=tf.bool) 51 | keep_val = self.s * (cos_theta - mm) 52 | cos_tm_temp = tf.where(cond, cos_tm, keep_val) 53 | 54 | # mask by label 55 | y_mask =+ K.epsilon() 56 | inv_mask = 1. - y_mask 57 | s_cos_theta = self.s * cos_theta 58 | output = K.softmax((s_cos_theta * inv_mask) + (cos_tm_temp * y_mask)) 59 | 60 | return output 61 | 62 | def compute_output_shape(self, input_shape): 63 | return input_shape[0], self.class_num 64 | 65 | # def model(): 66 | # inputs=Input((112,112,3)) 67 | # label=Input((3,)) 68 | # x = Conv2D(16,3,activation='relu')(inputs) 69 | # x = MaxPooling2D()(x) 70 | # x = Conv2D(32,3,activation='relu')(x) 71 | # x = MaxPooling2D()(x) 72 | # x = GlobalAveragePooling2D()(x) 73 | # x = ArcFaceLoss(class_num=3)([x,label]) 74 | # x = Dense(3, activation='softmax')(x) 75 | # 76 | # return Model(inputs=[inputs,label], outputs=x) 77 | # 78 | # model=model() 79 | # model.compile(loss='categorical_crossentropy',optimizer='adam') 80 | # model.summary() 81 | # X=np.random.random((1000,112,112,3)) 82 | # y=np.random.randint(0,3,(1000)) 83 | # y=utils.to_categorical(y,3) 84 | # model.fit([X,y],y) -------------------------------------------------------------------------------- /loss_function/center_loss.py: -------------------------------------------------------------------------------- 1 | # tensorflow version 2 | import tensorflow as tf 3 | 4 | # example: 5 | # https://github.com/godfanmiao/MNIST_CNN_CENTERLOSS_TENSORFLOW/blob/master/MNIST_CNN_BN_CENTERLOSS.ipynb 6 | 7 | def center_loss(features, label, alpha, nrof_classes): 8 | """Center loss based on the paper "A Discriminative Feature Learning Approach for Deep Face Recognition" 9 | (http://ydwen.github.io/papers/WenECCV16.pdf) 10 | """ 11 | # 获取特征向量长度 12 | nrof_features = features.get_shape()[1] 13 | 14 | # 生成可以共享的变量centers 15 | with tf.variable_scope('center', reuse=True): 16 | centers = tf.get_variable('centers') 17 | label = tf.reshape(label, [-1]) 18 | 19 | # 取出对应label下对应的center值,注意label里面的值可能会重复,因为一个标签下有可能会出现多个人 20 | centers_batch = tf.gather(centers, label) 21 | 22 | # 求特征点到中心的距离并乘以一定的系数,alfa是center的更新速度,越大代表更新的越慢 23 | diff = centers_batch - features 24 | 25 | # 获取一个batch中同一样本出现的次数,这里需要理解论文中的更新公式 26 | unique_label, unique_idx, unique_count = tf.unique_with_counts(label) 27 | appear_times = tf.gather(unique_count, unique_idx) 28 | appear_times = tf.reshape(appear_times, [-1, 1]) 29 | 30 | diff = diff / tf.cast((1 + appear_times), tf.float32) 31 | diff = alpha * diff 32 | 33 | # 更新center,输出是将对应于label的centers减去对应的diff,如果同一个标签出现多次,那么就减去多次 34 | centers = tf.scatter_sub(centers, label, diff) 35 | 36 | # 求center loss,这里是将l2_loss里面的值进行平方相加,再除以2,并没有进行开方 37 | loss = tf.nn.l2_loss(features - centers_batch) 38 | return loss, centers 39 | 40 | # keras version by@苏god 41 | from keras.layers import * 42 | from keras.models import * 43 | import numpy as np 44 | 45 | nb_classes = 100 # 类别数,同时也是聚类中心数 46 | feature_size = 32 # 聚类的deep feature维度 47 | 48 | input_image = Input(shape=(224,224,3)) 49 | cnn = Conv2D(10, (2,2))(input_image) 50 | cnn = MaxPooling2D((2,2))(cnn) 51 | cnn = Flatten()(cnn) 52 | feature = Dense(feature_size, activation='relu')(cnn) 53 | predict = Dense(nb_classes, activation='softmax', name='softmax')(feature) #至此,得到一个常规的softmax分类模型 54 | 55 | input_target = Input(shape=(1,)) 56 | centers = Embedding(nb_classes, feature_size)(input_target) # Embedding层用来存放中心(Embedding中的weights矩阵为nb_classes*feature_size,每行是一个中心) 57 | l2_loss = Lambda(lambda x: K.sum(K.square(x[0]-x[1][:,0]), 1, keepdims=True), name='l2_loss')([feature,centers]) 58 | 59 | model_train = Model(inputs=[input_image,input_target], outputs=[predict,l2_loss]) 60 | model_train.compile(optimizer='adam', loss=['sparse_categorical_crossentropy',lambda y_true,y_pred: y_pred], loss_weights=[1.,0.2], metrics={'softmax':'accuracy'}) 61 | 62 | model_predict = Model(inputs=input_image, outputs=predict) 63 | model_predict.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 64 | 65 | train_images=np.random.random((500,224,224,3)) 66 | train_targets=np.random.randint(0,100,500) # 由于Embedding的存在,无需one-hot 67 | random_y=np.random.random(500) # 由于keras必须要有y_true,这里其实是无用的 68 | model_train.fit([train_images,train_targets], [train_targets,random_y], epochs=10) -------------------------------------------------------------------------------- /loss_function/contrastive_loss.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | # contrastive loss 4 | # loss=(1/2N)*\sum(y*d^2+(1-y)max(alpha-d,0)^2) 5 | def contrastive_loss(y_true, y_pred, alpha = 0.2): 6 | left = y_pred[0] 7 | right = y_pred[1] 8 | d = tf.sqrt(tf.reduce_sum(tf.square(left - right), 1)) 9 | 10 | # if label(left)==label(right) y_same=1 11 | # else y_same=0 12 | y_same = tf.cast(tf.equal(y_true[0],y_true[1]),tf.float32) 13 | loss = y_same * tf.square(d) + (1-y_same) * tf.square(tf.maximum(alpha - d, 0.)) 14 | loss = 0.5 * tf.reduce_mean(loss) 15 | 16 | return loss -------------------------------------------------------------------------------- /loss_function/focal_loss.py: -------------------------------------------------------------------------------- 1 | # keras version 2 | from keras.layers import * 3 | # focal loss 4 | # (1-y)^k*log(y) 5 | # 交叉熵前乘以1-yred指数,使得低置信度的loss更高,高置信度的loss更低 6 | def focal_loss(y_true, y_pred, gamma=2, n_class=5): 7 | return K.mean(K.pow(K.ones(shape=(n_class,))-y_pred,gamma))*K.categorical_crossentropy(y_pred,y_true) 8 | 9 | # tensorflow version 10 | import tensorflow as tf 11 | def focal_loss_tf(y_true, y_pred, gamma=2, n_class=5): 12 | return tf.reduce_mean(tf.pow(tf.ones(shape=(n_class,))-y_pred,gamma))*tf.nn.softmax_cross_entropy_with_logits(y_true,y_pred) 13 | -------------------------------------------------------------------------------- /loss_function/huber_loss.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def huber_loss(y_true, y_pred, delta=1.0): 4 | return tf.losses.huber_loss(y_true,y_pred,delta=delta) -------------------------------------------------------------------------------- /loss_function/island_loss.py: -------------------------------------------------------------------------------- 1 | # tensorflow version 2 | import tensorflow as tf 3 | # example: 4 | # https://github.com/godfanmiao/MNIST_CNN_CENTERLOSS_TENSORFLOW/blob/master/MNIST_CNN_BN_CENTERLOSS.ipynb 5 | 6 | def island_loss(features, label, alpha, nrof_classes, nrof_features, lamda1=10): 7 | """Center loss based on the paper "Island Loss for Learning Discriminative Features in Facial Expression Recognition" 8 | (https://github.com/SeriaZheng/EmoNet/blob/master/loss_function/loss_paper/Island_loss.pdf) 9 | """ 10 | # 生成可以共享的变量centers 11 | with tf.variable_scope('center', reuse=True): 12 | centers = tf.get_variable('centers') 13 | label = tf.reshape(label, [-1]) 14 | 15 | # 取出对应label下对应的center值,注意label里面的值可能会重复,因为一个标签下有可能会出现多个人 16 | centers_batch = tf.gather(centers, label) 17 | 18 | # 求特征点到中心的距离并乘以一定的系数,diff1为center loss 19 | diff1 = centers_batch - features 20 | 21 | # 获取一个batch中同一样本出现的次数,这里需要理解论文中的更新公式 22 | unique_label, unique_idx, unique_count = tf.unique_with_counts(label) 23 | appear_times = tf.gather(unique_count, unique_idx) 24 | appear_times = tf.reshape(appear_times, [-1, 1]) 25 | 26 | diff1 = diff1 / tf.cast((1 + appear_times), tf.float32) 27 | diff1 = alpha * diff1 28 | 29 | # diff2为island loss的center更新项 30 | diff2 = tf.get_variable('diff2', [nrof_classes, nrof_features], dtype=tf.float32, 31 | initializer=tf.constant_initializer(0), trainable=False) 32 | for i in range(nrof_classes): 33 | for j in range(nrof_classes): 34 | if i!=j: 35 | diff2 = tf.scatter_add(diff2, i, 36 | (tf.gather(centers, i) / tf.sqrt( 37 | tf.reduce_sum(tf.square(tf.gather(centers, i)))) * tf.sqrt( 38 | tf.reduce_sum(tf.square(tf.gather(centers, j))))) 39 | - tf.multiply( 40 | (tf.reduce_sum( 41 | tf.multiply(tf.gather(centers, i), tf.gather(centers, j))) / tf.sqrt( 42 | tf.reduce_sum(tf.square(tf.gather(centers, i)))) * 43 | tf.pow(tf.sqrt(tf.reduce_sum(tf.square(tf.gather(centers, j)))), 3)), 44 | tf.gather(centers, j))) 45 | diff2 = diff2 * lamda1 / (nrof_classes - 1) 46 | diff2 = alpha * diff2 47 | 48 | # 求center loss,这里是将l2_loss里面的值进行平方相加,再除以2,并没有进行开方 49 | loss1 = tf.nn.l2_loss(features - centers_batch) 50 | 51 | # 求island loss 52 | loss2 = tf.zeros(1) 53 | for i in range(nrof_classes): 54 | for j in range(nrof_classes): 55 | if i!=j: 56 | loss2 = tf.add(tf.add(tf.reduce_sum(tf.multiply(tf.gather(centers, i), tf.gather(centers, j))) / ( 57 | tf.sqrt(tf.reduce_sum(tf.square(tf.gather(centers, i)))) * 58 | tf.sqrt(tf.reduce_sum(tf.square(tf.gather(centers, j))))), tf.ones(1)), loss2) 59 | loss2 = lamda1 * loss2 60 | 61 | loss = tf.add(loss1,loss2) 62 | 63 | # 更新center,输出是将对应于label的centers减去对应的diff,如果同一个标签出现多次,那么就减去多次(diff1与centers维度不同) 64 | centers = tf.scatter_sub(centers, label, diff1) 65 | # diff2维度与centers相同可以直接减 66 | centers = tf.subtract(centers, diff2) 67 | 68 | return loss, centers -------------------------------------------------------------------------------- /loss_function/spearman_loss.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import * 3 | from keras.layers import * 4 | from keras.optimizers import * 5 | import keras.backend as K 6 | 7 | def mse_loss(y_true, y_pred): 8 | return tf.losses.mean_squared_error(y_true, y_pred) 9 | 10 | # rank pearson loss 11 | def pearson_loss(y_true_rank, y_pred_rank, eps=1e-10): 12 | y_true_mean = K.mean(y_true_rank) 13 | y_pred_mean = K.mean(y_pred_rank) 14 | u1 = (y_true_rank - y_true_mean) 15 | u2 = (y_pred_rank - y_pred_mean) 16 | u=K.sum(tf.multiply(u1,u2)) 17 | d=K.sqrt(K.sum(K.square(u1))*K.sum(K.square(u2))) 18 | rou=tf.div(u,d+eps) 19 | return 1.-rou 20 | 21 | # feature to rank 22 | class SpearRank(Layer): 23 | def __init__(self, eps=1e-10, **kwargs): 24 | self.eps = eps 25 | super(SpearRank, self).__init__(**kwargs) 26 | 27 | def build(self, input_shape): 28 | super(SpearRank, self).build(input_shape) 29 | 30 | def call(self, x, mask=None): 31 | x_transpose = tf.reshape(tf.transpose(x), (1, -1)) 32 | x1 = x - x_transpose 33 | x2 = tf.abs(x - x_transpose) + self.eps 34 | x = tf.div(x1, x2) 35 | x = tf.reshape(tf.reduce_sum(x, axis=1), (-1, 1)) 36 | x = x + tf.reduce_min(x) * (-1) 37 | 38 | return x 39 | 40 | def model(timesteps=64, dim=512, unit=256,ac='sigmoid'): 41 | inputs = Input((timesteps, dim)) 42 | x = Masking(mask_value=0)(inputs) 43 | x = LSTM(unit, return_sequences=False)(x) 44 | x = Dense(1, activation=ac)(x) 45 | x1 = SpearRank()(x) 46 | 47 | return Model(inputs=inputs, outputs=[x,x1]) 48 | 49 | model=model() 50 | model.compile(optimizer='adam',loss=[mse_loss,pearson_loss],loss_weights=[0.9,0.1]) 51 | X=np.random.random((320,64,512)) 52 | y=np.random.random(320) 53 | y1=np.argsort(y) 54 | model.fit(X,[y,y1]) 55 | 56 | -------------------------------------------------------------------------------- /loss_function/sphere_loss.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def sphere_loss(x, y, l, num_cls, m = 2): 4 | ''' 5 | x: B x D - data 6 | y: B x 1 - label 7 | l: 1 - lambda 8 | ''' 9 | xs = x.get_shape() 10 | w = tf.get_variable("asoftmax/W", [xs[1], num_cls], dtype=tf.float32, 11 | initializer=tf.contrib.layers.xavier_initializer()) 12 | 13 | eps = 1e-8 14 | xw = tf.matmul(x,w) 15 | if m == 0: 16 | return xw, tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=xw)) 17 | w_norm = tf.norm(w, axis = 0) + eps 18 | logits = xw/w_norm 19 | if y is None: 20 | return logits, None 21 | ordinal = tf.constant(list(range(0, xs[0])), tf.int64) 22 | ordinal_y = tf.stack([ordinal, y], axis = 1) 23 | x_norm = tf.norm(x, axis = 1) + eps 24 | sel_logits = tf.gather_nd(logits, ordinal_y) 25 | cos_th = tf.div(sel_logits, x_norm) 26 | 27 | if m == 1: 28 | loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)) 29 | else: 30 | if m == 2: 31 | res = 2*tf.multiply(tf.sign(cos_th), tf.square(cos_th)) - 1 32 | elif m == 4: 33 | cos_th2 = tf.square(cos_th) 34 | cos_th4 = tf.pow(cos_th, 4) 35 | sign0 = tf.sign(cos_th) 36 | sign3 = tf.multiply(tf.sign(2*cos_th2 - 1), sign0) 37 | sign4 = 2*sign0 + sign3 - 3 38 | res = sign3*(8*cos_th4 - 8*cos_th2 + 1) + sign4 39 | else: 40 | raise ValueError('unsupported value of m') 41 | 42 | scaled_logits = tf.multiply(res, x_norm) 43 | 44 | f = 1.0/(1.0+l) 45 | ff = 1.0 - f 46 | comb_logits_diff = tf.add(logits, tf.scatter_nd(ordinal_y, tf.subtract(scaled_logits, sel_logits), logits.get_shape())) 47 | updated_logits = ff*logits + f*comb_logits_diff 48 | 49 | loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=updated_logits)) 50 | 51 | return logits, loss -------------------------------------------------------------------------------- /loss_function/triplet_loss.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | # triplet loss 4 | # loss=max(0, alpha + cos(anchor,negative) − cos(anchor,positive)) 5 | # loss=max(0, alpha + ||anchor,positive|| − ||anchor,negative||) 6 | def triplet_loss(y_true, y_pred, alpha = 0.2): 7 | """ 8 | Implementation of the triplet loss as defined by formula 9 | 10 | Arguments: 11 | y_true -- true labels, required when you define a loss in Keras, you don't need it in this function. 12 | y_pred -- python list containing three objects: 13 | anchor -- the encodings for the anchor images, of shape (None, 128) 14 | positive -- the encodings for the positive images, of shape (None, 128) 15 | negative -- the encodings for the negative images, of shape (None, 128) 16 | 17 | Returns: 18 | loss -- real number, value of the loss 19 | """ 20 | 21 | anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2] 22 | 23 | ### START CODE HERE ### (≈ 4 lines) 24 | # Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1 25 | pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive))) 26 | # Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1 27 | neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative))) 28 | # Step 3: subtract the two previous distances and add alpha. 29 | basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha) 30 | # Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples. 31 | loss = tf.reduce_sum(tf.maximum(basic_loss, 0.)) 32 | ### END CODE HERE ### 33 | 34 | return loss -------------------------------------------------------------------------------- /metrics/calccc.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | import argparse 3 | import os 4 | import csv 5 | import sys 6 | 7 | 8 | from scipy.stats import pearsonr 9 | import numpy 10 | import pandas 11 | 12 | 13 | def mse(y_true, y_pred): 14 | from sklearn.metrics import mean_squared_error 15 | return mean_squared_error(y_true,y_pred) 16 | 17 | def f1(y_true, y_pred): 18 | from sklearn.metrics import f1_score 19 | label = [0,1,2,3,4,5,6] 20 | return f1_score(y_true,y_pred,labels=label,average="micro") 21 | 22 | def ccc(y_true, y_pred): 23 | true_mean = numpy.mean(y_true) 24 | true_variance = numpy.var(y_true) 25 | pred_mean = numpy.mean(y_pred) 26 | pred_variance = numpy.var(y_pred) 27 | 28 | rho,_ = pearsonr(y_pred,y_true) 29 | 30 | std_predictions = numpy.std(y_pred) 31 | 32 | std_gt = numpy.std(y_true) 33 | 34 | 35 | ccc = 2 * rho * std_gt * std_predictions / ( 36 | std_predictions ** 2 + std_gt ** 2 + 37 | (pred_mean - true_mean) ** 2) 38 | 39 | return ccc, rho 40 | 41 | 42 | 43 | def calculateCCC(validationFile, modelOutputFile): 44 | 45 | 46 | dataY = pandas.read_csv(validationFile, header=0, sep=",") 47 | 48 | dataYPred = pandas.read_csv(modelOutputFile, header=0, sep=",") 49 | 50 | dataYArousal = dataY["arousal"] 51 | dataYValence = dataY["valence"] 52 | dataYPredArousal = dataYPred["arousal"] 53 | dataYPredValence = dataYPred["valence"] 54 | 55 | arousalCCC, acor = ccc(dataYArousal, dataYPredArousal) 56 | arousalmse = mse(dataYArousal, dataYPredArousal) 57 | valenceCCC, vcor = ccc(dataYValence, dataYPredValence) 58 | valencemse = mse(dataYValence, dataYPredValence) 59 | 60 | print ("Arousal CCC: ", arousalCCC) 61 | print ("Arousal Pearson Cor: ", acor) 62 | print ("Arousal MSE: ", arousalmse) 63 | print ("Valence CCC: ", valenceCCC) 64 | print ("Valence cor: ", vcor) 65 | print ("Valence MSE: ", valencemse) 66 | 67 | 68 | if __name__ == "__main__": 69 | parser = argparse.ArgumentParser() 70 | parser.add_argument("validationFile") 71 | parser.add_argument("modelOutputFile") 72 | 73 | opt = parser.parse_args() 74 | if not os.path.exists(opt.validationFile): 75 | print("Cannot find validation File") 76 | sys.exit(-1) 77 | 78 | if not os.path.exists(opt.modelOutputFile): 79 | print("Cannot find modelOutput File") 80 | sys.exit(-1) 81 | 82 | calculateCCC(opt.validationFile, opt.modelOutputFile) -------------------------------------------------------------------------------- /metrics/tensorflow_keras_metrics.py: -------------------------------------------------------------------------------- 1 | from keras import backend as K 2 | 3 | #TPR 4 | def true_positive_rate(y_true, y_pred, mode='p'): 5 | threshold_value = 0.5 6 | if mode=='n': 7 | threshold_value=1-threshold_value 8 | # works as round() with threshold_value 9 | y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), threshold_value), K.floatx()) 10 | true_positives = K.round(K.sum(K.clip(y_true * y_pred, 0, 1))) 11 | real_positives = K.sum(K.clip(y_true, 0, 1)) 12 | return true_positives / (real_positives + K.epsilon()) 13 | 14 | #TNR 15 | def true_negative_rate(y_true, y_pred): 16 | y_true = K.ones((32,))-y_true 17 | y_pred = K.ones((32,))-y_pred 18 | return true_positive_rate(y_true, y_pred,mode='n') 19 | 20 | #G-MEAN:(TPR*TNR)^0.5 21 | def geometric_mean(y_true, y_pred): 22 | return K.sqrt(true_positive_rate(y_true, y_pred)*true_negative_rate(y_true, y_pred)) 23 | 24 | #M-ACC (TPR*TNR)/2 25 | def mean_accuracy(y_true, y_pred): 26 | return (true_positive_rate(y_true, y_pred)+true_negative_rate(y_true, y_pred))/2 -------------------------------------------------------------------------------- /multi_pooling.py: -------------------------------------------------------------------------------- 1 | from DeMeshNet import DeMeshNet 2 | from skimage import io, transform 3 | from tqdm import tqdm 4 | import numpy as np 5 | import os 6 | import matplotlib.pyplot as plt 7 | os.environ["CUDA_VISIBLE_DEVICES"] = '2' 8 | # model = DeMeshNet(model_folder='model/Hourglass_modelsV3_epoch7.h5',gpu_use=0.3,input_shape=(224, 176, 3), nstack=2, level=4, module=1, filters=128) 9 | 10 | base_path='../../wangdexun/image_data/data_tang/person_img/' 11 | path1=np.array(os.listdir(base_path)) 12 | 13 | # 分割path 14 | split_num=12 15 | temp_len=len(path1)//split_num 16 | base_paths=[] 17 | for i in range(split_num): 18 | if i != split_num-1: 19 | base_paths.append(path1[i*temp_len:(i+1)*temp_len]) 20 | else: 21 | base_paths.append(path1[i*temp_len:]) 22 | 23 | def demesh(base_path, path1): 24 | model = DeMeshNet(model_folder='model/Hourglass_modelsV3_epoch7.h5',gpu_use=0.02,input_shape=(224, 176, 3), nstack=2, level=4, module=1, filters=128) 25 | mis_num=0 26 | for p in tqdm(path1): 27 | path2=base_path+p 28 | for p2 in os.listdir(path2): 29 | if '_m' in p2 and '_d' not in p2: 30 | try: 31 | img_demeshed=model.predict_one(path2+'/'+p2) 32 | io.imsave((path2+'/'+p2).split('.jpg')[0]+'_d.jpg',img_demeshed) 33 | except: 34 | mis_num+=1 35 | return mis_num 36 | 37 | from multiprocessing import Pool 38 | pool=Pool() 39 | result=[] 40 | for i in base_paths: 41 | result.append(pool.apply_async(demesh, kwds={'base_path':base_path, 'path1':i})) 42 | pool.close() 43 | pool.join() 44 | 45 | mis_num=np.sum([i.get() for i in result]) 46 | print('wrong:',mis_num) 47 | -------------------------------------------------------------------------------- /multi_thread.py: -------------------------------------------------------------------------------- 1 | from threadpool import ThreadPool,makeRequests 2 | import cv2 3 | import numpy as np 4 | from tqdm import tqdm 5 | # Load images and define data_generator 6 | # images=glob.glob(os.path.join(data_dir,'*.png')) 7 | data=[] 8 | label=[] 9 | 10 | def preprocess(image): 11 | image=np.transpose(image,(2,0,1)) 12 | image=(image-127.5)*0.0078125 13 | image=image.astype(np.float32) 14 | return image 15 | 16 | def load_image(path): 17 | sr_img=cv2.imread(path) 18 | lr_img=cv2.resize(sr_img,(sr_img.shape[1]//8,sr_img.shape[0]//8)) 19 | sr_img=preprocess(sr_img) 20 | lr_img=preprocess(lr_img) 21 | return lr_img,sr_img 22 | 23 | with tqdm(total=len(images),desc='Loading images') as pbar: 24 | def callback(req,x): 25 | data.append(x[0]) 26 | label.append(x[1]) 27 | pbar.update() 28 | 29 | t_pool=ThreadPool(16) 30 | requests=makeRequests(load_image,images,callback=callback) 31 | for req in requests: 32 | t_pool.putRequest(req) 33 | t_pool.wait() 34 | np.rot90() 35 | -------------------------------------------------------------------------------- /soft_attention/keras_version/attention_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewrfcas/Machine-Learning-Toolbox/b040f39201c318db545c32b408f0c56c553f60f6/soft_attention/keras_version/attention_model.png -------------------------------------------------------------------------------- /soft_attention/keras_version/attention_model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import * 3 | from keras.layers import * 4 | from keras.optimizers import * 5 | from keras.utils.vis_utils import plot_model 6 | 7 | def attention_model(timesteps=101, dim=512, unit=128, n_class=5): 8 | inputs = Input((timesteps, dim),name='input_data') 9 | # the weights f of timesteps (timesteps * n_class) 10 | f_weights = Input((timesteps, n_class),name='f_weights') 11 | 12 | x = BatchNormalization()(inputs) 13 | x = LSTM(unit, return_sequences=True)(x) 14 | x = Reshape((timesteps, unit))(x) 15 | 16 | # multiply weights f_weights to the result from LSTM 17 | x = Lambda(lambda x: tf.matmul(x[0], x[1], transpose_a=True),name='fweights_mul')([f_weights, x]) 18 | 19 | # final multi-softmax 20 | x = Lambda(lambda x: tf.unstack(x, axis=1),name='unstack1')(x) # len(x)=timestaps 21 | xs = [] 22 | # each class with different weights w^n 23 | for x_i in x: 24 | xs.append(Dense(1)(x_i)) 25 | x = Lambda(lambda x: tf.stack(x, axis=1),name='stack1')(xs) 26 | x = Reshape((n_class,))(x) 27 | x = Activation('softmax')(x) 28 | 29 | return Model(inputs=[inputs, f_weights], outputs=x) 30 | 31 | model=attention_model() 32 | plot_model(model,to_file='attention_model.png',show_shapes=False) -------------------------------------------------------------------------------- /soft_attention/keras_version/attention_weights_model.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from keras.models import * 3 | from keras.layers import * 4 | from keras.optimizers import * 5 | from keras.utils.vis_utils import plot_model 6 | 7 | def weights_model(timesteps=101, dim=512, unit=128, emotion_embedding_dim=64, n_class=5): 8 | inputs = Input((timesteps, dim)) 9 | x = BatchNormalization()(inputs) 10 | x = LSTM(unit, return_sequences=True)(x) 11 | x = Reshape((timesteps, unit))(x) 12 | 13 | # FC (W^h) for mapping the dim from unit to emotion_embedding_dim 14 | x = Lambda(lambda x: tf.unstack(x, axis=1),name='unstack1')(x) # len(x)=timestaps 15 | xs = [] 16 | # unit->emotion_embedding_dim 17 | dense2=Dense(emotion_embedding_dim,name='Wh') 18 | for x_i in x: 19 | x_temp=dense2(x_i) 20 | xs.append(x_temp) 21 | x = xs 22 | # FC (e={e_1,e_2,...}) for embedding mapping from emotion_embedding_dim to n_class 23 | xs = [] 24 | # emotion_embedding_dim->n_class 25 | emotion_vectors=Dense(n_class,activation='softmax', name='emotion_vectors') 26 | for x_i in x: 27 | x_temp=emotion_vectors(x_i) 28 | xs.append(x_temp) 29 | f_weights = Lambda(lambda x: tf.stack(x, axis=1), name='f_weights')(xs) # len(x)=timestaps 30 | x = Lambda(lambda x: tf.reduce_mean(x, axis=1),name='reduce_mean1')(f_weights) 31 | 32 | return Model(inputs=inputs, outputs=x) 33 | 34 | model=weights_model() 35 | plot_model(model,to_file='f_weights.png',show_shapes=False) -------------------------------------------------------------------------------- /soft_attention/keras_version/f_weights.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewrfcas/Machine-Learning-Toolbox/b040f39201c318db545c32b408f0c56c553f60f6/soft_attention/keras_version/f_weights.png -------------------------------------------------------------------------------- /tensorflow_api/multi_gpu_training.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import os 3 | import logging 4 | logging.getLogger().setLevel(logging.INFO) 5 | 6 | 7 | os.environ["CUDA_VISIBLE_DEVICES"] = '0,1' 8 | 9 | 10 | def model_fn(features, labels, mode): 11 | layer = tf.layers.Dense(1) 12 | logits = layer(features) 13 | 14 | if mode == tf.estimator.ModeKeys.PREDICT: 15 | predictions = {"logits": logits} 16 | return tf.estimator.EstimatorSpec(mode, predictions=predictions) 17 | 18 | loss = tf.losses.mean_squared_error(labels=labels, predictions=tf.squeeze(logits)) 19 | 20 | if mode == tf.estimator.ModeKeys.EVAL: 21 | return tf.estimator.EstimatorSpec(mode, loss=loss) 22 | 23 | if mode == tf.estimator.ModeKeys.TRAIN: 24 | train_op = tf.train.GradientDescentOptimizer(0.2).minimize(loss) 25 | return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op) 26 | 27 | def input_fn(): 28 | features = tf.data.Dataset.from_tensors([[1.]]).repeat(100).batch(10) 29 | labels = tf.data.Dataset.from_tensors(1.).repeat(100).batch(10) 30 | return tf.data.Dataset.zip((features, labels)) 31 | 32 | distribution = tf.contrib.distribute.MirroredStrategy(num_gpus=2) 33 | config = tf.estimator.RunConfig(train_distribute=distribution) 34 | classifier = tf.estimator.Estimator(model_fn=model_fn, config=config, model_dir='model') 35 | classifier.train(input_fn=input_fn) 36 | classifier.evaluate(input_fn=input_fn) -------------------------------------------------------------------------------- /tensorflow_api/read_record.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | def get_record_parser(is_test=False): 4 | def parser(example): 5 | para_limit = 400 if is_test else 128 6 | ques_limit = 400 if is_test else 128 7 | char_limit = 64 8 | features = tf.parse_single_example(example, 9 | features={ 10 | "context_idxs": tf.FixedLenFeature([], tf.string), 11 | "ques_idxs": tf.FixedLenFeature([], tf.string), 12 | "context_char_idxs": tf.FixedLenFeature([], tf.string), 13 | "ques_char_idxs": tf.FixedLenFeature([], tf.string), 14 | 'feats': tf.FixedLenFeature([], tf.string), 15 | "y1": tf.FixedLenFeature([], tf.float32), 16 | "y2": tf.FixedLenFeature([], tf.float32), 17 | "id": tf.FixedLenFeature([], tf.int64) 18 | }) 19 | context_idxs = tf.reshape(tf.decode_raw(features["context_idxs"], tf.int64), [para_limit]) 20 | ques_idxs = tf.reshape(tf.decode_raw(features["ques_idxs"], tf.int64), [ques_limit]) 21 | context_char_idxs = tf.reshape(tf.decode_raw(features["context_char_idxs"], tf.int64), [para_limit, char_limit]) 22 | ques_char_idxs = tf.reshape(tf.decode_raw(features["ques_char_idxs"], tf.int64), [ques_limit, char_limit]) 23 | feats = tf.reshape(tf.decode_raw(features['feats'], tf.float64), [para_limit]) 24 | y1 = features["y1"] 25 | y2 = features["y2"] 26 | qa_id = features["id"] 27 | 28 | return context_idxs, ques_idxs, context_char_idxs, ques_char_idxs, feats, y1, y2, qa_id 29 | 30 | return parser 31 | 32 | 33 | 34 | dataset = tf.data.TFRecordDataset('train.tfrecords').map(get_record_parser(), num_parallel_calls=2).shuffle(15000) 35 | dataset = dataset.batch(32) 36 | # GPU->CPU->GPU 37 | iterator = dataset.make_initializable_iterator() 38 | next_element = iterator.get_next() 39 | with tf.Session() as sess: 40 | for i in range(20): 41 | sess.run(iterator.initializer) 42 | print('epoch:',i+1) 43 | while True: 44 | try: 45 | context_idxs, ques_idxs, context_char_idxs, ques_char_idxs, feats, y1, y2, qa_id = sess.run(next_element) 46 | print(qa_id) 47 | except tf.errors.OutOfRangeError: 48 | print("End of epoch %d" % (i+1)) 49 | break 50 | -------------------------------------------------------------------------------- /tensorflow_api/save_record.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | from tqdm import tqdm 4 | 5 | tfrecords_filename = 'train.tfrecords' 6 | writer = tf.python_io.TFRecordWriter(tfrecords_filename) 7 | context_idxs = np.random.randint(0,1000,(3000,128)) 8 | ques_idxs = np.random.randint(0,1000,(3000,128)) 9 | context_char_idxs = np.random.randint(0,100,(3000,128,64)) 10 | ques_char_idxs = np.random.randint(0,100,(3000,128,64)) 11 | feats = np.random.random((3000,128)) 12 | y1=np.zeros(3000) 13 | y2=np.ones(3000) 14 | id=np.arange(3000) 15 | 16 | for i in tqdm(range(3000)): 17 | record = tf.train.Example(features=tf.train.Features(feature={ 18 | "context_idxs": tf.train.Feature(bytes_list=tf.train.BytesList(value=[context_idxs[i,::].tostring()])), 19 | "ques_idxs": tf.train.Feature(bytes_list=tf.train.BytesList(value=[ques_idxs[i,::].tostring()])), 20 | "context_char_idxs": tf.train.Feature(bytes_list=tf.train.BytesList(value=[context_char_idxs[i,::].tostring()])), 21 | "ques_char_idxs": tf.train.Feature(bytes_list=tf.train.BytesList(value=[ques_char_idxs[i,::].tostring()])), 22 | 'feats': tf.train.Feature(bytes_list=tf.train.BytesList(value=[feats[i,::].tostring()])), 23 | "y1": tf.train.Feature(float_list=tf.train.FloatList(value=[y1[i]])), 24 | "y2": tf.train.Feature(float_list=tf.train.FloatList(value=[y2[i]])), 25 | "id": tf.train.Feature(int64_list=tf.train.Int64List(value=[id[i]])) 26 | })) 27 | 28 | writer.write(record.SerializeToString()) 29 | 30 | writer.close() 31 | -------------------------------------------------------------------------------- /tensorflow_api/training_validating_for_tf.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | mnist_train = pd.read_csv('../data/mnist_train.csv',header=None) 4 | mnist_test = pd.read_csv('../data/mnist_test.csv',header=None) 5 | 6 | mnist_train.head() 7 | X_train=mnist_train.loc[:,1:].values 8 | y_train=mnist_train.loc[:,0].values 9 | X_test=mnist_test.loc[:,1:].values 10 | y_test=mnist_test.loc[:,0].values 11 | 12 | X_train=X_train/255. 13 | X_test=X_test/255. 14 | 15 | print('X_train:',X_train.shape) 16 | print('y_train:',y_train.shape) 17 | print('X_test:',X_test.shape) 18 | print('y_test:',y_test.shape) 19 | 20 | import tensorflow as tf 21 | sess = tf.InteractiveSession() 22 | 23 | inputs = tf.placeholder(tf.float32, [None, 784]) 24 | inputs_reshaped = tf.reshape(inputs, (-1,28,28,1)) 25 | y = tf.placeholder(tf.int64, [None, 1]) 26 | y_onehot = tf.one_hot(y, 10) 27 | n_class=10 28 | n_feature=512 29 | 30 | # 定义模型,复杂loss 31 | from tensorflow.contrib.keras import layers 32 | 33 | def VGG6(inputs, n_class=10): 34 | # Block 1 35 | x = layers.Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv1')(inputs) 36 | x = layers.Conv2D(32, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) 37 | x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 38 | 39 | # Block 2 40 | x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) 41 | x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) 42 | x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 43 | 44 | x = layers.Flatten(name='flatten')(x) 45 | x = layers.Dense(512, activation='relu', name='fc1')(x) 46 | features = layers.Dense(512, activation='relu', name='fc2')(x) 47 | outputs = layers.Dense(n_class, activation='softmax', name='predictions')(features) 48 | 49 | return outputs 50 | 51 | # training graph 52 | import numpy as np 53 | 54 | # cal loss 55 | preds = VGG6(inputs_reshaped) 56 | softmax_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=y_onehot)) 57 | 58 | # cal acc 59 | result = tf.argmax(preds, 1) 60 | ground_truth = tf.reshape(y, [-1]) 61 | correct_prediction = tf.equal(result, ground_truth) 62 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 63 | 64 | # train 65 | train_step = tf.train.AdamOptimizer(1e-4).minimize(softmax_loss) 66 | 67 | n_epochs = 50 68 | batch_size = 128 69 | n_batch = int(X_train.shape[0] / batch_size) 70 | n_batch_val = int(X_test.shape[0] / batch_size) 71 | sess.run(tf.global_variables_initializer()) 72 | early_stop=10 73 | best_val_acc=0 74 | patience=0 75 | for epoch in range(n_epochs): 76 | # training step 77 | index = np.arange(X_train.shape[0]) 78 | np.random.shuffle(index) 79 | X_train = X_train[index, :] 80 | y_train = y_train[index] 81 | sum_loss = 0 82 | sum_acc = 0 83 | last_train_str = "" 84 | for i in range(n_batch): 85 | x_temp = X_train[i * batch_size:(i + 1) * batch_size, :] 86 | y_temp = y_train[i * batch_size:(i + 1) * batch_size] 87 | y_temp = np.reshape(y_temp, (batch_size, 1)) 88 | feed_dict = {inputs: x_temp, y: y_temp} 89 | _, loss_value, acc_value = sess.run([train_step, softmax_loss, accuracy], feed_dict=feed_dict) 90 | sum_loss += loss_value 91 | sum_acc += (acc_value * 100) 92 | last_train_str = "\r[epoch:%d/%d, steps:%d/%d] -loss: %.4f - acc: %.2f%%" % \ 93 | (epoch + 1, n_epochs, i + 1, n_batch, sum_loss / (i + 1), sum_acc / (i + 1)) 94 | print(last_train_str, end=' ', flush=True) 95 | 96 | # validating step 97 | sum_loss = 0 98 | sum_acc = 0 99 | for i in range(n_batch_val): 100 | x_temp = X_test[i * batch_size:(i + 1) * batch_size, :] 101 | y_temp = y_test[i * batch_size:(i + 1) * batch_size] 102 | y_temp = np.reshape(y_temp, (batch_size, 1)) 103 | feed_dict = {inputs: x_temp, y: y_temp} 104 | loss_value, acc_value = sess.run([softmax_loss, accuracy], feed_dict=feed_dict) 105 | sum_loss += loss_value 106 | sum_acc += (acc_value * 100) 107 | print(last_train_str + " [validate:%d/%d] -loss: %.4f - acc: %.2f%%" % \ 108 | (i + 1, n_batch_val, sum_loss / (i + 1), sum_acc / (i + 1)), \ 109 | end=' ', flush=True) 110 | print('\n') 111 | 112 | # early stop 113 | if sum_acc / (n_batch_val) > best_val_acc: 114 | patience = 0 115 | print(str(round(sum_acc, 2)) + '% > ' + str(round(best_val_acc, 2)) + '%', 'patience:', patience) 116 | best_val_acc = sum_acc / (n_batch_val) 117 | else: 118 | patience += 1 119 | print(str(round(sum_acc, 2)) + '% <= ' + str(round(best_val_acc, 2)) + '%', 'patience:', patience) 120 | if patience > early_stop: 121 | print('early stopping!') 122 | break 123 | 124 | -------------------------------------------------------------------------------- /xgb_lgb/xgbtest.py: -------------------------------------------------------------------------------- 1 | # aud features 2 | import pandas as pd 3 | import numpy as np 4 | df_train=pd.read_csv('aud_feature_train_256d.csv') 5 | df_val=pd.read_csv('aud_feature_val_256d.csv') 6 | df_train.sort_values(by=['filename_1'],inplace=True) 7 | df_val.sort_values(by=['filename_1'],inplace=True) 8 | df_val.head() 9 | id_train=df_train.pop('filename_1').values 10 | id_val=df_val.pop('filename_1').values 11 | id_train=np.array(list(map(lambda x:x.split('_')[0]+'_'+x.split('_')[-1],id_train))) 12 | id_val=np.array(list(map(lambda x:x.split('_')[0]+'_'+x.split('_')[-1],id_val))) 13 | X_train_aud=df_train.values 14 | X_val_aud=df_val.values 15 | # read label 16 | from tqdm import tqdm 17 | label_train=pd.read_csv('train_label.csv') 18 | id_label_train=label_train['id'].values 19 | y_label_train=label_train[['y1','y2']].values 20 | 21 | label_val=pd.read_csv('val_label.csv') 22 | id_label_val=label_val['id'].values 23 | y_label_val=label_val[['y1','y2']].values 24 | 25 | # id_label_train=np.concatenate((id_label_train,id_label_val)) 26 | id_label_train=np.array(list(map(lambda x:x.split('_')[0]+'_'+x.split('_')[-1],id_label_train))) 27 | dicti={} 28 | for i,iid in enumerate(id_label_train): 29 | dicti[id_label_train[i]]=y_label_train[i,:] 30 | y_train_aud=[] 31 | for i in tqdm(id_train): 32 | y_train_aud.append(dicti[i]) 33 | y_train_aud=np.array(y_train_aud) 34 | 35 | id_label_val=np.array(list(map(lambda x:x.split('_')[0]+'_'+x.split('_')[-1],id_label_val))) 36 | dicti={} 37 | for i,iid in enumerate(id_label_val): 38 | dicti[id_label_val[i]]=y_label_val[i,:] 39 | y_val_aud=[] 40 | for i in tqdm(id_val): 41 | y_val_aud.append(dicti[i]) 42 | y_val_aud=np.array(y_val_aud) 43 | print(X_train_aud.shape) 44 | print(X_val_aud.shape) 45 | print(y_train_aud.shape) 46 | print(y_val_aud.shape) 47 | print(id_train) 48 | X_train=X_train_aud 49 | X_val=X_val_aud 50 | y_train=y_train_aud 51 | y_val=y_val_aud 52 | y_val[:,0]=(y_val[:,0]+1)/2 53 | 54 | # # 按照人来进行分割 55 | # X_person=np.unique(list(map(lambda x:x.split('_')[0],id_all))) 56 | # print(X_person) 57 | # np.random.seed(2018) 58 | # np.random.shuffle(X_person) 59 | # X_person_val=X_person[0:len(X_person)//5] 60 | # X_person_train=X_person[len(X_person)//5:] 61 | # train_index=[] 62 | # val_index=[] 63 | # from tqdm import tqdm 64 | # for i in tqdm(range(len(X_all))): 65 | # if id_all[i].split('_')[0] in X_person_val: 66 | # val_index.append(i) 67 | # else: 68 | # train_index.append(i) 69 | # train_index=np.array(train_index) 70 | # val_index=np.array(val_index) 71 | # print(train_index) 72 | # print(val_index) 73 | 74 | # lightgbm 75 | import lightgbm as lgb 76 | from metrics import calccc 77 | from sklearn.model_selection import ParameterGrid 78 | # 自定义ccc评价 79 | def metric_ccc(preds,lgbdata): 80 | labels=lgbdata.get_label() 81 | ccc,_=calccc.ccc(labels,preds) 82 | return 'ccc value:',ccc,True 83 | 84 | def correct(train_y, pred_val_y): 85 | train_std = np.std(train_y) 86 | val_std = np.std(pred_val_y) 87 | mean = np.mean(pred_val_y) 88 | pred_val_y = np.array(pred_val_y) 89 | pred_val_y = mean + (pred_val_y - mean) * train_std / val_std 90 | return pred_val_y 91 | 92 | params = { 93 | 'metric': ['metric_ccc'], 94 | 'application': ['regression'], 95 | 'learning_rate':[0.015], 96 | 'feature_fraction': [0.9], 97 | 'max_depth': [15], 98 | 'num_leaves':[100], 99 | 'bagging_fraction': [0.8], 100 | 'bagging_freq':[5], 101 | 'min_data_in_leaf':[10], 102 | 'min_gain_to_split':[0], 103 | 'num_iterations':[500], 104 | 'lambda_l1':[1], 105 | 'lambda_l2':[0.1], 106 | 'verbose':[1] 107 | # 'is_unbalance':[True] 108 | } 109 | params=list(ParameterGrid(params)) 110 | lgbtrain=lgb.Dataset(X_train,label=y_train[:,1]) 111 | lgbeval=lgb.Dataset(X_val,label=y_val[:,1],reference=lgbtrain) 112 | best_ccc=0 113 | for param in params: 114 | print(param) 115 | clf = lgb.cv(param, lgbtrain, nfold=5, num_boost_round=param['num_iterations'], \ 116 | early_stopping_rounds=50, feval=metric_ccc, verbose_eval=True) 117 | print(clf) 118 | # if clf.best_score['valid_0']['ccc value:']>best_ccc: 119 | # best_ccc=clf.best_score['valid_0']['ccc value:'] 120 | # best_param=param 121 | # best_it=clf.best_iteration 122 | # print('noval best interation: '+str(clf.best_iteration)) 123 | # y_pred=clf.predict(X_val) 124 | # y_pred2 = np.clip(correct(y_train[:,1], y_pred),-1,1) 125 | # ccc2,_=calccc.ccc(y_val[:,1],correct(y_train[:,1], y_pred2)) 126 | # print('best ccc:',best_ccc,'(',ccc2,')') 127 | # print('best param:',best_param) 128 | # print('best iteration:',best_it) --------------------------------------------------------------------------------