def run_batch(datasetName, strategies, neurons, neuronLayersCount, maxpoolings, fullLayers, batch_size, learning, train_decay, epoches, following_strategy, trees, learning_sample_ratios, dataset_format): #for time_counts in range(experiment_times): mix_model_svm_ratio = 0 #print strategies print learning_sample_ratios file_name, neighbors, raws_size, lines_size = data_util.prepare( learning_sample_ratios, datasetName, int(strategies), 2) neighbors = neighbors + 1 print "the neighbors strategy is: " + str(neighbors) print "starting ..." HSICNN.run_network(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay, epoches) print "the training of the network have done." if following_strategy == 1: print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) elif following_strategy == 2: print "now processing the cnn + rf joint framework..." cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) elif following_strategy == 3: print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) print "now processing the cnn + rf joint framework..." cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size, -1) file = open(file_name + "_experiment_description.txt", 'w') file.write("-------------Experiment Description-------------\n") file.write("Data set:" + file_name + "#\n") file.write("neighbor strategy:" + str(neighbors) + "#\n") file.write("Convolutional Neurons:" + str(neurons) + "#\n") file.write("Each Convolutional Neuron operate " + str(neuronLayersCount) + '#\n') file.write("Max Polling Kernel Size:" + str(maxpoolings) + "#\n") file.write("Full Layer Neuron number:" + str(fullLayers) + "#\n") file.write("Batch size of SGD training:" + str(batch_size) + "#\n") file.write("Training epoches of deep CNN:" + str(epoches) + "#\n") file.write("Learning ratio:" + str(learning) + "#\n") file.write("Train decay:" + str(train_decay) + "#\n") if following_strategy == 2 or following_strategy == 3: file.write("Number of trees in random forest: " + str(trees) + "#\n") file.write("===============================================\n") file.close() return file_name
def run_batch(datasetName,strategies, neurons, neuronLayersCount, maxpoolings, fullLayers, batch_size, learning, train_decay, epoches, following_strategy, trees, learning_sample_ratios, dataset_format): #for time_counts in range(experiment_times): mix_model_svm_ratio = 0 #print strategies print learning_sample_ratios file_name, neighbors, raws_size, lines_size = data_util.prepare(learning_sample_ratios, datasetName, int(strategies), 2) neighbors = neighbors + 1 print "the neighbors strategy is: " + str(neighbors) print "starting ..." HSICNN.run_network(file_name, neurons,neuronLayersCount, neighbors, maxpoolings, fullLayers,batch_size, learning, train_decay, epoches) print "the training of the network have done." if following_strategy == 1: print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) elif following_strategy == 2: print "now processing the cnn + rf joint framework..." cnnrf.run(file_name,trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) elif following_strategy == 3: print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) print "now processing the cnn + rf joint framework..." cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size,lines_size,-1) file = open(file_name + "_experiment_description.txt", 'w') file.write("-------------Experiment Description-------------\n") file.write("Data set:" + file_name + "#\n") file.write("neighbor strategy:" + str(neighbors) + "#\n") file.write("Convolutional Neurons:" + str(neurons) + "#\n") file.write("Each Convolutional Neuron operate " + str(neuronLayersCount) + '#\n') file.write("Max Polling Kernel Size:" + str(maxpoolings) + "#\n") file.write("Full Layer Neuron number:" + str(fullLayers) + "#\n") file.write("Batch size of SGD training:" + str(batch_size) + "#\n") file.write("Training epoches of deep CNN:" + str(epoches) + "#\n") file.write("Learning ratio:" + str(learning) + "#\n") file.write("Train decay:" + str(train_decay) +"#\n") if following_strategy == 2 or following_strategy == 3: file.write("Number of trees in random forest: " + str(trees) + "#\n") file.write("===============================================\n") file.close() return file_name
def complete_operate(operate_type, folder_path, trees, neurons, neuronLayersCount, maxpoolings, fullLayers): #TODO 添加增强健壮性的代码 if os.path.isdir(folder_path): if (operate_type != '5'): print "Processing " + folder_path #首先根据目录名字拿到数据集的文件名 file_name_split = folder_path.split('/') file_or_folder_name = file_name_split[len(file_name_split) - 1] dataset_name_sub = file_or_folder_name.split('_') #TODO:添加判断目录是否为有效实验结果的代码,有时候非实验结果的文件夹会不小心混在目录中,应该予以剔除 # print dataset_name_sub dataset_name = dataset_name_sub[0] + '_' + dataset_name_sub[ 1] + '_' + dataset_name_sub[2] #搜寻该文件夹中的网络配置文件 #TODO:统一的网络配置文件格式 #查询已有框架自动恢复网络结构的方式??? #TODO:与下面的else配套 #neurons = 0 #neuronLayersCount = 0 neighbors = int(dataset_name_sub[1]) + 1 #maxpoolings = 0 #fullLayers = 0 raws = 0 lines = 0 batch_size = 100 ratio = 0.001 decay = 0.00001 LabelsMat = sio.loadmat(data_prefix + dataset_name_sub[0] + '/' + dataset_name_sub[0] + 'Gt.mat') key_label_name = LabelsMat.keys() label_key = '' for temp_key in key_label_name: if temp_key != '__version__' and temp_key != '__header__' and temp_key != '__globals__': label_key = temp_key break Labels = LabelsMat[label_key] raws = int(len(Labels)) lines = int(len(Labels[0])) if operate_type == '1': print 'CNN+RF base on trained CNN model.' if len(trees.split('-')) > 0: small_tree_number = trees.split('-')[0] big_tree_number = trees.split('-')[1] exp_trees = range(int(small_tree_number), int(big_tree_number) + 1) cnnrf_acc_list = [] rf_acc_list = [] for trees_number in exp_trees: print "Tree Number:" + str(trees_number) cnnrf_acc, rf_acc = cnnrf.run(folder_path + "/" + dataset_name, trees_number, int(neurons), int(neuronLayersCount), int(neighbors), int(maxpoolings), int(fullLayers), raws, lines, test_cnn=-1) # cnnrf.run(file_name,trees, neurons, neuronLayersCount, /neighbors, maxpoolings, fullLayers, raws_size, lines_size) cnnrf_acc_list.append(cnnrf_acc) rf_acc_list.append(rf_acc) sio.savemat(folder_path + "/ALL_CNN_RF_EXP_RESULT.mat", { 'cnnrf': cnnrf_acc_list, 'rf': rf_acc_list }) else: cnnrf.run(dataset_name, trees_number, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, 0, 0, 0, raws, lines) elif operate_type == '2': print 'Rebuild CNN_+ SVM model experiments...' cnnsvm.run(folder_path + "/" + dataset_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, 1, 1, 1) elif operate_type == '3': print 'TODO' elif operate_type == '4': print 'TODO' # logic to compute the OA, AA and kappa index. elif operate_type == '5': #print 'Computing OA, AA and Kappa for ' + folder_path train_ratio = 0 neighbor_strategy = 0 folder_name_current = folder_path.split('_') dataset_name = folder_name_current[0].split('/')[3] strategy_current = folder_name_current[1] training_ratio_current = folder_name_current[2] current_dataset = dataset_name + "_" + strategy_current + "_" + training_ratio_current cnn_result = current_dataset + "CNNOnlyResult.mat" ccr_result = current_dataset + "CNNRFResult.mat" ccr_result_backup = current_dataset + "_trees_200_CNNRFResult.mat" ccs_result = current_dataset + "CNNSVMResult.mat" rf_result = current_dataset + "RFonlyResult.mat" svm_result = current_dataset + "SVMonlyResult.mat" results_index = [ cnn_result, ccr_result, ccr_result_backup, ccs_result, rf_result, svm_result ] #检查并创建sum_up_results目录用于存放所有文件 sum_up_result_folder = "../experiments/" + dataset_name + "/SumResults" if not os.path.exists(sum_up_result_folder): os.makedirs(sum_up_result_folder) for temp_index in results_index: if os.path.exists(folder_path + "/" + temp_index): #开始取出结果并汇总 data = sio.loadmat(folder_path + "/" + temp_index) actual = data['actual'][0] predict = data['predict'][0] file_name = sum_up_result_folder + "/" + temp_index if os.path.exists(file_name): data_old = sio.loadmat(file_name) actual_old = data_old['actual'][0] predict_old = data_old['predict'][0] actual_new = np.append(actual_old, actual) predict_new = np.append(predict_old, predict) sio.savemat(file_name, { 'actual': actual_new, 'predict': predict_new }, appendmat=False) else: sio.savemat(file_name, { 'actual': actual, 'predict': predict }) else: print 'Not under selection list, skip it.' else: print "not a folder, skipt it."
def run_single(learning_ratio, network_type): prompt = '>' #mix_model_svm_ratio是为了以后采用组合混合模型的时候,保存一个svm在所有模型中的占比。以后根据需求进行扩充。。。TODO mix_model_svm_ratio = 0 file_name, neighbors, raws_size, lines_size = data_util.prepare( learning_ratio, "NONE", 0, 2) print "now constructing the network..." #print "enter the layers each convolutional kernel covers: " #neighbors = int(raw_input(prompt)) neighbors = neighbors + 1 print "the neighbors strategy is: " + str(neighbors) print "enter the number of convolutional neurons:" neurons = int(raw_input(prompt)) print "enter the number of layers you want the CNN to operate convolutional operation:" neuronLayersCount = int(raw_input(prompt)) print "enter the kernel size of the maxpooling layer:" maxpoolings = int(raw_input(prompt)) print "enter the number of full layers\' neurons, default is 100:" fullLayers = int(raw_input(prompt)) #if tempfullLayers > 1: # fullLayers = tempfullLayers print "enter the batch size for bsgd:" batch_size = int(raw_input(prompt)) print "enter the learning ratio:" learning = float(raw_input(prompt)) print "enter the train decay:" train_decay = float(raw_input(prompt)) print "enter the epoches you want the network to be trained:" epoches = int(raw_input(prompt)) print "now choose the following strategy after the cnn network been trained:" print "#1:train a cnn-svm joint framework;" print "#2:train a cnn-rf joint framework;" print "#3:train both cnn-svm and cnn-rf joint frameworks;" print "#4:TODO: train a mix assemble cnn-classifier model." print "#5: train a CNN model only." following_strategy = int(raw_input(prompt)) if following_strategy == 4: print "enter the ratio of svm classifier:" mix_model_svm_ratio = int(row_input(prompt)) tress = 0 if following_strategy == 2 or following_strategy == 3: print "enter the count of trees you want to set in Random Forest:" trees = int(raw_input(prompt)) print "starting ..." if network_type == '1': HSICNN.run_network(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay, epoches) elif network_type == '2': hic.run_network(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay, epoches) print "the training of the network have done." if following_strategy == 1 and following_strategy != 5: #CNN + SVM print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) elif following_strategy == 2 and following_strategy != 5: #CNN + rfind print "now processing the cnn + rf joint framework..." # print "enter the count of trees you want to set in Random Forest:" # trees = int(raw_input(prompt)) cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) elif following_strategy == 3 != following_strategy != 5: #CNN+svm and CNN+RF print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) print "now processing the cnn + rf joint framework..." cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) file = open(file_name + "_experiment_description.txt", 'w') file.write("-------------Experiment Description-------------\n") file.write("Data set:" + file_name + "#\n") file.write("neighbor strategy:" + str(neighbors) + "#\n") file.write("Convolutional Neurons:" + str(neurons) + "#\n") file.write("Each convolutional neuron operates " + str(neuronLayersCount)) file.write("Max Polling Kernel Size:" + str(maxpoolings) + "#\n") file.write("Full Layer Neuron number:" + str(fullLayers) + "#\n") file.write("Batch size of SGD training:" + str(batch_size) + "#\n") file.write("Training epoches of deep CNN:" + str(epoches) + "#\n") file.write("Learning ratio:" + str(learning) + "#\n") file.write("Train decay:" + str(train_decay) + "#\n") if following_strategy == 2 or following_strategy == 3: file.write("Number of trees in random forest: " + str(trees) + "#\n") file.write("===============================================\n") file.close() return file_name
def complete_operate(operate_type, folder_path, trees, neurons, neuronLayersCount,maxpoolings, fullLayers): #TODO 添加增强健壮性的代码 if os.path.isdir(folder_path): if(operate_type != '5'): print "Processing " + folder_path #首先根据目录名字拿到数据集的文件名 file_name_split = folder_path.split('/') file_or_folder_name = file_name_split[len(file_name_split) - 1] dataset_name_sub = file_or_folder_name.split('_') #TODO:添加判断目录是否为有效实验结果的代码,有时候非实验结果的文件夹会不小心混在目录中,应该予以剔除 # print dataset_name_sub dataset_name = dataset_name_sub[0] + '_' + dataset_name_sub[1] + '_' + dataset_name_sub[2] #搜寻该文件夹中的网络配置文件 #TODO:统一的网络配置文件格式 #查询已有框架自动恢复网络结构的方式??? #TODO:与下面的else配套 #neurons = 0 #neuronLayersCount = 0 neighbors = int(dataset_name_sub[1]) + 1 #maxpoolings = 0 #fullLayers = 0 raws = 0 lines = 0 batch_size = 100 ratio = 0.001 decay = 0.00001 LabelsMat = sio.loadmat(data_prefix + dataset_name_sub[0] + '/' + dataset_name_sub[0] + 'Gt.mat') key_label_name = LabelsMat.keys() label_key = '' for temp_key in key_label_name: if temp_key != '__version__' and temp_key != '__header__' and temp_key != '__globals__': label_key = temp_key break Labels = LabelsMat[label_key] raws = int(len(Labels)) lines = int(len(Labels[0])) if operate_type == '1': print 'CNN+RF base on trained CNN model.' if len(trees.split('-')) > 0: small_tree_number = trees.split('-')[0] big_tree_number = trees.split('-')[1] exp_trees = range(int(small_tree_number),int(big_tree_number) + 1) cnnrf_acc_list = [] rf_acc_list = [] for trees_number in exp_trees: print "Tree Number:" + str(trees_number) cnnrf_acc, rf_acc = cnnrf.run(folder_path + "/" + dataset_name,trees_number, int(neurons), int(neuronLayersCount), int(neighbors), int(maxpoolings), int(fullLayers), raws, lines,test_cnn = -1) # cnnrf.run(file_name,trees, neurons, neuronLayersCount, /neighbors, maxpoolings, fullLayers, raws_size, lines_size) cnnrf_acc_list.append(cnnrf_acc) rf_acc_list.append(rf_acc) sio.savemat(folder_path + "/ALL_CNN_RF_EXP_RESULT.mat",{'cnnrf':cnnrf_acc_list, 'rf':rf_acc_list}) else: cnnrf.run(dataset_name,trees_number, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, 0, 0, 0, raws, lines) elif operate_type == '2': print 'Rebuild CNN_+ SVM model experiments...' cnnsvm.run(folder_path + "/" + dataset_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, 1, 1, 1) elif operate_type == '3': print 'TODO' elif operate_type == '4': print 'TODO' # logic to compute the OA, AA and kappa index. elif operate_type == '5': #print 'Computing OA, AA and Kappa for ' + folder_path train_ratio = 0 neighbor_strategy = 0 folder_name_current = folder_path.split('_') dataset_name = folder_name_current[0].split('/')[3] strategy_current = folder_name_current[1] training_ratio_current = folder_name_current[2] current_dataset = dataset_name + "_" + strategy_current + "_" + training_ratio_current cnn_result = current_dataset + "CNNOnlyResult.mat" ccr_result = current_dataset + "CNNRFResult.mat" ccr_result_backup = current_dataset + "_trees_200_CNNRFResult.mat" ccs_result = current_dataset + "CNNSVMResult.mat" rf_result = current_dataset + "RFonlyResult.mat" svm_result = current_dataset + "SVMonlyResult.mat" results_index = [cnn_result, ccr_result, ccr_result_backup, ccs_result, rf_result, svm_result] #检查并创建sum_up_results目录用于存放所有文件 sum_up_result_folder = "../experiments/" + dataset_name + "/SumResults" if not os.path.exists(sum_up_result_folder): os.makedirs(sum_up_result_folder) for temp_index in results_index: if os.path.exists(folder_path + "/" + temp_index): #开始取出结果并汇总 data = sio.loadmat(folder_path + "/" + temp_index) actual = data['actual'][0] predict = data['predict'][0] file_name = sum_up_result_folder + "/" + temp_index if os.path.exists(file_name): data_old = sio.loadmat(file_name) actual_old = data_old['actual'][0] predict_old = data_old['predict'][0] actual_new = np.append(actual_old, actual) predict_new = np.append(predict_old, predict) sio.savemat(file_name,{'actual':actual_new,'predict':predict_new},appendmat=False) else: sio.savemat(file_name,{'actual':actual, 'predict':predict}) else: print 'Not under selection list, skip it.' else: print "not a folder, skipt it."
def run_single(learning_ratio, network_type): prompt = '>' #mix_model_svm_ratio是为了以后采用组合混合模型的时候,保存一个svm在所有模型中的占比。以后根据需求进行扩充。。。TODO mix_model_svm_ratio = 0 file_name, neighbors, raws_size, lines_size = data_util.prepare(learning_ratio,"NONE", 0, 2) print "now constructing the network..." #print "enter the layers each convolutional kernel covers: " #neighbors = int(raw_input(prompt)) neighbors = neighbors + 1 print "the neighbors strategy is: " + str(neighbors) print "enter the number of convolutional neurons:" neurons = int(raw_input(prompt)) print "enter the number of layers you want the CNN to operate convolutional operation:" neuronLayersCount = int(raw_input(prompt)) print "enter the kernel size of the maxpooling layer:" maxpoolings = int(raw_input(prompt)) print "enter the number of full layers\' neurons, default is 100:" fullLayers = int(raw_input(prompt)) #if tempfullLayers > 1: # fullLayers = tempfullLayers print "enter the batch size for bsgd:" batch_size = int(raw_input(prompt)) print "enter the learning ratio:" learning = float(raw_input(prompt)) print "enter the train decay:" train_decay = float(raw_input(prompt)) print "enter the epoches you want the network to be trained:" epoches = int(raw_input(prompt)) print "now choose the following strategy after the cnn network been trained:" print "#1:train a cnn-svm joint framework;" print "#2:train a cnn-rf joint framework;" print "#3:train both cnn-svm and cnn-rf joint frameworks;" print "#4:TODO: train a mix assemble cnn-classifier model." print "#5: train a CNN model only." following_strategy = int(raw_input(prompt)) if following_strategy == 4: print "enter the ratio of svm classifier:" mix_model_svm_ratio = int(row_input(prompt)) tress = 0 if following_strategy == 2 or following_strategy == 3: print "enter the count of trees you want to set in Random Forest:" trees = int(raw_input(prompt)) print "starting ..." if network_type == '1': HSICNN.run_network(file_name, neurons,neuronLayersCount, neighbors, maxpoolings, fullLayers,batch_size, learning, train_decay, epoches) elif network_type == '2': hic.run_network(file_name, neurons,neuronLayersCount, neighbors, maxpoolings, fullLayers,batch_size, learning, train_decay, epoches) print "the training of the network have done." if following_strategy == 1 and following_strategy != 5: #CNN + SVM print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) elif following_strategy == 2 and following_strategy != 5: #CNN + rfind print "now processing the cnn + rf joint framework..." # print "enter the count of trees you want to set in Random Forest:" # trees = int(raw_input(prompt)) cnnrf.run(file_name,trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) elif following_strategy == 3 != following_strategy != 5: #CNN+svm and CNN+RF print "now processing the cnn + svm joint framework..." cnnsvm.run(file_name, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, batch_size, learning, train_decay) print "now processing the cnn + rf joint framework..." cnnrf.run(file_name, trees, neurons, neuronLayersCount, neighbors, maxpoolings, fullLayers, raws_size, lines_size) file = open(file_name + "_experiment_description.txt", 'w') file.write("-------------Experiment Description-------------\n") file.write("Data set:" + file_name + "#\n") file.write("neighbor strategy:" + str(neighbors) + "#\n") file.write("Convolutional Neurons:" + str(neurons) + "#\n") file.write("Each convolutional neuron operates " + str(neuronLayersCount)) file.write("Max Polling Kernel Size:" + str(maxpoolings) + "#\n") file.write("Full Layer Neuron number:" + str(fullLayers) + "#\n") file.write("Batch size of SGD training:" + str(batch_size) + "#\n") file.write("Training epoches of deep CNN:" + str(epoches) + "#\n") file.write("Learning ratio:" + str(learning) + "#\n") file.write("Train decay:" + str(train_decay) +"#\n") if following_strategy == 2 or following_strategy == 3: file.write("Number of trees in random forest: " + str(trees) + "#\n") file.write("===============================================\n") file.close() return file_name