Spaces:
Runtime error
Runtime error
Viihtorugo
commited on
Commit
·
8436dd5
1
Parent(s):
b53de58
Add the files
Browse files- app.py +17 -0
- requirements.txt +2 -0
- train.py +41 -0
app.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Carregar o modelo diretamente do Hugging Face Hub
|
| 5 |
+
classifier = pipeline("image-classification", model="mestrevh/computer-vision-cifar-10")
|
| 6 |
+
|
| 7 |
+
# Função de classificação
|
| 8 |
+
def predict_image(image):
|
| 9 |
+
return classifier(image)
|
| 10 |
+
|
| 11 |
+
# Interface Gradio
|
| 12 |
+
interface = gr.Interface(fn=predict_image,
|
| 13 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 14 |
+
outputs="label",
|
| 15 |
+
live=True)
|
| 16 |
+
|
| 17 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
train.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import Trainer, TrainingArguments
|
| 2 |
+
from datasets import load_dataset
|
| 3 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
| 4 |
+
|
| 5 |
+
# Carregar o dataset (exemplo com o dataset CIFAR-10)
|
| 6 |
+
dataset = load_dataset("cifar10")
|
| 7 |
+
|
| 8 |
+
# Carregar o modelo pré-treinado e o feature extractor
|
| 9 |
+
model = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 10 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 11 |
+
|
| 12 |
+
# Preprocessamento
|
| 13 |
+
def preprocess_function(examples):
|
| 14 |
+
return feature_extractor(examples["image"], return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
# Aplicando o preprocessamento ao dataset
|
| 17 |
+
dataset = dataset.map(preprocess_function, batched=True)
|
| 18 |
+
|
| 19 |
+
# Definir os parâmetros de treinamento
|
| 20 |
+
training_args = TrainingArguments(
|
| 21 |
+
output_dir="./results",
|
| 22 |
+
evaluation_strategy="epoch",
|
| 23 |
+
learning_rate=2e-5,
|
| 24 |
+
per_device_train_batch_size=16,
|
| 25 |
+
per_device_eval_batch_size=64,
|
| 26 |
+
num_train_epochs=3,
|
| 27 |
+
weight_decay=0.01,
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
trainer = Trainer(
|
| 31 |
+
model=model,
|
| 32 |
+
args=training_args,
|
| 33 |
+
train_dataset=dataset["train"],
|
| 34 |
+
eval_dataset=dataset["test"],
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Treinar o modelo
|
| 38 |
+
trainer.train()
|
| 39 |
+
|
| 40 |
+
model.save_pretrained("./computer-vision-cifar-10")
|
| 41 |
+
feature_extractor.save_pretrained("./computer-vision-cifar-10")
|