Пример #1
0
 def randrange(self, start, stop=None, step=1, intType=int, default=None, maxWidth=9007199254740992L):
     oldCount = self.count
     result = Random.randrange(self, start, stop, step, intType, default, maxWidth)
     if Debug.random:
         self.game.debug('%d calls to random by Random.randrange(%d,%s) from %s' % (
             self.count - oldCount, start, stop, stack('')[-2]))
     return result
Пример #2
0
 def sample(self, population, wantedLength):
     oldCount = self.count
     result = Random.sample(self, population, wantedLength)
     if Debug.random:
         self.game.debug('%d calls to random by Random.sample(x, %d) from %s' % (
             self.count - oldCount, wantedLength, stack('')[-2]))
     return result
Пример #3
0
def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print("Start:", problem.getStartState())
    print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
    print("Start's successors:", problem.getSuccessors(problem.getStartState()))
    """
    "*** YOUR CODE HERE ***"
    v = stack()
    visited = []
    visited.append(problem.getStartState())
    v.push((problem.getStartState(), []))
    while not v.isEmpty():
        (current_node, actions) = v.pop()

        if problem.isGoalState(current_node):
            return actions  ## if we reach goal state this is what it took to reach here

        children = problem.getSuccessors(current_node)
        for node in list(children):
            if node[0] not in visited:
                visited.append(node[0])
                v.push(
                    (node[0], actions + [node[1]])
                )  ##node 0 holds the xy; node[1] holds the action required to reach this node
    return []
Пример #4
0
    def __init__(self, S, D, T, N, M, error_threshold=1e-5):
        self.D = D
        self.T = T
        self.N = N
        self.S = S
        self.M_stacked = M  #M is S x D x T
        self.M_spread = util.spread(M)
        self.SS_tot = self.compute_total_sum_squares()
        self.error_threshold = error_threshold
        self.initialization_scale = 1.

        self.check_shapes()
        self.W_est_stacked = np.random.uniform(0,
                                               self.initialization_scale,
                                               size=(self.N, self.D, self.T))
        self.W_est_spread = util.spread(self.W_est_stacked)
        self.c_est = np.random.uniform(0,
                                       self.initialization_scale,
                                       size=(self.S, self.N))
        self.delays = np.zeros_like(self.c_est)  #******** change this

        self.construct_Theta()
        self.update_H()

        self.M_est_spread = self.W_est_spread.dot(self.H_spread)
        self.M_est_stacked = util.stack(self.M_est_spread, 2 * self.T)
Пример #5
0
 def shuffle(self, listValue, func=None):
     """pylint needed for python up to 2.7.5"""
     # pylint: disable=arguments-differ
     oldCount = self.count
     self.__shuffle(listValue, func)
     if Debug.random:
         self.game.debug(
             '%d out of %d calls to random by Random.shuffle from %s'
             % (self.count - oldCount, self.count, stack('')[-2]))
Пример #6
0
 def choice(self, fromList):
     if len(fromList) == 1:
         return fromList[0]
     oldCount = self.count
     result = Random.choice(self, fromList)
     if Debug.random:
         self.game.debug('%d calls to random by Random.choice(%s) from %s' % (
             self.count - oldCount, str([str(x) for x in fromList]), stack('')[-2]))
     return result
Пример #7
0
 def update_W_est(self,scale=1,normalize=False):
     zeros = (self.W_est_spread.dot(self.H_spread).dot(self.H_spread.T)==0)
     nonzero_indices = np.logical_not(zeros)
     mult_factor = scale*np.dot(self.M_spread,self.H_spread.T)/(
         self.W_est_spread.dot(self.H_spread).dot(self.H_spread.T))
     self.W_est_spread[nonzero_indices] = self.W_est_spread[
         nonzero_indices]*mult_factor[nonzero_indices]
     if normalize:
         self.W_est_spread = util.normalize(self.W_est_spread)
     self.W_est_stacked = util.stack(self.W_est_spread,self.T)
Пример #8
0
def convex_hull (list_of_points):
    list_of_points = [geometric.point (*args) for args in list_of_points]
    lowest_left_pt_index = geometric.lowest_left (list_of_points)

    print 'lowest_left_pt = %s' %list_of_points [lowest_left_pt_index]

    util.list_index_swap (0, lowest_left_pt_index, list_of_points)

    lowest_left_point = list_of_points [0]
    list_of_points = geometric.sort_by_slopes (list_of_points[1:], lowest_left_point)

    hull = util.stack ()
    hull.push (lowest_left_point)
    hull.push (list_of_points[0])

    list_of_points = list_of_points [1:]

    print 'Hull - ', hull
    print 'List of points - ', list_of_points

    ref_stack = util.stack ()
    ref_stack.push (lowest_left_point)

    while list_of_points:

        curr = hull.top ()
        pt = list_of_points.pop (0)
        ref = ref_stack.top ()
       
        while geometric.ref_slope_cmp (curr, ref, pt, curr):
            hull.pop ()
            ref_stack.pop ()

            ref = ref_stack.top ()
            curr = hull.top ()

        ref_stack.push (curr)
        hull.push (pt)
    
    print ''

    return hull
Пример #9
0
 def randrange(self, start, stop=None, step=1):
     # pylint: disable=arguments-differ
     oldCount = self.count
     result = self.__randrange(start, stop, step)
     if Debug.random:
         self.game.debug(
             '%d out of %d calls to random by '
             'Random.randrange(%d,%s) from %s'
             % (self.count - oldCount, self.count, start, stop,
                stack('')[-2]))
     return result
Пример #10
0
def parse(expr):
    ops = {'+': 1, '-': 1, '*': 2, '/': 2, '^': 3}
    pars = ['{', '[', '(', '}', ']', ')']
    operations, operands = stack(), stack()
    operand = ''

    for char in expr:
        if (char not in ops) and (char not in pars):
            operand += char
        if char in ops:
            if not (operand == ''):
                operands.push(node(operand))
            operand = ''
            if not operations.empty() and operations.peak() not in pars:
                if (ops[operations.peak()] >
                        ops[char]) or (ops[operations.peak()] == ops[char]):
                    operands.push(
                        make_operand(operands.pop(), operands.pop(),
                                     operations))
            operations.push(char)
        if char in pars[0:3]:
            operations.push(char)
        if char in pars[3:len(pars)]:
            if not (operand == ''):
                operands.push(node(operand))
            operand = ''
            while operations.peak() not in pars[0:3]:
                operands.push(
                    make_operand(operands.pop(), operands.pop(), operations))
            operations.pop()
    if len(operand) > 0:
        operands.push(node(operand))

    while not operations.empty():
        operands.push(make_operand(operands.pop(), operands.pop(), operations))

    return operands.pop(
    )  #Puu juure element on ainuke operandide magasinis olev element
Пример #11
0
def multiplicative_update_c(c_est, M, W_est, Theta, H, delays, scale=1):
    _, _, _, T = np.shape(Theta)
    S, N = np.shape(c_est)
    M = util.stack(M, T)
    # plt.figure(9)
    # plt.imshow(H)
    # print(np.shape(H),(N*T,T*S))
    H = util.stack(H, T)
    # print(np.shape(H),(S,N*T,T))
    # plt.figure(10)
    # plt.imshow(H[0,:,:])
    # plt.show()
    mult_factor = np.zeros_like(c_est)
    for s in range(S):
        for i in range(N):
            # print(delays[s,i])
            Theta_i_tis = Theta[i,
                                util.t_shift_to_index(delays[s, i], T), :, :]
            # print(np.sum(Theta_i_tis))
            # raw_input(' ')
            num = np.trace((M[s, :, :].T).dot(W_est).dot(Theta_i_tis))
            denom = np.trace(
                (H[s, :, :].T).dot(W_est.T).dot(W_est).dot(Theta_i_tis))
            # if denom==0:
            #     print(s,i)
            #     print('inf denom c update')
            #     plt.figure(55)
            #     plt.subplot(2,1,1)
            #     plt.imshow((H[s,:,:].T).dot(W_est.T).dot(W_est).dot(Theta_i_tis),
            #     interpolation='none')
            #     plt.subplot(2,1,2)
            #     plt.imshow((H[s,:,:]),interpolation='none')
            #     plt.show()
            #     raw_input(' ')
            mult_factor[s, i] = num / denom
    # print(mult_factor)
    c_est = c_est * scale * mult_factor
    return c_est
Пример #12
0
 def choice(self, fromList):
     """Choose a random element from a non-empty sequence."""
     if len(fromList) == 1:
         return fromList[0]
     oldCount = self.count
     idx = self.randrange(0, len(fromList)-1)
     result = fromList[idx]
     if Debug.random:
         self.game.debug(
             '%d out of %d calls to random by '
             'Random.choice(%s) from %s' % (
                 self.count - oldCount, self.count,
                 str([str(x) for x in fromList]),
                 stack('')[-2]))
     return result
Пример #13
0
 def focusTile(self, uiTile):
     """the uiTile of this board with focus. This is per Board!"""
     if uiTile is self._focusTile:
         return
     if uiTile:
         assert uiTile.tile.isKnown, uiTile
         if not isinstance(uiTile.board, DiscardBoard):
             assert uiTile.focusable, uiTile
         self.__prevPos = uiTile.sortKey()
     self._focusTile = uiTile
     if self._focusTile and self._focusTile.tile in Debug.focusable:
         logDebug('%s: new focus uiTile %s from %s' % (
             self.name, self._focusTile.tile if self._focusTile else 'None', stack('')[-1]))
     if self.hasFocus:
         self.scene().focusBoard = self
Пример #14
0
 def focusTile(self, uiTile):
     """the uiTile of this board with focus. This is per Board!"""
     if uiTile is self._focusTile:
         return
     if uiTile:
         assert uiTile.tile.isKnown, uiTile
         if not isinstance(uiTile.board, DiscardBoard):
             assert uiTile.focusable, uiTile
         self.__prevPos = uiTile.sortKey()
     self._focusTile = uiTile
     if self._focusTile and self._focusTile.tile in Debug.focusable:
         logDebug(u'%s: new focus uiTile %s from %s' % (
             self.name, self._focusTile.tile if self._focusTile else 'None', stack('')[-1]))
     if self.hasFocus:
         self.scene().focusBoard = self
Пример #15
0
 def focusTile(self, tile):
     """the tile of this board with focus. This is per Board!"""
     prevTile = self._focusTile
     if tile:
         assert tile.element != 'Xy', tile
         if not isinstance(tile.board, DiscardBoard):
             assert tile.focusable, tile
         self._focusTile = tile
     else:
         self._focusTile = self.autoSelectTile()
     if self._focusTile and self._focusTile.element in Debug.focusable:
         logDebug('new focus tile %s from %s' % (self._focusTile.element, stack('')[-1]))
     if (self._focusTile != prevTile
         and self.isHandBoard and self.player
         and not self.player.game.isScoringGame()
         and Internal.field.clientDialog):
         Internal.field.clientDialog.focusTileChanged()
     if self.hasFocus:
         self.scene().focusBoard = self
Пример #16
0
 def fset(self, tile):
     # pylint: disable=W0212
     prevTile = self._focusTile
     if tile:
         assert tile.element != 'Xy', tile
         if not isinstance(tile.board, DiscardBoard):
             assert tile.focusable, tile
         self._focusTile = tile
     else:
         self._focusTile = self.autoSelectTile()
     if self._focusTile and self._focusTile.element in Debug.focusable:
         logDebug('new focus tile %s from %s' % (self._focusTile.element, stack('')[-1]))
     if (self._focusTile != prevTile
         and self.isHandBoard and self.player
         and not self.player.game.isScoringGame()
         and InternalParameters.field.clientDialog):
         InternalParameters.field.clientDialog.focusTileChanged()
     if self.hasFocus:
         self.scene().focusBoard = self
Пример #17
0
def depthFirstSearch(problem):
    """
    Search the deepest nodes in the search tree first.

    Your search algorithm needs to return a list of actions that reaches the
    goal. Make sure to implement a graph search algorithm.

    To get started, you might want to try some of these simple commands to
    understand the search problem that is being passed in:

    print("Start:", problem.getStartState())
    print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
    print("Start's successors:", problem.getSuccessors(problem.getStartState()))
    """
    "*** YOUR CODE HERE ***"
    from util import Stack as stack
    stack = stack(
    )  # Depth First Search => Last in First out strategy using stack
    explored = []  # Memorize explored nodes
    stack.push(
        ([], problem.getStartState()))  # push the problem into stack class

    # pop from the stack until get the goal or explore all the nodes
    while not stack.isEmpty():
        path, location = stack.pop()
        explored.append(location)

        if problem.isGoalState(location):
            return path

        s = [
            x for x in problem.getSuccessors(location) if x[0] not in explored
        ]

        # update the path
        for x in s:
            fullpath = path + [x[1]]
            stack.push((fullpath, x[0]))

    print('Stack is empty')
    return []
Пример #18
0
def sort_bboxes(pred_bboxes):
    num_pred_boxes = np.sum([x.shape[0] for x in pred_bboxes])

    bboxes = util.stack([b for b in pred_bboxes if b.size > 0])
    if bboxes is None:
        return np.zeros((0, 5)), np.zeros((0, 1), dtype=int)
    image_ids = np.zeros((bboxes.shape[0], 1), dtype=int)

    # Concatenate them
    bbox_ind = 0
    for im_ind in xrange(len(pred_bboxes)):
        num_bbox_curr_image = pred_bboxes[im_ind].shape[0]
        image_ids[bbox_ind:bbox_ind + num_bbox_curr_image, 0] = im_ind
        bbox_ind = bbox_ind + num_bbox_curr_image

    # Sort
    IDX = np.argsort(-1 * bboxes[:, -1])
    bboxes = bboxes[IDX, :]
    image_ids = image_ids[IDX]

    return bboxes, image_ids
Пример #19
0
def solve(problem):
    state = problem.getInitialState()
    fringe = stack()
    fringe.push(state)
    visitedStates = set()
    while not fringe.isEmpty():
        #visitedStates.add("-".join(state.values()))
        #print "fringe size", fringe.size()
        state = fringe.pop()
        if problem.isGoal(state):
            print "A solution was found: "
            problem.prettyPrint(state)
            return True
        if str(state) not in visitedStates:
            visitedStates.add(str(state))
            successors = problem.getSuccessors(state)
            #print len(successors)
            for state in successors:
                fringe.push(state)

    print "There is no solution "
Пример #20
0
def getValidationAccuracy(model, scaler, X_pos_val, X_neg_val, evaluate=False, output_dir=MODELS_DIR):
    # Check accuracy on validation set
    print 'Validation set accuracy'
    print '-----------------------'
    with open(os.path.join(output_dir, 'results.txt'), 'a') as fp:
        print >> fp, '\n'
        print >> fp, 'Validation set accuracy'
        print >> fp, '-----------------------'
    # X_pos_val = util.normalizeAndAddBias(X_pos_val)
    # X_neg_val = util.normalizeAndAddBias(X_neg_val)
    X = util.stack([X_pos_val, X_neg_val])

    X, _ = util.normalizeAndAddBias(X, scaler)

    y = [np.ones((X_pos_val.shape[0], 1)), np.zeros((X_neg_val.shape[0], 1))]
    y = np.squeeze(np.vstack(tuple(y)))

    # print X.shape, y.shape
    
    # Classify negative features using small_svm
    y_hat = model.predict(X)
    pos_acc = 1.0 * np.sum(np.logical_and(y == y_hat, y==1)) / np.sum(y==1)
    neg_acc = 1.0 * np.sum(np.logical_and(y == y_hat, y==0)) / np.sum(y==0)
    tot_acc = 1.0 * np.sum(y == y_hat) / y.shape[0]
    print "Positive Accuracy:", pos_acc
    print "Negative Accuracy:", neg_acc
    print "Training Accuracy:", tot_acc
    print "-----------------------\n"
    with open(os.path.join(output_dir, 'results.txt'), 'a') as fp:
        print >> fp, "Positive Accuracy:", pos_acc
        print >> fp, "Negative Accuracy:", neg_acc
        print >> fp, "Training Accuracy:", tot_acc
        print >> fp, "-------------------------------------\n"

    if evaluate:
        return (pos_acc, neg_acc, tot_acc)
Пример #21
0
def trainBboxRegressionForClass(data,
                                class_id,
                                bbox_regression='normal',
                                debug=False):
    models = []
    X_train = []
    y_train = []

    num_images = len(data["train"]["gt"].keys())
    # Loop through all images and get features and bbox
    for i, image_name in enumerate(data["train"]["gt"].keys()):
        # if (i+1)%50 == 0:
        #     print 'Processing %d / %d'%(i+1, num_images)
        features_file_name = os.path.join(FEATURES_DIR, image_name + '.npy')
        if not os.path.isfile(features_file_name):
            print 'ERROR: Missing features file \'%s\'' % (features_file_name)
            sys.exit(1)

        if debug: print features_file_name
        features = np.load(features_file_name)

        if len(data["train"]["gt"][image_name][0]) == 0:
            # No Ground truth bboxes in image
            continue

        labels = np.array(data["train"]["gt"][image_name][0][0])
        gt_bboxes = np.array(data["train"]["gt"][image_name][1]).astype(
            np.int32)  # Otherwise uint8 by default
        IDX = np.where(labels == class_id)[0]

        # Find positive examples by computing overlap of all regions with GT bbox
        regions = data["train"]["ssearch"][image_name]
        overlaps = np.zeros((len(IDX), regions.shape[0]))

        if len(IDX) == 0:
            # No Ground truth bboxes for this class in image
            continue

        gt_cx, gt_cy, gt_H, gt_W = getBboxParameters(gt_bboxes[IDX])

        for j, gt_bbox in enumerate(gt_bboxes[IDX]):
            overlaps = util.computeOverlap(gt_bbox, regions)
            positive_idx = np.where(overlaps > BBOX_POSITIVE_THRESHOLD)[0]

            gt_features = features[IDX[j], :]
            proposals_features = features[
                positive_idx + len(data["train"]["gt"][image_name][0][0]), :]
            proposals_bboxes = regions[positive_idx, :]

            proposals_cx, proposals_cy, proposals_H, proposals_W = getBboxParameters(
                proposals_bboxes)

            # Compute the bbox parameters: (center_x, center_y, log(width), log(height))
            X_train.append(gt_features)
            y_train.append(np.array([0.0, 0.0, 0.0, 0.0]))

            X_train.append(proposals_features)
            targets = np.zeros((proposals_features.shape[0], 4))

            # print proposals_cx.shape
            targets[:, 0] = np.divide((gt_cx[j] - proposals_cx), proposals_cx)
            targets[:, 1] = np.divide((gt_cy[j] - proposals_cy), proposals_cy)
            targets[:, 2] = np.log(gt_W[j]) - np.log(proposals_W)
            targets[:, 3] = np.log(gt_H[j]) - np.log(proposals_H)
            y_train.append(targets)

    X_train = util.stack(X_train)
    y_train = util.stack(y_train)

    print 'X_train:', X_train.shape, 'Y_train', y_train.shape
    # Now train 4 different regressions, one for each bbox parameter (y_train)
    # So in total, for the three classes, we will have 12 regressions
    if bbox_regression == 'normal':
        for i in xrange(y_train.shape[1]):
            model = linear_model.Ridge(alpha=10000)
            model.fit(X_train, y_train[:, i])
            y_hat = model.predict(X_train)
            print 'Error (Model %d, Class %d): %0.2f' % (
                i + 1, class_id, np.mean(np.abs(y_hat - y_train[:, i])))
            models.append(model)
    else:
        model = linear_model.Ridge(alpha=10000)
        model.fit(X_train, y_train)
        y_hat = model.predict(X_train)
        l2norm = np.sum(np.abs(y_hat - y_train)**2, axis=-1)**(1. / 2)
        print 'Multivariate Error (Model %d, Class %d): %0.2f' % (
            i + 1, class_id, np.mean(l2norm))
        models.append(model)

    return models
Пример #22
0
# plt.show()

plt.text(0.65, 0.95, 'Estim. W', transform=plt.gcf().transFigure)

stacked_M = np.copy(M)
M = util.spread(M)  #reshape it so that it's D x T*S
W_est = util.spread(W_est)

counter = 1
# while abs(np.diff(R2))>R2_diff_threshold:
while abs(1. - R2) > unexp_var_threshold:
    print('--------------ITERATION: ' + str(counter) + '---------------')
    # print('R2 diff: '+str(abs(np.diff(R2)[0])))
    last = time.time()
    #Delay update
    delays = substeps.update_delay(stacked_M, util.stack(W_est, T), c_est,
                                   S)  #size of delays (t) is S x N

    Theta_i_tis = Theta[0, util.t_shift_to_index(delays[0, 0], T), :, :]

    SS_res = substeps.compute_squared_error(
        util.stack(W_est, T), c_est, np.zeros_like(c_est),
        stacked_M)  #*****change this to have actual shifts
    # R2[0] = R2[1]
    # R2[1] =  (1. - (SS_res/SS_tot))
    R2 = (1. - (SS_res / SS_tot))

    # print(SS_res,SS_tot)

    # print('R2 after delay update: '+str(R2[1]))
    print('R2 after delay update: ' + str(R2))
Пример #23
0
def draw_element(window,data,posx,posy):
    global ON_SCREEN
    c=g.Circle(g.Point(posx,posy),10)
    c.draw(window)
    ON_SCREEN.push(c)
    c = g.Text(g.Point(posx,posy),data)
    c.draw(window)
    ON_SCREEN.push(c)

#Graafilise ekraani puhastamine
def clear_screen(window):
    while not ON_SCREEN.empty():
        ON_SCREEN.pop().undraw()

#Magasin hoidmaks ekraanil olevaid graafilisi elemente
ON_SCREEN = stack()
def main():
    graphics_switch = input("Graafilised puud? (Y/n): ")
    if(graphics_switch == "Y" or graphics_switch == "y"):
        win = g.GraphWin("Avaldise puu",400,300)
        draw_tree = True
    else:
        draw_tree = False
    print("Sisesta tehe / q lõpetamiseks")
    #Peamine lõpmatu kordus
    while(True):
        expression = input(">> ")
        if(expression == "q" or expression == "Q"):
            break
        if ON_SCREEN.size > 0:
            clear_screen(win)
Пример #24
0
def trainSGDClassifierForClass(data, class_id, epochs=1, memory_size=1000, debug=False):
    # Split train into train and val set
    val_set_images = set(random.sample(data["train"]["gt"].keys(), 100))
    X_pos_val, X_neg_val = getTrainingFeatures(val_set_images, data["train"], class_id)
    X_pos_val = util.stack(X_pos_val)
    X_neg_val = util.stack(X_neg_val)

    train_set_images = list(set(data["train"]["gt"].keys()) - set(val_set_images))
    
    model = SGDClassifier(class_weight={1:200}, warm_start=True, alpha=10)

    curr_pos = []
    curr_neg = []
    num_images = len(train_set_images)
    start_time = time.time()
    for i, image_name in enumerate(train_set_images):
        # Load features from file for current image
        features_file_name = os.path.join(FEATURES_DIR, image_name + '.npy')
        if not os.path.isfile(features_file_name):
            print 'ERROR: Missing features file \'%s\''%(features_file_name) 
            sys.exit(1)
        features = np.load(features_file_name)

        num_gt_bboxes = data["train"]["gt"][image_name][0].shape[1]

        # Case 1: No GT boxes in image. Cannot compute overlap with regions.
        # Case 2: No GT boxes in image for current class
        # Case 3: GT boxes in image for current class
        if num_gt_bboxes == 0: # Case 1
            curr_neg.append(features)
        else:
            labels = np.array(data["train"]["gt"][image_name][0][0])
            gt_bboxes = np.array(data["train"]["gt"][image_name][1])
            IDX = np.where(labels == class_id)[0]

            if len(IDX) == 0: # Case 2
                curr_neg.append(features)
            else:
                regions = data["train"]["ssearch"][image_name]

                overlaps = np.zeros((regions.shape[0], len(IDX)))
                for j, gt_bbox in enumerate(gt_bboxes[IDX]):
                    overlaps[:,j] = util.computeOverlap(gt_bbox, regions)
                highest_overlaps = overlaps.max(axis=1)

                # Select Positive/Negatives Regions
                positive_idx = np.where(highest_overlaps > POSITIVE_THRESHOLD)[0]
                positive_idx += num_gt_bboxes
                curr_pos.append(features[IDX, :])
                curr_pos.append(features[positive_idx, :])
                
                # Only add negative examples where bbox is far from all GT boxes
                negative_idx = np.where(highest_overlaps < NEGATIVE_THRESHOLD)[0]
                negative_idx += num_gt_bboxes
                curr_neg.append(features[negative_idx, :])

            
            if len(curr_pos) == 0 or len(curr_neg) == 0:
                continue

            curr_pos = util.stack(curr_pos)
            curr_neg = util.stack(curr_neg)
            y_pos = np.ones((curr_pos.shape[0],1))
            y_neg = np.zeros((curr_neg.shape[0],1))

            # if debug: print curr_pos.shape
            # if debug: print curr_neg.shape

            # y_pos = np.ones((0,1))
            # y_neg = np.zeros((0,1))

            # if curr_pos is not None:
            #     y_pos = np.ones((curr_pos.shape[0],1))
            # else:
            #     curr_pos = np.zeros((0, 512))
            # if curr_neg is not None:
            #     y_neg = np.zeros((curr_neg.shape[0],1))

            X = util.stack([curr_pos, curr_neg])
            y = np.squeeze(util.stack([y_pos, y_neg]))
            model.fit(X,y)    

            curr_pos = []
            curr_neg = []

        if (i+1) % 50 == 0:
            print "Finished %i / %i.\tElapsed: %f" % (i+1, num_images, time.time()-start_time)
    
    getValidationAccuracy(model, 'NO_NORMALIZE', X_pos_val, X_neg_val)

    return model, 'NO_NORMALIZE'
Пример #25
0
 def update_M_est(self):  #********** this doesn't have t capacities yet
     self.M_est_spread = self.W_est_spread.dot(self.H_spread)
     self.M_est_stacked = util.stack(self.M_est_spread, 2 * self.T)
Пример #26
0
 def focusable(self, value):
     """redirect to self.graphics and generate Debug output"""
     if self.element in Debug.focusable:
         newStr = 'focusable' if value else 'unfocusable'
         logDebug('%s: %s from %s' % (newStr, self.element, stack('')[-2]))
     self.graphics.setFlag(QGraphicsItem.ItemIsFocusable, value)
Пример #27
0
 def focusable(self, value):
     """redirect to self.graphics and generate Debug output"""
     if self.element in Debug.focusable:
         newStr = 'focusable' if value else 'unfocusable'
         logDebug('%s: %s from %s' % (newStr, self.element, stack('')[-2]))
     self.graphics.setFlag(QGraphicsItem.ItemIsFocusable, value)
Пример #28
0
import dxfgrabber
import math
import imageMap
import util

# http://www.adammil.net/blog/v126_A_More_Efficient_Flood_Fill.html
# http://will.thimbleby.net/scanline-flood-fill/

def scanFill(image, x, y)
	if test(image[y][x]):
		return
	else:
		iamge[y][x] = true

	xLen, yLen = image.getArrayDimension()
	stack = util.stack()
	stack.push(lineRange(x, x+1, y, 0, True, True))

	while not stack.isEmpty():
		lineSeg = stack.pop()
		startX, endX = lineSeg.startX, lineSeg.endX
		yValue = lineSeg.y
		direction = lineSeg.direction
		if lineSeg.scanLeft:
			while startX > 0 and test(image[y][startX-1]):
				startX -= 1
				image[y][startX] = True
		if lineSeg.scanRight:
			while endX < xLen and test(image[y][endX]):
				image[y][endX] = True
				endX += 1
Пример #29
0
def det_eval(gt_bboxes, pred_bboxes):
    # det_eval
    # Arguments:
    # pred_bboxes: Cell array of predicted bounding boxes for a single class.
    #   Each element corresponds to a single image and is a matrix of dimensions
    #   n x 5, where n is the number of bounding boxes. Each bounding box is
    #   represented as [x1 y1 x2 y2 score], where 'score' is the detection score.
    # gt_bboxes: Cell array of ground truth bounding boxes in the same format as
    # pred_bboxes, without the score component.
    #
    # Returns:
    #   ap: average precision
    #   prec: The precision at each point on the PR curve
    #   rec: The recall at each point on the PR curve.
    minoverlap = 0.5
    assert (len(gt_bboxes) == len(pred_bboxes))
    num_images = len(gt_bboxes)

    num_gt_boxes = np.sum([x.shape[0] for x in gt_bboxes])
    num_pred_boxes = np.sum([x.shape[0] for x in pred_bboxes])
    [all_pred_bboxes, image_ids] = sort_bboxes(pred_bboxes)

    tp = np.zeros((num_pred_boxes, 1))
    fp = np.zeros((num_pred_boxes, 1))

    detected_pred_bboxes = [None] * num_images
    detected = [None] * num_images

    for pred_ind in xrange(num_pred_boxes):
        # if mod(pred_ind, 4096) == 0:
        #     fprintf(mstring('calculating detection box %d of %d\\n'), pred_ind, num_pred_boxes)
        # end
        num_gt_im_boxes = len(gt_bboxes[image_ids[pred_ind]])
        if detected[image_ids[pred_ind][0]] is None:
            detected[image_ids[pred_ind][0]] = np.zeros((num_gt_im_boxes, 1))

        bb = all_pred_bboxes[pred_ind, :]

        overlaps = np.zeros((num_gt_im_boxes))

        for g in xrange(num_gt_im_boxes):
            gt_bbox = gt_bboxes[image_ids[pred_ind]][g, :]
            overlaps[g] = util.computeOverlap(gt_bbox, np.array([bb]))

        if overlaps.size != 0:
            jmax = np.argmax(overlaps, axis=0)
            ovmax = overlaps[jmax]
        else:
            ovmax = float('-inf')

        # assign detection as true positive/don't care/false positive
        im_detected = detected[image_ids[pred_ind]]
        if ovmax >= minoverlap:
            if not im_detected[jmax]:
                tp[pred_ind] = 1  #true positive
                detected[image_ids[pred_ind]][jmax] = 1
                if detected_pred_bboxes[image_ids[pred_ind]] is None:
                    detected_pred_bboxes[image_ids[pred_ind]] = np.array([bb])
                else:
                    detected_pred_bboxes[image_ids[pred_ind]] = util.stack(
                        [detected_pred_bboxes[image_ids[pred_ind]], bb])
            else:
                fp[pred_ind] = 1  #false positive (multiple detections)
        else:
            fp[pred_ind] = 1  #false positive

    # compute precision/recall
    npos = num_gt_boxes
    fp = np.cumsum(fp)
    tp = np.cumsum(tp)

    rec = 1.0 * tp / npos
    prec = np.divide(tp, (fp + tp))
    ap = VOCap(rec, prec)

    return ap, prec, rec
Пример #30
0
def trainClassifierForClass(data, class_id, epochs=1, memory_size=30000, C=0.01, B=50, W={1:10}, output_dir=MODELS_DIR, evaluate=False, debug=False):
    X_pos = []
    X_neg = []
    X_pos_val = []
    X_neg_val = []

    # Split train into train and val set
    val_set_images = random.sample(data["train"]["gt"].keys(), 100)
    train_set_images = list(set(data["train"]["gt"].keys()) - set(val_set_images))
    # test_set_images = data["test"]["gt"].keys()

    X_pos, X_neg = getTrainingFeatures(train_set_images, data["train"], class_id)
    X_pos_val, X_neg_val = getTrainingFeatures(val_set_images, data["train"], class_id)
    # X_pos_test, X_neg_test = getTrainingFeatures(test_set_images, data["test"], class_id)

    model = None
    scaler = None
    if debug: print 'Stacking...'
    start_time = time.time()
    X_pos = util.stack(X_pos)
    X_neg = util.stack(X_neg)
    X_pos_val = util.stack(X_pos_val)
    X_neg_val = util.stack(X_neg_val)
    # X_pos_test = util.stack(X_pos_test)
    # X_neg_test = util.stack(X_neg_test)
    end_time = time.time()
    if debug: print 'Stacking took: %f'%(end_time - start_time)

    if debug: print X_pos.shape, X_neg.shape, X_pos_val.shape, X_neg_val.shape

    if debug: print 'Normalizing and adding bias to positive...'
    start_time = time.time()
    # X_pos = util.normalizeAndAddBias(X_pos)
    end_time = time.time()
    if debug: print 'Normalizing and adding bias to positive took: %f'%(end_time - start_time)

    num_positives = X_pos.shape[0]
    num_negatives = X_neg.shape[0]
    hard_negs = []
    curr_num_hard_negs = 0
    for epoch in xrange(epochs):
        X_neg_idx = np.random.permutation(num_negatives)

        start_idx = 0
        # increment = 2*num_positives
        increment = 30000
        while start_idx < num_negatives:
            if debug: print "[INFO] Negative traversal process: %d / %d"%(start_idx, num_negatives)
            end_idx = min(start_idx + increment, num_negatives)
            if model is None:
                if debug: print 'Picking Features'
                start_time = time.time()
                X_neg_subset = X_neg[X_neg_idx[start_idx:end_idx], :]
                end_time = time.time()
                if debug: print 'Picking took: %f'%(end_time - start_time)

                if debug: print 'Training SVM...'
                start_time = time.time()
                model, scaler = trainSVM(X_pos, X_neg_subset, C=C, B=B, W=W, output_dir=output_dir)
                end_time = time.time()
                if debug: print 'Training took: %f'%(end_time - start_time)

                hard_negs.append(X_neg_subset)
                num_iter_since_retrain = 0
            else:
                if debug: print 'Picking Features'
                start_time = time.time()
                X_neg_subset = X_neg[X_neg_idx[start_idx:end_idx], :]
                end_time = time.time()
                if debug: print 'Picking took: %f'%(end_time - start_time)

                if debug: print 'Normalizing and adding bias to negative subset...'
                start_time = time.time()
                X_neg_subset_1, _ = util.normalizeAndAddBias(X_neg_subset, scaler)
                end_time = time.time()
                if debug: print 'Normalizing and adding bias to negative subset took: %f'%(end_time - start_time)

                # Classify negative features using small_svm
                y_hat = model.predict(X_neg_subset_1)
                hard_idx = np.where(y_hat == 1)[0]
                hard_negs_subset = X_neg_subset[hard_idx, :]
                hard_negs.append(hard_negs_subset)
                
                curr_num_hard_negs += hard_negs_subset.shape[0]
                if debug: print 'Num Hard: %d / %d'%(hard_negs_subset.shape[0], X_neg_subset.shape[0])

                # Check if we need to retrain SVM
                if curr_num_hard_negs > 10000:
                    hard_negs = util.stack(hard_negs)

                    # Filter negs
                    X, _ = util.normalizeAndAddBias(hard_negs, scaler)
                    conf = model.decision_function(X)
                    y_hat = model.predict(X)
                    # y = model.predict(X)
                    # print conf[IDX[10000:10100]], y[IDX[10000:10100]]

                    # Discard 1/4th easy examples
                    # IDX = np.argsort(conf)
                    # IDX = IDX[IDX.shape[0]/4:]
                    
                    # Keep only max_num_neg negatives
                    IDX = np.argsort(-1*conf)
                    IDX = IDX[0:min(memory_size, IDX.shape[0])]
                    
                    # Keep only negatives with decision score > -1.0
                    # IDX = np.where(conf > -1.0)[0]
                    
                    # print 'Old num hard negs: %d'%(hard_negs.shape[0])
                    hard_negs = hard_negs[IDX,:]
                    # print 'New num hard negs: %d'%(hard_negs.shape[0])

                    if debug: print 'Retraining SVM (Skipped %d retrains) (Num hard: %d)...'%(num_iter_since_retrain, curr_num_hard_negs)
                    start_time = time.time()
                    model, scaler = trainSVM(X_pos, hard_negs, C=C, B=B, W=W, output_dir=output_dir)
                    end_time = time.time()
                    if debug: print 'Retraining took: %f'%(end_time - start_time)
                    # getValidationAccuracy(model, X_pos_val, X_neg_val)

                    # Keep only misclassified
                    # hard_idx = np.where(y_hat == 1)[0]
                    # hard_negs_subset = hard_negs[hard_idx, :]
                    
                    # Keep only max_num_neg negatives
                    # max_num_neg = 30000
                    # IDX = np.argsort(-1*conf)
                    # IDX = IDX[0:min(max_num_neg, IDX.shape[0])]
                    # hard_negs_subset = hard_negs[IDX,:]

                    hard_negs = [hard_negs]
                    curr_num_hard_negs = 0
                    num_iter_since_retrain = 0
                else:
                    num_iter_since_retrain += 1
                    # print 'No retrain required (Num hard: %d)'%(curr_num_hard_negs)

            start_idx += increment

    if debug: print 'Retraining SVM (Final)...'
    hard_negs = util.stack(hard_negs)
    X, _ = util.normalizeAndAddBias(hard_negs, scaler)
    conf = model.decision_function(X)
    
    # Keep only max_num_neg negatives
    IDX = np.argsort(-1*conf)
    IDX = IDX[0:min(memory_size, IDX.shape[0])]
    hard_negs = hard_negs[IDX,:]
    if evaluate:
        model, scaler, train_acc = trainSVM(X_pos, hard_negs, model=model,  C=C, B=B, W=W, output_dir=output_dir, evaluate=evaluate, debug=True)
        val_acc = getValidationAccuracy(model, scaler, X_pos_val, X_neg_val, evaluate=evaluate, output_dir=output_dir)
        return (model, scaler, train_acc, val_acc)
    else:
        model, scaler = trainSVM(X_pos, hard_negs, model=model,  C=C, B=B, W=W, output_dir=output_dir, evaluate=evaluate, debug=True)
        getValidationAccuracy(model, scaler, X_pos_val, X_neg_val, evaluate=evaluate, output_dir=output_dir)
        return (model, scaler)
Пример #31
0
 def shuffle(self, listValue, func=None, intType=int):
     oldCount = self.count
     Random.shuffle(self, listValue, func, intType)
     if Debug.random:
         self.game.debug('%d calls to random by Random.shuffle from %s' % (
             self.count - oldCount, stack('')[-2]))