示例#1
0
 def fit(self, X, Y, w=None, w_asymmetric=None, depth=1, T=100, **kwargs):
     self.X = X.copy()
     self.Y = Y.copy()
     N = len(self.Y)
     
     if w is None:
         w = (1.0/float(N))*numpy.ones(N)
     if w_asymmetric is None:
         w_asymmetric = (1.0/float(N))*numpy.ones(N)
     self.weights = w.copy()
     self.weights_asymmetric = numpy.array([i**(1.0/float(T)) 
                                                     for i in w_asymmetric])
     self.weights /= float(sum(self.weights))
     self.weak_classifier_ensemble = []
     self.alpha = []
     
     for t in with_progress(range(T), pbar=self.progressbar):
         # Apply asymmetric weights
         self.weights *= self.weights_asymmetric
         weak_learner = DecisionTree().fit(self.X,self.Y,self.weights, depth=depth)
         Y_pred = weak_learner.predict(self.X)
         e = sum(0.5*self.weights*abs(self.Y-Y_pred))/sum(self.weights)
         if e > 0.5:
             logging.warning(' ending training, no good weak classifiers.')
             break
         ee = (1.0-e)/float(e)
         alpha = 0.5*math.log(ee)
         # increase weights for wrongly classified:
         self.weights *= numpy.exp(-alpha*self.Y*Y_pred)
         self.weights /= sum(self.weights)
         self.weak_classifier_ensemble.append(weak_learner)
         self.alpha.append(alpha)
     return self
示例#2
0
def watershed(a, seeds=None, smooth_thresh=0.0, smooth_seeds=False, 
        minimum_seed_size=0, dams=True, show_progress=False, connectivity=1):
    seeded = seeds is not None
    sel = generate_binary_structure(a.ndim, connectivity)
    if smooth_thresh > 0.0:
        b = hminima(a, smooth_thresh)
    if seeded:
        if smooth_seeds:
            seeds = binary_opening(seeds, sel)
        b = impose_minima(a, seeds.astype(bool), connectivity)
    else:
        seeds = regional_minima(a, connectivity)
        b = a
    if seeds.dtype == bool:
        ws = label(seeds, sel)[0]
    else:
        ws = seeds
    levels = unique(a)
    a = pad(a, a.max()+1)
    b = pad(b, b.max()+1)
    ar = a.ravel()
    br = b.ravel()
    ws = pad(ws, 0)
    wsr = ws.ravel()
    maxlabel = iinfo(ws.dtype).max
    current_label = 0
    neighbors = build_neighbors_array(a, connectivity)
    level_pixels = build_levels_dict(a)
    if show_progress: wspbar = ip.StandardProgressBar('Watershed...')
    else: wspbar = ip.NoProgressBar()
    for i, level in ip.with_progress(enumerate(levels), 
                                            pbar=wspbar, length=len(levels)):
        idxs_adjacent_to_labels = queue([idx for idx in level_pixels[level] if
                                            any(wsr[neighbors[idx]])])
        while len(idxs_adjacent_to_labels) > 0:
            idx = idxs_adjacent_to_labels.popleft()
            if wsr[idx]> 0: continue # in case we already processed it
            nidxs = neighbors[idx] # neighbors
            lnidxs = nidxs[
                ((wsr[nidxs] != 0) * (wsr[nidxs] != maxlabel)).astype(bool)
            ] # labeled neighbors
            adj_labels = unique(wsr[lnidxs])
            if len(adj_labels) > 1 and dams: # build a dam
                wsr[idx] = maxlabel 
            elif len(adj_labels) >= 1: # assign a label
                wsr[idx] = wsr[lnidxs][ar[lnidxs].argmin()]
                idxs_adjacent_to_labels.extend(nidxs[((wsr[nidxs] == 0) * 
                                    (br[nidxs] == level)).astype(bool) ])
    if dams:
        ws[ws==maxlabel] = 0
    return juicy_center(ws)
示例#3
0
文件: classify.py 项目: cmor/gala
def label_merges(g, merge_history, feature_map_function, gt, loss_function):
    """Replay an agglomeration history and label the loss of each merge."""
    labels = np.zeros(len(merge_history))
    number_of_features = feature_map_function(g, *g.edges_iter().next()).size
    features = np.zeros((len(merge_history), number_of_features))
    labeled_image = np.zeros(gt.shape, np.double)
    for i, nodes in enumerate(ip.with_progress(
                            merge_history, title='Replaying merge history...', 
                            pbar=ip.StandardProgressBar())):
        n1, n2 = nodes
        features[i,:] = feature_map_function(g, n1, n2)
        labels[i] = loss_function(g, n1, n2, gt)
        labeled_image.ravel()[list(g[n1][n2]['boundary'])] = 2+labels[i]
        g.merge_nodes(n1,n2)
    return features, labels, labeled_image
示例#4
0
文件: classify.py 项目: cmor/gala
def label_merges(g, merge_history, feature_map_function, gt, loss_function):
    """Replay an agglomeration history and label the loss of each merge."""
    labels = np.zeros(len(merge_history))
    number_of_features = feature_map_function(g, *g.edges_iter().next()).size
    features = np.zeros((len(merge_history), number_of_features))
    labeled_image = np.zeros(gt.shape, np.double)
    for i, nodes in enumerate(
            ip.with_progress(merge_history,
                             title='Replaying merge history...',
                             pbar=ip.StandardProgressBar())):
        n1, n2 = nodes
        features[i, :] = feature_map_function(g, n1, n2)
        labels[i] = loss_function(g, n1, n2, gt)
        labeled_image.ravel()[list(g[n1][n2]['boundary'])] = 2 + labels[i]
        g.merge_nodes(n1, n2)
    return features, labels, labeled_image
示例#5
0
 def build_graph_from_watershed(self,
                                allow_shared_boundaries=True,
                                idxs=None):
     if self.watershed.size == 0: return  # stop processing for empty graphs
     if not allow_shared_boundaries:
         self.ignored_boundary = zeros(self.watershed.shape, bool)
     if idxs is None:
         idxs = arange(self.watershed.size)
         self.add_node(
             self.boundary_body,
             extent=set(flatnonzero(self.watershed == self.boundary_body)))
     inner_idxs = idxs[self.watershed_r[idxs] != self.boundary_body]
     pbar = ip.StandardProgressBar() if self.show_progress \
                                     else ip.NoProgressBar()
     for idx in ip.with_progress(inner_idxs, title='Graph... ', pbar=pbar):
         ns = self.neighbor_idxs(idx)
         adj_labels = self.watershed_r[ns]
         adj_labels = unique(adj_labels)
         adj_labels = adj_labels[adj_labels.nonzero()]
         nodeid = self.watershed_r[idx]
         if nodeid != 0:
             adj_labels = adj_labels[adj_labels != nodeid]
             edges = zip(repeat(nodeid), adj_labels)
             if not self.has_node(nodeid):
                 self.add_node(nodeid, extent=set())
             try:
                 self.node[nodeid]['extent'].add(idx)
             except KeyError:
                 self.node[nodeid]['extent'] = set([idx])
         else:
             if len(adj_labels) == 0: continue
             if adj_labels[-1] != self.boundary_body:
                 edges = list(combinations(adj_labels, 2))
             else:
                 edges = list(product([self.boundary_body],
                                      adj_labels[:-1]))
         if allow_shared_boundaries or len(edges) == 1:
             for l1, l2 in edges:
                 if self.has_edge(l1, l2):
                     self[l1][l2]['boundary'].add(idx)
                 else:
                     self.add_edge(l1, l2, boundary=set([idx]))
         elif len(edges) > 1:
             self.ignored_boundary.ravel()[idx] = True
示例#6
0
文件: agglo.py 项目: DerThorsten/ray
 def build_graph_from_watershed(self, 
                                 allow_shared_boundaries=True, idxs=None):
     if self.watershed.size == 0: return # stop processing for empty graphs
     if not allow_shared_boundaries:
         self.ignored_boundary = zeros(self.watershed.shape, bool)
     if idxs is None:
         idxs = arange(self.watershed.size)
         self.add_node(self.boundary_body, 
                 extent=set(flatnonzero(self.watershed==self.boundary_body)))
     inner_idxs = idxs[self.watershed_r[idxs] != self.boundary_body]
     pbar = ip.StandardProgressBar() if self.show_progress \
                                     else ip.NoProgressBar()
     for idx in ip.with_progress(inner_idxs, title='Graph... ', pbar=pbar):
         ns = self.neighbor_idxs(idx)
         adj_labels = self.watershed_r[ns]
         adj_labels = unique(adj_labels)
         adj_labels = adj_labels[adj_labels.nonzero()]
         nodeid = self.watershed_r[idx]
         if nodeid != 0:
             adj_labels = adj_labels[adj_labels != nodeid]
             edges = zip(repeat(nodeid), adj_labels)
             if not self.has_node(nodeid):
                 self.add_node(nodeid, extent=set())
             try:
                 self.node[nodeid]['extent'].add(idx)
             except KeyError:
                 self.node[nodeid]['extent'] = set([idx])
         else:
             if len(adj_labels) == 0: continue
             if adj_labels[-1] != self.boundary_body:
                 edges = list(combinations(adj_labels, 2))
             else:
                 edges = list(product([self.boundary_body], adj_labels[:-1]))
         if allow_shared_boundaries or len(edges) == 1:
             for l1,l2 in edges:
                 if self.has_edge(l1, l2): 
                     self[l1][l2]['boundary'].add(idx)
                 else: 
                     self.add_edge(l1, l2, boundary=set([idx]))
         elif len(edges) > 1:
             self.ignored_boundary.ravel()[idx] = True
示例#7
0
def watershed(a,
              seeds=None,
              connectivity=1,
              mask=None,
              smooth_thresh=0.0,
              smooth_seeds=False,
              minimum_seed_size=0,
              dams=False,
              override_skimage=False,
              show_progress=False):
    """Perform the watershed algorithm of Vincent & Soille (1991).
    
    Parameters
    ----------
    a : np.ndarray, arbitrary shape and type
        The input image on which to perform the watershed transform.
    seeds : np.ndarray, int or bool type, same shape as `a` (optional)
        The seeds for the watershed. If provided, these are the only basins
        allowed, and the algorithm proceeds by flooding from the seeds.
        Otherwise, every local minimum is used as a seed.
    connectivity : int, {1, ..., a.ndim} (optional, default 1)
        The neighborhood of each pixel, defined as in `scipy.ndimage`.
    mask : np.ndarray, type bool, same shape as `a`. (optional)
        If provided, perform watershed only in the parts of `a` that are set
        to `True` in `mask`.
    smooth_thresh : float (optional, default 0.0)
        Local minima that are less deep than this threshold are suppressed,
        using `hminima`.
    smooth_seeds : bool (optional, default False)
        Perform binary opening on the seeds, using the same connectivity as
        the watershed.
    minimum_seed_size : int (optional, default 0)
        Remove seed regions smaller than this size.
    dams : bool (optional, default False)
        Place a dam where two basins meet. Set this to True if you require
        0-labeled boundaries between different regions.
    override_skimage : bool (optional, default False)
        skimage.morphology.watershed is used to implement the main part of the
        algorithm when `dams=False`. Use this flag to use the separate pure
        Python implementation instead.
    show_progress : bool (optional, default False)
        Show a cute little ASCII progress bar (using the progressbar package)

    Returns
    -------
    ws : np.ndarray, same shape as `a`, int type.
        The watershed transform of the input image.
    """
    seeded = seeds is not None
    sel = generate_binary_structure(a.ndim, connectivity)
    # various keyword arguments operate by modifying the input image `a`.
    # However, we operate on a copy of it called `b`, so that `a` can be used
    # to break ties.
    b = a
    if smooth_thresh > 0.0:
        b = hminima(a, smooth_thresh)
    if not seeded:
        seeds = regional_minima(b, connectivity)
    if minimum_seed_size > 0:
        seeds = remove_small_connected_components(seeds,
                                                  minimum_seed_size,
                                                  in_place=True)
        seeds = relabel_from_one(seeds)[0]
    if smooth_seeds:
        seeds = binary_opening(seeds, sel)
    if seeds.dtype == bool:
        seeds = label(seeds, sel)[0]
    if skimage_available and not override_skimage and not dams:
        return skimage.morphology.watershed(b, seeds, sel, None, mask)
    elif seeded:
        b = impose_minima(a, seeds.astype(bool), connectivity)
    levels = unique(b)
    a = pad(a, a.max() + 1)
    b = pad(b, b.max() + 1)
    ar = a.ravel()
    br = b.ravel()
    ws = pad(seeds, 0)
    wsr = ws.ravel()
    neighbors = build_neighbors_array(a, connectivity)
    level_pixels = build_levels_dict(b)
    if show_progress: wspbar = ip.StandardProgressBar('Watershed...')
    else: wspbar = ip.NoProgressBar()
    for i, level in ip.with_progress(enumerate(levels),
                                     pbar=wspbar,
                                     length=len(levels)):
        idxs_adjacent_to_labels = queue(
            [idx for idx in level_pixels[level] if any(wsr[neighbors[idx]])])
        while len(idxs_adjacent_to_labels) > 0:
            idx = idxs_adjacent_to_labels.popleft()
            if wsr[idx] > 0: continue  # in case we already processed it
            nidxs = neighbors[idx]  # neighbors
            lnidxs = nidxs[(wsr[nidxs] != 0).astype(bool)]  # labeled neighbors
            adj_labels = unique(wsr[lnidxs])
            if len(adj_labels) == 1 or len(adj_labels) > 1 and not dams:
                # assign a label
                wsr[idx] = wsr[lnidxs][ar[lnidxs].argmin()]
                idxs_adjacent_to_labels.extend(
                    nidxs[((wsr[nidxs] == 0) *
                           (br[nidxs] == level)).astype(bool)])
    return juicy_center(ws)
示例#8
0
文件: morpho.py 项目: cmor/gala
def watershed(a, seeds=None, connectivity=1, mask=None, smooth_thresh=0.0, 
        smooth_seeds=False, minimum_seed_size=0, dams=False,
        override_skimage=False, show_progress=False):
    """Perform the watershed algorithm of Vincent & Soille (1991).
    
    Parameters
    ----------
    a : np.ndarray, arbitrary shape and type
        The input image on which to perform the watershed transform.
    seeds : np.ndarray, int or bool type, same shape as `a` (optional)
        The seeds for the watershed. If provided, these are the only basins
        allowed, and the algorithm proceeds by flooding from the seeds.
        Otherwise, every local minimum is used as a seed.
    connectivity : int, {1, ..., a.ndim} (optional, default 1)
        The neighborhood of each pixel, defined as in `scipy.ndimage`.
    mask : np.ndarray, type bool, same shape as `a`. (optional)
        If provided, perform watershed only in the parts of `a` that are set
        to `True` in `mask`.
    smooth_thresh : float (optional, default 0.0)
        Local minima that are less deep than this threshold are suppressed,
        using `hminima`.
    smooth_seeds : bool (optional, default False)
        Perform binary opening on the seeds, using the same connectivity as
        the watershed.
    minimum_seed_size : int (optional, default 0)
        Remove seed regions smaller than this size.
    dams : bool (optional, default False)
        Place a dam where two basins meet. Set this to True if you require
        0-labeled boundaries between different regions.
    override_skimage : bool (optional, default False)
        skimage.morphology.watershed is used to implement the main part of the
        algorithm when `dams=False`. Use this flag to use the separate pure
        Python implementation instead.
    show_progress : bool (optional, default False)
        Show a cute little ASCII progress bar (using the progressbar package)

    Returns
    -------
    ws : np.ndarray, same shape as `a`, int type.
        The watershed transform of the input image.
    """
    seeded = seeds is not None
    sel = generate_binary_structure(a.ndim, connectivity)
    # various keyword arguments operate by modifying the input image `a`.
    # However, we operate on a copy of it called `b`, so that `a` can be used
    # to break ties.
    b = a
    if smooth_thresh > 0.0:
        b = hminima(a, smooth_thresh)
    if not seeded:
        seeds = regional_minima(b, connectivity)
    if minimum_seed_size > 0:
        seeds = remove_small_connected_components(seeds, minimum_seed_size,
                                                  in_place=True)
        seeds = relabel_from_one(seeds)[0]
    if smooth_seeds:
        seeds = binary_opening(seeds, sel)
    if seeds.dtype == bool:
        seeds = label(seeds, sel)[0]
    if skimage_available and not override_skimage and not dams:
        return skimage.morphology.watershed(b, seeds, sel, None, mask)
    elif seeded:
        b = impose_minima(a, seeds.astype(bool), connectivity)
    levels = unique(b)
    a = pad(a, a.max()+1)
    b = pad(b, b.max()+1)
    ar = a.ravel()
    br = b.ravel()
    ws = pad(seeds, 0)
    wsr = ws.ravel()
    neighbors = build_neighbors_array(a, connectivity)
    level_pixels = build_levels_dict(b)
    if show_progress: wspbar = ip.StandardProgressBar('Watershed...')
    else: wspbar = ip.NoProgressBar()
    for i, level in ip.with_progress(enumerate(levels), 
                                            pbar=wspbar, length=len(levels)):
        idxs_adjacent_to_labels = queue([idx for idx in level_pixels[level] if
                                            any(wsr[neighbors[idx]])])
        while len(idxs_adjacent_to_labels) > 0:
            idx = idxs_adjacent_to_labels.popleft()
            if wsr[idx] > 0: continue # in case we already processed it
            nidxs = neighbors[idx] # neighbors
            lnidxs = nidxs[(wsr[nidxs] != 0).astype(bool)] # labeled neighbors
            adj_labels = unique(wsr[lnidxs])
            if len(adj_labels) == 1 or len(adj_labels) > 1 and not dams: 
                # assign a label
                wsr[idx] = wsr[lnidxs][ar[lnidxs].argmin()]
                idxs_adjacent_to_labels.extend(nidxs[((wsr[nidxs] == 0) * 
                                    (br[nidxs] == level)).astype(bool) ])
    return juicy_center(ws)