rararara9999 commited on
Commit
d6b43dd
·
verified ·
1 Parent(s): 1eaad61

Create Model

Browse files
Files changed (1) hide show
  1. Model +412 -0
Model ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ In [4]:
2
+ ! pip install -U git+https://github.com/huggingface/transformers.git
3
+ ! pip install -U git+https://github.com/huggingface/accelerate.git
4
+ Collecting git+https://github.com/huggingface/transformers.git
5
+ Cloning https://github.com/huggingface/transformers.git to /tmp/pip-req-build-srwrto6l
6
+ Running command git clone --filter=blob:none --quiet https://github.com/huggingface/transformers.git /tmp/pip-req-build-srwrto6l
7
+ Traceback (most recent call last):
8
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/base_command.py", line 179, in exc_logging_wrapper
9
+ status = run_func(*args)
10
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/req_command.py", line 67, in wrapper
11
+ return func(self, options, args)
12
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/commands/install.py", line 377, in run
13
+ requirement_set = resolver.resolve(
14
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/resolver.py", line 76, in resolve
15
+ collected = self.factory.collect_root_requirements(root_reqs)
16
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/factory.py", line 538, in collect_root_requirements
17
+ reqs = list(
18
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/factory.py", line 494, in _make_requirements_from_install_req
19
+ cand = self._make_base_candidate_from_link(
20
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/factory.py", line 231, in _make_base_candidate_from_link
21
+ self._link_candidate_cache[link] = LinkCandidate(
22
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 303, in __init__
23
+ super().__init__(
24
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 158, in __init__
25
+ self.dist = self._prepare()
26
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 235, in _prepare
27
+ dist = self._prepare_distribution()
28
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/resolution/resolvelib/candidates.py", line 314, in _prepare_distribution
29
+ return preparer.prepare_linked_requirement(self._ireq, parallel_builds=True)
30
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/operations/prepare.py", line 527, in prepare_linked_requirement
31
+ return self._prepare_linked_requirement(req, parallel_builds)
32
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/operations/prepare.py", line 598, in _prepare_linked_requirement
33
+ local_file = unpack_url(
34
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/operations/prepare.py", line 159, in unpack_url
35
+ unpack_vcs_link(link, location, verbosity=verbosity)
36
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/operations/prepare.py", line 81, in unpack_vcs_link
37
+ vcs_backend.unpack(location, url=hide_url(link.url), verbosity=verbosity)
38
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/vcs/versioncontrol.py", line 589, in unpack
39
+ self.obtain(location, url=url, verbosity=verbosity)
40
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/vcs/versioncontrol.py", line 502, in obtain
41
+ self.fetch_new(dest, url, rev_options, verbosity=verbosity)
42
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/vcs/git.py", line 277, in fetch_new
43
+ self.run_command(
44
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/vcs/versioncontrol.py", line 631, in run_command
45
+ return call_subprocess(
46
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/utils/subprocess.py", line 151, in call_subprocess
47
+ line: str = proc.stdout.readline()
48
+ KeyboardInterrupt
49
+
50
+ During handling of the above exception, another exception occurred:
51
+
52
+ Traceback (most recent call last):
53
+ File "/usr/lib/python3.10/logging/__init__.py", line 1732, in isEnabledFor
54
+ return self._cache[level]
55
+ KeyError: 50
56
+
57
+ During handling of the above exception, another exception occurred:
58
+
59
+ Traceback (most recent call last):
60
+ File "/usr/local/bin/pip3", line 8, in <module>
61
+ sys.exit(main())
62
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/main.py", line 80, in main
63
+ return command.main(cmd_args)
64
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/base_command.py", line 100, in main
65
+ return self._main(args)
66
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/base_command.py", line 232, in _main
67
+ return run(options, args)
68
+ File "/usr/local/lib/python3.10/dist-packages/pip/_internal/cli/base_command.py", line 215, in exc_logging_wrapper
69
+ logger.critical("Operation cancelled by user")
70
+ File "/usr/lib/python3.10/logging/__init__.py", line 1523, in critical
71
+ if self.isEnabledFor(CRITICAL):
72
+ File "/usr/lib/python3.10/logging/__init__.py", line 1740, in isEnabledFor
73
+ level >= self.getEffectiveLevel()
74
+ File "/usr/lib/python3.10/logging/__init__.py", line 1710, in getEffectiveLevel
75
+ def getEffectiveLevel(self):
76
+ KeyboardInterrupt
77
+ ^C
78
+ Collecting git+https://github.com/huggingface/accelerate.git
79
+ Cloning https://github.com/huggingface/accelerate.git to /tmp/pip-req-build-07w0b0ye
80
+ Running command git clone --filter=blob:none --quiet https://github.com/huggingface/accelerate.git /tmp/pip-req-build-07w0b0ye
81
+ Resolved https://github.com/huggingface/accelerate.git to commit 0e61127b5a6f99df51dd66803cd163f07bb85104
82
+ Installing build dependencies ... done
83
+ Getting requirements to build wheel ... done
84
+ Preparing metadata (pyproject.toml) ... done
85
+ Requirement already satisfied: numpy<3.0.0,>=1.17 in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (1.26.4)
86
+ Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (24.1)
87
+ Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (5.9.5)
88
+ Requirement already satisfied: pyyaml in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (6.0.2)
89
+ Requirement already satisfied: torch>=1.10.0 in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (2.4.1+cu121)
90
+ Requirement already satisfied: huggingface-hub>=0.21.0 in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (0.24.7)
91
+ Requirement already satisfied: safetensors>=0.4.3 in /usr/local/lib/python3.10/dist-packages (from accelerate==1.1.0.dev0) (0.4.5)
92
+ Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (3.16.1)
93
+ Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (2024.6.1)
94
+ Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (2.32.3)
95
+ Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (4.66.5)
96
+ Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (4.12.2)
97
+ Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate==1.1.0.dev0) (1.13.3)
98
+ Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate==1.1.0.dev0) (3.3)
99
+ Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.10.0->accelerate==1.1.0.dev0) (3.1.4)
100
+ Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.10.0->accelerate==1.1.0.dev0) (2.1.5)
101
+ Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (3.3.2)
102
+ Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (3.10)
103
+ Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (2.2.3)
104
+ Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.1.0.dev0) (2024.8.30)
105
+ Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch>=1.10.0->accelerate==1.1.0.dev0) (1.3.0)
106
+ Building wheels for collected packages: accelerate
107
+ Building wheel for accelerate (pyproject.toml) ... done
108
+ Created wheel for accelerate: filename=accelerate-1.1.0.dev0-py3-none-any.whl size=332417 sha256=5fb12ca3a9ceac357ee327959835b13f847381455bd8cfd6e8d80d23b3fc25be
109
+ Stored in directory: /tmp/pip-ephem-wheel-cache-le3a9ije/wheels/9c/a3/1e/47368f9b6575655fe9ee1b6350cfa7d4b0befe66a35f8a8365
110
+ Successfully built accelerate
111
+ Installing collected packages: accelerate
112
+ Attempting uninstall: accelerate
113
+ Found existing installation: accelerate 0.34.2
114
+ Uninstalling accelerate-0.34.2:
115
+ Successfully uninstalled accelerate-0.34.2
116
+ Successfully installed accelerate-1.1.0.dev0
117
+ In [5]:
118
+ ! pip install datasets
119
+ Collecting datasets
120
+ Downloading datasets-3.0.1-py3-none-any.whl.metadata (20 kB)
121
+ Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from datasets) (3.16.1)
122
+ Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from datasets) (1.26.4)
123
+ Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (16.1.0)
124
+ Collecting dill<0.3.9,>=0.3.0 (from datasets)
125
+ Downloading dill-0.3.8-py3-none-any.whl.metadata (10 kB)
126
+ Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets) (2.2.2)
127
+ Requirement already satisfied: requests>=2.32.2 in /usr/local/lib/python3.10/dist-packages (from datasets) (2.32.3)
128
+ Requirement already satisfied: tqdm>=4.66.3 in /usr/local/lib/python3.10/dist-packages (from datasets) (4.66.5)
129
+ Collecting xxhash (from datasets)
130
+ Downloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (12 kB)
131
+ Collecting multiprocess (from datasets)
132
+ Downloading multiprocess-0.70.17-py310-none-any.whl.metadata (7.2 kB)
133
+ Requirement already satisfied: fsspec<=2024.6.1,>=2023.1.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]<=2024.6.1,>=2023.1.0->datasets) (2024.6.1)
134
+ Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets) (3.10.9)
135
+ Requirement already satisfied: huggingface-hub>=0.22.0 in /usr/local/lib/python3.10/dist-packages (from datasets) (0.24.7)
136
+ Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from datasets) (24.1)
137
+ Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from datasets) (6.0.2)
138
+ Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (2.4.3)
139
+ Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.3.1)
140
+ Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (24.2.0)
141
+ Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.4.1)
142
+ Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (6.1.0)
143
+ Requirement already satisfied: yarl<2.0,>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (1.13.1)
144
+ Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets) (4.0.3)
145
+ Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.22.0->datasets) (4.12.2)
146
+ Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.3.2)
147
+ Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (3.10)
148
+ Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2.2.3)
149
+ Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.32.2->datasets) (2024.8.30)
150
+ INFO: pip is looking at multiple versions of multiprocess to determine which version is compatible with other requirements. This could take a while.
151
+ Downloading multiprocess-0.70.16-py310-none-any.whl.metadata (7.2 kB)
152
+ Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2.8.2)
153
+ Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)
154
+ Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets) (2024.2)
155
+ Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->datasets) (1.16.0)
156
+ Downloading datasets-3.0.1-py3-none-any.whl (471 kB)
157
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 471.6/471.6 kB 24.2 MB/s eta 0:00:00
158
+ Downloading dill-0.3.8-py3-none-any.whl (116 kB)
159
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 116.3/116.3 kB 9.8 MB/s eta 0:00:00
160
+ Downloading multiprocess-0.70.16-py310-none-any.whl (134 kB)
161
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 134.8/134.8 kB 11.3 MB/s eta 0:00:00
162
+ Downloading xxhash-3.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (194 kB)
163
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 194.1/194.1 kB 15.2 MB/s eta 0:00:00
164
+ Installing collected packages: xxhash, dill, multiprocess, datasets
165
+ Successfully installed datasets-3.0.1 dill-0.3.8 multiprocess-0.70.16 xxhash-3.5.0
166
+ In [6]:
167
+ model_checkpoint = "microsoft/resnet-50"
168
+ batch_size = 128
169
+ In [7]:
170
+ from datasets import load_dataset
171
+ In [8]:
172
+ !pip install evaluate
173
+ Collecting evaluate
174
+ Downloading evaluate-0.4.3-py3-none-any.whl.metadata (9.2 kB)
175
+ Requirement already satisfied: datasets>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from evaluate) (3.0.1)
176
+ Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from evaluate) (1.26.4)
177
+ Requirement already satisfied: dill in /usr/local/lib/python3.10/dist-packages (from evaluate) (0.3.8)
178
+ Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from evaluate) (2.2.2)
179
+ Requirement already satisfied: requests>=2.19.0 in /usr/local/lib/python3.10/dist-packages (from evaluate) (2.32.3)
180
+ Requirement already satisfied: tqdm>=4.62.1 in /usr/local/lib/python3.10/dist-packages (from evaluate) (4.66.5)
181
+ Requirement already satisfied: xxhash in /usr/local/lib/python3.10/dist-packages (from evaluate) (3.5.0)
182
+ Requirement already satisfied: multiprocess in /usr/local/lib/python3.10/dist-packages (from evaluate) (0.70.16)
183
+ Requirement already satisfied: fsspec>=2021.05.0 in /usr/local/lib/python3.10/dist-packages (from fsspec[http]>=2021.05.0->evaluate) (2024.6.1)
184
+ Requirement already satisfied: huggingface-hub>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from evaluate) (0.24.7)
185
+ Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from evaluate) (24.1)
186
+ Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from datasets>=2.0.0->evaluate) (3.16.1)
187
+ Requirement already satisfied: pyarrow>=15.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.0.0->evaluate) (16.1.0)
188
+ Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets>=2.0.0->evaluate) (3.10.9)
189
+ Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from datasets>=2.0.0->evaluate) (6.0.2)
190
+ Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.7.0->evaluate) (4.12.2)
191
+ Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->evaluate) (3.3.2)
192
+ Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->evaluate) (3.10)
193
+ Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->evaluate) (2.2.3)
194
+ Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.19.0->evaluate) (2024.8.30)
195
+ Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from pandas->evaluate) (2.8.2)
196
+ Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->evaluate) (2024.2)
197
+ Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.10/dist-packages (from pandas->evaluate) (2024.2)
198
+ Requirement already satisfied: aiohappyeyeballs>=2.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (2.4.3)
199
+ Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.3.1)
200
+ Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (24.2.0)
201
+ Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.4.1)
202
+ Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (6.1.0)
203
+ Requirement already satisfied: yarl<2.0,>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (1.13.1)
204
+ Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets>=2.0.0->evaluate) (4.0.3)
205
+ Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.8.2->pandas->evaluate) (1.16.0)
206
+ Downloading evaluate-0.4.3-py3-none-any.whl (84 kB)
207
+ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 84.0/84.0 kB 7.1 MB/s eta 0:00:00
208
+ Installing collected packages: evaluate
209
+ Successfully installed evaluate-0.4.3
210
+ In [9]:
211
+ from google.colab import drive
212
+ drive.mount('/content/drive/')
213
+ Drive already mounted at /content/drive/; to attempt to forcibly remount, call drive.mount("/content/drive/", force_remount=True).
214
+ In [10]:
215
+ from evaluate import load
216
+ metric = load("accuracy")
217
+ /usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_token.py:89: UserWarning:
218
+ The secret `HF_TOKEN` does not exist in your Colab secrets.
219
+ To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
220
+ You will be able to reuse this secret in all of your notebooks.
221
+ Please note that authentication is recommended but still optional to access public models or datasets.
222
+ warnings.warn(
223
+ Downloading builder script: 0%| | 0.00/4.20k [00:00<?, ?B/s]
224
+ In [11]:
225
+ dataset = load_dataset("imagefolder", data_dir="drive/MyDrive/Face Mask Dataset")
226
+ labels = dataset["train"].features["label"].names
227
+ label2id, id2label = dict(), dict()
228
+ for i, label in enumerate(labels):
229
+ label2id[label] = i
230
+ id2label[i] = label
231
+ Resolving data files: 0%| | 0/10000 [00:00<?, ?it/s]
232
+ Downloading data: 0%| | 0/10000 [00:00<?, ?files/s]
233
+ Generating train split: 0 examples [00:00, ? examples/s]
234
+ 新段落¶
235
+ In [12]:
236
+ from transformers import AutoImageProcessor
237
+ image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
238
+ image_processor
239
+ preprocessor_config.json: 0%| | 0.00/266 [00:00<?, ?B/s]
240
+ Out[12]:
241
+ ConvNextImageProcessor {
242
+ "crop_pct": 0.875,
243
+ "do_normalize": true,
244
+ "do_rescale": true,
245
+ "do_resize": true,
246
+ "image_mean": [
247
+ 0.485,
248
+ 0.456,
249
+ 0.406
250
+ ],
251
+ "image_processor_type": "ConvNextImageProcessor",
252
+ "image_std": [
253
+ 0.229,
254
+ 0.224,
255
+ 0.225
256
+ ],
257
+ "resample": 3,
258
+ "rescale_factor": 0.00392156862745098,
259
+ "size": {
260
+ "shortest_edge": 224
261
+ }
262
+ }
263
+ In [13]:
264
+ from torchvision.transforms import (
265
+ CenterCrop,
266
+ Compose,
267
+ Normalize,
268
+ RandomHorizontalFlip,
269
+ RandomResizedCrop,
270
+ Resize,
271
+ ToTensor,
272
+ ColorJitter,
273
+ RandomRotation
274
+ )
275
+
276
+ normalize = Normalize(mean=image_processor.image_mean, std=image_processor.image_std)
277
+ if "height" in image_processor.size:
278
+ size = (image_processor.size["height"], image_processor.size["width"])
279
+ crop_size = size
280
+ max_size = None
281
+ elif "shortest_edge" in image_processor.size:
282
+ size = image_processor.size["shortest_edge"]
283
+ crop_size = (size, size)
284
+ max_size = image_processor.size.get("longest_edge")
285
+
286
+ train_transforms = Compose(
287
+ [
288
+ RandomResizedCrop(crop_size),
289
+ RandomHorizontalFlip(),
290
+ RandomRotation(degrees=15),
291
+ ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1),
292
+ ToTensor(),
293
+ normalize,
294
+ ]
295
+ )
296
+
297
+ val_transforms = Compose(
298
+ [
299
+ Resize(size),
300
+ CenterCrop(crop_size),
301
+ RandomRotation(degrees=15),
302
+ ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1),
303
+ ToTensor(),
304
+ normalize,
305
+ ]
306
+ )
307
+
308
+ def preprocess_train(example_batch):
309
+ example_batch["pixel_values"] = [
310
+ train_transforms(image.convert("RGB")) for image in example_batch["image"]
311
+ ]
312
+ return example_batch
313
+
314
+ def preprocess_val(example_batch):
315
+ example_batch["pixel_values"] = [val_transforms(image.convert("RGB")) for image in example_batch["image"]]
316
+ return example_batch
317
+ In [15]:
318
+ splits = dataset["train"].train_test_split(test_size=0.3)
319
+ train_ds = splits['train']
320
+ val_ds = splits['test']
321
+
322
+ train_ds.set_transform(preprocess_train)
323
+ val_ds.set_transform(preprocess_val)
324
+ In [16]:
325
+ from transformers import AutoModelForImageClassification, TrainingArguments, Trainer
326
+
327
+ model = AutoModelForImageClassification.from_pretrained(model_checkpoint,
328
+ label2id=label2id,
329
+ id2label=id2label,
330
+ ignore_mismatched_sizes = True)
331
+ config.json: 0%| | 0.00/69.6k [00:00<?, ?B/s]
332
+ model.safetensors: 0%| | 0.00/102M [00:00<?, ?B/s]
333
+ Some weights of ResNetForImageClassification were not initialized from the model checkpoint at microsoft/resnet-50 and are newly initialized because the shapes did not match:
334
+ - classifier.1.bias: found shape torch.Size([1000]) in the checkpoint and torch.Size([2]) in the model instantiated
335
+ - classifier.1.weight: found shape torch.Size([1000, 2048]) in the checkpoint and torch.Size([2, 2048]) in the model instantiated
336
+ You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
337
+ In [23]:
338
+ model_name = model_checkpoint.split("/")[-1]
339
+
340
+ args = TrainingArguments(
341
+ f"{model_name}-finetuned",
342
+ remove_unused_columns=False,
343
+ evaluation_strategy = "epoch",
344
+ save_strategy = "epoch",
345
+ save_total_limit = 5,
346
+ learning_rate=1e-3,
347
+ per_device_train_batch_size=batch_size,
348
+ gradient_accumulation_steps=2,
349
+ per_device_eval_batch_size=batch_size,
350
+ num_train_epochs=2,
351
+ warmup_ratio=0.1,
352
+ weight_decay=0.01,
353
+ lr_scheduler_type="cosine",
354
+ logging_steps=10,
355
+ load_best_model_at_end=True,
356
+ metric_for_best_model="accuracy",)
357
+ /usr/local/lib/python3.10/dist-packages/transformers/training_args.py:1525: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead
358
+ warnings.warn(
359
+ In [19]:
360
+ import numpy as np
361
+
362
+ def compute_metrics(eval_pred):
363
+ """Computes accuracy on a batch of predictions"""
364
+ predictions = np.argmax(eval_pred.predictions, axis=1)
365
+ return metric.compute(predictions=predictions, references=eval_pred.label_ids)
366
+ In [20]:
367
+ import torch
368
+
369
+ def collate_fn(examples):
370
+ pixel_values = torch.stack([example["pixel_values"] for example in examples])
371
+ labels = torch.tensor([example["label"] for example in examples])
372
+ return {"pixel_values": pixel_values, "labels": labels}
373
+ In [24]:
374
+ trainer = Trainer(model,
375
+ args,
376
+ train_dataset=train_ds,
377
+ eval_dataset=val_ds,
378
+ tokenizer=image_processor,
379
+ compute_metrics=compute_metrics,
380
+ data_collator=collate_fn,)
381
+ In [25]:
382
+ train_results = trainer.train()
383
+ # 保存模型
384
+ trainer.save_model()
385
+ trainer.log_metrics("train", train_results.metrics)
386
+ trainer.save_metrics("train", train_results.metrics)
387
+ trainer.save_state()
388
+ [54/54 2:06:56, Epoch 1/2]
389
+ Epoch Training Loss Validation Loss Accuracy
390
+ 0 0.149200 0.036754 0.986667
391
+ 1 0.038400 0.020120 0.993333
392
+
393
+ ***** train metrics *****
394
+ epoch = 1.9636
395
+ total_flos = 272606314GF
396
+ train_loss = 0.1626
397
+ train_runtime = 2:10:43.64
398
+ train_samples_per_second = 1.785
399
+ train_steps_per_second = 0.007
400
+ In [26]:
401
+ metrics = trainer.evaluate()
402
+ # some nice to haves:
403
+ trainer.log_metrics("eval", metrics)
404
+ trainer.save_metrics("eval", metrics)
405
+ [24/24 00:40]
406
+ ***** eval metrics *****
407
+ epoch = 1.9636
408
+ eval_accuracy = 0.9947
409
+ eval_loss = 0.0184
410
+ eval_runtime = 0:00:44.17
411
+ eval_samples_per_second = 67.914
412
+ eval_steps_per_second = 0.543