Spaces:
Sleeping
Sleeping
second commit
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +4 -0
- Dockerfile +10 -0
- app.py +33 -4
- predict_model.py +63 -0
- raw-img/alf/15835_1.8.jpg +3 -0
- raw-img/alf/25d2bca0eeb53d25f03324ca0c81b34e-51.jpg_ooooxooxox_1280x720.jpg +3 -0
- raw-img/alf/42703_l2.jpg +3 -0
- raw-img/alf/647b6a6f-2203-4b10-a029-bd2a581d2fae.jpg +3 -0
- raw-img/alf/64bd2aaed9afb_o_full.jpg +3 -0
- raw-img/alf/8d2d6faa1012235783d06d42f8612ab4dee6a925fa0d1ffe26c1f6b5249dede6-191.jpg_ooooxooxox_1280x720.jpg +3 -0
- raw-img/alf/B2CCP976-0-NS-Alf-Chia-Pet_02.png +3 -0
- raw-img/alf/Bez-násdcscdsvzvu-1.jpg +3 -0
- raw-img/alf/LANDSCAPE_480.jpg +3 -0
- raw-img/alf/MV5BZGMxYjlhZmYtMThkZi00MDNiLThmNWItMjljZmUyZmIzYTJlXkEyXkFqcGdeQXVyNTM3MDMyMDQ@._V1_.jpg +3 -0
- raw-img/alf/alf-el-extraterrestre-adicto-a-los-gatos-que-llego-a-la-tierra-y-triunfo-en-television.jpg +3 -0
- raw-img/alf/gf-rCpJ-325V-QhQ4_.jpg +3 -0
- raw-img/alf/hqdefault.jpg +3 -0
- raw-img/alf/imagessad.jpg +3 -0
- raw-img/alf/s-l400.jpg +3 -0
- raw-img/alf/sddad.jpg +3 -0
- raw-img/alf/sddefault.jpg +3 -0
- raw-img/alf/z28687031AMP,Al.jpg +3 -0
- raw-img/butterfly/OIP--04ndbWy7I04gsPgu9qOeQHaHs.jpeg +3 -0
- raw-img/butterfly/OIP--3xxJkezSkNzxrQaxlhv-QHaHa.jpeg +3 -0
- raw-img/butterfly/OIP--4zW12JF-JpwKcDtEK7thgHaLC.jpeg +3 -0
- raw-img/butterfly/OIP--7K5JzW1ZEmrY2YRyVjFCQHaE7.jpeg +3 -0
- raw-img/butterfly/OIP--8RSoaU1iEvZhemy7FlYegHaD9.jpeg +3 -0
- raw-img/butterfly/OIP--BwCcfYAvrI0U8yYgWplXwHaF1.jpeg +3 -0
- raw-img/butterfly/OIP--CDKKVE9j_HyFGxf8DAV7wHaJQ.jpeg +3 -0
- raw-img/butterfly/OIP--ENWG-ez2NKhOnDxPMCeQAHaEA.jpeg +3 -0
- raw-img/butterfly/OIP--Mpfd_ozZWI-BuFQkizVrwHaFj.jpeg +3 -0
- raw-img/butterfly/OIP--MyP_OSXxDXRp6GD5Pci0wHaGS.jpeg +3 -0
- raw-img/butterfly/OIP--RuA1gbd_7OnfNia4Ao5aQAAAA.jpeg +3 -0
- raw-img/butterfly/OIP--SUPsDiwMnFmvHar3xCmjgHaFj.jpeg +3 -0
- raw-img/butterfly/OIP--VatM86VAW7kByE6tVfTfgHaHV.jpeg +3 -0
- raw-img/butterfly/OIP--YxmjyUUbmVOTOwA5fv3XgHaFL.jpeg +3 -0
- raw-img/butterfly/OIP--eJJJ5u2_KgNggs6GsMtswHaHk.jpeg +3 -0
- raw-img/butterfly/OIP--gEvtip9-FSfOy_MWMXSxQHaJ6.jpeg +3 -0
- raw-img/butterfly/OIP--lGiTfH7hOR6PZ238zlM9QHaE7.jpeg +3 -0
- raw-img/butterfly/OIP--mHsZ6cdBGKHe-NOtHgERgHaD-.jpeg +3 -0
- raw-img/butterfly/OIP--oJqQhn66-DKxbj2skHKuAHaCv.jpeg +3 -0
- raw-img/butterfly/OIP--ohLNsXa1I98upPDakBeBAHaE6.jpeg +3 -0
- raw-img/butterfly/OIP--r8lAMrI_Mbj1zCh4WroTgHaHa.jpeg +3 -0
- raw-img/butterfly/OIP--uJxQZUw1ibjIJDEuEXzpAHaEo.jpeg +3 -0
- raw-img/butterfly/OIP--uyxspAMAoa6IkxtgJWdYAHaE7.jpeg +3 -0
- raw-img/butterfly/OIP--v_vx0B7J5lpKWjLjknadQAAAA.jpeg +3 -0
- raw-img/butterfly/OIP-0-4j2LrYb7bKSeuUdc2yJQHaLX.jpeg +3 -0
- raw-img/butterfly/OIP-0-M4adMCfm8xuWUCy5IXdQHaKe.jpeg +3 -0
- raw-img/butterfly/OIP-0-OGDZB7ms6WPw21pssBbgHaFj.jpeg +3 -0
- raw-img/butterfly/OIP-02MfXWiFtDKuR4G4uMsjeAHaE7.jpeg +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,7 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
39 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM pytorch/pytorch:1.12.0-cuda11.3-cudnn8-runtime
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
7 |
+
|
8 |
+
COPY . /code
|
9 |
+
|
10 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
@@ -1,7 +1,36 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from train_model import train
|
4 |
+
from predict_model import predict_all
|
5 |
|
6 |
+
def train_model():
|
7 |
+
train()
|
8 |
+
return "Model trained and saved as animal_classifier_resnet.pth"
|
9 |
|
10 |
+
def download_model():
|
11 |
+
return "animal_classifier_resnet.pth"
|
12 |
+
|
13 |
+
def run_predictions():
|
14 |
+
results = predict_all()
|
15 |
+
return "\n".join(results)
|
16 |
+
|
17 |
+
with gr.Blocks() as demo:
|
18 |
+
gr.Markdown("# Animal Classifier Model")
|
19 |
+
|
20 |
+
with gr.Tab("Train"):
|
21 |
+
train_button = gr.Button("Train Model")
|
22 |
+
train_output = gr.Textbox()
|
23 |
+
train_button.click(train_model, outputs=train_output)
|
24 |
+
|
25 |
+
with gr.Tab("Predict"):
|
26 |
+
predict_button = gr.Button("Run Predictions")
|
27 |
+
predict_output = gr.Textbox()
|
28 |
+
predict_button.click(run_predictions, outputs=predict_output)
|
29 |
+
|
30 |
+
with gr.Tab("Download"):
|
31 |
+
gr.Markdown("## Download Trained Model")
|
32 |
+
download_button = gr.Button("Download Model")
|
33 |
+
download_button.click(download_model, outputs=gr.File())
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
demo.launch()
|
predict_model.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
from torchvision import datasets, transforms, models
|
4 |
+
import os
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Ustawienia parametrów modelu
|
8 |
+
img_width, img_height = 224, 224 # Wymiary obrazu wymagane przez model ResNet
|
9 |
+
model_path = 'animal_classifier_resnet.pth' # Ścieżka do wytrenowanego modelu
|
10 |
+
|
11 |
+
# Sprawdzenie, czy jest dostępny GPU i przypisanie urządzenia do zmiennej `device`
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
print(f"Using device: {device}")
|
14 |
+
|
15 |
+
# Wyłączenie CuDNN, chyba że twoja karta wspiera bibliotekę CuDNN (NVIDIA CUDA Deep Neural Network library)
|
16 |
+
# torch.backends.cudnn.enabled = False
|
17 |
+
|
18 |
+
# Transformacje danych wejściowych (zmiana rozmiaru, normalizacja)
|
19 |
+
transform = transforms.Compose([
|
20 |
+
transforms.Resize((img_width, img_height)), # Zmiana rozmiaru obrazu
|
21 |
+
transforms.ToTensor(), # Konwersja obrazu do tensoru
|
22 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) # Normalizacja obrazu
|
23 |
+
])
|
24 |
+
|
25 |
+
# Inicjalizacja modelu ResNet18 bez wstępnych wag
|
26 |
+
model = models.resnet18(weights=None)
|
27 |
+
num_ftrs = model.fc.in_features # Liczba wejściowych cech ostatniej warstwy
|
28 |
+
model.fc = nn.Linear(num_ftrs, len(datasets.ImageFolder('raw-img', transform=transform).classes)) # Zastąpienie ostatniej warstwy dopasowanej do liczby klas w danych
|
29 |
+
|
30 |
+
# Przeniesienie modelu na GPU, jeśli jest dostępny
|
31 |
+
model = model.to(device)
|
32 |
+
model.load_state_dict(torch.load(model_path, map_location=device)) # Wczytanie wytrenowanych wag modelu
|
33 |
+
model.eval() # Ustawienie modelu w tryb ewaluacyjny
|
34 |
+
|
35 |
+
# Funkcja do przewidywania klasy obrazu
|
36 |
+
def predict(image_path):
|
37 |
+
image = Image.open(image_path).convert('RGB') # Otworzenie obrazu i konwersja do RGB
|
38 |
+
image = transform(image).unsqueeze(0) # Zastosowanie transformacji i dodanie wymiaru batch
|
39 |
+
image = image.to(device) # Przeniesienie obrazu na GPU, jeśli jest dostępny
|
40 |
+
with torch.no_grad(): # Wyłączenie gradientów dla przewidywania
|
41 |
+
output = model(image) # Przekazanie obrazu przez model
|
42 |
+
_, predicted = torch.max(output, 1) # Wybranie klasy z najwyższym prawdopodobieństwem
|
43 |
+
return datasets.ImageFolder('raw-img', transform=transform).classes[predicted.item()] # Zwrócenie nazwy klasy
|
44 |
+
|
45 |
+
# Przykład użycia funkcji predict do przewidywania klasy obrazów w katalogu `recognize`
|
46 |
+
test_image_dir = 'recognize' # Ścieżka do katalogu z obrazami do przewidywania
|
47 |
+
for filename in os.listdir(test_image_dir): # Pętla przez pliki w katalogu
|
48 |
+
if filename.lower().endswith(('.jpg', '.jpeg', '.png')): # Sprawdzenie rozszerzenia pliku
|
49 |
+
image_path = os.path.join(test_image_dir, filename) # Pełna ścieżka do pliku
|
50 |
+
print(f"Prediction for {filename}: {predict(image_path)}") # Wyświetlenie przewidywanej klasy dla obrazu
|
51 |
+
|
52 |
+
def predict_all():
|
53 |
+
results = []
|
54 |
+
test_image_dir = 'recognize'
|
55 |
+
for filename in os.listdir(test_image_dir):
|
56 |
+
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
|
57 |
+
image_path = os.path.join(test_image_dir, filename)
|
58 |
+
prediction = predict(image_path)
|
59 |
+
results.append(f"Prediction for {filename}: {prediction}")
|
60 |
+
return results
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
print("\n".join(predict_all()))
|
raw-img/alf/15835_1.8.jpg
ADDED
Git LFS Details
|
raw-img/alf/25d2bca0eeb53d25f03324ca0c81b34e-51.jpg_ooooxooxox_1280x720.jpg
ADDED
Git LFS Details
|
raw-img/alf/42703_l2.jpg
ADDED
Git LFS Details
|
raw-img/alf/647b6a6f-2203-4b10-a029-bd2a581d2fae.jpg
ADDED
Git LFS Details
|
raw-img/alf/64bd2aaed9afb_o_full.jpg
ADDED
Git LFS Details
|
raw-img/alf/8d2d6faa1012235783d06d42f8612ab4dee6a925fa0d1ffe26c1f6b5249dede6-191.jpg_ooooxooxox_1280x720.jpg
ADDED
Git LFS Details
|
raw-img/alf/B2CCP976-0-NS-Alf-Chia-Pet_02.png
ADDED
Git LFS Details
|
raw-img/alf/Bez-násdcscdsvzvu-1.jpg
ADDED
Git LFS Details
|
raw-img/alf/LANDSCAPE_480.jpg
ADDED
Git LFS Details
|
raw-img/alf/MV5BZGMxYjlhZmYtMThkZi00MDNiLThmNWItMjljZmUyZmIzYTJlXkEyXkFqcGdeQXVyNTM3MDMyMDQ@._V1_.jpg
ADDED
Git LFS Details
|
raw-img/alf/alf-el-extraterrestre-adicto-a-los-gatos-que-llego-a-la-tierra-y-triunfo-en-television.jpg
ADDED
Git LFS Details
|
raw-img/alf/gf-rCpJ-325V-QhQ4_.jpg
ADDED
Git LFS Details
|
raw-img/alf/hqdefault.jpg
ADDED
Git LFS Details
|
raw-img/alf/imagessad.jpg
ADDED
Git LFS Details
|
raw-img/alf/s-l400.jpg
ADDED
Git LFS Details
|
raw-img/alf/sddad.jpg
ADDED
Git LFS Details
|
raw-img/alf/sddefault.jpg
ADDED
Git LFS Details
|
raw-img/alf/z28687031AMP,Al.jpg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--04ndbWy7I04gsPgu9qOeQHaHs.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--3xxJkezSkNzxrQaxlhv-QHaHa.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--4zW12JF-JpwKcDtEK7thgHaLC.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--7K5JzW1ZEmrY2YRyVjFCQHaE7.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--8RSoaU1iEvZhemy7FlYegHaD9.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--BwCcfYAvrI0U8yYgWplXwHaF1.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--CDKKVE9j_HyFGxf8DAV7wHaJQ.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--ENWG-ez2NKhOnDxPMCeQAHaEA.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--Mpfd_ozZWI-BuFQkizVrwHaFj.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--MyP_OSXxDXRp6GD5Pci0wHaGS.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--RuA1gbd_7OnfNia4Ao5aQAAAA.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--SUPsDiwMnFmvHar3xCmjgHaFj.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--VatM86VAW7kByE6tVfTfgHaHV.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--YxmjyUUbmVOTOwA5fv3XgHaFL.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--eJJJ5u2_KgNggs6GsMtswHaHk.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--gEvtip9-FSfOy_MWMXSxQHaJ6.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--lGiTfH7hOR6PZ238zlM9QHaE7.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--mHsZ6cdBGKHe-NOtHgERgHaD-.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--oJqQhn66-DKxbj2skHKuAHaCv.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--ohLNsXa1I98upPDakBeBAHaE6.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--r8lAMrI_Mbj1zCh4WroTgHaHa.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--uJxQZUw1ibjIJDEuEXzpAHaEo.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--uyxspAMAoa6IkxtgJWdYAHaE7.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP--v_vx0B7J5lpKWjLjknadQAAAA.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP-0-4j2LrYb7bKSeuUdc2yJQHaLX.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP-0-M4adMCfm8xuWUCy5IXdQHaKe.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP-0-OGDZB7ms6WPw21pssBbgHaFj.jpeg
ADDED
Git LFS Details
|
raw-img/butterfly/OIP-02MfXWiFtDKuR4G4uMsjeAHaE7.jpeg
ADDED
Git LFS Details
|