File size: 14,469 Bytes
dc4b134
bf1ff1f
 
b8fc96c
dc4b134
facb498
 
31f8e86
facb498
dc4b134
8933281
bf1ff1f
8933281
6d48781
d413b01
 
facb498
067a92f
facb498
 
 
ebd5f16
dc4b134
 
d413b01
31f8e86
d3b8219
 
 
d413b01
d3b8219
 
facb498
d413b01
d3b8219
 
31f8e86
 
facb498
 
90df017
facb498
d413b01
facb498
6d48781
067a92f
 
d413b01
6d48781
dd609dc
6d48781
dd609dc
 
 
00fa93d
dd609dc
31f8e86
6d48781
 
31f8e86
 
dc4b134
 
 
d413b01
dc4b134
 
067a92f
dc4b134
31f8e86
facb498
d413b01
 
 
 
 
 
6d48781
 
 
d413b01
 
facb498
dc4b134
d413b01
facb498
d413b01
 
 
 
 
 
6d48781
 
 
d413b01
 
facb498
bf1ff1f
d413b01
facb498
31f8e86
facb498
ebd5f16
bf1ff1f
 
9eafbca
bf1ff1f
 
 
 
31f8e86
 
 
 
 
bf1ff1f
b8fc96c
31f8e86
 
 
 
 
 
 
 
 
 
 
 
 
c523064
 
 
 
 
31f8e86
 
 
 
c523064
31f8e86
 
 
 
c523064
31f8e86
 
 
 
 
 
 
 
 
c523064
 
 
 
 
31f8e86
 
 
 
c523064
31f8e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c523064
 
 
 
 
31f8e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3fd32e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c6a3c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3fd32e
 
 
6d48781
c523064
6d48781
c523064
d3fd32e
 
 
 
 
c523064
 
6d48781
c523064
d3fd32e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c523064
 
6d48781
c523064
d3fd32e
 
 
 
 
c523064
 
 
7ea06f5
 
31f8e86
facb498
31f8e86
facb498
ebd5f16
31f8e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eafbca
dc4b134
31f8e86
dc4b134
6d48781
b8fc96c
dc4b134
 
31f8e86
ebd5f16
58150c4
ebd5f16
 
31f8e86
58150c4
 
ebd5f16
31f8e86
6d48781
31f8e86
6d48781
 
31f8e86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d48781
31f8e86
6d48781
 
31f8e86
facb498
6d48781
 
31f8e86
6d48781
31f8e86
 
 
6d48781
31f8e86
 
 
 
 
 
 
6d48781
31f8e86
 
 
 
 
 
 
 
 
 
 
 
2cb2a72
31f8e86
 
 
2cb2a72
31f8e86
2cb2a72
31f8e86
 
 
6d48781
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import streamlit as st
import torch
from PIL import Image
import open_clip

from transformers import (
    BlipProcessor,
    BlipForConditionalGeneration
)

st.set_page_config(page_title="Zero Shot Image Classification", layout="wide")

st.title("Zero Shot Image Classification")
st.write("BiomedCLIP + RemoteCLIP + AgriCLIP + BLIP")

device = "cpu"


# --------------------------------------------------
# LOAD MODELS
# --------------------------------------------------

@st.cache_resource
def load_models():

    # ---------- BIOMEDCLIP ----------
    biomed_model, _, biomed_preprocess = open_clip.create_model_and_transforms(
        "hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
    )

    biomed_tokenizer = open_clip.get_tokenizer(
        "hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
    )

    biomed_model = biomed_model.to(device).eval()


    # ---------- REMOTECLIP ----------
    remote_model, _, remote_preprocess = open_clip.create_model_and_transforms(
        "ViT-B-32",
        pretrained="laion2b_s34b_b79k"
    )

    remote_tokenizer = open_clip.get_tokenizer("ViT-B-32")

    remote_model = remote_model.to(device).eval()


    # ---------- AGRICLIP ----------
   # ---------- AGRICULTURE CLIP ----------
    agri_model, _, agri_preprocess = open_clip.create_model_and_transforms(
    "ViT-B-32",
    pretrained="laion2b_s34b_b79k"
)

    agri_tokenizer = open_clip.get_tokenizer("ViT-B-32")

    agri_model = agri_model.to(device).eval()


    # ---------- BLIP ----------
    blip_processor = BlipProcessor.from_pretrained(
        "Salesforce/blip-image-captioning-base"
    )

    blip_model = BlipForConditionalGeneration.from_pretrained(
        "Salesforce/blip-image-captioning-base"
    ).to(device).eval()


    return (
        biomed_model,
        biomed_preprocess,
        biomed_tokenizer,
        remote_model,
        remote_preprocess,
        remote_tokenizer,
        agri_model,
        agri_preprocess,
        agri_tokenizer,
        blip_processor,
        blip_model
    )


(
    biomed_model,
    biomed_preprocess,
    biomed_tokenizer,
    remote_model,
    remote_preprocess,
    remote_tokenizer,
    agri_model,
    agri_preprocess,
    agri_tokenizer,
    blip_processor,
    blip_model
) = load_models()


# --------------------------------------------------
# DATASET CLASSES
# --------------------------------------------------

DATASETS = {
    "medical": ["pneumonia", "Normal"],
    "skin_disease": ["Normal Skin", "eczema", "Melanoma", "psoriasis"],
    "satellite": ["HIGHWAY", "RIVER", "INDUSTRIAL", "FOREST", "CROP"],
    "agriculture": ["POWDERY_MILDEW", "HEALTHY", "RUST", "EARLY BLIGHT", "LATE BLIGHT"]
}


# --------------------------------------------------
# PROMPT TEMPLATES
# --------------------------------------------------

templates = {

"medical": {

"pneumonia":[
"a chest x ray showing pneumonia infection",
"a lung radiology scan with cloudy pneumonia regions",
"a chest radiograph indicating pneumonia"
],

"Normal":[
"a normal chest x ray with healthy lungs",
"a lung radiology scan without infection",
"a clear chest radiograph"
]

},

"skin_disease": {

"Normal Skin":[
"a dermatology image of healthy skin",
"a clinical photo of normal human skin"
],

"eczema":[
"a dermatology image showing eczema rash",
"a red irritated eczema patch on skin"
],

"Melanoma":[
"a dermatology image showing melanoma lesion",
"a dark irregular melanoma skin lesion"
],

"psoriasis":[
"a dermatology image showing psoriasis scales",
"a thick red psoriasis plaque"
]

},

"satellite": {

"HIGHWAY":[
"a satellite image showing a highway",
"an aerial view of highway road"
],

"RIVER":[
"a satellite image of a river",
"a remote sensing image of river channel"
],

"INDUSTRIAL":[
"a satellite image of an industrial area",
"a remote sensing image showing factories"
],

"FOREST":[
"a satellite image of dense forest",
"a remote sensing image showing forest canopy"
],

"CROP":[
"a satellite image of crop fields",
"a remote sensing image of farmland"
]

},

"agriculture": {

"POWDERY_MILDEW":[
"a plant leaf infected with powdery mildew",
"a leaf covered with white powder fungus"
],

"HEALTHY":[
"a healthy green plant leaf",
"a crop leaf without disease"
],

"RUST":[
"a crop leaf infected with rust disease",
"a plant leaf with rust spots"
],

"EARLY BLIGHT":[
"a plant leaf with early blight spots"
],

"LATE BLIGHT":[
"a plant leaf infected with late blight"
]

}

}


# --------------------------------------------------
# EXPLANATION BLOCK
# --------------------------------------------------

explanations = {

"medical": {
"pneumonia":
"""
The uploaded chest X-ray image appears to indicate pneumonia.  
Pneumonia is a lung infection that causes inflammation in the air sacs, which may fill with fluid or pus.  
Common visual indicators in X-rays include cloudy regions or opacities in the lung fields.  
Early detection is important because pneumonia can affect breathing and oxygen exchange.  
Medical professionals typically confirm the diagnosis using radiological evaluation and clinical symptoms.
""",

"Normal":
"""
The chest X-ray image appears to show healthy lungs with no visible infection.  
Normal lungs in radiographs usually show clear lung fields without abnormal opacities.  
The diaphragm and lung boundaries appear well defined in healthy scans.  
This suggests that the lungs are functioning normally without signs of pneumonia or infection.  
A medical professional would still review the image alongside patient symptoms for confirmation.
"""
},

"skin_disease": {

"eczema":
"""
The image appears to show signs consistent with eczema.  
Eczema is a skin condition that causes redness, inflammation, dryness, and itching.  
It often appears as irritated patches of skin with uneven texture.  
Environmental triggers, allergies, or immune responses may cause flare-ups.  
Dermatologists typically diagnose eczema through visual inspection and medical history.
""",

"Melanoma":
"""
The image may contain characteristics associated with melanoma.  
Melanoma is a serious form of skin cancer that develops in pigment-producing cells.  
Visual indicators can include dark, irregularly shaped lesions with uneven borders.  
Early detection is critical because melanoma can spread to other organs.  
A dermatologist should examine suspicious lesions and may perform a biopsy.
""",

"psoriasis":
"""
The image appears to show features consistent with psoriasis.  
Psoriasis is a chronic autoimmune condition that causes rapid skin cell buildup.  
This results in thick, red patches with silvery scales on the skin.  
The condition may cause itching or discomfort depending on severity.  
Medical treatment usually focuses on reducing inflammation and slowing skin cell growth.
""",

"Normal Skin":
"""
The image appears to show healthy skin without visible dermatological abnormalities.  
Normal skin typically has an even tone and smooth surface texture.  
There are no visible lesions, scales, or inflammatory patches in the image.  
This suggests that the skin does not currently show signs of common dermatological diseases.  
However, regular skin monitoring is still recommended for early detection of conditions.
"""
},

"satellite": {

"HIGHWAY":
"""
The satellite image appears to contain a highway or major roadway.  
Highways are large transportation corridors designed for high-speed vehicle movement.  
From satellite imagery, they appear as long linear structures crossing landscapes.  
They connect cities, industrial zones, and transportation hubs.  
Remote sensing is commonly used to monitor infrastructure development and urban planning.
""",

"RIVER":
"""
The satellite image likely shows a river or flowing water body.  
Rivers appear as long winding structures that transport water across landscapes.  
They are important for agriculture, ecosystems, and human settlements.  
Satellite imagery is often used to monitor river flow, flooding, and environmental changes.  
Remote sensing helps researchers track water resources over time.
""",

"FOREST":
"""
The satellite image appears to contain dense forest vegetation.  
Forests typically appear as large green areas with textured canopy patterns in aerial imagery.  
They play a critical role in maintaining biodiversity and regulating climate.  
Satellite monitoring is widely used to detect deforestation and vegetation changes.  
Remote sensing helps scientists study environmental conservation.
""",
    
"CROP":
"""
The satellite image appears to contain agricultural crop fields.  
Crop areas usually appear as organized patches or grids with varying shades of green or brown depending on growth stages.  
They are essential for food production and rural economies.  
Satellite imagery is widely used to monitor crop health, irrigation, and yield prediction.  
Remote sensing helps in precision agriculture and sustainable farming practices.
""",

"INDUSTRIAL":
"""
The satellite image appears to contain industrial areas or facilities.  
Industrial regions often appear as clusters of large buildings, warehouses, and infrastructure with geometric patterns.  
They are associated with manufacturing, production, and economic activities.  
Satellite imagery is used to monitor industrial expansion, pollution, and land use changes.  
Remote sensing supports urban planning and environmental impact assessment.
"""
},

"agriculture": {

"POWDERY_MILDEW":
"""
The plant leaf in the image appears to show powdery mildew infection.  
Powdery mildew is a fungal disease that creates white powder-like patches on leaves.  
It can reduce photosynthesis and weaken plant health if untreated.  
This disease spreads quickly in humid environments with limited airflow.  
Farmers usually manage it using fungicides and improved crop management practices.
""",

"RUST":
"""
The plant leaf shows symptoms consistent with rust disease.  
Rust infections create orange or brown spots on the leaf surface.  
This fungal disease spreads through airborne spores.  
Severe infections can reduce crop yield and plant health.  
Agricultural monitoring helps detect plant diseases early for better crop protection.
""",

"EARLY BLIGHT":
"""
The leaf may show symptoms of early blight disease.  
Early blight is a fungal infection commonly affecting tomato and potato plants.  
It often produces circular brown spots with concentric rings on leaves.  
If left untreated, the disease may spread across the plant.  
Proper crop rotation and fungicide treatment help control early blight.
""",

"LATE BLIGHT":
"""
The leaf may be affected by late blight disease.  
Late blight is a severe plant disease that affects crops such as potatoes and tomatoes.  
Symptoms often include dark lesions and rapid leaf decay.  
The disease spreads quickly in cool and humid conditions.  
Early detection helps farmers take preventive measures to protect crops.
""",

"HEALTHY":
"""
The plant leaf appears healthy with no visible disease symptoms.  
Healthy leaves typically have a uniform green color and smooth surface.  
There are no visible fungal spots, discoloration, or lesions.  
This indicates that the plant is likely growing under good conditions.  
Regular monitoring helps maintain crop health and productivity.
"""
}

}


# --------------------------------------------------
# SIDEBAR
# --------------------------------------------------

dataset_key = st.sidebar.selectbox(
    "Select Dataset",
    list(DATASETS.keys())
)


# --------------------------------------------------
# IMAGE UPLOAD
# --------------------------------------------------

uploaded_file = st.file_uploader(
    "Upload Image",
    type=["jpg","jpeg","png"]
)


if uploaded_file:

    image = Image.open(uploaded_file).convert("RGB")

    st.image(image, width=400)

    labels = DATASETS[dataset_key]

    text_queries = []
    label_mapping = []

    for label in labels:
        for caption in templates[dataset_key][label]:
            text_queries.append(caption)
            label_mapping.append(label)


    # --------------------------------------------------
    # MODEL SELECTION
    # --------------------------------------------------

    if dataset_key in ["medical","skin_disease"]:

        img = biomed_preprocess(image).unsqueeze(0).to(device)
        text = biomed_tokenizer(text_queries)

        with torch.no_grad():
            image_features = biomed_model.encode_image(img)
            text_features = biomed_model.encode_text(text)

        similarity = (image_features @ text_features.T).softmax(dim=-1)


    elif dataset_key == "satellite":

        img = remote_preprocess(image).unsqueeze(0).to(device)
        text = remote_tokenizer(text_queries)

        with torch.no_grad():
            image_features = remote_model.encode_image(img)
            text_features = remote_model.encode_text(text)

        similarity = (image_features @ text_features.T).softmax(dim=-1)


    elif dataset_key == "agriculture":

        img = agri_preprocess(image).unsqueeze(0).to(device)
        text = agri_tokenizer(text_queries)

        with torch.no_grad():
            image_features = agri_model.encode_image(img)
            text_features = agri_model.encode_text(text)

        similarity = (image_features @ text_features.T).softmax(dim=-1)


    conf, idx = torch.max(similarity, dim=1)

    predicted_class = label_mapping[idx.item()]

    st.success(f"Prediction: {predicted_class}")
    st.metric("Confidence", f"{conf.item():.2%}")


    # --------------------------------------------------
    # BLIP IMAGE CAPTION
    # --------------------------------------------------

    blip_inputs = blip_processor(images=image, return_tensors="pt").to(device)

    with torch.no_grad():
        caption_ids = blip_model.generate(**blip_inputs)

    caption = blip_processor.decode(caption_ids[0], skip_special_tokens=True)

    st.subheader("Image Description (BLIP)")
    st.write(caption)


    # --------------------------------------------------
    # SHOW EXPLANATION
    # --------------------------------------------------

    st.subheader("Explanation")

    if dataset_key in explanations and predicted_class in explanations[dataset_key]:
        st.write(explanations[dataset_key][predicted_class])
    else:
        st.write("The prediction was generated by comparing the uploaded image with multiple textual descriptions using a zero-shot vision-language model.")