-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelper_functions.py
More file actions
148 lines (132 loc) · 4.37 KB
/
helper_functions.py
File metadata and controls
148 lines (132 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from io import BytesIO
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import requests
import torch
import os
import glob
import pandas as pd
import xml.etree.ElementTree as ET
def save_images(folder, search_term, count=10):
if not os.path.exists(folder):
os.mkdir(folder)
# SEARCH_URL = "https://huggingface.co/api/experimental/images/search"
# params = {"q": search_term, "license": "public", "imageType": "photo", "count": count}
SEARCH_URL = "https://pixabay.com/api/"
params = {"q": search_term.replace(' ', '+'),
"image_type": "photo",
"per_page": count,
"page": 1,
"key": os.environ["PIXABAY_KEY"]}
resp = requests.get(SEARCH_URL, params=params)
if resp.status_code == 200:
# content = resp.json()['value']
# urls = [img['thumbnailUrl'] for img in content]
content = resp.json()['hits']
urls = [img['previewURL'] for img in content]
folder = os.path.join(folder, search_term)
if not os.path.exists(folder):
os.mkdir(folder)
i = 0
for url in urls:
try:
img = get_image_from_url(url)
fname = os.path.join(folder, f'{i}.jpg')
img.save(fname)
i += 1
except Exception:
pass
print(f'Retrieved {i} images for {search_term}')
else:
print(f'Failed to retrieve URLs for {search_term}')
def show(imgs, titles=None):
import torchvision.transforms.functional as tF
if not isinstance(imgs, list):
imgs = [imgs]
fig, axs = plt.subplots(ncols=len(imgs), squeeze=False)
for i, img in enumerate(imgs):
if isinstance(img, torch.Tensor):
img = img.detach()
img = tF.to_pil_image(img)
axs[0, i].imshow(np.asarray(img))
axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])
if titles is not None:
if i < len(titles):
axs[0, i].set_title(titles[i])
return fig
def get_image_from_url(url, headers=None):
resp = requests.get(url, headers=headers)
resp.raise_for_status()
img = Image.open(BytesIO(resp.content))
return img
def xml_to_csv(path):
"""Iterates through all .xml files (generated by labelImg) in a given directory and combines
them in a single Pandas dataframe.
Parameters:
----------
path : str
The path containing the .xml files
Returns
-------
Pandas DataFrame
The produced dataframe
"""
xml_list = []
for xml_file in glob.glob(path + '/*.xml'):
tree = ET.parse(xml_file)
root = tree.getroot()
filename = root.find('filename').text
width = int(root.find('size').find('width').text)
height = int(root.find('size').find('height').text)
for member in root.findall('object'):
bndbox = member.find('bndbox')
value = (filename,
width,
height,
member.find('name').text,
int(bndbox.find('xmin').text),
int(bndbox.find('ymin').text),
int(bndbox.find('xmax').text),
int(bndbox.find('ymax').text),
)
xml_list.append(value)
column_name = ['filename', 'width', 'height',
'class', 'xmin', 'ymin', 'xmax', 'ymax']
xml_df = pd.DataFrame(xml_list, columns=column_name)
return xml_df
def get_summarizer(model, tokenizer):
def summarizer(
texts,
max_length=128,
min_length=30,
num_beams=4,
do_sample=False,
truncation=True,
):
single_input = isinstance(texts, str)
if single_input:
texts = [texts]
batch = tokenizer(
texts,
return_tensors="pt",
padding=True,
truncation=truncation,
).to(model.device)
model.eval()
with torch.inference_mode():
output_ids = model.generate(
**batch,
max_new_tokens=max_length,
min_new_tokens=min_length,
num_beams=num_beams,
do_sample=do_sample,
)
summaries = tokenizer.batch_decode(
output_ids,
skip_special_tokens=True,
clean_up_tokenization_spaces=True,
)
result = [{"summary_text": s} for s in summaries]
return result[0] if single_input else result
return summarizer