Upload 6 files
Browse files- README.md +3 -0
- config.json +52 -0
- inference.py +25 -0
- model.safetensors +3 -0
- preprocessor_config.json +22 -0
- train.py +112 -0
README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Training of https://github.com/yuechen-yang/garbage-classification using this dataset: https://www.kaggle.com/datasets/mostafaabla/garbage-classification
|
| 2 |
+
|
| 3 |
+
|
config.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "google/vit-base-patch16-224-in21k",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"ViTForImageClassification"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.0,
|
| 7 |
+
"encoder_stride": 16,
|
| 8 |
+
"hidden_act": "gelu",
|
| 9 |
+
"hidden_dropout_prob": 0.0,
|
| 10 |
+
"hidden_size": 768,
|
| 11 |
+
"id2label": {
|
| 12 |
+
"0": "battery",
|
| 13 |
+
"1": "biological",
|
| 14 |
+
"10": "trash",
|
| 15 |
+
"11": "white-glass",
|
| 16 |
+
"2": "brown-glass",
|
| 17 |
+
"3": "cardboard",
|
| 18 |
+
"4": "clothes",
|
| 19 |
+
"5": "green-glass",
|
| 20 |
+
"6": "metal",
|
| 21 |
+
"7": "paper",
|
| 22 |
+
"8": "plastic",
|
| 23 |
+
"9": "shoes"
|
| 24 |
+
},
|
| 25 |
+
"image_size": 224,
|
| 26 |
+
"initializer_range": 0.02,
|
| 27 |
+
"intermediate_size": 3072,
|
| 28 |
+
"label2id": {
|
| 29 |
+
"battery": "0",
|
| 30 |
+
"biological": "1",
|
| 31 |
+
"brown-glass": "2",
|
| 32 |
+
"cardboard": "3",
|
| 33 |
+
"clothes": "4",
|
| 34 |
+
"green-glass": "5",
|
| 35 |
+
"metal": "6",
|
| 36 |
+
"paper": "7",
|
| 37 |
+
"plastic": "8",
|
| 38 |
+
"shoes": "9",
|
| 39 |
+
"trash": "10",
|
| 40 |
+
"white-glass": "11"
|
| 41 |
+
},
|
| 42 |
+
"layer_norm_eps": 1e-12,
|
| 43 |
+
"model_type": "vit",
|
| 44 |
+
"num_attention_heads": 12,
|
| 45 |
+
"num_channels": 3,
|
| 46 |
+
"num_hidden_layers": 12,
|
| 47 |
+
"patch_size": 16,
|
| 48 |
+
"problem_type": "single_label_classification",
|
| 49 |
+
"qkv_bias": true,
|
| 50 |
+
"torch_dtype": "float32",
|
| 51 |
+
"transformers_version": "4.46.1"
|
| 52 |
+
}
|
inference.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_name = "saved_model"
|
| 6 |
+
|
| 7 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
| 8 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
image_path = '/path/'
|
| 13 |
+
image = Image.open(image_path).convert('RGB')
|
| 14 |
+
|
| 15 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
with torch.no_grad():
|
| 18 |
+
outputs = model(**inputs)
|
| 19 |
+
logits = outputs.logits
|
| 20 |
+
|
| 21 |
+
predicted_class_idx = logits.argmax(-1).item()
|
| 22 |
+
|
| 23 |
+
classes = model.config.id2label
|
| 24 |
+
|
| 25 |
+
print(f"Predicted class: {classes[predicted_class_idx]}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f5e015ae0d2050e8ab7c4283977cf0368815bc8917b73cd34e69eece961579a7
|
| 3 |
+
size 343254736
|
preprocessor_config.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"do_normalize": true,
|
| 3 |
+
"do_rescale": true,
|
| 4 |
+
"do_resize": true,
|
| 5 |
+
"image_mean": [
|
| 6 |
+
0.5,
|
| 7 |
+
0.5,
|
| 8 |
+
0.5
|
| 9 |
+
],
|
| 10 |
+
"image_processor_type": "ViTFeatureExtractor",
|
| 11 |
+
"image_std": [
|
| 12 |
+
0.5,
|
| 13 |
+
0.5,
|
| 14 |
+
0.5
|
| 15 |
+
],
|
| 16 |
+
"resample": 2,
|
| 17 |
+
"rescale_factor": 0.00392156862745098,
|
| 18 |
+
"size": {
|
| 19 |
+
"height": 224,
|
| 20 |
+
"width": 224
|
| 21 |
+
}
|
| 22 |
+
}
|
train.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import math
|
| 3 |
+
from torchvision.datasets import ImageFolder
|
| 4 |
+
from torch.utils.data import DataLoader
|
| 5 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
| 6 |
+
import kagglehub
|
| 7 |
+
from torch.optim import AdamW
|
| 8 |
+
from transformers import get_scheduler
|
| 9 |
+
from tqdm.auto import tqdm
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
gpu_available = torch.cuda.is_available()
|
| 13 |
+
print("GPU available:", gpu_available)
|
| 14 |
+
|
| 15 |
+
if gpu_available:
|
| 16 |
+
print("GPU:", torch.cuda.get_device_name(0))
|
| 17 |
+
print("GPU count:", torch.cuda.device_count())
|
| 18 |
+
print("#memory avail:", torch.cuda.get_device_properties(0).total_memory / (1024 ** 3), "GB")
|
| 19 |
+
else:
|
| 20 |
+
print("No GPU available.")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
kaggle_path = kagglehub.dataset_download("mostafaabla/garbage-classification")
|
| 24 |
+
|
| 25 |
+
folder_root_path = kaggle_path+'/garbage_classification'
|
| 26 |
+
print("Path to dataset files:", kaggle_path)
|
| 27 |
+
|
| 28 |
+
ds = ImageFolder(folder_root_path)
|
| 29 |
+
indices = torch.randperm(len(ds)).tolist()
|
| 30 |
+
n_val = math.floor(len(indices) * .20)
|
| 31 |
+
train_ds = torch.utils.data.Subset(ds, indices[:-n_val])
|
| 32 |
+
val_ds = torch.utils.data.Subset(ds, indices[-n_val:])
|
| 33 |
+
|
| 34 |
+
print(ds.classes)
|
| 35 |
+
|
| 36 |
+
label2id = {}
|
| 37 |
+
id2label = {}
|
| 38 |
+
|
| 39 |
+
for i, class_name in enumerate(ds.classes):
|
| 40 |
+
label2id[class_name] = str(i)
|
| 41 |
+
id2label[str(i)] = class_name
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class ImageClassificationCollator:
|
| 45 |
+
def __init__(self, feature_extractor):
|
| 46 |
+
self.feature_extractor = feature_extractor
|
| 47 |
+
|
| 48 |
+
def __call__(self, batch):
|
| 49 |
+
encodings = self.feature_extractor([x[0] for x in batch], return_tensors='pt')
|
| 50 |
+
encodings['labels'] = torch.tensor([x[1] for x in batch], dtype=torch.long)
|
| 51 |
+
return encodings
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224-in21k')
|
| 55 |
+
|
| 56 |
+
model = ViTForImageClassification.from_pretrained(
|
| 57 |
+
'google/vit-base-patch16-224-in21k',
|
| 58 |
+
num_labels=len(label2id),
|
| 59 |
+
label2id=label2id,
|
| 60 |
+
id2label=id2label
|
| 61 |
+
)
|
| 62 |
+
collator = ImageClassificationCollator(feature_extractor)
|
| 63 |
+
train_loader = DataLoader(train_ds, batch_size=16, collate_fn=collator, num_workers=2, shuffle=True)
|
| 64 |
+
val_loader = DataLoader(val_ds, batch_size=16, collate_fn=collator, num_workers=2)
|
| 65 |
+
|
| 66 |
+
optimizer = AdamW(model.parameters(), lr=5e-5)
|
| 67 |
+
|
| 68 |
+
num_epochs = 10
|
| 69 |
+
num_training_steps = num_epochs * len(train_loader)
|
| 70 |
+
lr_scheduler = get_scheduler(
|
| 71 |
+
name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
| 75 |
+
model.to(device)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
progress_bar = tqdm(range(num_training_steps))
|
| 79 |
+
|
| 80 |
+
model.train()
|
| 81 |
+
for epoch in range(num_epochs):
|
| 82 |
+
for batch in train_loader:
|
| 83 |
+
batch = {k: v.to(device) for k, v in batch.items()}
|
| 84 |
+
outputs = model(**batch)
|
| 85 |
+
loss = outputs.loss
|
| 86 |
+
loss.backward()
|
| 87 |
+
|
| 88 |
+
optimizer.step()
|
| 89 |
+
lr_scheduler.step()
|
| 90 |
+
optimizer.zero_grad()
|
| 91 |
+
progress_bar.update(1)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
model.eval()
|
| 95 |
+
for batch in val_loader:
|
| 96 |
+
batch = {k: v.to(device) for k, v in batch.items()}
|
| 97 |
+
with torch.no_grad():
|
| 98 |
+
outputs = model(**batch)
|
| 99 |
+
|
| 100 |
+
logits = outputs.logits
|
| 101 |
+
predictions = torch.argmax(logits, dim=-1)
|
| 102 |
+
|
| 103 |
+
import os
|
| 104 |
+
|
| 105 |
+
save_directory = "./saved_model"
|
| 106 |
+
if not os.path.exists(save_directory):
|
| 107 |
+
os.makedirs(save_directory)
|
| 108 |
+
|
| 109 |
+
model.save_pretrained(save_directory)
|
| 110 |
+
feature_extractor.save_pretrained(save_directory)
|
| 111 |
+
|
| 112 |
+
print(f"Model saved: {save_directory}")
|