Beispiel #1
0
    def __init__(self, n_samples, warp_generator, img, pts, initial_warp, res):
        self.nodes = None
        self.resx = res[0]
        self.resy = res[1]
        self.gnn = True
        self.indx = []
        n_points = pts.shape[1]
        print "Sampling Warps..."
        self.warps = [np.asmatrix(np.eye(3))
                      ] + [warp_generator() for i in xrange(n_samples - 1)]
        print "Sampling Images..."
        self.images = np.empty((n_points, n_samples))
        for i, w in enumerate(self.warps):
            self.images[:, i] = sample_and_normalize(img, pts,
                                                     initial_warp * w.I)
        print('Graph based Nearest Neighbour')
        print('------------------------------')
        if self.gnn == True:
            #self.flann = pyflann.FLANN()
            #print(self.images.shape)
            #self.flann.build_index(self.images.T, algorithm='kdtree', trees=10)
            self.images = MA(self.images, 'f8')
            self.nodes = build_graph(self.images.T, 40)

        else:
            desc = self.list2array(self.pixel2sift(self.images))
            # --- Building Flann Index --- #
            self.flann = pyflann.FLANN()
        print "Done!"
def main():

    u = build_graph(sys.argv[1], sys.argv[2])

    pkl_file = open(sys.argv[3], "w")
    pickle.dump(u, pkl_file)
    pkl_file.close()
    def __init__(self, n_samples, warp_generator, img, pts, initial_warp,res):
	self.nodes = None
	self.resx = res[0]
	self.resy = res[1]
        self.gnn = True
        self.indx = []
	n_points = pts.shape[1]
        print "Sampling Warps..."
        self.warps = [np.asmatrix(np.eye(3))] + [warp_generator() for i in xrange(n_samples - 1)]
        print "Sampling Images..."
        self.images = np.empty((n_points, n_samples))
        for i,w in enumerate(self.warps):
            self.images[:,i] = sample_and_normalize(img, pts, initial_warp * w.I)
        print('Graph based Nearest Neighbour')
        print('------------------------------')
        if self.gnn == True:
            #self.flann = pyflann.FLANN()
	    #print(self.images.shape)
            #self.flann.build_index(self.images.T, algorithm='kdtree', trees=10)
	    self.images = MA(self.images,'f8')
	    self.nodes = build_graph(self.images.T,40)
           
        else:
            desc = self.list2array(self.pixel2sift(self.images))
            # --- Building Flann Index --- #
            self.flann = pyflann.FLANN()
        print "Done!"
Beispiel #4
0
def main():
    if len(sys.argv) < 2:
        print "you didn't specify a filename"
        return

    fp = open(sys.argv[1])
    j = json.load(fp)
    fp.close()

    nodes = build_graph(j)
    # print_graph(nodes)

    running = True
    while running:
        if not collapse_graph(nodes):
            running = False

    print_graph(nodes)
Beispiel #5
0
    Mask1 = WR_c + WL_c
    Mask2 = WU_c + WD_c
    Mask = Mask1 + Mask2
    Mask = np.where(Mask > 0, 1, 0)
    Mask = np.multiply(Mask, 255)  # use white color for segmentation boudry

    img = img + Mask
    img = np.where(img > 255, 255, img)
    cv2.imwrite('seg.png', img)


#test with small graph, wait, need to count active tiles
if __name__ == "__main__":

    img, WEIGHTS_UP, WEIGHTS_DOWN, WEIGHTS_LEFT, WEIGHTS_RIGHT, EXCESS_FLOW, PF, PB = build_graph(
        GRID_SIZE, BLOCK_SIZE, img, WEIGHTS_UP, WEIGHTS_DOWN, WEIGHTS_LEFT,
        WEIGHTS_RIGHT, EXCESS_FLOW, PF, PB)

    ORG_WU = np.copy(WEIGHTS_UP)
    ORG_WD = np.copy(WEIGHTS_DOWN)
    ORG_WL = np.copy(WEIGHTS_LEFT)
    ORG_WR = np.copy(WEIGHTS_RIGHT)

    maxflow()
    generate_image_seg_mask(WEIGHTS_LEFT, WEIGHTS_RIGHT, WEIGHTS_UP,
                            WEIGHTS_DOWN, img)
    #SEG = mincut(WEIGHTS_UP, WEIGHTS_DOWN, WEIGHTS_LEFT, WEIGHTS_RIGHT, ORG_WU, ORG_WD, ORG_WL, ORG_WR, SEG, GRID_SIZE, BLOCK_SIZE)

    wl = 0
    wr = 0
    wu = 0
def data_prep_train(sequence, detections, images_path, frames_look_back,
                    total_frames, most_recent_frame_back, graph_jump,
                    current_frame_train, current_frame_valid, distance_limit,
                    fps, type):

    if total_frames == None:
        total_frames = np.max(
            detections[:, 0]
        )  #change only if you want a subset of the total frames
    detections = sorted(detections, key=lambda x: x[0])
    data_list = []
    acceptable_object_types = [1, 2, 7]  # MOT specific types
    if type == "training":
        total_frames = current_frame_valid
        current_frame = current_frame_train
    elif type == "validation":
        total_frames = total_frames
        current_frame = current_frame_valid

    while current_frame <= total_frames:

        print("Sequence: " + sequence + ", Frame: " + str(current_frame) +
              "/" + str(int(total_frames)))
        ####find tracklets and new detections
        current_detections = []
        tracklets = []
        tracklet_IDs = []
        for j, detection in enumerate(detections):
            if detection[0] > current_frame:
                break
            else:
                xmin, ymin, width, height = int(round(detection[2])), int(round(detection[3])), \
                                            int(round(detection[4])), int(round(detection[5]))
                object_type = detection[7]
                if xmin > 0 and ymin > 0 and width > 0 and height > 0 and (
                        object_type in acceptable_object_types):
                    most_recent_frame_back2 = randint(1,
                                                      most_recent_frame_back)
                    if current_frame - most_recent_frame_back2 < 1:
                        most_recent_frame_back2 = 1
                    temp = current_frame - (most_recent_frame_back2 - 1)
                    if (detection[0] <
                            temp) and detection[0] >= temp - frames_look_back:
                        new_tracklet = True
                        for k, i in enumerate(tracklet_IDs):
                            if detection[1] == i:
                                new_tracklet = False
                                tracklets[k].append(detection)
                                break
                        if new_tracklet == True:
                            tracklet_IDs.append(int(detection[1]))
                            tracklets.append([detection])
                    elif detection[0] == current_frame:
                        current_detections.append(detection)
        data = build_graph(tracklets,
                           current_detections,
                           images_path,
                           current_frame,
                           distance_limit,
                           fps,
                           test=False)
        data_list.append(data)
        current_frame += graph_jump
    print("Data preparation finished")
    return data_list
Beispiel #7
0
from build_graph import *
from collapse_graph import *
from testutils import *

# Arduino interconnectivity
import serial

f = open('sample.upv')
obj = json.load(f)
f.close()

nodes = build_graph(obj)
# print_graph(nodes)

test_pairs = get_test_pairs(nodes)

print "Begin debugging? [y/n]"
a = raw_input()
if a.lower().strip() != 'y' :
    exit()
    

# initialize Arduino connection
try:
    ser = serial.Serial('/dev/tty.usbserial', 9600)
except:
    print 'USB connection failed!'
    exit()
line = 0

while line < len(test_pairs):
Beispiel #8
0
def main(_):

    # download and load data sets
    alldata = dataset(FLAGS.trainDir)
    alldata.maybe_download_and_extract()
    train_data, _, train_labels = alldata.load_training_data()
    test_data, _, test_labels = alldata.load_test_data()
    class_names = alldata.load_class_names()

    iterations = int(
        train_data.shape[0] /
        configTrain.batch_size)  # total training iterations in each epoch

    tf.reset_default_graph()
    g = tf.Graph()
    with g.as_default():

        model = build_graph(configModel)

        init = tf.global_variables_initializer()

        with tf.Session() as sess:
            sess.run(init)

            ## start training epochs
            epoch = 1
            while epoch <= configTrain.epochs:

                now = datetime.now()
                train_model_for_one_epoch(iterations,
                                          train_data,
                                          train_labels,
                                          model,
                                          sess,
                                          configTrain,
                                          record_train_loss=True)
                used_time = datetime.now() - now

                print("\nEpoch round ", epoch,
                      ' used {0} seconds. '.format(used_time.seconds))

                val_loss, val_accuracy = generate_prediction(
                    test_data, test_labels, model, sess, ['loss', 'accuracy'])
                validation_loss.append(val_loss)
                validation_accu.append(val_accuracy)
                print("Valiation loss ", val_loss, " and accuracy ",
                      val_accuracy)

                ## if required, visualize activations from image
                if configTrain.vis_weights_every_epoch > 0 and epoch % configTrain.vis_weights_every_epoch == 0:

                    vis_activations_from_model(train_data, model, sess,
                                               FLAGS.trainDir, 10)

                epoch += 1

            ## upon training done, plot training & validation losses and validation accuracy
            print("training done.")
            plot_training_loss(
                training_loss, os.path.join(FLAGS.trainDir,
                                            'train_losses.png'))
            plot_val_loss_n_accuracy(
                validation_loss, validation_accu,
                os.path.join(FLAGS.trainDir, 'val_losses_n_accuracy.png'))

            ## save trained session
            if not os.path.exists(FLAGS.savedSessionDir):
                os.makedirs(FLAGS.savedSessionDir)
            temp_saver = model['saver']()
            save_path = temp_saver.save(
                sess, os.path.join(FLAGS.savedSessionDir, modelName))

        print("\nTraining done. Model saved: ",
              os.path.join(FLAGS.savedSessionDir, modelName))
Beispiel #9
0
def model_testing(sequence, detections, images_path, total_frames, frames_look_back, model, distance_limit, fp_min_times_seen, match_thres, det_conf_thres, fp_look_back, fp_recent_frame_limit,min_height,fps):

    device = torch.device('cuda')
    #pick one frame and load previous results
    tf.io.gfile = tb.compat.tensorflow_stub.io.gfile
    current_frame= 2
    id_num= 0
    tracking_output= []
    checked_ids = []
    
    transform = ToTensor()

    while current_frame <= total_frames:
        print("Sequence: " + sequence+ ", Frame: " + str(current_frame)+'/'+str(int(total_frames)))
        data_list = []
        #Give IDs to the first frame
        tracklets = []
        if not tracking_output:
            for i, detection in enumerate(detections):
                if detection[0] == 1:
                    frame = detection[0]
                    xmin, ymin, width, height = int(round(detection[2])), int(round(detection[3])), \
                                                int(round(detection[4])), int(round(detection[5]))
                    confidence= detection[6]
                    if xmin > 0 and ymin > 0 and width > 0 and height > min_height and confidence>det_conf_thres:
                        id_num += 1
                        ID= int(id_num)
                        tracking_output.append([frame, ID, xmin, ymin, width, height, \
                            int(detection[6]), 1, 1])
                        tracklets.append([[frame, ID, xmin, ymin, width, height, \
                                                int(detection[6]), 1, 1]])
                else:
                    detections= detections[i:]
                    break
        else:
            #Get all tracklets
            tracklet_IDs = []
            for j, tracklet in enumerate(tracking_output):
                xmin, ymin, width, height = int(round(tracklet[2])), int(round(tracklet[3])), \
                                            int(round(tracklet[4])), int(round(tracklet[5]))
                if xmin > 0 and ymin > 0 and width > 0 and height > 0:
                    if (tracklet[0]<current_frame) and tracklet[0]>=current_frame-frames_look_back:
                        new_tracklet= True
                        for k,i in enumerate(tracklet_IDs):
                            if tracklet[1]==i:
                                new_tracklet=False
                                tracklets[k].append(tracklet)
                                break
                        if new_tracklet==True:
                            tracklet_IDs.append(int(tracklet[1]))
                            tracklets.append([tracklet])
        #Get new detections
        current_detections = []
        for i, detection in enumerate(detections):
            if detection[0] == current_frame:
                frame = detection[0]
                xmin, ymin, width, height = int(round(detection[2])), int(round(detection[3])), \
                                            int(round(detection[4])), int(round(detection[5]))
                confidence= detection[6]
                if xmin > 0 and ymin > 0 and width > 0 and height > min_height and confidence>det_conf_thres:
                    current_detections.append([frame, -1, xmin, ymin, width, height, \
                        int(detection[6]), 1, 1])
            else:
                detections= detections[i:]
                break
        #build graph and run model
        data = build_graph(tracklets, current_detections, images_path, current_frame, distance_limit, fps, test=True)
        if data:
            if current_detections and data.edge_attr.size()[0]!=0:
                data_list.append(data)

                loader = DataListLoader(data_list)
                for graph_num, batch in enumerate(loader):
                    #MODEL FORWARD
                    output, output2, ground_truth, ground_truth2, det_num, tracklet_num= model(batch)
                    #FEATURE MAPS on tensorboard
                    #embedding
                    images= batch[0].x
                    images = F.interpolate(images, size=250)
                    edge_index= data_list[graph_num].edges_complete
                    #THRESHOLDS
                    temp= []
                    for i in output2:
                        if i>match_thres:
                            temp.append(i)
                        else:
                            temp.append(i-i)
                    output2= torch.stack(temp)
                    # HUNGARIAN
                    cleaned_output= hungarian(output2, ground_truth2, det_num, tracklet_num)
                    # Give Ids to current frame
                    for i,detection in enumerate(current_detections):
                        match_found= False
                        for k,m in enumerate(cleaned_output):#cleaned_output):
                            if m==1 and edge_index[1,k]==i+len(tracklets): #match found
                                ID= tracklets[edge_index[0,k]][-1][1]
                                frame = detection[0]
                                xmin, ymin, width, height = int(round(detection[2])), int(round(detection[3])), \
                                                            int(round(detection[4])), int(round(detection[5]))
                                tracking_output.append([frame, ID, xmin, ymin, width, height, \
                                                        int(detection[6]), 1, 1])
                                match_found = True
                                break
                        if match_found==False: #give new ID
                            # print("no match")
                            id_num += 1
                            ID= id_num
                            frame = detection[0]
                            xmin, ymin, width, height = int(round(detection[2])), int(round(detection[3])), \
                                                        int(round(detection[4])), int(round(detection[5]))
                            tracking_output.append([frame, ID, xmin, ymin, width, height, \
                                                    int(detection[6]), 1, 1])
                    #Clean output for false positives
                    if current_frame>=fp_look_back:
                        # reduce to recent objects
                        recent_tracks = [i for i in tracking_output if i[0] >= current_frame-fp_look_back]
                        # find the different IDs
                        candidate_ids= []
                        times_seen= []
                        first_frame_seen= []
                        for i in recent_tracks:
                            if i[1] not in checked_ids:
                                if i[1] not in candidate_ids:
                                    candidate_ids.append(i[1])
                                    times_seen.append(1)
                                    first_frame_seen.append(i[0])
                                else:
                                    index= candidate_ids.index(i[1])
                                    times_seen[index]= times_seen[index] + 1
                        # find which IDs to remove
                        remove_ids = []
                        for i,j in enumerate(candidate_ids):
                            if times_seen[i] < fp_min_times_seen and current_frame-first_frame_seen[i]>=fp_look_back:
                                remove_ids.append(j)
                            elif times_seen[i] > fp_min_times_seen:
                                checked_ids.append(j)
                        #keep only those IDs that are seen enough times
                        tracking_output = [j for j in tracking_output if j[1] not in remove_ids]
        current_frame += 1
    # reduce to recent objects
    recent_tracks = [i for i in tracking_output if i[0] >= current_frame-fp_look_back]
    # find the different IDs
    candidate_ids= []
    times_seen= []
    for i in recent_tracks:
        if i[1] not in checked_ids:
            if i[1] not in candidate_ids:
                candidate_ids.append(i[1])
                times_seen.append(1)
            else:
                index= candidate_ids.index(i[1])
                times_seen[index]= times_seen[index] + 1
    # find which IDs to remove
    remove_ids = []
    for i,j in enumerate(candidate_ids):
        if times_seen[i] < fp_min_times_seen:
            remove_ids.append(j)
        elif times_seen[i] > fp_min_times_seen:
            checked_ids.append(j)
    #keep only those IDs that are seen enough times
    tracking_output = [j for j in tracking_output if j[1] not in remove_ids]

    return tracking_output
        champion_list.append(support_champion)
        role_list.append('support')
    elif role_list[0] == 'support':
        # select the 2nd based on the 1st: adc
        query = ('SELECT adc, support, max(winrate) '
                 'FROM adc_support_matchups_new '
                 'WHERE support = \'' + champion_list[0] + '\' '
                 'GROUP BY support')
        mycursor.execute(query)
        adc_champion, _, _ = mycursor.fetchall()[0]
        champion_list.append(adc_champion)
        role_list.append('adc')
# print(champion_list)
# print(role_list)

G = build_graph()

# stage 2 select, if jungle, top, middle have been selected, we select adc, support
if 'jungle' in role_list:
    adc_list = []
    support_list = []
    query = ('SELECT name, role '
             'FROM single_champion_stats '
             'WHERE role IN (\'adc\',\'support\') AND '
             'winrate = (SELECT max(winrate) '
             'FROM single_champion_stats as scs '
             'WHERE scs.name=single_champion_stats.name) ')
    mycursor.execute(query)
    for champion, role in mycursor.fetchall():
        if role == 'adc':
            adc_list.append(champion)
Beispiel #11
0
def homepage():
    ze_path = os.path.join(folder, '20140315/http.log')
    # get fields and values from all files
    nestLst, fieldsLst, ipLst, mostDomLst = getNestLst(ze_path)
    ipLstSet = set(ipLst)

    ipLstSet = set(ipLst)
    ipnumber = []
    hits = []

    # method to build graph with ip and number of times ip was logged
    for ip in ipLstSet:
        if ipLst.count(ip) > 1:
            ipnumber.append(ip)
            hits.append(ipLst.count(ip))

    # dictionary for geolocation records
    recordDict = {}
    # returns dicitonary with records for ip with more than 2 occurences
    for ip in ipLstSet:
        if ipLst.count(ip) > 2:
            record = ipLocator(ip)
            recordDict[ip] = record
    agentList = []
    for agent in nestLst:
        agentList.append(agent[11])
    setAgentLst = set(agentList)

    # dictionary for user agent and occurrences count
    agentDict = {}
    for agent in setAgentLst:
        if agentList.count(agent) >= 1:
            agentCount = agentList.count(agent)
            agentDict[agent] = agentCount
    # most used domain dictionary with counter for occurrences
    domainDict = {}
    for domain in mostDomLst:
        if mostDomLst.count(domain) >= 1:
            domainCount = mostDomLst.count(domain)
            domainDict[domain] = domainCount

    # most active private and public IP
    privIPLst = []
    for ip in nestLst:
        privIPLst.append(ip[2])
    setPrivIP = set(privIPLst)

    return render_template("homepage.html",
                           nestLst=nestLst,
                           fieldsLst=fieldsLst,
                           ipLst=ipLst,
                           ipLstSet=ipLstSet,
                           record=record,
                           recordDict=recordDict,
                           agentDict=agentDict,
                           domainDict=domainDict,
                           setPrivIP=setPrivIP,
                           privIPLst=privIPLst,
                           name='Most Active Public IP',
                           graph=build_graph(hits, ipnumber),
                           folder=folder)