python类subtract()的实例源码

vision.py 文件源码 项目:Vision2016 作者: Team3309 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def profile_score(contour, binary):
    """
    Calculate a score based on the "profile" of the target, basically how closely its geometry matches with the expected
    geometry of the goal
    :param contour:
    :param binary:
    :return:
    """
    bounding = cv2.boundingRect(contour)
    pixels = np.zeros((binary.shape[0], binary.shape[1]))
    cv2.drawContours(pixels, [contour], -1, 255, -1)
    col_averages = np.mean(pixels, axis=0)[bounding[0]:bounding[0] + bounding[2]]
    row_averages = np.mean(pixels, axis=1)[bounding[1]:bounding[1] + bounding[3]]
    # normalize to between 0 and 1
    col_averages *= 1.0 / col_averages.max()
    row_averages *= 1.0 / row_averages.max()

    col_diff = np.subtract(col_averages, col_profile(col_averages.shape[0], bounding[2]))
    row_diff = np.subtract(row_averages, row_profile(row_averages.shape[0], bounding[3]))

    # average difference should be close to 0
    avg_diff = np.mean([np.mean(col_diff), np.mean(row_diff)])
    return 100 - (avg_diff * 50)
facenet.py 文件源码 项目:facerecognition 作者: guoxiaolu 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def triplet_loss(anchor, positive, negative, alpha):
    """Calculate the triplet loss according to the FaceNet paper

    Args:
      anchor: the embeddings for the anchor images.
      positive: the embeddings for the positive images.
      negative: the embeddings for the negative images.

    Returns:
      the triplet loss according to the FaceNet paper as a float tensor.
    """
    with tf.variable_scope('triplet_loss'):
        pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
        neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)

        basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
        loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)

    return loss
predict_video_res10.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 54 收藏 0 点赞 0 评论 0
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[11:11+128,11:11+128];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores)
extract_res10.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[11:11+128,11:11+128]
    tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['feature'].data)
  return scores
test_vgg.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores)
test_res10.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[11:11+128,11:11+128];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores)
extract_emotion.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 53 收藏 0 点赞 0 评论 0
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[13:13+224,13:13+224]
    #tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['fc6'].data)
  return scores
predict_video.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 46 收藏 0 点赞 0 评论 0
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores)
test_afew_face_vgg.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def predict(image,the_net):
    inputs = []
    try:
        tmp_input = image
        tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
        tmp_input = tmp_input[13:13+224,13:13+224];
        tmp_input = np.subtract(tmp_input,mean)
        tmp_input = tmp_input.transpose((2, 0, 1))
        tmp_input = np.require(tmp_input, dtype=np.float32)
    except Exception as e:
        raise Exception("Image damaged or illegal file format")
        return
    the_net.blobs['data'].reshape(1, *tmp_input.shape)
    the_net.reshape()
    the_net.blobs['data'].data[...] = tmp_input
    the_net.forward()
    scores = the_net.blobs['prob'].data[0]
    return copy.deepcopy(scores)
extract_emotion_bak.py 文件源码 项目:EmotiW-2017-Audio-video-Emotion-Recognition 作者: xujinchang 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def predict(the_net,image):
  inputs = []
  if not os.path.exists(image):
    raise Exception("Image path not exist")
    return
  try:
    tmp_input = cv2.imread(image)
    tmp_input = cv2.resize(tmp_input,(SIZE,SIZE))
    tmp_input = tmp_input[13:13+224,13:13+224]
    tmp_input = np.subtract(tmp_input,mean)
    tmp_input = tmp_input.transpose((2, 0, 1))
    tmp_input = np.require(tmp_input, dtype=np.float32)
  except Exception as e:
    #raise Exception("Image damaged or illegal file format")
    return None
  the_net.blobs['data'].reshape(1, *tmp_input.shape)
  the_net.reshape()
  the_net.blobs['data'].data[...] = tmp_input
  the_net.forward()
  scores = copy.deepcopy(the_net.blobs['fc6'].data)
  return scores
FileHandler.py 文件源码 项目:Tweaker-3 作者: ChristophSchranz 项目源码 文件源码 阅读 51 收藏 0 点赞 0 评论 0
def rotate_ascii_stl(self, rotation_matrix, content, filename):
        """Rotate the mesh array and save as ASCII STL."""
        mesh = np.array(content, dtype=np.float64)

        # prefix area vector, if not already done (e.g. in STL format)
        if len(mesh[0]) == 3:
            row_number = int(len(content)/3)
            mesh = mesh.reshape(row_number, 3, 3)

        # upgrade numpy with: "pip install numpy --upgrade"
        rotated_content = np.matmul(mesh, rotation_matrix)

        v0 = rotated_content[:, 0, :]
        v1 = rotated_content[:, 1, :]
        v2 = rotated_content[:, 2, :]
        normals = np.cross(np.subtract(v1, v0), np.subtract(v2, v0)) \
            .reshape(int(len(rotated_content)), 1, 3)
        rotated_content = np.hstack((normals, rotated_content))

        tweaked = list("solid %s" % filename)
        tweaked += list(map(self.write_facett, list(rotated_content)))
        tweaked.append("\nendsolid %s\n" % filename)
        tweaked = "".join(tweaked)

        return tweaked
data.py 文件源码 项目:melanoma-transfer 作者: learningtitans 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def load_augment(fname, w, h, aug_params=no_augmentation_params,
                 transform=None, sigma=0.0, color_vec=None):
    """Load augmented image with output shape (w, h).

    Default arguments return non augmented image of shape (w, h).
    To apply a fixed transform (color augmentation) specify transform
    (color_vec).
    To generate a random augmentation specify aug_params and sigma.
    """
    img = load_image(fname)
    img = perturb(img, augmentation_params=aug_params, target_shape=(w, h))
    #if transform is None:
    #    img = perturb(img, augmentation_params=aug_params, target_shape=(w, h))
    #else:
    #    img = perturb_fixed(img, tform_augment=transform, target_shape=(w, h))

    #randString = str(np.random.normal(0,1,1))
    #im = Image.fromarray(img.transpose(1,2,0).astype('uint8'))
    #figName = fname.split("/")[-1]
    #im.save("imgs/"+figName+randString+".jpg")

    np.subtract(img, MEAN[:, np.newaxis, np.newaxis], out=img)
    #np.divide(img, STD[:, np.newaxis, np.newaxis], out=img)
    #img = augment_color(img, sigma=sigma, color_vec=color_vec)
    return img
test_ufunc.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b)
embeddings.py 文件源码 项目:django-corenlp 作者: arunchaganty 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def k_nearest_approx(self, vec, k):
        """Get the k nearest neighbors of a vector (in terms of cosine similarity).

        :param (np.array) vec: query vector
        :param (int) k: number of top neighbors to return

        :return (list[tuple[str, float]]): a list of (word, cosine similarity) pairs, in descending order
        """
        if not hasattr(self, 'lshf'):
            self.lshf = self._init_lsh_forest()

        # TODO(kelvin): make this inner product score, to be consistent with k_nearest
        distances, neighbors = self.lshf.kneighbors(vec, n_neighbors=k, return_distance=True)
        scores = np.subtract(1, distances)
        nbr_score_pairs = self.score_map(np.squeeze(neighbors), np.squeeze(scores))

        return sorted(nbr_score_pairs.items(), key=lambda x: x[1], reverse=True)
embeddings.py 文件源码 项目:django-corenlp 作者: arunchaganty 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def k_nearest_approx(self, vec, k):
        """Get the k nearest neighbors of a vector (in terms of cosine similarity).

        :param (np.array) vec: query vector
        :param (int) k: number of top neighbors to return

        :return (list[tuple[str, float]]): a list of (word, cosine similarity) pairs, in descending order
        """
        if not hasattr(self, 'lshf'):
            self.lshf = self._init_lsh_forest()

        # TODO(kelvin): make this inner product score, to be consistent with k_nearest
        distances, neighbors = self.lshf.kneighbors(vec, n_neighbors=k, return_distance=True)
        scores = np.subtract(1, distances)
        nbr_score_pairs = self.score_map(np.squeeze(neighbors), np.squeeze(scores))

        return sorted(nbr_score_pairs.items(), key=lambda x: x[1], reverse=True)
Optimization.py 文件源码 项目:enei 作者: mariolpantunes 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def gradient_descent(initial_point, gradient, cost_function, alpha=0.001, max_it=100):
    current_point = initial_point
    done = False
    costs = [cost_function(current_point)]
    it = 0
    while not done:
        initial_point = current_point
        #print "Cost = "+str(cost_function(current_point))
        delta = gradient(initial_point)
        #print "Delta: "+str(delta)
        #print "Alpha x Delta: "+str(np.dot(delta, alpha))
        current_point = np.subtract(initial_point, np.dot(delta, alpha))
        #print "Current Point:"+str(current_point)
        costs.append(cost_function(current_point))
        if it > max_it:
            done = True
        it += 1
    print it
    return current_point, costs
samestats.py 文件源码 项目:same-stats-different-graphs 作者: jmatejka 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def is_error_still_ok(df1, df2, decimals=2):
    """Checks to see if the statistics are still within the acceptable bounds

    Args:
        df1 (pd.DataFrame): The original data set
        df2 (pd.DataFrame): The test data set
        decimals (int):     The number of decimals of precision to check

    Returns:
        bool: ``True`` if the maximum error is acceptable, ``False`` otherwise
    """
    r1 = get_values(df1)
    r2 = get_values(df2)

    # check each of the error values to check if they are the same to the
    # correct number of decimals
    r1 = [math.floor(r * 10**decimals) for r in r1]
    r2 = [math.floor(r * 10**decimals) for r in r2]

    # we are good if r1 and r2 have the same numbers
    er = np.subtract(r1, r2)
    er = [abs(n) for n in er]

    return np.max(er) == 0
tools.py 文件源码 项目:quadpy 作者: nschloe 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def get_vol(simplex):
    # Compute the volume via the Cayley-Menger determinant
    # <http://mathworld.wolfram.com/Cayley-MengerDeterminant.html>. One
    # advantage is that it can compute the volume of the simplex indenpendent
    # of the dimension of the space where it's embedded.

    # compute all edge lengths
    edges = numpy.subtract(simplex[:, None], simplex[None, :])
    ei_dot_ej = numpy.einsum('...k,...k->...', edges, edges)

    j = simplex.shape[0] - 1
    a = numpy.empty((j+2, j+2) + ei_dot_ej.shape[2:])
    a[1:, 1:] = ei_dot_ej
    a[0, 1:] = 1.0
    a[1:, 0] = 1.0
    a[0, 0] = 0.0

    a = numpy.moveaxis(a, (0, 1), (-2, -1))
    det = numpy.linalg.det(a)

    vol = numpy.sqrt((-1.0)**(j+1) / 2**j / math.factorial(j)**2 * det)
    return vol
img_augmentation.py 文件源码 项目:lasagne_CNN_framework 作者: woshialex 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def load_augment(fname, w, h, aug_params=no_augmentation_params,
                 transform=None, sigma=0.0, color_vec=None):
    """Load augmented image with output shape (w, h).

    Default arguments return non augmented image of shape (w, h).
    To apply a fixed transform (color augmentation) specify transform
    (color_vec). 
    To generate a random augmentation specify aug_params and sigma.
    """
    img = load_image(fname)
    if transform is None:
        img = perturb(img, augmentation_params=aug_params, target_shape=(w, h))
    else:
        img = perturb_fixed(img, tform_augment=transform, target_shape=(w, h))

    np.subtract(img, MEAN[:, np.newaxis, np.newaxis], out=img)
    np.divide(img, STD[:, np.newaxis, np.newaxis], out=img)
    img = augment_color(img, sigma=sigma, color_vec=color_vec)
    return img
prep.py 文件源码 项目:pylibs 作者: skesiraju 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def standardize_data(data):
    """ Standardize the training data

    X = (X - mu) / (sigma)

    Parameters:
    -----------
    data: numpy ndarray, where rows are data points and cols are dimensions

    Returns:
    --------
    X_std (numpy array) \n
    col_mean (numpy array) \n
    col_std (numpy array)
    """
    col_mean = np.mean(data, axis=0)
    col_std = np.std(data, axis=0)
    sdata = np.subtract(data, col_mean) / col_std

    return sdata, col_mean, col_std
facenet.py 文件源码 项目:faceNet_RealTime 作者: jack55436001 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def triplet_loss(anchor, positive, negative, alpha):
    """Calculate the triplet loss according to the FaceNet paper

    Args:
      anchor: the embeddings for the anchor images.
      positive: the embeddings for the positive images.
      negative: the embeddings for the negative images.

    Returns:
      the triplet loss according to the FaceNet paper as a float tensor.
    """
    with tf.variable_scope('triplet_loss'):
        pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
        neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)

        basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
        loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)

    return loss
planner.py 文件源码 项目:lqRRT 作者: jnez71 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def __init__(self, dynamics, lqr, constraints,
                 horizon, dt=0.05, FPR=0,
                 error_tol=0.05, erf=np.subtract,
                 min_time=0.5, max_time=1, max_nodes=1E5,
                 goal0=None, sys_time=time.time, printing=True):

        self.set_system(dynamics, lqr, constraints, erf)

        self.set_resolution(horizon, dt, FPR, error_tol)

        self.set_runtime(min_time, max_time, max_nodes, sys_time)

        self.set_goal(goal0)

        self.printing = printing
        self.killed = False

#################################################
test_ufunc.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def test_NotImplemented_not_returned(self):
        # See gh-5964 and gh-2091. Some of these functions are not operator
        # related and were fixed for other reasons in the past.
        binary_funcs = [
            np.power, np.add, np.subtract, np.multiply, np.divide,
            np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
            np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
            np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
            np.logical_and, np.logical_or, np.logical_xor, np.maximum,
            np.minimum, np.mod
            ]

        # These functions still return NotImplemented. Will be fixed in
        # future.
        # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]

        a = np.array('1')
        b = 1
        for f in binary_funcs:
            assert_raises(TypeError, f, a, b)
yt_array.py 文件源码 项目:yt 作者: yt-project 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def convert_to_units(self, units):
        """
        Convert the array and units to the given units.

        Parameters
        ----------
        units : Unit object or str
            The units you want to convert to.

        """
        new_units = _unit_repr_check_same(self.units, units)
        (conversion_factor, offset) = self.units.get_conversion_factor(new_units)

        self.units = new_units
        values = self.d
        values *= conversion_factor

        if offset:
            np.subtract(self, offset*self.uq, self)

        return self
population.py 文件源码 项目:GASP-python 作者: henniggroup 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def get_composition_distance(self, comp_vect1, comp_vect2):
        """
        Returns the normalized distance between two composition vectors, which
        is defined as the L1 norm of their difference, divided by 2 to
        normalize (so the maximum distance is 1 and the minimum distance is 0).

        Args:
            comp_vect1: the first composition vector, as a numpy array

            comp_vect2: the second composition vector, as a numpy array
        """

        # compute the different between the two composition vectors
        diff_vector = np.subtract(comp_vect1, comp_vect2)
        # compute the L1 norm of the difference vector
        return 0.5*np.linalg.norm(diff_vector, ord=1)
stockstats.py 文件源码 项目:stockstats 作者: jealous 项目源码 文件源码 阅读 60 收藏 0 点赞 0 评论 0
def _get_boll(cls, df):
        """ Get Bollinger bands.

        boll_ub means the upper band of the Bollinger bands
        boll_lb means the lower band of the Bollinger bands
        boll_ub = MA + K?
        boll_lb = MA ? K?
        M = BOLL_PERIOD
        K = BOLL_STD_TIMES
        :param df: data
        :return: None
        """
        moving_avg = df['close_{}_sma'.format(cls.BOLL_PERIOD)]
        moving_std = df['close_{}_mstd'.format(cls.BOLL_PERIOD)]
        df['boll'] = moving_avg
        moving_avg = list(map(np.float64, moving_avg))
        moving_std = list(map(np.float64, moving_std))
        # noinspection PyTypeChecker
        df['boll_ub'] = np.add(moving_avg,
                               np.multiply(cls.BOLL_STD_TIMES, moving_std))
        # noinspection PyTypeChecker
        df['boll_lb'] = np.subtract(moving_avg,
                                    np.multiply(cls.BOLL_STD_TIMES,
                                                moving_std))
facenet.py 文件源码 项目:icyface_api 作者: bupticybee 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def triplet_loss(anchor, positive, negative, alpha):
    """Calculate the triplet loss according to the FaceNet paper

    Args:
      anchor: the embeddings for the anchor images.
      positive: the embeddings for the positive images.
      negative: the embeddings for the negative images.

    Returns:
      the triplet loss according to the FaceNet paper as a float tensor.
    """
    with tf.variable_scope('triplet_loss'):
        pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1)
        neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1)

        basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
        loss = tf.reduce_mean(tf.maximum(basic_loss, 0.0), 0)

    return loss
source_panel.py 文件源码 项目:CAAPR 作者: Stargrazer82301 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def load_sky_subtraction_to_process_stack(self):
        """
        Load sky subtraction into image processing stack
        If sky image is 3-d ([n,x,y]), then collapse to 2-d ([x,y]) by doing a median on axis=0
        """
        self.unload_sky_subtraction_from_process_stack()
        if self.sky_hdulist is not None:
            if self.sky_hdulist[0].data.ndim == 2:
                process_fxn = ImageProcessAction(np.subtract, self.sky_hdulist[0].data)
            elif self.sky_hdulist[0].data.ndim == 3:
                process_fxn = ImageProcessAction(np.subtract, np.median(self.sky_hdulist[0].data, axis=0))
            else:
                raise UnrecognizedNumberOfDimensions("Tried to load sky image with {} dimensions, " + 
                                                     "when can only handle 2-d or 3-d".format(
                                                     self.sky_hdulist[0].data.ndim))
            # assume that sky subtraction should always be first in processing stack.
            self.ztv_frame.image_process_functions_to_apply.insert(0, ('sky-subtraction', process_fxn))
            wx.CallAfter(pub.sendMessage, 'image-process-functions-to-apply-changed', 
                         msg=(self.ztv_frame._pause_redraw_image,))
            wx.CallAfter(pub.sendMessage, 'set-window-title', msg=None)
        self.sky_checkbox.SetValue(True)
source_panel.py 文件源码 项目:CAAPR 作者: Stargrazer82301 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def load_sky_subtraction_to_process_stack(self):
        """
        Load sky subtraction into image processing stack
        If sky image is 3-d ([n,x,y]), then collapse to 2-d ([x,y]) by doing a median on axis=0
        """
        self.unload_sky_subtraction_from_process_stack()
        if self.sky_hdulist is not None:
            if self.sky_hdulist[0].data.ndim == 2:
                process_fxn = ImageProcessAction(np.subtract, self.sky_hdulist[0].data)
            elif self.sky_hdulist[0].data.ndim == 3:
                process_fxn = ImageProcessAction(np.subtract, np.median(self.sky_hdulist[0].data, axis=0))
            else:
                raise UnrecognizedNumberOfDimensions("Tried to load sky image with {} dimensions, " + 
                                                     "when can only handle 2-d or 3-d".format(
                                                     self.sky_hdulist[0].data.ndim))
            # assume that sky subtraction should always be first in processing stack.
            self.ztv_frame.image_process_functions_to_apply.insert(0, ('sky-subtraction', process_fxn))
            wx.CallAfter(pub.sendMessage, 'image-process-functions-to-apply-changed', 
                         msg=(self.ztv_frame._pause_redraw_image,))
            wx.CallAfter(pub.sendMessage, 'set-window-title', msg=None)
        self.sky_checkbox.SetValue(True)
optimizer.py 文件源码 项目:mpi_learn 作者: duanders 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def apply_update(self, weights, gradient):
        """Update the running averages of gradients and weight updates,
            and compute the Adadelta update for this step."""
        if self.running_g2 is None:
            self.running_g2 = [ np.zeros_like(g) for g in gradient ]
        if self.running_dx2 is None:
            self.running_dx2 = [ np.zeros_like(g) for g in gradient ]

        self.running_g2 = self.running_average_square( self.running_g2, gradient )
        new_weights = []
        updates = []
        for w, g, g2, dx2 in zip(weights, gradient, self.running_g2, self.running_dx2):
            update = np.multiply( np.divide( self.sqrt_plus_epsilon(dx2), self.sqrt_plus_epsilon(g2) ), g )
            new_weights.append( np.subtract( w, update ) )
            updates.append(update)
        self.running_dx2 = self.running_average_square( self.running_dx2, updates )
        return new_weights


问题


面经


文章

微信
公众号

扫码关注公众号