mpd commited on
Commit
a0796cb
1 Parent(s): a294ed6

Update pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +21 -30
pipeline.py CHANGED
@@ -1,37 +1,25 @@
1
  from typing import Dict, List, Any
2
  from fastai.learner import load_learner
3
- import fastai
4
- from fastbook import *
5
  from PIL import Image
6
  import os
7
  import json
8
  import numpy as np
9
 
10
- dls = DataBlock(
11
- blocks=(ImageBlock, CategoryBlock),
12
- get_items=get_image_files,
13
- splitter=RandomSplitter(valid_pct=0.2, seed=42),
14
- get_y=parent_label,
15
- item_tfms=[Resize(192, method='squish')]
16
- ).dataloaders(path, bs=32)
17
-
18
- print('PIPELINE')
19
-
20
  class ImageClassificationPipeline:
21
- def __init__(self, path=""):
22
- # IMPLEMENT_THIS
23
- # Preload all the elements you are going to need at inference.
24
- # For instance your model, processors, tokenizer that might be needed.
25
- # This function is only called once, so do all the heavy processing I/O here"""
26
- print('init')
27
- self.model = load_learner(os.path.join(path, "model.pkl"))
28
- with open(os.path.join(path, "config.json")) as config:
29
- config = json.load(config)
30
- self.labels = config["labels"]
31
 
32
- def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
33
- print('call')
34
- """
 
 
 
 
 
 
 
 
 
 
35
  Args:
36
  inputs (:obj:`PIL.Image`):
37
  The raw image representation as PIL.
@@ -40,8 +28,11 @@ class ImageClassificationPipeline:
40
  A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
41
  It is preferred if the returned list is in decreasing `score` order
42
  """
43
- # IMPLEMENT_THIS
44
- # FastAI expects a np array, not a PIL Image.
45
- _, _, preds = self.model.predict(np.array(inputs))
46
- preds = preds.tolist()
47
- return [{"label": label, "score": preds[idx]} for idx, label in enumerate(self.labels)]
 
 
 
1
  from typing import Dict, List, Any
2
  from fastai.learner import load_learner
 
 
3
  from PIL import Image
4
  import os
5
  import json
6
  import numpy as np
7
 
 
 
 
 
 
 
 
 
 
 
8
  class ImageClassificationPipeline:
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def __init__(self, path=""):
11
+ # IMPLEMENT_THIS
12
+ # Preload all the elements you are going to need at inference.
13
+ # For instance your model, processors, tokenizer that might be needed.
14
+ # This function is only called once, so do all the heavy processing I/O here"""
15
+ self.model = load_learner(os.path.join(path, "model.pkl"))
16
+ with open(os.path.join(path, "config.json")) as config:
17
+ config = json.load(config)
18
+ self.labels = config["labels"]
19
+
20
+ def __call__(self, inputs: "Image.Image") -> List[Dict[str, Any]]:
21
+ print('call')
22
+ """
23
  Args:
24
  inputs (:obj:`PIL.Image`):
25
  The raw image representation as PIL.
28
  A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
29
  It is preferred if the returned list is in decreasing `score` order
30
  """
31
+ # IMPLEMENT_THIS
32
+ # FastAI expects a np array, not a PIL Image.
33
+ _, _, preds = self.model.predict(np.array(inputs))
34
+ preds = preds.tolist()
35
+ return [{
36
+ "label": label,
37
+ "score": preds[idx]
38
+ } for idx, label in enumerate(self.labels)]