Ejemplo n.º 1
0
def run_recognition():
    training_data_path = "training-data/"
    test_data_path = "test-data"
    # detection_classifier_path = "opencv/sources/data/lbpcascades/lbpcascade_frontalface_improved.xml"
    detection_classifier_path = "opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml"
    # detection_classifier_path = "opencv/sources/data/haarcascades_cuda/haarcascade_frontalface_default.xml"

    detection_classifier = Initializer.load_detection_classifier(
        detection_classifier_path)

    name_list = get_name_list(training_data_path)

    face_list, label_list = preprocess(training_data_path,
                                       detection_classifier)
    trained_recognizer = train_recognizer(face_list,
                                          label_list,
                                          opencv_recognizer_type="LBPH")

    for image_name in os.listdir(test_data_path):
        image_path = Path(test_data_path, image_name)
        img = Initializer.load_image(image_path)
        ret_img, ret_code = get_recognition(img, name_list,
                                            detection_classifier,
                                            trained_recognizer)
        Initializer.display_img("title", ret_img)
Ejemplo n.º 2
0
 def get_net_pair(self):
     net_pair = []
     for i in range(len(init.gridParameters(grid_info)['netInfo'])):
         twoPinNum = twoPinNum + (
             init.gridParameters(grid_info)['netInfo'][i]['numPins'] - 1)
         twoPinNumEachNet.append(
             init.gridParameters(grid_info)['netInfo'][i]['numPins'] - 1)
Ejemplo n.º 3
0
def unit_test():
    weka_file = "genevia_default.arff"
    weka_data = WekaData()
    Initializer.init_weka_data(weka_data)
    for a in AVAILABLE_ALGORITHMS.keys():
        weka_data.addAlgorithm(AVAILABLE_ALGORITHMS[a])
    WekaPrimer.write_to_file(weka_data, weka_file)
    run_weka(weka_data, weka_file=weka_file)
Ejemplo n.º 4
0
    def test_subgraph_sum(self):

        random_graph = ini.make_random_topology(400, 3)
        subgraph = ini.find_subgraph(random_graph)
        storage_capacity = ini.find_storage_capacity(random_graph, subgraph)
        subgraph_capacity_sum = ini.subgraph_capacity_sum(storage_capacity)
        self.assertTrue(subgraph_capacity_sum,
                        type(subgraph_capacity_sum) == list)
Ejemplo n.º 5
0
    def __init__(self, n_in, n_out, ini):

        self.input = None
        #init W and b using the given initializer
        self.W = ini.get_W(n_in, n_out)
        self.b = ini.get_b(n_out)

        #create instance variables for grad_w and grad_b
        self.grad_W = np.zeros(self.W.shape)
        self.grad_b = np.zeros(self.b.shape)
Ejemplo n.º 6
0
def get_num_discarded(data_path, detection_classifier):
    discard_total = 0
    image_total = 0
    dirs = os.listdir(
        data_path
    )  # get names of everything one level below training_data_path
    for dir in dirs:
        dir_path = Path(data_path, dir)
        if os.path.isdir(dir_path):  # only keep directories
            images = os.listdir(str(dir_path))
            for image in images:
                image_path = Path(dir_path, image)
                if imghdr.what(image_path
                               ) != None:  # check to make sure it's an image
                    img = Initializer.load_image(image_path)
                    image_total += 1
                    face_tuple = FaceDetector.get_faces(
                        img, detection_classifier)
                    if len(
                            face_tuple
                    ) <= 0:  # if one face detected, keep in training data, else skip
                        discard_total += 1
                        continue

    return discard_total, image_total
Ejemplo n.º 7
0
    def __init__(self, population_size, lamda, archive, landscape,
                 select_meth):

        self.population_size = population_size
        self.lamda = lamda
        self.select_meth = select_meth
        self.archive = archive
        self.initializer = Initializer.Initializer(landscape, population_size)
        self.landscape = landscape
Ejemplo n.º 8
0
def detect_faces(img, opencv_classifier):
    gray_img = Initializer.cvt2GRAY(
        img)  # convert to grayscale bc opencv classifiers expect
    # get list of coordinates (rectangle) for all faces
    #face_list = opencv_classifier.detectMultiScale(gray_img, scaleFactor=1.2, minNeighbors=3)  # scalefactor is 1.2 to rescale for faces closer to camera
    face_list = opencv_classifier.detectMultiScale(gray_img,
                                                   scaleFactor=1.3,
                                                   minNeighbors=3)
    #print("#faces found: " + str(len(face_list)))
    return face_list
Ejemplo n.º 9
0
def get_faces(img, opencv_classifier):
    gray_image = Initializer.cvt2GRAY(img)
    face_list = detect_faces(img, opencv_classifier)
    ret_list = []
    #loop through detected faces
    for (x, y, w, h) in face_list:
        face_only = gray_image[y:y + w, x:x + h]
        face_coord = (x, y, w, h)
        ret_list.append((face_only, face_coord))
    return ret_list
Ejemplo n.º 10
0
    def __init__(self, master=None, stages={}):
        # Initialise GUI
        tk.Frame.__init__(self, master)
        self.master.title("Prober Control")
        self.grid()

        # Get instance of Global_MeasureHandler
        if g.instance != None:
            self.gh = g.instance

        # Get Dictionaries of Handles of Stages
        self.Stages = stages
        # Set Active Stage to first Stage in Dictionary
        self.ActiveStage = '-1'                
        if self.Stages != {}:
            dev_names = sorted(self.Stages.keys())
            for st in dev_names:
                if st[0] == 'O' or st[0] == 'E':
                    self.ActiveStage = st
                    break
            #print 'Active Stage: ' + self.ActiveStage # why are we printing this?
        
        # Initialize Procedure Handler
        self.Maitre = Maitre()

        #Gain access to Initializer singleton
        self.init = i.Initializer()

        # Initialize Standard Values for GUI
        self.StatusText   = tk.StringVar()
        self.CommandText  = tk.StringVar()
        self.StepText     = tk.StringVar()
        self.ProcMod      = tk.StringVar()
        self.ProcFunc     = tk.StringVar()
        self.ArgText      = tk.StringVar()
        self.StageMod     = tk.StringVar()
        self.StageFunc    = tk.StringVar()
        self.StageArgText = tk.StringVar()
        self.Script_Path  = ''
        self.FileText     = tk.StringVar()

        if self.ActiveStage != '-1':
            self.StepText.set(self.Stages[self.ActiveStage].stepsize)
        self.createWidgets()

        # Bind Key Strokes To Function
        self.bind('<KeyRelease-Left>', self.leftKey)
        self.bind('<KeyRelease-Right>', self.rightKey)
        self.bind('<KeyRelease-Up>', self.forwardKey)
        self.bind('<KeyRelease-Down>', self.backwardKey)
        self.bind('<Shift-KeyRelease-Up>',self.upKey)
        self.bind('<Shift-KeyRelease-Down>',self.downKey)
Ejemplo n.º 11
0
    def __init__(self,
                 population_size,
                 lamda,
                 archive,
                 target_structure,
                 init_depth=10):

        self.population_size = population_size
        self.init_depth = init_depth
        self.lamda = lamda
        self.archive = archive
        self.initializer = Initializer.Initializer(target_structure,
                                                   population_size)
    def test_anneal_DS(self):

        storage_requirments = [1,3,5,6,10,3,20]
        random_graph = ini.make_random_topology(400,3)
        subgraph = ini.find_subgraph(random_graph)
        storage_capacity = ini.find_storage_capacity(random_graph, subgraph)
        subgraph_capacity_sum = ini.subgraph_capacity_sum(storage_capacity)
        clustered_topology = ini.mark_cluster_in_graph(random_graph, subgraph)
        allocated_network_topology = ini.allocate_DS(storage_requirments, subgraph_capacity_sum, storage_capacity, clustered_topology)
        solution = sa_ds.solution(allocated_network_topology)
        annealed_solution = sa_ds.anneal_DS(solution, allocated_network_topology)
Ejemplo n.º 13
0
def preprocess(training_data_path, detection_classifier):
    face_list = []
    label_list = []
    dirs = os.listdir(
        training_data_path
    )  # get names of everything one level below training_data_path
    label = 0
    for dir in dirs:
        dir_path = Path(training_data_path, dir)
        if os.path.isdir(dir_path):  # only keep directories
            images = os.listdir(str(dir_path))
            discard_count = 0
            image_count = 0
            for image in images:
                image_path = Path(dir_path, image)
                if imghdr.what(
                        image_path) != None:  #check to make sure it's an image
                    img = Initializer.load_image(image_path)
                    image_count += 1
                    face_tuple = FaceDetector.get_faces(
                        img, detection_classifier)
                    if len(
                            face_tuple
                    ) == 1:  # if one face detected, keep in training data, else skip
                        face_tuple = face_tuple[0]
                    else:
                        #print("No faces or more than one face found in "+ str(image_path) + " so not used")
                        discard_count += 1
                        continue
                    if face_tuple[
                            0] is not None:  # face_tuple[0] = face image in grayscale
                        face_list.append(face_tuple[0])
                        label_list.append(label)
                    else:
                        print("No face was detected in " + str(image_path) +
                              " so the image is not used")
            print(
                str(discard_count) + "/" + str(image_count) +
                " images discarded for " + dir)
            label += 1
    if len(face_list) <= 0:
        print("No faces detected in training data -> cannot proceed")
        raise SystemExit

    return face_list, label_list
    def test_neighbour(self):

        storage_requirments = [1,3,5,6,10,3,20]
        random_graph = ini.make_random_topology(400,3)
        subgraph = ini.find_subgraph(random_graph)
        storage_capacity = ini.find_storage_capacity(random_graph, subgraph)
        subgraph_capacity_sum = ini.subgraph_capacity_sum(storage_capacity)
        clustered_topology = ini.mark_cluster_in_graph(random_graph,subgraph)
        allocated_network_topology = ini.allocate_DS(storage_requirments, subgraph_capacity_sum, storage_capacity, clustered_topology)
        solution = sa_ds.solution(allocated_network_topology)
        new_solution = sa_ds.neighbor(solution, allocated_network_topology)
        self.assertTrue(new_solution, type(new_solution) == list)
        self.assertTrue(len(new_solution) == len(solution))
 def test_cost(self):
     storage_requirments = [1,3,5,6,10,3,20]
     random_graph = ini.make_random_topology(400,3)
     subgraph = ini.find_subgraph(random_graph)
     storage_capacity = ini.find_storage_capacity(random_graph, subgraph)
     subgraph_capacity_sum = ini.subgraph_capacity_sum(storage_capacity)
     clustered_topology = ini.mark_cluster_in_graph(random_graph,subgraph)
     allocated_network_topology = ini.allocate_DS(storage_requirments, subgraph_capacity_sum, storage_capacity, clustered_topology)
     solution = sa_ds.solution(allocated_network_topology)
     new_solution = sa_ds.neighbor(solution, allocated_network_topology)
     cost = sa_ds.cost(new_solution,allocated_network_topology)
     self.assertIsInstance(cost, int)
     print(cost)
Ejemplo n.º 16
0
def run_recog_video():
    training_data_path = "vid_train_data/HP_troll_start2/HP_troll_start"
    # detection_classifier_path = "opencv/sources/data/lbpcascades/lbpcascade_frontalface_improved.xml"
    detection_classifier_path = "opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml"
    # detection_classifier_path = "opencv/sources/data/haarcascades_cuda/haarcascade_frontalface_default.xml"

    detection_classifier = Initializer.load_detection_classifier(
        detection_classifier_path)
    name_list = get_name_list(training_data_path)
    face_list, label_list = preprocess(training_data_path,
                                       detection_classifier)
    trained_recognizer = train_recognizer(face_list,
                                          label_list,
                                          opencv_recognizer_type="LBPH")

    video_path = "vid_test_data/HP_troll_trim_start.mp4"
    ouput_path = "vid_test_data/HP_troll_trim_start_out.avi"
    video_get_recognition(video_path, name_list, detection_classifier,
                          trained_recognizer, ouput_path)
Ejemplo n.º 17
0
    def test_allocate_DS(self):

        storage_requirments = [1, 3, 5, 6, 10, 3, 20]
        random_graph = ini.make_random_topology(400, 3)
        subgraph = ini.find_subgraph(random_graph)
        storage_capacity = ini.find_storage_capacity(random_graph, subgraph)
        subgraph_capacity_sum = ini.subgraph_capacity_sum(storage_capacity)
        clustered_topology = ini.mark_cluster_in_graph(random_graph, subgraph)
        allocated_network_topology = ini.allocate_DS(storage_requirments,
                                                     subgraph_capacity_sum,
                                                     storage_capacity,
                                                     clustered_topology)
        self.assertTrue(allocated_network_topology,
                        type(allocated_network_topology) == nx.Graph)
Ejemplo n.º 18
0
#!/usr/bin/env python2
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Fri Aug 21 15:56:13 2015
##################################################

from optparse import OptionParser
import Initializer

Initializer.init_path()

from gnuradio import gr
from gnuradio.eng_option import eng_option
from grc_gnuradio import blks2 as grc_blks2
from InputHandlerThread import InputHandlerThread
import osmosdr
from gnuradio import zeromq


class top_block(gr.top_block):
    def __init__(self, samp_rate, freq, gain, bw, port):
        gr.top_block.__init__(self, "Top Block")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate
        self.gain = gain
        self.freq = freq
        self.bw = bw
Ejemplo n.º 19
0
class CorrNet(object):

    def init(self, numpy_rng, theano_rng=None, l_rate=0.01, optimization="sgd",
             tied=False, n_visible_left=None, n_visible_right=None, n_hidden=None, lamda=5,
             W_left=None, W_right=None, b=None, W_left_prime=None, W_right_prime=None,
             b_prime_left=None, b_prime_right=None, input_left=None, input_right=None,
             hidden_activation="sigmoid", output_activation="sigmoid", loss_fn = "squarrederror",
             op_folder=None):

        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        self.theano_rng = theano_rng

        self.optimization = optimization
        self.l_rate = l_rate

        self.optimizer = get_optimizer(self.optimization, self.l_rate)
        self.Initializer = Initializer(self.numpy_rng)

        self.n_visible_left = n_visible_left
        self.n_visible_right = n_visible_right
        self.n_hidden = n_hidden
        self.lamda = lamda
        self.hidden_activation = hidden_activation
        self.output_activation = output_activation
        self.loss_fn = loss_fn
        self.tied = tied
        self.op_folder = op_folder

        self.W_left = self.Initializer.fan_based_sigmoid("W_left", W_left, n_visible_left, n_hidden)
        self.optimizer.register_variable("W_left",n_visible_left,n_hidden)

        self.W_right = self.Initializer.fan_based_sigmoid("W_right", W_right, n_visible_right, n_hidden)
        self.optimizer.register_variable("W_right",n_visible_right,n_hidden)

        if not tied:
            self.W_left_prime = self.Initializer.fan_based_sigmoid("W_left_prime", W_left_prime, n_hidden, n_visible_left)
            self.optimizer.register_variable("W_left_prime",n_hidden, n_visible_left)
            self.W_right_prime = self.Initializer.fan_based_sigmoid("W_right_prime", W_right_prime, n_hidden, n_visible_right)
            self.optimizer.register_variable("W_right_prime",n_hidden, n_visible_right)
        else:
            self.W_left_prime = self.W_left.T
            self.W_right_prime = self.W_right.T

        self.b = self.Initializer.zero_vector("b", b, n_hidden)
        self.optimizer.register_variable("b",1,n_hidden)

        self.b_prime_left = self.Initializer.zero_vector("b_prime_left", b_prime_left, n_visible_left)
        self.optimizer.register_variable("b_prime_left",1,n_visible_left)
        self.b_prime_right = self.Initializer.zero_vector("b_prime_right", b_prime_right, n_visible_right)
        self.optimizer.register_variable("b_prime_right",1,n_visible_right)

        if input_left is None:
            self.x_left = T.matrix(name='x_left')
        else:
            self.x_left = input_left

        if input_right is None:
            self.x_right = T.matrix(name='x_right')
        else:
            self.x_right = input_right


        if tied:
            self.params = [self.W_left, self.W_right,  self.b, self.b_prime_left, self.b_prime_right]
            self.param_names = ["W_left", "W_right", "b", "b_prime_left", "b_prime_right"]
        else:
            self.params = [self.W_left, self.W_right,  self.b, self.b_prime_left, self.b_prime_right, self.W_left_prime, self.W_right_prime]
            self.param_names = ["W_left", "W_right", "b", "b_prime_left", "b_prime_right", "W_left_prime", "W_right_prime"]


        self.proj_from_left = theano.function([self.x_left],self.project_from_left())
        self.proj_from_right = theano.function([self.x_right],self.project_from_right())
        self.recon_from_left = theano.function([self.x_left],self.reconstruct_from_left())
        self.recon_from_right = theano.function([self.x_right],self.reconstruct_from_right())

        self.save_params()


    def train_common(self,mtype="1111"):

        y1_pre = T.dot(self.x_left, self.W_left) + self.b
        y1 = activation(y1_pre, self.hidden_activation)
        z1_left_pre = T.dot(y1, self.W_left_prime) + self.b_prime_left
        z1_right_pre = T.dot(y1,self.W_right_prime) + self.b_prime_right
        z1_left = activation(z1_left_pre, self.output_activation)
        z1_right = activation(z1_right_pre, self.output_activation)
        L1 = loss(z1_left, self.x_left, self.loss_fn) + loss(z1_right, self.x_right, self.loss_fn)

        y2_pre = T.dot(self.x_right, self.W_right) + self.b
        y2 = activation(y2_pre, self.hidden_activation)
        z2_left_pre = T.dot(y2, self.W_left_prime) + self.b_prime_left
        z2_right_pre = T.dot(y2,self.W_right_prime) + self.b_prime_right
        z2_left = activation(z2_left_pre, self.output_activation)
        z2_right = activation(z2_right_pre, self.output_activation)
        L2 = loss(z2_left, self.x_left, self.loss_fn) + loss(z2_right, self.x_right, self.loss_fn)

        y3_pre = T.dot(self.x_left, self.W_left) + T.dot(self.x_right, self.W_right) + self.b
        y3 = activation(y3_pre, self.hidden_activation)
        z3_left_pre = T.dot(y3, self.W_left_prime) + self.b_prime_left
        z3_right_pre = T.dot(y3,self.W_right_prime) + self.b_prime_right
        z3_left = activation(z3_left_pre, self.output_activation)
        z3_right = activation(z3_right_pre, self.output_activation)
        L3 = loss(z3_left, self.x_left, self.loss_fn) + loss(z3_right, self.x_right, self.loss_fn)

        y1_mean = T.mean(y1, axis=0)
        y1_centered = y1 - y1_mean
        y2_mean = T.mean(y2, axis=0)
        y2_centered = y2 - y2_mean
        corr_nr = T.sum(y1_centered * y2_centered, axis=0)
        corr_dr1 = T.sqrt(T.sum(y1_centered * y1_centered, axis=0)+1e-8)
        corr_dr2 = T.sqrt(T.sum(y2_centered * y2_centered, axis=0)+1e-8)
        corr_dr = corr_dr1 * corr_dr2
        corr = corr_nr/corr_dr
        L4 = T.sum(corr) * self.lamda

        ly4_pre = T.dot(self.x_left, self.W_left) + self.b
        ly4 = activation(ly4_pre, self.hidden_activation)
        lz4_right_pre = T.dot(ly4,self.W_right_prime) + self.b_prime_right
        lz4_right = activation(lz4_right_pre, self.output_activation)
        ry4_pre = T.dot(self.x_right, self.W_right) + self.b
        ry4 = activation(ry4_pre, self.hidden_activation)
        rz4_left_pre = T.dot(ry4,self.W_left_prime) + self.b_prime_left
        rz4_left = activation(rz4_left_pre, self.output_activation)
        L5 = loss(lz4_right, self.x_right, self.loss_fn) + loss(rz4_left, self.x_left, self.loss_fn)

        if mtype=="1111":
            print "1111"
            L = L1 + L2 + L3 - L4
        elif mtype=="1110":
            print "1110"
            L = L1 + L2 + L3
        elif mtype=="1101":
            print "1101"
            L = L1 + L2 - L4
        elif mtype == "0011":
            print "0011"
            L = L3 - L4
        elif mtype == "1100":
            print "1100"
            L = L1 + L2
        elif mtype == "0010":
            print "0010"
            L = L3
        elif mtype == "euc":
            print "euc"
            L = L5
        elif mtype == "euc-cor":
            print "euc-cor"
            L = L5 - L4

        cost = T.mean(L)

        gradients = T.grad(cost, self.params)
        updates = []
        for p,g,n in zip(self.params, gradients, self.param_names):
            gr, upd = self.optimizer.get_grad_update(n,g)
            updates.append((p,p+gr))
            updates.extend(upd)

        return cost, updates

    def train_left(self):

        y_pre = T.dot(self.x_left, self.W_left) + self.b
        y = activation(y_pre, self.hidden_activation)
        z_left_pre = T.dot(y, self.W_left_prime) + self.b_prime_left
        z_left = activation(z_left_pre, self.output_activation)
        L = loss(z_left, self.x_left, self.loss_fn)
        cost = T.mean(L)

        if self.tied:
            curr_params = [self.W_left, self.b, self.b_prime_left]
            curr_param_names = ["W_left", "b", "b_prime_left"]
        else:
            curr_params = [self.W_left, self.b, self.b_prime_left, self.W_left_prime]
            curr_param_names = ["W_left", "b", "b_prime_left", "W_left_prime"]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p,g,n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n,g)
            updates.append((p,p+gr))
            updates.extend(upd)
        return cost, updates

    def train_right(self):

        y_pre = T.dot(self.x_right, self.W_right) + self.b
        y = activation(y_pre, self.hidden_activation)
        z_right_pre = T.dot(y, self.W_right_prime) + self.b_prime_right
        z_right = activation(z_right_pre, self.output_activation)
        L = loss(z_right, self.x_right, self.loss_fn)
        cost = T.mean(L)

        if self.tied:
            curr_params = [self.W_right, self.b, self.b_prime_right]
            curr_param_names = ["W_right", "b", "b_prime_right"]
        else:
            curr_params = [self.W_right, self.b, self.b_prime_right, self.W_right_prime]
            curr_param_names = ["W_right", "b", "b_prime_right", "W_right_prime"]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p,g,n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n,g)
            updates.append((p,p+gr))
            updates.extend(upd)
        return cost, updates

    def project_from_left(self):

        y_pre = T.dot(self.x_left, self.W_left) + self.b
        y = activation(y_pre, self.hidden_activation)
        return y

    def project_from_right(self):

        y_pre = T.dot(self.x_right, self.W_right) + self.b
        y = activation(y_pre, self.hidden_activation)
        return y

    def reconstruct_from_left(self):

        y_pre = T.dot(self.x_left, self.W_left) + self.b
        y = activation(y_pre, self.hidden_activation)
        z_left_pre = T.dot(y, self.W_left_prime) + self.b_prime_left
        z_right_pre = T.dot(y,self.W_right_prime) + self.b_prime_right
        z_left = activation(z_left_pre, self.output_activation)
        z_right = activation(z_right_pre, self.output_activation)
        return z_left, z_right

    def reconstruct_from_right(self):

        y_pre = T.dot(self.x_right, self.W_right) + self.b
        y = activation(y_pre, self.hidden_activation)
        z_left_pre = T.dot(y, self.W_left_prime) + self.b_prime_left
        z_right_pre = T.dot(y,self.W_right_prime) + self.b_prime_right
        z_left = activation(z_left_pre, self.output_activation)
        z_right = activation(z_right_pre, self.output_activation)
        return z_left, z_right

    def get_lr_rate(self):
        return self.optimizer.get_l_rate()

    def set_lr_rate(self,new_lr):
        self.optimizer.set_l_rate(new_lr)

    def save_matrices(self, path):

        for p,nm in zip(self.params, self.param_names):
            numpy.save(self.op_folder+nm, p.get_value(borrow=True))

    def save_params(self):

        params = {}
        params["optimization"] = self.optimization
        params["l_rate"] = self.l_rate
        params["n_visible_left"] = self.n_visible_left
        params["n_visible_right"] = self.n_visible_right
        params["n_hidden"] = self.n_hidden
        params["lamda"] = self.lamda
        params["hidden_activation"] = self.hidden_activation
        params["output_activation"] = self.output_activation
        params["loss_fn"] = self.loss_fn
        params["tied"] = self.tied
        params["numpy_rng"] = self.numpy_rng
        params["theano_rng"] = self.theano_rng

        # pickle.dump(params,open(self.op_folder+"params.pck","wb"),-1)


    def load(self, folder, input_left=None, input_right=None):

        plist = pickle.load(open(folder+"params.pck","rb"))

        self.init(plist["numpy_rng"], theano_rng=plist["theano_rng"], l_rate=plist["l_rate"],
                  optimization=plist["optimization"], tied=plist["tied"],
                  n_visible_left=plist["n_visible_left"], n_visible_right=plist["n_visible_right"],
                  n_hidden=plist["n_hidden"], lamda=plist["lamda"], W_left=folder+"W_left",
                  W_right=folder+"W_right", b=folder+"b", W_left_prime=folder+"W_left_prime",
                  W_right_prime=folder+"W_right_prime", b_prime_left=folder+"b_prime_left",
                  b_prime_right=folder+"b_prime_right", input_left=input_left, input_right=input_right,
                  hidden_activation=plist["hidden_activation"], output_activation=plist["output_activation"],
                  loss_fn = plist["loss_fn"], op_folder=folder)
Ejemplo n.º 20
0
    def init(self, numpy_rng, theano_rng=None, l_rate=0.01, optimization="sgd",
             tied=False, n_visible_left=None, n_visible_right=None, n_hidden=None, lamda=5,
             W_left=None, W_right=None, b=None, W_left_prime=None, W_right_prime=None,
             b_prime_left=None, b_prime_right=None, input_left=None, input_right=None,
             hidden_activation="sigmoid", output_activation="sigmoid", loss_fn = "squarrederror",
             op_folder=None):

        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        self.theano_rng = theano_rng

        self.optimization = optimization
        self.l_rate = l_rate

        self.optimizer = get_optimizer(self.optimization, self.l_rate)
        self.Initializer = Initializer(self.numpy_rng)

        self.n_visible_left = n_visible_left
        self.n_visible_right = n_visible_right
        self.n_hidden = n_hidden
        self.lamda = lamda
        self.hidden_activation = hidden_activation
        self.output_activation = output_activation
        self.loss_fn = loss_fn
        self.tied = tied
        self.op_folder = op_folder

        self.W_left = self.Initializer.fan_based_sigmoid("W_left", W_left, n_visible_left, n_hidden)
        self.optimizer.register_variable("W_left",n_visible_left,n_hidden)

        self.W_right = self.Initializer.fan_based_sigmoid("W_right", W_right, n_visible_right, n_hidden)
        self.optimizer.register_variable("W_right",n_visible_right,n_hidden)

        if not tied:
            self.W_left_prime = self.Initializer.fan_based_sigmoid("W_left_prime", W_left_prime, n_hidden, n_visible_left)
            self.optimizer.register_variable("W_left_prime",n_hidden, n_visible_left)
            self.W_right_prime = self.Initializer.fan_based_sigmoid("W_right_prime", W_right_prime, n_hidden, n_visible_right)
            self.optimizer.register_variable("W_right_prime",n_hidden, n_visible_right)
        else:
            self.W_left_prime = self.W_left.T
            self.W_right_prime = self.W_right.T

        self.b = self.Initializer.zero_vector("b", b, n_hidden)
        self.optimizer.register_variable("b",1,n_hidden)

        self.b_prime_left = self.Initializer.zero_vector("b_prime_left", b_prime_left, n_visible_left)
        self.optimizer.register_variable("b_prime_left",1,n_visible_left)
        self.b_prime_right = self.Initializer.zero_vector("b_prime_right", b_prime_right, n_visible_right)
        self.optimizer.register_variable("b_prime_right",1,n_visible_right)

        if input_left is None:
            self.x_left = T.matrix(name='x_left')
        else:
            self.x_left = input_left

        if input_right is None:
            self.x_right = T.matrix(name='x_right')
        else:
            self.x_right = input_right


        if tied:
            self.params = [self.W_left, self.W_right,  self.b, self.b_prime_left, self.b_prime_right]
            self.param_names = ["W_left", "W_right", "b", "b_prime_left", "b_prime_right"]
        else:
            self.params = [self.W_left, self.W_right,  self.b, self.b_prime_left, self.b_prime_right, self.W_left_prime, self.W_right_prime]
            self.param_names = ["W_left", "W_right", "b", "b_prime_left", "b_prime_right", "W_left_prime", "W_right_prime"]


        self.proj_from_left = theano.function([self.x_left],self.project_from_left())
        self.proj_from_right = theano.function([self.x_right],self.project_from_right())
        self.recon_from_left = theano.function([self.x_left],self.reconstruct_from_left())
        self.recon_from_right = theano.function([self.x_right],self.reconstruct_from_right())

        self.save_params()
Ejemplo n.º 21
0
                    'q'
            ):  # wait key is time(ms) between frames, press q to exit
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    image_path = "HP_test_detect.jpg"
    opencv_classifier_path = "opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml"
    img = Initializer.load_image(image_path)
    #Initializer.display_img("title",img)a
    opencv_classifier = Initializer.load_detection_classifier(
        opencv_classifier_path)

    video_path = "vid_test_data/HP_troll_trim_start2.mp4"
    output_path = "vid_test_data/HP_troll_trim_start2_out.mp4"
    boxed_img = get_boxed_faces(img, opencv_classifier)
    face_only_list = get_faces(img, opencv_classifier)

    #Initializer.display_img("boxed", boxed_img)

    #for img, coord in face_only_list:
    #    print(coord)
    #    Initializer.display_img("face_only", img)
Ejemplo n.º 22
0
#!/usr/bin/env python2
##################################################
# GNU Radio Python Flow Graph
# Title: Top Block
# Generated: Fri Aug 21 17:31:13 2015
##################################################

from optparse import OptionParser
import Initializer

Initializer.init_path()

from gnuradio import gr
from gnuradio.eng_option import eng_option
from grc_gnuradio import blks2 as grc_blks2
import osmosdr
from gnuradio import blocks
from gnuradio import zeromq

class top_block(gr.top_block):
    def __init__(self, samp_rate, freq, gain, bw, port):
        gr.top_block.__init__(self, "Top Block")

        ##################################################
        # Variables
        ##################################################
        self.samp_rate = samp_rate
        self.gain = gain
        self.freq = freq
        self.bw = bw
Ejemplo n.º 23
0
    return action


def newFunction():
    pass


if __name__ == '__main__':
    # Filename corresponds to benchmark to route
    # filename = 'small.gr'
    filename = '4by4small.gr'
    # filename = 'adaptec1.capo70.2d.35.50.90.gr'
    # filename = 'sampleBenchmark'

    # Getting Net Info
    grid_info = init.read(filename)
    print(grid_info)

    # # # print(init.gridParameters(grid_info)['netInfo'])
    #
    for item in init.gridParameters(grid_info).items():
        print(item)

    # # for net in init.gridParameters(grid_info)['netInfo']:
    # #     print (net)
    # init.GridGraph(init.gridParameters(grid_info)).show_grid()
    # init.GridGraph(init.gridParameters(grid_info)).pin_density_plot()
    #
    capacity = GridGraph(init.gridParameters(grid_info)).generate_capacity()
    # print(capacity[:,:,0,1])
    # gridX, gridY, gridZ= GridGraph(init.gridParameters(grid_info)).generate_grid()
Ejemplo n.º 24
0
import Initializer, Mutator, Recombiner, Selector, Replacer, Terminator, LocalSearcher
from GeneticAlgorithm import GeneticAlgorithm
from ProblemDefinition import ProblemDefinition
from Benchmark import Benchmark

# Choose your operators
initializer = Initializer.RandomInitializer()
mutator = Mutator.RandomMutator(0.2, dynAdapt=True)
recombiner = Recombiner.CrossoverRecombiner()
selector = Selector.TournamentSelector(s=20, dynAdapt=True)
replacer = Replacer.bottomReplacer()
terminator = Terminator.maxRuntimeTerminator(10)
# Add a local searcher if you want LocalSearcher.Idle() does nothing
localSearcher = LocalSearcher.Idle()

# Set up an example problem
nrMachines, jobRuntimes = Benchmark.benchmark1()
probDef = ProblemDefinition(nrMachines, jobRuntimes)

# Set up GA parameters
popSize = 100
nrOffspring = int(popSize / 10)

# Create Genetic Algorithm instance
GA = GeneticAlgorithm(initializer, selector, recombiner, mutator, replacer,
                      terminator, probDef, popSize, nrOffspring, localSearcher)
bestIndividual, results = GA.run()
print("best Individuals fitness: ", bestIndividual.fitness)
Ejemplo n.º 25
0
    def init(self, network, x_left = None, x_right = None, load = False):

        self.network = network

        numpy_rng = numpy.random.RandomState(123)
        theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # Random number generators
        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        self.theano_rng = theano_rng

        # Optimization metadata
        self.optimizer = get_optimizer(network['optimization'], network['learning_rate'])  # Optimizer-object, with containers for weights, learning rate (shared)

        # Initialization
        self.Initializer = Initializer(self.numpy_rng)

        # Set inputs
        if x_left != None:
            self.x_left = x_left
        else:
            self.x_left = T.matrix('x_left')
        if x_right != None:
            self.x_right = x_right
        else:
            self.x_right = T.matrix('x_right')
        self.y_proj = T.matrix('y_proj')

        self.params = {}
        preload_params = {}
        # Initialize/Load params
        for layer_name, layer in network['layers'].iteritems():
            # Initialize/Load params
            for param in layer['params']:
                if param + '.npy' in os.listdir(network['params_folder']):
                    print 'loaded %s' % param
                    preload_params = os.path.join(network['params_folder'], param)
                else:
                    preload_params = None
                if layer_name == 'layer_1' and load == False:
                    preload_params = None
                # Encode weights
                if 'W' in param and 'prime' not in param:
                    self.params[param] = self.Initializer.fan_based_sigmoid(param, preload_params,\
                                                                            layer['n_in_%s' % param[-1]], layer['n_out'])
                    self.optimizer.register_variable(param, layer['n_in_%s' % param[-1]], layer['n_out'])
                # Decode weights
                if 'W_prime' in param:
                    self.params[param] = self.params[param[0] + 'W_' + param[-1]].T
                # Decod Biases
                if 'b_prime' in param:
                    self.params[param] = self.Initializer.zero_vector(param, preload_params,\
                                                                     layer['n_in_%s' % param[-1]])
                    self.optimizer.register_variable(param, 1, layer['n_in_%s' % param[-1]])
                # Encode bias
                if 'b' in param and 'prime' not in param:
                    self.params[param] = self.Initializer.zero_vector(param, preload_params,\
                                                                     layer['n_out'])
                    self.optimizer.register_variable(param, 1, layer['n_out'])

        self.proj_from_left = theano.function([self.x_left], self.project_from_left())
        self.proj_from_right = theano.function([self.x_right], self.project_from_right())
        self.recon_left = theano.function([self.y_proj], self.reconstruct_left())
        self.recon_right = theano.function([self.y_proj], self.reconstruct_right())
Ejemplo n.º 26
0
            G[neighbor] = candidateG
            H = graph.heuristic(neighbor,pinEnd)
            F[neighbor] = G[neighbor] + H
        
        # update capacity
        # capacity = gridgraph.updateCapacityRL(capacity,state,action)

    raise RuntimeError("A* failed to find a solution")

if __name__ == "__main__":
    filename = 'small.gr'
    # filename = 'adaptec1.capo70.2d.35.50.90.gr'
    # filename = 'sampleBenchmark'

    # # Getting Net Info
    grid_info = init.read(filename)
    # print(grid_info)
    # # print(init.gridParameters(grid_info)['netInfo'])
    for item in init.gridParameters(grid_info).items():
        print(item)
    gridParameters = init.gridParameters(grid_info)
    # # for net in init.gridParameters(grid_info)['netInfo']:
    # init.GridGraph(init.gridParameters(grid_info)).show_grid()
    # init.GridGraph(init.gridParameters(grid_info)).pin_density_plot()

    # # GridGraph
    capacity = gridgraph.GridGraph(init.gridParameters(grid_info)).generate_capacity()
    # print(capacity[:,:,0,0])
    gridX,gridY,gridZ = gridgraph.GridGraph(init.gridParameters(grid_info)).generate_grid()
    # print(gridX[1,1,0])
    # print(gridY[1,1,0])
Ejemplo n.º 27
0
class CorrNet(object):

    def init(self, network, x_left = None, x_right = None, load = False):

        self.network = network

        numpy_rng = numpy.random.RandomState(123)
        theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        # Random number generators
        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
        self.theano_rng = theano_rng

        # Optimization metadata
        self.optimizer = get_optimizer(network['optimization'], network['learning_rate'])  # Optimizer-object, with containers for weights, learning rate (shared)

        # Initialization
        self.Initializer = Initializer(self.numpy_rng)

        # Set inputs
        if x_left != None:
            self.x_left = x_left
        else:
            self.x_left = T.matrix('x_left')
        if x_right != None:
            self.x_right = x_right
        else:
            self.x_right = T.matrix('x_right')
        self.y_proj = T.matrix('y_proj')

        self.params = {}
        preload_params = {}
        # Initialize/Load params
        for layer_name, layer in network['layers'].iteritems():
            # Initialize/Load params
            for param in layer['params']:
                if param + '.npy' in os.listdir(network['params_folder']):
                    print 'loaded %s' % param
                    preload_params = os.path.join(network['params_folder'], param)
                else:
                    preload_params = None
                if layer_name == 'layer_1' and load == False:
                    preload_params = None
                # Encode weights
                if 'W' in param and 'prime' not in param:
                    self.params[param] = self.Initializer.fan_based_sigmoid(param, preload_params,\
                                                                            layer['n_in_%s' % param[-1]], layer['n_out'])
                    self.optimizer.register_variable(param, layer['n_in_%s' % param[-1]], layer['n_out'])
                # Decode weights
                if 'W_prime' in param:
                    self.params[param] = self.params[param[0] + 'W_' + param[-1]].T
                # Decod Biases
                if 'b_prime' in param:
                    self.params[param] = self.Initializer.zero_vector(param, preload_params,\
                                                                     layer['n_in_%s' % param[-1]])
                    self.optimizer.register_variable(param, 1, layer['n_in_%s' % param[-1]])
                # Encode bias
                if 'b' in param and 'prime' not in param:
                    self.params[param] = self.Initializer.zero_vector(param, preload_params,\
                                                                     layer['n_out'])
                    self.optimizer.register_variable(param, 1, layer['n_out'])

        self.proj_from_left = theano.function([self.x_left], self.project_from_left())
        self.proj_from_right = theano.function([self.x_right], self.project_from_right())
        self.recon_left = theano.function([self.y_proj], self.reconstruct_left())
        self.recon_right = theano.function([self.y_proj], self.reconstruct_right())

    def train(self):

        y1 = self.x_left
        y2 = self.x_right
        for layer_count in reversed(range(self.network['num_layers'])):
            if layer_count + 1 == 1:
                y3 = activation(T.dot(y1, self.params[str(layer_count + 1) + 'W_1']) +\
                                T.dot(y2, self.params[str(layer_count + 1) + 'W_2']) +\
                                self.params[str(layer_count + 1) + 'b'],\
                                self.network['activation'])
            y1 = activation(T.dot(y1, self.params[str(layer_count + 1) + 'W_1']) + self.params[str(layer_count + 1) + 'b'], self.network['activation'])
            y2 = activation(T.dot(y2, self.params[str(layer_count + 1) + 'W_2']) + self.params[str(layer_count + 1) + 'b'], self.network['activation'])
        
        [z1_left, z2_left, z3_left] = [y1, y2, y3]
        [z1_right, z2_right, z3_right] = [y1, y2, y3]
        for layer_count in range(self.network['num_layers']):
            z1_left = activation(T.dot(z1_left, self.params[str(layer_count + 1) + 'W_prime_1']) + self.params[str(layer_count + 1) + 'b_prime_1'],\
                                self.network['activation'])
            z1_right = activation(T.dot(z1_right, self.params[str(layer_count + 1) + 'W_prime_2']) + self.params[str(layer_count + 1) + 'b_prime_2'],\
                                self.network['activation'])
            z2_left = activation(T.dot(z2_left, self.params[str(layer_count + 1) + 'W_prime_1']) + self.params[str(layer_count + 1) + 'b_prime_1'],\
                                self.network['activation'])
            z2_right = activation(T.dot(z2_right, self.params[str(layer_count + 1) + 'W_prime_2']) + self.params[str(layer_count + 1) + 'b_prime_2'],\
                                self.network['activation'])
            z3_left = activation(T.dot(z3_left, self.params[str(layer_count + 1) + 'W_prime_1']) + self.params[str(layer_count + 1) + 'b_prime_1'],\
                                self.network['activation'])
            z3_right = activation(T.dot(z3_right, self.params[str(layer_count + 1) + 'W_prime_2']) + self.params[str(layer_count + 1) + 'b_prime_2'],\
                                self.network['activation'])

        y1_mean = T.mean(y1, axis=0)
        y1_centered = y1 - y1_mean
        y2_mean = T.mean(y2, axis=0)
        y2_centered = y2 - y2_mean
        corr_nr = T.sum(y1_centered * y2_centered, axis=0)
        corr_dr1 = T.sqrt(T.sum(y1_centered * y1_centered, axis=0)+1e-8)
        corr_dr2 = T.sqrt(T.sum(y2_centered * y2_centered, axis=0)+1e-8)
        corr_dr = corr_dr1 * corr_dr2
        corr = corr_nr/corr_dr
        
        # Mean is for the batch
        recon_loss = {'view1_self'  : T.mean(loss(z1_left, self.x_left, self.network['loss_function'])),\
                      'view1_cross' : T.mean(loss(z1_right, self.x_right, self.network['loss_function'])),\
                      'view2_self'  : T.mean(loss(z2_right, self.x_right, self.network['loss_function'])),\
                      'view2_cross' : T.mean(loss(z2_left, self.x_left, self.network['loss_function'])),\
                      'view12_left' : T.mean(loss(z3_left, self.x_left, self.network['loss_function'])),\
                      'view12_right': T.mean(loss(z3_right, self.x_right, self.network['loss_function']))}
       
        # L1, L2 and L3 are the reconstruction losses, L4 is the correlation loss
        L1 = recon_loss['view1_self']  + recon_loss['view1_cross']
        L2 = recon_loss['view2_self']  + recon_loss['view2_cross']
        L3 = recon_loss['view12_left'] + recon_loss['view12_right']
        L4 = T.mean(T.sum(corr)) * self.network['lambda']

        # Sum correlation across all dimensions; mean is for the batch
        only_corr = T.mean(T.sum(corr))

        # Mean cost for this batch
        cost = L1 + L2 + L3 - L4
        
        self.param_values = []
        self.param_names  = []
        for param_name, param_value in self.params.iteritems():
            if 'W_prime' in param_name:
                continue
            self.param_values.append(param_value)
            self.param_names.append(param_name)
        gradients = T.grad(cost, self.param_values)
        updates = []
        for p,g,n in zip(self.param_values, gradients, self.param_names):
            gr, upd = self.optimizer.get_grad_update(n,g)
            updates.append((p,p+gr))
            updates.extend(upd)

        #return cost, updates, T.mean(L1 + L2 + L3), T.mean(only_corr)
        return cost, updates, only_corr,\
               recon_loss['view1_self'], recon_loss['view1_cross'],\
               recon_loss['view2_self'], recon_loss['view2_cross'],\
               recon_loss['view12_left'], recon_loss['view12_right']

    def project_from_left(self):
        y1 = self.x_left
        for layer_count in reversed(range(self.network['num_layers'])):
            y1 = activation(T.dot(y1, self.params[str(layer_count + 1) + 'W_1']) + self.params[str(layer_count + 1) + 'b'], self.network['activation'])
        return y1

    def project_from_right(self):
        y2 = self.x_right
        for layer_count in reversed(range(self.network['num_layers'])):       
            y2 = activation(T.dot(y2, self.params[str(layer_count + 1) + 'W_2']) + self.params[str(layer_count + 1) + 'b'], self.network['activation'])
        return y2

    def reconstruct_left(self):
        z1 = self.y_proj
        for layer_count in range(self.network['num_layers']):
            z1 = activation(T.dot(z1, self.params[str(layer_count + 1) + 'W_prime_1']) + self.params[str(layer_count + 1) + 'b_prime_1'], self.network['activation'])
        return z1

    def reconstruct_right(self):
        z2 = self.y_proj
        for layer_count in range(self.network['num_layers']):
            z2 = activation(T.dot(z2, self.params[str(layer_count + 1) + 'W_prime_2']) + self.params[str(layer_count + 1) + 'b_prime_2'], self.network['activation'])
        return z2

    def get_lr_rate(self):
        return self.optimizer.get_l_rate()

    def set_lr_rate(self,new_lr):
        self.optimizer.set_l_rate(new_lr)

    def save_matrices(self):
        print 'saving matrices'
        for p,nm in zip(self.param_values, self.param_names):
            numpy.save(os.path.join(self.network['params_folder'], nm), p.get_value(borrow=True))
    def test_initialization(self):

        storage_requirments = [1,3,5]
        random_graph = ini.make_random_topology(100,3)
        print('hje')
        nx.write_edgelist(random_graph, '/home/christopher/IdeaProjects/Genetic_algortihm/graph.gz')
Ejemplo n.º 29
0
def executePlan(plan):
    global poseNumRecorder
    start = 0
    stop = len(plan)
    PRINT("Preparing prescript variables")
    for n in range(start, len(plan)):
        if "begin" in plan[n]:
            if (plan[n]["begin"]) and (start == 0):
                start = n
        if "end" in plan[n]:
            if plan[n]["end"]:
                if (n > start and n < stop):
                    stop = n
    PRINT("Start point determined")
    commonFolder = CONSTANTS["commonFolder"]
    targetFilename = CONSTANTS["targetProtein"]
    targetPDBFile = "%s/%s" % (commonFolder, targetFilename)
    #radius = CONSTANTS["cullRadiusMeasure"]
    #centerResidue = CONSTANTS["cullRadiusResidue"]
    permLogLoc = CONSTANTS.get("permLogLoc", None)
    #PRINT("Target Alpha Carbon consideration radius: "+str(radius)+" from residue "+str(centerResidue))

    #Creates a searchable tree with all the atoms in the target protein
    targetAll = PDBTools.Peptide(
        PDBTools.readPDBSelectively(targetPDBFile,
                                    CONSTANTS["targetProteinChain"])
    )  #PDBTools.readPDBFile(commonFolder+"/"+targetFilename)[0]
    targetTree = KDTree.KDTree.loadAtomArray(targetAll.atoms)

    PRINT("Ensuring base water file exists")
    if BaseWaters.shouldGenerateBaseWaterFile(CONSTANTS):
        PRINT("Creating base waters")
        baseWaters = BaseWaters.makeBaseWaters(CONSTANTS, targetTree,
                                               targetAll)
    baseWaters = BaseWaters.setupBaseWaters(CONSTANTS)
    PRINT("Base Waters Scored")

    fixvarfile = None
    currentPoseData = None
    waterData = None
    if (start > 0):
        PRINT("Prior Quant Record " + str(start))
        seq = priorSeq(plan, start)
        currentFolder = priorPlanFolder(plan, start)
        if (currentFolder != "__START__"):
            poseNumRecorder = Log.QuantityLog(plan[start], seq)
            poseNumRecorder.top = top
            os.chdir(currentFolder)
            PRINT("Loading Fixvar")
            fixvarfile = Fixvar.Fixvar()
            fixvarfile.setBB(ZMAT.ZMAT(zmatLoc(priorTMDstep(plan, start))))
            PRINT("Trying to load CPF")
            try:
                currentPoseData = Permutations.PeptidePermutations.readCPF(
                    "all.cpf")
                currentPoseData.linkToFixvar(fixvarfile)
            except:
                PRINT(traceback.format_exc())
                PRINT("Could not load CPF")
            priorCullType = priorCullStep(plan, start)
            if (priorCullType is not None):
                cullFilename = priorCullType + "Cull.pos"
                print("PCT ", priorCullType)
                PRINT("Loading which poses were culled during last " +
                      priorCullType + " cull")
                currentPoseData.applySavedCull(cullFilename)
            else:
                print("No prior cull")
            #if not "sequence" in plan[start]:
            #poseNumRecorder.setSequence()
            #poseNumRecorder.alignStep(plan[start]):
    else:
        poseNumRecorder = Log.QuantityLog()
        PRINT("New Quant Record")
    for n in range(start, stop):
        if (plan[n]["type"] in TMD_Like_Steps):
            currentFolder = top + "/" + plan[n]["folder"]
            try:
                os.stat(currentFolder)
            except:
                os.mkdir(currentFolder)
            PRINT("\n\n\n")
            poseNumRecorder.setSequence(plan[n]["sequence"])
            recorder.write("********************\n")
            PRINT("Starting Script on Peptide " + plan[n]["sequence"])
            recorder.write("********************\n")
            os.chdir(currentFolder)
            PRINT(Log.timeString())
        if (plan[n]["type"] == "TMD"):
            ZMAT.createZMAT(zmatLoc(plan[n]))
            numNewPoses = TMD.runTMD(CONSTANTS, plan[n], fixvarfile, recorder)
            poseNumRecorder.recordStep(Log.QuantityLog.TMD, numNewPoses)
            PRINT("TMD Calcs finished at " + Log.timeString())
            PRINT("\tPoses created " + str(numNewPoses))
            if isBBGrow(plan, n):
                if (n == 0):
                    fixvarfile = Fixvar.Fixvar()
                PRINT(Log.timeString())
                recorder.copyLog()
                seeker = HBond.PerfectHydrogenBondSeeker(
                    CONSTANTS, plan[n], fixvarfile, targetTree)
                seeker.analyzeAllPoses()
                fixvarfile = seeker.conclude()
                del seeker
                poseNumRecorder.recordStep(Log.QuantityLog.HBSEEK,
                                           fixvarfile.numGood())

            else:
                fixvarfile = Fixvar.Fixvar()
                fixvarfile.setBB(ZMAT.ZMAT(zmatLoc(plan[n])))

            PRINT("")
            recorder.copyLog()
            PRINT(Log.timeString())
            #makeOnePDB(plan[n],fixvarfile)
            PRINT("Calculating Pose Coordinates")
            #currentPoseData = Permutations.PeptidePermutations.readPDB("makeOnePDB/All.pdb")
            currentPoseData = createPeptidePermutations(
                plan[n], fixvarfile, targetTree)
            #PRINT("GOOD POSES: "+str(currentPoseData.numGood()))
            #currentPoseData.linkToFixvar(fixvarfile)
            PRINT("Saving dense pose file")

            currentPoseData.saveFile("all.cpf")
            if isBBGrow(plan, n):
                PRINT("Running extendibility cull")
                extendibilityCull(targetTree, getNextBBGrowStep(plan, n),
                                  currentPoseData)

            shutil.copy(getHelperScriptPath("GUI"),
                        CONSTANTS["HelperScripts"]["GUI"]["file"])
            PRINT("")
            recorder.copyLog()
            PRINT(Log.timeString())
            PRINT("TMD step Complete")
        elif (plan[n]["type"] == "initCTerminal"):
            import Initializer
            Initializer.initCTerminal(plan[n], CONSTANTS, targetAll)
            PRINT("Initial Routine Sucessful")
            fixvarfile = Fixvar.Fixvar()
            PRINT("")
            recorder.copyLog()
            PRINT(Log.timeString())
            PRINT("Calculating Pose Coordinates")
            currentPoseData = createPeptidePermutations(
                plan[n], fixvarfile, targetTree)
            PRINT("GOOD POSES: " + str(currentPoseData.numGood()))
            #currentPoseData.linkToFixvar(fixvarfile)
            PRINT("Saving dense pose file")

            currentPoseData.saveFile("all.cpf")

            shutil.copy(getHelperScriptPath("GUI"),
                        CONSTANTS["HelperScripts"]["GUI"]["file"])
            PRINT("")
            recorder.copyLog()
            PRINT(Log.timeString())
            PRINT("Initiation step Complete")
        else:
            if "folder" in plan[n]:
                currentFolder = top + "/" + plan[n]["folder"]
                os.chdir(currentFolder)
            else:
                currentFolder = priorPlanFolder(plan, n)
                if (currentFolder != "__START__"):
                    os.chdir(currentFolder)
        if (plan[n]["type"] == "seekHB"):
            raise Exception("Classic Hbond seek feature removed")
        if (plan[n]["type"] == "water"):
            recorder.write("********************\n")
            PRINT("Starting water on Peptide " + priorSeq(plan, n))
            recorder.write("********************\n")
            PRINT(Log.timeString())
            recorder.copyLog()
            highRes = PDBTools.lastResidueNum()
            hydrogenPoseData, waterData = RunWatGen(currentPoseData)
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            zzz = ZMAT.ZMAT(zmatLoc(priorSeq(plan, n, False)))
            zzz.readReference(CONSTANTS)
            scorePoses(currentPoseData, hydrogenPoseData, waterData, zzz,
                       baseWaters)
            #RunWatAnalysis(currentPoseData,hydrogenPoseData,waterData,fixvarfile,targetTree)
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            PRINT("")
            recorder.copyLog()
            os.chdir(currentFolder)
            #applyEnergyToFixvar(currentPoseData)
        if (plan[n]["type"] == "noWater"):
            recorder.write("********************\n")
            PRINT("Faking water on Peptide " + priorSeq(plan, n))
            recorder.write("********************\n")
            PRINT(Log.timeString())
            recorder.copyLog()
            highRes = PDBTools.lastResidueNum()
            hydrogenPoseData, waterData = FakeWatGen(currentPoseData)
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            zzz = ZMAT.ZMAT(zmatLoc(priorSeq(plan, n, False)))
            zzz.readReference(CONSTANTS)
            scorePoses(currentPoseData, hydrogenPoseData, waterData, zzz,
                       baseWaters)
            #RunWatAnalysis(currentPoseData,hydrogenPoseData,waterData,fixvarfile,targetTree)
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            PRINT("")
            recorder.copyLog()
            os.chdir(currentFolder)
            #applyEnergyToFixvar(currentPoseData)
        if (plan[n]["type"] == "analysis"):
            recorder.write("********************\n")
            PRINT("Starting only analysis on Peptide " + priorSeq(plan, n))
            recorder.write("********************\n")
            if (waterData is None):
                PRINT("NEED TO LOAD CURRENT POSE DATA")
                #currentPoseData = Permutations.PeptidePermutations.readCPF("All.cpf")
                #currentPoseData.linkToFixvar(fixvarfile)
                hydrogenPoseData = Permutations.PeptidePermutations.readCPF(
                    "AllH.cpf")
                waterData = Permutations.WaterPermutations.readCWF("Water.cwf")
                PRINT("FILES LOADED")
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            #RunWatAnalysis(currentPoseData,hydrogenPoseData,waterData,fixvarfile,targetTree)
            zzz = ZMAT.ZMAT(zmatLoc(priorSeq(plan, n, False)))
            zzz.readReference(CONSTANTS)
            scorePoses(currentPoseData, hydrogenPoseData, waterData, zzz,
                       baseWaters)
            PRINT(Log.timeString())
            os.chdir(currentFolder)
            PRINT("")
            recorder.copyLog()
            os.chdir(currentFolder)
            #applyEnergyToFixvar(currentPoseData)
        if (plan[n]["type"] == "atomCull"):
            if not fixvarfile.energyInfo:
                if (os.path.isfile("ETable.tsv")):
                    currentPoseData.applyEnergyToFixvar()
            recorder.write("********************\n")
            PRINT("Culling poses by side-chain redundancy")
            recorder.write("********************\n")
            atomCull(fixvarfile, plan[n])
            if (n < len(plan) - 1):
                if not (plan[n + 1]["type"] == "atomCull"):
                    PRINT("Saving output")
                    atomCullReviseOutputs(currentPoseData)
            PRINT("Done with cull")
            PRINT("\tRetained poses: " + str(fixvarfile.numGood()))
        if (plan[n]["type"] == "backboneCull"):
            if not fixvarfile.energyInfo:
                if (os.path.isfile("ETable.tsv")):
                    currentPoseData.applyEnergyToFixvar()
                else:
                    raise Exception("Must have energy data to run this cull")
            recorder.write("********************\n")
            PRINT("Culling poses by backbone similiarity")
            recorder.write("********************\n")
            backboneCull(currentPoseData, plan[n])
            PRINT("Done with cull")
            PRINT("\tRetained poses: " + str(fixvarfile.numGood()))
        if (plan[n]["type"] == "ControlCompare"):
            recorder.write("********************\n")
            PRINT(
                "Producing ordered list of resemblance to known structure\nNot using energy data"
            )
            recorder.write("********************\n")
            anchor = plan[n].get("anchor", None)
            makeLMSTableWithoutWatgen(currentPoseData, anchor)
        if (plan[n]["type"] == "KnownCompare"):
            recorder.write("********************\n")
            PRINT("Producing ordered list of resemblance to known structure")
            recorder.write("********************\n")
            anchor = plan[n].get("anchor", None)
            ETable.extendETable(CONSTANTS, currentPoseData, anchor)
            priorCullType = priorCullStep(plan, n)
            if priorCullType is not None:
                PRINT("PriorCullType " + str(priorCullType))
                PRINT("Producing table of poses that still remain")
                cutControlTable(priorCullType)
        if (plan[n]["type"] == "waterCull"):
            if not fixvarfile.energyInfo:
                if (os.path.isfile("ETable.tsv")):
                    currentPoseData.applyEnergyToFixvar()
                else:
                    raise Exception(
                        "Cannot perform water cull without WATGEN and scoring")
            recorder.write("********************\n")
            PRINT("Culling peptide by water energies")
            recorder.write("********************\n")
            if ((plan[n]["method"] == "50P_Energy")
                    or (plan[n]["method"] == "fracMaxEnergy")):
                frac = plan[n]["threshold"]
                PRINT("Threshold of max method with threshold " + str(frac))
                WaterCull.thresholdOfMaxEnergy(plan[n], currentPoseData)
                PRINT("\tNumber of poses retained:" +
                      str(currentPoseData.numGood()))
                poseNumRecorder.recordStep(Log.QuantityLog.WAT_CULL,
                                           currentPoseData.numGood(),
                                           "Within " + str(frac * 100) + "%")
            if (plan[n]["method"] == "top"):
                topNum = plan[n]["liminalNumber"]
                WaterCull.topWaters(plan[n], currentPoseData)
                PRINT("Top %i poses in terms of water energy" % topNum)
                poseNumRecorder.recordStep(Log.QuantityLog.WAT_CULL,
                                           currentPoseData.numGood(),
                                           "Top %i" % topNum)
            if (plan[n]["method"] == "rubber"):
                WaterCull.rubberTopWaters(plan[n], currentPoseData)
                PRINT("Performing water cull with \"rubber\" retension")
                poseNumRecorder.recordStep(
                    Log.QuantityLog.WAT_CULL, currentPoseData.numGood(),
                    "Top " + str(currentPoseData.numGood()))
            if (plan[n]["method"] == "stochastic"):
                WaterCull.stochasticWaterCull(plan[n], currentPoseData)
                PRINT("Performing stochastic cull")
                poseNumRecorder.recordStep(Log.QuantityLog.WAT_CULL,
                                           currentPoseData.numGood(),
                                           "Stochastic")
            if (plan[n]["method"] == "delta"):
                PRINT("Delta Cull")
                delta = WaterCull.getDeltaFrac(top, plan, n)
                PRINT("Culling poses at %3.1f%% Threshold" % (delta * 100))
                fakeStep = {"threshold": delta, "relative": True}
                WaterCull.thresholdOfMaxEnergy(fakeStep, currentPoseData)
                PRINT("\tNumber of poses retained:" +
                      str(currentPoseData.numGood()))
                poseNumRecorder.recordStep(Log.QuantityLog.WAT_CULL,
                                           currentPoseData.numGood(),
                                           "Within %3.1f%%" % (delta * 100))
        if (plan[n]["type"] == "distCull"):
            res = plan[n].get("residue", None)
            if res is None:
                lastStep = priorGly(plan, n)
                atmNum = lastStep["variableTorsions"][0]["atom"]
                res = ZMAT.residueNumber(zmatLoc(lastStep), atmNum)
            PRINT("Last res: " + str(res))
            distCull(targetTree, plan[n], currentPoseData, res)
        if (plan[n]["type"] == "overlayCull"):
            if not fixvarfile.energyInfo:
                if (os.path.isfile("ETable.tsv")):
                    currentPoseData.applyEnergyToFixvar()
                else:
                    raise Exception(
                        "Cannot perform water cull without WATGEN and scoring")
            recorder.write("********************\n")
            PRINT("Overlay Cull")
            recorder.write("********************\n")
            import OverlayCull
            OverlayCull.findOverlays(currentPoseData)
        if (plan[n]["type"] == "validityCheck"):
            recorder.write("********************\n")
            PRINT("Culling peptide by distance from protein")
            recorder.write("********************\n")
            PRINT(Log.timeString())
            recorder.copyLog()
            validityCheck(targetTree, plan[n], currentPoseData)
            PRINT(Log.timeString())
            recorder.copyLog()
        if (plan[n]["type"] == "gate"):
            recorder.write("********************\n")
            PRINT("Applying logic gate")
            if (plan[n]["method"] == "fewerPoses"):
                PRINT("FEATURE NOT YET IMPLEMENTED")
            recorder.write("********************\n")
        if (plan[n]["type"] == "ScoreControl"):
            recorder.write("********************\n")
            PRINT("Scoring segment of XRay Control")
            scSeq = plan[n]["sequence"]
            scSeqString = "\tSequence: " + scSeq
            scStartRes = plan[n].get("anchor", None)
            if scStartRes is not None:
                if isinstance(scStartRes, int):
                    scSeqString += "\t starting at residue " + str(scStartRes)
                elif isinstance(scStartRes, str):
                    if (scStartRes.upper() == "N"):
                        scSeqString += "\t starting with the most N-terminal match"
                    if (scStartRes.upper() == "C"):
                        scSeqString += "\t starting with the most C-terminal match"
            PRINT(scSeqString)
            recorder.write("********************\n")
            currentFolder = top + "/" + scSeq
            try:
                os.stat(currentFolder)
            except:
                os.mkdir(currentFolder)
            os.chdir(currentFolder)
            scoreControlPeptide(plan[n])
            shutil.copy(getHelperScriptPath("GUI"),
                        CONSTANTS["HelperScripts"]["GUI"]["file"])
            shutil.copy(commonFolder + "/out.pdb", currentFolder + "/out.pdb")
            currentPoseData = Permutations.PeptidePermutations.readCPF(
                "all.cpf")

            hydrogenPoseData, waterData = RunWatGen(currentPoseData)
            zzz = ZMAT.ZMAT(zmatLoc(priorSeq(plan, n, False)))
            zzz.readReference(CONSTANTS)
            currentPoseData.fixvar = Fixvar.Fixvar(file=None)
            currentPoseData.fixvar.poses.append(Fixvar.FixvarPoses([], 1))
            scorePoses(currentPoseData, hydrogenPoseData, waterData, zzz,
                       baseWaters)
        if (plan[n]["type"] == "CollectControl"):
            os.chdir(top)
            headerWriten = False
            xrayGrandTable = open("XrayGrandTable.tsv", 'w')
            for xrayStep in plan:
                if (xrayStep["type"] == "ScoreControl"):
                    xrayInf = open(
                        "%s/%s/ETable.tsv" % (top, xrayStep["sequence"]), 'r')
                    header = xrayInf.readline()
                    if not headerWriten:
                        xrayGrandTable.write(header)
                        headerWriten = True
                    xrayData = xrayInf.readline().split("\t")
                    xrayData[0] = xrayStep["sequence"]
                    xrayLine = "\t".join(xrayData)
                    xrayGrandTable.write(xrayLine)
            xrayGrandTable.close()

        if (plan[n]["type"] == "Consensus"):
            if not fixvarfile.energyInfo:
                if (os.path.isfile("ETable.tsv")):
                    currentPoseData.applyEnergyToFixvar()
            consensusPose(currentPoseData)
    PRINT("Script Complete")
    sRemove(top + "/log_current.txt")
    timeDifference = int(time.time() - Log.startTime)
    PRINT("Time to completetion: " + Log.secondsToHMS(timeDifference))
    PRINT("Time to completetion (seconds): " + str(timeDifference))
    PRINT("\n\nPose Counts:\n" + poseNumRecorder.dump())
    recorder.close()
    poseNumRecorder.close()
Ejemplo n.º 30
0
            self.curr_time += diffuser.dt
        self.plotter.plot()


if __name__ == "__main__":
    cap = 0.0089285714
    tissue = 0.00892916955592076
    # the m values below are tuned for 40% saturation diffusing out of the capillaries and 60% remaining inside the capillaries
    # m values for 2x2, four way: m = .228
    # m values for 3x3, four way: m = .105
    # m values for 2x2, eight way: m = .2513
    # m values for 3x3, eight way: m = .098

    # the observed area, initial capillary value, initial tissue value, and fraction of capillaries "dead" (diffusing normally without replenishment) can be changed here.
    initializer = Initializer.Initializer3x3(
        rows=200, cols=200, cap=cap / 4, tissue=cap / 4,
        fraction_dead=.25)  # to view the 2x2 layout, replace 3x3 with 2x2
    # the tissue death threshold, and live tissue consumption can be changed here.
    diffuser = Diffuser.DiffuserEightWay(
        initializer.tissue,
        initializer.caps,
        initializer.cap_value,
        death_tissue=cap * .05,
        m=.098)  # to view the four way diffusion, replace Eight with Four

    # Comment / uncomment out the plotters below to generate different plots
    plotter = Plotter.InteractivePlotter(
        cap=initializer.cap_value
    )  # shows the visual evolution of the system over time,
    # capillaries can be turned off in real time, and time steps can be iterated through by pressing space
Ejemplo n.º 31
0
class BridgeCorrNet(object):
    def init(self,
             numpy_rng,
             theano_rng=None,
             l_rate=0.01,
             optimization="sgd",
             tied=False,
             n_visible_left=None,
             n_visible_right=None,
             n_visible_pivot=None,
             n_hidden=None,
             lamda=5,
             W_left=None,
             W_right=None,
             W_pivot=None,
             b=None,
             W_left_prime=None,
             W_right_prime=None,
             W_pivot_prime=None,
             b_prime_left=None,
             b_prime_right=None,
             b_prime_pivot=None,
             input_left=None,
             input_right=None,
             input_pivot=None,
             hidden_activation="sigmoid",
             output_activation="sigmoid",
             loss_fn="squarrederror",
             op_folder=None):
        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2**30))
        self.theano_rng = theano_rng

        self.optimization = optimization
        self.l_rate = l_rate

        self.optimizer = get_optimizer(self.optimization, self.l_rate)
        self.Initializer = Initializer(self.numpy_rng)

        self.n_visible_left = n_visible_left
        self.n_visible_right = n_visible_right
        self.n_visible_pivot = n_visible_pivot
        self.n_hidden = n_hidden
        self.lamda = lamda
        self.hidden_activation = hidden_activation
        self.output_activation = output_activation
        self.loss_fn = loss_fn
        self.tied = tied
        self.op_folder = op_folder

        self.W_left = self.Initializer.fan_based_sigmoid(
            "W_left", W_left, n_visible_left, n_hidden)
        self.optimizer.register_variable("W_left", n_visible_left, n_hidden)

        self.W_right = self.Initializer.fan_based_sigmoid(
            "W_right", W_right, n_visible_right, n_hidden)
        self.optimizer.register_variable("W_right", n_visible_right, n_hidden)

        self.W_pivot = self.Initializer.fan_based_sigmoid(
            "W_pivot", W_pivot, n_visible_pivot, n_hidden)
        self.optimizer.register_variable("W_pivot", n_visible_pivot, n_hidden)

        if not tied:
            self.W_left_prime = self.Initializer.fan_based_sigmoid(
                "W_left_prime", W_left_prime, n_hidden, n_visible_left)
            self.optimizer.register_variable("W_left_prime", n_hidden,
                                             n_visible_left)

            self.W_right_prime = self.Initializer.fan_based_sigmoid(
                "W_right_prime", W_right_prime, n_hidden, n_visible_right)
            self.optimizer.register_variable("W_right_prime", n_hidden,
                                             n_visible_right)

            self.W_pivot_prime = self.Initializer.fan_based_sigmoid(
                "W_pivot_prime", W_pivot_prime, n_hidden, n_visible_pivot)
            self.optimizer.register_variable("W_pivot_prime", n_hidden,
                                             n_visible_pivot)
        else:
            self.W_left_prime = self.W_left.T
            self.W_right_prime = self.W_right.T
            self.W_pivot_prime = self.W_pivot.T

        self.b = self.Initializer.zero_vector("b", b, n_hidden)
        self.optimizer.register_variable("b", 1, n_hidden)

        self.b_prime_left = self.Initializer.zero_vector(
            "b_prime_left", b_prime_left, n_visible_left)
        self.optimizer.register_variable("b_prime_left", 1, n_visible_left)

        self.b_prime_right = self.Initializer.zero_vector(
            "b_prime_right", b_prime_right, n_visible_right)
        self.optimizer.register_variable("b_prime_right", 1, n_visible_right)

        self.b_prime_pivot = self.Initializer.zero_vector(
            "b_prime_pivot", b_prime_pivot, n_visible_pivot)
        self.optimizer.register_variable("b_prime_pivot", 1, n_visible_pivot)

        if input_left is None:
            self.x_left = T.matrix(name='x_left')
        else:
            self.x_left = input_left

        if input_right is None:
            self.x_right = T.matrix(name='x_right')
        else:
            self.x_right = input_right

        if input_pivot is None:
            self.x_pivot = T.matrix(name='x_pivot')
        else:
            self.x_pivot = input_pivot

        if not tied:
            self.params = [
                self.W_left, self.W_right, self.W_pivot, self.b,
                self.b_prime_left, self.b_prime_right, self.b_prime_pivot,
                self.W_left_prime, self.W_right_prime, self.W_pivot_prime
            ]
            self.param_names = [
                "W_left", "W_right", "W_pivot", "b", "b_prime_left",
                "b_prime_right", "b_prime_pivot", "W_left_prime",
                "W_right_prime", "W_pivot_prime"
            ]
        else:
            self.params = [
                self.W_left, self.W_right, self.W_pivot, self.b,
                self.b_prime_left, self.b_prime_right, self.b_prime_pivot
            ]
            self.param_names = [
                "W_left", "W_right", "W_pivot", "b", "b_prime_left",
                "b_prime_right", "b_prime_pivot"
            ]

        self.proj_from_left = theano.function([self.x_left],
                                              self.project_from_sources([LEFT
                                                                         ]))
        self.proj_from_right = theano.function([self.x_right],
                                               self.project_from_sources(
                                                   [RIGHT]))
        self.proj_from_pivot = theano.function([self.x_pivot],
                                               self.project_from_sources(
                                                   [PIVOT]))

        self.proj_from_left_pivot = theano.function(
            [self.x_left, self.x_pivot],
            self.project_from_sources([LEFT, PIVOT]))
        self.proj_from_right_pivot = theano.function(
            [self.x_right, self.x_pivot],
            self.project_from_sources([RIGHT, PIVOT]))
        self.proj_from_left_right = theano.function(
            [self.x_right, self.x_left],
            self.project_from_sources([RIGHT, LEFT]))
        self.proj_from_all = theano.function(
            [self.x_right, self.x_left, self.x_pivot],
            self.project_from_sources([RIGHT, LEFT, PIVOT]))

        self.recon_from_left = theano.function([self.x_left],
                                               self.reconstruct_from_sources(
                                                   [LEFT]))
        self.recon_from_right = theano.function([self.x_right],
                                                self.reconstruct_from_sources(
                                                    [RIGHT]))
        self.recon_from_pivot = theano.function([self.x_pivot],
                                                self.reconstruct_from_sources(
                                                    [PIVOT]))

        self.recon_from_left_pivot = theano.function(
            [self.x_left, self.x_pivot],
            self.reconstruct_from_sources([LEFT, PIVOT]))
        self.recon_from_right_pivot = theano.function(
            [self.x_right, self.x_pivot],
            self.reconstruct_from_sources([RIGHT, PIVOT]))
        self.recon_from_right_left = theano.function(
            [self.x_right, self.x_left],
            self.reconstruct_from_sources([RIGHT, LEFT]))
        self.recon_from_all = theano.function(
            [self.x_right, self.x_left, self.x_pivot],
            self.reconstruct_from_sources([RIGHT, LEFT, PIVOT]))

        self.save_params()

    def get_corr_loss(self, y1, y2):
        y1_mean = T.mean(
            y1, axis=0
        )  # Find mean for each dimension (columns) over all samples (rows)
        y1_centered = y1 - y1_mean
        y2_mean = T.mean(y2, axis=0)
        y2_centered = y2 - y2_mean
        corr_nr = T.sum(y1_centered * y2_centered, axis=0)
        # y1_centered * y2_centered finds the dimension-wise (column-wise) product of the mean centered value VECTORS
        # For a matrix, * does element wise ops
        # T.sum with axis=0 sums of the dimension-wise products over all samples
        corr_dr1 = T.sqrt(T.sum(y1_centered * y1_centered, axis=0) + 1e-8)
        corr_dr2 = T.sqrt(T.sum(y2_centered * y2_centered, axis=0) + 1e-8)
        corr_dr = corr_dr1 * corr_dr2
        corr = corr_nr / corr_dr  # dimension(column)-wise division
        L = T.sum(
            corr
        ) * self.lamda  # adds each dimension's correlation together to get a scalar
        return (L)

    def train_common(self, mtype="1111"):
        [z1_right, z1_left, z1_pivot] = self.reconstruct_from_sources([LEFT])
        L_left_only = loss(z1_left, self.x_left, self.loss_fn) + loss(
            z1_right, self.x_right, self.loss_fn) + loss(
                z1_pivot, self.x_pivot, self.loss_fn)

        [z2_right, z2_left, z2_pivot] = self.reconstruct_from_sources([RIGHT])
        L_right_only = loss(z2_left, self.x_left, self.loss_fn) + loss(
            z2_right, self.x_right, self.loss_fn) + loss(
                z2_pivot, self.x_pivot, self.loss_fn)

        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([PIVOT])
        L_pivot_only = loss(z_left, self.x_left, self.loss_fn) + loss(
            z_right, self.x_right, self.loss_fn) + loss(
                z_pivot, self.x_pivot, self.loss_fn)

        [z3_right, z3_left,
         z3_pivot] = self.reconstruct_from_sources([RIGHT, LEFT, PIVOT])
        L_all_recon = loss(z3_left, self.x_left, self.loss_fn) + loss(
            z3_right, self.x_right, self.loss_fn) + loss(
                z3_pivot, self.x_pivot, self.loss_fn)

        [z6_right, z6_left,
         z6_pivot] = self.reconstruct_from_sources([RIGHT, LEFT])
        L_right_left = loss(z6_left, self.x_left, self.loss_fn) + loss(
            z6_right, self.x_right, self.loss_fn) + loss(
                z6_pivot, self.x_pivot, self.loss_fn)

        [z7_right, z7_left,
         z7_pivot] = self.reconstruct_from_sources([RIGHT, PIVOT])
        L_right_pivot = loss(z7_left, self.x_left, self.loss_fn) + loss(
            z7_right, self.x_right, self.loss_fn) + loss(
                z7_pivot, self.x_pivot, self.loss_fn)

        [z8_right, z8_left,
         z8_pivot] = self.reconstruct_from_sources([LEFT, PIVOT])
        L_left_pivot = loss(z8_left, self.x_left, self.loss_fn) + loss(
            z8_right, self.x_right, self.loss_fn) + loss(
                z8_pivot, self.x_pivot, self.loss_fn)

        L3 = L_pivot_only + L_all_recon + L_right_left + L_right_pivot + L_left_pivot

        y1 = self.project_from_sources([LEFT])
        y2 = self.project_from_sources([RIGHT])
        y3 = self.project_from_sources([PIVOT])

        L_corr_right_left = self.get_corr_loss(y1, y2)
        L_corr_left_pivot = self.get_corr_loss(y1, y3)
        L_corr_right_pivot = self.get_corr_loss(y2, y3)

        L4 = L_corr_right_left + L_corr_left_pivot + L_corr_right_pivot

        L5 = loss(z1_pivot, self.x_pivot, self.loss_fn) + loss(z1_right, self.x_right, self.loss_fn) + \
             loss(z2_pivot, self.x_pivot, self.loss_fn) + loss(z2_left, self.x_left, self.loss_fn) + \
             loss(z_left, self.x_left, self.loss_fn) + loss(z_right, self.x_right, self.loss_fn)

        if mtype == "1111":
            print "1111"
            L = L_left_only + L_right_only + L3 - L4  # L5 not needed since it is already covered in L_left_ony, L_right_only
            # and L_pivot_only
        elif mtype == "1110":
            print "1110"
            L = L_left_only + L_right_only + L3
        elif mtype == "1101":
            print "1101"
            L = L_left_only + L_right_only - L4
        elif mtype == "0011":
            print "0011"
            L = L3 - L4
        elif mtype == "1100":
            print "1100"
            L = L_left_only + L_right_only
        elif mtype == "0010":
            print "0010"
            L = L3
        elif mtype == "euc":
            print "euc"
            L = L5
        elif mtype == "euc-cor":
            print "euc-cor"
            L = L5 - L4
        elif mtype == "all":
            L = L_left_only

        cost = T.mean(L)

        gradients = T.grad(cost, self.params)
        updates = []
        for p, g, n in zip(self.params, gradients, self.param_names):
            gr, upd = self.optimizer.get_grad_update(n, g)
            updates.append((p, p + gr))
            updates.extend(upd)

        return cost, updates

    def train_left_pivot(self, mtype="1111"):
        [z1_right, z1_left, z1_pivot] = self.reconstruct_from_sources([LEFT])
        L_left = loss(z1_left, self.x_left, self.loss_fn) + loss(
            z1_pivot, self.x_pivot, self.loss_fn)

        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([PIVOT])
        L_pivot = loss(z_left, self.x_left, self.loss_fn) + loss(
            z_pivot, self.x_pivot, self.loss_fn)

        [z8_right, z8_left,
         z8_pivot] = self.reconstruct_from_sources([LEFT, PIVOT])
        L_left_pivot = loss(z8_left, self.x_left, self.loss_fn) + loss(
            z8_pivot, self.x_pivot, self.loss_fn)

        y1 = self.project_from_sources([LEFT])
        y3 = self.project_from_sources([PIVOT])

        L_corr_left_pivot = self.get_corr_loss(y1, y3)

        L4 = L_corr_left_pivot

        L5 = loss(z1_pivot, self.x_pivot, self.loss_fn) + loss(
            z_left, self.x_left, self.loss_fn)

        if mtype == "1111":
            print "1111"
            L = L_left + L_pivot + L_left_pivot - L4
        elif mtype == "1110":
            print "1110"
            L = L_left + L_pivot + L_left_pivot
        elif mtype == "1101":
            print "1101"
            L = L_left + L_pivot - L4
        elif mtype == "0011":
            print "0011"
            L = L_left_pivot - L4
        elif mtype == "1100":
            print "1100"
            L = L_left + L_pivot
        elif mtype == "0010":
            print "0010"
            L = L_left_pivot
        elif mtype == "euc":
            print "euc"
            L = L5
        elif mtype == "euc-cor":
            print "euc-cor"
            L = L5 - L4
        elif mtype == "all":
            L = L_left

        cost = T.mean(L)

        if self.tied:
            curr_params = [
                self.W_left, self.W_pivot, self.b, self.b_prime_left,
                self.b_prime_pivot
            ]
            curr_param_names = [
                "W_left", "W_pivot", "b", "b_prime_left", "b_prime_pivot"
            ]
        else:
            curr_params = [
                self.W_left, self.W_pivot, self.b, self.b_prime_left,
                self.b_prime_pivot, self.W_left_prime, self.W_pivot_prime
            ]
            curr_param_names = [
                "W_left", "W_pivot", "b", "b_prime_left", "b_prime_pivot"
                "W_left_prime", "W_pivot_prime"
            ]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p, g, n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n, g)  # upd is empty list
            updates.append((p, p + gr))
            updates.extend(upd)

        return cost, updates

    def train_right_pivot(self, mtype="1111"):
        [z1_right, z1_left, z1_pivot] = self.reconstruct_from_sources([RIGHT])
        L_right = loss(z1_right, self.x_right, self.loss_fn) + loss(
            z1_pivot, self.x_pivot, self.loss_fn)

        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([PIVOT])
        L_pivot = loss(z_right, self.x_right, self.loss_fn) + loss(
            z_pivot, self.x_pivot, self.loss_fn)

        [z8_right, z8_left,
         z8_pivot] = self.reconstruct_from_sources([RIGHT, PIVOT])
        L_right_pivot = loss(z8_right, self.x_right, self.loss_fn) + loss(
            z8_pivot, self.x_pivot, self.loss_fn)

        y1 = self.project_from_sources([RIGHT])
        y3 = self.project_from_sources([PIVOT])

        L_corr_right_pivot = self.get_corr_loss(y1, y3)

        L4 = L_corr_right_pivot

        L5 = loss(z1_pivot, self.x_pivot, self.loss_fn) + loss(
            z_right, self.x_right, self.loss_fn)

        if mtype == "1111":
            print "1111"
            L = L_right + L_pivot + L_right_pivot - L4
        elif mtype == "1110":
            print "1110"
            L = L_right + L_pivot + L_right_pivot
        elif mtype == "1101":
            print "1101"
            L = L_right + L_pivot - L4
        elif mtype == "0011":
            print "0011"
            L = L_right_pivot - L4
        elif mtype == "1100":
            print "1100"
            L = L_right + L_pivot
        elif mtype == "0010":
            print "0010"
            L = L_right_pivot
        elif mtype == "euc":
            print "euc"
            L = L5
        elif mtype == "euc-cor":
            print "euc-cor"
            L = L5 - L4
        elif mtype == "all":
            L = L_right

        cost = T.mean(L)

        if self.tied:
            curr_params = [
                self.W_right, self.W_pivot, self.b, self.b_prime_right,
                self.b_prime_pivot
            ]
            curr_param_names = [
                "W_right", "W_pivot", "b", "b_prime_right", "b_prime_pivot"
            ]
        else:
            curr_params = [
                self.W_right, self.W_pivot, self.b, self.b_prime_right,
                self.b_prime_pivot, self.W_right_prime, self.W_pivot_prime
            ]
            curr_param_names = [
                "W_right", "W_pivot", "b", "b_prime_right", "b_prime_pivot"
                "W_right_prime", "W_pivot_prime"
            ]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p, g, n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(
                n, g
            )  # Just clips the gradient and multiplies the learning rate to
            #  the gradients. upd is empty list here
            updates.append((p, p + gr))
            updates.extend(upd)

        return cost, updates

    def train_left(self):
        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([LEFT])
        L = loss(z_left, self.x_left, self.loss_fn)
        cost = T.mean(L)

        if self.tied:
            curr_params = [self.W_left, self.b, self.b_prime_left]
            curr_param_names = ["W_left", "b", "b_prime_left"]
        else:
            curr_params = [
                self.W_left, self.b, self.b_prime_left, self.W_left_prime
            ]
            curr_param_names = ["W_left", "b", "b_prime_left", "W_left_prime"]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p, g, n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n, g)
            updates.append((p, p + gr))
            updates.extend(upd)
        return cost, updates

    def train_right(self):
        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([RIGHT])
        L = loss(z_right, self.x_right, self.loss_fn)
        cost = T.mean(L)

        if self.tied:
            curr_params = [self.W_right, self.b, self.b_prime_right]
            curr_param_names = ["W_right", "b", "b_prime_right"]
        else:
            curr_params = [
                self.W_right, self.b, self.b_prime_right, self.W_right_prime
            ]
            curr_param_names = [
                "W_right", "b", "b_prime_right", "W_right_prime"
            ]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p, g, n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n, g)
            updates.append((p, p + gr))
            updates.extend(upd)
        return cost, updates

    def train_pivot(self):
        [z_right, z_left, z_pivot] = self.reconstruct_from_sources([PIVOT])
        L = loss(z_pivot, self.x_pivot, self.loss_fn)
        cost = T.mean(L)

        if self.tied:
            curr_params = [self.W_pivot, self.b, self.b_prime_pivot]
            curr_param_names = ["W_pivot", "b", "b_prime_pivot"]
        else:
            curr_params = [
                self.W_pivot, self.b, self.b_prime_pivot, self.W_pivot_prime
            ]
            curr_param_names = [
                "W_pivot", "b", "b_prime_pivot", "W_pivot_prime"
            ]

        gradients = T.grad(cost, curr_params)
        updates = []
        for p, g, n in zip(curr_params, gradients, curr_param_names):
            gr, upd = self.optimizer.get_grad_update(n, g)
            updates.append((p, p + gr))
            updates.extend(upd)
        return cost, updates

    def project_from_sources(self, source_list):
        y_pre = self.b
        for source in source_list:
            if source == LEFT:
                y_pre += T.dot(self.x_left, self.W_left)
            elif source == RIGHT:
                y_pre += T.dot(self.x_right, self.W_right)
            elif source == PIVOT:
                y_pre += T.dot(self.x_pivot, self.W_pivot)
        y = activation(y_pre, self.hidden_activation)
        return y

    def reconstruct_from_sources(self, source_list):
        y = self.project_from_sources(source_list)
        recon_left = activation(
            T.dot(y, self.W_left_prime) + self.b_prime_left,
            self.output_activation)
        recon_right = activation(
            T.dot(y, self.W_right_prime) + self.b_prime_right,
            self.output_activation)
        recon_pivot = activation(
            T.dot(y, self.W_pivot_prime) + self.b_prime_pivot,
            self.output_activation)
        recon_list = [recon_right, recon_left, recon_pivot]
        return recon_list

    def get_lr_rate(self):
        return self.optimizer.get_l_rate()

    def set_lr_rate(self, new_lr):
        self.optimizer.set_l_rate(new_lr)

    def save_matrices(self):
        for p, nm in zip(self.params, self.param_names):
            numpy.save(self.op_folder + nm, p.get_value(borrow=True))

    def save_params(self):
        params = {}
        params["optimization"] = self.optimization
        params["l_rate"] = self.l_rate
        params["n_visible_left"] = self.n_visible_left
        params["n_visible_right"] = self.n_visible_right
        params["n_visible_pivot"] = self.n_visible_pivot
        params["n_hidden"] = self.n_hidden
        params["lamda"] = self.lamda
        params["hidden_activation"] = self.hidden_activation
        params["output_activation"] = self.output_activation
        params["loss_fn"] = self.loss_fn
        params["tied"] = self.tied
        params["numpy_rng"] = self.numpy_rng
        params["theano_rng"] = self.theano_rng
        pickle.dump(params, open(self.op_folder + "params.pck", "wb"), -1)

    def load(self,
             folder,
             input_left=None,
             input_right=None,
             input_pivot=None):

        plist = pickle.load(open(folder + "params.pck", "rb"))

        self.init(plist["numpy_rng"],
                  theano_rng=plist["theano_rng"],
                  l_rate=plist["l_rate"],
                  optimization=plist["optimization"],
                  tied=plist["tied"],
                  n_visible_left=plist["n_visible_left"],
                  n_visible_right=plist["n_visible_right"],
                  n_visible_pivot=plist["n_visible_pivot"],
                  n_hidden=plist["n_hidden"],
                  lamda=plist["lamda"],
                  W_left=folder + "W_left",
                  W_right=folder + "W_right",
                  W_pivot=folder + "W_pivot",
                  b=folder + "b",
                  W_left_prime=folder + "W_left_prime",
                  W_right_prime=folder + "W_right_prime",
                  W_pivot_prime=folder + "W_pivot_prime",
                  b_prime_left=folder + "b_prime_left",
                  b_prime_right=folder + "b_prime_right",
                  b_prime_pivot=folder + "b_prime_pivot",
                  input_left=input_left,
                  input_right=input_right,
                  input_pivot=input_pivot,
                  hidden_activation=plist["hidden_activation"],
                  output_activation=plist["output_activation"],
                  loss_fn=plist["loss_fn"],
                  op_folder=folder)
Ejemplo n.º 32
0
    def init(self,
             numpy_rng,
             theano_rng=None,
             l_rate=0.01,
             optimization="sgd",
             tied=False,
             n_visible_left=None,
             n_visible_right=None,
             n_visible_pivot=None,
             n_hidden=None,
             lamda=5,
             W_left=None,
             W_right=None,
             W_pivot=None,
             b=None,
             W_left_prime=None,
             W_right_prime=None,
             W_pivot_prime=None,
             b_prime_left=None,
             b_prime_right=None,
             b_prime_pivot=None,
             input_left=None,
             input_right=None,
             input_pivot=None,
             hidden_activation="sigmoid",
             output_activation="sigmoid",
             loss_fn="squarrederror",
             op_folder=None):
        self.numpy_rng = numpy_rng
        if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2**30))
        self.theano_rng = theano_rng

        self.optimization = optimization
        self.l_rate = l_rate

        self.optimizer = get_optimizer(self.optimization, self.l_rate)
        self.Initializer = Initializer(self.numpy_rng)

        self.n_visible_left = n_visible_left
        self.n_visible_right = n_visible_right
        self.n_visible_pivot = n_visible_pivot
        self.n_hidden = n_hidden
        self.lamda = lamda
        self.hidden_activation = hidden_activation
        self.output_activation = output_activation
        self.loss_fn = loss_fn
        self.tied = tied
        self.op_folder = op_folder

        self.W_left = self.Initializer.fan_based_sigmoid(
            "W_left", W_left, n_visible_left, n_hidden)
        self.optimizer.register_variable("W_left", n_visible_left, n_hidden)

        self.W_right = self.Initializer.fan_based_sigmoid(
            "W_right", W_right, n_visible_right, n_hidden)
        self.optimizer.register_variable("W_right", n_visible_right, n_hidden)

        self.W_pivot = self.Initializer.fan_based_sigmoid(
            "W_pivot", W_pivot, n_visible_pivot, n_hidden)
        self.optimizer.register_variable("W_pivot", n_visible_pivot, n_hidden)

        if not tied:
            self.W_left_prime = self.Initializer.fan_based_sigmoid(
                "W_left_prime", W_left_prime, n_hidden, n_visible_left)
            self.optimizer.register_variable("W_left_prime", n_hidden,
                                             n_visible_left)

            self.W_right_prime = self.Initializer.fan_based_sigmoid(
                "W_right_prime", W_right_prime, n_hidden, n_visible_right)
            self.optimizer.register_variable("W_right_prime", n_hidden,
                                             n_visible_right)

            self.W_pivot_prime = self.Initializer.fan_based_sigmoid(
                "W_pivot_prime", W_pivot_prime, n_hidden, n_visible_pivot)
            self.optimizer.register_variable("W_pivot_prime", n_hidden,
                                             n_visible_pivot)
        else:
            self.W_left_prime = self.W_left.T
            self.W_right_prime = self.W_right.T
            self.W_pivot_prime = self.W_pivot.T

        self.b = self.Initializer.zero_vector("b", b, n_hidden)
        self.optimizer.register_variable("b", 1, n_hidden)

        self.b_prime_left = self.Initializer.zero_vector(
            "b_prime_left", b_prime_left, n_visible_left)
        self.optimizer.register_variable("b_prime_left", 1, n_visible_left)

        self.b_prime_right = self.Initializer.zero_vector(
            "b_prime_right", b_prime_right, n_visible_right)
        self.optimizer.register_variable("b_prime_right", 1, n_visible_right)

        self.b_prime_pivot = self.Initializer.zero_vector(
            "b_prime_pivot", b_prime_pivot, n_visible_pivot)
        self.optimizer.register_variable("b_prime_pivot", 1, n_visible_pivot)

        if input_left is None:
            self.x_left = T.matrix(name='x_left')
        else:
            self.x_left = input_left

        if input_right is None:
            self.x_right = T.matrix(name='x_right')
        else:
            self.x_right = input_right

        if input_pivot is None:
            self.x_pivot = T.matrix(name='x_pivot')
        else:
            self.x_pivot = input_pivot

        if not tied:
            self.params = [
                self.W_left, self.W_right, self.W_pivot, self.b,
                self.b_prime_left, self.b_prime_right, self.b_prime_pivot,
                self.W_left_prime, self.W_right_prime, self.W_pivot_prime
            ]
            self.param_names = [
                "W_left", "W_right", "W_pivot", "b", "b_prime_left",
                "b_prime_right", "b_prime_pivot", "W_left_prime",
                "W_right_prime", "W_pivot_prime"
            ]
        else:
            self.params = [
                self.W_left, self.W_right, self.W_pivot, self.b,
                self.b_prime_left, self.b_prime_right, self.b_prime_pivot
            ]
            self.param_names = [
                "W_left", "W_right", "W_pivot", "b", "b_prime_left",
                "b_prime_right", "b_prime_pivot"
            ]

        self.proj_from_left = theano.function([self.x_left],
                                              self.project_from_sources([LEFT
                                                                         ]))
        self.proj_from_right = theano.function([self.x_right],
                                               self.project_from_sources(
                                                   [RIGHT]))
        self.proj_from_pivot = theano.function([self.x_pivot],
                                               self.project_from_sources(
                                                   [PIVOT]))

        self.proj_from_left_pivot = theano.function(
            [self.x_left, self.x_pivot],
            self.project_from_sources([LEFT, PIVOT]))
        self.proj_from_right_pivot = theano.function(
            [self.x_right, self.x_pivot],
            self.project_from_sources([RIGHT, PIVOT]))
        self.proj_from_left_right = theano.function(
            [self.x_right, self.x_left],
            self.project_from_sources([RIGHT, LEFT]))
        self.proj_from_all = theano.function(
            [self.x_right, self.x_left, self.x_pivot],
            self.project_from_sources([RIGHT, LEFT, PIVOT]))

        self.recon_from_left = theano.function([self.x_left],
                                               self.reconstruct_from_sources(
                                                   [LEFT]))
        self.recon_from_right = theano.function([self.x_right],
                                                self.reconstruct_from_sources(
                                                    [RIGHT]))
        self.recon_from_pivot = theano.function([self.x_pivot],
                                                self.reconstruct_from_sources(
                                                    [PIVOT]))

        self.recon_from_left_pivot = theano.function(
            [self.x_left, self.x_pivot],
            self.reconstruct_from_sources([LEFT, PIVOT]))
        self.recon_from_right_pivot = theano.function(
            [self.x_right, self.x_pivot],
            self.reconstruct_from_sources([RIGHT, PIVOT]))
        self.recon_from_right_left = theano.function(
            [self.x_right, self.x_left],
            self.reconstruct_from_sources([RIGHT, LEFT]))
        self.recon_from_all = theano.function(
            [self.x_right, self.x_left, self.x_pivot],
            self.reconstruct_from_sources([RIGHT, LEFT, PIVOT]))

        self.save_params()
 def test_solution(self):
     random_topology = ini.make_random_topology(400, 3)
     solution = sa_ds.solution(random_topology)