Beispiel #1
0
    def go_to_player(self, dif_x, dif_y):
        """
            determine where do we have to go to follow the player
        """
        if is_positive(dif_x):
            self.rect.x += self.speed // 2  #go right
        else:
            self.rect.x -= self.speed // 2  #go left

        if is_positive(dif_y):
            self.rect.y += self.speed // 2  #go down
        else:
            self.rect.y -= self.speed // 2  #go up
Beispiel #2
0
def get_feats_from_cnn(rows, model=None):
    """
    fprop rows using best trained model and returns activations of the
    penultimate layer
    """
    conf = utils.get_config()
    patch_size = conf['patch_size']
    region_size = conf['region_size']
    batch_size = None
    preds = utils.get_predictor(model=model, return_all=True)
    y = np.zeros(len(rows))
    samples = np.zeros(
        (len(rows), region_size, region_size, 1), dtype=np.float32)
    for i, row in enumerate(rows):
        print 'processing %i-th image: %s' % (i, row['image_filename'])
        try:
            samples[i] = utils.get_samples_from_image(row, False)[0]
        except ValueError as e:
            print '{1} Value error: {0}'.format(str(e), row['image_filename'])
        y[i] = utils.is_positive(row)
    ds = DenseDesignMatrix(topo_view=samples)
    pipeline = utils.get_pipeline(
        ds.X_topo_space.shape, patch_size, batch_size)
    pipeline.apply(ds)
    return preds[-2](ds.get_topological_view()), y
Beispiel #3
0
def get_feats_from_imagenet_in_partitions():
    conf = utils.get_config()
    imagenet_data = os.path.join(
        conf['models_path'], 'decafnet', 'imagenet.decafnet.epoch90')
    imagenet_meta = os.path.join(
        conf['models_path'], 'decafnet', 'imagenet.decafnet.meta')
    net = DecafNet(imagenet_data, imagenet_meta)
    rows = utils.get_filtered_rows()
    sets = utils.split_dataset(
        rows, conf['valid_percent'], conf['test_percent'], rng=conf['rng_seed'])
    feats = []
    ys = []
    for s in sets:
        X = np.zeros((len(s), 4096))
        y = np.zeros(len(s))
        for i, row in enumerate(s):
            try:
                log.info('processing %i-th of %i' % (i, len(s)))
                origin, im = utils.extract_roi(row, 30, True)
                scores = net.classify(np.asarray(im), center_only=True)
                X[i] = net.feature('fc7_cudanet_out')
                y[i] = utils.is_positive(row)
            except:
                continue
        feats.append(X)
        ys.append(y)

    return feats[0], ys[0], feats[1], ys[1], feats[2], ys[2]
Beispiel #4
0
def get_feats_from_cnn(rows, model=None):
    """
    fprop rows using best trained model and returns activations of the
    penultimate layer
    """
    conf = utils.get_config()
    patch_size = conf['patch_size']
    region_size = conf['region_size']
    batch_size = None
    preds = utils.get_predictor(model=model, return_all=True)
    y = np.zeros(len(rows))
    samples = np.zeros((len(rows), region_size, region_size, 1),
                       dtype=np.float32)
    for i, row in enumerate(rows):
        print 'processing %i-th image: %s' % (i, row['image_filename'])
        try:
            samples[i] = utils.get_samples_from_image(row, False)[0]
        except ValueError as e:
            print '{1} Value error: {0}'.format(str(e), row['image_filename'])
        y[i] = utils.is_positive(row)
    ds = DenseDesignMatrix(topo_view=samples)
    pipeline = utils.get_pipeline(ds.X_topo_space.shape, patch_size,
                                  batch_size)
    pipeline.apply(ds)
    return preds[-2](ds.get_topological_view()), y
Beispiel #5
0
def get_feats_from_imagenet_in_partitions():
    conf = utils.get_config()
    imagenet_data = os.path.join(conf['models_path'], 'decafnet',
                                 'imagenet.decafnet.epoch90')
    imagenet_meta = os.path.join(conf['models_path'], 'decafnet',
                                 'imagenet.decafnet.meta')
    net = DecafNet(imagenet_data, imagenet_meta)
    rows = utils.get_filtered_rows()
    sets = utils.split_dataset(rows,
                               conf['valid_percent'],
                               conf['test_percent'],
                               rng=conf['rng_seed'])
    feats = []
    ys = []
    for s in sets:
        X = np.zeros((len(s), 4096))
        y = np.zeros(len(s))
        for i, row in enumerate(s):
            try:
                log.info('processing %i-th of %i' % (i, len(s)))
                origin, im = utils.extract_roi(row, 30, True)
                scores = net.classify(np.asarray(im), center_only=True)
                X[i] = net.feature('fc7_cudanet_out')
                y[i] = utils.is_positive(row)
            except:
                continue
        feats.append(X)
        ys.append(y)

    return feats[0], ys[0], feats[1], ys[1], feats[2], ys[2]
Beispiel #6
0
 def dodge(self, dif_x, dif_y):
     """
         trying to dodge player shots
     """
     if is_positive(dif_x):  #enemy at the left of the player
         self.rect.x -= self.speed  #go left to try to dodge bullets
     else:
         self.rect.x += self.speed
Beispiel #7
0
def get_features(rows, features, segm_ids):
    '''
    Get features from a set of rows using segm_ids as a LUT.
    '''
    X = np.zeros((len(rows), features.shape[1]))
    y = np.zeros(len(rows))
    for i, row in enumerate(rows):
        X[i] = features[np.nonzero(int(row['segmentation_id']) == segm_ids)][0]
        y[i] = utils.is_positive(row)
    return X, y
Beispiel #8
0
def get_features(rows, features, segm_ids):
    '''
    Get features from a set of rows using segm_ids as a LUT.
    '''
    X = np.zeros((len(rows), features.shape[1]))
    y = np.zeros(len(rows))
    for i, row in enumerate(rows):
        X[i] = features[np.nonzero(int(row['segmentation_id']) == segm_ids)][0]
        y[i] = utils.is_positive(row)
    return X, y
Beispiel #9
0
    def movement(self, player_x, player_y):
        """
            function to estimate moves of enemies.
        """
        close = [i for i in range(-40, 41)]
        dif_x = player_x - self.rect.x
        dif_y = player_y - self.rect.y

        if dif_x in close and dif_y not in close and is_positive(
                dif_y):  #enemy in "danger zone"
            self.dodge(dif_x, dif_y)
        else:
            self.go_to_player(dif_x, dif_y)
Beispiel #10
0
def get_options(project_type, service_options):
    base_path = f"{project_type}/options/"
    path_len = len(base_path)
    if os.path.exists(base_path):
        paths = [f"{base_path}{f}" for f in os.listdir(base_path)]
        options = {}
        for path in paths:
            name = path[path_len:-5]
            if edge_case := OPTIONS_EDGE_CASES.get(name):
                name = edge_case["alias"]
            value = service_options.get(name)
            options[name] = {
                "path": path,
                "value": value,
                "is_positive": utils.is_positive(value)
            }
            if edge_case and utils.is_positive(value):
                options[name].update({
                    "extra_updates":
                    edge_case.get("extra_updates"),
                    "edgeCase":
                    True
                })
        return options
Beispiel #11
0
def get_feats_from_imagenet(rows):
    conf = utils.get_config()
    imagenet_data = os.path.join(
        conf['models_path'], 'decafnet', 'imagenet.decafnet.epoch90')
    imagenet_meta = os.path.join(
        conf['models_path'], 'decafnet', 'imagenet.decafnet.meta')
    net = DecafNet(imagenet_data, imagenet_meta)
    X = np.zeros((len(rows), 4096))
    y = np.zeros(len(rows))
    for i, row in enumerate(rows):
        try:
            log.info('processing %i-th of %i' % (i, len(rows)))
            origin, im = utils.extract_roi(row, 30, True)
            scores = net.classify(np.asarray(im), center_only=True)
            X[i] = net.feature('fc7_cudanet_out')
            y[i] = utils.is_positive(row)
        except:
            continue

    return X, y
Beispiel #12
0
def get_feats_from_imagenet(rows):
    conf = utils.get_config()
    imagenet_data = os.path.join(conf['models_path'], 'decafnet',
                                 'imagenet.decafnet.epoch90')
    imagenet_meta = os.path.join(conf['models_path'], 'decafnet',
                                 'imagenet.decafnet.meta')
    net = DecafNet(imagenet_data, imagenet_meta)
    X = np.zeros((len(rows), 4096))
    y = np.zeros(len(rows))
    for i, row in enumerate(rows):
        try:
            log.info('processing %i-th of %i' % (i, len(rows)))
            origin, im = utils.extract_roi(row, 30, True)
            scores = net.classify(np.asarray(im), center_only=True)
            X[i] = net.feature('fc7_cudanet_out')
            y[i] = utils.is_positive(row)
        except:
            continue

    return X, y
Beispiel #13
0
def get_all_feats():
    rows = utils.get_filtered_rows()
    y = np.asarray([utils.is_positive(r) for r in rows])
    conv = get_fprop_fn()
    X = get_feats_from_rows(rows, conv, 30)
    return X, y
Beispiel #14
0
def get_all_feats():
    rows = utils.get_filtered_rows()
    y = np.asarray([utils.is_positive(r) for r in rows])
    conv = get_fprop_fn()
    X = get_feats_from_rows(rows, conv, 30)
    return X, y
Beispiel #15
0
        feats = []
        for row in subrows:
            samples = utils.get_samples_from_image(
                row, oversampling=(subset == 'train' and conf['oversampling']))
            print "%i samples to %s taken from %s" % (
                len(samples), subset, row['image_filename'])
            if len(samples) == 0:
                continue
            samples = np.array(samples, dtype=np.float32) / 255.0
            # linearized dimension of im
            ndim = np.cumprod(samples.shape[1:])[-1]
            samples = samples.reshape(samples.shape[0], ndim)
            if X is None:
                f = h5py.File(paths['raw_' + subset], 'w')
                X = f.create_dataset('X', (0, ndim), maxshape=(None, ndim),
                                     compression = "gzip", compression_opts = 9)

            X.resize(X.shape[0] + samples.shape[0], axis=0)
            X[-len(samples):] = samples
            y.extend([utils.is_positive(row) for i in range(len(samples))])
            feats.extend(
                [[float(v) for k, v in row.iteritems() if len(filter(k.startswith, prefixes)) > 0]] * samples.shape[0])

        y = np.asarray(y)
        y = np.vstack((y, 1 - y)).T
        f.create_dataset('y', data=y)
        f.create_dataset('feats', data=np.asarray(feats))
        nsamples[subset] = X.shape[0]
        f.close()
        serial.save(paths[subset + '_rows'], subrows)