rararara9999 commited on
Commit
06fd26d
·
verified ·
1 Parent(s): a1a6f94

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ subprocess.run(["pip", "install", "scikit-learn"])
10
+
11
+ # Load the necessary libraries
12
+ from datasets import load_dataset
13
+ from transformers import AutoModelForImageClassification, AutoImageProcessor, TrainingArguments, Trainer
14
+ import torch
15
+ import torchvision.transforms as transforms
16
+ import numpy as np
17
+ from evaluate import load
18
+
19
+ # Load the dataset from Hugging Face Hub
20
+ dataset = load_dataset("DamarJati/Face-Mask-Detection")
21
+
22
+ # Define the labels
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
+ # Load the pre-trained model and processor
30
+ model_checkpoint = "microsoft/resnet-50"
31
+ model = AutoModelForImageClassification.from_pretrained(model_checkpoint, num_labels=len(labels))
32
+ image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
33
+
34
+ # Define the image transformations
35
+ transform = transforms.Compose([
36
+ transforms.Resize((224, 224)),
37
+ transforms.ToTensor(),
38
+ transforms.Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
39
+ ])
40
+
41
+ # Preprocess the dataset
42
+ def preprocess(example_batch):
43
+ example_batch["pixel_values"] = [transform(image.convert("RGB")) for image in example_batch["image"]]
44
+ return example_batch
45
+
46
+ dataset = dataset.with_transform(preprocess)
47
+
48
+ # Split the dataset into training and validation sets
49
+ splits = dataset["train"].train_test_split(test_size=0.3)
50
+ train_ds = splits['train']
51
+ val_ds = splits['test']
52
+
53
+ # Define the evaluation metric
54
+ metric = load("accuracy")
55
+
56
+ def compute_metrics(eval_pred):
57
+ predictions = np.argmax(eval_pred.predictions, axis=1)
58
+ return metric.compute(predictions=predictions, references=eval_pred.label_ids)
59
+
60
+ # Define the data collator
61
+ def collate_fn(examples):
62
+ pixel_values = torch.stack([example["pixel_values"] for example in examples])
63
+ labels = torch.tensor([example["label"] for example in examples])
64
+ return {"pixel_values": pixel_values, "labels": labels}
65
+
66
+ # Define the training arguments
67
+ args = TrainingArguments(
68
+ output_dir="./results",
69
+ per_device_eval_batch_size=128,
70
+ remove_unused_columns=False,
71
+ )
72
+
73
+ # Initialize the Trainer
74
+ trainer = Trainer(
75
+ model=model,
76
+ args=args,
77
+ eval_dataset=val_ds,
78
+ compute_metrics=compute_metrics,
79
+ data_collator=collate_fn,
80
+ )
81
+
82
+ # Evaluate the pre-trained model
83
+ metrics = trainer.evaluate()
84
+ print(metrics)