зеркало из
				https://github.com/ssciwr/AMMICO.git
				synced 2025-11-04 07:56:04 +02:00 
			
		
		
		
	* deleted lavis from utils * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fixed test_objects * added 'not gcv' to CI * fixed multimodal search and summary tests * disable doc build on PR for now * restrict ipywidgets version to avoid dummycomm error * limit deepface version * original repositories for retinaface lavis * update gcv test results * update display test outputs * update test env * run all tests * wo xdist to avoid segfault * remove widgets ref * skip long-running tests * skip long * verbose codecov upload * refactor summary test 2 * finish summary test refactor * reduce memory overhead of SummaryDetector * remove VQA models from self * remove VQA models from self * update notebook for changes * update notebook for changes * fixed multimodal search tests * fixed tests in multimodal search after precommit * run all tests * update doc notebook for summary changes * skip long-running multimodal * exclude blip2 from testing --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Inga Ulusoy <inga.ulusoy@uni-heidelberg.de>
		
			
				
	
	
		
			99 строки
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			99 строки
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import os
 | 
						|
import pytest
 | 
						|
from torch import device, cuda
 | 
						|
from lavis.models import load_model_and_preprocess
 | 
						|
import misinformation.summary as sm
 | 
						|
 | 
						|
 | 
						|
IMAGES = ["d755771b-225e-432f-802e-fb8dc850fff7.png", "IMG_2746.png"]
 | 
						|
 | 
						|
SUMMARY_DEVICE = device("cuda" if cuda.is_available() else "cpu")
 | 
						|
 | 
						|
TEST_KWARGS = {
 | 
						|
    "run1": {
 | 
						|
        "name": "blip_caption",
 | 
						|
        "model_type": "base_coco",
 | 
						|
        "is_eval": True,
 | 
						|
        "device": SUMMARY_DEVICE,
 | 
						|
    },
 | 
						|
    "run2": {
 | 
						|
        "name": "blip_caption",
 | 
						|
        "model_type": "base_coco",
 | 
						|
        "is_eval": True,
 | 
						|
        "device": SUMMARY_DEVICE,
 | 
						|
    },
 | 
						|
    "run3": {
 | 
						|
        "name": "blip_caption",
 | 
						|
        "model_type": "large_coco",
 | 
						|
        "is_eval": True,
 | 
						|
        "device": SUMMARY_DEVICE,
 | 
						|
    },
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
@pytest.fixture
 | 
						|
def get_dict(get_path):
 | 
						|
    mydict = {}
 | 
						|
    for img in IMAGES:
 | 
						|
        id_ = os.path.splitext(os.path.basename(img))[0]
 | 
						|
        mydict[id_] = {"filename": get_path + img}
 | 
						|
    return mydict
 | 
						|
 | 
						|
 | 
						|
@pytest.mark.long
 | 
						|
def test_analyse_image(get_dict):
 | 
						|
    reference_results = {
 | 
						|
        "run1": {
 | 
						|
            "d755771b-225e-432f-802e-fb8dc850fff7": "a river running through a city next to tall buildings",
 | 
						|
            "IMG_2746": "a crowd of people standing on top of a tennis court",
 | 
						|
        },
 | 
						|
        "run2": {
 | 
						|
            "d755771b-225e-432f-802e-fb8dc850fff7": "a river running through a city next to tall buildings",
 | 
						|
            "IMG_2746": "a crowd of people standing on top of a tennis court",
 | 
						|
        },
 | 
						|
        "run3": {
 | 
						|
            "d755771b-225e-432f-802e-fb8dc850fff7": "a river running through a town next to tall buildings",
 | 
						|
            "IMG_2746": "a crowd of people standing on top of a track",
 | 
						|
        },
 | 
						|
    }
 | 
						|
    # test three different models
 | 
						|
    for test_run in TEST_KWARGS.keys():
 | 
						|
        summary_model, summary_vis_processors, _ = load_model_and_preprocess(
 | 
						|
            **TEST_KWARGS[test_run]
 | 
						|
        )
 | 
						|
        # run two different images
 | 
						|
        for key in get_dict.keys():
 | 
						|
            get_dict[key] = sm.SummaryDetector(get_dict[key]).analyse_image(
 | 
						|
                summary_model, summary_vis_processors
 | 
						|
            )
 | 
						|
        assert len(get_dict) == 2
 | 
						|
        for key in get_dict.keys():
 | 
						|
            assert len(get_dict[key]["3_non-deterministic summary"]) == 3
 | 
						|
            assert (
 | 
						|
                get_dict[key]["const_image_summary"] == reference_results[test_run][key]
 | 
						|
            )
 | 
						|
        cuda.empty_cache()
 | 
						|
        summary_model = None
 | 
						|
        summary_vis_processors = None
 | 
						|
 | 
						|
 | 
						|
def test_analyse_questions(get_dict):
 | 
						|
    list_of_questions = [
 | 
						|
        "How many persons on the picture?",
 | 
						|
        "What happends on the picture?",
 | 
						|
    ]
 | 
						|
    for key in get_dict:
 | 
						|
        get_dict[key] = sm.SummaryDetector(get_dict[key]).analyse_questions(
 | 
						|
            list_of_questions
 | 
						|
        )
 | 
						|
    assert len(get_dict) == 2
 | 
						|
    list_of_questions_ans = ["2", "100"]
 | 
						|
    list_of_questions_ans2 = ["flood", "festival"]
 | 
						|
    test_answers = []
 | 
						|
    test_answers2 = []
 | 
						|
    for key in get_dict.keys():
 | 
						|
        test_answers.append(get_dict[key][list_of_questions[0]])
 | 
						|
        test_answers2.append(get_dict[key][list_of_questions[1]])
 | 
						|
    assert sorted(test_answers) == sorted(list_of_questions_ans)
 | 
						|
    assert sorted(test_answers2) == sorted(list_of_questions_ans2)
 |