|
import streamlit as st
|
|
|
|
|
|
st.markdown("""
|
|
<style>
|
|
.main-title {
|
|
font-size: 36px;
|
|
color: #4A90E2;
|
|
font-weight: bold;
|
|
text-align: center;
|
|
}
|
|
.sub-title {
|
|
font-size: 24px;
|
|
color: #4A90E2;
|
|
margin-top: 20px;
|
|
}
|
|
.section {
|
|
background-color: #f9f9f9;
|
|
padding: 15px;
|
|
border-radius: 10px;
|
|
margin-top: 20px;
|
|
}
|
|
.section h2 {
|
|
font-size: 22px;
|
|
color: #4A90E2;
|
|
}
|
|
.section p, .section ul {
|
|
color: #666666;
|
|
}
|
|
.link {
|
|
color: #4A90E2;
|
|
text-decoration: none;
|
|
}
|
|
.benchmark-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin-top: 20px;
|
|
}
|
|
.benchmark-table th, .benchmark-table td {
|
|
border: 1px solid #ddd;
|
|
padding: 8px;
|
|
text-align: left;
|
|
}
|
|
.benchmark-table th {
|
|
background-color: #4A90E2;
|
|
color: white;
|
|
}
|
|
.benchmark-table td {
|
|
background-color: #f2f2f2;
|
|
}
|
|
</style>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="main-title">Image Captioning with VisionEncoderDecoderModel</div>', unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown("""
|
|
<div class="section">
|
|
<p><strong>VisionEncoderDecoderModel</strong> allows you to initialize an image-to-text model using any pretrained Transformer-based vision model (e.g., ViT, BEiT, DeiT, Swin) as the encoder and any pretrained language model (e.g., RoBERTa, GPT2, BERT, DistilBERT) as the decoder.</p>
|
|
<p>This approach has been demonstrated to be effective in models like TrOCR: <a class="link" href="https://arxiv.org/abs/2103.14030" target="_blank">Transformer-based Optical Character Recognition with Pre-trained Models by Minghao Li et al.</a></p>
|
|
<p>After training or fine-tuning a VisionEncoderDecoderModel, it can be saved and loaded just like any other model. Examples are provided below.</p>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">What is Image Captioning?</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<p><strong>Image Captioning</strong> is the task of generating a textual description of an image. It uses a model to encode the image into a feature representation, which is then decoded by a language model to produce a natural language description.</p>
|
|
<h2>How It Works</h2>
|
|
<p>Image captioning typically involves the following steps:</p>
|
|
<ul>
|
|
<li><strong>Image Encoding</strong>: The image is passed through a vision model (e.g., ViT) to produce a feature representation.</li>
|
|
<li><strong>Caption Generation</strong>: The feature representation is fed into a language model (e.g., GPT2) to generate a caption for the image.</li>
|
|
</ul>
|
|
<h2>Why Use Image Captioning?</h2>
|
|
<p>Image captioning is useful for:</p>
|
|
<ul>
|
|
<li>Automatically generating descriptions for images, enhancing accessibility.</li>
|
|
<li>Improving search engine capabilities by allowing images to be indexed with textual content.</li>
|
|
<li>Supporting content management systems with automated tagging and description generation.</li>
|
|
</ul>
|
|
<h2>Where to Use It</h2>
|
|
<p>Applications of image captioning span various domains:</p>
|
|
<ul>
|
|
<li><strong>Social Media</strong>: Automatically generating captions for user-uploaded images.</li>
|
|
<li><strong>Digital Libraries</strong>: Creating descriptive metadata for image collections.</li>
|
|
<li><strong>Accessibility</strong>: Assisting visually impaired individuals by describing visual content.</li>
|
|
</ul>
|
|
<h2>Importance</h2>
|
|
<p>Image captioning is essential for bridging the gap between visual and textual information, enabling better interaction between machines and users by providing context and meaning to images.</p>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
|
|
st.code('''
|
|
import sparknlp
|
|
from sparknlp.base import *
|
|
from sparknlp.annotator import *
|
|
from pyspark.ml import Pipeline
|
|
|
|
# Load image data
|
|
imageDF = spark.read \\
|
|
.format("image") \\
|
|
.option("dropInvalid", value = True) \\
|
|
.load("src/test/resources/image/")
|
|
|
|
# Define Image Assembler
|
|
imageAssembler = ImageAssembler() \\
|
|
.setInputCol("image") \\
|
|
.setOutputCol("image_assembler")
|
|
|
|
# Define VisionEncoderDecoder for image captioning
|
|
imageCaptioning = VisionEncoderDecoderForImageCaptioning \\
|
|
.pretrained() \\
|
|
.setBeamSize(2) \\
|
|
.setDoSample(False) \\
|
|
.setInputCols(["image_assembler"]) \\
|
|
.setOutputCol("caption")
|
|
|
|
# Create pipeline
|
|
pipeline = Pipeline().setStages([imageAssembler, imageCaptioning])
|
|
|
|
# Apply pipeline to image data
|
|
pipelineDF = pipeline.fit(imageDF).transform(imageDF)
|
|
|
|
# Show results
|
|
pipelineDF \\
|
|
.selectExpr("reverse(split(image.origin, '/'))[0] as image_name", "caption.result") \\
|
|
.show(truncate = False)
|
|
''', language='python')
|
|
|
|
|
|
st.markdown('<div class="sub-title">Results</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<table class="benchmark-table">
|
|
<tr>
|
|
<th>Image Name</th>
|
|
<th>Result</th>
|
|
</tr>
|
|
<tr>
|
|
<td>palace.JPEG</td>
|
|
<td>[a large room filled with furniture and a large window]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>egyptian_cat.jpeg</td>
|
|
<td>[a cat laying on a couch next to another cat]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>hippopotamus.JPEG</td>
|
|
<td>[a brown bear in a body of water]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>hen.JPEG</td>
|
|
<td>[a flock of chickens standing next to each other]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>ostrich.JPEG</td>
|
|
<td>[a large bird standing on top of a lush green field]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>junco.JPEG</td>
|
|
<td>[a small bird standing on a wet ground]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>bluetick.jpg</td>
|
|
<td>[a small dog standing on a wooden floor]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>chihuahua.jpg</td>
|
|
<td>[a small brown dog wearing a blue sweater]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>tractor.JPEG</td>
|
|
<td>[a man is standing in a field with a tractor]</td>
|
|
</tr>
|
|
<tr>
|
|
<td>ox.JPEG</td>
|
|
<td>[a large brown cow standing on top of a lush green field]</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<table class="benchmark-table">
|
|
<tr>
|
|
<th>Attribute</th>
|
|
<th>Description</th>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Model Name</strong></td>
|
|
<td>image_captioning_vit_gpt2</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Compatibility</strong></td>
|
|
<td>Spark NLP 5.1.2+</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>License</strong></td>
|
|
<td>Open Source</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Edition</strong></td>
|
|
<td>Official</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Input Labels</strong></td>
|
|
<td>[image_assembler]</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Output Labels</strong></td>
|
|
<td>[caption]</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Language</strong></td>
|
|
<td>en</td>
|
|
</tr>
|
|
<tr>
|
|
<td><strong>Size</strong></td>
|
|
<td>890.3 MB</td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">Data Source</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<p>The image captioning model is available on <a class="link" href="https://huggingface.co/nlpconnect/vit-gpt2-image-captioning" target="_blank">Hugging Face</a>. This model uses ViT for image encoding and GPT2 for generating captions.</p>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">Conclusion</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<p>The <strong>VisionEncoderDecoderModel</strong> represents a powerful approach for bridging the gap between visual and textual information. By leveraging pretrained models for both image encoding and text generation, it effectively captures the nuances of both domains, resulting in high-quality outputs such as detailed image captions and accurate text-based interpretations of visual content.</p>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<ul>
|
|
<li><a class="link" href="https://sparknlp.org/2023/09/20/image_captioning_vit_gpt2_en.html" target="_blank" rel="noopener">Image Captioning Model on Spark NLP</a></li>
|
|
<li><a class="link" href="https://huggingface.co/nlpconnect/vit-gpt2-image-captioning" target="_blank">Image Captioning Model on Hugging Face</a></li>
|
|
<li><a class="link" href="https://arxiv.org/abs/2103.14030" target="_blank">TrOCR Paper</a></li
|
|
</ul>
|
|
</div>
|
|
""", unsafe_allow_html=True)
|
|
|
|
|
|
st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
|
|
st.markdown("""
|
|
<div class="section">
|
|
<ul>
|
|
<li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
|
|
<li><a class="link" href="https://join.slack.com/t/spark-nlp/shared_invite/zt-198dipu77-L3UWNe_AJ8xqDk0ivmih5Q" target="_blank">Slack</a>: Live discussion with the community and team</li>
|
|
<li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
|
|
<li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
|
|
<li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
|
|
</ul>
|
|
</div>
|
|
""", unsafe_allow_html=True) |