{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Image summary and visual question answering" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "This notebooks shows how to generate image captions and use the visual question answering with [LAVIS](https://github.com/salesforce/LAVIS). \n", "\n", "The first cell is only run on google colab and installs the [ammico](https://github.com/ssciwr/AMMICO) package.\n", "\n", "After that, we can import `ammico` and read in the files given a folder path." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:57:40.471566Z", "iopub.status.busy": "2023-06-14T17:57:40.470552Z", "iopub.status.idle": "2023-06-14T17:57:40.479784Z", "shell.execute_reply": "2023-06-14T17:57:40.479228Z" } }, "outputs": [], "source": [ "# if running on google colab\n", "# flake8-noqa-cell\n", "import os\n", "\n", "if \"google.colab\" in str(get_ipython()):\n", " # update python version\n", " # install setuptools\n", " # %pip install setuptools==61 -qqq\n", " # install ammico\n", " %pip install git+https://github.com/ssciwr/ammico.git -qqq\n", " # mount google drive for data and API key\n", " from google.colab import drive\n", "\n", " drive.mount(\"/content/drive\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:57:40.482843Z", "iopub.status.busy": "2023-06-14T17:57:40.482198Z", "iopub.status.idle": "2023-06-14T17:57:51.419141Z", "shell.execute_reply": "2023-06-14T17:57:51.418473Z" }, "tags": [] }, "outputs": [], "source": [ "import ammico\n", "from ammico import utils as mutils\n", "from ammico import display as mdisplay\n", "import ammico.summary as sm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:57:51.422490Z", "iopub.status.busy": "2023-06-14T17:57:51.421852Z", "iopub.status.idle": "2023-06-14T17:57:51.426521Z", "shell.execute_reply": "2023-06-14T17:57:51.425961Z" }, "tags": [] }, "outputs": [], "source": [ "# Here you need to provide the path to your google drive folder\n", "# or local folder containing the images\n", "images = mutils.find_files(\n", " path=\"data/\",\n", " limit=10,\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:57:51.429189Z", "iopub.status.busy": "2023-06-14T17:57:51.428852Z", "iopub.status.idle": "2023-06-14T17:57:51.432029Z", "shell.execute_reply": "2023-06-14T17:57:51.431394Z" }, "tags": [] }, "outputs": [], "source": [ "mydict = mutils.initialize_dict(images)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Create captions for images and directly write to csv" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Here you can choose between two models: \"base\" or \"large\". This will generate the caption for each image and directly put the results in a dataframe. This dataframe can be exported as a csv file.\n", "\n", "The results are written into the columns `const_image_summary` - this will always be the same result (as always the same seed will be used). The column `3_non-deterministic summary` displays three different answers generated with different seeds, these are most likely different when you run the analysis again." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:57:51.435313Z", "iopub.status.busy": "2023-06-14T17:57:51.434728Z", "iopub.status.idle": "2023-06-14T17:58:19.346682Z", "shell.execute_reply": "2023-06-14T17:58:19.345925Z" }, "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 people with blue robes are spraying water...
1data/106349S_por.pnga man wearing a face mask while looking at a c...[an image of a television screen that has a ma...
2data/102141_2_eng.pnga collage of images including a corona sign, a...[a hand holding an empty tube and another phot...
\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/106349S_por.png a man wearing a face mask while looking at a c... \n", "2 data/102141_2_eng.png a collage of images including a corona sign, a... \n", "\n", " 3_non-deterministic summary \n", "0 [two people with blue robes are spraying water... \n", "1 [an image of a television screen that has a ma... \n", "2 [a hand holding an empty tube and another phot... " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(10)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Write the csv file:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:58:53.257953Z", "iopub.status.busy": "2023-06-14T17:58:53.257445Z", "iopub.status.idle": "2023-06-14T17:58:53.262544Z", "shell.execute_reply": "2023-06-14T17:58:53.262010Z" } }, "outputs": [], "source": [ "df.to_csv(\"data_out.csv\")" ] }, { "attachments": {}, "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": 10, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:58:53.265362Z", "iopub.status.busy": "2023-06-14T17:58:53.264878Z", "iopub.status.idle": "2023-06-14T17:58:53.311443Z", "shell.execute_reply": "2023-06-14T17:58:53.310745Z" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dash is running on http://127.0.0.1:8055/\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:dash.dash:Dash is running on http://127.0.0.1:8055/\n", "\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify=\"summary\")\n", "analysis_explorer.run_server(port=8055)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Generate answers to free-form questions about images written in natural language. " ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Set the list of questions as a list of strings:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:58:53.314849Z", "iopub.status.busy": "2023-06-14T17:58:53.314379Z", "iopub.status.idle": "2023-06-14T17:58:53.317692Z", "shell.execute_reply": "2023-06-14T17:58:53.317049Z" } }, "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", "]" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Explore the analysis using the interface:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:58:53.320760Z", "iopub.status.busy": "2023-06-14T17:58:53.320175Z", "iopub.status.idle": "2023-06-14T17:58:53.840610Z", "shell.execute_reply": "2023-06-14T17:58:53.839967Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Dash is running on http://127.0.0.1:8055/\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:dash.dash:Dash is running on http://127.0.0.1:8055/\n", "\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "analysis_explorer = mdisplay.AnalysisExplorer(mydict, identify=\"summary\")\n", "analysis_explorer.run_server(port=8055)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Or directly analyze for further processing\n", "Instead of inspecting each of the images, you can also directly carry out the analysis and export the result into a csv. This may take a while depending on how many images you have loaded." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:58:53.844313Z", "iopub.status.busy": "2023-06-14T17:58:53.843678Z", "iopub.status.idle": "2023-06-14T17:59:41.700852Z", "shell.execute_reply": "2023-06-14T17:59:41.700142Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\r", " 0%| | 0.00/1.35G [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", " \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 people with blue robes are spraying water...2noyes
1data/106349S_por.pnga man wearing a face mask while looking at a c...[an image of a television screen that has a ma...1yesyes
2data/102141_2_eng.pnga collage of images including a corona sign, a...[a hand holding an empty tube and another phot...1noyes
\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/106349S_por.png a man wearing a face mask while looking at a c... \n", "2 data/102141_2_eng.png a collage of images including a corona sign, a... \n", "\n", " 3_non-deterministic summary \\\n", "0 [two people with blue robes are spraying water... \n", "1 [an image of a television screen that has a ma... \n", "2 [a hand holding an empty tube and another phot... \n", "\n", " How many persons on the picture? Are there any politicians in the picture? \\\n", "0 2 no \n", "1 1 yes \n", "2 1 no \n", "\n", " Does the picture show something from medicine? \n", "0 yes \n", "1 yes \n", "2 yes " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df2.head(10)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2023-06-14T17:59:41.723130Z", "iopub.status.busy": "2023-06-14T17:59:41.722802Z", "iopub.status.idle": "2023-06-14T17:59:41.727004Z", "shell.execute_reply": "2023-06-14T17:59:41.726511Z" } }, "outputs": [], "source": [ "df2.to_csv(\"data_out2.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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 }