quan0401 commited on
Commit
d999716
·
1 Parent(s): 77048ae

add pretrained model

Browse files
Files changed (2) hide show
  1. app.py +6 -35
  2. model.pkl +3 -0
app.py CHANGED
@@ -1,42 +1,13 @@
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
- torch.set_default_device('cpu')
11
-
12
- # %% cat_classifier.ipynb 3
13
- path = untar_data(URLs.PETS)
14
- path.ls()
15
-
16
- # %% cat_classifier.ipynb 5
17
- files = get_image_files(path/'images')
18
-
19
- files[0]
20
-
21
- # %% cat_classifier.ipynb 6
22
- def label_func(fn:str):
23
- return fn[0].isupper()
24
-
25
- # %% cat_classifier.ipynb 7
26
- dls = ImageDataLoaders.from_name_func(path/'images', fnames=files, label_func=label_func,
27
- item_tfms=Resize(224))
28
 
29
- # %% cat_classifier.ipynb 9
30
- learn = vision_learner(dls, resnet34, metrics=error_rate)
31
- learn.fine_tune(1)
32
 
33
- # %% cat_classifier.ipynb 12
34
- import gradio as gr
35
  categories = ('dog', 'cat')
 
36
  def image_classifier(input):
37
- pred, idx, probs = learn.predict(input)
38
  result = dict(zip(categories, map(float, probs)))
39
- print('result', result)
40
  return result
41
 
42
 
@@ -44,4 +15,4 @@ def image_classifier(input):
44
  gr.Interface(fn=image_classifier,
45
  inputs=gr.Image(type="pil"),
46
  outputs=gr.Label(),
47
- examples=['./cat.jpeg', './dog.jpg']).launch(share=True)
 
1
+ from fastai.vision.all import load_learner
2
+ import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ model = load_learner('./model.pkl')
 
 
5
 
 
 
6
  categories = ('dog', 'cat')
7
+
8
  def image_classifier(input):
9
+ pred, idx, probs = model.predict(input)
10
  result = dict(zip(categories, map(float, probs)))
 
11
  return result
12
 
13
 
 
15
  gr.Interface(fn=image_classifier,
16
  inputs=gr.Image(type="pil"),
17
  outputs=gr.Label(),
18
+ examples=['./cat.jpeg', './dog.jpg']).launch(share=True, inline=False)
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8ecdbdf838b356438d69c75f49ae2807f8b8b1ff706229b81e8a76e93ad13112
3
+ size 87549374