Этот коммит содержится в:
Petr Andriushchenko 2023-03-07 11:04:03 +01:00
родитель 04592825e6
Коммит 949b4b2275

Просмотреть файл

@ -3,35 +3,23 @@ from torch import device, cuda
from lavis.models import load_model_and_preprocess from lavis.models import load_model_and_preprocess
import misinformation.summary as sm import misinformation.summary as sm
TEST_IMAGE_1 = "./test/data/d755771b-225e-432f-802e-fb8dc850fff7.png" images = [
TEST_IMAGE_2 = "./test/data/IMG_2746.png" "./test/data/d755771b-225e-432f-802e-fb8dc850fff7.png",
TEST_IMAGE_3 = "./test/data/IMG_2750.png" "./test/data/IMG_2746.png",
TEST_IMAGE_4 = "./test/data/IMG_2805.png" "./test/data/IMG_2750.png",
TEST_IMAGE_5 = "./test/data/IMG_2806.png" "./test/data/IMG_2805.png",
TEST_IMAGE_6 = "./test/data/IMG_2807.png" "./test/data/IMG_2806.png",
TEST_IMAGE_7 = "./test/data/IMG_2808.png" "./test/data/IMG_2807.png",
TEST_IMAGE_8 = "./test/data/IMG_2809.png" "./test/data/IMG_2808.png",
TEST_IMAGE_9 = "./test/data/IMG_3755.jpg" "./test/data/IMG_2809.png",
TEST_IMAGE_10 = "./test/data/IMG_3756.jpg" "./test/data/IMG_3755.jpg",
TEST_IMAGE_11 = "./test/data/IMG_3757.jpg" "./test/data/IMG_3756.jpg",
TEST_IMAGE_12 = "./test/data/pic1.png" "./test/data/IMG_3757.jpg",
"./test/data/pic1.png",
]
def test_analyse_image(): 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,
]
mydict = {} mydict = {}
for img_path in images: for img_path in images:
id_ = os.path.splitext(os.path.basename(img_path))[0] id_ = os.path.splitext(os.path.basename(img_path))[0]
@ -44,42 +32,23 @@ def test_analyse_image():
for key in keys: for key in keys:
assert len(mydict[key]["3_non-deterministic summary"]) == 3 assert len(mydict[key]["3_non-deterministic summary"]) == 3
assert mydict[keys[0]]["const_image_summary"] == str( const_image_summary_list = [
"a river running through a city next to tall buildings" "a river running through a city next to tall buildings",
) "a crowd of people standing on top of a tennis court",
assert mydict[keys[1]]["const_image_summary"] == str( "a crowd of people standing on top of a field",
"a crowd of people standing on top of a tennis court" "a room with a desk and a chair",
) "a table with plastic containers on top of it",
assert mydict[keys[2]]["const_image_summary"] == str( "a view of a city with mountains in the background",
"a crowd of people standing on top of a field" "a view of a city street from a window",
) "a busy city street with cars and pedestrians",
assert mydict[keys[3]]["const_image_summary"] == str( "a close up of an open book with writing on it",
"a room with a desk and a chair" "a book that is open on a table",
) "a yellow book with green lettering on it",
assert mydict[keys[4]]["const_image_summary"] == str( "a person running on a beach near a rock formation",
"a table with plastic containers on top of it" ]
)
assert mydict[keys[5]]["const_image_summary"] == str( for i in range(len(const_image_summary_list)):
"a view of a city with mountains in the background" assert mydict[keys[i]]["const_image_summary"] == const_image_summary_list[i]
)
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"
)
del sm.SummaryDetector.summary_model, sm.SummaryDetector.summary_vis_processors del sm.SummaryDetector.summary_model, sm.SummaryDetector.summary_vis_processors
cuda.empty_cache() cuda.empty_cache()
@ -97,46 +66,28 @@ def test_analyse_image():
summary_model, summary_vis_processors summary_model, summary_vis_processors
) )
keys = list(mydict.keys()) keys = list(mydict.keys())
assert len(mydict) == 12 assert len(mydict) == 12
for key in keys: for key in keys:
assert len(mydict[key]["3_non-deterministic summary"]) == 3 assert len(mydict[key]["3_non-deterministic summary"]) == 3
assert mydict[keys[0]]["const_image_summary"] == str( const_image_summary_list2 = [
"a river running through a city next to tall buildings" "a river running through a city next to tall buildings",
) "a crowd of people standing on top of a tennis court",
assert mydict[keys[1]]["const_image_summary"] == str( "a crowd of people standing on top of a field",
"a crowd of people standing on top of a tennis court" "a room with a desk and a chair",
) "a table with plastic containers on top of it",
assert mydict[keys[2]]["const_image_summary"] == str( "a view of a city with mountains in the background",
"a crowd of people standing on top of a field" "a view of a city street from a window",
) "a busy city street with cars and pedestrians",
assert mydict[keys[3]]["const_image_summary"] == str( "a close up of an open book with writing on it",
"a room with a desk and a chair" "a book that is open on a table",
) "a yellow book with green lettering on it",
assert mydict[keys[4]]["const_image_summary"] == str( "a person running on a beach near a rock formation",
"a table with plastic containers on top of it" ]
)
assert mydict[keys[5]]["const_image_summary"] == str( for i in range(len(const_image_summary_list2)):
"a view of a city with mountains in the background" assert mydict[keys[i]]["const_image_summary"] == const_image_summary_list2[i]
)
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"
)
del summary_model, summary_vis_processors del summary_model, summary_vis_processors
cuda.empty_cache() cuda.empty_cache()
@ -157,59 +108,26 @@ def test_analyse_image():
for key in keys: for key in keys:
assert len(mydict[key]["3_non-deterministic summary"]) == 3 assert len(mydict[key]["3_non-deterministic summary"]) == 3
assert mydict[keys[0]]["const_image_summary"] == str( const_image_summary_list3 = [
"a river running through a town next to tall buildings" "a river running through a town next to tall buildings",
) "a crowd of people standing on top of a track",
assert mydict[keys[1]]["const_image_summary"] == str( "a group of people standing on top of a track",
"a crowd of people standing on top of a track" "a desk and chair in a small room",
) "a table that has some chairs on top of it",
assert mydict[keys[2]]["const_image_summary"] == str( "a view of a city from a window of a building",
"a group of people standing on top of a track" "a view of a city from a window",
) "a city street filled with lots of traffic",
assert mydict[keys[3]]["const_image_summary"] == str( "an open book with german text on it",
"a desk and chair in a small room" "a close up of a book on a table",
) "a book with a green cover on a table",
assert mydict[keys[4]]["const_image_summary"] == str( "a person running on a beach near the ocean",
"a table that has some chairs on top of it" ]
)
assert mydict[keys[5]]["const_image_summary"] == str( for i in range(len(const_image_summary_list2)):
"a view of a city from a window of a building" assert mydict[keys[i]]["const_image_summary"] == const_image_summary_list3[i]
)
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(): 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,
]
mydict = {} mydict = {}
for img_path in images: for img_path in images:
id_ = os.path.splitext(os.path.basename(img_path))[0] id_ = os.path.splitext(os.path.basename(img_path))[0]
@ -227,28 +145,22 @@ def test_analyse_questions():
keys = list(mydict.keys()) keys = list(mydict.keys())
assert len(mydict) == 12 assert len(mydict) == 12
assert mydict[keys[0]][list_of_questions[0]] == str(2) list_of_questions_ans = [2, 100, "many", 0, 0, "none", "two", 5, 0, 0, 0, 1]
assert mydict[keys[1]][list_of_questions[0]] == str(100)
assert mydict[keys[2]][list_of_questions[0]] == str("many")
assert mydict[keys[3]][list_of_questions[0]] == str(0)
assert mydict[keys[4]][list_of_questions[0]] == str(0)
assert mydict[keys[5]][list_of_questions[0]] == str("none")
assert mydict[keys[6]][list_of_questions[0]] == str("two")
assert mydict[keys[7]][list_of_questions[0]] == str(5)
assert mydict[keys[8]][list_of_questions[0]] == str(0)
assert mydict[keys[9]][list_of_questions[0]] == str(0)
assert mydict[keys[10]][list_of_questions[0]] == str(0)
assert mydict[keys[11]][list_of_questions[0]] == str(1)
assert mydict[keys[0]][list_of_questions[1]] == str("flood") list_of_questions_ans2 = [
assert mydict[keys[1]][list_of_questions[1]] == str("festival") "flood",
assert mydict[keys[2]][list_of_questions[1]] == str("people are flying kites") "festival",
assert mydict[keys[3]][list_of_questions[1]] == str("no one's home") "people are flying kites",
assert mydict[keys[4]][list_of_questions[1]] == str("chair is being moved") "no one's home",
assert mydict[keys[5]][list_of_questions[1]] == str("traffic jam") "chair is being moved",
assert mydict[keys[6]][list_of_questions[1]] == str("day time") "traffic jam",
assert mydict[keys[7]][list_of_questions[1]] == str("traffic jam") "day time",
assert mydict[keys[8]][list_of_questions[1]] == str("nothing") "traffic jam",
assert mydict[keys[9]][list_of_questions[1]] == str("nothing") "nothing",
assert mydict[keys[10]][list_of_questions[1]] == str("nothing") "nothing",
assert mydict[keys[11]][list_of_questions[1]] == str("running") "nothing",
"running",
]
for i in range(len(list_of_questions_ans)):
assert mydict[keys[i]][list_of_questions[1]] == str(list_of_questions_ans2[i])