-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add milabench.datasets.fake_images, improve instruments
- Loading branch information
Showing
6 changed files
with
212 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[flake8] | ||
ignore=E501,E203,W503,F722 | ||
exclude=tests/feat38.py | ||
per-file-ignores= | ||
__init__.py:F401 | ||
examples/*:F821 | ||
tests/*:F821 | ||
tests/test_tools.py:F401 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import multiprocessing | ||
import os | ||
|
||
from torchvision.datasets import FakeData | ||
from tqdm import tqdm | ||
|
||
from ..fs import XPath | ||
|
||
|
||
def write(args): | ||
image_size, count, offset, outdir = args | ||
dataset = FakeData( | ||
size=count, image_size=image_size, num_classes=1000, random_offset=offset | ||
) | ||
|
||
for i, (image, y) in tqdm(enumerate(dataset), total=count): | ||
class_val = int(y) | ||
image_name = f"{offset + i}.jpeg" | ||
|
||
path = os.path.join(outdir, str(class_val)) | ||
os.makedirs(path, exist_ok=True) | ||
|
||
image_path = os.path.join(path, image_name) | ||
image.save(image_path) | ||
|
||
|
||
def generate(image_size, n, outdir): | ||
p_count = min(multiprocessing.cpu_count(), 8) | ||
count = n // p_count | ||
offset_list = [(image_size, count, i, outdir) for i in range(0, n, count)] | ||
pool = multiprocessing.Pool(p_count) | ||
pool.map(write, offset_list) | ||
|
||
|
||
def generate_sets(root, sets, shape): | ||
root = XPath(root) | ||
sentinel = root / "done" | ||
if sentinel.exists(): | ||
print(f"{root} was already generated") | ||
return | ||
if root.exists(): | ||
print(f"{root} exists but is not marked complete; deleting") | ||
root.rm() | ||
for name, n in sets.items(): | ||
print(f"Generating {name}") | ||
generate(shape, n, os.path.join(root, name)) | ||
sentinel.touch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters