Skip to content

Commit

Permalink
fix: image captioning (#289)
Browse files Browse the repository at this point in the history
* fix: image captioning

* fix: download model

* fix: reqs
  • Loading branch information
dilyararimovna authored Jan 18, 2023
1 parent 7e8a14b commit 479427b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion assistant_dists/dream_multimodal/pipeline_conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"timeout": 3,
"url": "http://image-captioning:8123/respond"
},
"dialog_formatter": "state_formatters.dp_formatters:image_formatter_service",
"dialog_formatter": "state_formatters.dp_formatters:image_captioning_formatter",
"response_formatter": "state_formatters.dp_formatters:simple_formatter_service",
"state_manager_method": "add_annotation"
}
Expand Down
2 changes: 1 addition & 1 deletion services/image_captioning/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ RUN apt-get install wget -y

RUN mkdir -p /opt/conda/lib/python3.7/site-packages/data/models

RUN gdown 1WBQl0WlzvdctslJyLNgedYpRrWAZC69X -O /opt/conda/lib/python3.7/site-packages/data/models/caption.pt
RUN wget http://files.deeppavlov.ai/dream_data/image_captioning/caption.pt -O /opt/conda/lib/python3.7/site-packages/data/models/caption.pt

COPY . /ofa

Expand Down
3 changes: 2 additions & 1 deletion services/image_captioning/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ sentry-sdk[flask]==0.14.1
healthcheck==1.3.3
jinja2<=3.0.3
Werkzeug<=2.0.3
gdown==4.5.1
gdown==4.5.1
protobuf==3.20.1
10 changes: 6 additions & 4 deletions services/image_captioning/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def apply_half(t):
def respond():
st_time = time.time()

img_paths = request.json.get("text", [])
img_paths = request.json.get("image_paths", [])
captions = []
try:
for img_path in img_paths:
Expand All @@ -140,12 +140,14 @@ def respond():
with torch.no_grad():
caption, scores = eval_step(task, generator, models, sample)

captions.append(caption)
captions.append(caption[0])

except Exception as exc:
logger.exception(exc)
sentry_sdk.capture_exception(exc)
captions = [{}] * len(img_paths)

total_time = time.time() - st_time
logger.info(f"captioning exec time: {total_time:.3f}s")
return jsonify({"caption": captions})
logger.info(f"image-captioning exec time: {total_time:.3f}s")
logger.info(f"image-captioning result: {captions}")
return jsonify(captions)
8 changes: 3 additions & 5 deletions services/image_captioning/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
def test_respond():
url = "http://0.0.0.0:8123/respond"

img_path = ["example.jpg"]
image_paths = ["example.jpg"]

request_data = {"text": img_path}
request_data = {"image_paths": image_paths}
result = requests.post(url, json=request_data).json()
caption = result["caption"][0][0]["caption"]
print(caption)
obligatory_word = "bird"

assert obligatory_word in caption, f"Expected the word '{obligatory_word}' to present in caption"
assert obligatory_word in result[0]["caption"], f"Expected the word '{obligatory_word}' to present in caption"
print("\n", "Success!!!")


Expand Down
4 changes: 4 additions & 0 deletions state_formatters/dp_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,3 +996,7 @@ def context_formatter_dialog(dialog: Dict) -> List[Dict]:
dialog = utils.replace_with_annotated_utterances(dialog, mode="punct_sent")
contexts = [[uttr["text"] for uttr in dialog["utterances"][-num_last_utterances:]]]
return [{"contexts": contexts}]

def image_captioning_formatter(dialog: Dict) -> List[Dict]:
# Used by: image_captioning
return [{"image_paths": [dialog["human_utterances"][-1].get("attributes", {}).get("image")]}]

0 comments on commit 479427b

Please sign in to comment.