osanseviero HF staff commited on
Commit
204d5f4
1 Parent(s): 82598df

Add pipeline

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. README.md +26 -0
  3. config.json +6 -0
  4. pipeline.py +39 -0
.gitattributes CHANGED
@@ -14,4 +14,5 @@
14
  *.pb filter=lfs diff=lfs merge=lfs -text
15
  *.pt filter=lfs diff=lfs merge=lfs -text
16
  *.pth filter=lfs diff=lfs merge=lfs -text
 
17
  *tfevents* filter=lfs diff=lfs merge=lfs -text
14
  *.pb filter=lfs diff=lfs merge=lfs -text
15
  *.pt filter=lfs diff=lfs merge=lfs -text
16
  *.pth filter=lfs diff=lfs merge=lfs -text
17
+ *.pkl filter=lfs diff=lfs merge=lfs -text
18
  *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ library_name: generic
5
+ ---
6
+
7
+ # Dog vs Cat Image Classification with FastAI CNN
8
+
9
+ Training is based in FastAI [Quick Start](https://docs.fast.ai/quick_start.html). Example training
10
+
11
+
12
+ ## Training
13
+
14
+ The model was trained as follows
15
+
16
+ ```python
17
+ path = untar_data(URLs.PETS)/'images'
18
+
19
+ def is_cat(x): return x[0].isupper()
20
+ dls = ImageDataLoaders.from_name_func(
21
+ path, get_image_files(path), valid_pct=0.2, seed=42,
22
+ label_func=is_cat, item_tfms=Resize(224))
23
+
24
+ learn = cnn_learner(dls, resnet34, metrics=error_rate)
25
+ learn.fine_tune(1)
26
+ ```
config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ {
2
+ "id2label": {
3
+ "0": "dog",
4
+ "1": "cat"
5
+ }
6
+ }
pipeline.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from PIL import Image
3
+
4
+ import os
5
+ from fastai.learner import load_learner
6
+
7
+ # Custom code used by the model.
8
+ def is_cat(x):
9
+ return x[0].isupper()
10
+
11
+ class PreTrainedPipeline():
12
+ def __init__(self, path=""):
13
+ # IMPLEMENT_THIS
14
+ # Preload all the elements you are going to need at inference.
15
+ # For instance your model, processors, tokenizer that might be needed.
16
+ # This function is only called once, so do all the heavy processing I/O here"""
17
+ self.model = load_learner(os.path.join(path, "model.pkl"))
18
+ with open(os.path.join(path, "config.json")) as config:
19
+ config = json.load(config)
20
+ self.id2label = config["id2label"]
21
+
22
+ def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
23
+ """
24
+ Args:
25
+ inputs (:obj:`PIL.Image`):
26
+ The raw image representation as PIL.
27
+ No transformation made whatsoever from the input. Make all necessary transformations here.
28
+ Return:
29
+ A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
30
+ It is preferred if the returned list is in decreasing `score` order
31
+ """
32
+ # IMPLEMENT_THIS
33
+ # FastAI expects a np array, not a PIL Image.
34
+ _, _, preds = self.model.predict(np.array(inputs))
35
+ labels = [
36
+ {"label": str(self.id2label["0"]), "score": preds[0]},
37
+ {"label": str(self.id2label["1"]), "score": preds[1]},
38
+ ]
39
+ return labels