diff --git a/misinformation/test/test_summary.py b/misinformation/test/test_summary.py index 0283d3c..fde1045 100644 --- a/misinformation/test/test_summary.py +++ b/misinformation/test/test_summary.py @@ -17,25 +17,27 @@ TEST_IMAGE_10 = "./test/data/IMG_3756.jpg" TEST_IMAGE_11 = "./test/data/IMG_3757.jpg" TEST_IMAGE_12 = "./test/data/pic1.png" + def test_analyse_image(): - images = [TEST_IMAGE_1, - TEST_IMAGE_2, - TEST_IMAGE_3, - TEST_IMAGE_4, - TEST_IMAGE_5, - TEST_IMAGE_6, - TEST_IMAGE_7, - TEST_IMAGE_8, - TEST_IMAGE_9, - TEST_IMAGE_10, - TEST_IMAGE_11, - TEST_IMAGE_12, + images = [ + TEST_IMAGE_1, + TEST_IMAGE_2, + TEST_IMAGE_3, + TEST_IMAGE_4, + TEST_IMAGE_5, + TEST_IMAGE_6, + TEST_IMAGE_7, + TEST_IMAGE_8, + TEST_IMAGE_9, + TEST_IMAGE_10, + TEST_IMAGE_11, + TEST_IMAGE_12, ] mydict = {} for img_path in images: id_ = os.path.splitext(os.path.basename(img_path))[0] mydict[id_] = {"filename": img_path} - + summary_device = device("cuda" if cuda.is_available() else "cpu") summary_model, summary_vis_processors, _ = load_model_and_preprocess( name="blip_caption", @@ -43,7 +45,7 @@ def test_analyse_image(): is_eval=True, device=summary_device, ) - + for key in mydict: mydict[key] = sm.SummaryDetector(mydict[key]).analyse_image() keys = list(mydict.keys()) @@ -51,18 +53,42 @@ def test_analyse_image(): for key in keys: assert len(mydict[key]["3_non-deterministic summary"]) == 3 - assert mydict[keys[0]]["const_image_summary"] == str("a river running through a city next to tall buildings") - assert mydict[keys[1]]["const_image_summary"] == str("a crowd of people standing on top of a tennis court") - assert mydict[keys[2]]["const_image_summary"] == str("a crowd of people standing on top of a field") - assert mydict[keys[3]]["const_image_summary"] == str("a room with a desk and a chair") - assert mydict[keys[4]]["const_image_summary"] == str("a table with plastic containers on top of it") - assert mydict[keys[5]]["const_image_summary"] == str("a view of a city with mountains in the background") - assert mydict[keys[6]]["const_image_summary"] == str("a view of a city street from a window") - assert mydict[keys[7]]["const_image_summary"] == str("a busy city street with cars and pedestrians") - assert mydict[keys[8]]["const_image_summary"] == str("a close up of an open book with writing on it") - assert mydict[keys[9]]["const_image_summary"] == str("a book that is open on a table") - assert mydict[keys[10]]["const_image_summary"] == str("a yellow book with green lettering on it") - assert mydict[keys[11]]["const_image_summary"] == str("a person running on a beach near a rock formation") + assert mydict[keys[0]]["const_image_summary"] == str( + "a river running through a city next to tall buildings" + ) + assert mydict[keys[1]]["const_image_summary"] == str( + "a crowd of people standing on top of a tennis court" + ) + assert mydict[keys[2]]["const_image_summary"] == str( + "a crowd of people standing on top of a field" + ) + assert mydict[keys[3]]["const_image_summary"] == str( + "a room with a desk and a chair" + ) + assert mydict[keys[4]]["const_image_summary"] == str( + "a table with plastic containers on top of it" + ) + assert mydict[keys[5]]["const_image_summary"] == str( + "a view of a city with mountains in the background" + ) + assert mydict[keys[6]]["const_image_summary"] == str( + "a view of a city street from a window" + ) + assert mydict[keys[7]]["const_image_summary"] == str( + "a busy city street with cars and pedestrians" + ) + assert mydict[keys[8]]["const_image_summary"] == str( + "a close up of an open book with writing on it" + ) + assert mydict[keys[9]]["const_image_summary"] == str( + "a book that is open on a table" + ) + assert mydict[keys[10]]["const_image_summary"] == str( + "a yellow book with green lettering on it" + ) + assert mydict[keys[11]]["const_image_summary"] == str( + "a person running on a beach near a rock formation" + ) for key in mydict: mydict[key] = sm.SummaryDetector(mydict[key]).analyse_image( @@ -73,18 +99,42 @@ def test_analyse_image(): for key in keys: assert len(mydict[key]["3_non-deterministic summary"]) == 3 - assert mydict[keys[0]]["const_image_summary"] == str("a river running through a city next to tall buildings") - assert mydict[keys[1]]["const_image_summary"] == str("a crowd of people standing on top of a tennis court") - assert mydict[keys[2]]["const_image_summary"] == str("a crowd of people standing on top of a field") - assert mydict[keys[3]]["const_image_summary"] == str("a room with a desk and a chair") - assert mydict[keys[4]]["const_image_summary"] == str("a table with plastic containers on top of it") - assert mydict[keys[5]]["const_image_summary"] == str("a view of a city with mountains in the background") - assert mydict[keys[6]]["const_image_summary"] == str("a view of a city street from a window") - assert mydict[keys[7]]["const_image_summary"] == str("a busy city street with cars and pedestrians") - assert mydict[keys[8]]["const_image_summary"] == str("a close up of an open book with writing on it") - assert mydict[keys[9]]["const_image_summary"] == str("a book that is open on a table") - assert mydict[keys[10]]["const_image_summary"] == str("a yellow book with green lettering on it") - assert mydict[keys[11]]["const_image_summary"] == str("a person running on a beach near a rock formation") + assert mydict[keys[0]]["const_image_summary"] == str( + "a river running through a city next to tall buildings" + ) + assert mydict[keys[1]]["const_image_summary"] == str( + "a crowd of people standing on top of a tennis court" + ) + assert mydict[keys[2]]["const_image_summary"] == str( + "a crowd of people standing on top of a field" + ) + assert mydict[keys[3]]["const_image_summary"] == str( + "a room with a desk and a chair" + ) + assert mydict[keys[4]]["const_image_summary"] == str( + "a table with plastic containers on top of it" + ) + assert mydict[keys[5]]["const_image_summary"] == str( + "a view of a city with mountains in the background" + ) + assert mydict[keys[6]]["const_image_summary"] == str( + "a view of a city street from a window" + ) + assert mydict[keys[7]]["const_image_summary"] == str( + "a busy city street with cars and pedestrians" + ) + assert mydict[keys[8]]["const_image_summary"] == str( + "a close up of an open book with writing on it" + ) + assert mydict[keys[9]]["const_image_summary"] == str( + "a book that is open on a table" + ) + assert mydict[keys[10]]["const_image_summary"] == str( + "a yellow book with green lettering on it" + ) + assert mydict[keys[11]]["const_image_summary"] == str( + "a person running on a beach near a rock formation" + ) summary_model, summary_vis_processors, _ = load_model_and_preprocess( name="blip_caption", @@ -101,41 +151,71 @@ def test_analyse_image(): assert len(mydict) == 12 for key in keys: assert len(mydict[key]["3_non-deterministic summary"]) == 3 - - assert mydict[keys[0]]["const_image_summary"] == str("a river running through a town next to tall buildings") - assert mydict[keys[1]]["const_image_summary"] == str("a crowd of people standing on top of a track") - assert mydict[keys[2]]["const_image_summary"] == str("a group of people standing on top of a track") - assert mydict[keys[3]]["const_image_summary"] == str("a desk and chair in a small room") - assert mydict[keys[4]]["const_image_summary"] == str("a table that has some chairs on top of it") - assert mydict[keys[5]]["const_image_summary"] == str("a view of a city from a window of a building") - assert mydict[keys[6]]["const_image_summary"] == str("a view of a city from a window") - assert mydict[keys[7]]["const_image_summary"] == str("a city street filled with lots of traffic") - assert mydict[keys[8]]["const_image_summary"] == str("an open book with german text on it") - assert mydict[keys[9]]["const_image_summary"] == str("a close up of a book on a table") - assert mydict[keys[10]]["const_image_summary"] == str("a book with a green cover on a table") - assert mydict[keys[11]]["const_image_summary"] == str("a person running on a beach near the ocean") + + assert mydict[keys[0]]["const_image_summary"] == str( + "a river running through a town next to tall buildings" + ) + assert mydict[keys[1]]["const_image_summary"] == str( + "a crowd of people standing on top of a track" + ) + assert mydict[keys[2]]["const_image_summary"] == str( + "a group of people standing on top of a track" + ) + assert mydict[keys[3]]["const_image_summary"] == str( + "a desk and chair in a small room" + ) + assert mydict[keys[4]]["const_image_summary"] == str( + "a table that has some chairs on top of it" + ) + assert mydict[keys[5]]["const_image_summary"] == str( + "a view of a city from a window of a building" + ) + assert mydict[keys[6]]["const_image_summary"] == str( + "a view of a city from a window" + ) + assert mydict[keys[7]]["const_image_summary"] == str( + "a city street filled with lots of traffic" + ) + assert mydict[keys[8]]["const_image_summary"] == str( + "an open book with german text on it" + ) + assert mydict[keys[9]]["const_image_summary"] == str( + "a close up of a book on a table" + ) + assert mydict[keys[10]]["const_image_summary"] == str( + "a book with a green cover on a table" + ) + assert mydict[keys[11]]["const_image_summary"] == str( + "a person running on a beach near the ocean" + ) + def test_analyse_questions(): - images = [TEST_IMAGE_1, - TEST_IMAGE_2, - TEST_IMAGE_3, - TEST_IMAGE_4, - TEST_IMAGE_5, - TEST_IMAGE_6, - TEST_IMAGE_7, - TEST_IMAGE_8, - TEST_IMAGE_9, - TEST_IMAGE_10, - TEST_IMAGE_11, - TEST_IMAGE_12, + images = [ + TEST_IMAGE_1, + TEST_IMAGE_2, + TEST_IMAGE_3, + TEST_IMAGE_4, + TEST_IMAGE_5, + TEST_IMAGE_6, + TEST_IMAGE_7, + TEST_IMAGE_8, + TEST_IMAGE_9, + TEST_IMAGE_10, + TEST_IMAGE_11, + TEST_IMAGE_12, ] mydict = {} for img_path in images: id_ = os.path.splitext(os.path.basename(img_path))[0] mydict[id_] = {"filename": img_path} - + summary_device = device("cuda" if cuda.is_available() else "cpu") - (summary_VQA_model, summary_VQA_vis_processors, summary_VQA_txt_processors,) = load_model_and_preprocess( + ( + summary_VQA_model, + summary_VQA_vis_processors, + summary_VQA_txt_processors, + ) = load_model_and_preprocess( name="blip_vqa", model_type="vqav2", is_eval=True, device=summary_device ) list_of_questions = [ @@ -143,11 +223,13 @@ def test_analyse_questions(): "What happends on the picture?", ] for key in mydict: - mydict[key] = sm.SummaryDetector(mydict[key]).analyse_questions(list_of_questions) - + mydict[key] = sm.SummaryDetector(mydict[key]).analyse_questions( + list_of_questions + ) + keys = list(mydict.keys()) assert len(mydict) == 12 - + assert mydict[keys[0]][list_of_questions[0]] == str(2) assert mydict[keys[1]][list_of_questions[0]] == str(100) assert mydict[keys[2]][list_of_questions[0]] == str("many") @@ -173,4 +255,3 @@ def test_analyse_questions(): assert mydict[keys[9]][list_of_questions[1]] == str("nothing") assert mydict[keys[10]][list_of_questions[1]] == str("nothing") assert mydict[keys[11]][list_of_questions[1]] == str("running") -