Skip to content

Commit 9dda955

Browse files
author
Pengchuan Zhang
authored
Merge pull request #159 from microsoft/custom_entity
Custom entity and bug in detection model inference
2 parents a5f302b + 3a5b660 commit 9dda955

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

maskrcnn_benchmark/engine/predictor_glip.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,33 @@ def compute_prediction(self, original_image, original_caption, custom_entity = N
187187
seperation_tokens = " . "
188188
for word in original_caption:
189189

190-
tokens_positive.append([len(caption_string), len(caption_string) + len(word)])
190+
tokens_positive.append([[len(caption_string), len(caption_string) + len(word)]])
191191
caption_string += word
192192
caption_string += seperation_tokens
193193

194194
tokenized = self.tokenizer([caption_string], return_tensors="pt")
195-
tokens_positive = [tokens_positive]
196195

197196
original_caption = caption_string
198197
print(tokens_positive)
199198
else:
200199
tokenized = self.tokenizer([original_caption], return_tensors="pt")
201200
if custom_entity is None:
202201
tokens_positive = self.run_ner(original_caption)
202+
else:
203+
tokens_positive = []
204+
for entity in custom_entity:
205+
if "char_bounds" in entity:
206+
# char bounds provided
207+
tokens_positive.append([list(entity["char_bounds"])])
208+
else:
209+
# online look for char bounds
210+
for i, m in enumerate(re.finditer(entity["span"], original_caption)):
211+
if i >= 1:
212+
print(
213+
f"More than 1 match with the phrase {entity['span']} in {entity['span']}! We only take the first match."
214+
)
215+
break
216+
tokens_positive.append([[m.start(), m.end()]])
203217
print(tokens_positive)
204218
# process positive map
205219
positive_map = create_positive_map(tokenized, tokens_positive)

0 commit comments

Comments
 (0)