File size: 10,640 Bytes
b049c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""

Batik Classification Web App - Streamlit

Upload gambar batik dan model akan mendeteksi motifnya!

"""

import streamlit as st
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import json
import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px

# Page config
st.set_page_config(
    page_title="Batik Nusantara Classification",
    page_icon="๐ŸŽจ",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
st.markdown("""

<style>

    .main-header {

        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        padding: 2rem;

        border-radius: 10px;

        color: white;

        text-align: center;

        margin-bottom: 2rem;

    }

    

    .stButton>button {

        width: 100%;

        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);

        color: white;

        font-weight: bold;

        padding: 0.75rem;

        border-radius: 8px;

        border: none;

        font-size: 1.1em;

    }

    

    .prediction-box {

        background: #f8f9fa;

        padding: 1.5rem;

        border-radius: 10px;

        border-left: 5px solid #667eea;

        margin: 1rem 0;

    }

    

    .metric-card {

        background: white;

        padding: 1rem;

        border-radius: 8px;

        box-shadow: 0 2px 4px rgba(0,0,0,0.1);

        text-align: center;

    }

</style>

""", unsafe_allow_html=True)

# Load model
@st.cache_resource
def load_model():
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
    # Load config
    with open('model_config_final.json', 'r') as f:
        config = json.load(f)
    
    num_classes = config['num_classes']
    class_names = config['class_names']
    
    # Build model
    vgg16 = models.vgg16(pretrained=False)
    num_features = vgg16.classifier[0].in_features
    vgg16.classifier = nn.Sequential(
        nn.Linear(num_features, 4096),
        nn.ReLU(inplace=True),
        nn.Dropout(0.5),
        nn.Linear(4096, 4096),
        nn.ReLU(inplace=True),
        nn.Dropout(0.5),
        nn.Linear(4096, num_classes)
    )
    
    # Load weights
    checkpoint = torch.load('vgg16_batik_best.pth', map_location=device)
    if 'model_state_dict' in checkpoint:
        vgg16.load_state_dict(checkpoint['model_state_dict'])
        best_acc = checkpoint.get('best_acc', 0)
    else:
        vgg16.load_state_dict(checkpoint)
        best_acc = 0
    
    vgg16.to(device)
    vgg16.eval()
    
    return vgg16, class_names, device, best_acc, config

# Get transforms
def get_transforms():
    return transforms.Compose([
        transforms.Resize((256, 256)),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])

# Predict function
def predict_batik(image, model, class_names, device, top_k=10):
    transform = get_transforms()
    
    # Preprocess
    image_rgb = image.convert('RGB')
    input_tensor = transform(image_rgb).unsqueeze(0).to(device)
    
    # Predict
    with torch.no_grad():
        outputs = model(input_tensor)
        probabilities = torch.nn.functional.softmax(outputs, dim=1)
        confidence, predicted = torch.max(probabilities, 1)
    
    # Get top-k predictions
    topk_prob, topk_idx = torch.topk(probabilities, min(top_k, len(class_names)))
    
    predicted_class = class_names[predicted.item()]
    confidence_score = confidence.item() * 100
    
    top_predictions = [
        {
            'class': class_names[idx.item()],
            'confidence': prob.item() * 100
        }
        for idx, prob in zip(topk_idx[0], topk_prob[0])
    ]
    
    return predicted_class, confidence_score, top_predictions

# Load model
try:
    model, class_names, device, best_acc, config = load_model()
    model_loaded = True
except Exception as e:
    st.error(f"Error loading model: {e}")
    model_loaded = False

# Header
st.markdown("""

<div class="main-header">

    <h1>๐ŸŽจ Batik Nusantara Classification</h1>

    <h3>Deteksi Motif Batik Indonesia dengan AI</h3>

    <p style="font-size: 1.1em; margin-top: 10px;">

        {num_classes} Motif dari Berbagai Daerah โ€ข Powered by VGG16 Deep Learning

    </p>

</div>

""".format(num_classes=len(class_names) if model_loaded else 0), unsafe_allow_html=True)

if not model_loaded:
    st.error("โš ๏ธ Model belum di-load. Pastikan file model ada!")
    st.stop()

# Sidebar
with st.sidebar:
    st.image("https://via.placeholder.com/300x150/667eea/ffffff?text=Batik+AI", use_container_width=True)
    
    st.markdown("### ๐Ÿ“Š Model Info")
    st.metric("Total Classes", len(class_names))
    if best_acc > 0:
        st.metric("Model Accuracy", f"{best_acc:.2f}%")
    st.metric("Device", "GPU" if device.type == "cuda" else "CPU")
    
    st.markdown("---")
    st.markdown("### ๐ŸŽฏ Cara Menggunakan")
    st.markdown("""

    1. Upload gambar batik

    2. Tunggu hasil prediksi

    3. Lihat confidence score

    4. Cek alternatif motif

    """)
    
    st.markdown("---")
    st.markdown("### ๐Ÿ“š Region Coverage")
    regions = set([name.split('_')[0] for name in class_names if '_' in name])
    for region in sorted(regions)[:10]:
        st.markdown(f"โ€ข {region}")
    if len(regions) > 10:
        st.markdown(f"... dan {len(regions)-10} lainnya")

# Main content
col1, col2 = st.columns([1, 1])

with col1:
    st.markdown("### ๐Ÿ“ค Upload Gambar Batik")
    uploaded_file = st.file_uploader(
        "Pilih gambar batik (JPG, PNG, JPEG)",
        type=['jpg', 'jpeg', 'png'],
        help="Upload gambar batik yang jelas dan fokus untuk hasil terbaik"
    )
    
    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Gambar yang diupload", use_container_width=True)
        
        # Image info
        st.markdown(f"""

        **Info Gambar:**

        - Ukuran: {image.size[0]} x {image.size[1]} pixels

        - Format: {image.format}

        - Mode: {image.mode}

        """)
        
        predict_button = st.button("๐Ÿ” Deteksi Motif Batik", use_container_width=True)
    else:
        st.info("๐Ÿ‘† Upload gambar batik untuk memulai deteksi")
        predict_button = False

with col2:
    st.markdown("### ๐Ÿ“Š Hasil Prediksi")
    
    if uploaded_file is not None and predict_button:
        with st.spinner("๐Ÿ”„ Menganalisis motif batik..."):
            predicted_class, confidence_score, top_predictions = predict_batik(
                image, model, class_names, device, top_k=10
            )
        
        # Main prediction
        st.markdown(f"""

        <div class="prediction-box">

            <h2 style="color: #667eea; margin: 0;">๐ŸŽจ {predicted_class}</h2>

            <h4 style="color: #666; margin-top: 0.5rem;">Confidence: {confidence_score:.2f}%</h4>

        </div>

        """, unsafe_allow_html=True)
        
        # Extract region and pattern
        if '_' in predicted_class:
            region, pattern = predicted_class.split('_', 1)
            
            col_a, col_b = st.columns(2)
            with col_a:
                st.markdown(f"""

                <div class="metric-card">

                    <h4 style="color: #667eea;">๐Ÿ“ Region</h4>

                    <h3>{region}</h3>

                </div>

                """, unsafe_allow_html=True)
            
            with col_b:
                st.markdown(f"""

                <div class="metric-card">

                    <h4 style="color: #764ba2;">๐ŸŽจ Pattern</h4>

                    <h3>{pattern}</h3>

                </div>

                """, unsafe_allow_html=True)
        
        # Confidence interpretation
        st.markdown("---")
        st.markdown("### ๐Ÿ“ˆ Interpretasi Confidence")
        if confidence_score >= 90:
            st.success(f"โœ… Model SANGAT YAKIN ({confidence_score:.2f}%) - Prediksi sangat akurat!")
        elif confidence_score >= 70:
            st.info(f"โ„น๏ธ Model CUKUP YAKIN ({confidence_score:.2f}%) - Prediksi cukup akurat")
        else:
            st.warning(f"โš ๏ธ Model KURANG YAKIN ({confidence_score:.2f}%) - Gambar mungkin blur atau motif tidak umum")
        
        # Top predictions chart
        st.markdown("---")
        st.markdown("### ๐Ÿ“Š Top 10 Predictions")
        
        # Create dataframe
        df = pd.DataFrame(top_predictions)
        
        # Horizontal bar chart
        fig = go.Figure(go.Bar(
            x=df['confidence'],
            y=df['class'],
            orientation='h',
            marker=dict(
                color=df['confidence'],
                colorscale='Viridis',
                showscale=True,
                colorbar=dict(title="Confidence %")
            ),
            text=df['confidence'].apply(lambda x: f"{x:.2f}%"),
            textposition='outside'
        ))
        
        fig.update_layout(
            title="Top 10 Motif Predictions",
            xaxis_title="Confidence (%)",
            yaxis_title="Motif",
            height=500,
            yaxis={'categoryorder': 'total ascending'}
        )
        
        st.plotly_chart(fig, use_container_width=True)
        
        # Top predictions table
        with st.expander("๐Ÿ“‹ Lihat Detail Predictions"):
            df_display = df.copy()
            df_display['confidence'] = df_display['confidence'].apply(lambda x: f"{x:.2f}%")
            df_display.index = range(1, len(df_display) + 1)
            st.dataframe(df_display, use_container_width=True)
    
    else:
        st.info("Upload gambar dan klik tombol untuk melihat hasil prediksi")

# Footer
st.markdown("---")
st.markdown("""

<div style="text-align: center; color: #666; padding: 2rem;">

    <h3>๐Ÿ“š Tentang Model</h3>

    <p>Model ini menggunakan arsitektur <b>VGG16</b> yang telah di-train dengan <b>{num_classes} motif batik</b> dari berbagai daerah di Indonesia.</p>

    <p>Model dapat mengenali motif dari Jawa, Bali, Papua, Sumatra, Kalimantan, Sulawesi, dan daerah lainnya.</p>

    <br>

    <p style="font-size: 0.9em;">๐Ÿ‡ฎ๐Ÿ‡ฉ Preserving Indonesian Cultural Heritage through AI</p>

    <p style="font-size: 0.8em; color: #999;">VGG16 Architecture โ€ข PyTorch โ€ข Deep Learning</p>

</div>

""".format(num_classes=len(class_names)), unsafe_allow_html=True)