Example #1
0
 def compute_distances(self, features: List[Dict[str, np.array]]):
     query_feature = features[self.query_im_path]
     pos_feature = features[self.pos_im_path]
     neg_features = [features[path] for path in self.neg_im_paths]
     self.pos_dist = vector_distance(query_feature,
                                     pos_feature,
                                     method=self.distance_method)
     self.neg_dists = np.array(
         [vector_distance(query_feature, f) for f in neg_features])
Example #2
0
def test_vector_distance():
    vec1 = np.array([-1, 0, 1.0])
    vec2 = np.array([1, -6.2, 2])
    assert vector_distance(vec1, vec2, "l2", l2_normalize=False) == approx(
        distance.euclidean(vec1, vec2))
    vec1 = vec1 / np.linalg.norm(vec1, 2)
    vec2 = vec2 / np.linalg.norm(vec2, 2)
    assert vector_distance(vec1, vec2,
                           "l2") == approx(distance.euclidean(vec1, vec2))
Example #3
0
def test_compute_distances():
    query_feature = [-1, 0.2, 2]
    feature_dict = {"a": [0, 3, 1], "b": [-2, -7.2, -3], "c": [1, 2, 3]}
    distances = compute_distances(query_feature, feature_dict)
    assert len(distances) == 3
    assert distances[1][0] == 'b'
    assert vector_distance(query_feature,
                           feature_dict["b"]) == approx(distances[1][1])
Example #4
0
def compare_sig(app_img, device_img):
    start_time = time.time()
    signature_class_1 = None
    signature_class_2 = None
    if app_img:
        signature_class_1 = classify_learn.predict(app_img)[1]
        signature_class_1 = str(signature_class_1.item())
    if device_img:
        signature_class_2 = classify_learn.predict(device_img)[1]
        signature_class_2 = str(signature_class_2.item())
    result = ""
    similarity_score = -1
    if (app_img and device_img):
        if (config['sig_config']['similarity_response'][signature_class_1] !=
                config['sig_config']['success_case'] and
                config['sig_config']['similarity_response'][signature_class_2]
                != config['sig_config']['success_case']):
            result = config['sig_config']['similarity_response'][
                signature_class_1] if config['sig_config'][
                    'similarity_response'][signature_class_1] != config[
                        'sig_config']['success_case'] else config['sig_config'][
                            'similarity_response'][signature_class_2]
        if (result == ""):
            featurizer.features = None
            similarity_learn.predict(app_img)
            app_emb = featurizer.features[0][:]
            assert len(app_emb) > 1
            featurizer.features = None
            similarity_learn.predict(device_img)
            device_emb = featurizer.features[0][:]
            assert len(device_emb) > 1
            featurizer.features = None
            similarity_score = vector_distance(app_emb, device_emb)
            if similarity_score <= config['sig_config']['accept_threshold']:
                result = config['sig_config']['success_case']
            elif similarity_score >= config['sig_config']['deny_threshold']:
                result = config['sig_config']['fail_case']
            else:
                result = config['sig_config']['unknown_case']
    elif app_img or device_img:
        result = app_img and config['sig_config']['similarity_response'][
            signature_class_1] or config['sig_config']['similarity_response'][
                signature_class_2]
    else:
        return {"status": "error", 'description': "no files"}
    print("compare signature  --- %s seconds ---" % (time.time() - start_time))
    return {
        "status":
        result,
        "similarity":
        str(similarity_score),
        "app_class":
        signature_class_1
        and config['sig_config']['classify_response'][signature_class_1] or "",
        "device_class":
        signature_class_2
        and config['sig_config']['classify_response'][signature_class_2] or ""
    }
Example #5
0
def compare_sig(app_img, device_img):
    start_time = time.time()
    signature_class_1 = None
    signature_class_2 = None
    if not isinstance(app_img, NoneType):
        signature_class_1 = classify_image(app_img)
        # signature_class_1 = classify_learn.predict(app_img)[1]
        # signature_class_1 = str(signature_class_1.item())
    if not isinstance(device_img, NoneType):
        signature_class_2 = classify_image(device_img)
        # signature_class_2 = classify_learn.predict(device_img)[1]
        # signature_class_2 = str(signature_class_2.item())
    result = ""
    similarity_score = -1
    if not isinstance(app_img, NoneType) and not isinstance(
            device_img, NoneType):
        if (config["sig_config"]["similarity_response"][signature_class_1] !=
                config["sig_config"]["success_case"] and
                config["sig_config"]["similarity_response"][signature_class_2]
                != config["sig_config"]["success_case"]):
            result = (
                config["sig_config"]["similarity_response"][signature_class_1]
                if
                config["sig_config"]["similarity_response"][signature_class_1]
                != config["sig_config"]["success_case"] else
                config["sig_config"]["similarity_response"][signature_class_2])
        if result == "":
            featurizer.features = None
            similarity_learn.predict(app_img)
            app_emb = featurizer.features[0][:]
            assert len(app_emb) > 1
            featurizer.features = None
            similarity_learn.predict(device_img)
            device_emb = featurizer.features[0][:]
            assert len(device_emb) > 1
            featurizer.features = None
            similarity_score = vector_distance(app_emb, device_emb)
            if similarity_score <= config["sig_config"]["accept_threshold"]:
                result = config["sig_config"]["success_case"]
            elif similarity_score >= config["sig_config"]["deny_threshold"]:
                result = config["sig_config"]["fail_case"]
            else:
                result = config["sig_config"]["unknown_case"]
    elif not isinstance(app_img, NoneType) or not isinstance(
            device_img, NoneType):
        result = (
            app_img
            and config["sig_config"]["similarity_response"][signature_class_1]
            or config["sig_config"]["similarity_response"][signature_class_2])
    else:
        return {"status": "error", "description": "no files"}
    print("compare signature  --- %s seconds ---" % (time.time() - start_time))
    return {
        "status":
        result,
        "similarity":
        str(similarity_score),
        "app_class":
        signature_class_1
        and config["sig_config"]["classify_response"][signature_class_1] or "",
        "device_class":
        signature_class_2
        and config["sig_config"]["classify_response"][signature_class_2] or "",
    }