l45k commited on
Commit
44cadd4
·
verified ·
1 Parent(s): 87b6c28

Upload processor

Browse files
Files changed (2) hide show
  1. preprocessor_config.json +4 -13
  2. preprocessor_resnet.py +35 -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_resnet.ResNetProcessor"
4
+ },
5
+ "image_processor_type": "ResNetProcessor"
 
 
 
 
 
 
 
 
 
6
  }
preprocessor_resnet.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.image_utils import ImageInput
2
+ from transformers import BaseImageProcessor, BatchFeature
3
+ from torchvision.transforms import v2
4
+ import torch
5
+
6
+
7
+ class ResNetProcessor(BaseImageProcessor):
8
+ """
9
+ A custom processor for ResNet training
10
+ """
11
+
12
+ model_input_names = ["pixel_values"]
13
+
14
+ def __init__(self, **kwargs):
15
+ super().__init__(**kwargs)
16
+
17
+ def preprocess(self, images: ImageInput, return_tensors="pt", **kwargs) -> BatchFeature:
18
+ """
19
+ Preprocess a batch of grayscale images.
20
+ """
21
+ if not isinstance(images, list):
22
+ images = [images]
23
+
24
+ transform = v2.Compose([
25
+ v2.RandomResizedCrop(size=(224, 224), antialias=True),
26
+ v2.RandomHorizontalFlip(p=0.5),
27
+ v2.ToDtype(torch.float32, scale=True),
28
+ v2.Normalize(
29
+ mean=[0.485, 0.456, 0.406],
30
+ std=[0.229, 0.224, 0.225]
31
+ ),
32
+ ])
33
+
34
+ data = {"pixel_values": transform(images)}
35
+ return BatchFeature(data=data, tensor_type="pt")