{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image summary and visual question answering" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "This notebooks shows some preliminary work on Image Captioning and Visual question answering with lavis. It is mainly meant to explore its capabilities and to decide on future research directions. We package our code into a `ammico` package that is imported here:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:06:18.706226Z", "iopub.status.busy": "2023-05-16T09:06:18.705880Z", "iopub.status.idle": "2023-05-16T09:06:28.916479Z", "shell.execute_reply": "2023-05-16T09:06:28.915819Z" }, "tags": [] }, "outputs": [], "source": [ "from ammico import utils as mutils\n", "from ammico import display as mdisplay\n", "import ammico.summary as sm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set an image path as input file path." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:06:28.919753Z", "iopub.status.busy": "2023-05-16T09:06:28.919147Z", "iopub.status.idle": "2023-05-16T09:06:28.922900Z", "shell.execute_reply": "2023-05-16T09:06:28.922291Z" }, "tags": [] }, "outputs": [], "source": [ "images = mutils.find_files(\n", " path=\"data/\",\n", " limit=10,\n", ")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:06:28.925831Z", "iopub.status.busy": "2023-05-16T09:06:28.925286Z", "iopub.status.idle": "2023-05-16T09:06:28.928378Z", "shell.execute_reply": "2023-05-16T09:06:28.927805Z" }, "tags": [] }, "outputs": [], "source": [ "mydict = mutils.initialize_dict(images)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create captions for images and directly write to csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here you can choose between two models: \"base\" or \"large\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:06:28.930935Z", "iopub.status.busy": "2023-05-16T09:06:28.930593Z", "iopub.status.idle": "2023-05-16T09:06:58.888508Z", "shell.execute_reply": "2023-05-16T09:06:58.887817Z" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\r", " 0%| | 0.00/2.50G [00:00\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
filenameconst_image_summary3_non-deterministic summary
0data/102730_eng.pngtwo people in blue coats spray disinfection a van[two men in blue coveralls are spraying someth...
1data/102141_2_eng.pnga collage of images including a corona sign, a...[various photos with words on them and picture...
2data/106349S_por.pnga man wearing a face mask while looking at a c...[man in sweater and mask on television holding...
\n", "" ], "text/plain": [ " filename const_image_summary \n", "0 data/102730_eng.png two people in blue coats spray disinfection a van \\\n", "1 data/102141_2_eng.png a collage of images including a corona sign, a... \n", "2 data/106349S_por.png a man wearing a face mask while looking at a c... \n", "\n", " 3_non-deterministic summary \n", "0 [two men in blue coveralls are spraying someth... \n", "1 [various photos with words on them and picture... \n", "2 [man in sweater and mask on television holding... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the csv file:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:07:33.792935Z", "iopub.status.busy": "2023-05-16T09:07:33.792595Z", "iopub.status.idle": "2023-05-16T09:07:33.797229Z", "shell.execute_reply": "2023-05-16T09:07:33.796672Z" }, "tags": [] }, "outputs": [], "source": [ "df.to_csv(\"./data_out.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Manually inspect the summaries\n", "\n", "To check the analysis, you can inspect the analyzed elements here. Loading the results takes a moment, so please be patient. If you are sure of what you are doing.\n", "\n", "`const_image_summary` - the permanent summarys, which does not change from run to run (analyse_image).\n", "\n", "`3_non-deterministic summary` - 3 different summarys examples that change from run to run (analyse_image). " ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:07:33.800641Z", "iopub.status.busy": "2023-05-16T09:07:33.800432Z", "iopub.status.idle": "2023-05-16T09:07:34.603284Z", "shell.execute_reply": "2023-05-16T09:07:34.602604Z" }, "tags": [] }, "outputs": [ { "ename": "AttributeError", "evalue": "module 'ammico.display' has no attribute 'explore_analysis'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mmdisplay\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexplore_analysis\u001b[49m(mydict, identify\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msummary\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: module 'ammico.display' has no attribute 'explore_analysis'" ] } ], "source": [ "mdisplay.explore_analysis(mydict, identify=\"summary\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate answers to free-form questions about images written in natural language. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the list of questions" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:07:34.606284Z", "iopub.status.busy": "2023-05-16T09:07:34.605929Z", "iopub.status.idle": "2023-05-16T09:07:34.609143Z", "shell.execute_reply": "2023-05-16T09:07:34.608529Z" } }, "outputs": [], "source": [ "list_of_questions = [\n", " \"How many persons on the picture?\",\n", " \"Are there any politicians in the picture?\",\n", " \"Does the picture show something from medicine?\",\n", "]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:07:34.611993Z", "iopub.status.busy": "2023-05-16T09:07:34.611437Z", "iopub.status.idle": "2023-05-16T09:08:20.283561Z", "shell.execute_reply": "2023-05-16T09:08:20.282870Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\r", " 0%| | 0.00/1.35G [00:00 1\u001b[0m \u001b[43mmdisplay\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexplore_analysis\u001b[49m(mydict, identify\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msummary\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: module 'ammico.display' has no attribute 'explore_analysis'" ] } ], "source": [ "mdisplay.explore_analysis(mydict, identify=\"summary\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Convert the dictionary of dictionarys into a dictionary with lists:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:08:20.329714Z", "iopub.status.busy": "2023-05-16T09:08:20.329505Z", "iopub.status.idle": "2023-05-16T09:08:20.334084Z", "shell.execute_reply": "2023-05-16T09:08:20.333449Z" } }, "outputs": [], "source": [ "outdict2 = mutils.append_data_to_dict(mydict)\n", "df2 = mutils.dump_df(outdict2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:08:20.336862Z", "iopub.status.busy": "2023-05-16T09:08:20.336646Z", "iopub.status.idle": "2023-05-16T09:08:20.346678Z", "shell.execute_reply": "2023-05-16T09:08:20.346039Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
filenameconst_image_summary3_non-deterministic summaryHow many persons on the picture?Are there any politicians in the picture?Does the picture show something from medicine?
0data/102730_eng.pngtwo people in blue coats spray disinfection a van[two men in blue coveralls are spraying someth...2noyes
1data/102141_2_eng.pnga collage of images including a corona sign, a...[various photos with words on them and picture...1noyes
2data/106349S_por.pnga man wearing a face mask while looking at a c...[man in sweater and mask on television holding...1yesyes
\n", "
" ], "text/plain": [ " filename const_image_summary \n", "0 data/102730_eng.png two people in blue coats spray disinfection a van \\\n", "1 data/102141_2_eng.png a collage of images including a corona sign, a... \n", "2 data/106349S_por.png a man wearing a face mask while looking at a c... \n", "\n", " 3_non-deterministic summary \n", "0 [two men in blue coveralls are spraying someth... \\\n", "1 [various photos with words on them and picture... \n", "2 [man in sweater and mask on television holding... \n", "\n", " How many persons on the picture? Are there any politicians in the picture? \n", "0 2 no \\\n", "1 1 no \n", "2 1 yes \n", "\n", " Does the picture show something from medicine? \n", "0 yes \n", "1 yes \n", "2 yes " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2.head(10)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2023-05-16T09:08:20.349896Z", "iopub.status.busy": "2023-05-16T09:08:20.349671Z", "iopub.status.idle": "2023-05-16T09:08:20.354091Z", "shell.execute_reply": "2023-05-16T09:08:20.353495Z" } }, "outputs": [], "source": [ "df2.to_csv(\"./data_out2.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" }, "vscode": { "interpreter": { "hash": "f1142466f556ab37fe2d38e2897a16796906208adb09fea90ba58bdf8a56f0ba" } } }, "nbformat": 4, "nbformat_minor": 4 }