示例#1
0
 def getInitCameraUp(self):
     #va = self.getFocalPoint() - self.getInitPosition()
     va = self.getInitPosition() - self.getFocalPoint() # w
     vb = self.getZAxis() # up
     
     w = Utils.normalize(va) # w 
     u = Utils.normalize(numpy.cross(vb, w))
     v = numpy.cross(w, u)
     
     return v
示例#2
0
    def visualize(self):
        # Using PCA (Principal Components Analysis) to recognize faces
        # Face recognizer just in openCV version 2.4x, not in version 3.0 (alpha + beta)
        # cv2.createEigenFaceRecognizer()
        [X, y] = read_images(self._facePath)
        # get k principal components
        [D, W, mu] = pca(as_row_matrix(X), y)
        # to check how many principal components (eigenfaces) - which has the same dimension with observed vector
        # print(len(W[1, :]))
        # print(len(W[:, 1]))

        # Visualize eigenfaces
        E = []
        # take at most 16 people
        for i in xrange(min(len(X), 16)):
            e = W[:, i].reshape(X[0].shape)
            # normalize value of eigenvectors in [0, 255] to display on image
            E.append(normalize(e, 0, 255))
        # display k principal components (k eigenvectors)
        # eigenfaces encode facial features + illumination of image
        subplot(title="Eigencefaces AT&T",
                images=E,
                rows=4,
                cols=4,
                sptitle="Eigenface",
                colormap=cm.gray,
                filename="pca_eigenface.png")

        # Reconstructor image from eigenfaces
        # array of vector number [10, 30, 50 ..., min(len(X), 320)]
        steps = [i for i in xrange(10, min(len(X), 320), 20)]
        E = []
        # take at most 16 eigenvectors of first person
        for i in xrange(min(len(steps), 16)):
            num_eigenvectors = steps[i]
            # project each vector x1, x2, ... xn to num_eigenvectors
            P = project(W[:, 0:num_eigenvectors], X[0].reshape(1, -1), mu)
            # reconstruct image from num_eigenvectors
            R = reconstruct(W[:, 0:num_eigenvectors], P, mu)
            R = R.reshape(X[0].shape)
            E.append(normalize(R, 0, 255))
        # reconstruct image from k (10, 30, ...) eigenvector
        subplot(title="Reconstruction AT&T",
                images=E,
                rows=4,
                cols=4,
                sptitle="EigenVectors",
                sptitles=steps,
                colormap=cm.gray,
                filename="pca_reconstruction.png")
示例#3
0
    def control(self, dt):
        self.crosshair_sprite.rotate(dt*45)

        if self.aim_angle != self.last_aim_angle:
            self.barrel_sprite.rotate(
                self.aim_angle - self.barrel_sprite.rotation + 90)
            self.changed_pos = True
            self.last_aim_angle = self.aim_angle

        if self.x != self.last_x:
            self.changed_pos = True
            self.new_y = -1
            for y in range(max(self.y - 5, 0), MAP_H):
                if not self.map.collision_map[y-1][int(self.x)] and \
                        self.map.collision_map[y][int(self.x)]:
                    self.new_y = y
                    break

            if self.new_y == -1 and self.final_x == -1:
                self.x = self.last_x
                return

            self.y = self.new_y

            p1 = (self.x - 4, self.map.get_height(normalize(int(self.x) - 4),
                                                  normalize(int(self.y - 5))))
            p2 = (self.x + 4, self.map.get_height(normalize(int(self.x) + 4),
                                                  normalize(int(self.y - 5))))

            angle = degrees(atan2(p2[1] - p1[1], p2[0] - p1[0]))
            self.sprite.rotate(angle - self.sprite.rotation)
        else:
            self.new_y = -1
            for y in range(max(self.y - 5, 0), MAP_H):
                if not self.map.collision_map[y-1][int(self.x)] and \
                        self.map.collision_map[y][int(self.x)]:
                    self.new_y = y
                    break

            if self.new_y != self.y:
                self.y = self.new_y
                p1 = (self.x - 4, self.map.get_height(int(self.x) - 4,
                                                      int(self.y - 5)))
                p2 = (self.x + 4, self.map.get_height(int(self.x) + 4,
                                                      int(self.y - 5)))

                angle = degrees(atan2(p2[1] - p1[1], p2[0] - p1[0]))
                self.sprite.rotate(angle - self.sprite.rotation)

        self.last_x = self.x
def attack_pgd(encoder, classifier, X, y, epsilon=params.epsilon, alpha=params.pgd_alpha,
               attack_iters=params.attack_iters, restarts=params.restarts,
               norm=params.norm, early_stop=params.early_stop, clamps=params.clamps):

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    epsilon /= 255.0
    alpha /= 255.0
    max_loss = torch.zeros(y.shape[0]).to(device)
    max_delta = torch.zeros_like(X).to(device)
    for _ in range(restarts):
        delta = torch.zeros_like(X).to(device)
        if norm == "l_inf":
            delta.uniform_(-epsilon, epsilon)
        elif norm == "l_2":
            delta.uniform_(-0.5,0.5).renorm(p=2, dim=1, maxnorm=epsilon)
        else:
            raise ValueError
        delta = clamp(delta, clamps[0]-X, clamps[1]-X)
        delta.requires_grad = True
        for _ in range(attack_iters):
            feats = encoder((normalize(X + delta)))
            output = classifier(feats)
            if early_stop:
                index = torch.where(output.max(1)[1] == y)[0]
            else:
                index = slice(None,None,None)
            if not isinstance(index, slice) and len(index) == 0:
                break
            loss = F.cross_entropy(output, y)
            loss.backward()
            grad = delta.grad.detach()
            d = delta[index, :, :, :]
            g = grad[index, :, :, :]
            x = X[index, :, :, :]
            if norm == "l_inf":
                d = torch.clamp(d + alpha * torch.sign(g), min=-epsilon, max=epsilon)
            elif norm == "l_2":
                g_norm = torch.norm(g.view(g.shape[0],-1),dim=1).view(-1,1,1,1)
                scaled_g = g/(g_norm + 1e-10)
                d = (d + scaled_g*alpha).view(d.size(0),-1).renorm(p=2,dim=0,maxnorm=epsilon).view_as(d)
            d = clamp(d, clamps[0] - x, clamps[1] - x)
            delta.data[index, :, :, :] = d
            delta.grad.zero_()

        all_loss = F.cross_entropy(classifier(encoder(normalize(X+delta))), y, reduction='none')
        max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss]
        max_loss = torch.max(max_loss, all_loss)

    return max_delta
示例#5
0
def extract_features(parsed_file, unified_file, cube_file, cube_context_file, freq_data_file, stop_word_file, freq_pattern_file, base_dir, total_docs, filtered_cell_str):
  print filtered_cell_str
  freq_patterns = load_freq_patterns(freq_pattern_file)
  freq_data = load_freq(freq_data_file)
  cubes = load_cube(cube_file)
  contexts = load_context(cube_context_file)
  unified_list = load_unified_list(unified_file)
  print contexts.keys()
  #cubes['all'] = [i for i in range(total_docs)]
  all_docs = [i for i in range(total_docs)]
  total_cnt = agg_phrase_cnt(freq_data, all_docs)

  print sum(total_cnt.values())
 
  #extract the features of phrases in each cube
  phrase_feature_all = {}
  idx = 0
  for att in cubes:
    if att != filtered_cell_str: #'Topic|Sports;Location|Illinois;':
      continue
    print "start processing " + att
    selected_doc = cubes[att]
    selected_context = contexts[att]
    #print selected_context
    feature_extractor = FeatureExtractor(parsed_file, selected_doc, selected_context, freq_data, stop_word_file, freq_patterns, total_cnt, unified_list)
    phrase_features = feature_extractor.extract_features()
    for phrase in phrase_features:
      norm_phrase = normalize(phrase)
      phrase_features[phrase].append(unified_list[norm_phrase])
      cell_phrase = "{0}{1}".format(att.replace('|', '_').replace(';', '_').replace(' ', '_').lower(), norm_phrase)
      phrase_feature_all[cell_phrase] = phrase_features[phrase]

  file_name = "{0}/{1}.fea".format(base_dir, "cells.fea")
  save_features(file_name, phrase_feature_all)
示例#6
0
 def randDirection(self):
     x = random.random()
     y = random.random()
     z = random.random()
     v = numpy.array([x, y, z], dtype=numpy.float32)
     v = Utils.normalize(v)
     return v
示例#7
0
 def get_item_train(self, filename):
     images, kspaces = from_train_file_to_image_and_kspace(filename)
     mask = gen_mask(kspaces[0], accel_factor=self.af)
     fourier_mask = np.repeat(mask.astype(np.float), kspaces[0].shape[0], axis=0)
     img_batch = list()
     zero_img_batch = list()
     if self.norm and self.mode == 'validation':
         means = list()
         stddevs = list()
     for kspace, image in zip(kspaces, images):
         zero_filled_rec = zero_filled(kspace * fourier_mask)
         if self.norm:
             zero_filled_rec, mean, std = normalize_instance(zero_filled_rec, eps=1e-11)
             image = normalize(image, mean, std, eps=1e-11)
             if self.mode == 'validation':
                 means.append(mean)
                 stddevs.append(std)
         zero_filled_rec = zero_filled_rec[:, :, None]
         zero_img_batch.append(zero_filled_rec)
         image = image[..., None]
         img_batch.append(image)
     zero_img_batch = np.array(zero_img_batch)
     img_batch = np.array(img_batch)
     if self.norm and self.mode == 'validation':
         return zero_img_batch, img_batch, means, stddevs
     else:
         return (zero_img_batch, img_batch)
示例#8
0
 def isOccluded(self, pt1, pt2):
     vd = pt2 - pt1
     tMax = Utils.norm2(vd)
     vd = Utils.normalize(vd)
     isIntersect, t = Utils.rayIntersectObjects(pt1, vd, self.facesList, self.pointsList, 0, tMax)
     
     return isIntersect
示例#9
0
 def get_item_train(self, filename):
     images, kspaces = from_train_file_to_image_and_kspace(filename)
     mask = gen_mask(kspaces[0], accel_factor=self.af)
     fourier_mask = np.repeat(mask.astype(np.float), kspaces[0].shape[0], axis=0)
     img_batch = list()
     z_kspace_batch = list()
     to_pad = type(self).pad - kspaces.shape[2]
     pad_seq = [(0, 0), (to_pad // 2, to_pad // 2)]
     if self.norm and self.mode == 'validation':
         means = list()
         stddevs = list()
     for kspace, image in zip(kspaces, images):
         zero_filled_rec = ifft(kspace * fourier_mask)
         if self.norm:
             zero_filled_rec, mean, std = normalize_instance(zero_filled_rec, eps=1e-11)
             image = normalize(image, mean, std, eps=1e-11)
             if self.mode == 'validation':
                 means.append(mean)
                 stddevs.append(std)
         zero_filled_rec = np.pad(zero_filled_rec, pad_seq, mode='constant')
         z_kspace = fft(zero_filled_rec)
         z_kspace = z_kspace[:, :, None]
         z_kspace_batch.append(z_kspace)
         image = np.pad(image, pad_seq, mode='constant')
         image = image[..., None]
         img_batch.append(image)
     z_kspace_batch = np.array(z_kspace_batch)
     img_batch = np.array(img_batch)
     if self.norm and self.mode == 'validation':
         return z_kspace_batch, img_batch, means, stddevs
     else:
         return (z_kspace_batch, img_batch)
示例#10
0
def get_summary_dict(batch, run, visualization_keys=None, *others):
    if visualization_keys is None:
        visualization_keys = ['reconstruction', 'L1']
    run = dict(filter(lambda x: x[1] is not None, run.items()))
    visuals = np.asarray([
        255 * np.hstack([
            normalize(batch[i]),
            *[normalize(run[key][i]) for key in visualization_keys],
            *[normalize(element[i]) for element in others]
        ]) for i in range(len(batch))
    ])
    scalars = dict(
        filter(
            lambda x: not (type(x[1]) == float and x[1] != x[1]) and x[1].ndim
            == 0, run.items()))
    return scalars, visuals
示例#11
0
    def Load_data(self):
        '''
        Loading data

        return: 
         - source_data: [samples, self.max_len(steps), input_size]
        '''

        print('==> Loading data...\n')
        source_data = {}
        print('Source data shape (samples, max_data_steps, input_size):')
        for activity_name, frames_list in self.source_path.items():
            frame_data = np.zeros(
                (len(frames_list), self.num_joint, self.coord_dim),
                dtype=np.float32)
            for i, frame in enumerate(frames_list):
                with open(frame) as json_file:
                    data = json.load(json_file)
                    frame_data[i] = utils.normalize(
                        np.array(data['people'][0]['pose_keypoints']), 'cnn')
            source_data[activity_name] = np.copy(frame_data)
            print('  sample {}: {}'.format(activity_name,
                                           source_data[activity_name].shape))

        return source_data
示例#12
0
 def detect(self, text_proposals, scores, size):
     """
     Detecting texts from an image
     :return: the bounding boxes of the detected texts
     """
     # 删除得分较低的proposal
     # text_proposals, scores=self.text_proposal_detector.detect(im, cfg.MEAN)
     keep_inds = np.where(scores > self.config.TEXT_PROPOSALS_MIN_SCORE)[0]
     text_proposals, scores = text_proposals[keep_inds], scores[keep_inds] # choose that score > 0.7
     # 按得分排序
     sorted_indices = np.argsort(scores.ravel())[::-1]
     text_proposals, scores = text_proposals[sorted_indices], scores[sorted_indices] # sort
     # 对proposal做nms
     # nms for text proposals
     keep_inds = nms(np.hstack((text_proposals, scores)), self.config.TEXT_PROPOSALS_NMS_THRESH)
     text_proposals, scores = text_proposals[keep_inds], scores[keep_inds] # nms
     # 获取检测结果
     scores = normalize(scores)
     text_lines = self.connector.get_text_lines(text_proposals, scores, size)
     # 过滤boxes
     keep_inds = self.filter_boxes(text_lines)
     text_lines = text_lines[keep_inds]
     # 对lines做nms
     if text_lines.shape[0] != 0:
         keep_inds = nms(text_lines, self.config.TEXT_LINE_NMS_THRESH)
         text_lines = text_lines[keep_inds]
     return text_lines
示例#13
0
 def get_height(self, x, sy=0):
     x = normalize(x)
     h = sy
     for y in range(sy, MAP_H):
         if self.collision_map[y][x]:
             return h
         h += 1
     return h
示例#14
0
    def control(self, dt):
        if self.final_x > self.x + 500:
            self.final_x -= MAP_W
        if self.x > self.final_x + 500:
            self.final_x += MAP_W

        if self.x != self.final_x:
            self.x = normalize(lerp(self.x, self.final_x, dt*7))
        super().control(dt)
示例#15
0
def predict(obj, newdata, **kwargs):
    df_treat = newdata.copy()
    df_control = newdata.copy()

    df_treat['treated'] = 1
    df_control['treated'] = 0

    global normalize_vars
    df_treat, _ = normalize(df_treat, normalize_vars)
    df_control, _ = normalize(df_control, normalize_vars)

    pred_treat = [val[0] for val in obj.predict(df_treat)]
    pred_control = [val[0] for val in obj.predict(df_control)]

    pred_df = pd.DataFrame({
        "pr_y1_t1": pred_treat,
        "pr_y1_t0": pred_control,
    })
    return pred_df
示例#16
0
 def feature_extractor(self, y, sr):
     mfccs = get_mfcc_v2(y, sr, delta=True, delta_delta=True)
     if mfccs.shape[0] < self.frames:
         return None
     features = np.zeros(shape=(mfccs.shape[0] - self.frames,
                                mfccs.shape[1] * self.frames))
     for i in range(mfccs.shape[0] - self.frames):
         features[i] = mfccs[i:i + self.frames].reshape(
             1, mfccs.shape[1] * self.frames)
     return normalize(features).reshape(-1, self.frames, mfccs.shape[1])
示例#17
0
def fit_ground_plane_from_obj_points(points_3d,
                                     ground_plane_default=GP_DEFAULT,
                                     log=False):
    """
    fit ground planes for all frames from object 3d points
    :param points_3d: 3d points from all frames <list>[n_frames]
    :param ground_plane_default: default ground plane, useful when no object points exists
    :param log: print log information to terminal
    :return: ground planes for all frames <list>[n_frames]
    """
    # TODO: calculate CAM_GND_POINT from given ground plane (instead of using default)
    # cam_gnd_default = calculate_cam_gnd(ground_plane_default)
    ground_planes = []
    for frame_id, points_3d_in_frame in enumerate(points_3d):
        # iterate for 3d points for different frames
        if points_3d_in_frame is not None:
            points_3d_in_frame = points_3d_in_frame.T
            # at least one 3d point in current frame
            n_points = points_3d_in_frame.shape[0]
            if n_points == 1:
                ground_plane = calculate_ground_plane_from_one_point(
                    points_3d_in_frame[0])
            elif n_points == 2:
                ground_plane = calculate_ground_plane_from_two_points(
                    points_3d_in_frame[0], points_3d_in_frame[1])
            else:
                ground_plane = calculate_ground_plane_from_mul_points(
                    points_3d_in_frame)
        else:
            # no 3d point in current frame
            if ground_plane_default is None:
                ground_plane = normalize(GP_DEFAULT)
            else:
                ground_plane = normalize(ground_plane_default)

        if log:
            print("Ground plane fitting for frame %s:" % frame_id,
                  ground_plane)

        ground_planes.append(ground_plane)
    ground_planes = np.array(ground_planes)

    return ground_planes
示例#18
0
    def _calc_trajectory(self, game, data):
        x = normalize(data['start_x'])
        y = data['start_y']
        dt = 1 / 240
        t = dt
        px, py = 0, 0
        while not game['map'].check_collision(int(px), int(py)):
            px = normalize(x +
                           cos(radians(data['angle'])) * data['velocity'] * t)
            py = y + sin(radians(data['angle'])) * data['velocity'] * t + \
                (GRAVITY*t*t)/2
            t += dt

        if game['map'].check_collision(int(px), int(py)):
            game['map'].explode(int(px), int(py), data['r'])

        data['x'] = px
        data['y'] = py
        return data, t
示例#19
0
def __load_embeddding(model, data_iter, args, **kwargs):
    stats = args['input_data_stats']
    for x in data_iter:
        #x[:,:,:6] = 0
        if 'ignore_name' in kwargs and kwargs['ignore_name']:
            x[:, :, -model.name_dim:] = 0
        x = utils.normalize(x, stats, args['normalization_method'])
        model.load_embedding(x, **kwargs)
        if args['random_embedding']:
            break  # this gives a random sample of the embedding with the data_iter size
示例#20
0
    def Load_data(self):
        '''
        Loading data

        return: 
         - source_data: [samples, self.max_len(steps), input_size]
         - target_data: [samples, decoder_steps, self.max_len]
        '''

        print('==> Loading data...\n')
        source_data = {}
        print('Source data shape (samples, max_data_steps, input_size):')
        for activity_name, frames_list in self.source_path.items():
            if self.model == 'cnn':
                frame_data = np.zeros(
                    (len(frames_list), self.num_joint, self.coord_dim),
                    dtype=np.float32)  # cnn
            else:
                frame_data = np.zeros((len(frames_list), self.input_size),
                                      dtype=np.float32)  # rnn
            for i, frame in enumerate(frames_list):
                with open(frame) as json_file:
                    data = json.load(json_file)
                    frame_data[i] = utils.normalize(
                        np.array(data['people'][0]['pose_keypoints']),
                        self.model)
            source_data[activity_name] = np.copy(frame_data)
            print('  sample {}: {}'.format(activity_name,
                                           source_data[activity_name].shape))

        target_data = {}
        print('Target data shape (samples, decoder_steps, max_data_steps):')
        for activity_name, label_list in self.target_path.items():
            loaded = np.load(label_list)
            lf = np.zeros((self.decoder_steps, loaded.shape[0]),
                          dtype=np.float32)
            # ns = [2, 1, 0.25] # less accurate -> more accurate
            # ns = [2, 1, 0.5] # less accurate -> more accurate
            ns = [2, 2, 2]  # less accurate -> more accurate
            assert len(
                ns
            ) == self.decoder_steps, 'length of \'ns\' != \'decoder_steps\'.'
            for s in range(self.decoder_steps):
                if self.model == 'cnn':
                    lf[s] = utils.gaussian_like_weighted(loaded)  # cnn
                else:
                    lf[s] = utils.gaussian_like_weighted(loaded)  # cnn
                    # lf[s] = utils.gaussian_weighted(loaded, ns[s]) # rnn
            target_data[activity_name] = np.copy(lf)
            print('  sample {}: {}'.format(activity_name,
                                           target_data[activity_name].shape))
        print('\n')

        return source_data, target_data
示例#21
0
def parse_posiciones(tabla, posiciones=None):
    posiciones = int(posiciones[0]) if posiciones else 10
    LIMIT = 4
    # ['#', 'Equipo', 'Pts', 'PJ']
    headers = [th.text for th in tabla.thead.find_all('th')[:LIMIT]]
    a = []
    for row in tabla.tbody.find_all('tr')[:posiciones]:
        b = [normalize(r.text) for r in row.find_all('td')[:LIMIT]]
        a.append(b)
    a.insert(0, headers)
    return a
示例#22
0
    def _show_trajectory(self):
        if sf.Keyboard.is_key_pressed(sf.Keyboard.M) and \
                self.hwnd.has_focus():
            self.cheat_active = not self.cheat_active
            sleep(0.5)

        if self.cheat_active:
            x, y = self._calc_trajectory(self.tanks[self.tank_id].x,
                                         self.tanks[self.tank_id].y - 11, 60,
                                         True)
            x = normalize(x + self.map_scroll)
            rect = create_rect(x - 2, y - 2, 4, 4, sf.Color.CYAN)
            self.hwnd.draw(rect)
示例#23
0
    def visualize(self):
        # Using PCA (Principal Components Analysis) to recognize faces
        # Face recognizer just in openCV version 2.4x, not in version 3.0 (alpha + beta)
        # cv2.createEigenFaceRecognizer()
        [X, y] = read_images(self._facePath)
        # get k principal components
        [D, W, mu] = pca(as_row_matrix(X), y)
        # to check how many principal components (eigenfaces) - which has the same dimension with observed vector
        # print(len(W[1, :]))
        # print(len(W[:, 1]))

        # Visualize eigenfaces
        E = []
        # take at most 16 people
        for i in xrange(min(len(X), 16)):
            e = W[:, i].reshape(X[0].shape)
            # normalize value of eigenvectors in [0, 255] to display on image
            E.append(normalize(e, 0, 255))
        # display k principal components (k eigenvectors)
        # eigenfaces encode facial features + illumination of image
        subplot(title="Eigencefaces AT&T", images=E, rows=4, cols=4, sptitle="Eigenface", colormap=cm.gray,
                filename="pca_eigenface.png")

        # Reconstructor image from eigenfaces
        # array of vector number [10, 30, 50 ..., min(len(X), 320)]
        steps = [i for i in xrange(10, min(len(X), 320), 20)]
        E = []
        # take at most 16 eigenvectors of first person
        for i in xrange(min(len(steps), 16)):
            num_eigenvectors = steps[i]
            # project each vector x1, x2, ... xn to num_eigenvectors
            P = project(W[:, 0:num_eigenvectors], X[0].reshape(1, -1), mu)
            # reconstruct image from num_eigenvectors
            R = reconstruct(W[:, 0:num_eigenvectors], P, mu)
            R = R.reshape(X[0].shape)
            E.append(normalize(R, 0, 255))
        # reconstruct image from k (10, 30, ...) eigenvector
        subplot(title="Reconstruction AT&T", images=E, rows=4, cols=4, sptitle="EigenVectors", sptitles=steps,
                colormap=cm.gray, filename="pca_reconstruction.png")
示例#24
0
def get_minimal_movie(movie, trim_description=True):
    """Return image, title, synopsis, and Torrents from a movie."""
    title = movie['title_long']
    imdb = movie['imdb_code']
    yt_trailer = movie['yt_trailer_code']
    if trim_description:
        synopsis = normalize(movie['synopsis'], limit=150, trim_end='..')
    else:
        synopsis = movie['synopsis']

    rating = movie['rating']
    image = movie['large_cover_image']
    return title, synopsis, rating, imdb, yt_trailer, image
示例#25
0
    def _calc_trajectory(self, x, y, fps=60, draw=False):
        rect = create_rect(0, 0, 2, 2, sf.Color(0, 0, 0, 50))

        x = normalize(x)
        y = y
        px, py = 0, 0
        dt = 1 / fps
        t = dt
        angle = self.tanks[self.tank_id].aim_angle
        w = self.ui.get_widget('power_slider')
        if w is None:
            return
        velocity = w.get_value() * 540
        while not self.map.check_collision(int(px), int(py)):
            px = normalize(x + cos(radians(angle)) * velocity * t)
            py = y + sin(radians(angle)) * velocity * t + \
                0.5*(GRAVITY*t*t)

            t += dt
            if draw:
                rect.position = (normalize(px + self.map_scroll) - 1, py - 1)
                self.hwnd.draw(rect)
        return (px, py)
示例#26
0
def predict_img(net, img, gpu):

    img = img[:, :, :3]  # alphachannelを削除
    img = utils.normalize(img)
    img = np.transpose(img, axes=[2, 0, 1])
    X = torch.FloatTensor(img).unsqueeze(0)
    X = Variable(X, volatile=True)
    if gpu:
        X = X.cuda()

    y_hat = F.sigmoid(net(X))
    y_hat = np.asarray((y_hat > 0.5).data)
    y_hat = y_hat.reshape((y_hat.shape[2], y_hat.shape[3]))
    return y_hat
示例#27
0
def eval_tgt_robust(encoder, classifier, critic, data_loader):
    """Evaluate model for target domain with attack on labels and domains"""
    # Set eval state for Dropout and BN layers
    encoder.eval()
    classifier.eval()
    critic.eval()

    # Init loss and accuracy
    loss, acc = 0, 0
    test_robust_loss, test_robust_acc = 0, 0

    # Set loss function
    criterion = nn.CrossEntropyLoss()

    # Evaluate network
    for (images, labels) in data_loader:
        images = make_variable(images)
        labels = make_variable(labels)
        domain_tgt = make_variable(torch.ones(images.size(0)).long())

        delta_src = attack_pgd(encoder, classifier, images, labels)
        delta_domain = attack_pgd(encoder, critic, images, domain_tgt)
        delta_src = delta_src.detach()
        delta_domain = delta_domain.detach()

        # Compute loss
        robust_images = normalize(
            torch.clamp(images + delta_src[:images.size(0)] +
                        delta_domain[:images.size(0)],
                        min=params.lower_limit,
                        max=params.upper_limit))
        robust_preds = classifier(encoder(robust_images))
        test_robust_loss += criterion(robust_preds, labels).item()

        out = classifier(encoder(images))
        loss += criterion(out, labels).item()

        test_robust_acc += torch.sum(
            robust_preds.max(1)[1] == labels.data).double()
        acc += torch.sum(out.max(1)[1] == labels.data).double()

    loss /= len(data_loader)
    test_robust_loss /= len(data_loader)
    acc = acc / len(data_loader.dataset)
    test_robust_acc = test_robust_acc / len(data_loader.dataset)

    print(
        "Avg Evaluation Loss: {:.4f}, Avg Evaluation Accuracy: {:.4%}, Ave Evaluation Robust Loss: {:.4f}, "
        "Ave Robust Accuracy: {:.4%}".format(loss, acc, test_robust_loss,
                                             test_robust_acc))
示例#28
0
    def move(self, dt):
        if self.move_limit <= 0:
            return

        move_amount = dt*Tank.VELOCITY

        if sf.Keyboard.is_key_pressed(sf.Keyboard.A):
            self.x -= move_amount
            self.move_limit -= abs(move_amount)

        if sf.Keyboard.is_key_pressed(sf.Keyboard.D):
            self.x += move_amount
            self.move_limit -= abs(move_amount)

        self.x = normalize(self.x)
示例#29
0
def pretty_print_dolar(cotizaciones, limit=7):
    """Returns dolar rates separated by newlines and with code markdown syntax.
    ```
    Banco Nacion  | $30.00 | $40.00
    Banco Galicia | $30.00 | $40.00
                   ...
    ```
    """
    return monospace(
        '\n'.join(
            "{:8} | {:7} | {:7}".format(
                normalize(banco, limit), valor['compra'], valor['venta']
            )
            for banco, valor in cotizaciones.items()
        )
    )
示例#30
0
 def __init__(self, map, x, y, angle, vel, final_pos=None):
     self.map = map
     self.texture = texture_manager.textures[TEXTURE.MISSLE]
     self.sprite = sf.Sprite(self.texture)
     self.sprite.origin = (2, 2)
     self.enemy_missle = True if final_pos is not None else False
     self.final_pos = final_pos
     self.start_x = normalize(x)
     self.start_y = y
     self.x = self.start_x
     self.y = y
     self.angle = radians(angle)
     self.vel = vel
     self.clock = None
     self.should_remove = False
     self.r = 24
     self.t = 0
示例#31
0
def calculate_ground_plane_from_mul_points(points_3d,
                                           cam_gnd=CAM_GND_DEFAULT,
                                           use_cam_gnd=True):

    if use_cam_gnd:
        points_3d = np.vstack((points_3d, cam_gnd))

    n_points = points_3d.shape[0]
    params, residuals, _, _ = np.linalg.lstsq(points_3d,
                                              -np.ones((n_points, 1)),
                                              rcond=-1)
    params = np.squeeze(params)
    params = np.append(params, 1.0)
    params = normalize(params)
    # print(params, residuals)
    # TODO: how to use residuals?

    return params
示例#32
0
    def control(self, dt):
        if self.should_remove:
            return

        if self.map.check_collision(int(self.x), int(self.y)):
            if self.final_pos is not None:
                self.x = self.final_pos[0]
                self.y = self.final_pos[1]
                self.final_pos = None
            self.map.explode(int(self.x), int(self.y), self.r)
            self.should_remove = True
            return

        self.t += dt
        self.x = normalize(self.start_x + cos(self.angle) * self.vel * self.t)
        self.y = self.start_y + sin(self.angle) * self.vel * self.t + \
            0.5*(GRAVITY*self.t*self.t)
        self.sprite.rotate(dt*180)
示例#33
0
def main():
    data = datasets.load_digits()
    X = normalize(data.data)
    y = data.target

    # data preprocess: One-hot encoding of nominal y-values
    y = to_categorical(y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

    clf = Perceptron(n_iterations=5000, learning_rate=0.001, loss=CrossEntropy, activation_function=Sigmoid)
    clf.fit(X_train, y_train)

    y_pred = np.argmax(clf.predict(X_test), axis=1)
    y_test = np.argmax(y_test, axis=1)

    accuracy = accuracy_score(y_test, y_pred)
    print(f"Accuracy: {accuracy}")

    Plot().plot_in_2d(X_test, y_pred, title="perceptron", accuracy=accuracy, legend_labels=np.unique(y))
示例#34
0
文件: mcx.py 项目: fangbo-tao/TextExp
def pre_processing(pure_freq_file, unified_file, forward_file):
	phrase_dict = {}
	forward_list = []

	with open(pure_freq_file, 'r') as f:
		for line in f:
			elems = line.strip(' \n').split(':')
			# print line
			tokens = elems[1].split(',')
			phrases_in_doc = []
			for token in tokens:
				phrase = normalize(token.split('|')[0])
				# only keep n-grams
				if ' ' not in phrase:
					continue
				phrase_dict[phrase] = 1 if phrase not in phrase_dict else phrase_dict[phrase] + 1
				phrases_in_doc.append(phrase)

			forward_list.append(phrases_in_doc)

	# sort the forward list by frequency of phrase
	forward_list_true = []
	idx = 0
	for phrases_in_doc in forward_list:
		idx += 1
		print idx
		temp_list = {}
		for phrase in phrases_in_doc:
			occur = phrase_dict[phrase]
			temp_list[phrase] = occur
		
		ranked_list = sorted(temp_list, key=temp_list.get, reverse=False)
		forward_list_true.append(ranked_list)


	with open(unified_file, 'w+') as f:
		for phrase in phrase_dict:
			f.write(phrase + '\t' + str(phrase_dict[phrase]) + '\n')

	with open(forward_file, 'w+') as f:
		for phrases_in_doc in forward_list_true:
			f.write(','.join(phrases_in_doc) + '\n')
示例#35
0
    def draw(self, hwnd, map_scroll):
        x = normalize(self.x + map_scroll)
        positions = get_border_positions(x, 50)

        for x in positions:
            self.bar_green.position = (x-5, self.y-22)
            self.bar_red.position = (x-5+self.bar_green_len, self.y-22)
            self.barrel_sprite.position = (x, self.y-10)
            self.sprite.position = (x, self.y-8)
            self.crosshair_sprite.position = (
                x + 35*cos(radians(self.aim_angle)),
                self.y - 12 + 35*sin(radians(self.aim_angle)))
            hwnd.draw(self.barrel_sprite)
            hwnd.draw(self.sprite)
            hwnd.draw(self.bar_green)
            hwnd.draw(self.bar_red)

            self.rect = create_rect(
                x-1, self.y-1, 2, 2, sf.Color(255, 255, 255))
            hwnd.draw(self.rect)
示例#36
0
    def get_firm_choice(self, firms_idx, prices):
        """choice function"""

        consume = len(prices) > 0

        if consume:
            price = min(prices)  # Choose minimum price
            self.firm_choice = np.random.choice(firms_idx[prices == price])

        else:
            price = 0
            self.firm_choice = -1

        exploration_cost = self.t_cost * self.extra_view
        self.utility = int(consume) * self.u_consumption - (exploration_cost + price)

        self.network_target = normalize(self.utility, min_=self.utility_min, max_=self.utility_max)

        self._learn()

        return self.firm_choice
示例#37
0
def fit(x, y, t, **kwargs):
    lr = kwargs.get('lr')
    epochs = kwargs.get('epochs')
    batch_size = kwargs.get('batch_size')
    decay = kwargs.get('decay')
    method = kwargs.get('method')
    activation = 'sigmoid' if method == 'logistic' else 'linear'

    init_tf(1234)

    df = x.copy()
    df['treated'] = t

    global normalize_vars
    df, normalize_vars = normalize(df)

    model = build_model(df.shape[1], 1, activation)
    model = set_optimizer(model, lr, activation, decay)
    model.fit(df, y, epochs=epochs, batch_size=batch_size, validation_split=0.2, verbose=2)

    return model
示例#38
0
 def predict(self, X):
     X = normalize(polynomial_features(X, degree=self.degree))
     super(ElasticNet, self).predict(X)
示例#39
0
    def readBBox(self, scene):
        '''
        ReadBBox(self, scene): -> bbox, origin, vu, vv, vw, depth, width, height
        '''
        (bboxName, topMatName, leftMatName, frontMatName) = self.getFixBBoxParams()
        
        # read the bbox from scene
        bbox = MXS.getObject(scene, bboxName)
        if bbox.isNull():
            raise ("cannot find the object named {}".format(bboxName))
        
        # hide the bbox
        MXS.setObjectInvisible(bbox)
     
   
        # read the top, left and right planes
        topTriIds = MXS.getObjectTrianglesByMat(bbox, topMatName)
        leftTriIds = MXS.getObjectTrianglesByMat(bbox, leftMatName)
        frontTriIds = MXS.getObjectTrianglesByMat(bbox, frontMatName)
 

        if len(topTriIds) ==0 or len(leftTriIds) == 0 or len(frontTriIds) == 0:
            raise ("cannot correct find min_box")
        
        # read the bbox geometry information
        points      = MXS.getObjectVertices(bbox)
        triangles   = MXS.getObjectTriangles(bbox)
        
        # make points set of three planes
        topPtsSet = set()
        for topTriId in topTriIds:
            vs = triangles[topTriId]
            topPtsSet.add(vs[0])
            topPtsSet.add(vs[1])
            topPtsSet.add(vs[2])
        
        leftPtsSet = set()
        for leftTriId in leftTriIds:
            vs = triangles[leftTriId]
            leftPtsSet.add(vs[0])
            leftPtsSet.add(vs[1])
            leftPtsSet.add(vs[2])
        
        frontPtsSet = set()
        for frontTriId in frontTriIds:
            vs = triangles[frontTriId]
            frontPtsSet.add(vs[0])
            frontPtsSet.add(vs[1])
            frontPtsSet.add(vs[2])
            
        # find origin, u, v, w
        oPtId = topPtsSet & leftPtsSet & frontPtsSet
        uPtId = (topPtsSet & leftPtsSet ) - oPtId
        vPtId = (topPtsSet & frontPtsSet) - oPtId
        wPtId = (leftPtsSet& frontPtsSet) - oPtId
        
        oPtId = oPtId.pop()
        uPtId = uPtId.pop()
        vPtId = vPtId.pop()
        wPtId = wPtId.pop()
        
        # normalize the vector
        origin = points[oPtId]
        vu = points[uPtId] - origin
        vv = points[vPtId] - origin
        vw = points[wPtId] - origin
        
        depth = Utils.norm2(vu)
        width = Utils.norm2(vv)
        height = Utils.norm2(vw)
        
        vu = Utils.normalize(vu)
        vv = Utils.normalize(vv) 
        vw = Utils.normalize(vw)
        print("box depth: "+str(depth))
        print("box width: "+str(width))
        print("box height: "+str(height))
        return (bbox, points, triangles, origin, vu, vv, vw, depth, width, height)
示例#40
0
 def fit(self, X, y):
     X = normalize(polynomial_features(X, degree=self.degree))
     super(ElasticNet, self).fit(X, y)
示例#41
0
 def predict(self, X):
     X = normalize(polynomial_features(X, degree=self.degree))
     return super(LassoRegression, self).predict(X)
示例#42
0
 def fit(self, X, y):
     X = normalize(polynomial_features(X, degree=self.degree))
     super(PolynomialRidgeRegression, self).fit(X, y)
示例#43
0
	def compute(self, score_type='ALL'):
		'''
		-- score_type --
			ALL: all three factors
			POP: only popularity
			DIS: only distinctive
			INT: only integrity
			NOPOP: no populairty
			NODIS: no distinctive
			NOINT: no integrity

		'''
		scores = {}
		multiplier = 1

		
		sum_self = self.sum_cnt
		num_context_cells = len(self.sum_cnt_context) + 1
		total_sum = sum(self.sum_cnt_context.values()) + sum_self
		avgdl = total_sum / float(num_context_cells)

		# method 1
		for phrase in self.phrase_cnt:
			lower_phrase = phrase.lower()
			score = 1
			nor_phrase = normalize(lower_phrase)
			self_cnt = self.phrase_cnt[phrase]
			self_df = self.phrase_df[phrase]
			
			group = [(self_df, self.max_df, self_cnt, sum_self)]

			self.context_groups[phrase] = []
			for phrase_group, phrase_values in self.phrase_cnt_context.items():
				context_df = self.phrase_df_context[phrase_group].get(phrase, 0)
				sum_context = self.sum_cnt_context[phrase_group]
				context_cnt = phrase_values.get(phrase, 0)
				maxdf_context = self.max_df_context[phrase_group]

				if (context_cnt > 0):
					group.append((context_df, maxdf_context, context_cnt, sum_context))
					self.context_groups[phrase].append((context_df, maxdf_context, context_cnt, sum_context))
				
			score_list = []
			for record in group:
				score_list.append(self.bm25_df_paper(record[0], record[1], record[2], record[3], avgdl))
			distinct = self.softmax_paper(score_list)[0]
			
			# score_list = map(prettyfloat, score_list)
			# log_str = lower_phrase.encode('utf-8') + ':' + str(prettyfloat(score)) + '\t' +  str(group) +  '\t' + str(score_list) + '\n'
			# log_strs[lower_phrase] = log_str
			# log_scores[lower_phrase] = score
			# distinct = score
			popularity = math.log(1 + self_df, 2)
			try:
				integrity = float(self.global_scores[nor_phrase])
			except:
				integrity = 0.8

			if score_type == 'ALL':
				score = distinct * popularity * integrity
			elif score_type == 'POP':
				score = popularity
			elif score_type == 'DIS':
				score = distinct
			elif score_type == 'INT':
				score = integrity
			elif score_type == 'NOPOP':
				score = distinct * integrity
			elif score_type == 'NODIS':
				score = popularity * integrity
			elif score_type == 'NOINT':
				score = popularity * distinct
			else:
				score = 0

			scores[phrase] = score

		ranked_list = [(phrase, scores[phrase]) for phrase in sorted(scores, key=scores.get, reverse=True)]
		
		print 'OLAPORP DONE'
		return ranked_list
示例#44
0
	def update_computing(self, score_type='ALL'):
		scores = {}
		multiplier = 1

		sum_self = self.sum_cnt
		num_context_cells = len(self.sum_cnt_context) + 1
		total_sum = sum(self.sum_cnt_context.values()) + sum_self
		avgdl = total_sum / float(num_context_cells)

		# method 1
		for phrase in self.phrase_cnt:
			lower_phrase = phrase.lower()
			score = 1
			nor_phrase = normalize(lower_phrase)
			self_cnt = self.phrase_cnt[phrase]
			self_df = self.phrase_df[phrase]
			

			group = list(self.context_groups[phrase])

			group.append((self_df, self.max_df, self_cnt, sum_self))

			score_list = []
			for record in group:
				score_list.append(self.bm25_df_paper(record[0], record[1], record[2], record[3], avgdl))
			distinct = self.softmax_paper(score_list)[-1]
			
			
			# score_list = map(prettyfloat, score_list)
			# log_str = lower_phrase.encode('utf-8') + ':' + str(prettyfloat(score)) + '\t' +  str(group) +  '\t' + str(score_list) + '\n'
			# log_strs[lower_phrase] = log_str
			# log_scores[lower_phrase] = score
			# distinct = score
			popularity = math.log(1 + self_df, 2)
			try:
				integrity = float(self.global_scores[nor_phrase])
			except:
				integrity = 0.8

			if score_type == 'ALL':
				score = distinct * popularity * integrity
			elif score_type == 'POP':
				score = popularity
			elif score_type == 'DIS':
				score = distinct
			elif score_type == 'INT':
				score = integrity
			elif score_type == 'NOPOP':
				score = distinct * integrity
			elif score_type == 'NODIS':
				score = popularity * integrity
			elif score_type == 'NOINT':
				score = popularity * distinct
			else:
				score = 0

			scores[phrase] = score

		ranked_list = [(phrase, scores[phrase]) for phrase in sorted(scores, key=scores.get, reverse=True)]
		self.ranked_list = ranked_list
		
		print 'OLAPORP DONE'
		return ranked_list
示例#45
0
 def predict(self, X):
     X = normalize(polynomial_features(X, degree=self.degree))
     super(PolynomialRidgeRegression, self).predict(X)