l45k commited on
Commit
b3d7090
·
verified ·
1 Parent(s): c4e9566

Upload processor

Browse files
Files changed (2) hide show
  1. preprocessor_config.json +4 -13
  2. preprocessor_lenet.py +40 -0
preprocessor_config.json CHANGED
@@ -1,15 +1,6 @@
1
  {
2
- "crop_pct": 0.875,
3
- "do_normalize": true,
4
- "do_rescale": false,
5
- "do_resize": false,
6
- "image_mean": 0.1307,
7
- "image_processor_type": "ConvNextImageProcessor",
8
- "image_std": 0.3081,
9
- "resample": null,
10
- "rescale_factor": 0.00392156862745098,
11
- "size": {
12
- "height": 28,
13
- "width": 28
14
- }
15
  }
 
1
  {
2
+ "auto_map": {
3
+ "AutoImageProcessor": "preprocessor_lenet.LeNetProcessor"
4
+ },
5
+ "image_processor_type": "LeNetProcessor"
 
 
 
 
 
 
 
 
 
6
  }
preprocessor_lenet.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from PIL import Image
3
+ from transformers import BaseImageProcessor, BatchFeature
4
+ from transformers.image_utils import ImageInput
5
+ import torch
6
+ from torchvision.transforms import v2
7
+
8
+
9
+ class LeNetProcessor(BaseImageProcessor):
10
+ """
11
+ A custom processor that only normalizes a grayscale image
12
+ and prepares it for a model.
13
+ """
14
+
15
+ model_input_names = ["pixel_values"]
16
+
17
+ def __init__(self, **kwargs):
18
+ """
19
+ Args:
20
+ """
21
+ super().__init__(**kwargs)
22
+
23
+ def preprocess(self, images: ImageInput, return_tensors=None, **kwargs) -> BatchFeature:
24
+ """
25
+ Preprocess a batch of grayscale images.
26
+ """
27
+ if not isinstance(images, list):
28
+ images = [images]
29
+
30
+ transform = v2.Compose([
31
+ v2.RandomResizedCrop(size=(28, 28), antialias=True),
32
+ v2.ToDtype(torch.float32, scale=True),
33
+ v2.Normalize(
34
+ mean=[0.1307],
35
+ std=[0.3081]
36
+ ),
37
+ ])
38
+
39
+ data = {"pixel_values": transform(images)}
40
+ return BatchFeature(data=data, tensor_type=return_tensors)