{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"font-family:comfortaa;font-size:30px;color:crimson;\">PIX2STRUCT : A GENERAL PURPOSE GENERATIVE MODEL FOR VISUAL LANGUAGE UNDERSTANDING</h1>","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading1\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">A BRIEF ON GENERATIVE MODELS</h1>\n<pre>\n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">Though research into this field has been decades old, the AI world sat up and took notice with the advent of Generative Adversarial Networks(GANs).<span style=\"font-family:comfortaa;font-size:18px;\">Then along came, the transformers which allowed for ever larger models to be developed, first focussing on handling text and then moving on to combine other modalities like vision-text, text-image, text-speech and so on.\n\n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">Generative models gave the now famous (or infamous depending on how optimistic or pesimmistic one is!) ChatGPT and its variants, generally known as Large Language Models (LLMs). They are nowadays used to generate codes, for language translations, Image captioning, Image generation using text descriptions and for many more applications. New and exiting research is being published on this space at a breakneck speed.</span>\n\n\n\n","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading2\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">VISUAL LANGUAGE UNDERSTANDING</h1>\n<pre>    \n<span style=\"font-family:comfortaa;font-size:18px;;color:black\">Visual language understanding (VLU) is the capacity to interpret the relationship between visual content (like images or videos) and its natural language annotations. The basic approach is to employ Computer Vision techniques to extract semantically meaningful information (embeddings) from the image/video and then condition the Natural Language Processing (NLP) techniques on these embeddings, to predict the textual content contained in the data or which is to be inferred from the data.\n    \n<span style=\"font-family:comfortaa;font-size:18px;;color:black\">Various objectives like Visual Question Answering (VQA), Image Captioning, Visual Storytelling etc. can be covered under this domain","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading3\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">THE MOTIVATION BEHIND PIX2STRUCT</h1>\n<pre>    \n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">Authors noted that previous research was usually focussed on how language and vision work separately in tasks like answering questions about images or predicting captions for images.  But in real-life situations, language and vision are closely intertwined, say when we read documents, look at tables, infographics, or use user interfaces, both text and images are consumed together without clear separations between them. Hence they concluded that focus rather should be on understanding how language and vision work together in situations where text and images interact seamlessly. Tackling this requires the ability to recognize text, understand language, and incorporate diverse visual context at the same time.\n    \n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">Also previous attempts at decoding visually situated language relied on external and complex tools like OCR engines for document understanding, platform specific metadata for User-Input models etc., which were task specific and could not be scaled and generalized to other tasks. These methods also increased the complexity and computational cost.\n    \n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">The development of an OCR-free, end-to-end model for document understanding (OCR-free Document Understanding Transformer (DONUT): https://arxiv.org/abs/2111.15664) was the spark which inspired their work, since DONUT was limited in its scope of understanding only text from the images and leaving out much of the rich knowledge that unsupervised data brought to the table.\n    \n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">PIX2STRUCT started off from where DONUT drew its line. The idea was to engineer a general purpose model which could be fine-tuned to various task specific domains without much of a fuss.","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading4\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\"> SO, WHAT IS PIX2STRUCT?</h1>\n<pre> \n<span style=\"font-family:comfortaa;font-size:18px;color:black\">It is a pretrained model which uses a vision-encoder, text-decoder architecture. The inputs to the model are raw pixel only images, which are then encoded into embeddings using Vision-Transformer(ViT) and the text-decoder is conditioned on these embeddings to generate sequence of tokens. We shall look into how the authors approached the problem to deliver an end-to-end general purpose model for visual language understanding in the following paragraphs:</span>\n\n\n<span id=\"artists0\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">CHOICE OF DATA AND MODEL OBJECTIVE</span>\n\n<span style=\"font-family:comfortaa;font-size:18px;color:black\">The authors chose to pretrain the model on masked screenshots of web images, since the data is readily and abundantly available on the net. They formulated the objective of predicting the HTML parse from the masked images for the model training. They reasoned that HTML structure provides clean and structured information about the layouts, texts and images in the visual data and that masking out certain portion of the images helps the model predict the joint probability of their co-occurance.</span>\n    \n<span id=\"artists\" style=\"font-family:comfortaa;font-size:18px;color:black\">In order to balance the trade-off between decoder sequence-length and preserving semantics of web page image input, both the screenshots and its HTML ground truth were modified such that it resulted in dense learning of signals for the model during pretraining. This was accomplised by:  </span>\n<ul><li style=\"font-size:15px;\">Only HTML elements or descendants that contribute to the visible content of the webpage are retained</li>   \n<li style=\"font-size:15px;\">In the event an element without visible content has only a single child element, the child is replaced with a grand-child element</li>\n</ul>\n    \n\n<center><img src=\"https://i.postimg.cc/2yzMBKbm/pix2struct1.png\"  style=\"horizontal-align:middle\"></center><p style=\"font-size:15px;\"><center><b>Figure.1: Original web page (left) - Masked input- HTML parse output pairs (right) sampled from original</b></center></p><p style=\"font-size:15px;\"><center>(Image by original authors)</center></p>\n    \n<blockquote style=\"font-family:comfortaa;font-size:18px;color:red;\">The authors kept only the text, alt-text and filenames in each node. You can see it in the ground truth HTML parse in the output pair of the above figure. They note that further enrichment with element-tags, urls, titles and styles etc. is possible</blockquote>\n    \n<span id=\"artists1\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">WHAT DOES THIS OBJECTIVE INTENDS TO ACHIEVE?\n<p style=\"font-family:comfortaa;font-size:18px;color:black\">The authors cleverly crafted this objective to imbibe various facets of Visual Language Understanding into an umbrella model during pre-training, which can then be fine-tuned to further special tasks at hand. </p>\n<ul><li  style=\"font-family:comfortaa;font-size:18px;color:black\">Re-generating visible (unmasked) parts of web-page screenshot subsumes the role of Optical Character Recognition (OCR). Predicting C++ the unmasked portion of image in Figure.1 illustrates this.</li>\n<li  style=\"font-family:comfortaa;font-size:18px;color:black\">Predicting the masked parts of the image is akin to Masked Language Modelling (MLM). Predicting Python from masked portion of image in Figure.1 illustrates this point</li>\n<li style=\"font-family:comfortaa;font-size:18px;color:black\">Recovering the alt-text of the images attends to image-captioning and is a common pretraining strategy for the same. Predicting img alt=C++ illustrates this learning signal.</li> </ul>\n\n\n<span id=\"artists2\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">ARCHITECTURE</span>\n\n<span id = \"artists3\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Pix2Struct uses an Encoder-Decoder architecture. The traditional Vision-Transformer (ViT) acts as the Vision Encoder. But ViT pre-processes the image to fixed resolution/size (usually 224 x 224) before extracting fixed-size patches and messes up the aspect-ratio of the images and also rendering the leverage of model on tasks employing higher resolution images useless. </span>\n\n<span id = \"artists4\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Herein comes the secret sauce employed by Pix2Struct for its Vision-Encoder:</span><ul>\n<li style=\"font-family:comfortaa;font-size:18px;color:black;\">Rescaling the image flexibly to extract maximal number of fixed size patches and more critically</li>\n<li style=\"font-family:comfortaa;font-size:18px;color:black;\">Use 2-dimensional absolute positional embeddings  for input patches.</li>\n\n<center><img src=\"https://i.postimg.cc/8cN1vmqM/pix2struct2.png\"  style=\"horizontal-align:middle\"></center><p style=\"font-size:15px;\"><b>Figure.2: Illustration of patch extraction for target sequence length of 36 patches using Pix2Struct (left) & ViT (right).</b></p><p style=\"font-size:15px;\"><center>(Image by original authors)</center></p>\n<span style=\"font-family:comfortaa;font-size:18px;color:black;\">And the magic this approach delivers is:</span>\n\n<li style=\"font-family:comfortaa;font-size:18px;color:black;\">Model is able to handle extreme aspect-ratios,</li>\n<li style=\"font-family:comfortaa;font-size:18px;color:black;\">Making changes to sequence length and resolution is possible on the go.</li>\n","metadata":{}},{"cell_type":"markdown","source":"\n<h1 id=\"heading6\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">TRAINING</h1>\n<pre>\n\n<span id = \"artists5\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">CURRICULUM WARM-UP</span>\n\n<span id = \"artists6\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Rather than diving straightforward into training the model on screenshot parsing, authors drew inspiration from curriculum learning where the model is exposed to easy examples of a task and only then on the actual pre-training task. This led to faster and stabler convergence during pre-training. <a href=\"https://towardsdatascience.com/how-to-improve-your-network-performance-by-using-curriculum-learning-3471705efab4\">A nice explanation of curriculum learning can be found in this article</a> </span>\n\n<span id = \"artists7\" style=\"font-family:comfortaa;font-size:18px;color:black;\">For this \"warm-up\" stage as the authors like to call it, they used images with random and uniformly sampled fonts in Google-Fonts of various sizes ranging from 12pt to 36pt with random colors - and corresponding ground truth</span>\n\n\n<center><img src=\"https://i.postimg.cc/MGt4tk3s/pix2struct3.png\"  style=\"horizontal-align:middle\"></center><p style=\"font-size:15px;\"><center><b>Figure.3: Image-Ground Truth pairs during Warm-Up</b></center></p><p style=\"font-size:15px;\"><center>(Image by original authors)</center></p>\n\n<span id = \"artists8\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">PRE-TRAINING</span>\n\n<span id = \"artists9\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Pre-training continues with the sceenshot parsing objective mentioned above. To achieve reduced sequence length of decoder output, largest portion of ground-truth which fits within a pre-defined sequence length is chosen, and corresponding bounding-box is drawn on the image-pair. Also 50% of text in the images is masked-out randomly.</span>\n\n<span id = \"artists10\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Two model variants, Base (262 Million parameters & 768-dimensional hidden state) and Large (1.3 Billion parameters & 1536-dimensional hidden state) were pretrained, with a decoder sequence length of 128 tokens</span>\n\n\n<center><img src=\"https://i.postimg.cc/yYSsYTvf/pix2struct6.png\"  style=\"horizontal-align:middle\"></center><p style=\"font-size:15px;\"><center><b>Figure.4: Bounding-Box on Image & corresponding Ground Truth pair</b></center></p><p style=\"font-size:15px;\"><center>(Image by original authors)</center></p>\n\n<span id = \"artists11\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">FINE TUNING ON DOWNSTREAM DATASETS</span>\n\n<span id = \"artists12\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Fine-tuning on downstream datasets in the sub-domains of Visual Language Understanding was pretty much only a matter of curating the image-text pairs to unequivocally reflect the task at hand. The Datasets used were: <a href=\"https://github.com/vis-nlp/ChartQA\">ChartQA</a>, <a href=\"https://github.com/vis-nlp/ChartQA\">AI2D</a>, <a href=\"https://ocr-vqa.github.io/\">OCR-VQA</a>, <a href=\"https://github.com/mjhucla/Google_Refexp_toolbox\">RefExp</a>, <a href=\"https://github.com/google-research-datasets/widget-caption\">Widget Captioning</a>, <a href=\"https://github.com/google-research-datasets/screen2words\">Screen2Words</a>, <a href=\"https://textvqa.org/textcaps/\">TextCaps</a>, <a href=\"https://www.docvqa.org/datasets/docvqa\">DocVQA</a>, <a href=\"https://www.docvqa.org/datasets/infographicvqa\">InfographicVQA</a> across sub-domains of Illustration Understanding, User-Interface Understanding, Document Understanding and Natural-Image Understanding. </span>\n\n<span id = \"artists13\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Tuning on Image Captioning tasks were easist and most straightforward with image-text pairs. For tasks of Visual Question Answering (VQA) in datasets like ChartQA, DocVQA etc, the question to be answerd was directly rendered on top of the input images, which the model consumes via the Image encoder.</span>\n\n<span id = \"artists14\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Results on the fine-tuned models with a comparison to State of the Art (Sota) models is provided in the below table by the authors. The Pix2Stuct model outperforms visul only models like GIT2 and DONUT on 8 out of the 9 datasets and Sota models on 6 out of 9.</span>\n\n\n<center><img src=\"https://i.postimg.cc/Cxb8s2yZ/pix2struct7.png\"  style=\"horizontal-align:middle\"></center><p style=\"font-size:15px;\"><center><b>Figure.5: Pix2Struct Perfomance compared to other models on 9 benchmark Datasets</b></center></p>\n\n\n<span id = \"artists15\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">CHALLENGES</span>\n\n<span id = \"artists16\" style=\"font-family:comfortaa;font-size:18px;color:black;\">The input image resoluction plays a vital role in the performance of Pix2Struct in Pre-trainig as well as Fine-tuning. It requires a good resolution to perform well, but this same acts as an Achilles Heel for the model as very high resolutions translate to higher sequence length which it may not be able to handle - and this translates to sub-par performance compared to OCR pipelines.</span>\n\n<span id = \"artists17\" style=\"font-family:comfortaa;font-size:18px;color:black;\">But it is hoped that developments in Long-range Transformers might enable pixel-only models catch up with OCR based models.</span>","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading10\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">DOWNSTREAM MODELS USING PIX2STRUCT BACKBONE</h1>\n<pre>\n\n<span id = \"artists18\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">MATCHA (Math reasoning and Chart derendering pretraining)</span>\n\n<span id = \"artists19\" style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2212.09662\">MATCHA</a> uses Pix2Struct backbone with the twin objectives of:</span> <ul><li style=\"font-family:comfortaa;font-size:18px;color:black;\">Derendering the underlying data-table and and the matplotlib/seaborn code that is used to generate the figures of Chart images during pretraining.</li><li style=\"font-family:comfortaa;font-size:18px;color:black;\">Predicting the answer to a math-reasoning question rendered as an image.</li></ul>\n\n\n<span id = \"artists20\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">DePlot</span>\n\n<span id = \"artists21\" style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2212.10505\">DePlot</a> uses Pix2Struct backbone with weights of the MATCHA model to further fine-tuning the model to predict the underlying table of plots in a linear table format and then feeding this prediction to a Large Language Model(LLM) like FlanPaLM and use its One-Shot capabilites to reason over the question asked regarding the plot.</span> \n\n<span id = \"artists21\" style=\"font-family:comfortaa;font-size:18px;color:black;\">If you wish to see the capabilities of Deplot, follow this notebook <a href=\"https://www.kaggle.com/code/gomaki/deplot-trying-out\">DePlot - Trying Out by Masaki Gotou</a></span> ","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading7\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">USAGE IN KAGGLE COMPETITIONS</h1>\n<pre>\n\n<span id = \"artists22\" style=\"font-family:comfortaa;font-size:22px;color:purple;\">Benetech - Making Graphs Accessible</span>\n\n<span id = \"artists23\" style=\"font-family:comfortaa;font-size:18px;color:black;\">In the recently concluded <a href=\"https://www.kaggle.com/competitions/benetech-making-graphs-accessible\">Benetech - Making Graphs Accessible Competition</a>, models using MATCHA and DEPLOT variants of Pix2Struct secured some top positions, beating even object-detection and OCR based models, either on their own (<a href=\"https://www.kaggle.com/competitions/benetech-making-graphs-accessible/discussion/418430\">2nd place Solution</a>) or with object-detection model ensembles (<a href=\"https://www.kaggle.com/competitions/benetech-making-graphs-accessible/discussion/418786\">1st place Solution</a>, <a href=\"https://www.kaggle.com/competitions/benetech-making-graphs-accessible/discussion/418420\">3rd place Solution</a>).  </span> ","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading8\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">EXAMPLE NOTEBOOKS FOR TRAINING PIX2STRUCT/ VARIANTS ON CUSTOM DATASETS</h1>\n<pre>\n\n<span id = \"artists24\" style=\"font-family:comfortaa;font-size:18px;color:black;\">Various checkpoints of Pix2Struct and its variants is available on the HuggingFace Hub. Though the training is resource heavy and more so with higher max_patches (2048, 4096), one could use half precision & gradient checkpointing. Below notebooks serve as nice examples of fine-tuning on custom datasets:</span> <ol><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://www.kaggle.com/code/heyytanay/training-pix2struct-pytorch-amp-w-b\">Training Pix2Struct PyTorch + AMP + W&B on Benetech Competition Data</a> by Tanay Mehta.</li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://github.com/huggingface/notebooks/blob/main/examples/image_captioning_pix2struct.ipynb\">Fine-tune Pix2Struct using Hugging Face transformers and datasets </a> by Younes Belkada</li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://colab.research.google.com/github/NielsRogge/Transformers-Tutorials/blob/master/Pix2Struct/Fine_tune_Pix2Struct_on_key_value_pair_dataset_(PyTorch_Lightning).ipynb\">Fine_tune_Pix2Struct_on_key_value_pair_dataset_(PyTorch_Lightning)</a> by Niels Rogge</li>","metadata":{}},{"cell_type":"markdown","source":"<h1 id=\"heading9\" style=\"font-family:comfortaa;font-size:25px;color:crimson;\">LINKS AND CITATIONS</h1>\n<pre>\n\n<ol><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2210.03347\">Pix2Struct: Screenshot Parsing as Pretraining for Visual Language Understanding</a></li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2111.15664\">OCR-free Document Understanding Transformer</a></li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://towardsdatascience.com/how-to-improve-your-network-performance-by-using-curriculum-learning-3471705efab4\">How to Improve Your Network Performance by Using Curriculum Learning</a></li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2212.10505\">DePlot: One-shot visual language reasoning by plot-to-table translation</a></li><li style=\"font-family:comfortaa;font-size:18px;color:black;\"><a href=\"https://arxiv.org/abs/2212.09662\">MatCha: Enhancing Visual Language Pretraining with Math Reasoning and Chart Derendering</a></li>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nsubmission=pd.read_csv(\"/kaggle/input/2023-kaggle-ai-report/sample_submission.csv\")\nsubmission.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.loc[0]['value']='Generative AI'\nsubmission.loc[1]['value']='https://www.kaggle.com/code/sandeepmnair/generative-model-for-visual-language-understanding/'\nsubmission.loc[2]['value']='https://www.kaggle.com/code/chinmaypagey/generative-ai-report-by-team-sussy-baka/comments#2345611'\nsubmission.loc[3]['value']='https://www.kaggle.com/code/kkiryll/generative-ai-rise-of-the-transformers/comments#2346804'\nsubmission.loc[4]['value']='https://www.kaggle.com/code/rizvanam/generative-ai-evolution-and-powers/comments#2346806'\nsubmission.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]}]}