-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
100 lines (79 loc) · 2.91 KB
/
utils.py
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
""" Federated Text-driven Prompt Generation for Vision-Language Models (ICLR 2024).
Copyright (c) 2024 Robert Bosch GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import sys
import time
CUSTOM_TEMPLATES = {
'oxford_pets': 'a photo of a {}, a type of pet.',
'oxford_flowers': 'a photo of a {}, a type of flower.',
'fgvc_aircraft': 'a photo of a {}, a type of aircraft.',
'dtd': '{} texture.',
'eurosat': 'a centered satellite photo of {}.',
'stanford_cars': 'a photo of a {}.',
'food101': 'a photo of {}, a type of food.',
'sum397': 'a photo of a {}.',
'caltech101': 'a photo of a {}.',
'ucf101': 'a photo of a person doing {}.',
'ImageNet': 'a photo of a {}.',
'ImageNetSketch': 'a photo of a {}.',
'ImageNetV2': 'a photo of a {}.',
'ImageNetA': 'a photo of a {}.',
'ImageNetR': 'a photo of a {}.'
}
def mkdir_if_missing(dirname):
"""Create dirname if it is missing."""
if not os.path.exists(dirname):
os.makedirs(dirname)
class Logger:
"""Write console output to external text file.
Imported from `<https://github.com/Cysu/open-reid/blob/master/reid/utils/logging.py>`_
Args:
fpath (str): directory to save logging file.
"""
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
mkdir_if_missing(os.path.dirname(fpath))
self.file = open(fpath, "w")
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
self.console.close()
if self.file is not None:
self.file.close()
def setup_logger(output=None):
if output is None:
return
if output.endswith(".txt") or output.endswith(".log"):
fpath = output
else:
fpath = os.path.join(output, "log.txt")
if os.path.exists(fpath):
# make sure the existing log file is not over-written
fpath += time.strftime("-%Y-%m-%d-%H-%M-%S")
sys.stdout = Logger(fpath)