Spaces:
Sleeping
Sleeping
rararara9999
commited on
Commit
•
55fe921
1
Parent(s):
31ee0c4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
# Install necessary packages
|
4 |
+
subprocess.run(["pip", "install", "-U", "git+https://github.com/huggingface/transformers.git"])
|
5 |
+
subprocess.run(["pip", "install", "-U", "git+https://github.com/huggingface/accelerate.git"])
|
6 |
+
subprocess.run(["pip", "install", "datasets"])
|
7 |
+
subprocess.run(["pip", "install", "evaluate"])
|
8 |
+
|
9 |
+
# Rest of your code
|
10 |
+
model_checkpoint = "microsoft/resnet-50"
|
11 |
+
batch_size = 128
|
12 |
+
|
13 |
+
from datasets import load_dataset
|
14 |
+
|
15 |
+
# Load the dataset from Hugging Face Hub
|
16 |
+
dataset = load_dataset("DamarJati/Face-Mask-Detection")
|
17 |
+
|
18 |
+
# Continue with the rest of your script...
|
19 |
+
labels = dataset["train"].features["label"].names
|
20 |
+
label2id, id2label = dict(), dict()
|
21 |
+
for i, label in enumerate(labels):
|
22 |
+
label2id[label] = i
|
23 |
+
id2label[i] = label
|
24 |
+
|
25 |
+
from transformers import AutoImageProcessor
|
26 |
+
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
|
27 |
+
|
28 |
+
from torchvision.transforms import (
|
29 |
+
CenterCrop, Compose, Normalize, RandomHorizontalFlip, RandomResizedCrop, Resize, ToTensor, ColorJitter, RandomRotation
|
30 |
+
)
|
31 |
+
|
32 |
+
normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
|
33 |
+
if "height" in image_processor.size:
|
34 |
+
size = (image_processor.size["height"], image_processor.size["width"])
|
35 |
+
crop_size = size
|
36 |
+
max_size = None
|
37 |
+
elif "shortest_edge" in image_processor.size:
|
38 |
+
size = image_processor.size["shortest_edge"]
|
39 |
+
crop_size = (size, size)
|
40 |
+
max_size = image_processor.size.get("longest_edge")
|
41 |
+
|
42 |
+
train_transforms = Compose(
|
43 |
+
[
|
44 |
+
RandomResizedCrop(crop_size),
|
45 |
+
RandomHorizontalFlip(),
|
46 |
+
RandomRotation(degrees=15),
|
47 |
+
ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1),
|
48 |
+
ToTensor(),
|
49 |
+
normalize,
|
50 |
+
]
|
51 |
+
)
|
52 |
+
|
53 |
+
val_transforms = Compose(
|
54 |
+
[
|
55 |
+
Resize(size),
|
56 |
+
CenterCrop(crop_size),
|
57 |
+
RandomRotation(degrees=15),
|
58 |
+
ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1),
|
59 |
+
ToTensor(),
|
60 |
+
normalize,
|
61 |
+
]
|
62 |
+
)
|
63 |
+
|
64 |
+
def preprocess_train(example_batch):
|
65 |
+
example_batch["pixel_values"] = [
|
66 |
+
train_transforms(image.convert("RGB")) for image in example_batch["image"]
|
67 |
+
]
|
68 |
+
return example_batch
|
69 |
+
|
70 |
+
def preprocess_val(example_batch):
|
71 |
+
example_batch["pixel_values"] = [val_transforms(image.convert("RGB")) for image in example_batch["image"]]
|
72 |
+
return example_batch
|
73 |
+
|
74 |
+
splits = dataset["train"].train_test_split(test_size=0.3)
|
75 |
+
train_ds = splits['train']
|
76 |
+
val_ds = splits['test']
|
77 |
+
|
78 |
+
train_ds.set_transform(preprocess_train)
|
79 |
+
val_ds.set_transform(preprocess_val)
|
80 |
+
|
81 |
+
from transformers import AutoModelForImageClassification, TrainingArguments, Trainer
|
82 |
+
|
83 |
+
model = AutoModelForImageClassification.from_pretrained(model_checkpoint,
|
84 |
+
label2id=label2id,
|
85 |
+
id2label=id2label,
|
86 |
+
ignore_mismatched_sizes = True)
|
87 |
+
|
88 |
+
model_name = model_checkpoint.split("/")[-1]
|
89 |
+
|
90 |
+
args = TrainingArguments(
|
91 |
+
f"{model_name}-finetuned",
|
92 |
+
remove_unused_columns=False,
|
93 |
+
evaluation_strategy = "epoch",
|
94 |
+
save_strategy = "epoch",
|
95 |
+
save_total_limit = 5,
|
96 |
+
learning_rate=1e-3,
|
97 |
+
per_device_train_batch_size=batch_size,
|
98 |
+
gradient_accumulation_steps=2,
|
99 |
+
per_device_eval_batch_size=batch_size,
|
100 |
+
num_train_epochs=2,
|
101 |
+
warmup_ratio=0.1,
|
102 |
+
weight_decay=0.01,
|
103 |
+
lr_scheduler_type="cosine",
|
104 |
+
logging_steps=10,
|
105 |
+
load_best_model_at_end=True,
|
106 |
+
metric_for_best_model="accuracy",)
|
107 |
+
|
108 |
+
import numpy as np
|
109 |
+
|
110 |
+
def compute_metrics(eval_pred):
|
111 |
+
"""Computes accuracy on a batch of predictions"""
|
112 |
+
predictions = np.argmax(eval_pred.predictions, axis=1)
|
113 |
+
return metric.compute(predictions=predictions, references=eval_pred.label_ids)
|
114 |
+
|
115 |
+
import torch
|
116 |
+
|
117 |
+
def collate_fn(examples):
|
118 |
+
pixel_values = torch.stack([example["pixel_values"] for example in examples])
|
119 |
+
labels = torch.tensor([example["label"] for example in examples])
|
120 |
+
return {"pixel_values": pixel_values, "labels": labels}
|
121 |
+
|
122 |
+
trainer = Trainer(model,
|
123 |
+
args,
|
124 |
+
train_dataset=train_ds,
|
125 |
+
eval_dataset=val_ds,
|
126 |
+
tokenizer=image_processor,
|
127 |
+
compute_metrics=compute_metrics,
|
128 |
+
data_collator=collate_fn,)
|
129 |
+
|
130 |
+
train_results = trainer.train()
|
131 |
+
# Save the model
|
132 |
+
trainer.save_model()
|
133 |
+
trainer.log_metrics("train", train_results.metrics)
|
134 |
+
trainer.save_metrics("train", train_results.metrics)
|
135 |
+
trainer.save_state()
|
136 |
+
|
137 |
+
metrics = trainer.evaluate()
|
138 |
+
# Log and save evaluation metrics
|
139 |
+
trainer.log_metrics("eval", metrics)
|
140 |
+
trainer.save_metrics("eval", metrics)
|