-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils_img.py
More file actions
68 lines (54 loc) · 2.02 KB
/
utils_img.py
File metadata and controls
68 lines (54 loc) · 2.02 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
import os
import numpy as np
import torch
from torchvision import transforms
from torchvision.transforms import functional
from augly.image import functional as aug_functional
import warnings
import contextlib
import requests
from urllib3.exceptions import InsecureRequestWarning
import ssl
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def random_crop(x, scale):
crop_size = int(np.sqrt(scale) * x.size[0])
crop_transform = transforms.RandomCrop(crop_size)
x = crop_transform(x)
return x
def center_crop(x, scale):
scale = np.sqrt(scale)
new_edges_size = [int(s*scale) for s in x.size][::-1]
return functional.center_crop(x, new_edges_size)
def resize(x, scale):
scale = np.sqrt(scale)
new_edges_size = [int(s*scale) for s in x.size][::-1]
return functional.resize(x, new_edges_size)
def comb(x):
scale = np.sqrt(0.4)
new_edges_size = [int(s*scale) for s in x.size][::-1]
x = functional.center_crop(x, new_edges_size)
x = functional.adjust_brightness(x,1.5)
x = aug_functional.encoding_quality(x,quality=80)
return x
ssl._create_default_https_context = ssl._create_unverified_context
old_merge_environment_settings = requests.Session.merge_environment_settings
@contextlib.contextmanager
def no_ssl_verification():
opened_adapters = set()
def merge_environment_settings(self, url, proxies, stream, verify, cert):
opened_adapters.add(self.get_adapter(url))
settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
settings['verify'] = False
return settings
requests.Session.merge_environment_settings = merge_environment_settings
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', InsecureRequestWarning)
yield
finally:
requests.Session.merge_environment_settings = old_merge_environment_settings
for adapter in opened_adapters:
try:
adapter.close()
except:
pass