コード例 #1
0
ファイル: utils.py プロジェクト: DevangS/CoralNet
def apply_alleviate(image_id, label_probabilities):
    """
    Apply alleviate to a particular image: auto-accept top machine suggestions
    based on the confidence threshold.

    :param image_id: id of the image.
    :param label_probabilities: the machine's assigned label
           probabilities for each point of the image.
    :return: nothing.
    """
    img = Image.objects.get(id=image_id)
    source = img.source
    robot = source.get_latest_robot()
    alleviate_meta = get_alleviate_meta(robot) 

    if source.alleviate_threshold < 1:
        return
    if (not(alleviate_meta['ok'])):
        return

    if (source.alleviate_threshold == 100):
        # if the user wants 100% alleviation, we set the threhold to 0, meaning that all points will be annotated.
        confidenct_threshold = 0
    else:
        # this is a critical step in the alleviate logic. It translate the alleviate level to a confidence threshold for the classifier.
        # this confidence threshold is between 0 and 1.
        confidenct_threshold = alleviate_meta['score_translate'][source.alleviate_threshold]

    machine_annos = Annotation.objects.filter(image=img, user=get_robot_user())
    alleviate_was_applied = False

    for anno in machine_annos:
        pt_number = anno.point.point_number
        label_scores = label_probabilities[pt_number]
        descending_scores = sorted(label_scores, key=operator.itemgetter('score'), reverse=True)
        top_score = descending_scores[0]['score']
        top_confidence = top_score

        if top_confidence >= confidenct_threshold:
            # Save the annotation under the username Alleviate, so that it's no longer
            # a robot annotation.
            anno.user = get_alleviate_user()
            anno.save()
            alleviate_was_applied = True

    if alleviate_was_applied:
        # Are all points human annotated now?
        all_done = image_annotation_all_done(img)
        # Update image status, if needed
        if all_done:
            img.status.annotatedByHuman = True
            img.status.save()
コード例 #2
0
ファイル: utils.py プロジェクト: DevangS/CoralNet
def image_has_any_human_annotations(image):
    """
    Return True if the image has at least one human-made Annotation.
    Return False otherwise.
    """
    human_annotations = Annotation.objects.filter(image=image).exclude(user=get_robot_user()).exclude(user=get_alleviate_user())
    return human_annotations.count() > 0
コード例 #3
0
ファイル: utils.py プロジェクト: DevangS/CoralNet
def generate_points(img, usesourcemethod=True):
    """
    Generate annotation points for the Image img,
    and delete any points that had previously existed.

    Does nothing if the image already has human annotations,
    because we don't want to delete any human work.
    """

    # If there are any human annotations for this image,
    # abort point generation.
    human_annotations = Annotation.objects.filter(image = img).exclude(user = get_robot_user()).exclude(user = get_alleviate_user())
    if human_annotations:
        return

    # Find the annotation area, expressed in pixels.
    d = AnnotationAreaUtils.db_format_to_numbers(img.metadata.annotation_area)
    annoarea_type = d.pop('type')
    if annoarea_type == AnnotationAreaUtils.TYPE_PERCENTAGES:
        annoarea_dict = AnnotationAreaUtils.percentages_to_pixels(width=img.original_width, height=img.original_height, **d)
    elif annoarea_type == AnnotationAreaUtils.TYPE_PIXELS:
        annoarea_dict = d
    else:
        raise ValueError("Can't generate points with annotation area type '{0}'.".format(annoarea_type))

    # Calculate points.
    if usesourcemethod:
        point_gen_method = img.source.default_point_generation_method
    else:
        point_gen_method = img.point_generation_method
    
    new_points = calculate_points(
        img, annotation_area=annoarea_dict,
        **PointGen.db_to_args_format(point_gen_method)
    )

    # Delete old points for this image, if any.
    old_points = Point.objects.filter(image=img)
    for old_point in old_points:
        old_point.delete()

    # Save the newly calculated points.
    for new_point in new_points:
        Point(row=new_point['row'],
              column=new_point['column'],
              point_number=new_point['point_number'],
              image=img,
        ).save()

    # Update image status.
    # Make sure the image goes through the feature-making step again.
    status = img.status
    status.hasRandomPoints = True
    status.save()