rararara9999 commited on
Commit
aec5168
·
verified ·
1 Parent(s): b2ef3fb

Delete app.py

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