class GoogLeNet: network = input_data(shape=[None, 1024, 1024, 1]) network = conv_2d(network, 64, 9, strides=4, activation='relu', bias=False) '''Bottleneck''' network = tflearn.residual_bottleneck(network, nb_blocks=3, bottleneck_size=16, out_channels=64) network = tflearn.residual_bottleneck(network, nb_blocks=1, bottleneck_size=32, out_channels=128, downsample=True) network = tflearn.residual_bottleneck(network, nb_blocks=2, bottleneck_size=32, out_channels=128) network = tflearn.residual_bottleneck(network, nb_blocks=1, bottleneck_size=64, out_channels=256, downsample=True) network = tflearn.residual_bottleneck(network, nb_blocks=2, bottleneck_size=64, out_channels=256) network = batch_normalization(network) network = tflearn.activation(network, 'relu') network = global_avg_pool(network) '''Output layer''' output = fully_connected(network, 15, activation='sigmoid') network = regression(output, optimizer='momentum', loss='binary_crossentropy', learning_rate=0.01) '''Set model + Save parameters + Tensorboard''' model = tflearn.DNN(network, checkpoint_path='params_resnet_cxr', max_checkpoints=1, tensorboard_verbose=0) '''Feed the oxflowers17 dataset to the model''' model.fit(train_x, train_t, n_epoch=10, validation_set=(test_x, test_t), show_metric=True, batch_size=16, snapshot_epoch=False, snapshot_step=100, run_id='resnet_cxr')
def ResNet1(network): network = conv_2d(network, 64, 3, activation='relu', bias=False) # Residual blocks network = residual_bottleneck(network, 3, 16, 64) network = residual_bottleneck(network, 1, 32, 128, downsample=True) network = residual_bottleneck(network, 2, 32, 128) network = residual_bottleneck(network, 1, 64, 256, downsample=True) network = residual_bottleneck(network, 2, 64, 256) network = batch_normalization(network) network = activation(network, 'relu') network = global_avg_pool(network) # Regression network = fully_connected(network, output_dim, activation='softmax') return network
def architecture03(input, num_class): net = tflearn.conv_2d(input, 64, 3, activation='relu', bias=False) net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=False) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=False) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, num_class, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.001) return tflearn.DNN(net, tensorboard_verbose=0)
def train(X_train_R, Y_train, X_test_R, Y_test, predict_data): X_train_R, mean = du.featurewise_zero_center(X_train_R) X_test_R = du.featurewise_zero_center(X_test_R, mean) net = tflearn.input_data(shape=[None, 28, 28, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) model = tflearn.DNN(net, checkpoint_path='model_Digit', max_checkpoints=2, tensorboard_verbose=0, tensorboard_dir='logs') model.fit(X_train_R, Y_train, n_epoch=1, validation_set=(X_test_R, Y_test), show_metric=True, batch_size=10, run_id='Digit') model.save("model_Digit/digit_recognition") print("model train OK!\n save model OK!") model.load("model_Digit/digit_recognition") print("model already loaded!") print("model predict") label = model.predict(predict_data) print("predict label: ", label)
def demo(): import tflearn.datasets.mnist as mnist x, y, test_x, test_y = mnist.load_data(one_hot='True') print(x.shape) x = x.reshape([-1, 28, 28, 1]) test_x = test_x.reshape([-1, 28, 28, 1]) # 按功能划分的零中心将每个样本的中心置零,并指定平均值。如果未指定,则对所有样品评估平均值。 # Returns : A numpy array with same shape as input. Or a tuple (array, mean) if no mean value was specified. x, mean = du.featurewise_zero_center(x) test_x = du.featurewise_zero_center(test_x, mean) net = tflearn.input_data(shape=[None, 28, 28, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_mnist', max_checkpoints=10, tensorboard_verbose=0) model.fit(x, y, n_epoch=100, validation_set=(test_x, test_y), show_metric=True, batch_size=256, run_id='resnet_mnist')
def build_resnet(): ''' Specify layers for ResNet and return constructed network ''' net = tflearn.input_data( shape=[None, INPUT_DATA_SIZE_X, INPUT_DATA_SIZE_Y, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) # Residual blocks net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, OUTPUT_DATA_SIZE, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) return net
fully_connect = tflearn.fully_connected(net,HIDDEN,activation='relu') # [1000] net = tflearn.fully_connected(fully_connect,LEN_OUT,activation='softmax') # [7211] #mom = tflearn.SGD(0.1,lr_decay=0.1,decay_step=3086 * 20) mom = tflearn.Momentum(0.01,lr_decay=0.1,decay_step=int(395000 / BATCH_SIZE) * 10,staircase=True) reg = tflearn.regression(net,optimizer=mom,loss='categorical_crossentropy') model = tflearn.DNN(reg,checkpoint_path='models/{}'.format(MODEL_FILE),max_checkpoints=100,session=sess) elif NET_TYPE == 'resnet50' or NET_TYPE == 'resnet101' or NET_TYPE == 'resnet152': with tf.device("/gpu:{}".format(GPU_CORE)): net = tflearn.input_data(shape=[None,IMG_SIZE,IMG_SIZE,3]) net = tflearn.conv_2d(net,64,7,2, regularizer='L2', weight_decay=0.0001) # [64,64,32] net = tflearn.max_pool_2d(net,3,2) # [32,32,32] net = tflearn.residual_bottleneck(net,3,64,256) # [32,32,64] net = tflearn.residual_bottleneck(net,1,128,512,downsample=True) # [16,16,128] net = tflearn.residual_bottleneck(net,3,128,512) # [16,16,128] net = tflearn.residual_bottleneck(net,1,256,1024,downsample=True) # [16,16,256] if NET_TYPE == 'resnet50': net = tflearn.residual_bottleneck(net,5,256,1024) elif NET_TYPE == 'resnet101': net = tflearn.residual_bottleneck(net,22,256,1024) elif NET_TYPE == 'resnet152': net = tflearn.residual_bottleneck(net,35,256,1024) # [16,16,256]
def get_resnet_feature(test_data, test_label, test_pid, pid_map): n_dim = 6000 n_split = 300 tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) ### split #X = X.reshape([-1, n_split, 1]) #testX = testX.reshape([-1, n_split, 1]) # Building Residual Network net = tflearn.input_data(shape=[None, n_dim, 1]) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) net = tflearn.conv_1d(net, 64, 16, 2) #net = tflearn.conv_1d(net, 64, 16, 2, regularizer='L2', weight_decay=0.0001) net = tflearn.batch_normalization(net) # Residual blocks net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') #net = tflearn.global_avg_pool(net) # LSTM ############ reshape for sub_seq net = tf.reshape(net, [-1, n_dim // n_split, 512]) net = bidirectional_rnn(net, BasicLSTMCell(256), BasicLSTMCell(256)) #net = tflearn.layers.recurrent.lstm(net, n_units=512) #print("after LSTM", net.get_shape()) net = dropout(net, 0.5) # Regression feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.dropout(feature_layer, 0.5) net = tflearn.fully_connected(net, 4, activation='softmax') net = tflearn.regression( net, optimizer='adam', #momentum', loss='categorical_crossentropy') #,learning_rate=0.1) ## save model ### load model = tflearn.DNN(net) run_id = 'resnet_6000_500_10_5_v1' model.load('../model/resNet/' + run_id) ### create new model, and get features m2 = tflearn.DNN(feature_layer, session=model.session) tmp_feature = [] num_of_test = len(test_data) cur_data = [] pre = [] for i in range(num_of_test): cur_data.append(test_data[i]) if (i % 2000 == 0 or i == (num_of_test - 1)) and i != 0: #tmp_test_data = test_data[i].reshape([-1, n_dim, 1]) tmp_testX = np.array(cur_data, dtype=np.float32) tmp_feature.extend(m2.predict(tmp_testX.reshape([-1, n_dim, 1]))) cur_data = [] pre.extend(model.predict(tmp_testX)) tmp_feature = np.array(tmp_feature) test_pid = np.array(test_pid, dtype=np.string_) y_num = len(pid_map) features = [[0. for j in range(32)] for i in range(y_num)] re_labels = [[0. for j in range(4)] for i in range(y_num)] y_pre = [[0. for j in range(4)] for i in range(y_num)] y_sec_pre = [[0. for j in range(4)] for i in range(y_num)] y_third_pre = [[0. for j in range(4)] for i in range(y_num)] y_fourth_pre = [[0. for j in range(4)] for i in range(y_num)] y_fifth_pre = [[0. for j in range(4)] for i in range(y_num)] y_sixth_pre = [[0. for j in range(4)] for i in range(y_num)] y_seventh_pre = [[0. for j in range(4)] for i in range(y_num)] y_groundtruth = [[0. for j in range(4)] for i in range(y_num)] #print(y_num) for j in range(len(tmp_feature)): feature_pred = np.array(tmp_feature[j], dtype=np.float32) #print(len(feature_pred)) i_pred = np.array(pre[j], dtype=np.float32) cur_pid = str(test_pid[j], 'utf-8') list_id = pid_map[cur_pid] #print (list_id) temp_feature = np.array(features[list_id], dtype=np.float32) temp_pre = np.array(y_pre[list_id], dtype=np.float32) temp_sec_pre = np.array(y_sec_pre[list_id], dtype=np.float32) temp_third_pre = np.array(y_third_pre[list_id], dtype=np.float32) #print(temp_pre) max_p = temp_pre[np.argmax(temp_pre)] max_sec_p = temp_sec_pre[np.argmax(temp_sec_pre)] max_third_p = temp_third_pre[np.argmax(temp_third_pre)] sec_p = 0 sec_sec_p = 0 sec_third_p = 0 for k in range(len(temp_pre)): if temp_pre[k] == max_p: continue if temp_pre[k] > sec_p: sec_p = temp_pre[k] if temp_sec_pre[k] == max_sec_p: continue if temp_sec_pre[k] > sec_sec_p: sec_sec_p = temp_sec_pre[k] if temp_third_pre[k] == max_third_p: continue if temp_third_pre[k] > sec_third_p: sec_third_p = temp_third_pre[k] cur_max_p = i_pred[np.argmax(i_pred)] cur_sec_p = 0 for k in range(len(i_pred)): if i_pred[k] == cur_max_p: continue if i_pred[k] > cur_sec_p: cur_sec_p = i_pred[k] if (cur_max_p - cur_sec_p) > (max_p - sec_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = y_fifth_pre[list_id] y_fifth_pre[list_id] = y_fourth_pre[list_id] y_fourth_pre[list_id] = y_third_pre[list_id] y_third_pre[list_id] = y_sec_pre[list_id] y_sec_pre[list_id] = y_pre[list_id] y_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_sec_p - sec_sec_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = y_fifth_pre[list_id] y_fifth_pre[list_id] = y_fourth_pre[list_id] y_fourth_pre[list_id] = y_third_pre[list_id] y_third_pre[list_id] = y_sec_pre[list_id] y_sec_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_third_p - sec_third_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = y_fifth_pre[list_id] y_fifth_pre[list_id] = y_fourth_pre[list_id] y_fourth_pre[list_id] = y_third_pre[list_id] y_third_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_fourth_p - sec_fourth_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = y_fifth_pre[list_id] y_fifth_pre[list_id] = y_fourth_pre[list_id] y_fourth_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_fifth_p - sec_fifth_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = y_fifth_pre[list_id] y_fifth_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_sixth_p - sec_sixth_p): y_seventh_pre[list_id] = y_sixth_pre[list_id] y_sixth_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_seventh_p - sec_seventh_p): y_seventh_pre[list_id] = i_pred max_f = 0 for k in range(len(temp_feature)): if temp_feature[k] > max_f: max_f = temp_feature[k] if max_f > 0: feature_pred = (feature_pred + temp_feature) / 2 #for k in range(len(temp_feature)): # feature_pred[k] = (feature_pred[k]+temp_feature[k])/2 features[list_id] = feature_pred y_groundtruth[list_id] = test_label[j] gt_list = ["N", "A", "O", "~"] pred_1 = gt_list[np.argmax(i_pred)] if pred_1 == 'N': re_labels[list_id][0] += 1 elif pred_1 == 'A': re_labels[list_id][1] += 1 elif pred_1 == 'O': re_labels[list_id][2] += 1 elif pred_1 == '~': re_labels[list_id][3] += 1 else: print('wrong label') out_feature = [] for i in range(len(features)): out_feature.append(features[i]) out_feature = np.array(out_feature) for k in range(len(y_pre)): labels = [0. for j in range(4)] pred_1 = np.argmax(y_pre[k]) labels[pred_1] += 1 pred_2 = np.argmax(y_sec_pre[k]) labels[pred_2] += 1 pred_3 = np.argmax(y_third_pre[k]) labels[pred_3] += 1 if pred_1 == 2: print("O was selected!") continue elif pred_2 == 2: y_pre[k] = y_sec_pre[k] print("O was selected!") elif pred_3 == 2: y_pre[k] = y_third_pre[k] print("O was selected!") if pred_1 != np.argmax(labels): if pred_2 == np.argmax(labels): y_pre[k] = y_sec_pre[k] print("Second was selected!") MyEval.F1Score3_num(pre, test_label[:len(pre)]) MyEval.F1Score3_num(y_pre, y_groundtruth) MyEval.F1Score3_num(re_labels, y_groundtruth) return out_feature
def get_resnet_feature(test_data): n_dim = 6000 n_split = 300 tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) ### split #X = X.reshape([-1, n_split, 1]) #testX = testX.reshape([-1, n_split, 1]) # Building Residual Network net = tflearn.input_data(shape=[None, n_dim, 1]) print("input", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) print("reshaped input", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) #net = tflearn.conv_1d(net, 64, 16, 2, regularizer='L2', weight_decay=0.0001) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) # Residual blocks '''net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn10", net.get_shape())''' net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn10", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn12", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn14", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn16", net.get_shape()) '''net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn18", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn20", net.get_shape())''' net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') #net = tflearn.global_avg_pool(net) # LSTM print("before LSTM, before reshape", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_dim//n_split, 512]) print("before LSTM", net.get_shape()) net = bidirectional_rnn(net, BasicLSTMCell(256), BasicLSTMCell(256)) print("after LSTM", net.get_shape()) #net = tflearn.layers.recurrent.lstm(net, n_units=512) #print("after LSTM", net.get_shape()) net = dropout(net, 0.5) # Regression net = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.dropout(net, 0.5) # net, feature_layer = tflearn.fully_connected(net, 4, activation='softmax', return_logit = True) feature_layer = tflearn.fully_connected(net, 4, activation='softmax') print('feature_layer: ', feature_layer.get_shape()) print("dense", net.get_shape()) net = tflearn.regression(net, optimizer='adam',#momentum', loss='categorical_crossentropy') #,learning_rate=0.1) print('final output: ', net.get_shape()) ## save model ### load model = tflearn.DNN(net) run_id = 'resnet_6000_500_10_5_v1' model.load('../model/resNet/'+run_id) # print(tflearn.variables.get_all_variables()) ### create new model, and get features m2 = tflearn.DNN(feature_layer, session=model.session) tmp_feature = [] num_of_test = len(test_data) cur_data = [] pre = [] for i in range(num_of_test): cur_data.append(test_data[i]) if (i % 2000 == 0 or i == (num_of_test - 1)) and i !=0: #tmp_test_data = test_data[i].reshape([-1, n_dim, 1]) tmp_testX = np.array(cur_data, dtype=np.float32) tmp_feature.extend(m2.predict(tmp_testX.reshape([-1, n_dim, 1]))) cur_data = [] pre.extend(model.predict(tmp_testX)) print(i, len(tmp_feature), len(tmp_feature[0])) tmp_feature = np.array(tmp_feature) return tmp_feature
def make_core_network(net, regularizer='L2'): ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) print("reshaped input", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) #net = tflearn.conv_1d(net, 64, 16, 2, regularizer='L2', weight_decay=0.0001) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) # Residual blocks '''net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn10", net.get_shape())''' net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn10", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn12", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) print("resn14", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) print("resn16", net.get_shape()) '''net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn18", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn20", net.get_shape())''' net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') #net = tflearn.global_avg_pool(net) # LSTM print("before LSTM, before reshape", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_dim // n_split, 512]) print("before LSTM", net.get_shape()) net = bidirectional_rnn(net, BasicLSTMCell(256), BasicLSTMCell(256)) print("after LSTM", net.get_shape()) #net = tflearn.layers.recurrent.lstm(net, n_units=512) #print("after LSTM", net.get_shape()) net = dropout(net, 0.5) # Regression feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.dropout(feature_layer, 0.5) net = tflearn.fully_connected(net, 4, activation='softmax') print("dense", net.get_shape()) return net, feature_layer
import tflearn import tflearn.data_utils as du # Data loading and preprocessing import tflearn.datasets.mnist as mnist X, Y, testX, testY = mnist.load_data(one_hot=True) X = X.reshape([-1, 28, 28, 1]) testX = testX.reshape([-1, 28, 28, 1]) X, mean = du.featurewise_zero_center(X) testX = du.featurewise_zero_center(testX, mean) # Building Residual Network net = tflearn.input_data(shape=[None, 28, 28, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) # Residual blocks net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_mnist', max_checkpoints=10, tensorboard_verbose=0)
def get_deep_centerwave_feature(test_data): tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) _, n_dim = test_data.shape ############################### model net = tflearn.input_data(shape=[None, n_dim, 1]) print("input", net.get_shape()) net = tflearn.avg_pool_1d(net, kernel_size=5, strides=5) print("avg_pool_1d", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn6", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) # print("resn8", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) # print("resn10", net.get_shape()) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') print("feature_layer", feature_layer.get_shape()) net = feature_layer net = tflearn.fully_connected(net, 4, activation='softmax') print("dense", net.get_shape()) net = tflearn.regression(net, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.01) ############################### ### load model = tflearn.DNN(net) model.load( '../model/model_deep_centerwave_0810_all/model_deep_centerwave_resnet') ### create new model, and get features m2 = tflearn.DNN(feature_layer, session=model.session) out_feature = [] pred = [] num_of_test = len(test_data) for i in range(num_of_test): tmp_test_data = test_data[i].reshape([-1, n_dim, 1]) out_feature.append(m2.predict(tmp_test_data)[0]) # pred.append(model.predict(tmp_test_data)[0]) out_feature = np.array(out_feature) # ### eval # print(len(pred), pred[0], all_label[0]) # MyEval.F1Score3_num(pred, all_label[:num_of_test]) return out_feature
def get_deep_mimic_feats(test_data): n_dim = 6000 n_split = 300 tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Building Residual Network net = tflearn.input_data(shape=[None, n_dim, 1]) print("input", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) print("reshaped input", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) #net = tflearn.conv_1d(net, 64, 16, 2, regularizer='L2', weight_decay=0.0001) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) # Residual blocks net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 4, downsample=True) print("resn4", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 4, downsample=True) # print("resn6", net.get_shape()) # net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 4, downsample=True) # print("resn8", net.get_shape()) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') print("before reshape", net.get_shape()) # net = tf.reshape(net, [-1, n_dim//n_split*net.get_shape()[-2], net.get_shape()[-1]]) # LSTM ############ reshape for sub_seq before_reshaped_shape = net.get_shape().as_list() net = tf.reshape(net, [-1, n_dim//n_split, before_reshaped_shape[1]*before_reshaped_shape[2]]) print("before LSTM", net.get_shape()) net = bidirectional_rnn(net, BasicLSTMCell(64), BasicLSTMCell(64)) print("after LSTM", net.get_shape()) net = dropout(net, 0.5) # Regression net = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.fully_connected(net, 4, activation='softmax') print("dense", net.get_shape()) net = tflearn.regression(net, optimizer='adam', loss='mean_square') # Training model = tflearn.DNN(net) model.load('../model/mimic/mimic_model_offline_v4.1') pred = [] num_of_test = len(test_data) cur_data = [] for i in range(num_of_test): cur_data.append(test_data[i]) if (i % 2000 == 0 or i == (num_of_test - 1)) and i !=0: tmp_testX = np.array(cur_data, dtype=np.float32) pred.extend(model.predict(tmp_testX.reshape([-1, n_dim, 1]))) cur_data = [] return pred
X, Y, valX, valY, testX, testY = data.train.images, data.train.labels, \ data.validation.images, data.validation.labels, \ data.test.images, data.test.labels X = X.reshape([-1, 28, 28, 1]) valX = valX.reshape([-1, 28, 28, 1]) testX = testX.reshape([-1, 28, 28, 1]) X, mean = du.featurewise_zero_center(X) valX = du.featurewise_zero_center(valX, mean) testX = du.featurewise_zero_center(testX, mean) # Building Residual Network net = tflearn.input_data(shape=[None, 28, 28, 1]) net = tflearn.conv_2d(net, 64, 3, activation='relu', bias=False) # Residual blocks net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=True) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=True) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.1) # Training model = tflearn.DNN(net,
def get_resNet_proba(long_data, long_pid, model_path): all_pid = np.array(long_pid) all_feature = np.array(long_data) all_label = np.array([]) test_data, test_label, test_pid = slide_and_cut(all_feature, all_label, all_pid) pid_map = {} for i in range(len(all_pid)): pid_map[all_pid[i]] = i n_dim = 6000 n_split = 300 tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) # Building Residual Network net = tflearn.input_data(shape=[None, n_dim, 1]) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) net = tflearn.conv_1d(net, 64, 16, 2) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') # Residual blocks net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') #net = tflearn.global_avg_pool(net) # LSTM ############ reshape for sub_seq net = tf.reshape(net, [-1, n_dim // n_split, 512]) net = bidirectional_rnn(net, BasicLSTMCell(256), BasicLSTMCell(256)) net = dropout(net, 0.5) # Regression feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.dropout(feature_layer, 0.5) net = tflearn.fully_connected(net, 4, activation='softmax') net = tflearn.regression( net, optimizer='adam', #momentum', loss='categorical_crossentropy') #,learning_rate=0.1) ## save model ### load model = tflearn.DNN(net) model.load(model_path) ### create new model, and get features num_of_test = len(test_data) cur_data = [] pre = [] for i in range(num_of_test): cur_data.append(test_data[i]) if (num_of_test > 1 and (i % 2000 == 0 or i == (num_of_test - 1)) and i != 0) or (num_of_test == 1): tmp_testX = np.array(cur_data, dtype=np.float32) cur_data = [] pre.extend(model.predict(tmp_testX)) test_pid = np.array(test_pid, dtype=np.string_) y_num = len(pid_map) y_pre = [[0. for j in range(4)] for i in range(y_num)] y_sec_pre = [[0. for j in range(4)] for i in range(y_num)] y_third_pre = [[0. for j in range(4)] for i in range(y_num)] #print(y_num) for j in range(len(pre)): i_pred = np.array(pre[j], dtype=np.float32) cur_pid = str(test_pid[j], 'utf-8') list_id = pid_map[cur_pid] temp_pre = np.array(y_pre[list_id], dtype=np.float32) temp_sec_pre = np.array(y_sec_pre[list_id], dtype=np.float32) temp_third_pre = np.array(y_third_pre[list_id], dtype=np.float32) max_p = temp_pre[np.argmax(temp_pre)] max_sec_p = temp_sec_pre[np.argmax(temp_sec_pre)] max_third_p = temp_third_pre[np.argmax(temp_third_pre)] sec_p = 0 sec_sec_p = 0 sec_third_p = 0 for k in range(len(temp_pre)): if temp_pre[k] == max_p: continue if temp_pre[k] > sec_p: sec_p = temp_pre[k] if temp_sec_pre[k] == max_sec_p: continue if temp_sec_pre[k] > sec_sec_p: sec_sec_p = temp_sec_pre[k] if temp_third_pre[k] == max_third_p: continue if temp_third_pre[k] > sec_third_p: sec_third_p = temp_third_pre[k] cur_max_p = i_pred[np.argmax(i_pred)] cur_sec_p = 0 for k in range(len(i_pred)): if i_pred[k] == cur_max_p: continue if i_pred[k] > cur_sec_p: cur_sec_p = i_pred[k] if (cur_max_p - cur_sec_p) > (max_p - sec_p): y_third_pre[list_id] = y_sec_pre[list_id] y_sec_pre[list_id] = y_pre[list_id] y_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_sec_p - sec_sec_p): y_third_pre[list_id] = y_sec_pre[list_id] y_sec_pre[list_id] = i_pred elif (cur_max_p - cur_sec_p) > (max_third_p - sec_third_p): y_third_pre[list_id] = i_pred for k in range(len(y_pre)): labels = [0. for j in range(4)] pred_1 = np.argmax(y_pre[k]) labels[pred_1] += 1 pred_2 = np.argmax(y_sec_pre[k]) labels[pred_2] += 1 pred_3 = np.argmax(y_third_pre[k]) labels[pred_3] += 1 if pred_1 == 2: # and (abs(y_pre[k][np.argmax(labels)] - y_pre[k][2])/y_pre[k][np.argmax(labels)] <= 0.2): continue elif pred_2 == 2: # and (abs(y_pre[k][np.argmax(labels)] - y_sec_pre[k][2])/y_pre[k][np.argmax(labels)] <= 0.2): y_pre[k] = y_sec_pre[k] elif pred_3 == 2: # and (abs(y_pre[k][np.argmax(labels)] - y_third_pre[k][2])/y_pre[k][np.argmax(labels)] <= 0.2): y_pre[k] = y_third_pre[k] elif pred_1 != np.argmax(labels): if pred_2 == np.argmax(labels): y_pre[k] = y_sec_pre[k] return y_pre
# Residual blocks '''net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn10", net.get_shape())''' net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128,
def get_model(): n_dim = 6000 n_split = 300 tf.reset_default_graph() sess = tf.Session(config=tf.ConfigProto(log_device_placement=True)) ### split #X = X.reshape([-1, n_split, 1]) #testX = testX.reshape([-1, n_split, 1]) # Building Residual Network net = tflearn.input_data(shape=[None, n_dim, 1]) print("input", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_split, 1]) print("reshaped input", net.get_shape()) net = tflearn.conv_1d(net, 64, 16, 2) #net = tflearn.conv_1d(net, 64, 16, 2, regularizer='L2', weight_decay=0.0001) print("cov1", net.get_shape()) net = tflearn.batch_normalization(net) print("bn1", net.get_shape()) net = tflearn.activation(net, 'relu') print("relu1", net.get_shape()) # Residual blocks '''net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides = 2, downsample=True, is_first_block = True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides = 2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides = 2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides = 2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn10", net.get_shape())''' net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True, is_first_block=True) print("resn2", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 64, downsample_strides=2, downsample=True) print("resn4", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn6", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 128, downsample_strides=2, downsample=True) print("resn8", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn10", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 256, downsample_strides=2, downsample=True) print("resn12", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) print("resn14", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 512, downsample_strides=2, downsample=True) print("resn16", net.get_shape()) '''net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn18", net.get_shape()) net = tflearn.residual_bottleneck(net, 2, 16, 1024, downsample_strides = 2, downsample=True) print("resn20", net.get_shape())''' net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') #net = tflearn.global_avg_pool(net) # LSTM print("before LSTM, before reshape", net.get_shape()) ############ reshape for sub_seq net = tf.reshape(net, [-1, n_dim // n_split, 512]) print("before LSTM", net.get_shape()) net = bidirectional_rnn(net, BasicLSTMCell(256), BasicLSTMCell(256)) print("after LSTM", net.get_shape()) #net = tflearn.layers.recurrent.lstm(net, n_units=512) #print("after LSTM", net.get_shape()) net = dropout(net, 0.5) # Regression feature_layer = tflearn.fully_connected(net, 32, activation='sigmoid') net = tflearn.dropout(feature_layer, 0.5) net = tflearn.fully_connected(net, 4, activation='softmax') print("dense", net.get_shape()) net = tflearn.regression( net, optimizer='adam', #momentum', loss='categorical_crossentropy') #,learning_rate=0.1) ## save model ### load model = tflearn.DNN(net) run_id = 'resnet_6000_500_10_5_v1' model.load('../model/resNet/' + run_id) all_names = tflearn.variables.get_all_variables() print(all_names[0]) ttt = model.get_weights(all_names[0]) print(type(ttt)) print(ttt) # tflearn.variables.get_value(all_names[0], xxx) return all_names