Ejemplo n.º 1
0
def select_evaluation():
    """Selects the vehicles that will appear in NYC3DCars."""

    nyc3dcars_session = SESSION()
    labeler_session = labeler.SESSION()

    try:
        # Reset photos
        photos = nyc3dcars_session.query(Photo) \
            .options(joinedload('dataset'))

        for photo in photos:
            photo.daynight = None

        # Reset vehicles
        vehicles = nyc3dcars_session.query(Vehicle)
        for vehicle in vehicles:
            nyc3dcars_session.delete(vehicle)

        # Turn photos back on if they have at least 1 final revision
        # pylint: disable-msg=E1101
        photos = labeler_session.query(labeler.Photo) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .options(joinedload('daynights')) \
            .options(joinedload('annotations.flags')) \
            .options(joinedload('annotations')) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.User.trust == True) \
            .distinct()
        # pylint: enable-msg=E1101

        photos = list(photos)

        num_photos = len(photos)

        num_test = 0
        num_train = 0
        num_flagged = 0

        print('Checking for new photos')
        for labeler_photo in photos:
            # Do not consider photos that have been flagged
            num_flags = sum(
                len(annotation.flags)
                for annotation in labeler_photo.annotations
                if annotation.user.trust
            )
            if num_flags > 0:
                num_flagged += 1
                continue

            days = 0
            nights = 0

            for daynight in labeler_photo.daynights:
                if not daynight.user.trust:
                    continue
                if daynight.daynight == 'day':
                    days += 1
                else:
                    nights += 1
            if days + nights == 0:
                print('Need Day/Night for photo: %d' % labeler_photo.id)
                continue

            nyc3dcars_photo = nyc3dcars_session.query(Photo) \
                .filter_by(id=labeler_photo.id) \
                .one()

            if nyc3dcars_photo.test == True:
                num_test += 1
            elif nyc3dcars_photo.test == False:
                num_train += 1
            else:
                if num_train > num_test:
                    print('Test: %d' % labeler_photo.id)
                    nyc3dcars_photo.test = True
                    num_test += 1
                else:
                    print('Train: %d' % labeler_photo.id)
                    nyc3dcars_photo.test = False
                    num_train += 1

            if days > nights:
                nyc3dcars_photo.daynight = 'day'
            else:
                nyc3dcars_photo.daynight = 'night'
        print('New photos done')
        print('%d photos' % num_photos)
        print('%d flagged' % num_flagged)
        print('%d test' % num_test)
        print('%d train' % num_train)

        # pylint: disable-msg=E1101
        good_pids = nyc3dcars_session.query(Photo.id) \
            .filter(Photo.test != None) \
            .all()
        # pylint: enable-msg=E1101

        # get photos with 1 and 2 users
        # pylint: disable-msg=E1101
        photos_one_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) == 1)

        photos_two_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) == 2)

        photos_more_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) > 2)
        for photo in photos_more_user:
            print(photo.id)

        for photo, in photos_one_user:
            vehicles = labeler_session.query(labeler.Vehicle) \
                .select_from(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .options(joinedload('revision')) \
                .options(joinedload('revision.annotation')) \
                .options(joinedload('revision.annotation.user')) \
                .options(joinedload('occlusionrankings')) \
                .options(joinedload('occlusionrankings.occlusion_session')) \
                .options(joinedload('bbox_sessions')) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Annotation.pid == photo) \
                .filter(labeler.Revision.final == True) \
                .distinct()

            for vehicle in vehicles:
                convert_vehicle(nyc3dcars_session, vehicle)

        # get good vehicles for 2 user case
        for photo, in photos_two_user:
            annotations = labeler_session.query(labeler.Annotation) \
                .join(labeler.User) \
                .join(labeler.Revision) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Annotation.pid == photo) \
                .filter(labeler.Revision.final == True) \
                .all()
            assert len(annotations) == 2

            vehicles1 = labeler_session.query(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Revision.aid == annotations[0].id) \
                .filter(labeler.Revision.final == True) \
                .all()

            vehicles2 = labeler_session.query(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Revision.aid == annotations[1].id) \
                .filter(labeler.Revision.final == True) \
                .all()

            if len(vehicles1) > len(vehicles2):
                vehicles = vehicles1
            else:
                vehicles = vehicles2

            for vehicle in vehicles:
                print(vehicle.id)
                convert_vehicle(nyc3dcars_session, vehicle)

        num_vehicles, = nyc3dcars_session.query(
            func.count(Vehicle.id)) \
            .one()

        photo_test, = nyc3dcars_session.query(
            func.count(Photo.id)) \
            .filter(Photo.test == True) \
            .one()

        photo_train, = nyc3dcars_session.query(
            func.count(Photo.id)) \
            .filter(Photo.test == False) \
            .one()
        # pylint: enable-msg=E1101

        print('%d vehicles in dataset' % num_vehicles)
        print('%d images for training' % photo_train)
        print('%d images for testing' % photo_test)

        nyc3dcars_session.commit()
    except:
        nyc3dcars_session.rollback()
        labeler_session.rollback()
        raise
    finally:
        nyc3dcars_session.close()
        labeler_session.close()
Ejemplo n.º 2
0
def select_evaluation():
    """Selects the vehicles that will appear in NYC3DCars."""

    nyc3dcars_session = SESSION()
    labeler_session = labeler.SESSION()

    try:
        # Reset photos
        photos = nyc3dcars_session.query(Photo) \
            .options(joinedload('dataset'))

        for photo in photos:
            photo.daynight = None

        # Reset vehicles
        vehicles = nyc3dcars_session.query(Vehicle)
        for vehicle in vehicles:
            nyc3dcars_session.delete(vehicle)

        # Turn photos back on if they have at least 1 final revision
        # pylint: disable-msg=E1101
        photos = labeler_session.query(labeler.Photo) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .options(joinedload('daynights')) \
            .options(joinedload('annotations.flags')) \
            .options(joinedload('annotations')) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.User.trust == True) \
            .distinct()
        # pylint: enable-msg=E1101

        photos = list(photos)

        num_photos = len(photos)

        num_test = 0
        num_train = 0
        num_flagged = 0

        print('Checking for new photos')
        for labeler_photo in photos:
            # Do not consider photos that have been flagged
            num_flags = sum(
                len(annotation.flags)
                for annotation in labeler_photo.annotations
                if annotation.user.trust)
            if num_flags > 0:
                num_flagged += 1
                continue

            days = 0
            nights = 0

            for daynight in labeler_photo.daynights:
                if not daynight.user.trust:
                    continue
                if daynight.daynight == 'day':
                    days += 1
                else:
                    nights += 1
            if days + nights == 0:
                print('Need Day/Night for photo: %d' % labeler_photo.id)
                continue

            nyc3dcars_photo = nyc3dcars_session.query(Photo) \
                .filter_by(id=labeler_photo.id) \
                .one()

            if nyc3dcars_photo.test == True:
                num_test += 1
            elif nyc3dcars_photo.test == False:
                num_train += 1
            else:
                if num_train > num_test:
                    print('Test: %d' % labeler_photo.id)
                    nyc3dcars_photo.test = True
                    num_test += 1
                else:
                    print('Train: %d' % labeler_photo.id)
                    nyc3dcars_photo.test = False
                    num_train += 1

            if days > nights:
                nyc3dcars_photo.daynight = 'day'
            else:
                nyc3dcars_photo.daynight = 'night'
        print('New photos done')
        print('%d photos' % num_photos)
        print('%d flagged' % num_flagged)
        print('%d test' % num_test)
        print('%d train' % num_train)

        # pylint: disable-msg=E1101
        good_pids = nyc3dcars_session.query(Photo.id) \
            .filter(Photo.test != None) \
            .all()
        # pylint: enable-msg=E1101

        # get photos with 1 and 2 users
        # pylint: disable-msg=E1101
        photos_one_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) == 1)

        photos_two_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) == 2)

        photos_more_user = labeler_session.query(labeler.Photo.id) \
            .select_from(labeler.Photo) \
            .join(labeler.Annotation) \
            .join(labeler.User) \
            .join(labeler.Revision) \
            .filter(labeler.User.trust == True) \
            .filter(labeler.Revision.final == True) \
            .filter(labeler.Photo.id.in_(good_pids)) \
            .group_by(labeler.Photo.id) \
            .having(func.count(labeler.Revision.id) > 2)
        for photo in photos_more_user:
            print(photo.id)

        for photo, in photos_one_user:
            vehicles = labeler_session.query(labeler.Vehicle) \
                .select_from(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .options(joinedload('revision')) \
                .options(joinedload('revision.annotation')) \
                .options(joinedload('revision.annotation.user')) \
                .options(joinedload('occlusionrankings')) \
                .options(joinedload('occlusionrankings.occlusion_session')) \
                .options(joinedload('bbox_sessions')) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Annotation.pid == photo) \
                .filter(labeler.Revision.final == True) \
                .distinct()

            for vehicle in vehicles:
                convert_vehicle(nyc3dcars_session, vehicle)

        # get good vehicles for 2 user case
        for photo, in photos_two_user:
            annotations = labeler_session.query(labeler.Annotation) \
                .join(labeler.User) \
                .join(labeler.Revision) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Annotation.pid == photo) \
                .filter(labeler.Revision.final == True) \
                .all()
            assert len(annotations) == 2

            vehicles1 = labeler_session.query(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Revision.aid == annotations[0].id) \
                .filter(labeler.Revision.final == True) \
                .all()

            vehicles2 = labeler_session.query(labeler.Vehicle) \
                .join(labeler.Revision) \
                .join(labeler.Annotation) \
                .join(labeler.User) \
                .filter(labeler.User.trust == True) \
                .filter(labeler.Revision.aid == annotations[1].id) \
                .filter(labeler.Revision.final == True) \
                .all()

            if len(vehicles1) > len(vehicles2):
                vehicles = vehicles1
            else:
                vehicles = vehicles2

            for vehicle in vehicles:
                print(vehicle.id)
                convert_vehicle(nyc3dcars_session, vehicle)

        num_vehicles, = nyc3dcars_session.query(
            func.count(Vehicle.id)) \
            .one()

        photo_test, = nyc3dcars_session.query(
            func.count(Photo.id)) \
            .filter(Photo.test == True) \
            .one()

        photo_train, = nyc3dcars_session.query(
            func.count(Photo.id)) \
            .filter(Photo.test == False) \
            .one()
        # pylint: enable-msg=E1101

        print('%d vehicles in dataset' % num_vehicles)
        print('%d images for training' % photo_train)
        print('%d images for testing' % photo_test)

        nyc3dcars_session.commit()
    except:
        nyc3dcars_session.rollback()
        labeler_session.rollback()
        raise
    finally:
        nyc3dcars_session.close()
        labeler_session.close()