NifulIslam
commited on
Commit
•
9a953f6
1
Parent(s):
2c872f4
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai import *
|
2 |
+
from fastai.vision.all import *
|
3 |
+
from fastai.vision.widgets import *
|
4 |
+
from PIL import ImageFile
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
root='paintings/data/'
|
8 |
+
dblock = DataBlock(
|
9 |
+
blocks=(ImageBlock, CategoryBlock),
|
10 |
+
get_items=get_image_files,
|
11 |
+
splitter=RandomSplitter(valid_pct=0.1, seed=42),
|
12 |
+
get_y=parent_label,
|
13 |
+
item_tfms=Resize(128))
|
14 |
+
dataloader=dblock.dataloaders(root, bs=8)
|
15 |
+
|
16 |
+
dblock = dblock.new(item_tfms=Resize(224, ResizeMethod.Squish), batch_tfms=aug_transforms(do_flip=True,
|
17 |
+
flip_vert=True,
|
18 |
+
max_rotate=10,
|
19 |
+
max_lighting=0.1,
|
20 |
+
))
|
21 |
+
dls = dblock.dataloaders(root)
|
22 |
+
|
23 |
+
model = cnn_learner(dls, densenet201, metrics=[error_rate,accuracy])
|
24 |
+
model.load('modelv2')
|
25 |
+
|
26 |
+
|
27 |
+
def recognize_image(image):
|
28 |
+
pred, idx, probs = model.predict(image)
|
29 |
+
return dict(zip(model.dls.vocab, map(float, probs)))
|
30 |
+
|
31 |
+
image = gr.inputs.Image(shape=(224,224))
|
32 |
+
label = gr.outputs.Label(num_top_classes=10)
|
33 |
+
|
34 |
+
examples = [
|
35 |
+
'test_img/still-life.jpeg',
|
36 |
+
'test_img/landscape.jpeg',
|
37 |
+
'test_img/abstract2.jpg',
|
38 |
+
]
|
39 |
+
|
40 |
+
|
41 |
+
iface = gr.Interface(fn=recognize_image, inputs=image, outputs=label, examples=examples)
|
42 |
+
iface.launch(inline=False)
|