poziom1.py
· 1.9 KiB · Python
Raw
# Importowanie bibliotek
# Torch - głębokie sieci neuronowe (ang. Deep Neural Networks)
import torch
from torchvision import transforms
from PIL import Image
import timm
# Wczytaj model Swin Transformer z biblioteki timm
# ta wersja jest wyuczona na zbiorze ImageNet-21k
nazwa_modelu = "swin_large_patch4_window7_224"
klasyfikator = timm.create_model(nazwa_modelu, pretrained=True)
# klasyfikator.eval()
# Pobierz zbiór ImageNet (1000 klas)
import json
imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
import requests
labels = json.loads(requests.get(imagenet_labels_url).text)
# Potok przetwarzania obrazu na potrzeby klasyfikatora
preprocess = transforms.Compose([
transforms.Resize((224, 224)), # model Swin Transformer bierze na wejściu obrazy 224x224
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), # Normalizacja
])
def recognize_image(img_path):
"""Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje"""
img = Image.open(img_path).convert("RGB")
# Dodatkowy wymiar na wejściu
img_tensor = preprocess(img).unsqueeze(0)
# Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego
with torch.no_grad():
logits = klasyfikator(img_tensor)
probabilities = torch.nn.functional.softmax(logits, dim=-1)
top5_prob, top5_catid = torch.topk(probabilities, 5)
# Przeliczenie prawdopodobieństw na etykiety klas
results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])]
return results
if __name__ == "__main__":
img_path = "obrazek.jpg"
wyjscie_klasyfikatora = recognize_image(img_path)
for i, (label, confidence) in enumerate(wyjscie_klasyfikatora):
print(f"{i + 1}: {label} ({confidence:.2f})")
1 | # Importowanie bibliotek |
2 | # Torch - głębokie sieci neuronowe (ang. Deep Neural Networks) |
3 | import torch |
4 | from torchvision import transforms |
5 | from PIL import Image |
6 | import timm |
7 | |
8 | # Wczytaj model Swin Transformer z biblioteki timm |
9 | # ta wersja jest wyuczona na zbiorze ImageNet-21k |
10 | nazwa_modelu = "swin_large_patch4_window7_224" |
11 | klasyfikator = timm.create_model(nazwa_modelu, pretrained=True) |
12 | # klasyfikator.eval() |
13 | |
14 | # Pobierz zbiór ImageNet (1000 klas) |
15 | import json |
16 | imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json" |
17 | import requests |
18 | labels = json.loads(requests.get(imagenet_labels_url).text) |
19 | |
20 | # Potok przetwarzania obrazu na potrzeby klasyfikatora |
21 | preprocess = transforms.Compose([ |
22 | transforms.Resize((224, 224)), # model Swin Transformer bierze na wejściu obrazy 224x224 |
23 | transforms.ToTensor(), |
24 | transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), # Normalizacja |
25 | ]) |
26 | |
27 | |
28 | def recognize_image(img_path): |
29 | """Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje""" |
30 | img = Image.open(img_path).convert("RGB") |
31 | # Dodatkowy wymiar na wejściu |
32 | img_tensor = preprocess(img).unsqueeze(0) |
33 | |
34 | # Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego |
35 | with torch.no_grad(): |
36 | logits = klasyfikator(img_tensor) |
37 | probabilities = torch.nn.functional.softmax(logits, dim=-1) |
38 | top5_prob, top5_catid = torch.topk(probabilities, 5) |
39 | |
40 | # Przeliczenie prawdopodobieństw na etykiety klas |
41 | results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])] |
42 | return results |
43 | |
44 | |
45 | if __name__ == "__main__": |
46 | img_path = "obrazek.jpg" |
47 | wyjscie_klasyfikatora = recognize_image(img_path) |
48 | for i, (label, confidence) in enumerate(wyjscie_klasyfikatora): |
49 | print(f"{i + 1}: {label} ({confidence:.2f})") |
50 |
poziom2_flask.py
· 2.5 KiB · Python
Raw
from flask import Flask, request, render_template, redirect, url_for
import torch
from torchvision import transforms
from PIL import Image
import timm
import json
import requests
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Folder na załadowane obrazki
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Wczytaj model Swin Transformer z biblioteki timm
# ta wersja jest wyuczona na zbiorze ImageNet-21k
nazwa_modelu = "swin_large_patch4_window7_224"
klasyfikator = timm.create_model(nazwa_modelu, pretrained=True)
#klasyfikator.eval()
# Pobierz zbiór ImageNet (1000 klas)
imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json"
labels = json.loads(requests.get(imagenet_labels_url).text)
# Potok przetwarzania obrazu na potrzeby klasyfikatora
preprocess = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])
def recognize_image(img_path):
"""
Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje
"""
img = Image.open(img_path).convert("RGB")
# Dodatkowy wymiar na wejściu
img_tensor = preprocess(img).unsqueeze(0)
# Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego
with torch.no_grad():
logits = klasyfikator(img_tensor)
probabilities = torch.nn.functional.softmax(logits, dim=-1)
top5_prob, top5_catid = torch.topk(probabilities, 5)
# Przeliczenie prawdopodobieństw na etykiety klas
results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])]
return results
@app.route('/')
def index():
return render_template("index.html")
@app.route('/classify', methods=['POST'])
def classify_image():
if 'image' not in request.files:
return redirect(url_for('index'))
image = request.files['image']
if image.filename == '':
return redirect(url_for('index'))
try:
# Zapisz wczytany obraz do katalogu UPLOAD_FOLDER
image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(image_path)
# Klasyfikacja
wyjscie_klasyfikatora = recognize_image(image_path)
return render_template("result.html", image_path=image_path, predictions=wyjscie_klasyfikatora)
except Exception as e:
return str(e), 500
if __name__ == "__main__":
app.run(debug=True)
1 | from flask import Flask, request, render_template, redirect, url_for |
2 | import torch |
3 | from torchvision import transforms |
4 | from PIL import Image |
5 | import timm |
6 | import json |
7 | import requests |
8 | import os |
9 | |
10 | app = Flask(__name__) |
11 | UPLOAD_FOLDER = 'static/uploads' |
12 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER |
13 | |
14 | # Folder na załadowane obrazki |
15 | os.makedirs(UPLOAD_FOLDER, exist_ok=True) |
16 | |
17 | # Wczytaj model Swin Transformer z biblioteki timm |
18 | # ta wersja jest wyuczona na zbiorze ImageNet-21k |
19 | nazwa_modelu = "swin_large_patch4_window7_224" |
20 | klasyfikator = timm.create_model(nazwa_modelu, pretrained=True) |
21 | #klasyfikator.eval() |
22 | |
23 | # Pobierz zbiór ImageNet (1000 klas) |
24 | imagenet_labels_url = "https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json" |
25 | labels = json.loads(requests.get(imagenet_labels_url).text) |
26 | |
27 | # Potok przetwarzania obrazu na potrzeby klasyfikatora |
28 | preprocess = transforms.Compose([ |
29 | transforms.Resize((224, 224)), |
30 | transforms.ToTensor(), |
31 | transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), |
32 | ]) |
33 | |
34 | def recognize_image(img_path): |
35 | """ |
36 | Wczytuje obraz z pliku, przetwarza zgodnie z potokiem preprocess a na koniec klasyfikuje |
37 | """ |
38 | img = Image.open(img_path).convert("RGB") |
39 | # Dodatkowy wymiar na wejściu |
40 | img_tensor = preprocess(img).unsqueeze(0) |
41 | |
42 | # Predykcja, czyli przypisanie prawdopodobieństw klas do obrazu wejściowego |
43 | with torch.no_grad(): |
44 | logits = klasyfikator(img_tensor) |
45 | probabilities = torch.nn.functional.softmax(logits, dim=-1) |
46 | top5_prob, top5_catid = torch.topk(probabilities, 5) |
47 | |
48 | # Przeliczenie prawdopodobieństw na etykiety klas |
49 | results = [(labels[catid], prob.item()) for catid, prob in zip(top5_catid[0], top5_prob[0])] |
50 | return results |
51 | |
52 | @app.route('/') |
53 | def index(): |
54 | return render_template("index.html") |
55 | |
56 | @app.route('/classify', methods=['POST']) |
57 | def classify_image(): |
58 | if 'image' not in request.files: |
59 | return redirect(url_for('index')) |
60 | |
61 | image = request.files['image'] |
62 | if image.filename == '': |
63 | return redirect(url_for('index')) |
64 | |
65 | try: |
66 | # Zapisz wczytany obraz do katalogu UPLOAD_FOLDER |
67 | image_path = os.path.join(app.config['UPLOAD_FOLDER'], image.filename) |
68 | image.save(image_path) |
69 | |
70 | # Klasyfikacja |
71 | wyjscie_klasyfikatora = recognize_image(image_path) |
72 | return render_template("result.html", image_path=image_path, predictions=wyjscie_klasyfikatora) |
73 | except Exception as e: |
74 | return str(e), 500 |
75 | |
76 | if __name__ == "__main__": |
77 | app.run(debug=True) |
78 |