l45k commited on
Commit
0d80455
·
verified ·
1 Parent(s): ab8b989

Upload processor

Browse files
Files changed (2) hide show
  1. preprocessor_config.json +3 -0
  2. preprocessor_lenet.py +36 -0
preprocessor_config.json CHANGED
@@ -1,4 +1,7 @@
1
  {
 
 
 
2
  "crop_size": null,
3
  "data_format": "channels_first",
4
  "default_to_square": true,
 
1
  {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "preprocessor_lenet.LeNetProcessor"
4
+ },
5
  "crop_size": null,
6
  "data_format": "channels_first",
7
  "default_to_square": true,
preprocessor_lenet.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import BaseImageProcessorFast, BatchFeature
2
+ from transformers.image_transforms import (
3
+ normalize,
4
+ )
5
+ from transformers.image_utils import (
6
+ ImageInput,
7
+ to_numpy_array,
8
+ )
9
+
10
+ class LeNetProcessor(BaseImageProcessorFast):
11
+ model_input_names = ["pixel_values"]
12
+
13
+ def __init__(
14
+ self,
15
+ **kwargs
16
+ ):
17
+ super().__init__(**kwargs)
18
+
19
+ def preprocess(
20
+ self,
21
+ images: ImageInput,
22
+ return_tensors=None,
23
+ **kwargs
24
+ ) -> BatchFeature:
25
+
26
+ if not isinstance(images, list):
27
+ images = [images]
28
+
29
+ images = [to_numpy_array(img) for img in images]
30
+
31
+ processed_images = []
32
+ for img in images:
33
+ processed_images.append(normalize(img, mean=0.1307, std=0.3081))
34
+
35
+ data = {"pixel_values": processed_images}
36
+ return BatchFeature(data=data, tensor_type=return_tensors)