quan0401 commited on
Commit
f22b93e
·
1 Parent(s): ecefea5

cat classifier

Browse files
Files changed (1) hide show
  1. app.py +43 -4
app.py CHANGED
@@ -1,7 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
1
+ # AUTOGENERATED! DO NOT EDIT! File to edit: cat_classifier.ipynb.
2
+
3
+ # %% auto 0
4
+ __all__ = ['path', 'files', 'dls', 'learn', 'categories', 'label_func', 'image_classifier']
5
+
6
+ # %% cat_classifier.ipynb 1
7
+ from fastai.vision.all import ImageDataLoaders, URLs, untar_data, get_image_files, Resize, \
8
+ vision_learner, resnet34, error_rate, default_device, load_learner, show_image, PILImage
9
+ import torch
10
+
11
+ # %% cat_classifier.ipynb 3
12
+ path = untar_data(URLs.PETS)
13
+ path.ls()
14
+
15
+ # %% cat_classifier.ipynb 5
16
+ files = get_image_files(path/'images')
17
+
18
+ files[0]
19
+
20
+ # %% cat_classifier.ipynb 6
21
+ def label_func(fn:str):
22
+ return fn[0].isupper()
23
+
24
+ # %% cat_classifier.ipynb 7
25
+ dls = ImageDataLoaders.from_name_func(path/'images', fnames=files, label_func=label_func,
26
+ item_tfms=Resize(224), device=default_device(1))
27
+
28
+ # %% cat_classifier.ipynb 9
29
+ learn = vision_learner(dls, resnet34, metrics=error_rate)
30
+ learn.fine_tune(1)
31
+
32
+ # %% cat_classifier.ipynb 12
33
  import gradio as gr
34
+ categories = ('dog', 'cat')
35
+ def image_classifier(input):
36
+ pred, idx, probs = learn.predict(input)
37
+ result = dict(zip(categories, map(float, probs)))
38
+ print('result', result)
39
+ return result
40
+
41
 
 
 
42
 
43
+ gr.Interface(fn=image_classifier,
44
+ inputs=gr.Image(type="pil"),
45
+ outputs=gr.Label(),
46
+ examples=['./cat.jpeg', './dog.jpg']).launch(share=True)