Spaces:
Build error
Build error
File size: 34,262 Bytes
feac658 0784f23 feac658 0784f23 feac658 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 |
import gc
import multiprocessing as mp
import os
import shutil
import sys
import time
from os import path
import cv2
import torch
from huggingface_hub import hf_hub_download
from PIL import Image
import ape
import detectron2.data.transforms as T
import gradio as gr
from ape.model_zoo import get_config_file
from demo_lazy import get_parser, setup_cfg
from detectron2.config import CfgNode
from detectron2.data.detection_utils import read_image
from detectron2.evaluation.coco_evaluation import instances_to_coco_json
from detectron2.utils.logger import setup_logger
from predictor_lazy import VisualizationDemo
this_dir = path.dirname(path.abspath(__file__))
# os.system("git clone https://github.com/shenyunhang/APE.git")
# os.system("python3.10 -m pip install -e APE/")
example_list = [
[
this_dir + "/examples/Totoro01.png",
# "Sky, Water, Tree, The biggest Chinchilla, The older girl wearing skirt on branch, Grass",
"Girl with hat",
# 0.05,
0.25,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/Totoro01.png",
"Sky, Water, Tree, Chinchilla, Grass, Girl",
0.15,
["semantic segmentation"],
],
[
this_dir + "/examples/199_3946193540.jpg",
"chess piece of horse head",
0.30,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/TheGreatWall.jpg",
"The Great Wall",
0.1,
["semantic segmentation"],
],
[
this_dir + "/examples/Pisa.jpg",
"Pisa",
0.01,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/SolvayConference1927.jpg",
# "Albert Einstein, Madame Curie",
"Madame Curie",
# 0.01,
0.03,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/Transformers.webp",
"Optimus Prime",
0.11,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/Terminator3.jpg",
"Humanoid Robot",
0.10,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/MatrixRevolutionForZion.jpg",
"""machine killer with gun in fighting,
donut with colored granules on the surface,
railings being crossed by horses,
a horse running or jumping,
equestrian rider's helmet,
outdoor dog led by rope,
a dog being touched,
clothed dog,
basketball in hand,
a basketball player with both feet off the ground,
player with basketball in the hand,
spoon on the plate,
coffee cup with coffee,
the nearest dessert to the coffee cup,
the bartender who is mixing wine,
a bartender in a suit,
wine glass with wine,
a person in aprons,
pot with food,
a knife being used to cut vegetables,
striped sofa in the room,
a sofa with pillows on it in the room,
lights on in the room,
an indoor lying pet,
a cat on the sofa,
one pet looking directly at the camera indoors,
a bed with patterns in the room,
the lamp on the table beside the bed,
pillow placed at the head of the bed,
a blackboard full of words in the classroom,
child sitting at desks in the classroom,
a person standing in front of bookshelves in the library,
the table someone is using in the library,
a person who touches books in the library,
a person standing in front of the cake counter,
a square plate full of cakes,
a cake decorated with cream,
hot dog with vegetables,
hot dog with sauce on the surface,
red sausage,
flowerpot with flowers potted inside,
monochrome flowerpot,
a flowerpot filled with black soil,
apple growing on trees,
red complete apple,
apple with a stalk,
a woman brushing her teeth,
toothbrush held by someone,
toilet brush with colored bristles,
a customer whose hair is being cut by barber,
a barber at work,
cloth covering the barber,
shopping cart pushed by people in the supermarket,
shopping cart with people in the supermarket,
shopping cart full of goods,
a child wearing a mask,
refrigerator with fruit,
a drink bottle in the refrigerator,
refrigerator with more than two doors,
a watch placed on a table or cloth,
a watch with three or more watch hands can be seen,
a watch with one or more small dials,
clothes hanger,
a piece of clothing hanging on the hanger,
a piece of clothing worn on plastic models,
leather bag with glossy surface,
backpack,
open package,
a fish held by people,
a person who is fishing with a fishing rod,
a fisherman standing on the shore with his body soaked in water, camera hold on someone's shoulder,
a person being interviewed,
a person with microphone hold in hand,
""",
0.20,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/094_56726435.jpg",
# "donut with colored granules on the surface",
"""donut with colored granules on the surface,
railings being crossed by horses,
a horse running or jumping,
equestrian rider's helmet,
outdoor dog led by rope,
a dog being touched,
clothed dog,
basketball in hand,
a basketball player with both feet off the ground,
player with basketball in the hand,
spoon on the plate,
coffee cup with coffee,
the nearest dessert to the coffee cup,
the bartender who is mixing wine,
a bartender in a suit,
wine glass with wine,
a person in aprons,
pot with food,
a knife being used to cut vegetables,
striped sofa in the room,
a sofa with pillows on it in the room,
lights on in the room,
an indoor lying pet,
a cat on the sofa,
one pet looking directly at the camera indoors,
a bed with patterns in the room,
the lamp on the table beside the bed,
pillow placed at the head of the bed,
a blackboard full of words in the classroom,
a blackboard or whiteboard with something pasted,
child sitting at desks in the classroom,
a person standing in front of bookshelves in the library,
the table someone is using in the library,
a person who touches books in the library,
a person standing in front of the cake counter,
a square plate full of cakes,
a cake decorated with cream,
hot dog with vegetables,
hot dog with sauce on the surface,
red sausage,
flowerpot with flowers potted inside,
monochrome flowerpot,
a flowerpot filled with black soil,
apple growing on trees,
red complete apple,
apple with a stalk,
a woman brushing her teeth,
toothbrush held by someone,
toilet brush with colored bristles,
a customer whose hair is being cut by barber,
a barber at work,
cloth covering the barber,
a plastic toy,
a plush toy,
a humanoid toy,
shopping cart pushed by people in the supermarket,
shopping cart with people in the supermarket,
shopping cart full of goods,
a child wearing a mask,
a mask on face with half a face exposed,
a mask on face with only eyes exposed,
refrigerator with fruit,
a drink bottle in the refrigerator,
refrigerator with more than two doors,
a watch placed on a table or cloth,
a watch with three or more watch hands can be seen,
a watch with one or more small dials,
clothes hanger,
a piece of clothing hanging on the hanger,
a piece of clothing worn on plastic models,
leather bag with glossy surface,
backpack,
open package,
a fish held by people,
a person who is fishing with a fishing rod,
a fisherman standing on the shore with his body soaked in water, camera hold on someone's shoulder,
a person being interviewed,
a person with microphone hold in hand,
""",
0.50,
["object detection", "instance segmentation"],
],
[
this_dir + "/examples/013_438973263.jpg",
# "a male lion with a mane",
"""a male lion with a mane,
railings being crossed by horses,
a horse running or jumping,
equestrian rider's helmet,
outdoor dog led by rope,
a dog being touched,
clothed dog,
basketball in hand,
a basketball player with both feet off the ground,
player with basketball in the hand,
spoon on the plate,
coffee cup with coffee,
the nearest dessert to the coffee cup,
the bartender who is mixing wine,
a bartender in a suit,
wine glass with wine,
a person in aprons,
pot with food,
a knife being used to cut vegetables,
striped sofa in the room,
a sofa with pillows on it in the room,
lights on in the room,
an indoor lying pet,
a cat on the sofa,
one pet looking directly at the camera indoors,
a bed with patterns in the room,
the lamp on the table beside the bed,
pillow placed at the head of the bed,
a blackboard full of words in the classroom,
a blackboard or whiteboard with something pasted,
child sitting at desks in the classroom,
a person standing in front of bookshelves in the library,
the table someone is using in the library,
a person who touches books in the library,
a person standing in front of the cake counter,
a square plate full of cakes,
a cake decorated with cream,
hot dog with vegetables,
hot dog with sauce on the surface,
red sausage,
flowerpot with flowers potted inside,
monochrome flowerpot,
a flowerpot filled with black soil,
apple growing on trees,
red complete apple,
apple with a stalk,
a woman brushing her teeth,
toothbrush held by someone,
toilet brush with colored bristles,
a customer whose hair is being cut by barber,
a barber at work,
cloth covering the barber,
a plastic toy,
a plush toy,
a humanoid toy,
shopping cart pushed by people in the supermarket,
shopping cart with people in the supermarket,
shopping cart full of goods,
a child wearing a mask,
a mask on face with half a face exposed,
a mask on face with only eyes exposed,
refrigerator with fruit,
a drink bottle in the refrigerator,
refrigerator with more than two doors,
a watch placed on a table or cloth,
a watch with three or more watch hands can be seen,
a watch with one or more small dials,
clothes hanger,
a piece of clothing hanging on the hanger,
a piece of clothing worn on plastic models,
leather bag with glossy surface,
backpack,
open package,
a fish held by people,
a person who is fishing with a fishing rod,
a fisherman standing on the shore with his body soaked in water, camera hold on someone's shoulder,
a person being interviewed,
a person with microphone hold in hand,
""",
# 0.25,
0.50,
["object detection", "instance segmentation"],
],
]
ckpt_repo_id = "shenyunhang/APE"
def setup_model(name):
gc.collect()
torch.cuda.empty_cache()
if save_memory:
pass
else:
return
for key, demo in all_demo.items():
if key == name:
demo.predictor.model.to(running_device)
else:
demo.predictor.model.to("cpu")
gc.collect()
torch.cuda.empty_cache()
def run_on_image_A(input_image_path, input_text, score_threshold, output_type):
logger.info("run_on_image")
setup_model("APE_A")
demo = all_demo["APE_A"]
cfg = all_cfg["APE_A"]
demo.predictor.model.model_vision.test_score_thresh = score_threshold
return run_on_image(
input_image_path,
input_text,
output_type,
demo,
cfg,
)
def run_on_image_C(input_image_path, input_text, score_threshold, output_type):
logger.info("run_on_image_C")
setup_model("APE_C")
demo = all_demo["APE_C"]
cfg = all_cfg["APE_C"]
demo.predictor.model.model_vision.test_score_thresh = score_threshold
return run_on_image(
input_image_path,
input_text,
output_type,
demo,
cfg,
)
def run_on_image_D(input_image_path, input_text, score_threshold, output_type):
logger.info("run_on_image_D")
setup_model("APE_D")
demo = all_demo["APE_D"]
cfg = all_cfg["APE_D"]
demo.predictor.model.model_vision.test_score_thresh = score_threshold
return run_on_image(
input_image_path,
input_text,
output_type,
demo,
cfg,
)
def run_on_image_comparison(input_image_path, input_text, score_threshold, output_type):
logger.info("run_on_image_comparison")
r = []
for key in all_demo.keys():
logger.info("run_on_image_comparison {}".format(key))
setup_model(key)
demo = all_demo[key]
cfg = all_cfg[key]
demo.predictor.model.model_vision.test_score_thresh = score_threshold
img, _ = run_on_image(
input_image_path,
input_text,
output_type,
demo,
cfg,
)
r.append(img)
return r
def run_on_image(
input_image_path,
input_text,
output_type,
demo,
cfg,
):
with_box = False
with_mask = False
with_sseg = False
if "object detection" in output_type:
with_box = True
if "instance segmentation" in output_type:
with_mask = True
if "semantic segmentation" in output_type:
with_sseg = True
if isinstance(input_image_path, dict):
input_mask_path = input_image_path["mask"]
input_image_path = input_image_path["image"]
print("input_image_path", input_image_path)
print("input_mask_path", input_mask_path)
else:
input_mask_path = None
print("input_text", input_text)
if isinstance(cfg, CfgNode):
input_format = cfg.INPUT.FORMAT
else:
if "model_vision" in cfg.model:
input_format = cfg.model.model_vision.input_format
else:
input_format = cfg.model.input_format
input_image = read_image(input_image_path, format="BGR")
# img = cv2.imread(input_image_path)
# cv2.imwrite("tmp.jpg", img)
# # input_image = read_image("tmp.jpg", format=input_format)
# input_image = read_image("tmp.jpg", format="BGR")
if input_mask_path is not None:
input_mask = read_image(input_mask_path, "L").squeeze(2)
print("input_mask", input_mask)
print("input_mask", input_mask.shape)
else:
input_mask = None
if not with_box and not with_mask and not with_sseg:
return input_image[:, :, ::-1]
if input_image.shape[0] > 1024 or input_image.shape[1] > 1024:
transform = aug.get_transform(input_image)
input_image = transform.apply_image(input_image)
else:
transform = None
start_time = time.time()
predictions, visualized_output, _, metadata = demo.run_on_image(
input_image,
text_prompt=input_text,
mask_prompt=input_mask,
with_box=with_box,
with_mask=with_mask,
with_sseg=with_sseg,
)
logger.info(
"{} in {:.2f}s".format(
"detected {} instances".format(len(predictions["instances"]))
if "instances" in predictions
else "finished",
time.time() - start_time,
)
)
output_image = visualized_output.get_image()
print("output_image", output_image.shape)
# if input_format == "RGB":
# output_image = output_image[:, :, ::-1]
if transform:
output_image = transform.inverse().apply_image(output_image)
print("output_image", output_image.shape)
output_image = Image.fromarray(output_image)
gc.collect()
torch.cuda.empty_cache()
json_results = instances_to_coco_json(predictions["instances"].to(demo.cpu_device), 0)
for json_result in json_results:
json_result["category_name"] = metadata.thing_classes[json_result["category_id"]]
del json_result["image_id"]
return output_image, json_results
def load_APE_A():
# init_checkpoint= "output2/APE/configs/LVISCOCOCOCOSTUFF_O365_OID_VG/ape_deta/ape_deta_vitl_eva02_lsj_cp_720k_20230504_002019/model_final.pth"
init_checkpoint = "configs/LVISCOCOCOCOSTUFF_O365_OID_VG/ape_deta/ape_deta_vitl_eva02_lsj_cp_720k_20230504_002019/model_final.pth"
init_checkpoint = hf_hub_download(repo_id=ckpt_repo_id, filename=init_checkpoint)
args = get_parser().parse_args()
args.config_file = get_config_file(
"LVISCOCOCOCOSTUFF_O365_OID_VG/ape_deta/ape_deta_vitl_eva02_lsj1024_cp_720k.py"
)
args.confidence_threshold = 0.01
args.opts = [
"train.init_checkpoint='{}'".format(init_checkpoint),
"model.model_language.cache_dir=''",
"model.model_vision.select_box_nums_for_evaluation=500",
"model.model_vision.backbone.net.xattn=False",
"model.model_vision.transformer.encoder.pytorch_attn=True",
"model.model_vision.transformer.decoder.pytorch_attn=True",
]
if running_device == "cpu":
args.opts += [
"model.model_language.dtype='float32'",
]
logger.info("Arguments: " + str(args))
cfg = setup_cfg(args)
cfg.model.model_vision.criterion[0].use_fed_loss = False
cfg.model.model_vision.criterion[2].use_fed_loss = False
cfg.train.device = running_device
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["layers"] = 1
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["fusedLN"] = False
demo = VisualizationDemo(cfg, args=args)
if save_memory:
demo.predictor.model.to("cpu")
# demo.predictor.model.half()
else:
demo.predictor.model.to(running_device)
all_demo["APE_A"] = demo
all_cfg["APE_A"] = cfg
def load_APE_B():
# init_checkpoint= "output2/APE/configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj_cp_1080k_20230702_225418/model_final.pth"
init_checkpoint = "configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj_cp_1080k_20230702_225418/model_final.pth"
init_checkpoint = hf_hub_download(repo_id=ckpt_repo_id, filename=init_checkpoint)
args = get_parser().parse_args()
args.config_file = get_config_file(
"LVISCOCOCOCOSTUFF_O365_OID_VGR_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj1024_cp_1080k.py"
)
args.confidence_threshold = 0.01
args.opts = [
"train.init_checkpoint='{}'".format(init_checkpoint),
"model.model_language.cache_dir=''",
"model.model_vision.select_box_nums_for_evaluation=500",
"model.model_vision.text_feature_bank_reset=True",
"model.model_vision.backbone.net.xattn=False",
"model.model_vision.transformer.encoder.pytorch_attn=True",
"model.model_vision.transformer.decoder.pytorch_attn=True",
]
if running_device == "cpu":
args.opts += [
"model.model_language.dtype='float32'",
]
logger.info("Arguments: " + str(args))
cfg = setup_cfg(args)
cfg.model.model_vision.criterion[0].use_fed_loss = False
cfg.model.model_vision.criterion[2].use_fed_loss = False
cfg.train.device = running_device
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["layers"] = 1
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["fusedLN"] = False
demo = VisualizationDemo(cfg, args=args)
if save_memory:
demo.predictor.model.to("cpu")
# demo.predictor.model.half()
else:
demo.predictor.model.to(running_device)
all_demo["APE_B"] = demo
all_cfg["APE_B"] = cfg
def load_APE_C():
# init_checkpoint= "output2/APE/configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj_cp_1080k_20230702_210950/model_final.pth"
init_checkpoint = "configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj_cp_1080k_20230702_210950/model_final.pth"
init_checkpoint = hf_hub_download(repo_id=ckpt_repo_id, filename=init_checkpoint)
args = get_parser().parse_args()
args.config_file = get_config_file(
"LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO/ape_deta/ape_deta_vitl_eva02_vlf_lsj1024_cp_1080k.py"
)
args.confidence_threshold = 0.01
args.opts = [
"train.init_checkpoint='{}'".format(init_checkpoint),
"model.model_language.cache_dir=''",
"model.model_vision.select_box_nums_for_evaluation=500",
"model.model_vision.text_feature_bank_reset=True",
"model.model_vision.backbone.net.xattn=False",
"model.model_vision.transformer.encoder.pytorch_attn=True",
"model.model_vision.transformer.decoder.pytorch_attn=True",
]
if running_device == "cpu":
args.opts += [
"model.model_language.dtype='float32'",
]
logger.info("Arguments: " + str(args))
cfg = setup_cfg(args)
cfg.model.model_vision.criterion[0].use_fed_loss = False
cfg.model.model_vision.criterion[2].use_fed_loss = False
cfg.train.device = running_device
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["layers"] = 1
ape.modeling.text.eva01_clip.eva_clip._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["fusedLN"] = False
demo = VisualizationDemo(cfg, args=args)
if save_memory:
demo.predictor.model.to("cpu")
# demo.predictor.model.half()
else:
demo.predictor.model.to(running_device)
all_demo["APE_C"] = demo
all_cfg["APE_C"] = cfg
def load_APE_D():
# init_checkpoint= "output2/APE/configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO_GQA_PhraseCut_Flickr30k/ape_deta/ape_deta_vitl_eva02_clip_vlf_lsj1024_cp_16x4_1080k_mdl_20230829_162438/model_final.pth"
init_checkpoint = "configs/LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO_GQA_PhraseCut_Flickr30k/ape_deta/ape_deta_vitl_eva02_clip_vlf_lsj1024_cp_16x4_1080k_mdl_20230829_162438/model_final.pth"
init_checkpoint = hf_hub_download(repo_id=ckpt_repo_id, filename=init_checkpoint)
args = get_parser().parse_args()
args.config_file = get_config_file(
"LVISCOCOCOCOSTUFF_O365_OID_VGR_SA1B_REFCOCO_GQA_PhraseCut_Flickr30k/ape_deta/ape_deta_vitl_eva02_clip_vlf_lsj1024_cp_16x4_1080k.py"
)
args.confidence_threshold = 0.01
args.opts = [
"train.init_checkpoint='{}'".format(init_checkpoint),
"model.model_language.cache_dir=''",
"model.model_vision.select_box_nums_for_evaluation=500",
"model.model_vision.text_feature_bank_reset=True",
"model.model_vision.backbone.net.xattn=False",
"model.model_vision.transformer.encoder.pytorch_attn=True",
"model.model_vision.transformer.decoder.pytorch_attn=True",
]
if running_device == "cpu":
args.opts += [
"model.model_language.dtype='float32'",
]
logger.info("Arguments: " + str(args))
cfg = setup_cfg(args)
cfg.model.model_vision.criterion[0].use_fed_loss = False
cfg.model.model_vision.criterion[2].use_fed_loss = False
cfg.train.device = running_device
ape.modeling.text.eva02_clip.factory._MODEL_CONFIGS[cfg.model.model_language.clip_model][
"vision_cfg"
]["layers"] = 1
demo = VisualizationDemo(cfg, args=args)
if save_memory:
demo.predictor.model.to("cpu")
# demo.predictor.model.half()
else:
demo.predictor.model.to(running_device)
all_demo["APE_D"] = demo
all_cfg["APE_D"] = cfg
def APE_A_tab():
with gr.Tab("APE A"):
with gr.Row(equal_height=False):
with gr.Column(scale=1):
input_image = gr.Image(
sources=["upload"],
type="filepath",
# tool="sketch",
# brush_radius=50,
)
input_text = gr.Textbox(
label="Object Prompt (optional, if not provided, will only find COCO object.)",
info="格式: word1,word2,word3,...",
)
score_threshold = gr.Slider(
label="Score Threshold", minimum=0.01, maximum=1.0, value=0.3, step=0.01
)
output_type = gr.CheckboxGroup(
["object detection", "instance segmentation"],
value=["object detection", "instance segmentation"],
label="Output Type",
info="Which kind of output is displayed?",
).style(item_container=True, container=True)
run_button = gr.Button("Run")
with gr.Column(scale=2):
gallery = gr.Image(
type="pil",
)
example_data = gr.Dataset(
components=[input_image, input_text, score_threshold],
samples=examples,
samples_per_page=5,
)
example_data.click(fn=set_example, inputs=example_data, outputs=example_data.components)
# add_tail_info()
output_json = gr.JSON(label="json results")
run_button.click(
fn=run_on_image,
inputs=[input_image, input_text, score_threshold, output_type],
outputs=[gallery, output_json],
)
def APE_C_tab():
with gr.Tab("APE C"):
with gr.Row(equal_height=False):
with gr.Column(scale=1):
input_image = gr.Image(
sources=["upload"],
type="filepath",
# tool="sketch",
# brush_radius=50,
)
input_text = gr.Textbox(
label="Object Prompt (optional, if not provided, will only find COCO object.)",
info="格式: word1,word2,sentence1,sentence2,...",
)
score_threshold = gr.Slider(
label="Score Threshold", minimum=0.01, maximum=1.0, value=0.3, step=0.01
)
output_type = gr.CheckboxGroup(
["object detection", "instance segmentation", "semantic segmentation"],
value=["object detection", "instance segmentation"],
label="Output Type",
info="Which kind of output is displayed?",
).style(item_container=True, container=True)
run_button = gr.Button("Run")
with gr.Column(scale=2):
gallery = gr.Image(
type="pil",
)
example_data = gr.Dataset(
components=[input_image, input_text, score_threshold],
samples=example_list,
samples_per_page=5,
)
example_data.click(fn=set_example, inputs=example_data, outputs=example_data.components)
# add_tail_info()
output_json = gr.JSON(label="json results")
run_button.click(
fn=run_on_image_C,
inputs=[input_image, input_text, score_threshold, output_type],
outputs=[gallery, output_json],
)
def APE_D_tab():
with gr.Tab("APE D"):
with gr.Row(equal_height=False):
with gr.Column(scale=1):
input_image = gr.Image(
sources=["upload"],
type="filepath",
# tool="sketch",
# brush_radius=50,
)
input_text = gr.Textbox(
label="Object Prompt (optional, if not provided, will only find COCO object.)",
info="格式: word1,word2,sentence1,sentence2,...",
)
score_threshold = gr.Slider(
label="Score Threshold", minimum=0.01, maximum=1.0, value=0.1, step=0.01
)
output_type = gr.CheckboxGroup(
["object detection", "instance segmentation", "semantic segmentation"],
value=["object detection", "instance segmentation"],
label="Output Type",
info="Which kind of output is displayed?",
)
run_button = gr.Button("Run")
with gr.Column(scale=2):
gallery = gr.Image(
type="pil",
)
gr.Examples(
examples=example_list,
inputs=[input_image, input_text, score_threshold, output_type],
examples_per_page=20,
)
# add_tail_info()
output_json = gr.JSON(label="json results")
run_button.click(
fn=run_on_image_D,
inputs=[input_image, input_text, score_threshold, output_type],
outputs=[gallery, output_json],
)
def comparison_tab():
with gr.Tab("APE all"):
with gr.Row(equal_height=False):
with gr.Column(scale=1):
input_image = gr.Image(
sources=["upload"],
type="filepath",
# tool="sketch",
# brush_radius=50,
)
input_text = gr.Textbox(
label="Object Prompt (optional, if not provided, will only find COCO object.)",
info="格式: word1,word2,sentence1,sentence2,...",
)
score_threshold = gr.Slider(
label="Score Threshold", minimum=0.01, maximum=1.0, value=0.1, step=0.01
)
output_type = gr.CheckboxGroup(
["object detection", "instance segmentation", "semantic segmentation"],
value=["object detection", "instance segmentation"],
label="Output Type",
info="Which kind of output is displayed?",
)
run_button = gr.Button("Run")
gallery_all = []
with gr.Column(scale=2):
for key in all_demo.keys():
gallery = gr.Image(
label=key,
type="pil",
)
gallery_all.append(gallery)
gr.Examples(
examples=example_list,
inputs=[input_image, input_text, score_threshold, output_type],
examples_per_page=20,
)
# add_tail_info()
run_button.click(
fn=run_on_image_comparison,
inputs=[input_image, input_text, score_threshold, output_type],
outputs=gallery_all,
)
def is_port_in_use(port: int) -> bool:
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(("localhost", port)) == 0
def add_head_info(max_available_memory):
gr.Markdown(
"# APE: Aligning and Prompting Everything All at Once for Universal Visual Perception"
)
if max_available_memory:
gr.Markdown(
"Note multiple models are deployed on single GPU, so it may take several minutes to run the models and visualize the results."
)
else:
gr.Markdown(
"Note multiple models are deployed on CPU, so it may take a while to run the models and visualize the results."
)
gr.Markdown(
"Noted results computed by CPU are slightly different to results computed by GPU, and some libraries are disabled on CPU."
)
gr.Markdown(
"If the demo is out of memory, try to ***decrease*** the number of object prompt and ***increase*** score threshold."
)
gr.Markdown("---")
def add_tail_info():
gr.Markdown("---")
gr.Markdown("### We also support Prompt")
gr.Markdown(
"""
| Location prompt | result | Location prompt | result |
| ---- | ---- | ---- | ---- |
| ![Location prompt](/file=examples/prompt/20230627-131346_11.176.20.67_mask.PNG) | ![结果](/file=examples/prompt/20230627-131346_11.176.20.67_pred.png) | ![Location prompt](/file=examples/prompt/20230627-131530_11.176.20.67_mask.PNG) | ![结果](/file=examples/prompt/20230627-131530_11.176.20.67_pred.png) |
| ![Location prompt](/file=examples/prompt/20230627-131520_11.176.20.67_mask.PNG) | ![结果](/file=examples/prompt/20230627-131520_11.176.20.67_pred.png) | ![Location prompt](/file=examples/prompt/20230627-114219_11.176.20.67_mask.PNG) | ![结果](/file=examples/prompt/20230627-114219_11.176.20.67_pred.png) |
"""
)
gr.Markdown("---")
if __name__ == "__main__":
available_port = [80, 8080]
for port in available_port:
if is_port_in_use(port):
continue
else:
server_port = port
break
print("server_port", server_port)
available_memory = [
torch.cuda.mem_get_info(i)[0] / 1024**3 for i in range(torch.cuda.device_count())
]
global running_device
if len(available_memory) > 0:
max_available_memory = max(available_memory)
device_id = available_memory.index(max_available_memory)
running_device = "cuda:" + str(device_id)
else:
max_available_memory = 0
running_device = "cpu"
global save_memory
save_memory = False
if max_available_memory > 0 and max_available_memory < 40:
save_memory = True
print("available_memory", available_memory)
print("max_available_memory", max_available_memory)
print("running_device", running_device)
print("save_memory", save_memory)
# ==========================================================================================
mp.set_start_method("spawn", force=True)
setup_logger(name="fvcore")
setup_logger(name="ape")
global logger
logger = setup_logger()
global aug
aug = T.ResizeShortestEdge([1024, 1024], 1024)
global all_demo
all_demo = {}
all_cfg = {}
# load_APE_A()
# load_APE_B()
# load_APE_C()
save_memory = False
load_APE_D()
title = "APE: Aligning and Prompting Everything All at Once for Universal Visual Perception"
block = gr.Blocks(title=title).queue()
with block:
add_head_info(max_available_memory)
# APE_A_tab()
# APE_C_tab()
APE_D_tab()
comparison_tab()
# add_tail_info()
block.launch(
share=False,
# server_name="0.0.0.0",
# server_port=server_port,
show_api=False,
show_error=True,
)
|