python类union1d()的实例源码

sample.py 文件源码 项目:Master-R-CNN 作者: Mark110 项目源码 文件源码 阅读 54 收藏 0 点赞 0 评论 0
def sample_rpn_outputs_wrt_gt_boxes(boxes, scores, gt_boxes, is_training=False, only_positive=False):
    """sample boxes for refined output"""
    boxes, scores, batch_inds = sample_rpn_outputs(boxes, scores, is_training, only_positive)

    if gt_boxes.size > 0:
        overlaps = cython_bbox.bbox_overlaps(
                np.ascontiguousarray(boxes[:, 0:4], dtype=np.float),
                np.ascontiguousarray(gt_boxes[:, 0:4], dtype=np.float))
        gt_assignment = overlaps.argmax(axis=1) # B
        max_overlaps = overlaps[np.arange(boxes.shape[0]), gt_assignment] # B
        fg_inds = np.where(max_overlaps >= cfg.FLAGS.fg_threshold)[0]

        mask_fg_inds = np.where(max_overlaps >= cfg.FLAGS.mask_threshold)[0]
        if mask_fg_inds.size > cfg.FLAGS.masks_per_image:
            mask_fg_inds = np.random.choice(mask_fg_inds, size=cfg.FLAGS.masks_per_image, replace=False)

        if True:
            gt_argmax_overlaps = overlaps.argmax(axis=0) # G
            fg_inds = np.union1d(gt_argmax_overlaps, fg_inds)

    fg_rois = int(min(fg_inds.size, cfg.FLAGS.rois_per_image * cfg.FLAGS.fg_roi_fraction))
        if fg_inds.size > 0 and fg_rois < fg_inds.size:
           fg_inds = np.random.choice(fg_inds, size=fg_rois, replace=False)

    # TODO: sampling strategy
        bg_inds = np.where((max_overlaps < cfg.FLAGS.bg_threshold))[0]
        bg_rois = max(min(cfg.FLAGS.rois_per_image - fg_rois, fg_rois * 3), 64)
        if bg_inds.size > 0 and bg_rois < bg_inds.size:
           bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

    keep_inds = np.append(fg_inds, bg_inds)
    else:
        bg_inds = np.arange(boxes.shape[0])
        bg_rois = min(int(cfg.FLAGS.rois_per_image * (1-cfg.FLAGS.fg_roi_fraction)), 64)
        if bg_rois < bg_inds.size:
            bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

        keep_inds = bg_inds
        mask_fg_inds = np.arange(0)

    return boxes[keep_inds, :], scores[keep_inds], batch_inds[keep_inds],\
           boxes[mask_fg_inds, :], scores[mask_fg_inds], batch_inds[mask_fg_inds]
svm.py 文件源码 项目:prml 作者: Yevgnen 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def fit(self, X, T, max_iter=int(1e3), tol=1e-5):
        """Use training data ``X`` and ``T`` to fit a SVC models."""
        n_samples = X.shape[0]
        n_dual_vars = 2 * n_samples
        # Compute the Gram matrix of training data
        K = self.kernel.inner(X, X)

        # The equality constraints: H(x) = 0
        ones = np.ones(n_samples)
        A = np.concatenate((ones, -ones))
        cons = ({'type': 'eq', 'fun': lambda x: A.dot(x), 'jac': lambda x: A})

        # The inequality constaints: 0 <= G(x) <= C
        bnds = [(0, self.C) for i in range(n_dual_vars)]

        # The target function: (1/2)*x'*Q*x + p'*x
        Q = np.array(np.bmat([[K, -K], [-K, K]]))
        p = self.eps - A * np.concatenate((T, T))
        lagrange = lambda x: (0.5 * x.dot(Q).dot(x) + p.dot(x), Q.dot(x) + p)

        # Solve the quadratic program
        opt_solution = minimize(lagrange,
                                np.zeros(n_dual_vars),
                                method='SLSQP',
                                constraints=cons,
                                bounds=bnds,
                                tol=tol,
                                jac=True,
                                options={'maxiter': max_iter,
                                         'disp': True})

        self.dual_var = np.array([None, None], dtype=np.object)
        self.dual_var[0] = opt_solution.x[:n_samples]
        self.dual_var[1] = opt_solution.x[n_samples:]

        self.sv_indices = np.array([None, None], dtype=np.object)
        self.sv_indices[0] = np.nonzero((1 - np.isclose(self.dual_var[0], 0)))[
            0]
        self.sv_indices[1] = np.nonzero((1 - np.isclose(self.dual_var[1], 0)))[
            0]

        self.union_sv_inices = np.union1d(*self.sv_indices)

        self.inner_sv_indices = np.array([None, None], dtype=np.object)
        self.inner_sv_indices[0] = np.nonzero(
            (1 - np.isclose(self.dual_var[0], 0)) *
            (1 - np.isclose(self.dual_var[0], self.C)))[0]
        self.inner_sv_indices[1] = np.nonzero(
            (1 - np.isclose(self.dual_var[1], 0)) *
            (1 - np.isclose(self.dual_var[1], self.C)))[0]

        return self
mdf.py 文件源码 项目:asammdf 作者: danielhrisca 项目源码 文件源码 阅读 14 收藏 0 点赞 0 评论 0
def select(self, channels, dataframe=False):
        """ return the channels listed in *channels* argument

        Parameters
        ----------
        channels : list
            list of channel names to be filtered
        dataframe: bool
            return a pandas DataFrame instead of a list of Signals; in this
            case the signals will be interpolated using the union of all
            timestamps

        Returns
        -------
        signals : list
            lsit of *Signal* objects based on the input channel list

        """

        # group channels by group index
        gps = {}
        for ch in channels:
            if ch in self.channels_db:
                for group, index in self.channels_db[ch]:
                    if group not in gps:
                        gps[group] = []
                    gps[group].append(index)
            else:
                message = ('MDF filter error: '
                           'Channel "{}" not found, it will be ignored')
                warn(message.format(ch))
                continue

        # append filtered channels to new MDF
        signals = {}
        for group in gps:
            grp = self.groups[group]
            data = self._load_group_data(grp)
            for index in gps[group]:
                signal = self.get(group=group, index=index, data=data)
                signals[signal.name] = signal

        signals = [signals[channel] for channel in channels]

        if dataframe:
            times = [s.timestamps for s in signals]
            t = reduce(np.union1d, times).flatten().astype(np.float64)
            signals = [s.interp(t) for s in signals]
            times = None

            pandas_dict = {'t': t}
            for sig in signals:
                pandas_dict[sig.name] = sig.samples

            signals = DataFrame.from_dict(pandas_dict)

        return signals
postprocessing.py 文件源码 项目:deepgestures_lasagne 作者: nneverova 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def gesture_overlap_csv(csvpathgt, csvpathpred, seqlenght):
    """ Evaluate this sample against the ground truth file """
    maxGestures = 20

    # Get the list of gestures from the ground truth and frame activation
    gtGestures = []
    binvec_gt = numpy.zeros((maxGestures, seqlenght))
    with open(csvpathgt, 'rb') as csvfilegt:
        csvgt = csv.reader(csvfilegt)
        for row in csvgt:
            binvec_gt[int(row[0])-1, int(row[1])-1:int(row[2])-1] = 1
            gtGestures.append(int(row[0]))

    # Get the list of gestures from prediction and frame activation
    predGestures = []
    binvec_pred = numpy.zeros((maxGestures, seqlenght))
    with open(csvpathpred, 'rb') as csvfilepred:
        csvpred = csv.reader(csvfilepred)
        for row in csvpred:
            binvec_pred[int(row[0])-1, int(row[1])-1:int(row[2])-1] = 1
            predGestures.append(int(row[0]))

    # Get the list of gestures without repetitions for ground truth and predicton
    gtGestures = numpy.unique(gtGestures)
    predGestures = numpy.unique(predGestures)

    bgt = (numpy.argmax(binvec_gt,axis=0)+1) * (numpy.max(binvec_gt,axis=0)>0)
    bpred = (numpy.argmax(binvec_pred,axis=0)+1) * (numpy.max(binvec_pred,axis=0)>0)

    # Find false positives
    falsePos=numpy.setdiff1d(gtGestures,numpy.union1d(gtGestures,numpy.union1d(gtGestures,predGestures)))

    # Get overlaps for each gesture
    overlaps = []
    for idx in gtGestures:
        intersec = sum(binvec_gt[idx-1] * binvec_pred[idx-1])
        aux = binvec_gt[idx-1] + binvec_pred[idx-1]
        union = sum(aux > 0)
        overlaps.append(intersec/union)

    # Use real gestures and false positive gestures to calculate the final score
    return sum(overlaps)/(len(overlaps)+len(falsePos))
spectrum.py 文件源码 项目:pysynphot 作者: spacetelescope 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def MergeWaveSets(waveset1, waveset2):
    """Return the union of the two wavelength sets.

    The union is computed using `numpy.union1d`, unless one or
    both of them is `None`.

    The merged result may sometimes contain numbers which are nearly
    equal but differ at levels as small as 1E-14. Having values this
    close together can cause problems due to effectively duplicate
    wavelength values. Therefore, wavelength values having differences
    smaller than or equal to ``pysynphot.spectrum.MERGETHRESH``
    (defaults to 1E-12) are considered as the same.

    Parameters
    ----------
    waveset1, waveset2 : array_like or `None`
        Wavelength sets to combine.

    Returns
    -------
    MergedWaveSet : array_like or `None`
        Merged wavelength set. It is `None` if both inputs are such.

    """
    if waveset1 is None and waveset2 is not None:
        MergedWaveSet = waveset2
    elif waveset2 is None and waveset1 is not None:
        MergedWaveSet = waveset1
    elif waveset1 is None and waveset2 is None:
        MergedWaveSet = None
    else:
        MergedWaveSet = N.union1d(waveset1, waveset2)

        # The merged wave sets may sometimes contain numbers which are nearly
        # equal but differ at levels as small as 1e-14. Having values this
        # close together can cause problems down the line so here we test
        # whether any such small differences are present, with a small
        # difference defined as less than MERGETHRESH.
        #
        # If small differences are present we make a copy of the union'ed array
        # with the lower of the close together pairs removed.
        delta = MergedWaveSet[1:] - MergedWaveSet[:-1]

        if not (delta > MERGETHRESH).all():
            newlen = len(delta[delta > MERGETHRESH]) + 1
            newmerged = N.zeros(newlen, dtype=MergedWaveSet.dtype)
            newmerged[:-1] = MergedWaveSet[:-1][delta > MERGETHRESH]
            newmerged[-1] = MergedWaveSet[-1]

            MergedWaveSet = newmerged

    return MergedWaveSet
object.py 文件源码 项目:spdb 作者: jhuapl-boss 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_ids_in_region(
            self, cutout_fcn, resource, resolution, corner, extent,
            t_range=[0, 1], version=0):
        """
        Method to get all the ids within a defined region.

        Args:
            cutout_fcn (function): SpatialDB's cutout method.  Provided for naive search of ids in sub-regions
            resource (project.BossResource): Data model info based on the request or target resource
            resolution (int): the resolution level
            corner ((int, int, int)): xyz location of the corner of the region
            extent ((int, int, int)): xyz extents of the region
            t_range (optional[list[int]]): time range, defaults to [0, 1]
            version (optional[int]): Reserved for future use.  Defaults to 0

        Returns:
            (dict): { 'ids': ['1', '4', '8'] }

        """

        # Identify sub-region entirely contained by cuboids.
        cuboids = Region.get_cuboid_aligned_sub_region(
            resolution, corner, extent)

        # Get all non-cuboid aligned sub-regions.
        non_cuboid_list = Region.get_all_partial_sub_regions(
            resolution, corner, extent)

        # Do cutouts on each partial region and build id set.
        id_set = np.array([], dtype='uint64')
        for partial_region in non_cuboid_list:
            extent = partial_region.extent
            if extent[0] == 0 or extent[1] == 0 or extent[2] == 0:
                continue
            id_arr = self._get_ids_from_cutout(
                cutout_fcn, resource, resolution,
                partial_region.corner, partial_region.extent,
                t_range, version)
            # TODO: do a unique first?  perf test
            id_set = np.union1d(id_set, id_arr)

        # Get ids from dynamo for sub-region that's 100% cuboid aligned.
        obj_key_list = self._get_object_keys(
            resource, resolution, cuboids, t_range)
        cuboid_ids = self.obj_ind.get_ids_in_cuboids(obj_key_list, version)
        cuboid_ids_arr = np.asarray([int(id) for id in cuboid_ids], dtype='uint64')

        # Union ids from cuboid aligned sub-region.
        id_set = np.union1d(id_set, cuboid_ids_arr)

        # Convert ids back to strings for transmission via HTTP.
        ids_as_str = ['%d' % n for n in id_set]

        return { 'ids': ids_as_str }
sample.py 文件源码 项目:FastMaskRCNN 作者: CharlesShang 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def sample_rpn_outputs_wrt_gt_boxes(boxes, scores, gt_boxes, is_training=False, only_positive=False):
    """sample boxes for refined output"""
    boxes, scores, batch_inds = sample_rpn_outputs(boxes, scores, is_training, only_positive)

    if gt_boxes.size > 0:
        overlaps = cython_bbox.bbox_overlaps(
                np.ascontiguousarray(boxes[:, 0:4], dtype=np.float),
                np.ascontiguousarray(gt_boxes[:, 0:4], dtype=np.float))
        gt_assignment = overlaps.argmax(axis=1) # B
        max_overlaps = overlaps[np.arange(boxes.shape[0]), gt_assignment] # B
        fg_inds = np.where(max_overlaps >= cfg.FLAGS.fg_threshold)[0]
        if _DEBUG and np.argmax(overlaps[fg_inds],axis=1).size < gt_boxes.size/5.0:
            print("gt_size")
            print(gt_boxes)
            gt_height = (gt_boxes[:,2]-gt_boxes[:,0])
            gt_width = (gt_boxes[:,3]-gt_boxes[:,1])
            gt_dim = np.vstack((gt_height, gt_width))
            print(np.transpose(gt_dim))
            #print(gt_height)
            #print(gt_width)

            print('SAMPLE: %d after overlaps by %s' % (len(fg_inds),cfg.FLAGS.fg_threshold))
            print("detected object no.")
            print(np.argmax(overlaps[fg_inds],axis=1))
            print("total object")
            print(gt_boxes.size/5.0)

        mask_fg_inds = np.where(max_overlaps >= cfg.FLAGS.mask_threshold)[0]
        if mask_fg_inds.size > cfg.FLAGS.masks_per_image:
            mask_fg_inds = np.random.choice(mask_fg_inds, size=cfg.FLAGS.masks_per_image, replace=False)

        if True:
            gt_argmax_overlaps = overlaps.argmax(axis=0) # G
            fg_inds = np.union1d(gt_argmax_overlaps, fg_inds)

    fg_rois = int(min(fg_inds.size, cfg.FLAGS.rois_per_image * cfg.FLAGS.fg_roi_fraction))
        if fg_inds.size > 0 and fg_rois < fg_inds.size:
           fg_inds = np.random.choice(fg_inds, size=fg_rois, replace=False)

    # TODO: sampling strategy
        bg_inds = np.where((max_overlaps < cfg.FLAGS.bg_threshold))[0]
        bg_rois = max(min(cfg.FLAGS.rois_per_image - fg_rois, fg_rois * 3), 8)#64
        if bg_inds.size > 0 and bg_rois < bg_inds.size:
           bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

        keep_inds = np.append(fg_inds, bg_inds)
        #print(gt_boxes[np.argmax(overlaps[fg_inds],axis=1),4])
    else:
        bg_inds = np.arange(boxes.shape[0])
        bg_rois = min(int(cfg.FLAGS.rois_per_image * (1-cfg.FLAGS.fg_roi_fraction)), 8)#64
        if bg_rois < bg_inds.size:
            bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

        keep_inds = bg_inds
        mask_fg_inds = np.arange(0)

    return boxes[keep_inds, :], scores[keep_inds], batch_inds[keep_inds],\
           boxes[mask_fg_inds, :], scores[mask_fg_inds], batch_inds[mask_fg_inds]
_marker_gui.py 文件源码 项目:decoding_challenge_cortana_2016_3rd 作者: kingjr 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def _get_points(self):
        # in case only one or no source is enabled
        if not (self.src1 and self.src1.enabled):
            if (self.src2 and self.src2.enabled):
                return self.src2.points
            else:
                return np.zeros((5, 3))
        elif not (self.src2 and self.src2.enabled):
            return self.src1.points

        # Average method
        if self.method == 'Average':
            if len(np.union1d(self.src1.use, self.src2.use)) < 5:
                error(None, "Need at least one source for each point.",
                      "Marker Average Error")
                return np.zeros((5, 3))

            pts = (self.src1.points + self.src2.points) / 2.
            for i in np.setdiff1d(self.src1.use, self.src2.use):
                pts[i] = self.src1.points[i]
            for i in np.setdiff1d(self.src2.use, self.src1.use):
                pts[i] = self.src2.points[i]

            return pts

        # Transform method
        idx = np.intersect1d(self.src1.use, self.src2.use, assume_unique=True)
        if len(idx) < 3:
            error(None, "Need at least three shared points for trans"
                  "formation.", "Marker Interpolation Error")
            return np.zeros((5, 3))

        src_pts = self.src1.points[idx]
        tgt_pts = self.src2.points[idx]
        est = fit_matched_points(src_pts, tgt_pts, out='params')
        rot = np.array(est[:3]) / 2.
        tra = np.array(est[3:]) / 2.

        if len(self.src1.use) == 5:
            trans = np.dot(translation(*tra), rotation(*rot))
            pts = apply_trans(trans, self.src1.points)
        elif len(self.src2.use) == 5:
            trans = np.dot(translation(* -tra), rotation(* -rot))
            pts = apply_trans(trans, self.src2.points)
        else:
            trans1 = np.dot(translation(*tra), rotation(*rot))
            pts = apply_trans(trans1, self.src1.points)
            trans2 = np.dot(translation(* -tra), rotation(* -rot))
            for i in np.setdiff1d(self.src2.use, self.src1.use):
                pts[i] = apply_trans(trans2, self.src2.points[i])

        return pts
sample.py 文件源码 项目:TFMaskRCNN 作者: hillox 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def sample_rpn_outputs_wrt_gt_boxes(boxes, scores, gt_boxes, is_training=False, only_positive=False):
    """sample boxes for refined output"""
    boxes, scores, batch_inds = sample_rpn_outputs(boxes, scores, is_training, only_positive)

    if gt_boxes.size > 0:
        overlaps = cython_bbox.bbox_overlaps(
                np.ascontiguousarray(boxes[:, 0:4], dtype=np.float),
                np.ascontiguousarray(gt_boxes[:, 0:4], dtype=np.float))
        gt_assignment = overlaps.argmax(axis=1) # B
        max_overlaps = overlaps[np.arange(boxes.shape[0]), gt_assignment] # B
        fg_inds = np.where(max_overlaps >= cfg.FLAGS.fg_threshold)[0]

        mask_fg_inds = np.where(max_overlaps >= cfg.FLAGS.mask_threshold)[0]
        if mask_fg_inds.size > cfg.FLAGS.masks_per_image:
            mask_fg_inds = np.random.choice(mask_fg_inds, size=cfg.FLAGS.masks_per_image, replace=False)

        if True:
            gt_argmax_overlaps = overlaps.argmax(axis=0) # G
            fg_inds = np.union1d(gt_argmax_overlaps, fg_inds)

    fg_rois = int(min(fg_inds.size, cfg.FLAGS.rois_per_image * cfg.FLAGS.fg_roi_fraction))
        if fg_inds.size > 0 and fg_rois < fg_inds.size:
           fg_inds = np.random.choice(fg_inds, size=fg_rois, replace=False)

    # TODO: sampling strategy
        bg_inds = np.where((max_overlaps < cfg.FLAGS.bg_threshold))[0]
        bg_rois = max(min(cfg.FLAGS.rois_per_image - fg_rois, fg_rois * 3), 64)
        if bg_inds.size > 0 and bg_rois < bg_inds.size:
           bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

    keep_inds = np.append(fg_inds, bg_inds)
    else:
        bg_inds = np.arange(boxes.shape[0])
        bg_rois = min(int(cfg.FLAGS.rois_per_image * (1-cfg.FLAGS.fg_roi_fraction)), 64)
        if bg_rois < bg_inds.size:
            bg_inds = np.random.choice(bg_inds, size=bg_rois, replace=False)

        keep_inds = bg_inds
        mask_fg_inds = np.arange(0)

    return boxes[keep_inds, :], scores[keep_inds], batch_inds[keep_inds],\
           boxes[mask_fg_inds, :], scores[mask_fg_inds], batch_inds[mask_fg_inds]


问题


面经


文章

微信
公众号

扫码关注公众号