molokhovdmitry
commited on
Commit
·
732a846
1
Parent(s):
62390f4
Add main.py, change model_execute.py
Browse filesFormer-commit-id: 4da2a83f1b275ec8320c2ed7bed56916c863e44b
Former-commit-id: 962450d9b097706e9622530cff0fa3091aa8a348
- .gitignore +1 -1
- main.py +91 -0
- model_execute.py +47 -34
- requirements.txt +3 -1
.gitignore
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
__pycache__
|
main.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import pickle
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
from model_execute import preprocess_images, output_to_names
|
8 |
+
from summarization import init_model_and_tokenizer, summarize
|
9 |
+
from wikipedia_api import getWikipedia
|
10 |
+
|
11 |
+
@st.cache_resource
|
12 |
+
def load_recognition_model():
|
13 |
+
"""
|
14 |
+
Loads the translation model pipeline.
|
15 |
+
"""
|
16 |
+
filename = "pickle_model.pkl"
|
17 |
+
with open(filename, 'rb') as file:
|
18 |
+
model = pickle.load(file)
|
19 |
+
return model
|
20 |
+
|
21 |
+
@st.cache_resource
|
22 |
+
def load_summarizer():
|
23 |
+
"""
|
24 |
+
Loads the summarization model.
|
25 |
+
"""
|
26 |
+
|
27 |
+
summarizer, tokenizer = init_model_and_tokenizer()
|
28 |
+
return summarizer, tokenizer
|
29 |
+
|
30 |
+
def predict_images(images, model):
|
31 |
+
"""
|
32 |
+
Predicts each landmark name in `images` list.
|
33 |
+
"""
|
34 |
+
images = preprocess_images(images)
|
35 |
+
|
36 |
+
with torch.no_grad():
|
37 |
+
output = model(images)
|
38 |
+
|
39 |
+
names = output_to_names(output)
|
40 |
+
|
41 |
+
return names
|
42 |
+
|
43 |
+
def load_images():
|
44 |
+
"""
|
45 |
+
Loads user's images.
|
46 |
+
"""
|
47 |
+
uploaded_files = st.file_uploader(
|
48 |
+
label="Загрузите ваши фотографии.",
|
49 |
+
type=['png', 'jpg'],
|
50 |
+
accept_multiple_files=True
|
51 |
+
)
|
52 |
+
if uploaded_files is not None:
|
53 |
+
images = []
|
54 |
+
for file in uploaded_files:
|
55 |
+
image_data = file.getvalue()
|
56 |
+
st.image(image_data)
|
57 |
+
images.append(image_data)
|
58 |
+
|
59 |
+
return [Image.open(io.BytesIO(image_data)) for image_data in images]
|
60 |
+
else:
|
61 |
+
return None
|
62 |
+
|
63 |
+
|
64 |
+
# Load models
|
65 |
+
landmark_model = load_recognition_model()
|
66 |
+
summarizer, tokenizer = load_summarizer()
|
67 |
+
|
68 |
+
st.title("Распознавание достопримечательностей")
|
69 |
+
|
70 |
+
# Images input.
|
71 |
+
images = load_images()
|
72 |
+
|
73 |
+
result = st.button('Распознать')
|
74 |
+
|
75 |
+
if result:
|
76 |
+
# Get predictions
|
77 |
+
names = predict_images(images, landmark_model)
|
78 |
+
|
79 |
+
st.write(names)
|
80 |
+
# Request descriptions and coordinates from Wikipedia.
|
81 |
+
wiki_data = getWikipedia(names)
|
82 |
+
|
83 |
+
# Show summarized descriptions for each landmark.
|
84 |
+
for landmark in wiki_data:
|
85 |
+
description = landmark['summary']
|
86 |
+
summarized = summarize(description, summarizer, tokenizer)
|
87 |
+
landmark['summarized'] = summarized
|
88 |
+
|
89 |
+
st.write(wiki_data)
|
90 |
+
|
91 |
+
# Draw a map.
|
model_execute.py
CHANGED
@@ -2,7 +2,36 @@ import pickle
|
|
2 |
import torch
|
3 |
import torchvision.transforms as transforms
|
4 |
from PIL import Image
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def check_photo(name, photo):
|
8 |
preprocess = transforms.Compose([
|
@@ -27,39 +56,23 @@ def check_photo(name, photo):
|
|
27 |
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
28 |
print(name, probabilities)
|
29 |
|
30 |
-
pkl_filename = "pickle_model.pkl"
|
31 |
-
with open(pkl_filename, 'rb') as file:
|
32 |
-
model = pickle.load(file)
|
33 |
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
gates_photo = Image.open("gates500.jpg")
|
38 |
-
musk_photo = Image.open("mask.jpg")
|
39 |
-
bezos_photo = Image.open("bezos500.jpg")
|
40 |
-
zuker_photo = Image.open("zuckerberg500.jpg")
|
41 |
-
jobs_photo = Image.open("jobs500.jpg")
|
42 |
-
test_photos_dict = {'gates':gates_photo, 'musk':musk_photo, 'bezos':bezos_photo,'zuker': zuker_photo,'jobs': jobs_photo}
|
43 |
-
for name in test_photos_dict:
|
44 |
-
|
45 |
-
# preprocess = transforms.Compose([
|
46 |
-
# transforms.Resize(256),
|
47 |
-
# transforms.CenterCrop(224),
|
48 |
-
# transforms.ToTensor(),
|
49 |
-
# transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
50 |
-
# ])
|
51 |
-
# input_tensor = preprocess(test_photos_list)
|
52 |
-
# input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
53 |
-
#
|
54 |
-
# # move the input and model to GPU for speed if available
|
55 |
-
# if torch.cuda.is_available():
|
56 |
-
# input_batch = input_batch.to('cuda')
|
57 |
-
# model.to('cuda')
|
58 |
-
#
|
59 |
-
# with torch.no_grad():
|
60 |
-
# output = model(input_batch)
|
61 |
-
# # Tensor of shape 1000, with confidence scores over ImageNet's 1000 classes
|
62 |
-
# print(output[0])
|
63 |
-
# print(model)
|
64 |
|
65 |
-
|
|
|
|
2 |
import torch
|
3 |
import torchvision.transforms as transforms
|
4 |
from PIL import Image
|
5 |
+
import csv
|
6 |
+
|
7 |
+
def preprocess_images(images):
|
8 |
+
"""
|
9 |
+
Preprocess image for the model.
|
10 |
+
"""
|
11 |
+
preprocess = transforms.Compose([
|
12 |
+
transforms.Resize([70, 70]),
|
13 |
+
transforms.ToTensor(),
|
14 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
15 |
+
])
|
16 |
+
images_tensor = [preprocess(image) for image in images]
|
17 |
+
image_batch = torch.stack(images_tensor)
|
18 |
+
|
19 |
+
return image_batch
|
20 |
+
|
21 |
+
def output_to_names(output):
|
22 |
+
"""
|
23 |
+
Converts model outputs to category names names.
|
24 |
+
"""
|
25 |
+
with open('cat.csv') as file:
|
26 |
+
reader = csv.reader(file)
|
27 |
+
cat_list = list(reader)[0]
|
28 |
+
|
29 |
+
names = []
|
30 |
+
for prediction in output:
|
31 |
+
probabilities = torch.nn.functional.softmax(prediction, dim=0)
|
32 |
+
index = probabilities.argmax()
|
33 |
+
names.append(cat_list[index])
|
34 |
+
return names
|
35 |
|
36 |
def check_photo(name, photo):
|
37 |
preprocess = transforms.Compose([
|
|
|
56 |
probabilities = torch.nn.functional.softmax(output[0], dim=0)
|
57 |
print(name, probabilities)
|
58 |
|
|
|
|
|
|
|
59 |
|
60 |
+
if __name__ == "__main__":
|
61 |
+
pkl_filename = "pickle_model.pkl"
|
62 |
+
with open(pkl_filename, 'rb') as file:
|
63 |
+
model = pickle.load(file)
|
64 |
+
|
65 |
+
model.eval()
|
66 |
+
# sample execution (requires torchvision)
|
67 |
|
68 |
+
gates_photo = Image.open("gates500.jpg")
|
69 |
+
musk_photo = Image.open("mask.jpg")
|
70 |
+
bezos_photo = Image.open("bezos500.jpg")
|
71 |
+
zuker_photo = Image.open("zuckerberg500.jpg")
|
72 |
+
jobs_photo = Image.open("jobs500.jpg")
|
73 |
+
test_photos_dict = {'gates':gates_photo, 'musk':musk_photo, 'bezos':bezos_photo,'zuker': zuker_photo,'jobs': jobs_photo}
|
74 |
+
for name in test_photos_dict:
|
75 |
+
check_photo(name, test_photos_dict[name])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
tensor = torch.tensor([[-1.8637, -1.6411, -1.5038, -2.9645, -1.8477, 6.5004], [-1.6067, -1.6597, -1.0925, 5.1295, -1.6491, -1.4739], [-0.2427, -0.6140, -1.1936, -2.1147, 4.8429, -2.0129]])
|
78 |
+
print(output_to_names(tensor))
|
requirements.txt
CHANGED
@@ -5,4 +5,6 @@ bs4
|
|
5 |
lxml
|
6 |
requests
|
7 |
requests
|
8 |
-
transformers
|
|
|
|
|
|
5 |
lxml
|
6 |
requests
|
7 |
requests
|
8 |
+
transformers
|
9 |
+
streamlit
|
10 |
+
wikipedia
|