Python numpy.digitize() Examples

The following are 30 code examples of numpy.digitize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module numpy , or try the search function .
Example #1
Source File: population.py    From ibllib with MIT License 6 votes vote down vote up
def _index_of(arr, lookup):
    """Replace scalars in an array by their indices in a lookup table.

    Implicitely assume that:

    * All elements of arr and lookup are non-negative integers.
    * All elements or arr belong to lookup.

    This is not checked for performance reasons.

    """
    # Equivalent of np.digitize(arr, lookup) - 1, but much faster.
    # TODO: assertions to disable in production for performance reasons.
    # TODO: np.searchsorted(lookup, arr) is faster on small arrays with large
    # values
    lookup = np.asarray(lookup, dtype=np.int32)
    m = (lookup.max() if len(lookup) else 0) + 1
    tmp = np.zeros(m + 1, dtype=np.int)
    # Ensure that -1 values are kept.
    tmp[-1] = -1
    if len(lookup):
        tmp[lookup] = np.arange(len(lookup))
    return tmp[arr] 
Example #2
Source File: breakdown_metric.py    From lingvo with Apache License 2.0 6 votes vote down vote up
def Discretize(self, bboxes):
    rotations = self._CalculateRotation(bboxes)
    p = self.params
    bin_width = (
        p.metadata.MaximumRotation() / float(self.NumBinsOfHistogram()))
    # TODO(shlens): Consider merging the entries with -1 and 0 bin index
    # because rotation is circular.
    rotations_binned = np.digitize(
        rotations, np.arange(0.0, p.metadata.MaximumRotation(), bin_width))
    # index == 0 corresponds to distances outside less than 0.0. Since this is
    # not possible, we discard this possibility and make the output 0 indexed to
    # match the behavior of np.histogram().
    assert np.all(rotations_binned > 0.0), ('Rotation is negative: %s' %
                                            rotations_binned)
    rotations_binned -= 1
    return rotations_binned 
Example #3
Source File: kopparapuPlot.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def quantize(self, specs, plan_id, star_ind):
        r'''Compute the radius, luminosity, and combined bins for a given planet and star.
        This is Rhonda's original code. It is here to allow cross-checks but is not used now.
        Returns None if the planet/star lies outside the bin boundaries.'''

        # return value indicating error
        error_rval = (None, None, None)
        # extract planet values
        Rp_single = strip_units(specs['Rp'][plan_id])
        a_single = strip_units(specs['a'][plan_id])
        L_star = specs['L'][star_ind]
        L_plan = L_star/a_single**2
        # bin them
        tmpbinplace_Rp = np.digitize(Rp_single, self.Rp_bins)
        if tmpbinplace_Rp >= len(self.Rp_bins):
            return error_rval # out of range
        tmpbinplace_L = np.digitize(L_plan, self.L_bins[tmpbinplace_Rp-1])
        if tmpbinplace_L >= len(self.L_bins[tmpbinplace_Rp-1]):
            return error_rval # out of range
        Rp_bin = tmpbinplace_Rp.item()
        L_bin = tmpbinplace_L.item()
        RpL_bin = tmpbinplace_L.item() + 10*tmpbinplace_Rp.item()
        return Rp_bin, L_bin, RpL_bin 
Example #4
Source File: kopparapuPlot.py    From EXOSIMS with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def quantize_final(self, specs, plan_id, star_ind):
        r'''Compute the final radius/luminosity bin, an integer, for a given planet and star.
        Returns 0 if the planet/star lies outside the bin boundaries.  Returns 1..15 otherwise.'''
        # extract planet and star properties
        Rp_plan = strip_units(specs['Rp'][plan_id])
        a_plan = strip_units(specs['a'][plan_id])
        L_star = specs['L'][star_ind]
        L_plan = L_star / (a_plan**2) # adjust star luminosity by distance^2 in AU
        # Bin by Rp.  "too low" maps to 0, and "too high" maps to len(Rp_bins).
        Rp_bin = np.digitize(Rp_plan, self.Rp_bins)
        # index into L_bins array: if Rp is out-of-range, index is irrelevant
        Rp_bin_index = Rp_bin-1 if (Rp_bin > 0) and (Rp_bin < len(self.Rp_bins)) else 0
        # bin by L
        L_bin = np.digitize(L_plan, self.L_bins[Rp_bin_index])
        # map the pair (Rp,L) -> RpL
        # Rp_bin and L_bin are np arrays, so need to cast to integers
        return self.Rp_L_to_RpL_bin[(int(Rp_bin), int(L_bin))] 
Example #5
Source File: breakdown_metric.py    From lingvo with Apache License 2.0 6 votes vote down vote up
def Discretize(self, num_points):
    num_points_binned = np.digitize(num_points,
                                    self._LogSpacedBinEdgesofPoints())
    # index == 0 corresponds to boxes with 0 points. Because we plot everything
    # logarithmically, this is a pain in the buttocks. For simplicity, we merely
    # accumulate the boxes with 0 points into the first bin.
    num_points_binned[num_points_binned == 0] = 1
    num_bins = len(self._LogSpacedBinEdgesofPoints())
    # index == len(self._LogSpacedBinEdgesofPoints()) corresponds to
    # points with to points outside of the range of the last edge. We map
    # these points back to the final bucket for simplicity.
    num_points_binned[num_points_binned == num_bins] -= 1
    # There is an inconsistency between how np.digitize() and np.histogram()
    # index their bins and this is due to the fact that index == 0 is reserved
    # for examples less than the minimum bin edge.
    num_points_binned -= 1
    return num_points_binned 
Example #6
Source File: xrft.py    From xrft with MIT License 6 votes vote down vote up
def _radial_wvnum(k, l, N, nfactor):
    """ Creates a radial wavenumber based on two horizontal wavenumbers
    along with the appropriate index map
    """

    # compute target wavenumbers
    k = k.values
    l = l.values
    K = np.sqrt(k[np.newaxis,:]**2 + l[:,np.newaxis]**2)
    nbins = int(N/nfactor)
    if k.max() > l.max():
        ki = np.linspace(0., l.max(), nbins)
    else:
        ki = np.linspace(0., k.max(), nbins)

    # compute bin index
    kidx = np.digitize(np.ravel(K), ki)
    # compute number of points for each wavenumber
    area = np.bincount(kidx)
    # compute the average radial wavenumber for each bin
    kr = (np.bincount(kidx, weights=K.ravel())
          / np.ma.masked_where(area==0, area))

    return ki, kr[1:-1] 
Example #7
Source File: cca.py    From ibllib with MIT License 6 votes vote down vote up
def bin_spikes_trials(spikes, trials, bin_size=0.01):
    """
    Binarizes the spike times into a raster and assigns a trial number to each bin

    :param spikes: spikes object
    :type spikes: Bunch
    :param trials: trials object
    :type trials: Bunch
    :param bin_size: size, in s, of the bins
    :type bin_size: float
    :return: a matrix (bins, SpikeCounts), and a vector of bins size with trial ID,
    and a vector bins size with the time that the bins start
    """
    binned_spikes, bin_times, _ = bincount2D(spikes['times'], spikes['clusters'], bin_size)
    trial_start_times = trials['intervals'][:, 0]
    binned_trialIDs = np.digitize(bin_times, trial_start_times)
    # correct, as index 0 is whatever happens before the first trial
    binned_trialIDs_corrected = binned_trialIDs - 1

    return binned_spikes.T, binned_trialIDs_corrected, bin_times 
Example #8
Source File: combine_score.py    From visual_foresight with MIT License 6 votes vote down vote up
def make_stats(dir, score, name, bounds):
    bin_edges = np.linspace(bounds[0], bounds[1], 11)
    binned_ind = np.digitize(score, bin_edges)
    occurrence, _ = np.histogram(score, bin_edges, density=False)
    bin_width = bin_edges[1] - bin_edges[0]
    bin_mid = bin_edges + bin_width / 2
    plt.figure()
    plt.bar(bin_mid[:-1], occurrence, bin_width, facecolor='b', alpha=0.5)
    plt.title(name)
    plt.xlabel(name)
    plt.ylabel('occurences')
    plt.savefig(dir + '/' + name + '.png')
    plt.close()
    f = open(dir + '/{}_histo.txt'.format(name), 'w')
    for i in range(bin_edges.shape[0]-1):
        f.write('indices for bin {}, {} to {} : {} \n'.format(i, bin_edges[i], bin_edges[i+1], np.where(binned_ind == i+1)[0].tolist())) 
Example #9
Source File: datasets.py    From deep-head-pose with Apache License 2.0 6 votes vote down vote up
def __getitem__(self, index):
        img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext))
        img = img.convert(self.image_mode)
        txt_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # We get the pose in radians
        annot = open(txt_path, 'r')
        line = annot.readline().split(' ')
        pose = [float(line[1]), float(line[2]), float(line[3])]
        # And convert to degrees.
        yaw = pose[0] * 180 / np.pi
        pitch = pose[1] * 180 / np.pi
        roll = pose[2] * 180 / np.pi
        # Fix the roll in AFLW
        roll *= -1
        # Bin values
        bins = np.array(range(-99, 102, 3))
        labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1)
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #10
Source File: lsh.py    From ms_deisotope with Apache License 2.0 6 votes vote down vote up
def discretize(self, span=None):
        if span is None:
            span = self.span
        else:
            span = self.span = max(span, self._guess_span())
        num_bins = power_of_2(int(math.ceil(span / self.epsilon)))
        bins = np.linspace(0, span + self.epsilon, num=num_bins)
        min_mz = self.peak_set[0].mz
        # This ignores the possibility that a peak delta falls outside of span
        self.indices = np.digitize([peak.mz - min_mz for peak in self.peak_set], bins)
        self.binned_signal = np.zeros(num_bins)
        for i, peak in enumerate(self.peak_set):
            # The original implementation just set the bin to exp(intensity) of the last
            # peak to fall in it, but exp() overflows with numbers higher than ~700.
            #
            # The absolute magnitude of the value here doesn't matter, just that it is
            # non-zero?
            self.binned_signal[self.indices[i]] += peak.intensity 
Example #11
Source File: data_manager.py    From westpa with MIT License 6 votes vote down vote up
def _find_multi_iter_group(self, n_iter, master_group_name):
        with self.lock:
            master_group = self.we_h5file[master_group_name]
            master_index = master_group['index'][...]
            set_id = numpy.digitize([n_iter], master_index['iter_valid']) - 1
            group_ref = master_index[set_id]['group_ref']

            # Check if reference is Null
            if not bool(group_ref):
                return None

            # This extra [0] is to work around a bug in h5py
            try:
                group = self.we_h5file[group_ref]
            except AttributeError:
                group = self.we_h5file[group_ref[0]]
            else:
                log.debug('h5py fixed; remove alternate code path')
            log.debug('reference {!r} points to group {!r}'.format(group_ref, group))
            return group 
Example #12
Source File: DistanceUtils.py    From RaptorX-Contact with GNU General Public License v3.0 6 votes vote down vote up
def DiscretizeDistMatrix(distm, bins=None, invalidDistanceSeparated=False):

	assert (bins is not None)

	result = np.digitize(distm, bins) - 1
	if invalidDistanceSeparated:
		## -1 and the maximum distance bin are separated
		np.putmask(result, result == -1, len(bins) )
		labels = range( len(bins) + 1 )
	else:
		## -1 and the maximum distance bin are merged into a single bin
		np.putmask(result, result == -1, len(bins) -1 )
		labels = range( len(bins) )

	return result.astype(np.int32), np.int32(labels), bins

## calculate the natural logarithm of a matrix 
Example #13
Source File: stats.py    From typhon with MIT License 6 votes vote down vote up
def binned_statistic(coords, data, bins, statistic=None):
    """Bin data and calculate statistics on them

    As :func:`scipy.stats.binned_statistic` but faster for simple
    statistics.

    Args:
        coords: 1-dimensional numpy.array with the coordinates that should be
            binned.
        data: A numpy.array on which the statistic should be applied.
        bins: The bins used for the binning.
        statistic: So far only *mean* is implemented.

    Returns:
        A numpy array with statistic results.
    """
    bin_indices = numpy.digitize(coords, bins)
    data_sum = numpy.bincount(bin_indices, weights=data, minlength=bins.size)
    data_number = numpy.bincount(bin_indices, minlength=bins.size)
    return data_sum / data_number 
Example #14
Source File: test_function_base.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_forward(self):
        x = np.arange(-6, 5)
        bins = np.arange(-5, 5)
        assert_array_equal(digitize(x, bins), np.arange(11)) 
Example #15
Source File: cityscapes.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def _class_to_index(self, mask):
        # assert the values
        values = np.unique(mask)
        for value in values:
            assert(value in self._mapping)
        index = np.digitize(mask.ravel(), self._mapping, right=True)
        return self._key[index].reshape(mask.shape) 
Example #16
Source File: process_predictions.py    From causal-text-embeddings with MIT License 5 votes vote down vote up
def stratify_by_value(df, num_bins=10, sort_by='treatment_probability', col_to_add='strata'):
	values = df[sort_by].values
	min_val = values.min()
	max_val = values.max()
	interval = (max_val-min_val)/num_bins
	bins = np.arange(min_val, max_val, step=interval)
	bin_indices = np.digitize(values, bins)
	df[col_to_add] = bin_indices
	return df 
Example #17
Source File: test_function_base.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_random(self):
        x = rand(10)
        bin = np.linspace(x.min(), x.max(), 10)
        assert_(np.all(digitize(x, bin) != 0)) 
Example #18
Source File: test_function_base.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_reverse(self):
        x = np.arange(5, -6, -1)
        bins = np.arange(5, -5, -1)
        assert_array_equal(digitize(x, bins), np.arange(11)) 
Example #19
Source File: test_function_base.py    From Mastering-Elasticsearch-7.0 with MIT License 5 votes vote down vote up
def test_right_basic(self):
        x = [1, 5, 4, 10, 8, 11, 0]
        bins = [1, 5, 10]
        default_answer = [1, 2, 1, 3, 2, 3, 0]
        assert_array_equal(digitize(x, bins), default_answer)
        right_answer = [0, 1, 1, 2, 2, 3, 0]
        assert_array_equal(digitize(x, bins, True), right_answer) 
Example #20
Source File: cityscapes.py    From PyTorch-Encoding with MIT License 5 votes vote down vote up
def make_pred(self, mask):
        values = np.unique(mask)
        for i in range(len(values)):
            assert(values[i] in self._indices)
        index = np.digitize(mask.ravel(), self._indices, right=True)
        return self._classes[index].reshape(mask.shape) 
Example #21
Source File: kdetools.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def counts(x, v):
    """
    Counts the number of elements of x that fall within the grid points v

    Notes
    -----
    Using np.digitize and np.bincount
    """
    idx = np.digitize(x, v)
    try: # numpy 1.6
        return np.bincount(idx, minlength=len(v))
    except:
        bc = np.bincount(idx)
        return np.r_[bc, np.zeros(len(v) - len(bc))] 
Example #22
Source File: test_regression.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_mem_digitize(self):
        # Ticket #95
        for i in range(100):
            np.digitize([1, 2, 3, 4], [1, 3])
            np.digitize([0, 1, 2, 3, 4], [1, 3]) 
Example #23
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        txt_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)
        img_name = self.X_train[index].split('_')[0]

        img = Image.open(os.path.join(self.data_dir, img_name + self.img_ext))
        img = img.convert(self.image_mode)
        txt_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # We get the pose in degrees
        annot = open(txt_path, 'r')
        line = annot.readline().split(' ')
        yaw, pitch, roll = [float(line[1]), float(line[2]), float(line[3])]

        # Crop the face loosely
        k = 0.32
        x1 = float(line[4])
        y1 = float(line[5])
        x2 = float(line[6])
        y2 = float(line[7])
        x1 -= 0.8 * k * abs(x2 - x1)
        y1 -= 2 * k * abs(y2 - y1)
        x2 += 0.8 * k * abs(x2 - x1)
        y2 += 1 * k * abs(y2 - y1)

        img = img.crop((int(x1), int(y1), int(x2), int(y2)))

        # Bin values
        bins = np.array(range(-99, 102, 3))
        labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1)
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #24
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext))
        img = img.convert(self.image_mode)
        mat_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # Crop the face loosely
        pt2d = utils.get_pt2d_from_mat(mat_path)
        x_min = min(pt2d[0,:])
        y_min = min(pt2d[1,:])
        x_max = max(pt2d[0,:])
        y_max = max(pt2d[1,:])

        k = 0.20
        x_min -= 2 * k * abs(x_max - x_min)
        y_min -= 2 * k * abs(y_max - y_min)
        x_max += 2 * k * abs(x_max - x_min)
        y_max += 0.6 * k * abs(y_max - y_min)
        img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        ds = 3  # downsampling factor
        original_size = img.size
        img = img.resize((img.size[0] / ds, img.size[1] / ds), resample=Image.NEAREST)
        img = img.resize((original_size[0], original_size[1]), resample=Image.NEAREST)

        # We get the pose in radians
        pose = utils.get_ypr_from_mat(mat_path)
        # And convert to degrees.
        pitch = pose[0] * 180 / np.pi
        yaw = pose[1] * 180 / np.pi
        roll = pose[2] * 180 / np.pi
        # Bin values
        bins = np.array(range(-99, 102, 3))
        labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1)
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #25
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext))
        img = img.convert(self.image_mode)
        mat_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # Crop the face loosely
        pt2d = utils.get_pt2d_from_mat(mat_path)

        x_min = min(pt2d[0,:])
        y_min = min(pt2d[1,:])
        x_max = max(pt2d[0,:])
        y_max = max(pt2d[1,:])

        k = 0.20
        x_min -= 2 * k * abs(x_max - x_min)
        y_min -= 2 * k * abs(y_max - y_min)
        x_max += 2 * k * abs(x_max - x_min)
        y_max += 0.6 * k * abs(y_max - y_min)
        img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        # We get the pose in radians
        pose = utils.get_ypr_from_mat(mat_path)
        # And convert to degrees.
        pitch = pose[0] * 180 / np.pi
        yaw = pose[1] * 180 / np.pi
        roll = pose[2] * 180 / np.pi
        # Bin values
        bins = np.array(range(-99, 102, 3))
        labels = torch.LongTensor(np.digitize([yaw, pitch, roll], bins) - 1)
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #26
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext))
        img = img.convert(self.image_mode)
        mat_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # Crop the face loosely
        pt2d = utils.get_pt2d_from_mat(mat_path)
        x_min = min(pt2d[0,:])
        y_min = min(pt2d[1,:])
        x_max = max(pt2d[0,:])
        y_max = max(pt2d[1,:])

        # k = 0.2 to 0.40
        k = np.random.random_sample() * 0.2 + 0.2
        x_min -= 0.6 * k * abs(x_max - x_min)
        y_min -= 2 * k * abs(y_max - y_min)
        x_max += 0.6 * k * abs(x_max - x_min)
        y_max += 0.6 * k * abs(y_max - y_min)
        img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        # We get the pose in radians
        pose = utils.get_ypr_from_mat(mat_path)
        pitch = pose[0] * 180 / np.pi
        yaw = pose[1] * 180 / np.pi
        roll = pose[2] * 180 / np.pi

        ds = 1 + np.random.randint(0,4) * 5
        original_size = img.size
        img = img.resize((img.size[0] / ds, img.size[1] / ds), resample=Image.NEAREST)
        img = img.resize((original_size[0], original_size[1]), resample=Image.NEAREST)

        # Flip?
        rnd = np.random.random_sample()
        if rnd < 0.5:
            yaw = -yaw
            roll = -roll
            img = img.transpose(Image.FLIP_LEFT_RIGHT)

        # Blur?
        rnd = np.random.random_sample()
        if rnd < 0.05:
            img = img.filter(ImageFilter.BLUR)

        # Bin values
        bins = np.array(range(-99, 102, 3))
        binned_pose = np.digitize([yaw, pitch, roll], bins) - 1

        # Get target tensors
        labels = binned_pose
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #27
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        img = Image.open(os.path.join(self.data_dir, self.X_train[index] + self.img_ext))
        img = img.convert(self.image_mode)
        mat_path = os.path.join(self.data_dir, self.y_train[index] + self.annot_ext)

        # Crop the face loosely
        pt2d = utils.get_pt2d_from_mat(mat_path)
        x_min = min(pt2d[0,:])
        y_min = min(pt2d[1,:])
        x_max = max(pt2d[0,:])
        y_max = max(pt2d[1,:])

        # k = 0.2 to 0.40
        k = np.random.random_sample() * 0.2 + 0.2
        x_min -= 0.6 * k * abs(x_max - x_min)
        y_min -= 2 * k * abs(y_max - y_min)
        x_max += 0.6 * k * abs(x_max - x_min)
        y_max += 0.6 * k * abs(y_max - y_min)
        img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        # We get the pose in radians
        pose = utils.get_ypr_from_mat(mat_path)
        # And convert to degrees.
        pitch = pose[0] * 180 / np.pi
        yaw = pose[1] * 180 / np.pi
        roll = pose[2] * 180 / np.pi

        # Flip?
        rnd = np.random.random_sample()
        if rnd < 0.5:
            yaw = -yaw
            roll = -roll
            img = img.transpose(Image.FLIP_LEFT_RIGHT)

        # Blur?
        rnd = np.random.random_sample()
        if rnd < 0.05:
            img = img.filter(ImageFilter.BLUR)

        # Bin values
        bins = np.array(range(-99, 102, 3))
        binned_pose = np.digitize([yaw, pitch, roll], bins) - 1

        # Get target tensors
        labels = binned_pose
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #28
Source File: datasets.py    From deep-head-pose with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, index):
        path = os.path.join(self.data_dir, self.X_train.iloc[index]).strip('.jpg') + '.png'
        img = Image.open(path)
        img = img.convert('RGB')

        x_min, y_min, x_max, y_max, yaw, pitch, roll = self.y_train.iloc[index]
        x_min = float(x_min); x_max = float(x_max)
        y_min = float(y_min); y_max = float(y_max)
        yaw = -float(yaw); pitch = float(pitch); roll = float(roll)

        # k = 0.2 to 0.40
        k = np.random.random_sample() * 0.2 + 0.2
        x_min -= 0.6 * k * abs(x_max - x_min)
        y_min -= 2 * k * abs(y_max - y_min)
        x_max += 0.6 * k * abs(x_max - x_min)
        y_max += 0.6 * k * abs(y_max - y_min)

        width, height = img.size
        # Crop the face
        img = img.crop((int(x_min), int(y_min), int(x_max), int(y_max)))

        # Flip?
        rnd = np.random.random_sample()
        if rnd < 0.5:
            yaw = -yaw
            roll = -roll
            img = img.transpose(Image.FLIP_LEFT_RIGHT)

        # Blur?
        rnd = np.random.random_sample()
        if rnd < 0.05:
            img = img.filter(ImageFilter.BLUR)

        # Bin values
        bins = np.array(range(-99, 102, 3))
        binned_pose = np.digitize([yaw, pitch, roll], bins) - 1

        labels = torch.LongTensor(binned_pose)
        cont_labels = torch.FloatTensor([yaw, pitch, roll])

        if self.transform is not None:
            img = self.transform(img)

        return img, labels, cont_labels, self.X_train[index] 
Example #29
Source File: test_regression.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def test_mem_digitize(self, level=rlevel):
        # Ticket #95
        for i in range(100):
            np.digitize([1, 2, 3, 4], [1, 3])
            np.digitize([0, 1, 2, 3, 4], [1, 3]) 
Example #30
Source File: cityscapes_Dataset.py    From Deeplab-v3plus with MIT License 5 votes vote down vote up
def _class_to_index(self, mask):
        # assert the values
        values = np.unique(mask)
        for value in values:
            assert (value in self._mapping)
        index = np.digitize(mask.ravel(), self._mapping, right=True)
        return self._key[index].reshape(mask.shape)