-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFeature Extractor.py
57 lines (40 loc) · 1.43 KB
/
Feature Extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from datasets import load_dataset
ds = load_dataset("beans")
ex = ds["train"][400]
test_image = ex["image"]
labels = ds["train"].features["labels"]
label_name = labels.int2str(ex["labels"])
from transformers import ViTFeatureExtractor
base_model_url = 'google/vit-base-patch16-224-in21k'
feature_extractor = ViTFeatureExtractor.from_pretrained(base_model_url)
print(feature_extractor)
feature_test = feature_extractor(test_image, return_tensors="pt")
print(feature_test)
print(feature_test.keys())
print(feature_test["pixel_values"].shape)
def process_example(example):
inputs = feature_extractor(example["image"], return_tensors="pt")
inputs["labels"] = example["labels"]
return inputs
preprocess_test = ds["train"][10]
print(preprocess_test)
preprocess_test = process_example(preprocess_test)
print("cleaned")
print(preprocess_test)
print(preprocess_test["pixel_values"].shape)
def transform(example_batch):
inputs = feature_extractor([x for x in example_batch["image"]], return_tensors="pt")
inputs["labels"] = example_batch["labels"]
return inputs
print("batch")
clean = ds.with_transform(transform)
print(clean["train"][0:2])
print(clean["train"][0:2]["pixel_values"].shape)
import torch
def collate_fn(batch):
return {
"pixel_values": torch.stack([x["pixel_values"] for x in batch]),
"labels": torch.tensor([x["labels"] for x in batch])
}