File size: 2,142 Bytes
7c7f84e
 
 
93e8667
7c7f84e
 
 
 
 
93e8667
64d3902
4d4ad49
7c7f84e
 
93e8667
 
 
 
 
 
 
 
 
64d3902
93e8667
 
 
 
 
 
 
 
 
 
 
 
7c7f84e
 
93e8667
7c7f84e
 
 
 
 
93e8667
 
 
 
7c7f84e
93e8667
7c7f84e
93e8667
 
 
 
7c7f84e
 
 
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
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.

# %% auto 0
__all__ = ['vocab', 'img_path', 'dblock', 'dls', 'learn', 'labels', 'example_files', 'demo', 'predict']

# %% app.ipynb 3
import os
import gradio as gr
from fastai.vision.all import *
import pathlib
import timm
# import dill

# %% app.ipynb 5
vocab = ['bacterial_leaf_blight', 'bacterial_leaf_streak', 'bacterial_panicle_blight', 
         'blast', 'brown_spot', 'dead_heart', 'downy_mildew', 'hispa', 'normal', 'tungro']

# Dummy image path - replace with your real test image path
img_path = pathlib.Path("examples/hispa/200999.jpg")
dblock = DataBlock(
    blocks=(ImageBlock, CategoryBlock(vocab=vocab)),
    get_items=lambda x: [img_path],  # x is source, ignored here
    get_y=lambda x: 'normal',
    item_tfms=[Resize(192, method='squish')]
)

# Pass a dummy 'source' argument, e.g. '.' or pathlib.Path('.')
dls = dblock.dataloaders(pathlib.Path('.'), bs=1)

# %% app.ipynb 7
learn = vision_learner(dls,"mobilenetv4_conv_small.e3600_r256_in1k", metrics=[error_rate, accuracy]); learn

# %% app.ipynb 8
learn.load("mobilenetv4_conv_small.e3600_r256_in1k_v3"); learn
# learn.dls.vocab = ['bacterial_leaf_blight', 'bacterial_leaf_streak', 'bacterial_panicle_blight', 'blast', 'brown_spot', 'dead_heart', 'downy_mildew', 'hispa', 'normal', 'tungro']
# learn.dls.c = len(['bacterial_leaf_blight', 'bacterial_leaf_streak', 'bacterial_panicle_blight', 'blast', 'brown_spot', 'dead_heart', 'downy_mildew', 'hispa', 'normal', 'tungro'])
labels = learn.dls.vocab; labels

# %% app.ipynb 10
def predict(img):
    img = PILImage.create(img)
    pred,pred_idx,probs = learn.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

# %% app.ipynb 12
example_files = list(pathlib.Path('./examples').glob("*/*.jpg")); example_files

# %% app.ipynb 15
demo = gr.Interface(
    fn=predict,
    inputs=gr.Image(), 
    outputs=gr.Label(num_top_classes=3),
    examples=[[str(f)] for f in example_files],
    title="Paddy Disease Classifier",
    description="Upload an image or select one of the examples to classify rice diseases."
)

demo.launch(share=True)