rararara9999 commited on
Commit
11f58c0
·
verified ·
1 Parent(s): 7368f0a

Create app.py

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