def gen_img_type(shape,imagetype, SNRval,filename,islowres=False): glob_const=globalconstants.GlobalConstants(); init_set(glob_const); glob_const.particle_shape=shape; glob_const.image_type=imagetype; glob_const.savefile(); img_gen=image_generate.GenerateImage(SNRval); if islowres==False: img_gen.generate_img(); full_img_matrix=img_gen.noisy_imgarr; part_img_matrix=full_img_matrix[0:150,0:130]; else: img_gen.lowres_image(); full_img_matrix=img_gen.noisy_lowres; part_img_matrix=full_img_matrix[0:37,0:32]; #filename='scripts/Paper_Figures/Figure1/A_round_SNR2.png'; plt.imshow(part_img_matrix,cmap=cm.Greys_r,interpolation='none'); plt.axis('off') #plt.show() #also tried scipy.misc.imsave but this provides more #flexibility and more options plt.savefig(filename,dpi=300); plt.show(); return;
def training_round_script(): glob_const=globalconstants.GlobalConstants(); init_set(glob_const); SNRval=0.5; #training_round_list=[]; training_round_list=range(2,15); training_acc=[]; testing_acc=[]; rounds_tillnow=[]; pos_tr_eg=glob_const.no_pos_training_eg; neg_tr_eg=glob_const.no_neg_training_eg; pos_te_eg=glob_const.no_pos_testing_eg; neg_te_eg=glob_const.no_neg_testing_eg; for training_round in training_round_list: boost=adaboost_train_test.AdaBoostTrainTest(SNRval,training_round,\ pos_tr_eg,neg_tr_eg,pos_te_eg,neg_te_eg); boost.train_adaboost(); boost.test_adaboost(); training_acc.append(boost.training_accuracy); testing_acc.append(boost.testing_accuracy); rounds_tillnow.append(training_round); #this now saves the file at every iteration #if you want to quit the program before it finishes vid_utils.savefile(rounds_tillnow,training_acc,'data/adaboost_training_acc.txt') vid_utils.savefile(rounds_tillnow,testing_acc,'data/adaboost_testing_acc.txt') plt.plot(training_round_list,training_acc,'bo-',label='Training Accuracy'); plt.plot(training_round_list,testing_acc,'ro-',label='Testing Accuracy'); plt.legend(); plt.show();
def training_round_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 2. #overfitting_e_list=[]; overfitting_e_list = [0.1 * x for x in range(0, 5)] training_acc = [] testing_acc = [] list_till_now = [] for overfitting_e in overfitting_e_list: glob_const.overfitting_e = overfitting_e glob_const.savefile() traintest = train_and_test.TrainAndTest(SNRval) traintest.train_tree() traintest.test_tree() list_till_now.append(overfitting_e) training_acc.append(traintest.training_accuracy) testing_acc.append(traintest.testing_accuracy) vid_utils.savefile(list_till_now, training_acc, 'data/overfittng_e_training_acc.txt') vid_utils.savefile(list_till_now, testing_acc, 'data/overfitting_e_testing_acc.txt') print "Processed e: " + str(overfitting_e) + "...." plt.plot(overfitting_e_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(overfitting_e_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend() plt.show() pass
def training_round_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 0.5 #training_round_list=[]; training_round_list = range(2, 11) training_acc = [] testing_acc = [] for training_round in training_round_list: glob_const.training_rounds = training_round glob_const.savefile() traintest = train_and_test.TrainAndTest(SNRval) traintest.train_tree() traintest.test_tree() training_acc.append(traintest.training_accuracy) testing_acc.append(traintest.testing_accuracy) vid_utils.savefile(training_round_list, training_acc, 'data/training_acc.txt') vid_utils.savefile(training_round_list, testing_acc, 'data/testing_acc.txt') plt.plot(training_round_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(training_round_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend() plt.show() pass
def __init__(self): glob_const=globalconstants.GlobalConstants() self.height=glob_const.global_window_height; self.width=glob_const.global_window_width; #image_generate.GenerateImage(11,11); self.gray_level=50; self.firstrect=None; self.secondrect=None;
def __init__(self, SNR, no_pos_eg, no_neg_eg): example_images.ExampleImages.__init__(self, SNR, no_pos_eg, no_neg_eg) glob_const = globalconstants.GlobalConstants() self._lowres_width = glob_const.lowres_width self._lowres_height = glob_const.lowres_height self.img_width = 512 self.img_height = 512 self._ratio = None self._set_ratio()
def __init__(self): glob_const = globalconstants.GlobalConstants() self.global_window_width = glob_const.global_window_width self.global_window_height = glob_const.global_window_height self.rectlist = None #defined by subclass #Other variables, to be defined during optimization #should we initialize them here? self.threshold = None self.toggle = None self.error = None self.margin = None self.response_vector = None
def depth_script(): #important: line 199 in function train_node.py #which is self._print_histogram in create_children() #function is uncommented for this script to run #comment it back and reinstall after the script runs #this also depends on information from self._print_loginfo #in tree_node.py glob_const=globalconstants.GlobalConstants(); init_set(glob_const); SNRval=0.5; traintest=train_and_test.TrainAndTest(SNRval); traintest.train_tree();
def exampleno_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 0.5 #exampleno_list=[]; exampleno_list = range(50, 1000, 50) training_acc = [] testing_acc = [] list_tillnow = [] time_list = [] training_rounds = 15 pos_te_eg = glob_const.no_pos_testing_eg neg_te_eg = glob_const.no_neg_testing_eg for exampleno in exampleno_list: starttime = time.time() glob_const.no_pos_training_eg = exampleno glob_const.no_neg_training_eg = exampleno glob_const.savefile() boost=adaboost_train_test.AdaBoostTrainTest(SNRval,training_rounds,\ exampleno,exampleno,pos_te_eg,neg_te_eg) boost.train_adaboost() boost.test_adaboost() elapsed = time.time() - starttime #append values training_acc.append(boost.training_accuracy) testing_acc.append(boost.testing_accuracy) list_tillnow.append(exampleno) time_list.append(elapsed) vid_utils.savefile(list_tillnow,training_acc,\ 'data/training_acc_eg.txt') vid_utils.savefile(list_tillnow,testing_acc,\ 'data/testing_acc_eg.txt') vid_utils.savefile(list_tillnow,time_list,\ 'data/time_vals.txt') plt.plot(exampleno_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(exampleno_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend() plt.show()
def compare_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) #SNRlist=[]; SNRlist = [0.25 * x for x in range(8, 9)] tree_acc = [] boost_acc = [] training_rounds = 7 pos_tr_eg = glob_const.no_pos_training_eg neg_tr_eg = glob_const.no_neg_training_eg pos_te_eg = glob_const.no_pos_testing_eg neg_te_eg = glob_const.no_neg_testing_eg list_till_now = [] for SNRval in SNRlist: boost=adaboost_train_test.AdaBoostTrainTest(SNRval,training_rounds,\ pos_tr_eg,neg_tr_eg,pos_te_eg,neg_te_eg) traintest = train_and_test.TrainAndTest(SNRval) #Train boost.train_adaboost() traintest.train_tree() #Test boost.test_adaboost() traintest.test_tree() #traintest.post_prune_tree(); #traintest.test_tree(); #get accuracies boost_acc.append(boost.testing_accuracy) tree_acc.append(traintest.testing_accuracy) list_till_now.append(SNRval) vid_utils.savefile(list_till_now, tree_acc, "data/Tree_accuracies.txt") vid_utils.savefile(list_till_now, boost_acc, "data/Boost_accuracies.txt") print "Processed SNR: " + str(SNRval) + "..." plt.plot(SNRlist, tree_acc, 'ro', label="Tree") plt.plot(SNRlist, boost_acc, 'bo', label="Adaboost") plt.xlim(0, 2.5) plt.legend(loc=2) plt.show()
def training_round_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 2. #exampleno_list=[]; round_list = range(5, 30, 5) training_acc = [] testing_acc = [] list_tillnow = [] time_list = [] pos_te_eg = glob_const.no_pos_testing_eg neg_te_eg = glob_const.no_neg_testing_eg for training_rounds in round_list: starttime = time.time() pos_training_eg = glob_const.no_pos_training_eg neg_training_eg = glob_const.no_neg_training_eg glob_const.savefile() boost=adaboost_train_test.AdaBoostTrainTest(SNRval,training_rounds,\ pos_training_eg,neg_training_eg,pos_te_eg,neg_te_eg) boost.train_adaboost() boost.test_adaboost() elapsed = time.time() - starttime #append values training_acc.append(boost.training_accuracy) testing_acc.append(boost.testing_accuracy) list_tillnow.append(training_rounds) time_list.append(elapsed) vid_utils.savefile(list_tillnow,training_acc,\ 'data/training_acc_trainingrounds.txt') vid_utils.savefile(list_tillnow,testing_acc,\ 'data/testing_acc_trainingrounds.txt') vid_utils.savefile(list_tillnow,time_list,\ 'data/time_vals.txt') plt.plot(round_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(round_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend(loc=2) plt.show()
def training_round_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) examples = glob_const.no_pos_training_eg init_t = time.time() SNRval = 0.5 traintest = train_and_test.TrainAndTest(SNRval) traintest.train_tree() print "Trained: " + str(time.time() - init_t) init_t = time.time() traintest.test_tree() print "Tested: " + str(time.time() - init_t) init_t = time.time() traintest.post_prune_tree() print "Post Pruned: " + str(time.time() - init_t) return
def __init__(self,SNR,pos_eg_no=10,neg_eg_no=10): glob_const=globalconstants.GlobalConstants(); self.global_window_width=glob_const.global_window_width; self.global_window_height=glob_const.global_window_height; self.SNR=SNR;#we should be able to change SNR from the highest point in the program self._img_gen=image_generate.GenerateImage(self.SNR); self._img_width=self._img_gen.width; self._img_height=self._img_gen.height; self.num_particles=self._img_gen.part_rows*self._img_gen.part_columns; #training examples self.training_examples=None; if pos_eg_no ==0 and neg_eg_no==0: raise ValueError("Cannot create instance with 0 positive and 0 negative examples") if pos_eg_no>=0: self._num_pos_eg=pos_eg_no;#public variable: num_pos_eg else: raise ValueError("number of positive examples cannot be negative") if neg_eg_no>=0: self._num_neg_eg=neg_eg_no;#public variable: num_neg_eg else: raise ValueError("number of positive examples cannot be negative") #example weights: self._pos_example_weights=None; self._neg_example_weights=None; self._set_example_weights(); self.num_neg_eg_img=self.num_particles;#this is the no. of negative examples generated per image self._eg_filename='_examples.pickle'; #random number generator random.seed(); #to be defined later self.test_img=None;
def roc_script(SNRval,particle_shape,image_type): glob_const=globalconstants.GlobalConstants(); init_set(glob_const); glob_const.image_type=image_type; glob_const.particle_shape=particle_shape; glob_const.savefile(); traintest=train_and_test.TrainAndTest(SNRval); traintest.train_tree(); traintest.test_tree(); [precision,recall]=traintest.get_precision_recall_curve(); precision_filename="data/precision_"+str(SNRval)+"_"+str(image_type)+\ "_"+str(particle_shape)+".txt"; vid_utils.savefile(precision,recall,precision_filename) [tpr,fpr]=traintest.get_roc_curve(); roc_filename="data/roc_"+str(SNRval)+"_"+str(image_type)+\ "_"+str(particle_shape)+".txt"; vid_utils.savefile(tpr,fpr,roc_filename)
def exampleno_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 0.5 #exampleno_list=[]; exampleno_list = range(50, 250, 50) training_acc = [] testing_acc = [] list_tillnow = [] time_list = [] for exampleno in exampleno_list: starttime = time.time() glob_const.no_pos_training_eg = exampleno glob_const.no_neg_training_eg = exampleno glob_const.savefile() traintest = train_and_test.TrainAndTest(SNRval) traintest.train_tree() traintest.test_tree() elapsed = time.time() - starttime #append values training_acc.append(traintest.training_accuracy) testing_acc.append(traintest.testing_accuracy) list_tillnow.append(exampleno) time_list.append(elapsed) vid_utils.savefile(list_tillnow,training_acc,\ 'data/training_acc_eg.txt') vid_utils.savefile(list_tillnow,testing_acc,\ 'data/testing_acc_eg.txt') vid_utils.savefile(list_tillnow,time_list,\ 'data/time_vals.txt') plt.plot(exampleno_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(exampleno_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend() plt.show()
def gamma_script(): glob_const = globalconstants.GlobalConstants() init_set(glob_const) SNRval = 2. #maxdepth_list=[]; #gamma_list=range(1,7); gamma_list = [0] training_acc = [] testing_acc = [] list_tillnow = [] time_list = [] for gamma in gamma_list: starttime = time.time() glob_const.gamma = gamma glob_const.savefile() traintest = train_and_test.TrainAndTest(SNRval) traintest.train_tree() traintest.test_tree() elapsed = time.time() - starttime #append values training_acc.append(traintest.training_accuracy) testing_acc.append(traintest.testing_accuracy) list_tillnow.append(gamma) time_list.append(elapsed) vid_utils.savefile(list_tillnow,training_acc,\ 'data/training_acc_gamma.txt') vid_utils.savefile(list_tillnow,testing_acc,\ 'data/testing_acc_gamma.txt') vid_utils.savefile(list_tillnow,time_list,\ 'data/time_vals_depth.txt') plt.plot(gamma_list, training_acc, 'bo-', label='Training Accuracy') plt.plot(gamma_list, testing_acc, 'ro-', label='Testing Accuracy') plt.legend() plt.show()
def __init__(self, training_rounds=10, conv_theta=0.45): self.training_rounds = training_rounds #list of features; glob_const = globalconstants.GlobalConstants() self.global_window_width = glob_const.global_window_width self.global_window_height = glob_const.global_window_height #logging.basicConfig(filename='logfile.txt',level=logging.DEBUG) self.conv_theta = conv_theta #saving stuff self._feature_filename = 'data/feature_list.pickle' self._response_matrix_filename = 'data/response_matrix.pickle' #to be defined later self._features = None self._training_examples = None self._no_pos_training_eg = None self._no_neg_training_eg = None self.adaboost_rule = None self.log_loss = None self.training_error = None self._int_image_list = None
import pbtspot from pbtspot import set_default,train_and_test, globalconstants import matplotlib.pyplot as plt set_default.set_default_constants(); #set constants glob_const=globalconstants.GlobalConstants(); glob_const.no_pos_training_eg=50; glob_const.no_neg_training_eg=50; glob_const.no_pos_testing_eg=1000; glob_const.no_neg_testing_eg=1000; glob_const.savefile(); #main script SNR=0.5; train_test=train_and_test.TrainAndTest(SNR); train_test.load_saved_tree(); #train_test.train_tree(); #train_test.save_tree(); train_test.test_tree(); [tpr,fpr]=train_test.get_roc_curve(); plt.plot(fpr,tpr,'ro-'); plt.xlim(-0.05, 1.05) plt.ylim(-0.05,1.05) plt.xlabel("False Positive Rate") plt.ylabel("True Positive Rate") print "tpr: "+str(tpr) print "fpr: "+str(fpr) plt.show();
def __init__(self, SNR, width=512, height=512): """ Generates a synthetic image of particles @param: SNR: float, signal to noise ratio @param width:integer, image width @param height: integer, image height public methods: def self.generate_img() def self.reset_image() def save_image(self,filename='A.jpg',highres=True): def lowres_image(self) public variables: self.noisy_imgarr self.particle_loc self.scaled_noisy_imgarr self.noisy_lowres self.scaled_noisy_lowres """ if (height <= 0): raise ValueError("Height must be positive:%d" % height) if (width <= 0): raise ValueError("Width must be positive:%d" % width) #from global values glob_const = globalconstants.GlobalConstants() self.global_window_width = glob_const.global_window_width self.global_window_height = glob_const.global_window_height self.particle_shape = glob_const.particle_shape #round or elongated self.image_type = glob_const.image_type #image type, determining the type of noise #To be defined later self.part_arr = None #image array containing only particle Gaussians self.imgarr = None #image array with particles and background self.noisy_imgarr = None # noisy array, after adding Poisson noise to self.imgarr self.scaled_noisy_imgarr = None #after scaling noisy_imgarr, so that mean=0, sttdev=1 self.particle_loc = None #array of particle coordinates self.bg_noise = None #Only when required to generate low resolution images self.noisy_lowres = None self.scaled_noisy_lowres = None #consider putting these to global constants as well self.lowres_height = 128 self.lowres_width = 128 #from paper, resolution of camera is: #delta_x=50nm, delta_y=50nm. Thus all values are multiples of #50nm. So, sigma_x=2 implies sigma_x=100nm, since 1 pixel=50nm #no. of particles: rows and columns self.part_rows = 16 #by default self.part_columns = 16 #std. dev for the Gaussian, in pixels self.sigma_sym = 2. self.sigma_major = 4. if self.particle_shape == 'round': self.sigma_x = self.sigma_sym self.sigma_y = self.sigma_sym elif self.particle_shape == 'elongated': self.sigma_x = self.sigma_major self.sigma_y = self.sigma_sym self.height = height self.width = width #Image parameters self.SNR = SNR self.background_level = 10
#Always run this script before other scripts to give default values to global variables #ignore at your own peril #do not change default values here #change values in specific scripts #these are default values and should NOT be touched import pbtspot from pbtspot import globalconstants default_const=globalconstants.GlobalConstants(); #write all default values default_const.particle_shape='round' default_const.global_window_width=10; default_const.global_window_height=10; default_const.image_type='A' #default values end here default_const.savefile();