abdullahmubeen10 commited on
Commit
e78c183
·
verified ·
1 Parent(s): 6396423

Upload 16 files

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [theme]
2
+ base="light"
3
+ primaryColor="#29B4E8"
Demo.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sparknlp
3
+ import os
4
+ import pandas as pd
5
+
6
+ from sparknlp.base import *
7
+ from sparknlp.annotator import *
8
+ from pyspark.ml import Pipeline
9
+ from sparknlp.pretrained import PretrainedPipeline
10
+ from streamlit_tags import st_tags
11
+
12
+ # Page configuration
13
+ st.set_page_config(
14
+ layout="wide",
15
+ initial_sidebar_state="auto"
16
+ )
17
+
18
+ # CSS for styling
19
+ st.markdown("""
20
+ <style>
21
+ .main-title {
22
+ font-size: 36px;
23
+ color: #4A90E2;
24
+ font-weight: bold;
25
+ text-align: center;
26
+ }
27
+ .section {
28
+ background-color: #f9f9f9;
29
+ padding: 10px;
30
+ border-radius: 10px;
31
+ margin-top: 10px;
32
+ }
33
+ .section p, .section ul {
34
+ color: #666666;
35
+ }
36
+ </style>
37
+ """, unsafe_allow_html=True)
38
+
39
+ @st.cache_resource
40
+ def init_spark():
41
+ return sparknlp.start()
42
+
43
+ @st.cache_resource
44
+ def create_pipeline(model):
45
+ image_assembler = ImageAssembler()\
46
+ .setInputCol("image")\
47
+ .setOutputCol("image_assembler")
48
+
49
+ imageClassifier = SwinForImageClassification.pretrained()\
50
+ .setInputCols("image_assembler")\
51
+ .setOutputCol("class")
52
+
53
+ pipeline = Pipeline(stages=[image_assembler, imageClassifier])
54
+ return pipeline
55
+
56
+ def fit_data(pipeline, data):
57
+ empty_df = spark.createDataFrame([['']]).toDF('text')
58
+ model = pipeline.fit(empty_df)
59
+ light_pipeline = LightPipeline(model)
60
+ annotations_result = light_pipeline.fullAnnotateImage(data)
61
+ return annotations_result[0]['class'][0].result
62
+
63
+ def save_uploadedfile(uploadedfile):
64
+ filepath = os.path.join(IMAGE_FILE_PATH, uploadedfile.name)
65
+ with open(filepath, "wb") as f:
66
+ if hasattr(uploadedfile, 'getbuffer'):
67
+ f.write(uploadedfile.getbuffer())
68
+ else:
69
+ f.write(uploadedfile.read())
70
+
71
+ # Sidebar content
72
+ model_list = ['image_classifier_swin_base_patch4_window7_224 ', 'image_classifier_swin_base_patch4_window12_384_in22k']
73
+ model = st.sidebar.selectbox(
74
+ "Choose the pretrained model",
75
+ model_list,
76
+ help="For more info about the models visit: https://sparknlp.org/models"
77
+ )
78
+
79
+ # Set up the page layout
80
+ st.markdown(f'<div class="main-title">Swin For Image Classification</div>', unsafe_allow_html=True)
81
+ # st.markdown(f'<div class="section"><p>{sub_title}</p></div>', unsafe_allow_html=True)
82
+
83
+ # Reference notebook link in sidebar
84
+ link = """
85
+ <a href="https://github.com/JohnSnowLabs/spark-nlp/blob/master/examples/python/annotation/image/SwinForImageClassification.ipynb">
86
+ <img src="https://colab.research.google.com/assets/colab-badge.svg" style="zoom: 1.3" alt="Open In Colab"/>
87
+ </a>
88
+ """
89
+ st.sidebar.markdown('Reference notebook:')
90
+ st.sidebar.markdown(link, unsafe_allow_html=True)
91
+
92
+ # Load examples
93
+ IMAGE_FILE_PATH = f"inputs"
94
+ image_files = sorted([file for file in os.listdir(IMAGE_FILE_PATH) if file.split('.')[-1]=='png' or file.split('.')[-1]=='jpg' or file.split('.')[-1]=='JPEG' or file.split('.')[-1]=='jpeg'])
95
+
96
+ img_options = st.selectbox("Select an image", image_files)
97
+ uploadedfile = st.file_uploader("Try it for yourself!")
98
+
99
+ if uploadedfile:
100
+ file_details = {"FileName":uploadedfile.name,"FileType":uploadedfile.type}
101
+ save_uploadedfile(uploadedfile)
102
+ selected_image = f"{IMAGE_FILE_PATH}/{uploadedfile.name}"
103
+ elif img_options:
104
+ selected_image = f"{IMAGE_FILE_PATH}/{img_options}"
105
+
106
+ st.subheader('Classified Image')
107
+
108
+ image_size = st.slider('Image Size', 400, 1000, value=400, step = 100)
109
+
110
+ try:
111
+ st.image(f"{IMAGE_FILE_PATH}/{selected_image}", width=image_size)
112
+ except:
113
+ st.image(selected_image, width=image_size)
114
+
115
+ st.subheader('Classification')
116
+
117
+ spark = init_spark()
118
+ Pipeline = create_pipeline(model)
119
+ output = fit_data(Pipeline, selected_image)
120
+
121
+ st.markdown(f'This document has been classified as : **{output}**')
Dockerfile ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Download base image ubuntu 18.04
2
+ FROM ubuntu:18.04
3
+
4
+ # Set environment variables
5
+ ENV NB_USER jovyan
6
+ ENV NB_UID 1000
7
+ ENV HOME /home/${NB_USER}
8
+
9
+ # Install required packages
10
+ RUN apt-get update && apt-get install -y \
11
+ tar \
12
+ wget \
13
+ bash \
14
+ rsync \
15
+ gcc \
16
+ libfreetype6-dev \
17
+ libhdf5-serial-dev \
18
+ libpng-dev \
19
+ libzmq3-dev \
20
+ python3 \
21
+ python3-dev \
22
+ python3-pip \
23
+ unzip \
24
+ pkg-config \
25
+ software-properties-common \
26
+ graphviz \
27
+ openjdk-8-jdk \
28
+ ant \
29
+ ca-certificates-java \
30
+ && apt-get clean \
31
+ && update-ca-certificates -f;
32
+
33
+ # Install Python 3.8 and pip
34
+ RUN add-apt-repository ppa:deadsnakes/ppa \
35
+ && apt-get update \
36
+ && apt-get install -y python3.8 python3-pip \
37
+ && apt-get clean;
38
+
39
+ # Set up JAVA_HOME
40
+ ENV JAVA_HOME /usr/lib/jvm/java-8-openjdk-amd64/
41
+ RUN mkdir -p ${HOME} \
42
+ && echo "export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/" >> ${HOME}/.bashrc \
43
+ && chown -R ${NB_UID}:${NB_UID} ${HOME}
44
+
45
+ # Create a new user named "jovyan" with user ID 1000
46
+ RUN useradd -m -u ${NB_UID} ${NB_USER}
47
+
48
+ # Switch to the "jovyan" user
49
+ USER ${NB_USER}
50
+
51
+ # Set home and path variables for the user
52
+ ENV HOME=/home/${NB_USER} \
53
+ PATH=/home/${NB_USER}/.local/bin:$PATH
54
+
55
+ # Set the working directory to the user's home directory
56
+ WORKDIR ${HOME}
57
+
58
+ # Upgrade pip and install Python dependencies
59
+ RUN python3.8 -m pip install --upgrade pip
60
+ COPY requirements.txt /tmp/requirements.txt
61
+ RUN python3.8 -m pip install -r /tmp/requirements.txt
62
+
63
+ # Copy the application code into the container at /home/jovyan
64
+ COPY --chown=${NB_USER}:${NB_USER} . ${HOME}
65
+
66
+ # Expose port for Streamlit
67
+ EXPOSE 7860
68
+
69
+ # Define the entry point for the container
70
+ ENTRYPOINT ["streamlit", "run", "Demo.py", "--server.port=7860", "--server.address=0.0.0.0"]
inputs/image-1.png ADDED
inputs/image-10.png ADDED
inputs/image-11.png ADDED
inputs/image-2.png ADDED
inputs/image-3.png ADDED
inputs/image-4.png ADDED
inputs/image-5.png ADDED
inputs/image-6.png ADDED
inputs/image-7.png ADDED
inputs/image-8.png ADDED
inputs/image-9.png ADDED
pages/Workflow & Model Overview.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # Custom CSS for better styling
4
+ st.markdown("""
5
+ <style>
6
+ .main-title {
7
+ font-size: 36px;
8
+ color: #4A90E2;
9
+ font-weight: bold;
10
+ text-align: center;
11
+ }
12
+ .sub-title {
13
+ font-size: 24px;
14
+ color: #4A90E2;
15
+ margin-top: 20px;
16
+ }
17
+ .section {
18
+ background-color: #f9f9f9;
19
+ padding: 15px;
20
+ border-radius: 10px;
21
+ margin-top: 20px;
22
+ }
23
+ .section h2 {
24
+ font-size: 22px;
25
+ color: #4A90E2;
26
+ }
27
+ .section p, .section ul {
28
+ color: #666666;
29
+ }
30
+ .link {
31
+ color: #4A90E2;
32
+ text-decoration: none;
33
+ }
34
+ .benchmark-table {
35
+ width: 100%;
36
+ border-collapse: collapse;
37
+ margin-top: 20px;
38
+ }
39
+ .benchmark-table th, .benchmark-table td {
40
+ border: 1px solid #ddd;
41
+ padding: 8px;
42
+ text-align: left;
43
+ }
44
+ .benchmark-table th {
45
+ background-color: #4A90E2;
46
+ color: white;
47
+ }
48
+ .benchmark-table td {
49
+ background-color: #f2f2f2;
50
+ }
51
+ </style>
52
+ """, unsafe_allow_html=True)
53
+
54
+ # Main Title
55
+ st.markdown('<div class="main-title">Image Classification with Swin</div>', unsafe_allow_html=True)
56
+
57
+ # Description
58
+ st.markdown("""
59
+ <div class="section">
60
+ <p><strong>Swin Transformer</strong> is a cutting-edge image classification model introduced in the paper "Swin Transformer: Hierarchical Vision Transformer using Shifted Windows" by Liu et al. The model image_classifier_swin_base_patch4_window7_224 is a Swin model, adapted from Hugging Face and curated for scalability and production-readiness using Spark NLP.</p>
61
+ </div>
62
+ """, unsafe_allow_html=True)
63
+
64
+ # Image Classification Overview
65
+ st.markdown('<div class="sub-title">What is Image Classification?</div>', unsafe_allow_html=True)
66
+ st.markdown("""
67
+ <div class="section">
68
+ <p><strong>Image Classification</strong> is a computer vision task where an algorithm is trained to recognize and classify objects within images. This process involves assigning a label or category to an image based on its visual content.</p>
69
+ <h2>How It Works</h2>
70
+ <p>Image classification typically involves the following steps:</p>
71
+ <ul>
72
+ <li><strong>Data Collection</strong>: Gather a dataset of labeled images.</li>
73
+ <li><strong>Preprocessing</strong>: Normalize and resize images to prepare them for the model.</li>
74
+ <li><strong>Model Training</strong>: Use a machine learning model, such as Swin, to learn patterns and features from the images.</li>
75
+ <li><strong>Inference</strong>: Apply the trained model to new images to predict their labels.</li>
76
+ </ul>
77
+ <h2>Why Use Image Classification?</h2>
78
+ <p>Image classification can automate and streamline many tasks, such as:</p>
79
+ <ul>
80
+ <li>Identifying objects in photos for content tagging.</li>
81
+ <li>Enhancing search functionality by categorizing images.</li>
82
+ <li>Supporting autonomous systems like self-driving cars.</li>
83
+ </ul>
84
+ <h2>Where to Use It</h2>
85
+ <p>Applications of image classification span across various industries:</p>
86
+ <ul>
87
+ <li><strong>Healthcare</strong>: Diagnosing diseases from medical images.</li>
88
+ <li><strong>Retail</strong>: Sorting and tagging product images.</li>
89
+ <li><strong>Security</strong>: Facial recognition for authentication.</li>
90
+ </ul>
91
+ <h2>Importance</h2>
92
+ <p>Image classification is crucial because it enables machines to interpret visual data, which is essential for creating intelligent systems capable of understanding and interacting with the world in a more human-like manner.</p>
93
+ <p>The <strong>Swin Transformer</strong> model used in this example is a state-of-the-art approach for image classification, offering advanced performance and scalability. It utilizes a hierarchical transformer architecture to capture intricate patterns and relationships within images, enhancing classification accuracy and efficiency.</p>
94
+ </div>
95
+ """, unsafe_allow_html=True)
96
+
97
+ # How to Use
98
+ st.markdown('<div class="sub-title">How to Use the Model</div>', unsafe_allow_html=True)
99
+ st.code('''
100
+ import sparknlp
101
+ from sparknlp.base import *
102
+ from sparknlp.annotator import *
103
+ from pyspark.ml import Pipeline
104
+
105
+ # Load image data
106
+ imageDF = spark.read \\
107
+ .format("image") \\
108
+ .option("dropInvalid", value = True) \\
109
+ .load("src/test/resources/image/")
110
+
111
+ # Define Image Assembler
112
+ imageAssembler: ImageAssembler = ImageAssembler() \\
113
+ .setInputCol("image") \\
114
+ .setOutputCol("image_assembler")
115
+
116
+ # Define Swin classifier
117
+ imageClassifier = SwinForImageClassification \\
118
+ .pretrained("image_classifier_swin_base_patch4_window7_224") \\
119
+ .setInputCols(["image_assembler"]) \\
120
+ .setOutputCol("class")
121
+
122
+ # Create pipeline
123
+ pipeline = Pipeline().setStages([imageAssembler, imageClassifier])
124
+
125
+ # Apply pipeline to image data
126
+ pipelineDF = pipeline.fit(imageDF).transform(imageDF)
127
+
128
+ # Show results
129
+ pipelineDF \\
130
+ .selectExpr("reverse(split(image.origin, '/'))[0] as image_name", "class.result") \\
131
+ .show(truncate=False)
132
+ ''', language='python')
133
+
134
+ # Results
135
+ st.markdown('<div class="sub-title">Results</div>', unsafe_allow_html=True)
136
+ st.markdown("""
137
+ <div class="section">
138
+ <table class="benchmark-table">
139
+ <tr>
140
+ <th>Image Name</th>
141
+ <th>Result</th>
142
+ </tr>
143
+ <tr>
144
+ <td>dog.JPEG</td>
145
+ <td>[whippet]</td>
146
+ </tr>
147
+ <tr>
148
+ <td>cat.JPEG</td>
149
+ <td>[Siamese]</td>
150
+ </tr>
151
+ <tr>
152
+ <td>bird.JPEG</td>
153
+ <td>[peacock]</td>
154
+ </tr>
155
+ </table>
156
+ </div>
157
+ """, unsafe_allow_html=True)
158
+
159
+ # Model Information
160
+ st.markdown('<div class="sub-title">Model Information</div>', unsafe_allow_html=True)
161
+ st.markdown("""
162
+ <div class="section">
163
+ <table class="benchmark-table">
164
+ <tr>
165
+ <th>Attribute</th>
166
+ <th>Description</th>
167
+ </tr>
168
+ <tr>
169
+ <td><strong>Model Name</strong></td>
170
+ <td>image_classifier_swin_base_patch4_window7_224</td>
171
+ </tr>
172
+ <tr>
173
+ <td><strong>Compatibility</strong></td>
174
+ <td>Spark NLP 4.3.0+</td>
175
+ </tr>
176
+ <tr>
177
+ <td><strong>License</strong></td>
178
+ <td>Open Source</td>
179
+ </tr>
180
+ <tr>
181
+ <td><strong>Edition</strong></td>
182
+ <td>Official</td>
183
+ </tr>
184
+ <tr>
185
+ <td><strong>Input Labels</strong></td>
186
+ <td>[image_assembler]</td>
187
+ </tr>
188
+ <tr>
189
+ <td><strong>Output Labels</strong></td>
190
+ <td>[class]</td>
191
+ </tr>
192
+ <tr>
193
+ <td><strong>Language</strong></td>
194
+ <td>en</td>
195
+ </tr>
196
+ <tr>
197
+ <td><strong>Size</strong></td>
198
+ <td>108.0 MB</td>
199
+ </tr>
200
+ </table>
201
+ </div>
202
+ """, unsafe_allow_html=True)
203
+
204
+ # References
205
+ st.markdown('<div class="sub-title">References</div>', unsafe_allow_html=True)
206
+ st.markdown("""
207
+ <div class="section">
208
+ <ul>
209
+ <li><a class="link" href="https://sparknlp.org/2023/03/28/image_classifier_swin_base_patch4_window7_224_en.html" target="_blank" rel="noopener">Swin Model on Spark NLP</a></li>
210
+ <li><a class="link" href="https://huggingface.co/microsoft/swin-base-patch4-window7-224" target="_blank" rel="noopener">Swin Model on Hugging Face</a></li>
211
+ <li><a class="link" href="https://github.com/microsoft/Swin-Transformer" target="_blank" rel="noopener">Swin Transformer GitHub Repository</a></li>
212
+ <li><a class="link" href="https://arxiv.org/abs/2103.14030" target="_blank" rel="noopener">Swin Transformer Paper</a></li>
213
+ </ul>
214
+ </div>
215
+ """, unsafe_allow_html=True)
216
+
217
+ # Community & Support
218
+ st.markdown('<div class="sub-title">Community & Support</div>', unsafe_allow_html=True)
219
+ st.markdown("""
220
+ <div class="section">
221
+ <ul>
222
+ <li><a class="link" href="https://sparknlp.org/" target="_blank">Official Website</a>: Documentation and examples</li>
223
+ <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>
224
+ <li><a class="link" href="https://github.com/JohnSnowLabs/spark-nlp" target="_blank">GitHub</a>: Bug reports, feature requests, and contributions</li>
225
+ <li><a class="link" href="https://medium.com/spark-nlp" target="_blank">Medium</a>: Spark NLP articles</li>
226
+ <li><a class="link" href="https://www.youtube.com/channel/UCmFOjlpYEhxf_wJUDuz6xxQ/videos" target="_blank">YouTube</a>: Video tutorials</li>
227
+ </ul>
228
+ </div>
229
+ """, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ streamlit-tags
3
+ pandas
4
+ numpy
5
+ spark-nlp
6
+ pyspark