Vishakaraj
commited on
Commit
•
24b8876
1
Parent(s):
a567fa4
Delete app.py
Browse files
app.py
DELETED
@@ -1,136 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
# os.system("sudo apt-get update && sudo apt-get install -y git")
|
3 |
-
# os.system("sudo apt-get -y install pybind11-dev")
|
4 |
-
# os.system("git clone https://github.com/facebookresearch/detectron2.git")
|
5 |
-
# os.system("pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html")
|
6 |
-
os.system("cd detectron2 && pip install detectron2-0.6-cp310-cp310-linux_x86_64.whl")
|
7 |
-
# os.system("pip3 install torch torchvision torchaudio")
|
8 |
-
os.system("pip install deepspeed==0.7.0")
|
9 |
-
|
10 |
-
import site
|
11 |
-
from importlib import reload
|
12 |
-
reload(site)
|
13 |
-
|
14 |
-
from PIL import Image
|
15 |
-
from io import BytesIO
|
16 |
-
import argparse
|
17 |
-
import sys
|
18 |
-
import numpy as np
|
19 |
-
import torch
|
20 |
-
import gradio as gr
|
21 |
-
|
22 |
-
from detectron2.config import get_cfg
|
23 |
-
from detectron2.data.detection_utils import read_image
|
24 |
-
from detectron2.utils.logger import setup_logger
|
25 |
-
|
26 |
-
sys.path.insert(0, "third_party/CenterNet2/projects/CenterNet2/")
|
27 |
-
from centernet.config import add_centernet_config
|
28 |
-
from grit.config import add_grit_config
|
29 |
-
|
30 |
-
from grit.predictor import VisualizationDemo
|
31 |
-
|
32 |
-
def get_parser():
|
33 |
-
parser = argparse.ArgumentParser(description="Detectron2 demo for builtin configs")
|
34 |
-
parser.add_argument(
|
35 |
-
"--config-file",
|
36 |
-
default="configs/GRiT_B_DenseCap_ObjectDet.yaml",
|
37 |
-
metavar="FILE",
|
38 |
-
help="path to config file",
|
39 |
-
)
|
40 |
-
parser.add_argument("--cpu", action="store_true", help="Use CPU only.")
|
41 |
-
parser.add_argument(
|
42 |
-
"--confidence-threshold",
|
43 |
-
type=float,
|
44 |
-
default=0.5,
|
45 |
-
help="Minimum score for instance predictions to be shown",
|
46 |
-
)
|
47 |
-
parser.add_argument(
|
48 |
-
"--test-task",
|
49 |
-
type=str,
|
50 |
-
default="",
|
51 |
-
help="Choose a task to have GRiT perform",
|
52 |
-
)
|
53 |
-
parser.add_argument(
|
54 |
-
"--opts",
|
55 |
-
help="Modify config options using the command-line 'KEY VALUE' pairs",
|
56 |
-
default=["MODEL.WEIGHTS", "./models/grit_b_densecap_objectdet.pth"],
|
57 |
-
nargs=argparse.REMAINDER,
|
58 |
-
)
|
59 |
-
return parser
|
60 |
-
|
61 |
-
def setup_cfg(args):
|
62 |
-
cfg = get_cfg()
|
63 |
-
if args.cpu:
|
64 |
-
cfg.MODEL.DEVICE = "cpu"
|
65 |
-
add_centernet_config(cfg)
|
66 |
-
add_grit_config(cfg)
|
67 |
-
cfg.merge_from_file(args.config_file)
|
68 |
-
cfg.merge_from_list(args.opts)
|
69 |
-
# Set score_threshold for builtin models
|
70 |
-
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold
|
71 |
-
cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = (
|
72 |
-
args.confidence_threshold
|
73 |
-
)
|
74 |
-
if args.test_task:
|
75 |
-
cfg.MODEL.TEST_TASK = args.test_task
|
76 |
-
cfg.MODEL.BEAM_SIZE = 1
|
77 |
-
cfg.MODEL.ROI_HEADS.SOFT_NMS_ENABLED = False
|
78 |
-
cfg.USE_ACT_CHECKPOINT = False
|
79 |
-
cfg.freeze()
|
80 |
-
return cfg
|
81 |
-
|
82 |
-
def predict(image_file):
|
83 |
-
image_array = np.array(image_file)[:, :, ::-1] # BGR
|
84 |
-
predictions, visualized_output = dense_captioning_demo.run_on_image(image_array)
|
85 |
-
buffer = BytesIO()
|
86 |
-
visualized_output.fig.savefig(buffer, format='png')
|
87 |
-
buffer.seek(0)
|
88 |
-
detections = {}
|
89 |
-
predictions = predictions["instances"].to(torch.device("cpu"))
|
90 |
-
|
91 |
-
for box, description, score in zip(
|
92 |
-
predictions.pred_boxes,
|
93 |
-
predictions.pred_object_descriptions.data,
|
94 |
-
predictions.scores,
|
95 |
-
):
|
96 |
-
if description not in detections:
|
97 |
-
detections[description] = []
|
98 |
-
detections[description].append(
|
99 |
-
{
|
100 |
-
"xmin": float(box[0]),
|
101 |
-
"ymin": float(box[1]),
|
102 |
-
"xmax": float(box[2]),
|
103 |
-
"ymax": float(box[3]),
|
104 |
-
"score": float(score),
|
105 |
-
}
|
106 |
-
)
|
107 |
-
|
108 |
-
output = {
|
109 |
-
"dense_captioning_results": {
|
110 |
-
"detections": detections,
|
111 |
-
}
|
112 |
-
}
|
113 |
-
|
114 |
-
return Image.open(buffer), output
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
args = get_parser().parse_args()
|
119 |
-
args.test_task = "DenseCap"
|
120 |
-
setup_logger(name="fvcore")
|
121 |
-
logger = setup_logger()
|
122 |
-
logger.info("Arguments: " + str(args))
|
123 |
-
|
124 |
-
cfg = setup_cfg(args)
|
125 |
-
|
126 |
-
dense_captioning_demo = VisualizationDemo(cfg)
|
127 |
-
|
128 |
-
demo = gr.Interface(
|
129 |
-
title="Dense Captioning - GRiT",
|
130 |
-
fn=predict,
|
131 |
-
inputs=gr.Image(type='pil', label="Original Image"),
|
132 |
-
outputs=[gr.Image(type="pil",label="Output Image"), "json"],
|
133 |
-
examples=["example_1.jpg", "example_2.jpg"],
|
134 |
-
)
|
135 |
-
|
136 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|