├── HoMM_mnist ├── .idea │ ├── JLSPT.iml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── Alexnet.py ├── Alexnet.pyc ├── DataLoader.py ├── Lenet.py ├── Lenet2D.py ├── Sensitivity_plot.py ├── TrainLenet.py ├── TrainLenet3.py ├── TrainLenetsne.py ├── TrainNet.py ├── Utils.py └── center_loss.py ├── HoMM_office ├── data │ ├── Art.txt │ ├── Clipart.txt │ ├── Product.txt │ ├── RealWorld.txt │ ├── amazon.txt │ ├── dslr.txt │ ├── train (copy).txt │ ├── val.txt │ └── webcam.txt ├── resnet │ ├── Utils.py │ ├── Utils.pyc │ ├── center_loss.py │ ├── download_weights.sh │ ├── examples.sh │ ├── finetune.py │ ├── model.py │ ├── model.pyc │ └── ttt.py └── utils │ ├── __pycache__ │ └── preprocessor.cpython-35.pyc │ ├── preprocessor.py │ └── preprocessor.pyc ├── README.md └── img ├── img1.PNG ├── img2.PNG ├── img3.PNG ├── img4.PNG ├── img5.PNG ├── img6.PNG └── img7.PNG /HoMM_mnist/.idea/JLSPT.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /HoMM_mnist/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /HoMM_mnist/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /HoMM_mnist/Alexnet.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from numpy import * 3 | import matplotlib.pyplot as plt 4 | import warnings 5 | import os 6 | from scipy import io 7 | import tensorflow.contrib.slim as slim 8 | warnings.filterwarnings("ignore") 9 | # os.environ['CUDA_VISIBLE_DEVICES']='' 10 | 11 | 12 | 13 | class AlexNet(): 14 | 15 | def __init__(self,images,num_class,keep_prob,Training_flag,scope_name,reuse=True): 16 | self.input=images 17 | self.NUM_class=num_class 18 | self.KEEP_prob=keep_prob 19 | self.scope=scope_name 20 | self.Regularizer_scale = 0.01 21 | self.Training_flag = Training_flag 22 | self.reuse=reuse 23 | self.BuildAlexNet() 24 | 25 | 26 | 27 | def BuildAlexNet(self): 28 | with tf.variable_scope("input_layer"): 29 | input_layer=self.input 30 | 31 | conv1 = tf.layers.conv2d(inputs=input_layer, filters=96, kernel_size=[11, 11], strides=4, padding="VALID", 32 | activation=tf.nn.relu, name=self.scope+"conv1",reuse=self.reuse) 33 | norm1 = tf.layers.batch_normalization(conv1, training=self.Training_flag, name=self.scope+"norm1", reuse=self.reuse) 34 | pool1 = tf.layers.max_pooling2d(inputs=norm1, pool_size=[3,3], strides=2, padding="VALID", name=self.scope+"pool1") 35 | 36 | 37 | conv2 = tf.layers.conv2d(inputs=pool1, filters=256, kernel_size=[5, 5], strides=1, padding="SAME", 38 | activation=tf.nn.relu,name=self.scope+"conv2", reuse=self.reuse) 39 | norm2 = tf.layers.batch_normalization(conv2, training=self.Training_flag, name=self.scope+"norm_2", reuse=self.reuse) 40 | pool2 = tf.layers.max_pooling2d(inputs=norm2, pool_size=[3, 3], strides=2, padding="VALID",name=self.scope + "pool2") 41 | 42 | 43 | conv3 = tf.layers.conv2d(inputs=pool2, filters=384, kernel_size=[3, 3], strides=1, padding="SAME", 44 | activation=tf.nn.relu,name=self.scope + "conv3", reuse=self.reuse) 45 | norm3 = tf.layers.batch_normalization(conv3, training=self.Training_flag, name=self.scope + "norm3", reuse=self.reuse) 46 | 47 | 48 | conv4 = tf.layers.conv2d(inputs=norm3, filters=384, kernel_size=[3, 3], strides=1, padding="SAME", 49 | activation=tf.nn.relu,name=self.scope + "conv4", reuse=self.reuse) 50 | norm4 = tf.layers.batch_normalization(conv4, training=self.Training_flag, name=self.scope + "norm4", reuse=self.reuse) 51 | 52 | conv5 = tf.layers.conv2d(inputs=norm4, filters=256, kernel_size=[3, 3], strides=1, padding="SAME", 53 | activation=tf.nn.relu,name=self.scope + "conv5", reuse=self.reuse) 54 | norm5 = tf.layers.batch_normalization(conv5, training=self.Training_flag, name=self.scope + "norm5", reuse=self.reuse) 55 | pool5 = tf.layers.max_pooling2d(inputs=norm5, pool_size=[3, 3], strides=2, padding="VALID", name=self.scope + "pool5") 56 | 57 | flatten_size=int(pool5.shape[1]*pool5.shape[2]*pool5.shape[3]) 58 | flatten_layer=tf.reshape(pool5,[int(pool5.shape[0]),flatten_size]) 59 | 60 | self.fc6 = tf.layers.dense(inputs=flatten_layer, units=4096, name=self.scope + "fc6", reuse=self.reuse) 61 | droplayer6 = tf.layers.dropout(inputs=self.fc6, rate=self.KEEP_prob, training=self.Training_flag, name=self.scope+"droplayer6") 62 | 63 | self.fc7 = tf.layers.dense(inputs=droplayer6, units=4096, name=self.scope + "fc7", reuse=self.reuse) 64 | droplayer7 = tf.layers.dropout(inputs=self.fc7, rate=self.KEEP_prob, training=self.Training_flag, name=self.scope + "droplayer7") 65 | 66 | self.fc8 = tf.layers.dense(inputs=droplayer7, units=self.NUM_class, name=self.scope + "fc8", reuse=self.reuse) 67 | self.softmax_output = tf.nn.softmax(logits=self.fc8, name=self.scope+"softlayer") 68 | 69 | 70 | 71 | def weights_initial(self,session): 72 | weights_dict=load("bvlc_alexnet.npy") 73 | dict=weights_dict.item() 74 | for op_name in dict: 75 | if op_name== u'fc8': 76 | continue 77 | else: 78 | with tf.variable_scope(self.scope + op_name, reuse=True): 79 | for data in dict[op_name]: 80 | if len(data.shape) == 1: 81 | var = tf.get_variable('bias', trainable=True) 82 | session.run(var.assign(data)) 83 | else: 84 | var = tf.get_variable('kernel', trainable=True) 85 | if var.shape==data.shape: 86 | session.run(var.assign(data)) 87 | else: 88 | data=concatenate((data,data),axis=2) 89 | session.run(var.assign(data)) 90 | 91 | 92 | 93 | 94 | 95 | 96 | def Train(self): 97 | pass 98 | 99 | 100 | 101 | def Test(self): 102 | pass 103 | 104 | 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /HoMM_mnist/Alexnet.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/HoMM_mnist/Alexnet.pyc -------------------------------------------------------------------------------- /HoMM_mnist/DataLoader.py: -------------------------------------------------------------------------------- 1 | # import cv2 2 | import os 3 | import matplotlib.pyplot as plt 4 | from numpy import * 5 | import pickle 6 | import random 7 | from sklearn.decomposition import PCA 8 | 9 | 10 | class DataLoader: 11 | def __init__(self, Dataset,source="Amazon",target="Dslr"): 12 | self.Dataset=Dataset 13 | self.source_name=source 14 | self.target_name=target 15 | 16 | 17 | 18 | 19 | def Read(self): 20 | self.IMAGE=zeros((4110,227,227,3)) 21 | self.IMAGE=uint8(self.IMAGE) 22 | k=0 23 | m = 0 24 | self.NumList=zeros(93) 25 | if self.Dataset=="office31": 26 | list=os.listdir("./Dataset/office31/") 27 | list.sort() 28 | for filename in list: 29 | list1=os.listdir("./Dataset/office31/"+filename+"/"+"images/") 30 | list1.sort() 31 | for classes in list1: 32 | list2=os.listdir("./Dataset/office31/"+filename+"/"+"images/"+classes) 33 | list2.sort() 34 | self.NumList[m]=len(list2) 35 | m+=1 36 | for imgname in list2: 37 | img=cv2.imread("./Dataset/office31/"+filename+"/"+"images/"+classes+"/"+imgname) 38 | img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB) 39 | img=cv2.resize(img,(227,227),interpolation=cv2.INTER_CUBIC) 40 | self.IMAGE[k,:,:,:]=img 41 | k+=1 42 | 43 | 44 | self.Construct() 45 | 46 | 47 | 48 | def Construct(self): 49 | print self.NumList 50 | print sum(self.NumList) 51 | amazon_list=self.NumList[:31] 52 | dslr_list=self.NumList[31:62] 53 | webcam_list=self.NumList[62:] 54 | print sum(amazon_list) 55 | print sum(dslr_list) 56 | print sum(webcam_list) 57 | Amazon={} 58 | Dslr={} 59 | Webcam={} 60 | Amazon["Data"]=self.IMAGE[:sum(amazon_list),:,:,:] 61 | Dslr["Data"]=self.IMAGE[sum(amazon_list):sum(amazon_list)+sum(dslr_list),:,:,:] 62 | Webcam["Data"]=self.IMAGE[sum(amazon_list)+sum(dslr_list):sum(amazon_list)+sum(dslr_list)+sum(webcam_list),:,:,:] 63 | Amazon["Label"]=self.list2LabelMatrix(amazon_list) 64 | Dslr["Label"]=self.list2LabelMatrix(dslr_list) 65 | Webcam["Label"]=self.list2LabelMatrix(webcam_list) 66 | output1=open("Amazon.pkl","wb") 67 | pickle.dump(Amazon,output1) 68 | output2 = open("Dslr.pkl", "wb") 69 | pickle.dump(Dslr,output2) 70 | output3 = open("Webcam.pkl", "wb") 71 | pickle.dump(Webcam,output3) 72 | 73 | 74 | 75 | 76 | def list2LabelMatrix(self,Num_list): 77 | num=sum(Num_list) 78 | Label=zeros((num,31)) 79 | List=Num_list.tolist() 80 | List.insert(0,0) 81 | List=cumsum(List) 82 | for i in range (31): 83 | Label[List[i]:List[i+1],i]=1 84 | return Label 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | ############################################################################## 94 | 95 | 96 | def LoadSource(self): 97 | filename=self.source_name+".pkl" 98 | f=open(filename,"r") 99 | Source=pickle.load(f) 100 | SourceData=Source.get("Data") 101 | SourceLabel=Source.get("Label") 102 | # SourceData=SourceData/255.0 103 | f.close() 104 | return self.shuffle(SourceData, SourceLabel) 105 | 106 | 107 | 108 | def LoadTarget(self): 109 | filename=self.target_name+".pkl" 110 | f=open(filename,"r") 111 | Target=pickle.load(f) 112 | TargetData=Target.get("Data") 113 | TargetLabel=Target.get("Label") 114 | # TargetData=TargetData/255.0 115 | TargetData,TargetLabel=self.shuffle(TargetData,TargetLabel) 116 | return TargetData, TargetData, TargetLabel 117 | 118 | 119 | 120 | 121 | def shuffle(self,Data,Label): 122 | ind=range(Data.shape[0]) 123 | random.shuffle(ind) 124 | Data=Data[ind,:,:,:] 125 | Label=Label[ind,:] 126 | return Data, Label 127 | 128 | ################################################################### 129 | 130 | def CalLapMatrix(Data,alpha,K): 131 | dist=Cal_pairwise_dist(Data) 132 | W=exp(-dot(dist,alpha)) 133 | W=get_Ksparse(W,K) 134 | D=get_D(W) 135 | LapMatrix=D-W 136 | return W, LapMatrix 137 | 138 | 139 | 140 | def Cal_pairwise_dist(Data): 141 | DataVector=ImgVectorize(Data) 142 | pca=PCA(n_components=5) 143 | DataVector=pca.fit_transform(DataVector) 144 | norm = lambda x: sum(square(x), 1) 145 | dist = transpose(norm(expand_dims(DataVector, 2) - transpose(DataVector))) 146 | # dist=zeros((DataVector.shape[0],DataVector.shape[0])) 147 | # for i in range(DataVector.shape[0]): 148 | # for j in range(DataVector.shape[0]): 149 | # dist[i,j]=linalg.norm(DataVector[i,:]-DataVector[j,:]) 150 | return dist 151 | 152 | 153 | def ImgVectorize(Data): 154 | datasize=Data.shape[1]*Data.shape[2]*Data.shape[3] 155 | DataVector=zeros((Data.shape[0],datasize)) 156 | for i in range(Data.shape[0]): 157 | img=Data[i,:] 158 | # img=cv2.resize(img,(32,32),interpolation=cv2.INTER_CUBIC) 159 | DataVector[i,:]=img.flatten() 160 | # DataVector /= 255.0 161 | return DataVector 162 | 163 | 164 | 165 | def ImgVectorize_CNN(Data): 166 | vgg_model=resnet50.ResNet50(weights="imagenet") 167 | processed_image=resnet50.preprocess_input(Data.copy()) 168 | predictions=vgg_model.predict(processed_image) 169 | return predictions 170 | 171 | 172 | 173 | def get_Ksparse(Data,K): 174 | for i in range(Data.shape[0]): 175 | X=Data[i,:] 176 | X=list(X) 177 | X.sort(reverse=True) 178 | ind=Data[i,:] > X[K] 179 | ind=ind.astype("float") 180 | Data[i,:]=ind 181 | # Data[i,:]=Data[i,:] * ind 182 | return Data 183 | 184 | 185 | 186 | def get_D(Data): 187 | D=zeros(Data.shape) 188 | for i in range(Data.shape[0]): 189 | D[i,i]=sum(Data[i,:]) 190 | return D 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | if __name__=="__main__": 203 | Data = DataLoader("office31", source="amazon", target="dslr") 204 | A=Data.Read() 205 | 206 | # f=open("Amazon.pkl","r") 207 | # Amazon=pickle.load(f) 208 | # Data=Amazon.get("Data") 209 | # Label=Amazon.get("Label") 210 | # f.close() 211 | # for i in range(100): 212 | # plt.imshow(Data[i,:,:,:]) 213 | # plt.show() 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | -------------------------------------------------------------------------------- /HoMM_mnist/Lenet.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | from tensorflow.contrib import slim 4 | 5 | 6 | class Lenet(object): 7 | def __init__(self, inputs, scope='lenet', training_flag=True, reuse=False): 8 | self.scope=scope 9 | self.inputs=inputs 10 | if inputs.get_shape()[3] == 3: 11 | self.inputs = tf.image.rgb_to_grayscale(self.inputs) 12 | self.training_flag=training_flag 13 | self.is_training=True 14 | self.reuse=reuse 15 | self.create() 16 | 17 | 18 | def create(self,is_training=False): 19 | 20 | with tf.variable_scope(self.scope, reuse=self.reuse): 21 | with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu): 22 | with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu, padding='VALID'): 23 | net=self.inputs 24 | net = slim.conv2d(net, 64, 5, scope='conv1') 25 | self.conv1=net 26 | net = slim.max_pool2d(net, 2, stride=2, scope='pool1') 27 | self.pool1 = net 28 | net = slim.conv2d(net,128, 5, scope='conv2') 29 | self.conv2= net 30 | net = slim.max_pool2d(net, 2, stride=2, scope='pool2') 31 | self.pool2= net 32 | net = tf.contrib.layers.flatten(net) 33 | net = slim.fully_connected(net, 1024, activation_fn=tf.nn.relu, scope='fc3') 34 | self.fc3= net 35 | net = slim.dropout(net,0.5, is_training=self.training_flag) 36 | ################################################################################ 37 | # tf.nn.tanh 38 | net = slim.fully_connected(net,90, activation_fn=tf.nn.tanh,scope='fc4') 39 | self.fc4 = net 40 | net = slim.fully_connected(net,10, activation_fn=None, scope='fc5') 41 | self.fc5 = net 42 | self.softmax_output=slim.softmax(net,scope='prediction') 43 | 44 | -------------------------------------------------------------------------------- /HoMM_mnist/Lenet2D.py: -------------------------------------------------------------------------------- 1 | 2 | import tensorflow as tf 3 | from tensorflow.contrib import slim 4 | 5 | 6 | class Lenet(object): 7 | def __init__(self,inputs, scope='lenet', training_flag=True, reuse=False): 8 | self.scope=scope 9 | self.inputs=inputs 10 | if inputs.get_shape()[3] == 3: 11 | self.inputs = tf.image.rgb_to_grayscale(self.inputs) 12 | self.training_flag=training_flag 13 | self.is_training=True 14 | self.reuse=reuse 15 | self.create() 16 | 17 | def create(self,is_training=False): 18 | 19 | with tf.variable_scope(self.scope, reuse=self.reuse): 20 | with slim.arg_scope([slim.fully_connected], activation_fn=tf.nn.relu): 21 | with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu, padding='VALID'): 22 | net=self.inputs 23 | net = slim.conv2d(net, 64, 5, scope='conv1') 24 | self.conv1=net 25 | net = slim.max_pool2d(net, 2, stride=2, scope='pool1') 26 | self.pool1 = net 27 | net = slim.conv2d(net,128, 5, scope='conv2') 28 | self.conv2= net 29 | net = slim.max_pool2d(net, 2, stride=2, scope='pool2') 30 | self.pool2= net 31 | net = tf.contrib.layers.flatten(net) 32 | net = slim.fully_connected(net, 1024, activation_fn=tf.nn.relu, scope='fc3') 33 | self.fc3= net 34 | net = slim.dropout(net,1.0, is_training=self.training_flag) 35 | net = slim.fully_connected(net,2, activation_fn=None,scope='fc4') 36 | self.fc4 = net 37 | net = slim.fully_connected(net,10, activation_fn=None, scope='fc5') 38 | self.fc5 = net 39 | self.softmax_output=slim.softmax(net,scope='prediction') 40 | 41 | -------------------------------------------------------------------------------- /HoMM_mnist/Sensitivity_plot.py: -------------------------------------------------------------------------------- 1 | from matplotlib import pyplot as plt 2 | import numpy as np 3 | 4 | plt.rcParams['font.size']=10 5 | plt.rcParams['axes.labelsize']=12 6 | plt.rcParams['axes.labelweight']='bold' 7 | plt.rcParams['axes.titlesize']=12 8 | plt.rcParams['xtick.labelsize']=12 9 | plt.rcParams['ytick.labelsize']=12 10 | plt.rcParams['legend.fontsize']=10 11 | plt.rcParams['figure.titlesize']=12 12 | 13 | 14 | def ParameterSentivity(): 15 | lambda1=[1,10,50,100,500,1000,10000,100000] 16 | acc_mnist_lambda1=[0.70,0.86,0.962,0.965,0.970,0.972,0.933,0.916] 17 | acc_amazon_lambda1=[0.825,0.832,0.846,0.849,0.853,0.851,0.837,0.762] 18 | 19 | plt.figure(figsize=(4,3)) 20 | plt.semilogx(lambda1,acc_mnist_lambda1,marker="s",c="b",lw=3,ms=8,label="SVHN-MNIST") 21 | plt.hlines(0.895,1,100000,color='b',linestyles='dashed') 22 | plt.semilogx(lambda1, acc_amazon_lambda1, marker="o", c="r", lw=3, ms=8, label="A-D") 23 | plt.hlines(0.787, 1, 100000, color='r', linestyles='dashed') 24 | plt.xlabel(r"$\lambda_{ssc}$") 25 | plt.ylabel("Accuracy") 26 | # plt.xticks([1,10,50,100,500,1000,10000,100000],[1,2,3,4,5,6,7,8]) 27 | plt.grid() 28 | plt.legend(loc="lower center") 29 | plt.show() 30 | 31 | lambda2=[0.00001,0.0001,0.001,0.003,0.01,0.03,0.1,0.3] 32 | acc_mnist_lambda2=[0.962,0.970,0.972,0.966,0.952,0.923,0.861,0.648] 33 | acc_amazon_lambda2=[0.812,0.834,0.845,0.852,0.854,0.848,0.842,0.788] 34 | plt.figure(figsize=(4, 3)) 35 | plt.semilogx(lambda2, acc_mnist_lambda2, marker="s", c="b", lw=3,ms=8,label="SVHN-MNIST") 36 | plt.semilogx(lambda2, acc_amazon_lambda2, marker="o", c="r", lw=3, ms=8, label="A-D") 37 | plt.hlines(0.942, 0, 0.3, color='b', linestyles='dashed') 38 | plt.hlines(0.763, 0, 0.3, color='r', linestyles='dashed') 39 | plt.xlabel(r"$\lambda_{intra}$") 40 | plt.ylabel("Accuracy") 41 | plt.grid() 42 | plt.legend(loc="lower center") 43 | plt.show() 44 | 45 | 46 | 47 | lambda3=[0.000001,0.00001,0.0001,0.0003,0.0005,0.001,0.003,0.01] 48 | acc_mnist_lambda3=[0.945,0.946,0.947,0.959,0.964,0.973,0.955,0.82] 49 | acc_amazon_lambda3=[0.804,0.818,0.844,0.853,0.864,0.829,0.812,0.772] 50 | plt.figure(figsize=(4, 3)) 51 | plt.semilogx(lambda3, acc_mnist_lambda3, marker="s", c="b", lw=3,ms=8,label="SVHN-MNIST") 52 | plt.hlines(0.942, 0, 0.01, color='b', linestyles='dashed') 53 | plt.semilogx(lambda3, acc_amazon_lambda3, marker="o", c="r", lw=3,ms=8,label="A-D") 54 | plt.hlines(0.763, 0, 0.01, color='r', linestyles='dashed') 55 | plt.xlabel(r"$\lambda_{inter}$") 56 | plt.ylabel("Accuracy") 57 | plt.grid() 58 | plt.legend(loc="lower center") 59 | plt.show() 60 | 61 | 62 | 63 | def ParameterSentivity_HoMM(): 64 | lambda_d=[1,10,100,1000,10000,100000,1000000,10000000] 65 | acc_mnist_lambda_d=[0.69,0.86,0.93,0.957,0.972,0.958,0.933,0.916] 66 | acc_amazon_lambda_d=[0.776,0.832,0.88,0.90,0.861,0.821,0.73,0.66] 67 | plt.figure(figsize=(4,3)) 68 | plt.semilogx(lambda_d,acc_mnist_lambda_d,marker="s",c="b",lw=3,ms=8,label="SVHN-MNIST") 69 | plt.semilogx(lambda_d, acc_amazon_lambda_d, marker="o", c="r", lw=3, ms=8, label="A-W") 70 | # plt.hlines(0.895, 1, 10000000, color='b', linestyles='dashed') 71 | # plt.hlines(0.793, 1, 10000000, color='r', linestyles='dashed') 72 | plt.xlabel(r"$\lambda_{d}$") 73 | plt.ylabel("Accuracy") 74 | # plt.xticks([1,10,50,100,500,1000,10000,100000],[1,2,3,4,5,6,7,8]) 75 | plt.grid() 76 | plt.legend(loc="lower center") 77 | plt.show() 78 | 79 | 80 | 81 | lambda_dc=[0.00001,0.0001,0.001,0.01,0.03,0.1,0.3,1.0] 82 | acc_mnist_lambda_dc=[0.972,0.971,0.974,0.975,0.978,0.988,0.981,0.912] 83 | acc_amazon_lambda_dc=[0.904,0.904,0.905,0.904,0.909,0.917,0.902,0.858] 84 | plt.figure(figsize=(4, 3)) 85 | plt.semilogx(lambda_dc, acc_mnist_lambda_dc, marker="s", c="b", lw=3,ms=8,label="SVHN-MNIST") 86 | plt.semilogx(lambda_dc, acc_amazon_lambda_dc, marker="o", c="r", lw=3, ms=8, label="A-W") 87 | plt.hlines(0.972, 0.00001, 1.0, color='b', linestyles='dashed') 88 | plt.hlines(0.905, 0.00001, 1.0, color='r', linestyles='dashed') 89 | plt.xlabel(r"$\lambda_{dc}$") 90 | plt.ylabel("Accuracy") 91 | plt.grid() 92 | plt.legend(loc="lower center") 93 | plt.show() 94 | 95 | 96 | N = [10,100,1000, 3000, 10000, 30000, 100000, 300000] 97 | acc_mnist_lambda_N = [0.872,0.943, 0.959, 0.962, 0.968, 0.971, 0.970, 0.972] 98 | acc_amazon_lambda_N = [0.74, 0.842, 0.878, 0.883, 0.889, 0.895, 0.895, 0.896] 99 | plt.figure(figsize=(4, 3)) 100 | plt.semilogx(N, acc_mnist_lambda_N, marker="s", c="b", lw=3, ms=8, label="SVHN-MNIST") 101 | plt.semilogx(N, acc_amazon_lambda_N, marker="o", c="r", lw=3, ms=8, label="A-W") 102 | # plt.hlines(0.895, 100, 500000, color='b', linestyles='dashed') 103 | # plt.hlines(0.793, 100, 500000, color='r', linestyles='dashed') 104 | plt.xlabel(r"$\lambda_{dc}$") 105 | plt.ylabel("Accuracy") 106 | plt.grid() 107 | plt.legend(loc="lower center") 108 | plt.show() 109 | 110 | delta = [0.5,0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95] 111 | acc_mnist_lambda_delta = [0.931,0.952, 0.966, 0.970, 0.982, 0.987, 0.986, 0.988, 0.983] 112 | acc_amazon_lambda_delta = [0.88,0.912, 0.914, 0.917, 0.914, 0.915, 0.915, 0.908, 0.907] 113 | plt.figure(figsize=(4, 3)) 114 | plt.plot(delta, acc_mnist_lambda_delta, marker="s", c="b", lw=3, ms=8, label="SVHN-MNIST") 115 | plt.plot(delta, acc_amazon_lambda_delta, marker="o", c="r", lw=3, ms=8, label="A-W") 116 | plt.hlines(0.972, 0.5, 0.95, color='b', linestyles='dashed') 117 | plt.hlines(0.905, 0.5, 0.95, color='r', linestyles='dashed') 118 | plt.xlabel(r"$\lambda_{dc}$") 119 | plt.ylabel("Accuracy") 120 | plt.grid() 121 | plt.legend(loc="lower center") 122 | plt.show() 123 | 124 | 125 | 126 | 127 | if __name__=="__main__": 128 | # ParameterSentivity() 129 | ParameterSentivity_HoMM() 130 | -------------------------------------------------------------------------------- /HoMM_mnist/TrainLenet.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pickle 3 | from Lenet import * 4 | from Utils import * 5 | import scipy.io 6 | import numpy as np 7 | from tensorflow.contrib import slim 8 | from center_loss import * 9 | os.environ['CUDA_VISIBLE_DEVICES']='0' 10 | 11 | 12 | 13 | class Train(): 14 | def __init__(self,class_num,batch_size,iters,learning_rate,keep_prob,param): 15 | self.ClassNum=class_num 16 | self.BatchSize=batch_size 17 | self.Iters=iters 18 | self.LearningRate=learning_rate 19 | self.KeepProb=keep_prob 20 | self.discriminative_loss_param=param[0] 21 | self.domain_loss_param=param[1] 22 | self.adver_loss_param=param[2] 23 | self.ring_loss_param=param[3] 24 | self.target_loss_param =tf.get_variable(name="target_loss_param",shape=None,dtype=tf.float32,initializer=tf.constant(0.0),trainable=False) 25 | self.ring_loss_param=tf.get_variable(name="ring_loss_param",shape=None,dtype=tf.float32,initializer=tf.constant(0.0),trainable=False) 26 | self.ring_norm = tf.get_variable(name="ring_norm", shape=None, dtype=tf.float32, initializer=tf.constant(10.0),trainable=False) 27 | self.logits_threshold=tf.get_variable(name="logits_threshold",shape=None,dtype=tf.float32,initializer=tf.constant(0.9),trainable=False) 28 | 29 | 30 | self.SourceData,self.SourceLabel=load_svhn('svhn') 31 | self.TargetData, self.TargetLabel=load_mnist('mnist') 32 | self.TestData, self.TestLabel = load_mnist('mnist',split='test') 33 | # self.EdgeWeights=Label2EdgeWeights(self.SourceLabel) 34 | # self.EdgeWeights=zeros((self.SourceLabel.shape[0],self.SourceLabel.shape[0])) 35 | ################################################################################### 36 | 37 | 38 | 39 | 40 | ####################################################################################### 41 | self.source_image = tf.placeholder(tf.float32, shape=[self.BatchSize, 32,32,3],name="source_image") 42 | self.source_label = tf.placeholder(tf.float32, shape=[self.BatchSize, self.ClassNum],name="source_label") 43 | self.target_image = tf.placeholder(tf.float32, shape=[self.BatchSize, 32, 32,1],name="target_image") 44 | self.Training_flag = tf.placeholder(tf.bool, shape=None,name="Training_flag") 45 | self.W = tf.placeholder(tf.float32, shape=[self.BatchSize, self.BatchSize]) 46 | 47 | 48 | 49 | def TrainNet(self): 50 | self.source_model=Lenet(inputs=self.source_image, training_flag=self.Training_flag, reuse=False) 51 | self.target_model=Lenet(inputs=self.target_image, training_flag=self.Training_flag, reuse=True) 52 | self.CalLoss() 53 | varall=tf.trainable_variables() 54 | with tf.control_dependencies([self.centers_update_op]): 55 | self.solver = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss) 56 | self.source_prediction = tf.argmax(self.source_model.softmax_output, 1) 57 | self.target_prediction = tf.argmax(self.target_model.softmax_output, 1) 58 | with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess: 59 | init = tf.global_variables_initializer() 60 | sess.run(init) 61 | self.SourceLabel=sess.run(tf.one_hot(self.SourceLabel,10)) 62 | self.TestLabel=sess.run(tf.one_hot(self.TestLabel,10)) 63 | true_num = 0.0 64 | for step in range(self.Iters): 65 | i= step % int(self.SourceData.shape[0]/self.BatchSize) 66 | j= step % int(self.TargetData.shape[0]/self.BatchSize) 67 | source_batch_x = self.SourceData[i * self.BatchSize: (i + 1) * self.BatchSize] 68 | source_batch_y = self.SourceLabel[i * self.BatchSize: (i + 1) * self.BatchSize] 69 | target_batch_x = self.TargetData[j * self.BatchSize: (j + 1) * self.BatchSize] 70 | # W = self.EdgeWeights[i * self.BatchSize: (i + 1) * self.BatchSize,i * self.BatchSize: (i + 1) * self.BatchSize] 71 | total_loss, source_loss,domain_loss, clustering_loss, source_prediction,_= sess.run( 72 | fetches=[self.loss, self.source_loss, self.domain_loss, self.Clustering_loss, self.source_prediction, self.solver], 73 | feed_dict={self.source_image: source_batch_x, self.source_label: source_batch_y,self.target_image: target_batch_x, self.Training_flag: True}) 74 | true_label = argmax(source_batch_y, 1) 75 | true_num = true_num + sum(true_label == source_prediction) 76 | 77 | 78 | if step % 200 ==0: 79 | print "Iters-{} ### TotalLoss={} ### SourceLoss={} ###DomainLoss={} ### ClusteringLoss={}".format( 80 | step, total_loss, source_loss, domain_loss, clustering_loss) 81 | train_accuracy = true_num / (200*self.BatchSize) 82 | true_num = 0.0 83 | print " ########## train_accuracy={} ###########".format(train_accuracy) 84 | self.Test(sess) 85 | 86 | 87 | if step==30000: 88 | sess.run(tf.assign(self.target_loss_param,0.1)) 89 | sess.run(tf.assign(self.logits_threshold,0.92)) 90 | 91 | 92 | # if step!=0 and step % 1000 == 0: 93 | # self.SourceData, self.SourceLabel= shuffle0(self.SourceData, self.SourceLabel) 94 | 95 | # if step!=0 and step % 5000 == 0: 96 | # self.SourceData, self.SourceLabel, self.EdgeWeights= shuffle(self.SourceData, self.SourceLabel, self.EdgeWeights) 97 | 98 | 99 | 100 | 101 | def CalLoss(self): 102 | self.source_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=self.source_label, logits=self.source_model.fc5) 103 | self.source_loss = tf.reduce_mean(self.source_cross_entropy) 104 | self.CalDomainLoss(method="HoMM3") 105 | self.DisClustering() 106 | self.CalTargetLoss("Entropy") 107 | # self.CalAdver() 108 | self.loss=self.source_loss+self.domain_loss_param*self.domain_loss+self.target_loss_param*self.Clustering_loss 109 | 110 | 111 | 112 | def CalDomainLoss(self,method): 113 | if method=="MMD": 114 | Xs=self.source_model.fc4 115 | Xt=self.target_model.fc4 116 | diff=tf.reduce_mean(Xs, 0, keep_dims=False) - tf.reduce_mean(Xt, 0, keep_dims=False) 117 | self.domain_loss=tf.reduce_sum(tf.multiply(diff,diff)) 118 | 119 | 120 | elif method=="KMMD": 121 | Xs=self.source_model.fc4 122 | Xt=self.target_model.fc4 123 | self.domain_loss=tf.maximum(0.0001,KMMD(Xs,Xt)) 124 | 125 | 126 | elif method=="CORAL": 127 | Xs = self.source_model.fc4 128 | Xt = self.target_model.fc4 129 | self.domain_loss=self.coral_loss(Xs,Xt) 130 | 131 | 132 | elif method =='LCORAL': 133 | Xs = self.source_model.fc4 134 | Xt = self.target_model.fc4 135 | self.domain_loss=self.log_coral_loss(Xs,Xt) 136 | 137 | 138 | elif method=='SqrtCORAL': 139 | Xs = self.source_model.fc4 140 | Xt = self.target_model.fc4 141 | self.domain_loss = self.sqrt_coral_loss(Xs, Xt) 142 | 143 | elif method=='HoMM3': 144 | Xs = self.source_model.fc4 145 | Xt = self.target_model.fc4 146 | self.domain_loss = self.HoMM3_loss(Xs, Xt) 147 | 148 | 149 | elif method=='HoMM4': 150 | Xs = self.source_model.fc4 151 | Xt = self.target_model.fc4 152 | self.domain_loss = self.HoMM4(Xs, Xt) 153 | 154 | elif method=="HoMM": 155 | Xs = self.source_model.fc4 156 | Xt = self.target_model.fc4 157 | self.domain_loss = self.HoMM(Xs, Xt,order=3,num=300000) 158 | 159 | 160 | elif method=="KHoMM": 161 | Xs = self.source_model.fc4 162 | Xt = self.target_model.fc4 163 | self.domain_loss = self.KHoMM(Xs, Xt, order=3, num=10000) 164 | 165 | 166 | 167 | 168 | elif method =="mmatch": 169 | Xs = self.source_model.fc4 170 | Xt = self.target_model.fc4 171 | self.domain_loss=mmatch(Xs,Xt,5) 172 | 173 | elif method == "CorNorm": 174 | gamma=0.001 # 0.001 #0.0003 mnist->mnist-m 175 | Xs = self.source_model.fc4 176 | Xt = self.target_model.fc4 177 | self.CorNorm(Xs,Xt,gamma) 178 | 179 | 180 | 181 | def CalTargetLoss(self,method): 182 | if method=="Entropy": 183 | source_softmax=self.source_model.softmax_output 184 | target_softmax=self.target_model.softmax_output 185 | target_softmax_mean=tf.reduce_mean(target_softmax,axis=0) 186 | index=tf.where(tf.equal(tf.greater(target_softmax,self.logits_threshold),True))[:,0] 187 | target_softmax=tf.gather(target_softmax,index) 188 | self.target_loss_1=tf.reduce_mean(target_softmax_mean*tf.log(tf.clip_by_value(target_softmax_mean,0.0001,1))) 189 | self.target_loss_2=-tf.reduce_mean(tf.reduce_sum(target_softmax * tf.log(tf.clip_by_value(target_softmax,0.0001,1)), axis=1)) 190 | self.target_loss=0.0*self.target_loss_1+self.target_loss_2 191 | 192 | 193 | 194 | 195 | def CalDiscriminativeLoss(self,method): 196 | if method=="InstanceBased": 197 | Xs = self.source_model.fc4 198 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 199 | self.F0 = tf.transpose(norm(tf.expand_dims(Xs, 2) - tf.transpose(Xs))) 200 | margin0 = 0 201 | margin1 = 100 202 | F0=tf.pow(tf.maximum(0.0, self.F0-margin0),2) 203 | F1=tf.pow(tf.maximum(0.0, margin1-self.F0),2) 204 | self.intra_loss=tf.reduce_mean(tf.multiply(F0, self.W)) 205 | self.inter_loss=tf.reduce_mean(tf.multiply(F1, 1.0-self.W)) 206 | self.discriminative_loss = (self.intra_loss+self.inter_loss) / (self.BatchSize * self.BatchSize) 207 | 208 | 209 | 210 | elif method=="CenterBased": 211 | Xs=self.source_model.fc4 212 | labels=tf.argmax(self.source_label,1) 213 | self.inter_loss, self.intra_loss, self.centers_update_op = get_center_loss(Xs, labels, 0.5, 10) 214 | self.intra_loss=self.intra_loss/(self.ClassNum*self.BatchSize+self.ClassNum*self.ClassNum) 215 | self.inter_loss=self.inter_loss/(self.ClassNum*self.BatchSize+self.ClassNum*self.ClassNum) 216 | self.discriminative_loss = self.intra_loss+ self.inter_loss 217 | 218 | 219 | 220 | def DisClustering(self): 221 | target_feature, target_logits = SelectTargetSamples(self.target_model.fc4, self.target_model.fc5, logits_threshold=self.logits_threshold) 222 | target_pseudo_label = tf.argmax(target_logits, axis=1) 223 | with tf.variable_scope('target'): 224 | self.inter_loss, self.intra_loss, self.centers_update_op = get_center_loss(target_feature,target_pseudo_label,0.5,10) 225 | self.Clustering_loss=self.intra_loss/(self.ClassNum*self.BatchSize) 226 | 227 | 228 | 229 | def Cal_RingLoss(self): 230 | Xs=self.source_model.fc4 231 | Xs_norm=tf.norm(Xs,ord="euclidean",axis=1) 232 | Xt = self.target_model.fc4 233 | Xt_norm = tf.norm(Xt, ord="euclidean", axis=1) 234 | self.ring_loss=tf.reduce_mean(tf.norm(Xs_norm-self.ring_norm))+tf.reduce_mean(tf.norm(Xt_norm-self.ring_norm)) 235 | 236 | 237 | 238 | 239 | 240 | def coral_loss(self, h_src, h_trg, gamma=1e-3): 241 | # regularized covariances (D-Coral is not regularized actually..) 242 | # First: subtract the mean from the data matrix 243 | batch_size = self.BatchSize 244 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 245 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 246 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src, 247 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 248 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, 249 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 250 | # cov_source=tf.linalg.cholesky(cov_source) 251 | # cov_target=tf.linalg.cholesky(cov_target) 252 | return tf.reduce_mean(tf.square(tf.subtract(cov_source, cov_target))) 253 | 254 | 255 | 256 | 257 | 258 | def log_coral_loss(self, h_src, h_trg, gamma=1e-3): 259 | # regularized covariances result in inf or nan 260 | # First: subtract the mean from the data matrix 261 | batch_size = float(self.BatchSize) 262 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 263 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 264 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src,transpose_a=True) + gamma * tf.eye(64) 265 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg,transpose_a=True) + gamma * tf.eye(64) 266 | # eigen decomposition 267 | eig_source = tf.self_adjoint_eig(cov_source) 268 | eig_target = tf.self_adjoint_eig(cov_target) 269 | log_cov_source = tf.matmul(eig_source[1],tf.matmul(tf.diag(tf.log(eig_source[0])), eig_source[1], transpose_b=True)) 270 | log_cov_target = tf.matmul(eig_target[1],tf.matmul(tf.diag(tf.log(eig_target[0])), eig_target[1], transpose_b=True)) 271 | return tf.reduce_mean(tf.square(tf.subtract(log_cov_source, log_cov_target))) 272 | 273 | 274 | def sqrt_coral_loss(self, xs, xt, gamma=1e-3): 275 | # regularized covariances result in inf or nan 276 | # First: subtract the mean from the data matrix 277 | batch_size = float(self.BatchSize) 278 | xs = xs - tf.reduce_mean(xs, axis=0) 279 | xt = xt - tf.reduce_mean(xt, axis=0) 280 | cov_source = (1. / (batch_size - 1)) * tf.matmul(xs, xs,transpose_a=True) #+ gamma * tf.eye(64) 281 | cov_target = (1. / (batch_size - 1)) * tf.matmul(xt, xt,transpose_a=True) #+ gamma * tf.eye(64) 282 | sqrt_cov_source=self.CovSqrt(cov_source) 283 | sqrt_cov_target=self.CovSqrt(cov_target) 284 | return tf.reduce_mean(tf.square(tf.subtract(sqrt_cov_source, sqrt_cov_target))) 285 | 286 | 287 | 288 | def CovSqrt(self,Cov): 289 | Cov=Cov/tf.trace(Cov) 290 | Y0=Cov 291 | Z0=tf.eye(int(Cov.shape[0])) 292 | I=tf.eye(int(Cov.shape[0])) 293 | Y1=0.5*tf.matmul(Y0,3*I-tf.matmul(Z0,Y0)) 294 | Z1=0.5*tf.matmul(3*I-tf.matmul(Z0,Y0),Z0) 295 | Y2 = 0.5 * tf.matmul(Y1, 3 * I - tf.matmul(Z1, Y1)) 296 | Z2 = 0.5 * tf.matmul(3 * I - tf.matmul(Z1, Y1), Z1) 297 | Y3 = 0.5 * tf.matmul(Y2, 3 * I - tf.matmul(Z2, Y2)) 298 | Z3 = 0.5 * tf.matmul(3 * I - tf.matmul(Z2, Y2), Z2) 299 | Y4 = 0.5 * tf.matmul(Y3, 3 * I - tf.matmul(Z3, Y3)) 300 | Z4 = 0.5 * tf.matmul(3 * I - tf.matmul(Z3, Y3), Z3) 301 | Y5 = 0.5 * tf.matmul(Y4, 3 * I - tf.matmul(Z4, Y4)) 302 | Z5 = 0.5 * tf.matmul(3 * I - tf.matmul(Z4, Y4), Z4) 303 | Y6 = 0.5 * tf.matmul(Y5, 3 * I - tf.matmul(Z5, Y5)) 304 | Y6 = tf.multiply(tf.sign(Y6), tf.sqrt(tf.abs(Y6)+1e-12)) 305 | Y6=Y6/tf.norm(Y6) 306 | return Y6 307 | 308 | 309 | def HoMM3_loss(self, xs, xt): 310 | xs = xs - tf.reduce_mean(xs, axis=0) 311 | # xs=self.decoupling(xs) 312 | xt = xt - tf.reduce_mean(xt, axis=0) 313 | # xt=self.decoupling(xt) 314 | xs=tf.expand_dims(xs,axis=-1) 315 | xs = tf.expand_dims(xs, axis=-1) 316 | xt = tf.expand_dims(xt, axis=-1) 317 | xt = tf.expand_dims(xt, axis=-1) 318 | xs_1=tf.transpose(xs,[0,2,1,3]) 319 | xs_2 = tf.transpose(xs, [0, 2, 3, 1]) 320 | xt_1 = tf.transpose(xt, [0, 2, 1, 3]) 321 | xt_2 = tf.transpose(xt, [0, 2, 3, 1]) 322 | HR_Xs=xs*xs_1*xs_2 323 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 324 | HR_Xt = xt * xt_1 * xt_2 325 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 326 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 327 | 328 | 329 | def decoupling(self,X): 330 | dim=X.shape[0] 331 | Ones=0.0001*tf.ones(shape=[dim,1]) 332 | Y=tf.concat([Ones,X],axis=1) 333 | return Y 334 | 335 | 336 | 337 | def HoMM4(self,xs,xt): 338 | ind=tf.range(tf.cast(xs.shape[1],tf.int32)) 339 | ind=tf.random_shuffle(ind) 340 | xs=tf.transpose(xs,[1,0]) 341 | xs=tf.gather(xs,ind) 342 | xs = tf.transpose(xs, [1, 0]) 343 | xt = tf.transpose(xt, [1, 0]) 344 | xt = tf.gather(xt, ind) 345 | xt = tf.transpose(xt, [1, 0]) 346 | return self.HoMM4_loss(xs[:,:30],xt[:,:30])+self.HoMM4_loss(xs[:,30:60],xt[:,30:60])+self.HoMM4_loss(xs[:,60:90],xt[:,60:90]) 347 | 348 | 349 | 350 | def HoMM4_loss(self, xs, xt): 351 | xs = xs - tf.reduce_mean(xs, axis=0) 352 | xt = xt - tf.reduce_mean(xt, axis=0) 353 | xs = tf.expand_dims(xs,axis=-1) 354 | xs = tf.expand_dims(xs, axis=-1) 355 | xs = tf.expand_dims(xs, axis=-1) 356 | xt = tf.expand_dims(xt, axis=-1) 357 | xt = tf.expand_dims(xt, axis=-1) 358 | xt = tf.expand_dims(xt, axis=-1) 359 | xs_1 = tf.transpose(xs,[0,2,1,3,4]) 360 | xs_2 = tf.transpose(xs, [0, 2, 3, 1,4]) 361 | xs_3 = tf.transpose(xs, [0, 2, 3, 4, 1]) 362 | xt_1 = tf.transpose(xt, [0, 2, 1, 3,4]) 363 | xt_2 = tf.transpose(xt, [0, 2, 3, 1,4]) 364 | xt_3 = tf.transpose(xt, [0, 2, 3, 4, 1]) 365 | HR_Xs=xs*xs_1*xs_2*xs_3 366 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 367 | HR_Xt = xt * xt_1 * xt_2*xt_3 368 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 369 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 370 | 371 | 372 | 373 | ## high-order moment matching 374 | def HoMM(self,xs, xt, order=3, num=300000): 375 | xs = xs - tf.reduce_mean(xs, axis=0) 376 | xt = xt - tf.reduce_mean(xt, axis=0) 377 | dim = tf.cast(xs.shape[1], tf.int32) 378 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32) 379 | index = index[:, :order] 380 | xs = tf.transpose(xs) 381 | xs = tf.gather(xs, index) ##dim=[num,order,batchsize] 382 | xt = tf.transpose(xt) 383 | xt = tf.gather(xt, index) 384 | HO_Xs = tf.reduce_prod(xs, axis=1) 385 | HO_Xs = tf.reduce_mean(HO_Xs, axis=1) 386 | HO_Xt = tf.reduce_prod(xt, axis=1) 387 | HO_Xt = tf.reduce_mean(HO_Xt, axis=1) 388 | return tf.reduce_mean(tf.square(tf.subtract(HO_Xs, HO_Xt))) 389 | 390 | 391 | 392 | 393 | #### 394 | def KHoMM(self, xs, xt, order=3, num=10000): 395 | xs = xs - tf.reduce_mean(xs, axis=0) 396 | xt = xt - tf.reduce_mean(xt, axis=0) 397 | dim = tf.cast(xs.shape[1], tf.int32) 398 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32, seed=0) 399 | index = index[:, :order] 400 | xs = tf.transpose(xs) 401 | xs = tf.gather(xs, index) ##dim=[num,order,batchsize] 402 | xt = tf.transpose(xt) 403 | xt = tf.gather(xt, index) 404 | Ho_Xs = tf.transpose(tf.reduce_prod(xs, axis=1)) 405 | Ho_Xt = tf.transpose(tf.reduce_prod(xt, axis=1)) 406 | KHoMM = KernelHoMM(Ho_Xs, Ho_Xt, sigma=0.00001) 407 | return KHoMM 408 | 409 | 410 | 411 | 412 | def CorNorm(self,Xs,Xt,gamma): 413 | Xs = tf.transpose(Xs - tf.reduce_mean(Xs, axis=0)) 414 | Xt = tf.transpose(Xt - tf.reduce_mean(Xt, axis=0)) 415 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 416 | self.Xs_Cor_norm = tf.transpose(norm(tf.expand_dims(Xs, 2) - tf.transpose(Xs))) 417 | self.Xt_Cor_norm = tf.transpose(norm(tf.expand_dims(Xt, 2) - tf.transpose(Xt))) 418 | self.W1 = tf.exp(-gamma * self.Xs_Cor_norm) 419 | self.W2 = tf.exp(-gamma * self.Xt_Cor_norm) 420 | # self.W1=self.CovSqrt(self.W1) 421 | # self.W2 = self.CovSqrt(self.W2) 422 | # self.W1=tf.multiply(tf.sign(self.W1),tf.sqrt(tf.abs(self.W1)+1e-12)) 423 | # self.W2 = tf.multiply(tf.sign(self.W2), tf.sqrt(tf.abs(self.W2)+1e-12)) 424 | # self.W1=self.W1/(tf.norm(self.W1)) 425 | # self.W2=self.W2/(tf.norm(self.W2)) 426 | self.domain_loss = tf.reduce_mean(tf.square(tf.subtract(self.W1, self.W2))) 427 | 428 | 429 | 430 | 431 | 432 | def Test(self,sess): 433 | true_num=0.0 434 | num = int(self.TestData.shape[0] / self.BatchSize) 435 | total_num=num*self.BatchSize 436 | for i in range (num): 437 | k = i % int(self.TestData.shape[0] / self.BatchSize) 438 | target_batch_x = self.TestData[k * self.BatchSize: (k + 1) * self.BatchSize] 439 | target_batch_y= self.TestLabel[k * self.BatchSize: (k + 1) * self.BatchSize] 440 | prediction=sess.run(fetches=self.target_prediction, feed_dict={self.target_image:target_batch_x, self.Training_flag: False}) 441 | true_label = argmax(target_batch_y, 1) 442 | true_num+=sum(true_label==prediction) 443 | accuracy=true_num / total_num 444 | print "########### Test Accuracy={} ##########".format(accuracy) 445 | 446 | 447 | 448 | 449 | 450 | def main(): 451 | discriminative_loss_param = 0.0 ##0.03 for InstanceBased method, 0.01 for CenterBased method 452 | domain_loss_param = 10000 # 8.0 for Coral; 1000 for CorNorm, SqrtCORAL, 10000(3000) for TensorCORAL 453 | adver_loss_param=0.0 454 | ring_loss_param=0.0 ## svhn-> mnist 0.0005 ## mnist-> mnistm 0.0003 ## USPS-> mnist 0.0005 ## syn -> mnist 0.0005 ## 0.00001 455 | param=[discriminative_loss_param, domain_loss_param,adver_loss_param,ring_loss_param] 456 | Runer=Train(class_num=10,batch_size=128,iters=200000,learning_rate=0.0001,keep_prob=1,param=param) 457 | Runer.TrainNet() 458 | 459 | 460 | 461 | 462 | def load_mnist(image_dir, split='train'): 463 | print ('Loading MNIST dataset.') 464 | 465 | image_file = 'train.pkl' if split == 'train' else 'test.pkl' 466 | image_dir = os.path.join(image_dir, image_file) 467 | with open(image_dir, 'rb') as f: 468 | mnist = pickle.load(f) 469 | images = mnist['X'] / 127.5 - 1 470 | labels = mnist['y'] 471 | labels=np.squeeze(labels).astype(int) 472 | return images,labels 473 | 474 | 475 | def load_svhn(image_dir, split='train'): 476 | print ('Loading SVHN dataset.') 477 | 478 | image_file = 'train_32x32.mat' if split == 'train' else 'test_32x32.mat' 479 | 480 | image_dir = os.path.join(image_dir, image_file) 481 | svhn = scipy.io.loadmat(image_dir) 482 | images = np.transpose(svhn['X'], [3, 0, 1, 2]) / 127.5 - 1 483 | # ~ images= resize_images(images) 484 | labels = svhn['y'].reshape(-1) 485 | labels[np.where(labels == 10)] = 0 486 | return images, labels 487 | 488 | 489 | def load_USPS(image_dir,split='train'): 490 | print('Loading USPS dataset.') 491 | image_file='USPS_train.pkl' if split=='train' else 'USPS_test.pkl' 492 | image_dir=os.path.join(image_dir,image_file) 493 | with open(image_dir, 'rb') as f: 494 | usps = pickle.load(f) 495 | images = usps['data'] 496 | images=np.reshape(images,[-1,32,32,1]) 497 | labels = usps['label'] 498 | labels=np.squeeze(labels).astype(int) 499 | return images,labels 500 | 501 | 502 | 503 | def load_syn(image_dir,split='train'): 504 | print('load syn dataset') 505 | image_file='synth_train_32x32.mat' if split=='train' else 'synth_test_32x32.mat' 506 | image_dir=os.path.join(image_dir,image_file) 507 | syn = scipy.io.loadmat(image_dir) 508 | images = np.transpose(syn['X'], [3, 0, 1, 2]) / 127.5 - 1 509 | labels = syn['y'].reshape(-1) 510 | return images,labels 511 | 512 | 513 | def load_mnistm(image_dir,split='train'): 514 | print('Loading mnistm dataset.') 515 | image_file='mnistm_train.pkl' if split=='train' else 'mnistm_test.pkl' 516 | image_dir=os.path.join(image_dir,image_file) 517 | with open(image_dir, 'rb') as f: 518 | mnistm = pickle.load(f) 519 | images = mnistm['data'] 520 | 521 | labels = mnistm['label'] 522 | labels=np.squeeze(labels).astype(int) 523 | return images,labels 524 | 525 | 526 | if __name__=="__main__": 527 | main() -------------------------------------------------------------------------------- /HoMM_mnist/TrainLenet3.py: -------------------------------------------------------------------------------- 1 | from Lenet import * 2 | from DataLoader import * 3 | from Utils import * 4 | import scipy.io 5 | import numpy as np 6 | from tensorflow.contrib import slim 7 | import matplotlib.pyplot as plt 8 | import matplotlib as mpl 9 | from sklearn.manifold import TSNE 10 | os.environ['CUDA_VISIBLE_DEVICES']='0' 11 | 12 | 13 | 14 | class Train(): 15 | def __init__(self,class_num,batch_size,iters,learning_rate,keep_prob,param): 16 | self.ClassNum=class_num 17 | 18 | self.BatchSize=batch_size 19 | self.Iters=iters 20 | self.LearningRate=learning_rate 21 | self.KeepProb=keep_prob 22 | self.target_loss_param=param[0] 23 | self.domain_loss_param=param[1] 24 | self.adver_loss_param=param[2] 25 | 26 | self.SourceData,self.SourceLabel=load_syn('syn') 27 | self.SourceData=self.SourceData[0:80000] 28 | self.SourceLabel=self.SourceLabel[0:80000] 29 | 30 | 31 | self.TargetData, self.TargetLabel=load_svhn('svhn') 32 | self.TestData, self.TestLabel = load_svhn('svhn',split='test') 33 | 34 | self.EdgeWeights=Label2EdgeWeights(self.SourceLabel) 35 | ####################################################################################### 36 | self.source_image = tf.placeholder(tf.float32, shape=[None, 32,32,3],name="source_image") 37 | self.source_label = tf.placeholder(tf.float32, shape=[None, self.ClassNum],name="source_label") 38 | 39 | self.target_image = tf.placeholder(tf.float32, shape=[None, 32, 32,3],name="target_image") 40 | self.Training_flag = tf.placeholder(tf.bool, shape=None,name="Training_flag") 41 | self.W = tf.placeholder(tf.float32, shape=[self.BatchSize, self.BatchSize]) 42 | 43 | 44 | def TrainNet(self): 45 | self.source_model=Lenet(inputs=self.source_image,training_flag=self.Training_flag, reuse=False) 46 | self.target_model=Lenet(inputs=self.target_image, training_flag=self.Training_flag,reuse=True) 47 | self.CalLoss() 48 | varall=tf.trainable_variables() 49 | 50 | self.solver = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss) 51 | self.source_prediction = tf.argmax(self.source_model.softmax_output, 1) 52 | self.target_prediction = tf.argmax(self.target_model.softmax_output, 1) 53 | 54 | with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess: 55 | # self.solver = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss) 56 | init = tf.global_variables_initializer() 57 | sess.run(init) 58 | self.SourceLabel=sess.run(tf.one_hot(self.SourceLabel,10)) 59 | self.TestLabel=sess.run(tf.one_hot(self.TestLabel,10)) 60 | # self.source_model.weights_initial(sess) 61 | # self.target_model.weights_initial(sess) 62 | true_num = 0.0 63 | for step in range(self.Iters): 64 | 65 | i= step % int(self.SourceData.shape[0]/self.BatchSize) 66 | j= step % int(self.TargetData.shape[0]/self.BatchSize) 67 | source_batch_x = self.SourceData[i * self.BatchSize: (i + 1) * self.BatchSize] 68 | source_batch_y = self.SourceLabel[i * self.BatchSize: (i + 1) * self.BatchSize] 69 | target_batch_x = self.TargetData[j * self.BatchSize: (j + 1) * self.BatchSize] 70 | W = self.EdgeWeights[i * self.BatchSize: (i + 1) * self.BatchSize,i * self.BatchSize: (i + 1) * self.BatchSize] 71 | total_loss, source_loss,domain_loss,target_loss,source_prediction,_= sess.run( 72 | fetches=[self.loss, self.source_loss, self.domain_loss,self.target_loss,self.source_prediction, self.solver], 73 | feed_dict={self.source_image: source_batch_x, self.source_label: source_batch_y,self.target_image: target_batch_x, self.Training_flag: True,self.W: W}) 74 | 75 | true_label = argmax(source_batch_y, 1) 76 | true_num = true_num + sum(true_label == source_prediction) 77 | 78 | # if step % 100==0: 79 | # self.SourceData, self.SourceLabel = shuffle(self.SourceData, self.SourceLabel) 80 | if step % 200 ==0: 81 | print "Iters-{} ### TotalLoss={} ### SourceLoss={} ###DomainLoss={} ###TargetLoss={}".format(step, total_loss, source_loss,domain_loss,target_loss) 82 | train_accuracy = true_num / (200*self.BatchSize) 83 | true_num = 0.0 84 | print " ########## train_accuracy={} ###########".format(train_accuracy) 85 | self.Test(sess) 86 | 87 | if step % 2000 == 0: 88 | pass 89 | # self.conputeTSNE(step, self.SourceData, self.TargetData,self.SourceLabel, self.TargetLabel, sess) 90 | self.SourceData,self.SourceLabel,self.EdgeWeights=shuffle(self.SourceData,self.SourceLabel,self.EdgeWeights) 91 | # print("success") 92 | def CalLoss(self): 93 | self.source_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=self.source_label, logits=self.source_model.fc5) 94 | self.source_loss = tf.reduce_mean(self.source_cross_entropy) 95 | 96 | self.CalTargetLoss(method="Manifold") 97 | self.CalDomainLoss(method="CORAL") 98 | # self.CalAdver() 99 | # self.L2Loss() 100 | self.loss=self.source_loss+self.domain_loss_param*self.domain_loss+self.target_loss_param*self.target_loss 101 | 102 | 103 | def L2Loss(self): 104 | all_variables = tf.trainable_variables() 105 | self.l2 = 1e-5 * tf.add_n([tf.nn.l2_loss(v) for v in all_variables if 'bias' not in v.name]) 106 | 107 | def CalDomainLoss(self,method): 108 | if method=="MMD": 109 | Xs=self.source_model.fc4 110 | Xt=self.target_model.fc4 111 | diff=tf.reduce_mean(Xs, 0, keep_dims=False) - tf.reduce_mean(Xt, 0, keep_dims=False) 112 | self.domain_loss=tf.reduce_sum(tf.multiply(diff,diff)) 113 | 114 | 115 | elif method=="KMMD": 116 | Xs=self.source_model.fc4 117 | Xt=self.target_model.fc4 118 | self.domain_loss=tf.maximum(0.0001,KMMD(Xs,Xt)) 119 | 120 | 121 | 122 | elif method=="CORAL": 123 | Xs = self.source_model.fc4 124 | Xt = self.target_model.fc4 125 | # d=int(Xs.shape[1]) 126 | # Xms = Xs - tf.reduce_mean(Xs, 0, keep_dims=True) 127 | # Xcs = tf.matmul(tf.transpose(Xms), Xms) / self.BatchSize 128 | # Xmt = Xt - tf.reduce_mean(Xt, 0, keep_dims=True) 129 | # Xct = tf.matmul(tf.transpose(Xmt), Xmt) / self.BatchSize 130 | # self.domain_loss = tf.reduce_sum(tf.multiply((Xcs - Xct), (Xcs - Xct))) 131 | # self.domain_loss=self.domain_loss / (4.0*d*d) 132 | self.domain_loss=self.coral_loss(Xs,Xt) 133 | 134 | 135 | elif method =='LCORAL': 136 | Xs = self.source_model.fc4 137 | Xt = self.target_model.fc4 138 | self.domain_loss=self.log_coral_loss(Xs,Xt) 139 | 140 | 141 | def CalTargetLoss(self,method): 142 | if method=="Entropy": 143 | trg_softmax=self.target_model.softmax_output 144 | self.target_loss=-tf.reduce_mean(tf.reduce_sum(trg_softmax * tf.log(trg_softmax), axis=1)) 145 | 146 | 147 | elif method=="Manifold": 148 | loss=0.0 149 | # Xt = self.target_model.fc4 150 | Xt = self.source_model.fc4 151 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 152 | self.F0 = tf.transpose(norm(tf.expand_dims(Xt, 2) - tf.transpose(Xt))) 153 | margin0 = 0 154 | margin1 = 100 155 | F0=tf.pow(tf.maximum(0.0, self.F0-margin0),2) 156 | F1=tf.pow(tf.maximum(0.0, margin1-self.F0),2) 157 | loss+=tf.reduce_mean(tf.multiply(F0, self.W)) 158 | loss+=tf.reduce_mean(tf.multiply(F1, 1.0-self.W)) 159 | self.target_loss = loss / (self.BatchSize * self.BatchSize) 160 | 161 | 162 | 163 | 164 | 165 | def coral_loss(self, h_src, h_trg, gamma=1e-3): 166 | 167 | # regularized covariances (D-Coral is not regularized actually..) 168 | # First: subtract the mean from the data matrix 169 | batch_size = self.BatchSize 170 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 171 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 172 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src, 173 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 174 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, 175 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 176 | # Returns the Frobenius norm (there is an extra 1/4 in D-Coral actually) 177 | # The reduce_mean account for the factor 1/d^2 178 | return tf.reduce_mean(tf.square(tf.subtract(cov_source, cov_target))) 179 | 180 | def log_coral_loss(self, h_src, h_trg, gamma=1e-3): 181 | # regularized covariances result in inf or nan 182 | # First: subtract the mean from the data matrix 183 | batch_size = float(self.BatchSize) 184 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 185 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 186 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src, 187 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 188 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, 189 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 190 | # eigen decomposition 191 | eig_source = tf.self_adjoint_eig(cov_source) 192 | eig_target = tf.self_adjoint_eig(cov_target) 193 | log_cov_source = tf.matmul(eig_source[1], 194 | tf.matmul(tf.diag(tf.log(eig_source[0])), eig_source[1], transpose_b=True)) 195 | log_cov_target = tf.matmul(eig_target[1], 196 | tf.matmul(tf.diag(tf.log(eig_target[0])), eig_target[1], transpose_b=True)) 197 | 198 | # Returns the Frobenius norm 199 | return tf.reduce_mean(tf.square(tf.subtract(log_cov_source, log_cov_target))) 200 | 201 | # ~ return tf.reduce_mean(tf.reduce_max(eig_target[0])) 202 | # ~ return tf.to_float(tf.equal(tf.count_nonzero(h_src), tf.count_nonzero(h_src))) 203 | 204 | 205 | def Test(self,sess): 206 | true_num=0.0 207 | # num=int(self.TargetData.shape[0]/self.BatchSize) 208 | num = int(self.TestData.shape[0] / self.BatchSize) 209 | total_num=num*self.BatchSize 210 | for i in range (num): 211 | # self.TestData, self.TestLabel = shuffle(self.TestData, self.TestLabel) 212 | k = i % int(self.TestData.shape[0] / self.BatchSize) 213 | target_batch_x = self.TestData[k * self.BatchSize: (k + 1) * self.BatchSize] 214 | target_batch_y= self.TestLabel[k * self.BatchSize: (k + 1) * self.BatchSize] 215 | prediction=sess.run(fetches=self.target_prediction, feed_dict={self.target_image:target_batch_x, self.Training_flag: False}) 216 | true_label = argmax(target_batch_y, 1) 217 | 218 | true_num+=sum(true_label==prediction) 219 | accuracy=true_num / total_num 220 | print "########### Test Accuracy={} ##########".format(accuracy) 221 | 222 | def conputeTSNE(self,step,source_images, target_images,source_labels,target_labels,sess): 223 | 224 | target_images = target_images[:2000] 225 | target_labels = target_labels[:2000] 226 | source_images = source_images[:2000] 227 | source_labels = source_labels[:2000] 228 | 229 | target_labels = one_hot(target_labels.astype(int), 10) 230 | print(source_labels.shape) 231 | 232 | assert len(target_labels) == len(source_labels) 233 | 234 | 235 | 236 | n_slices = int(2000 / 128) 237 | 238 | fx_src = np.empty((0, 64)) 239 | fx_trg = np.empty((0, 64)) 240 | 241 | for src_im, trg_im in zip(np.array_split(source_images, n_slices), 242 | np.array_split(target_images, n_slices), 243 | ): 244 | feed_dict = {self.source_image: src_im, self.target_image: trg_im,self.Training_flag:False} 245 | 246 | fx_src_, fx_trg_ = sess.run([self.source_model.fc4, self.target_model.fc4], feed_dict) 247 | 248 | fx_src = np.vstack((fx_src, np.squeeze(fx_src_))) 249 | fx_trg = np.vstack((fx_trg, np.squeeze(fx_trg_))) 250 | 251 | src_labels = np.argmax(source_labels, 1) 252 | trg_labels = np.argmax(target_labels, 1) 253 | 254 | assert len(src_labels) == len(fx_src) 255 | assert len(trg_labels) == len(fx_trg) 256 | 257 | print 'Computing T-SNE.' 258 | 259 | model = TSNE(n_components=2, random_state=0) 260 | print(plt.style.available) 261 | plt.style.use('ggplot') 262 | 263 | TSNE_hA = model.fit_transform(np.vstack((fx_src, fx_trg))) 264 | plt.figure(1,facecolor="white") 265 | plt.cla() 266 | plt.scatter(TSNE_hA[:,0], TSNE_hA[:,1], c = np.hstack((src_labels, trg_labels,)),s=10, cmap = mpl.cm.jet) 267 | plt.savefig('img01/%d.png'%step) 268 | 269 | def main(): 270 | target_loss_param =0.03 271 | domain_loss_param =8 272 | adver_loss_param=0 273 | param=[target_loss_param, domain_loss_param,adver_loss_param] 274 | Runer=Train(class_num=10,batch_size=128,iters=200200,learning_rate=0.0001,keep_prob=1,param=param) 275 | Runer.TrainNet() 276 | 277 | def load_mnist(image_dir, split='train'): 278 | print ('Loading MNIST dataset.') 279 | 280 | image_file = 'train.pkl' if split == 'train' else 'test.pkl' 281 | image_dir = os.path.join(image_dir, image_file) 282 | with open(image_dir, 'rb') as f: 283 | mnist = pickle.load(f) 284 | images = mnist['X'] / 127.5 - 1 285 | labels = mnist['y'] 286 | labels=np.squeeze(labels).astype(int) 287 | 288 | return images,labels 289 | def load_svhn(image_dir, split='train'): 290 | print ('Loading SVHN dataset.') 291 | 292 | image_file = 'train_32x32.mat' if split == 'train' else 'test_32x32.mat' 293 | 294 | image_dir = os.path.join(image_dir, image_file) 295 | svhn = scipy.io.loadmat(image_dir) 296 | images = np.transpose(svhn['X'], [3, 0, 1, 2]) / 127.5 - 1 297 | # ~ images= resize_images(images) 298 | labels = svhn['y'].reshape(-1) 299 | labels[np.where(labels == 10)] = 0 300 | return images, labels 301 | 302 | def load_USPS(image_dir,split='train'): 303 | print('Loading USPS dataset.') 304 | image_file='USPS_train.pkl' if split=='train' else 'USPS_test.pkl' 305 | image_dir=os.path.join(image_dir,image_file) 306 | with open(image_dir, 'rb') as f: 307 | usps = pickle.load(f) 308 | images = usps['data'] 309 | images=np.reshape(images,[-1,32,32,1]) 310 | labels = usps['label'] 311 | labels=np.squeeze(labels).astype(int) 312 | return images,labels 313 | 314 | def load_syn(image_dir,split='train'): 315 | print('load syn dataset') 316 | image_file='synth_train_32x32.mat' if split=='train' else 'synth_test_32x32.mat' 317 | image_dir=os.path.join(image_dir,image_file) 318 | syn = scipy.io.loadmat(image_dir) 319 | images = np.transpose(syn['X'], [3, 0, 1, 2]) / 127.5 - 1 320 | labels = syn['y'].reshape(-1) 321 | return images,labels 322 | 323 | 324 | def load_mnistm(image_dir,split='train'): 325 | print('Loading mnistm dataset.') 326 | image_file='mnistm_train.pkl' if split=='train' else 'mnistm_test.pkl' 327 | image_dir=os.path.join(image_dir,image_file) 328 | with open(image_dir, 'rb') as f: 329 | mnistm = pickle.load(f) 330 | images = mnistm['data'] 331 | 332 | labels = mnistm['label'] 333 | labels=np.squeeze(labels).astype(int) 334 | return images,labels 335 | 336 | def one_hot(x,n): 337 | if type(x) == list: 338 | x = np.array(x) 339 | x = x.flatten() 340 | o_h = np.zeros((len(x),n)) 341 | o_h[np.arange(len(x)),x] = 1 342 | return o_h 343 | 344 | if __name__=="__main__": 345 | main() -------------------------------------------------------------------------------- /HoMM_mnist/TrainLenetsne.py: -------------------------------------------------------------------------------- 1 | import os 2 | import pickle 3 | from Lenet import * 4 | from Utils import * 5 | import scipy.io 6 | import numpy as np 7 | from tensorflow.contrib import slim 8 | from center_loss import * 9 | # 10 | import matplotlib.pyplot as plt 11 | import matplotlib as mpl 12 | from sklearn.manifold import TSNE 13 | os.environ['CUDA_VISIBLE_DEVICES']='0' 14 | 15 | 16 | 17 | class Train(): 18 | def __init__(self,class_num,batch_size,iters,learning_rate,keep_prob,param): 19 | self.ClassNum=class_num 20 | self.BatchSize=batch_size 21 | self.Iters=iters 22 | self.LearningRate=learning_rate 23 | self.KeepProb=keep_prob 24 | self.discriminative_loss_param=param[0] 25 | self.domain_loss_param=param[1] 26 | self.adver_loss_param=param[2] 27 | self.ring_loss_param = param[3] 28 | 29 | self.SourceData,self.SourceLabel=load_svhn('svhn') 30 | self.TargetData, self.TargetLabel=load_mnist('mnist') 31 | self.TestData, self.TestLabel = load_mnist('mnist',split='test') 32 | # self.EdgeWeights=Label2EdgeWeights(self.SourceLabel) 33 | self.EdgeWeights=zeros((self.SourceLabel.shape[0],self.SourceLabel.shape[0])) 34 | 35 | ######################################################################################## 36 | self.ring_norm = tf.get_variable(name="ring_norm", shape=None, dtype=tf.float32, initializer=tf.constant(1.0),trainable=True) 37 | self.logits_threshold = tf.get_variable(name="logits_threshold", shape=None, dtype=tf.float32,initializer=tf.constant(0.0), trainable=False) 38 | self.cluster_loss_param= tf.get_variable(name="cluster_loss", shape=None, dtype=tf.float32,initializer=tf.constant(0.0), trainable=False) 39 | 40 | ####################################################################################### 41 | self.source_image = tf.placeholder(tf.float32, shape=[None, 32,32,3],name="source_image") 42 | self.source_label = tf.placeholder(tf.float32, shape=[None, self.ClassNum],name="source_label") 43 | self.target_image = tf.placeholder(tf.float32, shape=[None, 32, 32,1],name="target_image") 44 | self.Training_flag = tf.placeholder(tf.bool, shape=None,name="Training_flag") 45 | self.W = tf.placeholder(tf.float32, shape=[self.BatchSize, self.BatchSize]) 46 | 47 | 48 | def TrainNet(self): 49 | self.source_model=Lenet(inputs=self.source_image, training_flag=self.Training_flag, reuse=False) 50 | self.target_model=Lenet(inputs=self.target_image, training_flag=self.Training_flag, reuse=True) 51 | self.CalLoss() 52 | varall=tf.trainable_variables() 53 | with tf.control_dependencies([self.centers_update_op]): 54 | self.solver = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss) 55 | self.source_prediction = tf.argmax(self.source_model.softmax_output, 1) 56 | self.target_prediction = tf.argmax(self.target_model.softmax_output, 1) 57 | with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess: 58 | init = tf.global_variables_initializer() 59 | sess.run(init) 60 | self.SourceLabel=sess.run(tf.one_hot(self.SourceLabel,10)) 61 | self.TestLabel=sess.run(tf.one_hot(self.TestLabel,10)) 62 | true_num = 0.0 63 | lossData=[] 64 | for step in range(self.Iters): 65 | i= step % int(self.SourceData.shape[0]/self.BatchSize) 66 | j= step % int(self.TargetData.shape[0]/self.BatchSize) 67 | source_batch_x = self.SourceData[i * self.BatchSize: (i + 1) * self.BatchSize] 68 | source_batch_y = self.SourceLabel[i * self.BatchSize: (i + 1) * self.BatchSize] 69 | target_batch_x = self.TargetData[j * self.BatchSize: (j + 1) * self.BatchSize] 70 | W = self.EdgeWeights[i * self.BatchSize: (i + 1) * self.BatchSize,i * self.BatchSize: (i + 1) * self.BatchSize] 71 | total_loss, source_loss,domain_loss,intra_loss,inter_loss, source_prediction,_= sess.run( 72 | fetches=[self.loss, self.source_loss, self.domain_loss, self.intra_loss, self.inter_loss, self.source_prediction, self.solver], 73 | feed_dict={self.source_image: source_batch_x, self.source_label: source_batch_y,self.target_image: target_batch_x, self.Training_flag: True, self.W: W}) 74 | 75 | true_label = argmax(source_batch_y, 1) 76 | true_num = true_num + sum(true_label == source_prediction) 77 | 78 | 79 | if step % 200 ==0: 80 | print "Iters-{} ### TotalLoss={} ### SourceLoss={} ###DomainLoss={}".format(step, total_loss, source_loss, domain_loss) 81 | train_accuracy = true_num / (200*self.BatchSize) 82 | true_num = 0.0 83 | print " ########## train_accuracy={} ###########".format(train_accuracy) 84 | self.Test(sess,lossData) 85 | 86 | 87 | if step % 5000 == 0: 88 | self.computeTSNE(step, self.SourceData, self.TargetData, self.SourceLabel, self.TargetLabel,sess) 89 | 90 | if step==200000: 91 | sess.run(tf.assign(self.cluster_loss_param,0.1)) 92 | sess.run(tf.assign(self.logits_threshold,0.90)) 93 | 94 | # savedata = np.array(lossData) 95 | # np.save("SVHNtoMNIST.npy", savedata) 96 | 97 | 98 | 99 | 100 | 101 | 102 | def CalLoss(self): 103 | self.source_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=self.source_label, logits=self.source_model.fc5) 104 | self.source_loss = tf.reduce_mean(self.source_cross_entropy) 105 | self.CalDomainLoss(method="HoMM3") 106 | self.CalTargetLoss("Entropy") 107 | self.DisClustering() 108 | # self.CalAdver() 109 | self.loss=self.source_loss+self.domain_loss_param*self.domain_loss+self.cluster_loss_param*self.target_loss 110 | 111 | 112 | 113 | def CalDomainLoss(self,method): 114 | if method=="MMD": 115 | Xs=self.source_model.fc4 116 | Xt=self.target_model.fc4 117 | diff=tf.reduce_mean(Xs, 0, keep_dims=False) - tf.reduce_mean(Xt, 0, keep_dims=False) 118 | self.domain_loss=tf.reduce_sum(tf.multiply(diff,diff)) 119 | 120 | 121 | elif method=="KMMD": 122 | Xs=self.source_model.fc4 123 | Xt=self.target_model.fc4 124 | self.domain_loss=tf.maximum(0.0001,KMMD(Xs,Xt)) 125 | 126 | 127 | elif method=="CORAL": 128 | Xs = self.source_model.fc4 129 | Xt = self.target_model.fc4 130 | self.domain_loss=self.coral_loss(Xs,Xt) 131 | 132 | 133 | elif method=="HoMM": 134 | Xs = self.source_model.fc4 135 | Xt = self.target_model.fc4 136 | self.domain_loss = self.HoMM(Xs, Xt,order=3,num=100000) 137 | 138 | elif method=='HoMM3': 139 | Xs = self.source_model.fc4 140 | Xt = self.target_model.fc4 141 | self.domain_loss = self.HoMM3_loss(Xs, Xt) 142 | 143 | 144 | elif method=='HoMM4': 145 | Xs = self.source_model.fc4 146 | Xt = self.target_model.fc4 147 | self.domain_loss = self.HoMM4(Xs, Xt) 148 | 149 | 150 | elif method =='LCORAL': 151 | Xs = self.source_model.fc4 152 | Xt = self.target_model.fc4 153 | self.domain_loss=self.log_coral_loss(Xs,Xt) 154 | 155 | 156 | 157 | elif method =="mmatch": 158 | Xs = self.source_model.fc4 159 | Xt = self.target_model.fc4 160 | self.domain_loss=mmatch(Xs,Xt,3) 161 | 162 | 163 | elif method == "CorNorm": 164 | gamma=0.001 # 0.001 #0.0003 mnist->mnist-m 165 | Xs = self.source_model.fc4 166 | Xt = self.target_model.fc4 167 | Xs = tf.transpose(Xs - tf.reduce_mean(Xs, axis=0)) 168 | Xt = tf.transpose(Xt - tf.reduce_mean(Xt, axis=0)) 169 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 170 | self.Xs_Cor_norm = tf.transpose(norm(tf.expand_dims(Xs, 2) - tf.transpose(Xs))) 171 | self.Xt_Cor_norm = tf.transpose(norm(tf.expand_dims(Xt, 2) - tf.transpose(Xt))) 172 | self.W1=tf.exp(-gamma*self.Xs_Cor_norm) 173 | self.W2=tf.exp(-gamma*self.Xt_Cor_norm) 174 | self.W1=self.W1/(tf.reduce_mean(self.W1)) 175 | self.W2=self.W2/(tf.reduce_mean(self.W2)) 176 | self.domain_loss = tf.reduce_mean(tf.square(tf.subtract(self.W1, self.W2))) 177 | 178 | 179 | 180 | def CalTargetLoss(self,method): 181 | if method=="Entropy": 182 | source_softmax = self.source_model.softmax_output 183 | target_softmax = self.target_model.softmax_output 184 | target_softmax_mean = tf.reduce_mean(target_softmax, axis=0) 185 | index = tf.where(tf.equal(tf.greater(target_softmax, self.logits_threshold), True))[:, 0] 186 | target_softmax = tf.gather(target_softmax, index) 187 | self.target_loss_1 = tf.reduce_mean(target_softmax_mean * tf.log(tf.clip_by_value(target_softmax_mean, 0.0001, 1))) 188 | self.target_loss_2 = -tf.reduce_mean(tf.reduce_sum(target_softmax * tf.log(tf.clip_by_value(target_softmax, 0.0001, 1)), axis=1)) 189 | self.target_loss = 0.0 * self.target_loss_1 + self.target_loss_2 190 | 191 | 192 | 193 | 194 | def CalDiscriminativeLoss(self,method): 195 | if method=="InstanceBased": 196 | Xs = self.source_model.fc4 197 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 198 | self.F0 = tf.transpose(norm(tf.expand_dims(Xs, 2) - tf.transpose(Xs))) 199 | margin0 = 0 200 | margin1 = 100 201 | F0=tf.pow(tf.maximum(0.0, self.F0-margin0),2) 202 | F1=tf.pow(tf.maximum(0.0, margin1-self.F0),2) 203 | self.intra_loss=tf.reduce_mean(tf.multiply(F0, self.W)) 204 | self.inter_loss=tf.reduce_mean(tf.multiply(F1, 1.0-self.W)) 205 | self.discriminative_loss = (self.intra_loss+self.inter_loss) / (self.BatchSize * self.BatchSize) 206 | 207 | 208 | 209 | 210 | elif method == "CenterBased": 211 | Xs = self.source_model.fc4 212 | labels = tf.argmax(self.source_label, 1) 213 | self.inter_loss, self.intra_loss, self.centers_update_op = get_center_loss(Xs, labels, 0.5, 10) 214 | self.intra_loss = self.intra_loss / (self.ClassNum * self.BatchSize + self.ClassNum * self.ClassNum) 215 | self.inter_loss = self.inter_loss / (self.ClassNum * self.BatchSize + self.ClassNum * self.ClassNum) 216 | self.discriminative_loss = self.intra_loss + self.inter_loss 217 | 218 | 219 | 220 | def DisClustering(self): 221 | target_feature, target_logits = SelectTargetSamples(self.target_model.fc4, self.target_model.fc5, logits_threshold=self.logits_threshold) 222 | target_pseudo_label = tf.argmax(target_logits, axis=1) 223 | with tf.variable_scope('target'): 224 | self.inter_loss, self.intra_loss, self.centers_update_op = get_center_loss(target_feature,target_pseudo_label,0.5,10) 225 | self.clustering_loss=self.intra_loss/(self.ClassNum*self.BatchSize) 226 | 227 | 228 | 229 | def Cal_RingLoss(self): 230 | Xs=self.source_model.fc4 231 | Xs_norm=tf.norm(Xs,ord="euclidean",axis=1) 232 | Xt = self.target_model.fc4 233 | Xt_norm = tf.norm(Xt, ord="euclidean", axis=1) 234 | self.ring_loss=tf.reduce_mean(tf.norm(Xs_norm-self.ring_norm))+tf.reduce_mean(tf.norm(Xt_norm-self.ring_norm)) 235 | 236 | 237 | 238 | def coral_loss(self, h_src, h_trg, gamma=1e-3): 239 | # regularized covariances (D-Coral is not regularized actually..) 240 | # First: subtract the mean from the data matrix 241 | batch_size = self.BatchSize 242 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 243 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 244 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src, 245 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 246 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, 247 | transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 248 | # cov_source=tf.linalg.cholesky(cov_source) 249 | # cov_target=tf.linalg.cholesky(cov_target) 250 | return tf.reduce_mean(tf.square(tf.subtract(cov_source, cov_target))) 251 | 252 | 253 | 254 | 255 | 256 | def log_coral_loss(self, h_src, h_trg, gamma=1e-3): 257 | # regularized covariances result in inf or nan 258 | # First: subtract the mean from the data matrix 259 | batch_size = float(self.BatchSize) 260 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 261 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 262 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src,transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 263 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg,transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 264 | # eigen decomposition 265 | eig_source = tf.self_adjoint_eig(cov_source) 266 | eig_target = tf.self_adjoint_eig(cov_target) 267 | log_cov_source = tf.matmul(eig_source[1],tf.matmul(tf.diag(tf.log(eig_source[0])), eig_source[1], transpose_b=True)) 268 | log_cov_target = tf.matmul(eig_target[1],tf.matmul(tf.diag(tf.log(eig_target[0])), eig_target[1], transpose_b=True)) 269 | return tf.reduce_mean(tf.square(tf.subtract(log_cov_source, log_cov_target))) 270 | 271 | 272 | 273 | def HoMM3_loss(self, xs, xt): 274 | xs = xs - tf.reduce_mean(xs, axis=0) 275 | # xs=self.decoupling(xs) 276 | xt = xt - tf.reduce_mean(xt, axis=0) 277 | # xt=self.decoupling(xt) 278 | xs=tf.expand_dims(xs,axis=-1) 279 | xs = tf.expand_dims(xs, axis=-1) 280 | xt = tf.expand_dims(xt, axis=-1) 281 | xt = tf.expand_dims(xt, axis=-1) 282 | xs_1=tf.transpose(xs,[0,2,1,3]) 283 | xs_2 = tf.transpose(xs, [0, 2, 3, 1]) 284 | xt_1 = tf.transpose(xt, [0, 2, 1, 3]) 285 | xt_2 = tf.transpose(xt, [0, 2, 3, 1]) 286 | HR_Xs=xs*xs_1*xs_2 287 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 288 | HR_Xt = xt * xt_1 * xt_2 289 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 290 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 291 | 292 | 293 | def decoupling(self,X): 294 | dim=X.shape[0] 295 | Ones=0.0001*tf.ones(shape=[dim,1]) 296 | Y=tf.concat([Ones,X],axis=1) 297 | return Y 298 | 299 | 300 | 301 | def HoMM4(self,xs,xt): 302 | ind=tf.range(tf.cast(xs.shape[1],tf.int32)) 303 | ind=tf.random_shuffle(ind) 304 | xs=tf.transpose(xs,[1,0]) 305 | xs=tf.gather(xs,ind) 306 | xs = tf.transpose(xs, [1, 0]) 307 | xt = tf.transpose(xt, [1, 0]) 308 | xt = tf.gather(xt, ind) 309 | xt = tf.transpose(xt, [1, 0]) 310 | return self.HoMM4_loss(xs[:,:30],xt[:,:30])+self.HoMM4_loss(xs[:,30:60],xt[:,30:60])+self.HoMM4_loss(xs[:,60:90],xt[:,60:90]) 311 | 312 | 313 | 314 | def HoMM4_loss(self, xs, xt): 315 | xs = xs - tf.reduce_mean(xs, axis=0) 316 | xt = xt - tf.reduce_mean(xt, axis=0) 317 | xs = tf.expand_dims(xs,axis=-1) 318 | xs = tf.expand_dims(xs, axis=-1) 319 | xs = tf.expand_dims(xs, axis=-1) 320 | xt = tf.expand_dims(xt, axis=-1) 321 | xt = tf.expand_dims(xt, axis=-1) 322 | xt = tf.expand_dims(xt, axis=-1) 323 | xs_1 = tf.transpose(xs,[0,2,1,3,4]) 324 | xs_2 = tf.transpose(xs, [0, 2, 3, 1,4]) 325 | xs_3 = tf.transpose(xs, [0, 2, 3, 4, 1]) 326 | xt_1 = tf.transpose(xt, [0, 2, 1, 3,4]) 327 | xt_2 = tf.transpose(xt, [0, 2, 3, 1,4]) 328 | xt_3 = tf.transpose(xt, [0, 2, 3, 4, 1]) 329 | HR_Xs=xs*xs_1*xs_2*xs_3 330 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 331 | HR_Xt = xt * xt_1 * xt_2*xt_3 332 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 333 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 334 | 335 | 336 | 337 | 338 | def HoMM(self,xs, xt, order=3, num=400000): 339 | xs = xs - tf.reduce_mean(xs, axis=0) 340 | xt = xt - tf.reduce_mean(xt, axis=0) 341 | dim = tf.cast(xs.shape[1], tf.int32) 342 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32, seed=0) 343 | index = index[:, :order] 344 | xs = tf.transpose(xs) 345 | xs = tf.gather(xs, index) ##dim=[num,order,batchsize] 346 | xt = tf.transpose(xt) 347 | xt = tf.gather(xt, index) 348 | HO_Xs = tf.reduce_prod(xs, axis=1) 349 | HO_Xs = tf.reduce_mean(HO_Xs, axis=1) 350 | HO_Xt = tf.reduce_prod(xt, axis=1) 351 | HO_Xt = tf.reduce_mean(HO_Xt, axis=1) 352 | return tf.reduce_mean(tf.square(tf.subtract(HO_Xs, HO_Xt))) 353 | 354 | 355 | 356 | 357 | 358 | 359 | def Test(self,sess,lossData): 360 | true_num=0.0 361 | num = int(self.TestData.shape[0] / self.BatchSize) 362 | total_num=num*self.BatchSize 363 | for i in range (num): 364 | k = i % int(self.TestData.shape[0] / self.BatchSize) 365 | target_batch_x = self.TestData[k * self.BatchSize: (k + 1) * self.BatchSize] 366 | target_batch_y= self.TestLabel[k * self.BatchSize: (k + 1) * self.BatchSize] 367 | prediction=sess.run(fetches=self.target_prediction, feed_dict={self.target_image:target_batch_x, self.Training_flag: False}) 368 | true_label = argmax(target_batch_y, 1) 369 | true_num+=sum(true_label==prediction) 370 | accuracy=true_num / total_num 371 | lossData.append(accuracy) 372 | print "########### Test Accuracy={} ##########".format(accuracy) 373 | 374 | def computeTSNE(self,step,source_images, target_images,source_labels,target_labels,sess): 375 | 376 | target_images = target_images[:2000] 377 | target_labels = target_labels[:2000] 378 | source_images = source_images[:2000] 379 | source_labels = source_labels[:2000] 380 | 381 | target_labels = one_hot(target_labels.astype(int), 10) 382 | print(source_labels.shape) 383 | 384 | assert len(target_labels) == len(source_labels) 385 | 386 | 387 | 388 | n_slices = int(2000 / 128) 389 | 390 | fx_src = np.empty((0, 90)) 391 | fx_trg = np.empty((0, 90)) 392 | 393 | for src_im, trg_im in zip(np.array_split(source_images, n_slices), 394 | np.array_split(target_images, n_slices), 395 | ): 396 | feed_dict = {self.source_image: src_im, self.target_image: trg_im,self.Training_flag:False} 397 | 398 | fx_src_, fx_trg_ = sess.run([self.source_model.fc4, self.target_model.fc4], feed_dict) 399 | 400 | fx_src = np.vstack((fx_src, np.squeeze(fx_src_))) 401 | fx_trg = np.vstack((fx_trg, np.squeeze(fx_trg_))) 402 | 403 | src_labels = np.argmax(source_labels, 1) 404 | trg_labels = np.argmax(target_labels, 1) 405 | 406 | assert len(src_labels) == len(fx_src) 407 | assert len(trg_labels) == len(fx_trg) 408 | 409 | print 'Computing T-SNE.' 410 | 411 | model = TSNE(n_components=2, random_state=0) 412 | 413 | print(plt.style.available) 414 | plt.style.use('seaborn-paper') 415 | 416 | TSNE_hA = model.fit_transform(np.vstack((fx_src, fx_trg))) 417 | plt.figure(1,facecolor="white") 418 | plt.cla() 419 | plt.scatter(TSNE_hA[:,0], TSNE_hA[:,1], c = np.hstack((src_labels, trg_labels,)),s=10, cmap = mpl.cm.jet) 420 | plt.savefig('tsne_00/c_%d.eps'%step,format="eps",dpi=1000,bbox_inches="tight") 421 | 422 | plt.figure(2,facecolor="white") 423 | plt.cla() 424 | plt.scatter(TSNE_hA[:, 0], TSNE_hA[:, 1], c=np.hstack((np.ones((2000,)), 2 * np.ones((2000,)))), s=10, 425 | cmap=mpl.cm.jet) 426 | 427 | plt.savefig('tsne_01/d_%d.eps'%step,format="eps",dpi=1000,bbox_inches="tight") 428 | 429 | 430 | 431 | def compute_2D(self, step, source_images, target_images, source_labels, target_labels, sess): 432 | 433 | target_images = target_images[:2000] 434 | target_labels = target_labels[:2000] 435 | source_images = source_images[:2000] 436 | source_labels = source_labels[:2000] 437 | 438 | target_labels = one_hot(target_labels.astype(int), 10) 439 | print(source_labels.shape) 440 | 441 | assert len(target_labels) == len(source_labels) 442 | 443 | n_slices = int(2000 / 128) 444 | 445 | fx_src = np.empty((0, 2)) 446 | fx_trg = np.empty((0, 2)) 447 | 448 | for src_im, trg_im in zip(np.array_split(source_images, n_slices), 449 | np.array_split(target_images, n_slices), 450 | ): 451 | feed_dict = {self.source_image: src_im, self.target_image: trg_im, self.Training_flag: False} 452 | 453 | fx_src_, fx_trg_ = sess.run([self.source_model.fc4, self.target_model.fc4], feed_dict) 454 | 455 | fx_src = np.vstack((fx_src, np.squeeze(fx_src_))) 456 | fx_trg = np.vstack((fx_trg, np.squeeze(fx_trg_))) 457 | 458 | src_labels = np.argmax(source_labels, 1) 459 | trg_labels = np.argmax(target_labels, 1) 460 | # images=np.vstack((fx_src, fx_trg)) 461 | assert len(src_labels) == len(fx_src) 462 | assert len(trg_labels) == len(fx_trg) 463 | 464 | print 'Computing T-SNE 2D.' 465 | 466 | plt.style.use('seaborn-paper') 467 | 468 | plt.figure(1, facecolor="white") 469 | plt.cla() 470 | plt.scatter(fx_src[:, 0], fx_src[:, 1], c=src_labels, s=10, cmap=mpl.cm.jet) 471 | plt.savefig('img_2D1/%d.eps' % step, format="eps", dpi=1000, bbox_inches="tight") 472 | 473 | 474 | 475 | def main(): 476 | discriminative_loss_param = 0.0 ##0.03 for InstanceBased method, 0.01 for CenterBased method 477 | domain_loss_param = 10000 #1000 for Cor_Norm and 8 for Coral 478 | adver_loss_param=0 479 | ring_loss_param = 0.0 # 0.0005 480 | param=[discriminative_loss_param, domain_loss_param,adver_loss_param,ring_loss_param] 481 | Runer=Train(class_num=10,batch_size=128,iters=200200,learning_rate=0.0001,keep_prob=1,param=param) 482 | Runer.TrainNet() 483 | 484 | 485 | 486 | 487 | def load_mnist(image_dir, split='train'): 488 | print ('Loading MNIST dataset.') 489 | 490 | image_file = 'train.pkl' if split == 'train' else 'test.pkl' 491 | image_dir = os.path.join(image_dir, image_file) 492 | with open(image_dir, 'rb') as f: 493 | mnist = pickle.load(f) 494 | images = mnist['X'] / 127.5 - 1 495 | labels = mnist['y'] 496 | labels=np.squeeze(labels).astype(int) 497 | return images,labels 498 | 499 | 500 | def load_svhn(image_dir, split='train'): 501 | print ('Loading SVHN dataset.') 502 | 503 | image_file = 'train_32x32.mat' if split == 'train' else 'test_32x32.mat' 504 | 505 | image_dir = os.path.join(image_dir, image_file) 506 | svhn = scipy.io.loadmat(image_dir) 507 | images = np.transpose(svhn['X'], [3, 0, 1, 2]) / 127.5 - 1 508 | # ~ images= resize_images(images) 509 | labels = svhn['y'].reshape(-1) 510 | labels[np.where(labels == 10)] = 0 511 | return images, labels 512 | 513 | 514 | def load_USPS(image_dir,split='train'): 515 | print('Loading USPS dataset.') 516 | image_file='USPS_train.pkl' if split=='train' else 'USPS_test.pkl' 517 | image_dir=os.path.join(image_dir,image_file) 518 | with open(image_dir, 'rb') as f: 519 | usps = pickle.load(f) 520 | images = usps['data'] 521 | images=np.reshape(images,[-1,32,32,1]) 522 | labels = usps['label'] 523 | labels=np.squeeze(labels).astype(int) 524 | return images,labels 525 | 526 | 527 | 528 | def load_syn(image_dir,split='train'): 529 | print('load syn dataset') 530 | image_file='synth_train_32x32.mat' if split=='train' else 'synth_test_32x32.mat' 531 | image_dir=os.path.join(image_dir,image_file) 532 | syn = scipy.io.loadmat(image_dir) 533 | images = np.transpose(syn['X'], [3, 0, 1, 2]) / 127.5 - 1 534 | labels = syn['y'].reshape(-1) 535 | return images,labels 536 | 537 | 538 | def load_mnistm(image_dir,split='train'): 539 | print('Loading mnistm dataset.') 540 | image_file='mnistm_train.pkl' if split=='train' else 'mnistm_test.pkl' 541 | image_dir=os.path.join(image_dir,image_file) 542 | with open(image_dir, 'rb') as f: 543 | mnistm = pickle.load(f) 544 | images = mnistm['data'] 545 | 546 | labels = mnistm['label'] 547 | labels=np.squeeze(labels).astype(int) 548 | return images,labels 549 | 550 | def one_hot(x,n): 551 | if type(x) == list: 552 | x = np.array(x) 553 | x = x.flatten() 554 | o_h = np.zeros((len(x),n)) 555 | o_h[np.arange(len(x)),x] = 1 556 | return o_h 557 | 558 | 559 | if __name__=="__main__": 560 | 561 | main() -------------------------------------------------------------------------------- /HoMM_mnist/TrainNet.py: -------------------------------------------------------------------------------- 1 | from alexnet import * 2 | from DataLoader import * 3 | from Utils import * 4 | os.environ['CUDA_VISIBLE_DEVICES']='' 5 | 6 | 7 | 8 | class Train(): 9 | def __init__(self,class_num,batch_size,iters,learning_rate,param): 10 | self.ClassNum=class_num 11 | self.BatchSize=batch_size 12 | self.Iters=iters 13 | self.LearningRate=learning_rate 14 | self.target_loss_param=param[0] 15 | self.domain_loss_param=param[1] 16 | self.adver_loss_param=param[2] 17 | Data=DataLoader("office31",source="Amazon",target="Webcam") 18 | self.SourceData,self.SourceLabel=Data.LoadSource() 19 | self.TargetData,self.TestData, self.TestLabel=Data.LoadTarget() 20 | 21 | ####################################################################################### 22 | self.source_image = tf.placeholder(tf.float32, shape=[self.BatchSize, 227,227,3],name="source_image") 23 | self.source_label = tf.placeholder(tf.float32, shape=[self.BatchSize, self.ClassNum],name="source_label") 24 | self.target_image = tf.placeholder(tf.float32, shape=[self.BatchSize, 227, 227, 3],name="target_image") 25 | self.Training_flag = tf.placeholder(tf.bool, shape=None,name="Training_flag") 26 | self.KeepProb = tf.placeholder(tf.float32,name='keep_prob') 27 | 28 | 29 | 30 | def TrainNet(self): 31 | self.source_model=AlexNet(self.source_image, self.ClassNum, self.KeepProb, "model/", reuse=False) 32 | self.target_model=AlexNet(self.target_image, self.ClassNum, self.KeepProb, "model/", reuse=True) 33 | self.CalLoss() 34 | varall=tf.trainable_variables() 35 | var_fc=[var for var in varall if 'fc' in var.name] 36 | 37 | self.solver1 = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss,var_list=var_fc) 38 | 39 | self.source_prediction = tf.argmax(self.source_model.softmax_output, 1) 40 | self.target_prediction = tf.argmax(self.target_model.softmax_output, 1) 41 | with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess: 42 | # update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 43 | # with tf.control_dependencies(update_ops): 44 | # self.solver = tf.train.AdamOptimizer(learning_rate=self.LearningRate).minimize(self.loss) 45 | init = tf.global_variables_initializer() 46 | sess.run(init) 47 | self.source_model.weights_initial(sess) 48 | self.target_model.weights_initial(sess) 49 | true_num = 0.0 50 | for step in range(self.Iters): 51 | # self.SourceData,self.SourceLabel=shuffle(self.SourceData,self.SourceLabel) 52 | i= step % int(self.SourceData.shape[0]/self.BatchSize) 53 | j= step % int(self.TargetData.shape[0]/self.BatchSize) 54 | source_batch_x = self.SourceData[i * self.BatchSize: (i + 1) * self.BatchSize, :] 55 | source_batch_y = self.SourceLabel[i * self.BatchSize: (i + 1) * self.BatchSize, :] 56 | target_batch_x = self.TargetData[j * self.BatchSize: (j + 1) * self.BatchSize, :] 57 | total_loss, source_loss, target_loss, domain_loss,maping_loss,source_prediction,_= sess.run( 58 | fetches=[self.loss, self.source_loss, self.target_loss, self.domain_loss,self.maping_loss, self.source_prediction, self.solver1], 59 | feed_dict={self.source_image: source_batch_x, self.source_label: source_batch_y,self.target_image: target_batch_x, self.Training_flag: True,self.KeepProb:0.5}) 60 | 61 | true_label = argmax(source_batch_y, 1) 62 | true_num = true_num + sum(true_label == source_prediction) 63 | 64 | if step % 100==0: 65 | self.SourceData, self.SourceLabel = shuffle(self.SourceData, self.SourceLabel) 66 | if step % 20 ==0: 67 | print "Iters-{} ### TotalLoss={} ### SourceLoss={} ### TargetLoss={} ### domain_loss={}###maping_loss={}".format(step, total_loss, source_loss, target_loss, domain_loss,maping_loss) 68 | train_accuracy = true_num / (20*self.BatchSize) 69 | true_num = 0.0 70 | print " ########## train_accuracy={} ###########".format(train_accuracy) 71 | self.Test(sess) 72 | 73 | 74 | 75 | 76 | 77 | 78 | def CalLoss(self): 79 | self.source_cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=self.source_label, logits=self.source_model.fc8) 80 | self.source_loss = tf.reduce_mean(self.source_cross_entropy) 81 | self.CalTargetLoss(method="Entropy") 82 | self.CalDomainLoss(method="MMD") 83 | self.CalAdver() 84 | self.L2Loss() 85 | self.loss=self.source_loss+ self.target_loss_param * self.target_loss+ self.domain_loss_param * self.domain_loss+self.l2+self.adver_loss_param*self.maping_loss 86 | 87 | def L2Loss(self): 88 | all_variables = tf.trainable_variables() 89 | self.l2 = 1e-5 * tf.add_n([tf.nn.l2_loss(v) for v in all_variables if 'bias' not in v.name]) 90 | 91 | def CalDomainLoss(self,method): 92 | if method=="MMD": 93 | Xs=self.source_model.AdapLayer 94 | Xt=self.target_model.AdapLayer 95 | diff=tf.reduce_mean(Xs, 0, keep_dims=False) - tf.reduce_mean(Xt, 0, keep_dims=False) 96 | self.domain_loss=tf.reduce_sum(tf.multiply(diff,diff)) 97 | 98 | 99 | elif method=="KMMD": 100 | Xs=self.source_model.AdapLayer 101 | Xt=self.target_model.AdapLayer 102 | self.domain_loss=tf.maximum(0.0001,KMMD(Xs,Xt)) 103 | 104 | 105 | elif method=="CORAL": 106 | Xs = self.source_model.AdapLayer 107 | Xt = self.target_model.Adaplayer 108 | d=int(Xs.shape[1]) 109 | Xms = Xs - tf.reduce_mean(Xs, 0, keep_dims=True) 110 | Xcs = tf.matmul(tf.transpose(Xms), Xms) / self.BatchSize 111 | Xmt = Xt - tf.reduce_mean(Xt, 0, keep_dims=True) 112 | Xct = tf.matmul(tf.transpose(Xmt), Xmt) / self.BatchSize 113 | self.domain_loss = tf.reduce_sum(tf.multiply((Xcs - Xct), (Xcs - Xct))) 114 | self.domain_loss=self.domain_loss / (4.0*d*d) 115 | 116 | def CalTargetLoss(self,method): 117 | if method=="Entropy": 118 | trg_softmax=self.target_model.softmax_output 119 | self.target_loss=-tf.reduce_mean(tf.reduce_sum(trg_softmax * tf.log(trg_softmax), axis=1)) 120 | 121 | 122 | elif method=="Manifold": 123 | pass 124 | 125 | 126 | 127 | def CalParamLoss(self): 128 | pass 129 | 130 | def CalAdver(self): 131 | source_adversary_label = tf.zeros([self.BatchSize], tf.int32) 132 | target_adversary_label = tf.ones([self.BatchSize], tf.int32) 133 | adversary_label = tf.concat([source_adversary_label, target_adversary_label], 0) 134 | adversary_label = tf.one_hot(adversary_label, 2) 135 | adversary_logits = tf.concat([self.source_model.AdverLayer2, self.target_model.AdverLayer2], 0) 136 | self.maping_loss = tf.reduce_mean( 137 | tf.nn.softmax_cross_entropy_with_logits(labels=1 - adversary_label, logits=adversary_logits)) 138 | self.adver_loss = tf.reduce_mean( 139 | tf.nn.softmax_cross_entropy_with_logits(labels=adversary_label, logits=adversary_logits)) 140 | 141 | 142 | def Test(self,sess): 143 | true_num=0.0 144 | num=int(self.TargetData.shape[0]/self.BatchSize) 145 | total_num=num*self.BatchSize 146 | for i in range (num): 147 | # self.TestData, self.TestLabel = shuffle(self.TestData, self.TestLabel) 148 | k = i % int(self.TestData.shape[0] / self.BatchSize) 149 | target_batch_x = self.TestData[k * self.BatchSize: (k + 1) * self.BatchSize, :] 150 | target_batch_y= self.TestLabel[k * self.BatchSize: (k + 1) * self.BatchSize, :] 151 | prediction=sess.run(fetches=self.target_prediction, feed_dict={self.target_image:target_batch_x, self.Training_flag: False,self.KeepProb:1.0}) 152 | true_label = argmax(target_batch_y, 1) 153 | true_num+=sum(true_label==prediction) 154 | accuracy=true_num / total_num 155 | print "########### Test Accuracy={} ##########".format(accuracy) 156 | 157 | def main(): 158 | target_loss_param =0 159 | domain_loss_param = 0 160 | adver_loss_param=0 161 | param=[target_loss_param, domain_loss_param,adver_loss_param] 162 | Runer=Train(class_num=31,batch_size=128,iters=100000,learning_rate=0.0001,param=param) 163 | Runer.TrainNet() 164 | 165 | 166 | 167 | if __name__=="__main__": 168 | main() -------------------------------------------------------------------------------- /HoMM_mnist/Utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | import tensorflow as tf 3 | from functools import partial 4 | from numpy import * 5 | import matplotlib.pyplot as plt 6 | import matplotlib as mpl 7 | from sklearn.manifold import TSNE 8 | 9 | 10 | 11 | def shuffle0(Data, Label): 12 | ind = range(Data.shape[0]) 13 | random.shuffle(ind) 14 | Data = Data[ind, :, :, :] 15 | Label = Label[ind, :] 16 | return Data, Label 17 | 18 | 19 | 20 | def shuffle(Data, Label, Weights): 21 | ind = range(Data.shape[0]) 22 | random.shuffle(ind) 23 | Data = Data[ind, :, :, :] 24 | Label = Label[ind, :] 25 | Weights=Weights[ind,:] 26 | Weights=Weights[:,ind] 27 | return Data, Label, Weights 28 | 29 | def compute_pairwise_distances(x, y): 30 | if not len(x.get_shape()) == len(y.get_shape()) == 2: 31 | raise ValueError('Both inputs should be matrices.') 32 | if x.get_shape().as_list()[1] != y.get_shape().as_list()[1]: 33 | raise ValueError('The number of features should be the same.') 34 | 35 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 36 | return tf.transpose(norm(tf.expand_dims(x, 2) - tf.transpose(y))) 37 | 38 | 39 | def gaussian_kernel_matrix(x, y, sigmas): 40 | beta = 1. / (2. * (tf.expand_dims(sigmas, 1))) 41 | dist = compute_pairwise_distances(x, y) 42 | s = tf.matmul(beta, tf.reshape(dist, (1, -1))) 43 | return tf.reshape(tf.reduce_sum(tf.exp(-s), 0), tf.shape(dist)) 44 | 45 | 46 | def maximum_mean_discrepancy(x, y, kernel=gaussian_kernel_matrix): 47 | cost = tf.reduce_mean(kernel(x, x)) 48 | cost += tf.reduce_mean(kernel(y, y)) 49 | cost -= 2 * tf.reduce_mean(kernel(x, y)) 50 | # We do not allow the loss to become negative. 51 | cost = tf.where(cost > 0, cost, 0, name='value') 52 | return cost 53 | 54 | 55 | def KMMD(Xs,Xt): 56 | # sigmas=[1e-2,0.1,1,5,10,20,25,30,35,100] 57 | # guassian_kernel=partial(kernel,sigmas=tf.constant(sigmas)) 58 | # cost = tf.reduce_mean(guassian_kernel(Xs, Xs)) 59 | # cost += tf.reduce_mean(guassian_kernel(Xt, Xt)) 60 | # cost -= 2 * tf.reduce_mean(guassian_kernel(Xs, Xt)) 61 | # cost = tf.where(cost > 0, cost, 0) 62 | 63 | sigmas = [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100, 1e3, 1e4, 1e5, 1e6] 64 | gaussian_kernel = partial(gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) 65 | cost= maximum_mean_discrepancy(Xs, Xt, kernel=gaussian_kernel) 66 | 67 | 68 | return cost 69 | 70 | def kernel(X, Y, sigmas): 71 | beta = 1.0/(2.0 * (tf.expand_dims(sigmas,1))) 72 | dist = Cal_pairwise_dist(X,Y) 73 | s = tf.matmul(beta, tf.reshape(dist,(1,-1))) 74 | return tf.reshape(tf.reduce_sum(tf.exp(-s), 0), tf.shape(dist)) 75 | 76 | 77 | 78 | def Cal_pairwise_dist(X,Y): 79 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 80 | dist= tf.transpose(norm(tf.expand_dims(X, 2)-tf.transpose(Y))) 81 | return dist 82 | 83 | 84 | def KernelHoMM(Ho_Xs,Ho_Xt,sigma): 85 | dist_ss=Cal_pairwise_dist(Ho_Xs,Ho_Xs) 86 | dist_tt=Cal_pairwise_dist(Ho_Xt,Ho_Xt) 87 | dist_st=Cal_pairwise_dist(Ho_Xs,Ho_Xt) 88 | loss=tf.reduce_mean(tf.exp(-sigma*dist_ss))+tf.reduce_mean(tf.exp(-sigma*dist_tt))-2*tf.reduce_mean(tf.exp(-sigma*dist_st)) 89 | return tf.where(loss > 0, loss, 0) 90 | 91 | 92 | 93 | def SelectTargetSamples(features,logits,logits_threshold): 94 | target_softmax=tf.nn.softmax(logits) 95 | index = tf.where(tf.equal(tf.greater(target_softmax, logits_threshold), True))[:, 0] 96 | target_softmax = tf.gather(target_softmax, index) 97 | target_feature = tf.gather(features, index) 98 | return target_feature,target_softmax 99 | 100 | 101 | def mmatch(x1,x2,n_moments): 102 | mx1=tf.reduce_mean(x1,axis=0) 103 | mx2=tf.reduce_mean(x2,axis=0) 104 | sx1=x1-mx1 105 | sx2=x2-mx2 106 | dm=matchnorm(mx1,mx2) 107 | scms=dm 108 | for i in range(n_moments-1): 109 | scms+=scm(sx1,sx2,i+2) 110 | return scms 111 | 112 | def matchnorm(x1,x2): 113 | return tf.sqrt(tf.reduce_sum((x1-x2)**2)) 114 | 115 | def scm(sx1,sx2,k): 116 | ss1=tf.reduce_mean(sx1**k,axis=0) 117 | ss2=tf.reduce_mean(sx2**k,axis=0) 118 | return matchnorm(ss1,ss2) 119 | 120 | 121 | def Label2EdgeWeights(Label): 122 | Label=Label+1 123 | Label.astype("float32") 124 | n=Label.shape 125 | n=n[0] 126 | EdgeWeights=zeros((n,n)) 127 | Label=expand_dims(Label,axis=0) 128 | EdgeWeights=transpose(1.0/Label)*Label 129 | indx,indy=where(EdgeWeights!=1.0) 130 | EdgeWeights[indx,indy]=0. 131 | return EdgeWeights 132 | 133 | 134 | 135 | def symmetric_matrix_square_root(mat,eps=1e-8): 136 | s,u,v=tf.svd(mat) 137 | si=tf.where(tf.less(s,eps),s,tf.sqrt(s)) 138 | return tf.matmul(tf.matmul(u,tf.diag(si)),v,transpose_b=True) 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | -------------------------------------------------------------------------------- /HoMM_mnist/center_loss.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import tensorflow as tf 4 | 5 | def get_center_loss(features, labels, alpha, num_classes): 6 | len_features = features.get_shape()[1] 7 | # 建立一个Variable,shape为[num_classes, len_features],用于存储整个网络的样本中心, 8 | # 设置trainable=False是因为样本中心不是由梯度进行更新的 9 | centers = tf.get_variable('centers', [num_classes, len_features], dtype=tf.float32, 10 | initializer=tf.constant_initializer(0), trainable=False) 11 | # 将label展开为一维的,输入如果已经是一维的,则该动作其实无必要 12 | labels = tf.reshape(labels, [-1]) 13 | 14 | 15 | 16 | ############################################################## 17 | centers0=tf.unsorted_segment_mean(features,labels,num_classes) 18 | EdgeWeights=tf.ones((num_classes,num_classes))-tf.eye(num_classes) 19 | margin=tf.constant(100,dtype="float32") 20 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 21 | center_pairwise_dist = tf.transpose(norm(tf.expand_dims(centers0, 2) - tf.transpose(centers0))) 22 | loss_0= tf.reduce_sum(tf.multiply(tf.maximum(0.0, margin-center_pairwise_dist),EdgeWeights)) 23 | ########################################################################### 24 | 25 | 26 | 27 | # 根据样本label,获取mini-batch中每一个样本对应的中心值 28 | centers_batch = tf.gather(centers, labels) 29 | # 当前mini-batch的特征值与它们对应的中心值之间的差 30 | diff = centers_batch - features 31 | unique_label, unique_idx, unique_count = tf.unique_with_counts(labels) 32 | appear_times = tf.gather(unique_count, unique_idx) 33 | appear_times = tf.reshape(appear_times, [-1, 1]) 34 | diff = diff / tf.cast((1 + appear_times), tf.float32) 35 | diff = alpha * diff 36 | 37 | 38 | 39 | # 计算loss 40 | loss_1 = tf.nn.l2_loss(features - centers_batch) 41 | centers_update_op= tf.scatter_sub(centers, labels, diff) 42 | 43 | 44 | return loss_0, loss_1, centers_update_op 45 | -------------------------------------------------------------------------------- /HoMM_office/data/dslr.txt: -------------------------------------------------------------------------------- 1 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0001.jpg 0 2 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0002.jpg 0 3 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0003.jpg 0 4 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0004.jpg 0 5 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0005.jpg 0 6 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0006.jpg 0 7 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0007.jpg 0 8 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0008.jpg 0 9 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0009.jpg 0 10 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0010.jpg 0 11 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0011.jpg 0 12 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/back_pack/frame_0012.jpg 0 13 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0001.jpg 1 14 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0002.jpg 1 15 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0003.jpg 1 16 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0004.jpg 1 17 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0005.jpg 1 18 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0006.jpg 1 19 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0007.jpg 1 20 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0008.jpg 1 21 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0009.jpg 1 22 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0010.jpg 1 23 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0011.jpg 1 24 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0012.jpg 1 25 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0013.jpg 1 26 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0014.jpg 1 27 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0015.jpg 1 28 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0016.jpg 1 29 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0017.jpg 1 30 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0018.jpg 1 31 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0019.jpg 1 32 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0020.jpg 1 33 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike/frame_0021.jpg 1 34 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0001.jpg 2 35 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0002.jpg 2 36 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0003.jpg 2 37 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0004.jpg 2 38 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0005.jpg 2 39 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0006.jpg 2 40 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0007.jpg 2 41 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0008.jpg 2 42 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0009.jpg 2 43 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0010.jpg 2 44 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0011.jpg 2 45 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0012.jpg 2 46 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0013.jpg 2 47 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0014.jpg 2 48 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0015.jpg 2 49 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0016.jpg 2 50 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0017.jpg 2 51 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0018.jpg 2 52 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0019.jpg 2 53 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0020.jpg 2 54 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0021.jpg 2 55 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0022.jpg 2 56 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0023.jpg 2 57 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bike_helmet/frame_0024.jpg 2 58 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0001.jpg 3 59 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0002.jpg 3 60 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0003.jpg 3 61 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0004.jpg 3 62 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0005.jpg 3 63 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0006.jpg 3 64 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0007.jpg 3 65 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0008.jpg 3 66 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0009.jpg 3 67 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0010.jpg 3 68 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0011.jpg 3 69 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bookcase/frame_0012.jpg 3 70 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0001.jpg 4 71 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0002.jpg 4 72 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0003.jpg 4 73 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0004.jpg 4 74 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0005.jpg 4 75 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0006.jpg 4 76 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0007.jpg 4 77 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0008.jpg 4 78 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0009.jpg 4 79 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0010.jpg 4 80 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0011.jpg 4 81 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0012.jpg 4 82 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0013.jpg 4 83 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0014.jpg 4 84 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0015.jpg 4 85 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/bottle/frame_0016.jpg 4 86 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0001.jpg 5 87 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0002.jpg 5 88 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0003.jpg 5 89 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0004.jpg 5 90 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0005.jpg 5 91 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0006.jpg 5 92 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0007.jpg 5 93 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0008.jpg 5 94 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0009.jpg 5 95 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0010.jpg 5 96 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0011.jpg 5 97 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/calculator/frame_0012.jpg 5 98 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0001.jpg 6 99 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0002.jpg 6 100 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0003.jpg 6 101 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0004.jpg 6 102 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0005.jpg 6 103 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0006.jpg 6 104 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0007.jpg 6 105 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0008.jpg 6 106 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0009.jpg 6 107 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0010.jpg 6 108 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0011.jpg 6 109 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0012.jpg 6 110 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0013.jpg 6 111 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0014.jpg 6 112 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desktop_computer/frame_0015.jpg 6 113 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0001.jpg 7 114 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0002.jpg 7 115 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0003.jpg 7 116 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0004.jpg 7 117 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0005.jpg 7 118 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0006.jpg 7 119 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0007.jpg 7 120 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0008.jpg 7 121 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0009.jpg 7 122 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0010.jpg 7 123 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0011.jpg 7 124 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0012.jpg 7 125 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_chair/frame_0013.jpg 7 126 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0001.jpg 8 127 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0002.jpg 8 128 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0003.jpg 8 129 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0004.jpg 8 130 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0005.jpg 8 131 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0006.jpg 8 132 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0007.jpg 8 133 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0008.jpg 8 134 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0009.jpg 8 135 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0010.jpg 8 136 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0011.jpg 8 137 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0012.jpg 8 138 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0013.jpg 8 139 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/desk_lamp/frame_0014.jpg 8 140 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0001.jpg 9 141 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0002.jpg 9 142 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0003.jpg 9 143 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0004.jpg 9 144 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0005.jpg 9 145 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0006.jpg 9 146 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0007.jpg 9 147 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0008.jpg 9 148 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0009.jpg 9 149 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0010.jpg 9 150 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0011.jpg 9 151 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0012.jpg 9 152 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0013.jpg 9 153 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0014.jpg 9 154 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/file_cabinet/frame_0015.jpg 9 155 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0001.jpg 10 156 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0002.jpg 10 157 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0003.jpg 10 158 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0004.jpg 10 159 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0005.jpg 10 160 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0006.jpg 10 161 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0007.jpg 10 162 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0008.jpg 10 163 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0009.jpg 10 164 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0010.jpg 10 165 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0011.jpg 10 166 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0012.jpg 10 167 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/headphones/frame_0013.jpg 10 168 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0001.jpg 11 169 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0002.jpg 11 170 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0003.jpg 11 171 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0004.jpg 11 172 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0005.jpg 11 173 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0006.jpg 11 174 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0007.jpg 11 175 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0008.jpg 11 176 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0009.jpg 11 177 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/keyboard/frame_0010.jpg 11 178 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0001.jpg 12 179 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0002.jpg 12 180 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0003.jpg 12 181 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0004.jpg 12 182 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0005.jpg 12 183 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0006.jpg 12 184 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0007.jpg 12 185 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0008.jpg 12 186 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0009.jpg 12 187 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0010.jpg 12 188 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0011.jpg 12 189 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0012.jpg 12 190 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0013.jpg 12 191 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0014.jpg 12 192 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0015.jpg 12 193 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0016.jpg 12 194 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0017.jpg 12 195 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0018.jpg 12 196 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0019.jpg 12 197 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0020.jpg 12 198 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0021.jpg 12 199 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0022.jpg 12 200 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0023.jpg 12 201 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/laptop_computer/frame_0024.jpg 12 202 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0001.jpg 13 203 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0002.jpg 13 204 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0003.jpg 13 205 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0004.jpg 13 206 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0005.jpg 13 207 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0006.jpg 13 208 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0007.jpg 13 209 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0008.jpg 13 210 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0009.jpg 13 211 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0010.jpg 13 212 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0011.jpg 13 213 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0012.jpg 13 214 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0013.jpg 13 215 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0014.jpg 13 216 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0015.jpg 13 217 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/letter_tray/frame_0016.jpg 13 218 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0001.jpg 14 219 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0002.jpg 14 220 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0003.jpg 14 221 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0004.jpg 14 222 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0005.jpg 14 223 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0006.jpg 14 224 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0007.jpg 14 225 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0008.jpg 14 226 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0009.jpg 14 227 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0010.jpg 14 228 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0011.jpg 14 229 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0012.jpg 14 230 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0013.jpg 14 231 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0014.jpg 14 232 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0015.jpg 14 233 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0016.jpg 14 234 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0017.jpg 14 235 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0018.jpg 14 236 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0019.jpg 14 237 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0020.jpg 14 238 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0021.jpg 14 239 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0022.jpg 14 240 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0023.jpg 14 241 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0024.jpg 14 242 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0025.jpg 14 243 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0026.jpg 14 244 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0027.jpg 14 245 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0028.jpg 14 246 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0029.jpg 14 247 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0030.jpg 14 248 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mobile_phone/frame_0031.jpg 14 249 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0001.jpg 15 250 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0002.jpg 15 251 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0003.jpg 15 252 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0004.jpg 15 253 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0005.jpg 15 254 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0006.jpg 15 255 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0007.jpg 15 256 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0008.jpg 15 257 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0009.jpg 15 258 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0010.jpg 15 259 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0011.jpg 15 260 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0012.jpg 15 261 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0013.jpg 15 262 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0014.jpg 15 263 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0015.jpg 15 264 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0016.jpg 15 265 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0017.jpg 15 266 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0018.jpg 15 267 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0019.jpg 15 268 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0020.jpg 15 269 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0021.jpg 15 270 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/monitor/frame_0022.jpg 15 271 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0001.jpg 16 272 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0002.jpg 16 273 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0003.jpg 16 274 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0004.jpg 16 275 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0005.jpg 16 276 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0006.jpg 16 277 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0007.jpg 16 278 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0008.jpg 16 279 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0009.jpg 16 280 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0010.jpg 16 281 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0011.jpg 16 282 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mouse/frame_0012.jpg 16 283 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0001.jpg 17 284 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0002.jpg 17 285 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0003.jpg 17 286 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0004.jpg 17 287 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0005.jpg 17 288 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0006.jpg 17 289 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0007.jpg 17 290 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/mug/frame_0008.jpg 17 291 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0001.jpg 18 292 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0002.jpg 18 293 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0003.jpg 18 294 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0004.jpg 18 295 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0005.jpg 18 296 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0006.jpg 18 297 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0007.jpg 18 298 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0008.jpg 18 299 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0009.jpg 18 300 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/paper_notebook/frame_0010.jpg 18 301 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0001.jpg 19 302 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0002.jpg 19 303 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0003.jpg 19 304 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0004.jpg 19 305 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0005.jpg 19 306 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0006.jpg 19 307 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0007.jpg 19 308 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0008.jpg 19 309 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0009.jpg 19 310 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/pen/frame_0010.jpg 19 311 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0001.jpg 20 312 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0002.jpg 20 313 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0003.jpg 20 314 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0004.jpg 20 315 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0005.jpg 20 316 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0006.jpg 20 317 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0007.jpg 20 318 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0008.jpg 20 319 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0009.jpg 20 320 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0010.jpg 20 321 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0011.jpg 20 322 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0012.jpg 20 323 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/phone/frame_0013.jpg 20 324 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0001.jpg 21 325 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0002.jpg 21 326 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0003.jpg 21 327 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0004.jpg 21 328 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0005.jpg 21 329 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0006.jpg 21 330 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0007.jpg 21 331 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0008.jpg 21 332 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0009.jpg 21 333 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0010.jpg 21 334 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0011.jpg 21 335 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0012.jpg 21 336 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0013.jpg 21 337 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0014.jpg 21 338 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/printer/frame_0015.jpg 21 339 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0001.jpg 22 340 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0002.jpg 22 341 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0003.jpg 22 342 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0004.jpg 22 343 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0005.jpg 22 344 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0006.jpg 22 345 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0007.jpg 22 346 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0008.jpg 22 347 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0009.jpg 22 348 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0010.jpg 22 349 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0011.jpg 22 350 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0012.jpg 22 351 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0013.jpg 22 352 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0014.jpg 22 353 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0015.jpg 22 354 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0016.jpg 22 355 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0017.jpg 22 356 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0018.jpg 22 357 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0019.jpg 22 358 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0020.jpg 22 359 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0021.jpg 22 360 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0022.jpg 22 361 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/projector/frame_0023.jpg 22 362 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0001.jpg 23 363 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0002.jpg 23 364 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0003.jpg 23 365 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0004.jpg 23 366 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0005.jpg 23 367 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0006.jpg 23 368 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0007.jpg 23 369 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0008.jpg 23 370 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0009.jpg 23 371 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0010.jpg 23 372 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0011.jpg 23 373 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0012.jpg 23 374 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0013.jpg 23 375 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0014.jpg 23 376 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0015.jpg 23 377 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0016.jpg 23 378 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0017.jpg 23 379 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/punchers/frame_0018.jpg 23 380 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0001.jpg 24 381 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0002.jpg 24 382 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0003.jpg 24 383 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0004.jpg 24 384 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0005.jpg 24 385 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0006.jpg 24 386 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0007.jpg 24 387 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0008.jpg 24 388 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0009.jpg 24 389 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ring_binder/frame_0010.jpg 24 390 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0001.jpg 25 391 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0002.jpg 25 392 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0003.jpg 25 393 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0004.jpg 25 394 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0005.jpg 25 395 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0006.jpg 25 396 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/ruler/frame_0007.jpg 25 397 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0001.jpg 26 398 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0002.jpg 26 399 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0003.jpg 26 400 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0004.jpg 26 401 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0005.jpg 26 402 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0006.jpg 26 403 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0007.jpg 26 404 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0008.jpg 26 405 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0009.jpg 26 406 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0010.jpg 26 407 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0011.jpg 26 408 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0012.jpg 26 409 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0013.jpg 26 410 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0014.jpg 26 411 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0015.jpg 26 412 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0016.jpg 26 413 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0017.jpg 26 414 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/scissors/frame_0018.jpg 26 415 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0001.jpg 27 416 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0002.jpg 27 417 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0003.jpg 27 418 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0004.jpg 27 419 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0005.jpg 27 420 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0006.jpg 27 421 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0007.jpg 27 422 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0008.jpg 27 423 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0009.jpg 27 424 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0010.jpg 27 425 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0011.jpg 27 426 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0012.jpg 27 427 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0013.jpg 27 428 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0014.jpg 27 429 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0015.jpg 27 430 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0016.jpg 27 431 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0017.jpg 27 432 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0018.jpg 27 433 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0019.jpg 27 434 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0020.jpg 27 435 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0021.jpg 27 436 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0022.jpg 27 437 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0023.jpg 27 438 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0024.jpg 27 439 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0025.jpg 27 440 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/speaker/frame_0026.jpg 27 441 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0001.jpg 28 442 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0002.jpg 28 443 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0003.jpg 28 444 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0004.jpg 28 445 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0005.jpg 28 446 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0006.jpg 28 447 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0007.jpg 28 448 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0008.jpg 28 449 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0009.jpg 28 450 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0010.jpg 28 451 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0011.jpg 28 452 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0012.jpg 28 453 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0013.jpg 28 454 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0014.jpg 28 455 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0015.jpg 28 456 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0016.jpg 28 457 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0017.jpg 28 458 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0018.jpg 28 459 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0019.jpg 28 460 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0020.jpg 28 461 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/stapler/frame_0021.jpg 28 462 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0001.jpg 29 463 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0002.jpg 29 464 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0003.jpg 29 465 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0004.jpg 29 466 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0005.jpg 29 467 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0006.jpg 29 468 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0007.jpg 29 469 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0008.jpg 29 470 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0009.jpg 29 471 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0010.jpg 29 472 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0011.jpg 29 473 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0012.jpg 29 474 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0013.jpg 29 475 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0014.jpg 29 476 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0015.jpg 29 477 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0016.jpg 29 478 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0017.jpg 29 479 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0018.jpg 29 480 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0019.jpg 29 481 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0020.jpg 29 482 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0021.jpg 29 483 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/tape_dispenser/frame_0022.jpg 29 484 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0001.jpg 30 485 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0002.jpg 30 486 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0003.jpg 30 487 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0004.jpg 30 488 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0005.jpg 30 489 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0006.jpg 30 490 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0007.jpg 30 491 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0008.jpg 30 492 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0009.jpg 30 493 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0010.jpg 30 494 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0011.jpg 30 495 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0012.jpg 30 496 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0013.jpg 30 497 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0014.jpg 30 498 | /ext/czh/tensorflow-cnn-finetune-master/office31/dslr/images/trash_can/frame_0015.jpg 30 499 | -------------------------------------------------------------------------------- /HoMM_office/resnet/Utils.py: -------------------------------------------------------------------------------- 1 | import random 2 | import tensorflow as tf 3 | from functools import partial 4 | 5 | 6 | 7 | def shuffle(Data, Label): 8 | ind = range(Data.shape[0]) 9 | random.shuffle(ind) 10 | Data = Data[ind, :, :, :] 11 | Label = Label[ind, :] 12 | return Data, Label 13 | 14 | def compute_pairwise_distances(x, y): 15 | if not len(x.get_shape()) == len(y.get_shape()) == 2: 16 | raise ValueError('Both inputs should be matrices.') 17 | if x.get_shape().as_list()[1] != y.get_shape().as_list()[1]: 18 | raise ValueError('The number of features should be the same.') 19 | 20 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 21 | return tf.transpose(norm(tf.expand_dims(x, 2) - tf.transpose(y))) 22 | 23 | 24 | def gaussian_kernel_matrix(x, y, sigmas): 25 | beta = 1. / (2. * (tf.expand_dims(sigmas, 1))) 26 | dist = compute_pairwise_distances(x, y) 27 | s = tf.matmul(beta, tf.reshape(dist, (1, -1))) 28 | return tf.reshape(tf.reduce_sum(tf.exp(-s), 0), tf.shape(dist)) 29 | 30 | 31 | def maximum_mean_discrepancy(x, y, kernel=gaussian_kernel_matrix): 32 | cost = tf.reduce_mean(kernel(x, x)) 33 | cost += tf.reduce_mean(kernel(y, y)) 34 | cost -= 2 * tf.reduce_mean(kernel(x, y)) 35 | # We do not allow the loss to become negative. 36 | cost = tf.where(cost > 0, cost, 0, name='value') 37 | return cost 38 | 39 | 40 | def KMMD(Xs,Xt): 41 | # sigmas=[1e-2,0.1,1,5,10,20,25,30,35,100] 42 | # guassian_kernel=partial(kernel,sigmas=tf.constant(sigmas)) 43 | # cost = tf.reduce_mean(guassian_kernel(Xs, Xs)) 44 | # cost += tf.reduce_mean(guassian_kernel(Xt, Xt)) 45 | # cost -= 2 * tf.reduce_mean(guassian_kernel(Xs, Xt)) 46 | # cost = tf.where(cost > 0, cost, 0) 47 | 48 | sigmas = [1e6, 5e5, 3e5, 1e5, 5e4, 3e4, 1e4, 5e3,3e3] 49 | gaussian_kernel = partial(gaussian_kernel_matrix, sigmas=tf.constant(sigmas)) 50 | cost= maximum_mean_discrepancy(Xs, Xt, kernel=gaussian_kernel) 51 | return cost 52 | 53 | def kernel(X, Y, sigmas): 54 | beta = 1.0/(2.0 * (tf.expand_dims(sigmas,1))) 55 | dist = Cal_pairwise_dist(X,Y) 56 | s = tf.matmul(beta, tf.reshape(dist,(1,-1))) 57 | return tf.reshape(tf.reduce_sum(tf.exp(-s), 0), tf.shape(dist)) 58 | 59 | 60 | 61 | def Cal_pairwise_dist(X,Y): 62 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 63 | dist= tf.transpose(norm(tf.expand_dims(X, 2)-tf.transpose(Y))) 64 | return dist 65 | 66 | 67 | 68 | def KernelHoMM(Ho_Xs,Ho_Xt,sigma): 69 | dist_ss=Cal_pairwise_dist(Ho_Xs,Ho_Xs) 70 | dist_tt=Cal_pairwise_dist(Ho_Xt,Ho_Xt) 71 | dist_st=Cal_pairwise_dist(Ho_Xs,Ho_Xt) 72 | loss=tf.reduce_mean(tf.exp(-sigma*dist_ss))+tf.reduce_mean(tf.exp(-sigma*dist_tt))-2*tf.reduce_mean(tf.exp(-sigma*dist_st)) 73 | return tf.where(loss > 0, loss, 0) 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /HoMM_office/resnet/Utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/HoMM_office/resnet/Utils.pyc -------------------------------------------------------------------------------- /HoMM_office/resnet/center_loss.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | import tensorflow as tf 4 | 5 | def get_center_loss(features, labels, alpha, num_classes): 6 | len_features = features.get_shape()[1] 7 | centers = tf.get_variable('centers', [num_classes, len_features], dtype=tf.float32,initializer=tf.constant_initializer(0), trainable=False) 8 | labels = tf.reshape(labels, [-1]) 9 | 10 | 11 | ############################################################## 12 | centers0=tf.unsorted_segment_mean(features,labels,num_classes) 13 | EdgeWeights=tf.ones((num_classes,num_classes))-tf.eye(num_classes) 14 | margin=tf.constant(100,dtype="float32") 15 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 16 | center_pairwise_dist = tf.transpose(norm(tf.expand_dims(centers0, 2) - tf.transpose(centers0))) 17 | loss_0= tf.reduce_sum(tf.multiply(tf.maximum(0.0, margin-center_pairwise_dist),EdgeWeights)) 18 | ########################################################################### 19 | 20 | 21 | 22 | # 根据样本label,获取mini-batch中每一个样本对应的中心值 23 | centers_batch = tf.gather(centers, labels) 24 | # 当前mini-batch的特征值与它们对应的中心值之间的差 25 | diff = centers_batch - features 26 | unique_label, unique_idx, unique_count = tf.unique_with_counts(labels) 27 | appear_times = tf.gather(unique_count, unique_idx) 28 | appear_times = tf.reshape(appear_times, [-1, 1]) 29 | diff = diff / tf.cast((1 + appear_times), tf.float32) 30 | diff = alpha * diff 31 | 32 | 33 | 34 | # 计算loss 35 | loss_1 = tf.nn.l2_loss(features - centers_batch) 36 | centers_update_op= tf.scatter_sub(centers, labels, diff) 37 | 38 | 39 | return loss_0, loss_1, centers_update_op, centers 40 | -------------------------------------------------------------------------------- /HoMM_office/resnet/download_weights.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # By using curl 4 | curl -O https://deniz.co/tensorflow-cnn-finetune/ResNet-L50.npy 5 | curl -O https://deniz.co/tensorflow-cnn-finetune/ResNet-L101.npy 6 | curl -O https://deniz.co/tensorflow-cnn-finetune/ResNet-L152.npy 7 | 8 | # or by using wget 9 | # wget https://www.dropbox.com/s/txb16f1khleyvdh/ResNet-L50.npy?dl=1 10 | # wget https://www.dropbox.com/s/8w10cs6v4rd9616/ResNet-L101.npy?dl=1 11 | # wget https://www.dropbox.com/s/n5inup5a7fi8lom/ResNet-L152.npy?dl=1 12 | -------------------------------------------------------------------------------- /HoMM_office/resnet/examples.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | python finetune.py \ 4 | --learning_rate "0.0001" \ 5 | --train_layers "fc" 6 | 7 | python finetune.py \ 8 | --learning_rate "0.00001" \ 9 | --train_layers "fc,scale5/block3" 10 | 11 | python finetune.py \ 12 | --learning_rate "0.00001" \ 13 | --train_layers "fc,scale5/block3,scale5/block2" 14 | 15 | python finetune.py \ 16 | --learning_rate "0.00001" \ 17 | --train_layers "fc,scale5/block3,scale5/block2,scale5/block1" 18 | 19 | python finetune.py \ 20 | --learning_rate "0.00001" \ 21 | --multi_scale "225,256" \ 22 | --train_layers "fc,scale5" 23 | 24 | python finetune.py \ 25 | --learning_rate "0.00001" \ 26 | --multi_scale "225,256" \ 27 | --train_layers "fc,scale5,scale4/block6" 28 | 29 | python finetune.py \ 30 | --learning_rate "0.00001" \ 31 | --multi_scale "225,256" \ 32 | --train_layers "fc,scale5,scale4/block6,scale4/block5" 33 | -------------------------------------------------------------------------------- /HoMM_office/resnet/finetune.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | import numpy as np 3 | import tensorflow as tf 4 | import datetime 5 | from model import ResNetModel 6 | sys.path.insert(0, '../utils') 7 | from preprocessor import BatchPreprocessor 8 | os.environ['CUDA_VISIBLE_DEVICES']='0' 9 | from Utils import * 10 | from center_loss import * 11 | CONV_WEIGHT_DECAY = 0.00004 12 | CONV_WEIGHT_STDDEV = 0.1 13 | MOVING_AVERAGE_DECAY = 0.9997 14 | BN_DECAY = MOVING_AVERAGE_DECAY 15 | BN_EPSILON = 0.0001 16 | UPDATE_OPS_COLLECTION = 'resnet_update_ops' 17 | FC_WEIGHT_STDDEV = 0.01 18 | 19 | 20 | tf.app.flags.DEFINE_float('learning_rate', 0.00003, 'Learning rate for adam optimizer') 21 | tf.app.flags.DEFINE_integer('resnet_depth', 50, 'ResNet architecture to be used: 50, 101 or 152') 22 | tf.app.flags.DEFINE_integer('num_epochs',1000, 'Number of epochs for training') 23 | tf.app.flags.DEFINE_integer('num_classes',31, 'Number of classes') 24 | tf.app.flags.DEFINE_integer('batch_size', 70, 'Batch size') 25 | tf.app.flags.DEFINE_string('train_layers', "adapt,fc,scale5/block3", 'Finetuning layers, seperated by commas') #scale5/block3 26 | tf.app.flags.DEFINE_string('multi_scale', '', 'As preprocessing; scale the image randomly between 2 numbers and crop randomly at network\'s input size') 27 | tf.app.flags.DEFINE_string('training_file', '../data/amazon.txt', 'Training dataset file') 28 | tf.app.flags.DEFINE_string('val_file', '../data/webcam.txt', 'Validation dataset file') 29 | tf.app.flags.DEFINE_string('tensorboard_root_dir', '../training', 'Root directory to put the training logs and weights') 30 | tf.app.flags.DEFINE_integer('log_step', 10, 'Logging period in terms of iteration') 31 | 32 | FLAGS = tf.app.flags.FLAGS 33 | 34 | 35 | def main(_): 36 | # Create training directories 37 | now = datetime.datetime.now() 38 | train_dir_name = now.strftime('resnet_%Y%m%d_%H%M%S') 39 | train_dir = os.path.join(FLAGS.tensorboard_root_dir, train_dir_name) 40 | checkpoint_dir = os.path.join(train_dir, 'checkpoint') 41 | tensorboard_dir = os.path.join(train_dir, 'tensorboard') 42 | tensorboard_train_dir = os.path.join(tensorboard_dir, 'train') 43 | tensorboard_val_dir = os.path.join(tensorboard_dir, 'val') 44 | 45 | if not os.path.isdir(FLAGS.tensorboard_root_dir): os.mkdir(FLAGS.tensorboard_root_dir) 46 | if not os.path.isdir(train_dir): os.mkdir(train_dir) 47 | if not os.path.isdir(checkpoint_dir): os.mkdir(checkpoint_dir) 48 | if not os.path.isdir(tensorboard_dir): os.mkdir(tensorboard_dir) 49 | if not os.path.isdir(tensorboard_train_dir): os.mkdir(tensorboard_train_dir) 50 | if not os.path.isdir(tensorboard_val_dir): os.mkdir(tensorboard_val_dir) 51 | 52 | # Write flags to txt 53 | flags_file_path = os.path.join(train_dir, 'flags.txt') 54 | flags_file = open(flags_file_path, 'w') 55 | flags_file.write('learning_rate={}\n'.format(FLAGS.learning_rate)) 56 | flags_file.write('resnet_depth={}\n'.format(FLAGS.resnet_depth)) 57 | flags_file.write('num_epochs={}\n'.format(FLAGS.num_epochs)) 58 | flags_file.write('batch_size={}\n'.format(FLAGS.batch_size)) 59 | flags_file.write('train_layers={}\n'.format(FLAGS.train_layers)) 60 | flags_file.write('multi_scale={}\n'.format(FLAGS.multi_scale)) 61 | 62 | 63 | flags_file.write('tensorboard_root_dir={}\n'.format(FLAGS.tensorboard_root_dir)) 64 | flags_file.write('log_step={}\n'.format(FLAGS.log_step)) 65 | flags_file.close() 66 | 67 | # Placeholders 68 | source = tf.placeholder(tf.float32, [FLAGS.batch_size, 224, 224, 3]) 69 | target = tf.placeholder(tf.float32, [FLAGS.batch_size, 224, 224, 3]) 70 | y = tf.placeholder(tf.float32, [None, FLAGS.num_classes]) 71 | is_training = tf.placeholder('bool', []) 72 | dropout_rate=tf.placeholder(dtype=tf.float32,shape=None) 73 | 74 | domain_loss_param=tf.get_variable(name="domain_loss_param",dtype=tf.float32,initializer=tf.constant(1.0),trainable=False) 75 | target_loss_param=tf.get_variable(name='target_loss_param',dtype=tf.float32,initializer=tf.constant(0.0),trainable=False) 76 | logits_threshold=tf.get_variable(name='logits_threshold',dtype=tf.float32,initializer=tf.constant(0.0),trainable=False) 77 | ring_norm = tf.get_variable(name="fc/ring_norm", shape=None, dtype=tf.float32, initializer=tf.constant(100.0),trainable=False) 78 | clustering_param=tf.get_variable(name='Ortho_loss_param',dtype=tf.float32,initializer=tf.constant(0.0),trainable=False) 79 | 80 | # Model 81 | train_layers = FLAGS.train_layers.split(',') 82 | source_model = ResNetModel(source,is_training, depth=FLAGS.resnet_depth, dropout_rate=dropout_rate,num_classes=FLAGS.num_classes) 83 | target_model = ResNetModel(target,is_training,reuse=True, depth=FLAGS.resnet_depth, dropout_rate=dropout_rate,num_classes=FLAGS.num_classes) 84 | # fc_weights=tf.get_default_graph().get_tensor_by_name("fc/weights:0") 85 | # Orthogonal_regularizer=tf.reduce_mean(tf.norm(tf.matmul(tf.transpose(fc_weights),fc_weights)-tf.eye(FLAGS.num_classes),ord=2)) 86 | # Grad_loss=GradRegularization(target_model.prob,target_model.avg_pool) 87 | 88 | ### Calculating the loss function 89 | 90 | cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=source_model.prob, labels=y) 91 | cross_entropy_mean = tf.reduce_mean(cross_entropy) 92 | regularization_losses = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) 93 | source_loss = cross_entropy_mean+1.0*regularization_losses 94 | domain_loss= HoMM(source_model.adapt,target_model.adapt,order=4,num=300000) 95 | target_loss=Target_loss(tf.nn.softmax(target_model.prob),logits_threshold) 96 | centers_update_op,discriminative_loss,source_centers=CenterBased(source_model.adapt,y) 97 | target_feature,target_logits=SelectTargetSamples(target_model.adapt,target_model.prob,logits_threshold=0.75) 98 | target_predict_label=tf.argmax(target_logits,axis=1) 99 | target_pseudo_label=tf.one_hot(target_predict_label,FLAGS.num_classes) 100 | with tf.variable_scope('target'): 101 | centers_update_op_1, discriminative_clustering,target_centers = CenterBased(target_feature, target_pseudo_label) 102 | # class_domain_loss=AlignCenter(centers_update_op,centers_update_op_1) 103 | ring_loss = Cal_RingLoss(ring_norm,source_model.avg_pool,target_model.avg_pool) 104 | 105 | 106 | # office 1000 0.01 0.0003 ## office-Home 1000 0.01 0.001 107 | loss=source_loss+200*domain_loss_param*domain_loss+clustering_param*discriminative_clustering 108 | 109 | 110 | 111 | 112 | # train_op = model.optimize(FLAGS.learning_rate, train_layers) 113 | Varall=tf.trainable_variables() 114 | # print(Varall) 115 | trainable_var_names = ['weights', 'biases', 'beta', 'gamma','adapt'] 116 | # var_list_1 = [v for v in tf.trainable_variables() if v.name.split(':')[0].split('/')[-1] in trainable_var_names and contains(v.name, train_layers)] 117 | var_list_1 = [var for var in tf.trainable_variables() if 'scale5/block3' in var.name] 118 | var_list_2=[var for var in tf.trainable_variables() if 'fc' in var.name or 'adapt' in var.name] 119 | var_list_3 = [var for var in tf.trainable_variables() if 'scale5/block2' in var.name] 120 | 121 | Varall = tf.trainable_variables() 122 | optimizer1 = tf.train.AdamOptimizer(FLAGS.learning_rate) 123 | optimizer2 = tf.train.AdamOptimizer(learning_rate=0.0003) 124 | # optimizer3 = tf.train.AdamOptimizer(learning_rate=0.000005) 125 | 126 | update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) 127 | with tf.control_dependencies(update_ops): 128 | with tf.control_dependencies([centers_update_op,centers_update_op_1]): 129 | op1 = optimizer1.minimize(loss,var_list=var_list_1) 130 | op2 = optimizer2.minimize(loss,var_list=var_list_2) 131 | # op3 = optimizer3.minimize(loss,var_list=var_list_3) 132 | train_op=tf.group(op1,op2) 133 | 134 | 135 | 136 | # Training accuracy of the model 137 | correct_pred = tf.equal(tf.argmax(source_model.prob, 1), tf.argmax(y, 1)) 138 | accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) 139 | 140 | # Summaries 141 | tf.summary.scalar('train_loss', loss) 142 | tf.summary.scalar('train_accuracy', accuracy) 143 | merged_summary = tf.summary.merge_all() 144 | 145 | train_writer = tf.summary.FileWriter(tensorboard_train_dir) 146 | val_writer = tf.summary.FileWriter(tensorboard_val_dir) 147 | saver = tf.train.Saver() 148 | 149 | # Batch preprocessors 150 | multi_scale = FLAGS.multi_scale.split(',') 151 | if len(multi_scale) == 2: 152 | multi_scale = [int(multi_scale[0]), int(multi_scale[1])] 153 | else: 154 | multi_scale = None 155 | 156 | train_preprocessor = BatchPreprocessor(dataset_file_path=FLAGS.training_file, num_classes=FLAGS.num_classes, 157 | output_size=[224, 224], horizontal_flip=False, shuffle=True, multi_scale=multi_scale) 158 | 159 | target_preprocessor = BatchPreprocessor(dataset_file_path='../data/webcam.txt', num_classes=FLAGS.num_classes,output_size=[224, 224],shuffle=True) 160 | 161 | val_preprocessor = BatchPreprocessor(dataset_file_path=FLAGS.val_file, num_classes=FLAGS.num_classes, output_size=[224, 224]) 162 | 163 | # Get the number of training/validation steps per epoch 164 | train_batches_per_epoch = np.floor(len(train_preprocessor.labels) / FLAGS.batch_size).astype(np.int16) 165 | target_batches_per_epoch = np.floor(len(target_preprocessor.labels) / FLAGS.batch_size).astype(np.int16) 166 | val_batches_per_epoch = np.floor(len(val_preprocessor.labels) / FLAGS.batch_size).astype(np.int16) 167 | 168 | # train_batches_per_epoch=np.minimum(train_batches_per_epoch,target_batches_per_epoch) 169 | with tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))) as sess: 170 | varall=tf.trainable_variables() 171 | sess.run(tf.global_variables_initializer()) 172 | train_writer.add_graph(sess.graph) 173 | # Load the pretrained weights 174 | source_model.load_original_weights(sess, skip_layers=train_layers) 175 | # target_model.load_original_weights(sess, skip_layers=train_layers) 176 | 177 | # Directly restore (your model should be exactly the same with checkpoint) 178 | # saver.restore(sess, "/Users/dgurkaynak/Projects/marvel-training/alexnet64-fc6/model_epoch10.ckpt") 179 | 180 | print("{} Start training...".format(datetime.datetime.now())) 181 | print("{} Open Tensorboard at --logdir {}".format(datetime.datetime.now(), tensorboard_dir)) 182 | 183 | 184 | Acc_convergency=[] 185 | for epoch in range(FLAGS.num_epochs): 186 | print("{} Epoch number: {}".format(datetime.datetime.now(), epoch+1)) 187 | step = 1 188 | # Start training 189 | while step < train_batches_per_epoch: 190 | if step%target_batches_per_epoch==0: 191 | target_preprocessor.reset_pointer() 192 | batch_xs, batch_ys = train_preprocessor.next_batch(FLAGS.batch_size) 193 | batch_xt, batch_yt = target_preprocessor.next_batch(FLAGS.batch_size) 194 | TotalLoss, SourceLoss, DomainLoss, TargetLoss, RingLoss, _=sess.run( 195 | fetches=[loss, source_loss, domain_loss, target_loss, ring_loss, train_op], 196 | feed_dict={source: batch_xs,target:batch_xt, y: batch_ys, is_training: True,dropout_rate:1.0}) 197 | 198 | ############################ print loss ################################################## 199 | print "Loss={} ### SourceLoss={} ### DomainLoss={} ### TargetLoss={} ### RingLoss={}".format(TotalLoss, SourceLoss, DomainLoss, TargetLoss, RingLoss) 200 | 201 | # Logging 202 | # if step % FLAGS.log_step == 0: 203 | # s = sess.run(merged_summary, feed_dict={source: batch_xs, y: batch_ys, is_training: False}) 204 | # train_writer.add_summary(s, epoch * train_batches_per_epoch + step) 205 | 206 | step += 1 207 | 208 | if epoch % 3 == 0 : 209 | 210 | # Epoch completed, start validation 211 | print("{} Start validation".format(datetime.datetime.now())) 212 | test_acc = 0. 213 | test_count = 0 214 | 215 | for _ in range(val_batches_per_epoch): 216 | batch_tx, batch_ty = val_preprocessor.next_batch(FLAGS.batch_size) 217 | acc= sess.run(accuracy, feed_dict={source: batch_tx, y: batch_ty, is_training: False,dropout_rate:1.0}) 218 | test_acc += acc 219 | test_count += 1 220 | 221 | test_acc /= test_count 222 | s = tf.Summary(value=[tf.Summary.Value(tag="validation_accuracy", simple_value=test_acc)]) 223 | val_writer.add_summary(s, epoch+1) 224 | print("{} Validation Accuracy = {:.4f}".format(datetime.datetime.now(), test_acc)) 225 | Acc_convergency.append(test_acc) 226 | print Acc_convergency 227 | 228 | 229 | if epoch==100: 230 | sess.run(tf.assign(clustering_param, 0.0)) 231 | 232 | 233 | 234 | 235 | 236 | 237 | # Reset the dataset pointers 238 | val_preprocessor.reset_pointer() 239 | train_preprocessor.reset_pointer() 240 | target_preprocessor.reset_pointer() 241 | 242 | ####################### log the convergency data####################### 243 | savedata = np.array(Acc_convergency) 244 | np.save("AtoD_SDDA_Source.npy", savedata) 245 | # 246 | # print("{} Saving checkpoint of model...".format(datetime.datetime.now())) 247 | # 248 | # #save checkpoint of the model 249 | # checkpoint_path = os.path.join(checkpoint_dir, 'model_epoch'+str(epoch+1)+'.ckpt') 250 | # save_path = saver.save(sess, checkpoint_path) 251 | # 252 | # print("{} Model checkpoint saved at {}".format(datetime.datetime.now(), checkpoint_path)) 253 | 254 | def contains(target_str, search_arr): 255 | rv = False 256 | 257 | for search_str in search_arr: 258 | if search_str in target_str: 259 | rv = True 260 | break 261 | 262 | return rv 263 | 264 | def CORAL(h_src,h_trg,batchsize=128, gamma=1e-3): 265 | # regularized covariances (D-Coral is not regularized actually..) 266 | # First: subtract the mean from the data matrix 267 | batch_size = batchsize 268 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 269 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 270 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src,transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 271 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg,transpose_a=True) # + gamma * tf.eye(self.hidden_repr_size) 272 | # Returns the Frobenius norm (there is an extra 1/4 in D-Coral actually) 273 | # The reduce_mean account for the factor 1/d^2 274 | return tf.reduce_mean(tf.square(tf.subtract(cov_source, cov_target))) 275 | 276 | def SqrtCORAL(h_src,h_trg): 277 | batch_size = 160.0 278 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 279 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 280 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src, transpose_a=True) 281 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, transpose_a=True) 282 | sqrt_cov_source = CovSqrt(cov_source) 283 | sqrt_cov_target = CovSqrt(cov_target) 284 | return tf.reduce_mean(tf.square(tf.subtract(sqrt_cov_source, sqrt_cov_target))) 285 | 286 | 287 | 288 | def HoMM3(h_src, h_trg): 289 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 290 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 291 | xs=tf.expand_dims(h_src,axis=-1) 292 | xs = tf.expand_dims(xs, axis=-1) 293 | xt = tf.expand_dims(h_trg, axis=-1) 294 | xt = tf.expand_dims(xt, axis=-1) 295 | xs_1=tf.transpose(xs,[0,2,1,3]) 296 | xs_2 = tf.transpose(xs, [0, 2, 3, 1]) 297 | xt_1 = tf.transpose(xt, [0, 2, 1, 3]) 298 | xt_2 = tf.transpose(xt, [0, 2, 3, 1]) 299 | HR_Xs=xs*xs_1*xs_2 300 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 301 | HR_Xt = xt * xt_1 * xt_2 302 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 303 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 304 | 305 | 306 | def HoMM4(xs,xt): 307 | ind=tf.range(tf.cast(xs.shape[1],tf.int32)) 308 | ind=tf.random_shuffle(ind) 309 | xs=tf.transpose(xs,[1,0]) 310 | xs=tf.gather(xs,ind) 311 | xs=tf.transpose(xs,[1,0]) 312 | xt = tf.transpose(xt, [1, 0]) 313 | xt=tf.gather(xt,ind) 314 | xt=tf.transpose(xt,[1,0]) 315 | return HoMM4_loss(xs[:,0:30], xt[:,0:30])+HoMM4_loss(xs[:,30:60], xt[:,30:60])+HoMM4_loss(xs[:,60:90], xt[:,60:90])\ 316 | +HoMM4_loss(xs[:,90:120], xt[:,90:120])+HoMM4_loss(xs[:,120:150], xt[:,120:150])+HoMM4_loss(xs[:,150:180], xt[:,150:180]) 317 | 318 | 319 | 320 | 321 | def HoMM4_loss(xs, xt): 322 | xs = xs - tf.reduce_mean(xs, axis=0) 323 | xt = xt - tf.reduce_mean(xt, axis=0) 324 | xs = tf.expand_dims(xs,axis=-1) 325 | xs = tf.expand_dims(xs, axis=-1) 326 | xs = tf.expand_dims(xs, axis=-1) 327 | xt = tf.expand_dims(xt, axis=-1) 328 | xt = tf.expand_dims(xt, axis=-1) 329 | xt = tf.expand_dims(xt, axis=-1) 330 | xs_1 = tf.transpose(xs,[0,2,1,3,4]) 331 | xs_2 = tf.transpose(xs, [0, 2, 3, 1,4]) 332 | xs_3 = tf.transpose(xs, [0, 2, 3, 4, 1]) 333 | xt_1 = tf.transpose(xt, [0, 2, 1, 3,4]) 334 | xt_2 = tf.transpose(xt, [0, 2, 3, 1,4]) 335 | xt_3 = tf.transpose(xt, [0, 2, 3, 4, 1]) 336 | HR_Xs=xs*xs_1*xs_2*xs_3 337 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) 338 | HR_Xt = xt * xt_1 * xt_2*xt_3 339 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 340 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 341 | 342 | 343 | 344 | ## high-order moment matching 345 | def HoMM(xs,xt,order=4,num=100000): 346 | xs = xs - tf.reduce_mean(xs, axis=0) 347 | xt = xt - tf.reduce_mean(xt, axis=0) 348 | dim=tf.cast(xs.shape[1], tf.int32) 349 | index=tf.random_uniform(shape=(num,dim),minval=0,maxval=dim-1,dtype=tf.int32,seed=0) 350 | index=index[:,:order] 351 | xs=tf.transpose(xs) 352 | xs=tf.gather(xs,index) ##dim=[num,order,batchsize] 353 | xt = tf.transpose(xt) 354 | xt = tf.gather(xt, index) 355 | HO_Xs=tf.reduce_prod(xs,axis=1) 356 | HO_Xs=tf.reduce_mean(HO_Xs,axis=1) 357 | HO_Xt = tf.reduce_prod(xt, axis=1) 358 | HO_Xt = tf.reduce_mean(HO_Xt, axis=1) 359 | return tf.reduce_mean(tf.square(tf.subtract(HO_Xs, HO_Xt))) 360 | 361 | 362 | 363 | 364 | #### 365 | def KHoMM(xs,xt,order=4,num=30000): 366 | xs = xs - tf.reduce_mean(xs, axis=0) 367 | xt = xt - tf.reduce_mean(xt, axis=0) 368 | dim = tf.cast(xs.shape[1], tf.int32) 369 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32, seed=0) 370 | index = index[:, :order] 371 | xs = tf.transpose(xs) 372 | xs = tf.gather(xs, index) ##dim=[num,order,batchsize] 373 | xt = tf.transpose(xt) 374 | xt = tf.gather(xt, index) 375 | Ho_Xs = tf.transpose(tf.reduce_prod(xs, axis=1)) 376 | Ho_Xt = tf.transpose(tf.reduce_prod(xt, axis=1)) 377 | KHoMM=KernelHoMM(Ho_Xs,Ho_Xt,sigma=0.00001) 378 | return KHoMM 379 | 380 | 381 | 382 | 383 | def CovSqrt(Cov): 384 | Cov=Cov/tf.trace(Cov) 385 | Y0=Cov 386 | Z0=tf.eye(int(Cov.shape[0])) 387 | I=tf.eye(int(Cov.shape[0])) 388 | Y1=0.5*tf.matmul(Y0,3*I-tf.matmul(Z0,Y0)) 389 | Z1=0.5*tf.matmul(3*I-tf.matmul(Z0,Y0),Z0) 390 | Y2 = 0.5 * tf.matmul(Y1, 3 * I - tf.matmul(Z1, Y1)) 391 | Z2 = 0.5 * tf.matmul(3 * I - tf.matmul(Z1, Y1), Z1) 392 | Y3 = 0.5 * tf.matmul(Y2, 3 * I - tf.matmul(Z2, Y2)) 393 | Z3 = 0.5 * tf.matmul(3 * I - tf.matmul(Z2, Y2), Z2) 394 | Y4 = 0.5 * tf.matmul(Y3, 3 * I - tf.matmul(Z3, Y3)) 395 | Z4 = 0.5 * tf.matmul(3 * I - tf.matmul(Z3, Y3), Z3) 396 | Y5 = 0.5 * tf.matmul(Y4, 3 * I - tf.matmul(Z4, Y4)) 397 | Z5 = 0.5 * tf.matmul(3 * I - tf.matmul(Z4, Y4), Z4) 398 | Y6 = 0.5 * tf.matmul(Y5, 3 * I - tf.matmul(Z5, Y5)) 399 | Y6 = tf.multiply(tf.sign(Y6), tf.sqrt(tf.abs(Y6)+1e-12)) 400 | Y6=Y6/tf.norm(Y6) 401 | return Y6 402 | 403 | 404 | def Target_loss(target_softmax,logits_threshold): 405 | index=tf.where(tf.equal(tf.greater(target_softmax,logits_threshold),True))[:,0] 406 | target_softmax=tf.gather(target_softmax,index) 407 | return -tf.reduce_mean(tf.reduce_sum(target_softmax * tf.log(tf.clip_by_value(target_softmax,0.0001,1)), axis=1)) 408 | 409 | 410 | def GradRegularization(target_y,target_x): 411 | gradient=tf.gradients(target_y,target_x)[0] 412 | loss=tf.reduce_sum(tf.square(gradient)) 413 | return loss 414 | 415 | 416 | def AlignCenter(source_center,target_center): 417 | num=10000 418 | order=4 419 | dim = tf.cast(source_center.shape[1], tf.int32) 420 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32, seed=0) 421 | index = index[:, :order] 422 | source_center = tf.transpose(source_center) 423 | source_center = tf.gather(source_center, index) 424 | target_center = tf.transpose(target_center) 425 | target_center = tf.gather(target_center, index) 426 | HO_source_center = tf.reduce_prod(source_center, axis=1) 427 | HO_target_center = tf.reduce_prod(target_center, axis=1) 428 | class_domain_loss = tf.reduce_mean(tf.norm(HO_source_center - HO_target_center, axis=0)) 429 | # class_domain_loss = tf.reduce_mean(tf.norm(source_center - target_center, axis=1)) 430 | return class_domain_loss 431 | 432 | 433 | def SelectTargetSamples(features,logits,logits_threshold): 434 | target_softmax=tf.nn.softmax(logits) 435 | index = tf.where(tf.equal(tf.greater(target_softmax, logits_threshold), True))[:, 0] 436 | target_softmax = tf.gather(target_softmax, index) 437 | target_feature = tf.gather(features, index) 438 | return target_feature,target_softmax 439 | 440 | 441 | def MMD(xs,xt): 442 | diff = tf.reduce_mean(xs, 0, keep_dims=False) - tf.reduce_mean(xt, 0, keep_dims=False) 443 | test=tf.multiply(diff, diff) 444 | loss=tf.reduce_sum(tf.multiply(diff, diff)) 445 | return tf.reduce_sum(tf.multiply(diff, diff)) 446 | 447 | 448 | def MMD1(xs,xt): 449 | diff=xs-xt 450 | test=tf.matmul(diff,tf.transpose(diff)) 451 | loss=tf.reduce_mean(tf.matmul(diff,tf.transpose(diff))) 452 | return loss 453 | 454 | 455 | 456 | def log_coral_loss(h_src, h_trg,batch_size=128,gamma=1e-3): 457 | # regularized covariances result in inf or nan 458 | # First: subtract the mean from the data matrix 459 | batch_size = float(batch_size) 460 | h_src = h_src - tf.reduce_mean(h_src, axis=0) 461 | h_trg = h_trg - tf.reduce_mean(h_trg, axis=0) 462 | cov_source = (1. / (batch_size - 1)) * tf.matmul(h_src, h_src,transpose_a=True) + gamma * tf.eye(128) 463 | cov_target = (1. / (batch_size - 1)) * tf.matmul(h_trg, h_trg, transpose_a=True) + gamma * tf.eye(128) 464 | # eigen decomposition 465 | eig_source = tf.self_adjoint_eig(cov_source) 466 | eig_target = tf.self_adjoint_eig(cov_target) 467 | log_cov_source = tf.matmul(eig_source[1],tf.matmul(tf.diag(tf.log(eig_source[0])), eig_source[1], transpose_b=True)) 468 | log_cov_target = tf.matmul(eig_target[1],tf.matmul(tf.diag(tf.log(eig_target[0])), eig_target[1], transpose_b=True)) 469 | return tf.reduce_mean(tf.square(tf.subtract(log_cov_source, log_cov_target))) 470 | 471 | 472 | 473 | def CenterBased(fc4,y): 474 | source_label=y 475 | Xs = fc4 476 | labels = tf.argmax(source_label, 1) 477 | inter_loss, intra_loss, centers_update_op,centers = get_center_loss(Xs, labels, 0.2, FLAGS.num_classes) 478 | discriminative_loss = intra_loss 479 | discriminative_loss = discriminative_loss / (FLAGS.num_classes * FLAGS.batch_size) 480 | return centers_update_op, discriminative_loss,centers 481 | 482 | 483 | def CorNorm_loss(h_src,h_trg): 484 | gamma = 0.0001 # 0.001 485 | Xs = h_src 486 | Xt = h_trg 487 | Xs = tf.transpose(Xs - tf.reduce_mean(Xs, axis=0)) 488 | Xt = tf.transpose(Xt - tf.reduce_mean(Xt, axis=0)) 489 | norm = lambda x: tf.reduce_sum(tf.square(x), 1) 490 | Xs_Cor_norm = tf.transpose(norm(tf.expand_dims(Xs, 2) - tf.transpose(Xs))) 491 | Xt_Cor_norm = tf.transpose(norm(tf.expand_dims(Xt, 2) - tf.transpose(Xt))) 492 | W1 = tf.exp(-gamma * Xs_Cor_norm) 493 | W2 = tf.exp(-gamma * Xt_Cor_norm) 494 | W1 = W1 / (tf.reduce_mean(W1)) 495 | W2 = W2 / (tf.reduce_mean(W2)) 496 | domain_loss = tf.reduce_mean(tf.square(tf.subtract(W1, W2))) 497 | return domain_loss 498 | 499 | 500 | def Cal_RingLoss(ring_norm,Xs,Xt): 501 | Xs_norm=tf.norm(Xs,ord="euclidean",axis=1) 502 | Xt_norm = tf.norm(Xt, ord="euclidean", axis=1) 503 | ring_loss=tf.reduce_mean(tf.norm(Xs_norm-ring_norm))+tf.reduce_mean(tf.norm(Xt_norm-ring_norm)) 504 | return ring_loss 505 | 506 | 507 | 508 | if __name__ == '__main__': 509 | tf.app.run() 510 | -------------------------------------------------------------------------------- /HoMM_office/resnet/model.py: -------------------------------------------------------------------------------- 1 | """ 2 | Derived from: https://github.com/ry/tensorflow-resnet 3 | """ 4 | import tensorflow as tf 5 | import numpy as np 6 | from tensorflow.python.ops import control_flow_ops 7 | from tensorflow.python.training import moving_averages 8 | 9 | 10 | NUM_BLOCKS = { 11 | 50: [3, 4, 6, 3], 12 | 101: [3, 4, 23, 3], 13 | 152: [3, 8, 36, 3] 14 | } 15 | CONV_WEIGHT_DECAY = 0.00004 16 | CONV_WEIGHT_STDDEV = 0.1 17 | MOVING_AVERAGE_DECAY = 0.9997 18 | BN_DECAY = MOVING_AVERAGE_DECAY 19 | BN_EPSILON = 0.001 20 | UPDATE_OPS_COLLECTION = 'resnet_update_ops' 21 | FC_WEIGHT_STDDEV = 0.01 22 | 23 | 24 | class ResNetModel(object): 25 | 26 | def __init__(self,x, is_training,reuse=False,depth=50, dropout_rate=0.5,num_classes=1000): 27 | self.is_training = is_training 28 | self.num_classes = num_classes 29 | self.depth = depth 30 | self.input=x 31 | self.reuse=reuse 32 | self.dropout_rate=dropout_rate 33 | if depth in NUM_BLOCKS: 34 | self.num_blocks = NUM_BLOCKS[depth] 35 | else: 36 | raise ValueError('Depth is not supported; it must be 50, 101 or 152') 37 | self.inference() 38 | 39 | 40 | def inference(self): 41 | # Scale 1 42 | with tf.variable_scope('scale1',reuse=self.reuse): 43 | s1_conv = conv(self.input, ksize=7, stride=2, filters_out=64) 44 | s1_bn = bn(s1_conv, is_training=self.is_training) 45 | s1 = tf.nn.relu(s1_bn) 46 | 47 | # Scale 2 48 | with tf.variable_scope('scale2',reuse=self.reuse): 49 | s2_mp = tf.nn.max_pool(s1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME') 50 | s2 = stack(s2_mp, is_training=self.is_training, num_blocks=self.num_blocks[0], stack_stride=1, block_filters_internal=64) 51 | 52 | # Scale 3 53 | with tf.variable_scope('scale3',reuse=self.reuse): 54 | s3 = stack(s2, is_training=self.is_training, num_blocks=self.num_blocks[1], stack_stride=2, block_filters_internal=128) 55 | 56 | # Scale 4 57 | with tf.variable_scope('scale4',reuse=self.reuse): 58 | s4 = stack(s3, is_training=self.is_training, num_blocks=self.num_blocks[2], stack_stride=2, block_filters_internal=256) 59 | self.s4=s4 60 | 61 | # Scale 5 62 | with tf.variable_scope('scale5',reuse=self.reuse): 63 | s5 = stack(s4, is_training=self.is_training, num_blocks=self.num_blocks[3], stack_stride=2, block_filters_internal=512) 64 | 65 | # post-net 66 | self.avg_pool = tf.reduce_mean(s5, reduction_indices=[1, 2], name='avg_pool') 67 | 68 | ######### add adapted layer ############### 69 | with tf.variable_scope('adapt',reuse=self.reuse): 70 | # self.avg_pool=tf.nn.dropout(self.avg_pool,keep_prob=self.dropout_rate) 71 | self.adapt= tf.nn.tanh(fc(self.avg_pool, num_units_out=180)) 72 | # self.adapt=tf.transpose(self.adapt)/tf.norm(self.adapt,axis=1) 73 | # self.adapt=tf.transpose(self.adapt) 74 | 75 | with tf.variable_scope('fc',reuse=self.reuse): 76 | self.prob = fc(self.adapt, num_units_out=self.num_classes) 77 | 78 | return self.prob 79 | 80 | 81 | 82 | def loss(self, batch_x, batch_y=None): 83 | y_predict = self.inference(batch_x) 84 | # cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y_predict, labels=batch_y) 85 | cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=y_predict, labels=batch_y) 86 | cross_entropy_mean = tf.reduce_mean(cross_entropy) 87 | regularization_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) 88 | self.loss = tf.add_n([cross_entropy_mean] + regularization_losses) 89 | return self.loss 90 | 91 | def optimize(self, learning_rate, train_layers=[]): 92 | trainable_var_names = ['weights', 'biases', 'beta', 'gamma'] 93 | var_list = [v for v in tf.trainable_variables() if 94 | v.name.split(':')[0].split('/')[-1] in trainable_var_names and 95 | contains(v.name, train_layers)] 96 | train_op = tf.train.AdamOptimizer(learning_rate).minimize(self.loss, var_list=var_list) 97 | 98 | ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY) 99 | tf.add_to_collection(UPDATE_OPS_COLLECTION, ema.apply([self.loss])) 100 | 101 | batchnorm_updates = tf.get_collection(UPDATE_OPS_COLLECTION) 102 | batchnorm_updates_op = tf.group(*batchnorm_updates) 103 | 104 | return tf.group(train_op, batchnorm_updates_op) 105 | 106 | def load_original_weights(self, session, skip_layers=[]): 107 | weights_path = 'ResNet-L{}.npy'.format(self.depth) 108 | weights_dict = np.load(weights_path, encoding='bytes').item() 109 | 110 | for op_name in weights_dict: 111 | parts = op_name.split('/') 112 | 113 | # if contains(op_name, skip_layers): 114 | # continue 115 | 116 | if parts[0] == 'fc' and self.num_classes != 1000: 117 | continue 118 | 119 | full_name = "{}:0".format(op_name) 120 | var = [v for v in tf.global_variables() if v.name == full_name][0] 121 | session.run(var.assign(weights_dict[op_name])) 122 | 123 | 124 | """ 125 | Helper methods 126 | """ 127 | def _get_variable(name, shape, initializer, weight_decay=0.0, dtype='float', trainable=True): 128 | "A little wrapper around tf.get_variable to do weight decay" 129 | 130 | if weight_decay > 0: 131 | regularizer = tf.contrib.layers.l2_regularizer(weight_decay) 132 | else: 133 | regularizer = None 134 | 135 | return tf.get_variable(name, shape=shape, initializer=initializer, dtype=dtype, regularizer=regularizer, 136 | trainable=trainable) 137 | 138 | def conv(x, ksize, stride, filters_out): 139 | filters_in = x.get_shape()[-1] 140 | shape = [ksize, ksize, filters_in, filters_out] 141 | initializer = tf.truncated_normal_initializer(stddev=CONV_WEIGHT_STDDEV) 142 | weights = _get_variable('weights', shape=shape, dtype='float', initializer=initializer, 143 | weight_decay=CONV_WEIGHT_DECAY) 144 | return tf.nn.conv2d(x, weights, [1, stride, stride, 1], padding='SAME') 145 | 146 | def bn(x, is_training): 147 | x_shape = x.get_shape() 148 | params_shape = x_shape[-1:] 149 | 150 | axis = list(range(len(x_shape) - 1)) 151 | 152 | beta = _get_variable('beta', params_shape, initializer=tf.zeros_initializer()) 153 | gamma = _get_variable('gamma', params_shape, initializer=tf.ones_initializer()) 154 | 155 | moving_mean = _get_variable('moving_mean', params_shape, initializer=tf.zeros_initializer(), trainable=False) 156 | moving_variance = _get_variable('moving_variance', params_shape, initializer=tf.ones_initializer(), trainable=False) 157 | 158 | # These ops will only be preformed when training. 159 | mean, variance = tf.nn.moments(x, axis) 160 | update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, BN_DECAY) 161 | update_moving_variance = moving_averages.assign_moving_average(moving_variance, variance, BN_DECAY) 162 | tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_mean) 163 | tf.add_to_collection(UPDATE_OPS_COLLECTION, update_moving_variance) 164 | 165 | mean, variance = control_flow_ops.cond( 166 | is_training, lambda: (mean, variance), 167 | lambda: (moving_mean, moving_variance)) 168 | 169 | return tf.nn.batch_normalization(x, mean, variance, beta, gamma, BN_EPSILON) 170 | 171 | def stack(x, is_training, num_blocks, stack_stride, block_filters_internal): 172 | for n in range(num_blocks): 173 | block_stride = stack_stride if n == 0 else 1 174 | with tf.variable_scope('block%d' % (n + 1)): 175 | x = block(x, is_training, block_filters_internal=block_filters_internal, block_stride=block_stride) 176 | return x 177 | 178 | 179 | def block(x, is_training, block_filters_internal, block_stride): 180 | filters_in = x.get_shape()[-1] 181 | 182 | m = 4 183 | filters_out = m * block_filters_internal 184 | shortcut = x 185 | 186 | with tf.variable_scope('a'): 187 | a_conv = conv(x, ksize=1, stride=block_stride, filters_out=block_filters_internal) 188 | a_bn = bn(a_conv, is_training) 189 | a = tf.nn.relu(a_bn) 190 | 191 | with tf.variable_scope('b'): 192 | b_conv = conv(a, ksize=3, stride=1, filters_out=block_filters_internal) 193 | b_bn = bn(b_conv, is_training) 194 | b = tf.nn.relu(b_bn) 195 | 196 | with tf.variable_scope('c'): 197 | c_conv = conv(b, ksize=1, stride=1, filters_out=filters_out) 198 | c = bn(c_conv, is_training) 199 | 200 | with tf.variable_scope('shortcut'): 201 | if filters_out != filters_in or block_stride != 1: 202 | shortcut_conv = conv(x, ksize=1, stride=block_stride, filters_out=filters_out) 203 | shortcut = bn(shortcut_conv, is_training) 204 | 205 | return tf.nn.relu(c + shortcut) 206 | 207 | def stack_Final(x, is_training, num_blocks, stack_stride, block_filters_internal): 208 | for n in range(num_blocks): 209 | block_stride = stack_stride if n == 0 else 1 210 | with tf.variable_scope('block%d' % (n + 1)): 211 | x = block_Final(x, is_training, block_filters_internal=block_filters_internal, block_stride=block_stride) 212 | return x 213 | 214 | def block_Final(x, is_training, block_filters_internal, block_stride): 215 | filters_in = x.get_shape()[-1] 216 | 217 | m = 4 218 | filters_out = m * block_filters_internal 219 | shortcut = x 220 | 221 | with tf.variable_scope('a'): 222 | a_conv = conv(x, ksize=1, stride=block_stride, filters_out=block_filters_internal) 223 | a_bn = bn(a_conv, is_training) 224 | a = tf.nn.relu(a_bn) 225 | 226 | with tf.variable_scope('b'): 227 | b_conv = conv(a, ksize=3, stride=1, filters_out=block_filters_internal) 228 | b_bn = bn(b_conv, is_training) 229 | b = tf.nn.relu(b_bn) 230 | 231 | with tf.variable_scope('c'): 232 | c_conv = conv(b, ksize=1, stride=1, filters_out=filters_out) 233 | c = bn(c_conv, is_training) 234 | 235 | with tf.variable_scope('shortcut'): 236 | if filters_out != filters_in or block_stride != 1: 237 | shortcut_conv = conv(x, ksize=1, stride=block_stride, filters_out=filters_out) 238 | shortcut = bn(shortcut_conv, is_training) 239 | 240 | return tf.tanh(c + shortcut) 241 | 242 | def fc(x, num_units_out): 243 | num_units_in = x.get_shape()[1] 244 | weights_initializer = tf.truncated_normal_initializer(stddev=FC_WEIGHT_STDDEV) 245 | weights = _get_variable('weights', shape=[num_units_in, num_units_out], initializer=weights_initializer, 246 | weight_decay=FC_WEIGHT_STDDEV) 247 | biases = _get_variable('biases', shape=[num_units_out], initializer=tf.zeros_initializer()) 248 | return tf.nn.xw_plus_b(x, weights, biases) 249 | 250 | def contains(target_str, search_arr): 251 | rv = False 252 | 253 | for search_str in search_arr: 254 | if search_str in target_str: 255 | rv = True 256 | break 257 | 258 | return rv 259 | -------------------------------------------------------------------------------- /HoMM_office/resnet/model.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/HoMM_office/resnet/model.pyc -------------------------------------------------------------------------------- /HoMM_office/resnet/ttt.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | for epoch in range(100): 4 | param = 2 / (1 + np.exp(-10 * (epoch) / 100)) - 1 5 | print(param) -------------------------------------------------------------------------------- /HoMM_office/utils/__pycache__/preprocessor.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/HoMM_office/utils/__pycache__/preprocessor.cpython-35.pyc -------------------------------------------------------------------------------- /HoMM_office/utils/preprocessor.py: -------------------------------------------------------------------------------- 1 | """ 2 | Derived from: https://github.com/kratzert/finetune_alexnet_with_tensorflow/ 3 | """ 4 | import numpy as np 5 | import cv2 6 | 7 | 8 | class BatchPreprocessor(object): 9 | 10 | def __init__(self, dataset_file_path, num_classes, output_size=[227, 227], horizontal_flip=False, shuffle=False, 11 | mean_color=[132.2766, 139.6506, 146.9702], multi_scale=None): 12 | self.num_classes = num_classes 13 | self.output_size = output_size 14 | self.horizontal_flip = horizontal_flip 15 | self.shuffle = shuffle 16 | self.mean_color = mean_color 17 | self.multi_scale = multi_scale 18 | 19 | self.pointer = 0 20 | self.images = [] 21 | self.labels = [] 22 | 23 | # Read the dataset file 24 | dataset_file = open(dataset_file_path) 25 | lines = dataset_file.readlines() 26 | for line in lines: 27 | items = line.split() 28 | self.images.append(items[0]) 29 | self.labels.append(int(items[1])) 30 | 31 | # Shuffle the data 32 | if self.shuffle: 33 | self.shuffle_data() 34 | 35 | def shuffle_data(self): 36 | images = self.images[:] 37 | labels = self.labels[:] 38 | self.images = [] 39 | self.labels = [] 40 | 41 | idx = np.random.permutation(len(labels)) 42 | for i in idx: 43 | self.images.append(images[i]) 44 | self.labels.append(labels[i]) 45 | 46 | def reset_pointer(self): 47 | self.pointer = 0 48 | 49 | if self.shuffle: 50 | self.shuffle_data() 51 | 52 | def next_batch(self, batch_size): 53 | # Get next batch of image (path) and labels 54 | paths = self.images[self.pointer:(self.pointer+batch_size)] 55 | labels = self.labels[self.pointer:(self.pointer+batch_size)] 56 | 57 | # Update pointer 58 | self.pointer += batch_size 59 | 60 | # Read images 61 | images = np.ndarray([batch_size, self.output_size[0], self.output_size[1], 3]) 62 | for i in range(len(paths)): 63 | img = cv2.imread(paths[i]) 64 | 65 | # Flip image at random if flag is selected 66 | if self.horizontal_flip and np.random.random() < 0.5: 67 | img = cv2.flip(img, 1) 68 | 69 | if self.multi_scale is None: 70 | # Resize the image for output 71 | img = cv2.resize(img, (self.output_size[0], self.output_size[0])) 72 | img = img.astype(np.float32) 73 | elif isinstance(self.multi_scale, list): 74 | # Resize to random scale 75 | new_size = np.random.randint(self.multi_scale[0], self.multi_scale[1], 1)[0] 76 | img = cv2.resize(img, (new_size, new_size)) 77 | img = img.astype(np.float32) 78 | 79 | # random crop at output size 80 | diff_size = new_size - self.output_size[0] 81 | random_offset_x = np.random.randint(0, diff_size, 1)[0] 82 | random_offset_y = np.random.randint(0, diff_size, 1)[0] 83 | img = img[random_offset_x:(random_offset_x+self.output_size[0]), 84 | random_offset_y:(random_offset_y+self.output_size[0])] 85 | 86 | # Subtract mean color 87 | img -= np.array(self.mean_color) 88 | 89 | images[i] = img 90 | 91 | # Expand labels to one hot encoding 92 | one_hot_labels = np.zeros((batch_size, self.num_classes)) 93 | for i in range(len(labels)): 94 | one_hot_labels[i][labels[i]] = 1 95 | 96 | # Return array of images and labels 97 | return images, one_hot_labels 98 | -------------------------------------------------------------------------------- /HoMM_office/utils/preprocessor.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/HoMM_office/utils/preprocessor.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HoMM: Higher-order Moment Matching for Unsupervised Domain Adaptation (AAAI-2020) 2 | 3 | ## HoMM-Master 4 |
5 | 6 | 7 | * This repository contains code for our paper **HoMM: Higher-order Moment Matching for Unsupervised Domain Adaptation** [Download paper here](https://arxiv.org/abs/1912.11976) 8 | * If you have any question about our paper or code, please don't hesitate to contact with me ahucomputer@126.com, we will update our repository accordingly 9 | 10 | ## Setup 11 | * **Dataset** The code as well as the dataset can be downloaded here [HoMM in MNIST](https://drive.google.com/open?id=167tVIBI2dVa0D18i6CiM-hicFJ3DJFzX) and [HoMM in Office&Office-Home](https://drive.google.com/open?id=1-OSkyh1Vzg_sxWJ6u4nvuQ3FRfKmZ-UF) 12 | 13 | * **requirements** Python==2.7, tensorflow==1.9, opencv 14 | 15 | ## Training 16 | * **MNIST** You can run **TrainLenet.py** in HoMM-mnist. 17 | * **Office&Office-Home** You can run **finetune.py** in HoMM_office/resnet/. 18 | * We have provide four functions **HoMM3**, **HoMM4**, **HoMM** and **KHoMM** conresponding to the third-order HoMM, fourth-order HoMM, Arbitrary-order moment matching, and Kernel HoMM. 19 | 20 | ## Reimplement HoMM in your work 21 | * Readers can reimplement the HoMM in their work very easily by using the following function. 22 | * In our code, **xs** and **xt** denotes source and target deep features in the adapted layer. the dimension of **xs** and **xt** is b*L where b is the batchsize and L is the number of neurons in the adapted layer. num denotes the N in our paper, which indicates the number of sampled values in the high-level tensor. 23 | * **It is worth noting that the relu activation function can not be applied to the adapted layer, as relu activation function will make most of the values in the high-level tensor to be zero, which will make our HoMM fail. Therefore, we adopt tanh activation function in the adapted layer** 24 | 25 | **HoMM3** 26 | ```python 27 | def HoMM3_loss(self, xs, xt): 28 | xs = xs - tf.reduce_mean(xs, axis=0) 29 | xt = xt - tf.reduce_mean(xt, axis=0) 30 | xs=tf.expand_dims(xs,axis=-1) 31 | xs = tf.expand_dims(xs, axis=-1) 32 | xt = tf.expand_dims(xt, axis=-1) 33 | xt = tf.expand_dims(xt, axis=-1) 34 | xs_1=tf.transpose(xs,[0,2,1,3]) 35 | xs_2 = tf.transpose(xs, [0, 2, 3, 1]) 36 | xt_1 = tf.transpose(xt, [0, 2, 1, 3]) 37 | xt_2 = tf.transpose(xt, [0, 2, 3, 1]) 38 | HR_Xs=xs*xs_1*xs_2 # dim: b*L*L*L 39 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) #dim: L*L*L 40 | HR_Xt = xt * xt_1 * xt_2 41 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 42 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 43 | ``` 44 | 45 | * **HoMM4** 46 | * The adapted layer has 90 neurons, we divided them into 3 group with each group 30 neurons. 47 | ```python 48 | def HoMM4(self,xs,xt): 49 | ind=tf.range(tf.cast(xs.shape[1],tf.int32)) 50 | ind=tf.random_shuffle(ind) 51 | xs=tf.transpose(xs,[1,0]) 52 | xs=tf.gather(xs,ind) 53 | xs = tf.transpose(xs, [1, 0]) 54 | xt = tf.transpose(xt, [1, 0]) 55 | xt = tf.gather(xt, ind) 56 | xt = tf.transpose(xt, [1, 0]) 57 | return self.HoMM4_loss(xs[:,:30],xt[:,:30])+self.HoMM4_loss(xs[:,30:60],xt[:,30:60])+self.HoMM4_loss(xs[:,60:90],xt[:,60:90]) 58 | 59 | 60 | 61 | def HoMM4_loss(self, xs, xt): 62 | xs = xs - tf.reduce_mean(xs, axis=0) 63 | xt = xt - tf.reduce_mean(xt, axis=0) 64 | xs = tf.expand_dims(xs,axis=-1) 65 | xs = tf.expand_dims(xs, axis=-1) 66 | xs = tf.expand_dims(xs, axis=-1) 67 | xt = tf.expand_dims(xt, axis=-1) 68 | xt = tf.expand_dims(xt, axis=-1) 69 | xt = tf.expand_dims(xt, axis=-1) 70 | xs_1 = tf.transpose(xs,[0,2,1,3,4]) 71 | xs_2 = tf.transpose(xs, [0, 2, 3, 1,4]) 72 | xs_3 = tf.transpose(xs, [0, 2, 3, 4, 1]) 73 | xt_1 = tf.transpose(xt, [0, 2, 1, 3,4]) 74 | xt_2 = tf.transpose(xt, [0, 2, 3, 1,4]) 75 | xt_3 = tf.transpose(xt, [0, 2, 3, 4, 1]) 76 | HR_Xs=xs*xs_1*xs_2*xs_3 # dim: b*L*L*L*L 77 | HR_Xs=tf.reduce_mean(HR_Xs,axis=0) # dim: L*L*L*L 78 | HR_Xt = xt * xt_1 * xt_2*xt_3 79 | HR_Xt = tf.reduce_mean(HR_Xt, axis=0) 80 | return tf.reduce_mean(tf.square(tf.subtract(HR_Xs, HR_Xt))) 81 | ``` 82 | 83 | * **Arbitrary-order Moment Matching** 84 | ```python 85 | def HoMM(self,xs, xt, order=3, num=300000): 86 | xs = xs - tf.reduce_mean(xs, axis=0) 87 | xt = xt - tf.reduce_mean(xt, axis=0) 88 | dim = tf.cast(xs.shape[1], tf.int32) 89 | index = tf.random_uniform(shape=(num, dim), minval=0, maxval=dim - 1, dtype=tf.int32) 90 | index = index[:, :order] 91 | xs = tf.transpose(xs) 92 | xs = tf.gather(xs, index) ##dim=[num,order,batchsize] 93 | xt = tf.transpose(xt) 94 | xt = tf.gather(xt, index) 95 | HO_Xs = tf.reduce_prod(xs, axis=1) 96 | HO_Xs = tf.reduce_mean(HO_Xs, axis=1) 97 | HO_Xt = tf.reduce_prod(xt, axis=1) 98 | HO_Xt = tf.reduce_mean(HO_Xt, axis=1) 99 | return tf.reduce_mean(tf.square(tf.subtract(HO_Xs, HO_Xt))) 100 | ``` 101 | 102 | 103 | ## Results 104 |
105 | 106 |
107 | 108 |
109 | 110 | 111 | ## Citation 112 | * If you find it helpful for you, please cite our paper 113 | ``` 114 | @inproceedings{chen2020HoMM, 115 | title={HoMM: Higher-order Moment Matching for Unsupervised Domain Adaptation}, 116 | author={Chao Chen, Zhihang Fu, Zhihong Chen, Sheng Jin, Zhaowei Cheng, Xinyu Jin, Xian-Sheng Hua}, 117 | booktitle={Proceedings of the AAAI Conference on Artificial Intelligence}, 118 | volume={34}, 119 | year={2020} 120 | } 121 | ``` 122 | -------------------------------------------------------------------------------- /img/img1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img1.PNG -------------------------------------------------------------------------------- /img/img2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img2.PNG -------------------------------------------------------------------------------- /img/img3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img3.PNG -------------------------------------------------------------------------------- /img/img4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img4.PNG -------------------------------------------------------------------------------- /img/img5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img5.PNG -------------------------------------------------------------------------------- /img/img6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img6.PNG -------------------------------------------------------------------------------- /img/img7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenchao666/HoMM-Master/78ff9be03c03cc2606abd4b5276486969bd7c005/img/img7.PNG --------------------------------------------------------------------------------