Spaces:
Runtime error
Runtime error
Merge pull request #1 from edadaltocg/master
Browse files- .gitignore +2 -1
- README.md +1 -7
- app.py +48 -27
- centroids_resnet50.tv2_in1k_igeood_logits.pkl +3 -0
- imagenet_ood.py +132 -0
- images/imagenet/n02828884_603_bench.jpg +0 -0
- images/imagenet/n02834778_3678_bicycle.jpg +0 -0
- images/imagenet/n02880940_17711_bowl.jpg +0 -0
- images/imagenet/n03062245_2005_cocktail_shaker.jpg +0 -0
- images/imagenet/n03495258_9079_harp.jpg +0 -0
- images/ood/Rademacher_025_Rademacher_02897.jpg +0 -0
- images/ood/art_2.jpg +0 -0
- images/ood/bumpy_0140.jpg +0 -0
- images/ood/door_022_00033.jpg +0 -0
- images/ood/fdb9d2ac3f37c0c80baa7f91775e58ce.jpg +0 -0
- images/ood/fed8bd31654ee16a9cd83c8de72ddb5b.jpg +0 -0
- images/ood/ff7f83dfb2485306b62bf64726f4f932.jpg +0 -0
- images/ood/ffd5b90b142ebcb46cffc96314e6bcd3.jpg +0 -0
- images/ood/fireworks_001_0001.png +0 -0
- images/ood/i_ice_floe_00002019.jpg +0 -0
- images/ood/i_igloo_00002495.jpg +0 -0
- images/ood/knitted_0141.jpg +0 -0
- images/ood/pyramid_008_image_0011.jpg +0 -0
- images/ood/scissors_040_scissors_0085_pixabay.jpg +0 -0
- images/ood/striped_0063.jpg +0 -0
- images/ood/sun_awovauomdhnolaul.jpg +0 -0
- images/ood/sun_bzrmbfcxyebbxuqu.jpg +0 -0
- images/ood/sun_bzuroamlnffhyuqn.jpg +0 -0
- images/ood/toy_2.jpg +0 -0
- images/ood/w_waterfall_00004924.jpg +0 -0
- images/ood/w_wheat_field_00004628.jpg +0 -0
- images/ood/watermelon_0.9992305.JPEG +0 -0
- requirements.txt +87 -5
.gitignore
CHANGED
|
@@ -138,4 +138,5 @@ dmypy.json
|
|
| 138 |
cython_debug/
|
| 139 |
|
| 140 |
.DS_Store
|
| 141 |
-
.vscode
|
|
|
|
|
|
| 138 |
cython_debug/
|
| 139 |
|
| 140 |
.DS_Store
|
| 141 |
+
.vscode
|
| 142 |
+
data/
|
README.md
CHANGED
|
@@ -16,7 +16,7 @@ Out-of-distribution (OOD) detection is an essential safety measure for machine l
|
|
| 16 |
|
| 17 |
This demo is [online](https://huggingface.co/spaces/edadaltocg/ood-detection) at `https://huggingface.co/spaces/edadaltocg/ood-detection`
|
| 18 |
|
| 19 |
-
## Running Gradio app locally
|
| 20 |
|
| 21 |
1. Install dependencies:
|
| 22 |
|
|
@@ -31,9 +31,3 @@ python app.py
|
|
| 31 |
```
|
| 32 |
|
| 33 |
3. Open the app in your browser at `http://localhost:7860`.
|
| 34 |
-
|
| 35 |
-
## Methods implemented
|
| 36 |
-
|
| 37 |
-
- [ ] [Mahalanobis Distance](https://arxiv.org/abs/1807.03888)
|
| 38 |
-
- [x] [Maximum Softmax Probability](https://arxiv.org/abs/1610.02136)
|
| 39 |
-
- [x] [Energy Based Out-of-Distribution Detection](https://arxiv.org/abs/2010.03759)
|
|
|
|
| 16 |
|
| 17 |
This demo is [online](https://huggingface.co/spaces/edadaltocg/ood-detection) at `https://huggingface.co/spaces/edadaltocg/ood-detection`
|
| 18 |
|
| 19 |
+
## Running Gradio app locally
|
| 20 |
|
| 21 |
1. Install dependencies:
|
| 22 |
|
|
|
|
| 31 |
```
|
| 32 |
|
| 33 |
3. Open the app in your browser at `http://localhost:7860`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
|
@@ -3,22 +3,21 @@ Gradio demo of image classification with OOD detection.
|
|
| 3 |
|
| 4 |
If the image example is probably OOD, the model will abstain from the prediction.
|
| 5 |
"""
|
| 6 |
-
import os
|
| 7 |
-
import pickle
|
| 8 |
import json
|
|
|
|
|
|
|
| 9 |
from glob import glob
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
-
from gradio.components import Image, Label, JSON
|
| 13 |
import numpy as np
|
| 14 |
-
import torch
|
| 15 |
import timm
|
|
|
|
|
|
|
|
|
|
| 16 |
from timm.data import resolve_data_config
|
| 17 |
from timm.data.transforms_factory import create_transform
|
| 18 |
from torchvision.models.feature_extraction import create_feature_extractor, get_graph_node_names
|
| 19 |
|
| 20 |
-
import logging
|
| 21 |
-
|
| 22 |
_logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -26,7 +25,7 @@ TOPK = 3
|
|
| 26 |
|
| 27 |
# load model
|
| 28 |
print("Loading model...")
|
| 29 |
-
model = timm.create_model("resnet50", pretrained=True)
|
| 30 |
model.to(device)
|
| 31 |
model.eval()
|
| 32 |
|
|
@@ -34,28 +33,25 @@ model.eval()
|
|
| 34 |
idx2label = json.loads(open("ilsvrc2012.json").read())
|
| 35 |
idx2label = {int(k): v for k, v in idx2label.items()}
|
| 36 |
print(idx2label)
|
|
|
|
| 37 |
|
| 38 |
# transformation
|
| 39 |
config = resolve_data_config({}, model=model)
|
| 40 |
config["is_training"] = False
|
| 41 |
transform = create_transform(**config)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
print(get_graph_node_names(model)[0])
|
| 45 |
-
|
| 46 |
-
# load train scores
|
| 47 |
penultimate_features_key = "global_pool.flatten"
|
| 48 |
logits_key = "fc"
|
| 49 |
features_names = [penultimate_features_key, logits_key]
|
| 50 |
|
| 51 |
-
# create feature extractor
|
| 52 |
feature_extractor = create_feature_extractor(model, features_names)
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
msp_threshold = 0.3796
|
| 56 |
energy_threshold = 0.3781
|
| 57 |
-
|
| 58 |
-
## unpickle detectors
|
| 59 |
|
| 60 |
|
| 61 |
def mahalanobis_penult(features):
|
|
@@ -72,9 +68,18 @@ def energy(logits):
|
|
| 72 |
return torch.logsumexp(logits, dim=1).item()
|
| 73 |
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
def predict(image):
|
| 76 |
# forward pass
|
| 77 |
inputs = transform(image).unsqueeze(0)
|
|
|
|
| 78 |
with torch.no_grad():
|
| 79 |
features = feature_extractor(inputs)
|
| 80 |
|
|
@@ -86,13 +91,16 @@ def predict(image):
|
|
| 86 |
|
| 87 |
result = {idx2label[i.item()]: v.item() for i, v in zip(class_idxs.squeeze(), softmax.squeeze())}
|
| 88 |
# OOD
|
| 89 |
-
msp_score = msp(features[logits_key])
|
| 90 |
-
energy_score = energy(features[logits_key])
|
|
|
|
| 91 |
ood_scores = {
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
"
|
| 95 |
-
"
|
|
|
|
|
|
|
| 96 |
}
|
| 97 |
_logger.info(ood_scores)
|
| 98 |
return result, ood_scores
|
|
@@ -100,9 +108,9 @@ def predict(image):
|
|
| 100 |
|
| 101 |
def main():
|
| 102 |
# image examples for demo shuffled
|
| 103 |
-
examples = glob("images/imagenet
|
| 104 |
np.random.seed(42)
|
| 105 |
-
np.random.shuffle(examples)
|
| 106 |
|
| 107 |
# gradio interface
|
| 108 |
interface = gr.Interface(
|
|
@@ -117,11 +125,24 @@ def main():
|
|
| 117 |
allow_flagging="never",
|
| 118 |
theme="default",
|
| 119 |
title="OOD Detection 🧐",
|
| 120 |
-
description=
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
|
|
|
| 125 |
interface.close()
|
| 126 |
|
| 127 |
|
|
|
|
| 3 |
|
| 4 |
If the image example is probably OOD, the model will abstain from the prediction.
|
| 5 |
"""
|
|
|
|
|
|
|
| 6 |
import json
|
| 7 |
+
import logging
|
| 8 |
+
import pickle
|
| 9 |
from glob import glob
|
| 10 |
|
| 11 |
import gradio as gr
|
|
|
|
| 12 |
import numpy as np
|
|
|
|
| 13 |
import timm
|
| 14 |
+
import torch
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
from gradio.components import JSON, Image, Label
|
| 17 |
from timm.data import resolve_data_config
|
| 18 |
from timm.data.transforms_factory import create_transform
|
| 19 |
from torchvision.models.feature_extraction import create_feature_extractor, get_graph_node_names
|
| 20 |
|
|
|
|
|
|
|
| 21 |
_logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 25 |
|
| 26 |
# load model
|
| 27 |
print("Loading model...")
|
| 28 |
+
model = timm.create_model("resnet50.tv2_in1k", pretrained=True)
|
| 29 |
model.to(device)
|
| 30 |
model.eval()
|
| 31 |
|
|
|
|
| 33 |
idx2label = json.loads(open("ilsvrc2012.json").read())
|
| 34 |
idx2label = {int(k): v for k, v in idx2label.items()}
|
| 35 |
print(idx2label)
|
| 36 |
+
print(idx2label.values())
|
| 37 |
|
| 38 |
# transformation
|
| 39 |
config = resolve_data_config({}, model=model)
|
| 40 |
config["is_training"] = False
|
| 41 |
transform = create_transform(**config)
|
| 42 |
|
| 43 |
+
# create feature extractor
|
|
|
|
|
|
|
|
|
|
| 44 |
penultimate_features_key = "global_pool.flatten"
|
| 45 |
logits_key = "fc"
|
| 46 |
features_names = [penultimate_features_key, logits_key]
|
| 47 |
|
|
|
|
| 48 |
feature_extractor = create_feature_extractor(model, features_names)
|
| 49 |
|
| 50 |
+
centroids = torch.from_numpy(pickle.load(open("centroids_resnet50.tv2_in1k_igeood_logits.pkl", "rb"))).to(device)
|
| 51 |
+
# OOD detector thresholds
|
| 52 |
msp_threshold = 0.3796
|
| 53 |
energy_threshold = 0.3781
|
| 54 |
+
igeood_threshold = 2.4984
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
def mahalanobis_penult(features):
|
|
|
|
| 68 |
return torch.logsumexp(logits, dim=1).item()
|
| 69 |
|
| 70 |
|
| 71 |
+
def igeoodlogits_vec(logits, temperature, centroids, epsilon=1e-12):
|
| 72 |
+
logits = torch.sqrt(F.softmax(logits / temperature, dim=1))
|
| 73 |
+
centroids = torch.sqrt(F.softmax(centroids / temperature, dim=1))
|
| 74 |
+
mult = logits @ centroids.T
|
| 75 |
+
stack = 2 * torch.acos(torch.clamp(mult, -1 + epsilon, 1 - epsilon))
|
| 76 |
+
return stack.mean(dim=1).item()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
def predict(image):
|
| 80 |
# forward pass
|
| 81 |
inputs = transform(image).unsqueeze(0)
|
| 82 |
+
inputs = inputs.to(device)
|
| 83 |
with torch.no_grad():
|
| 84 |
features = feature_extractor(inputs)
|
| 85 |
|
|
|
|
| 91 |
|
| 92 |
result = {idx2label[i.item()]: v.item() for i, v in zip(class_idxs.squeeze(), softmax.squeeze())}
|
| 93 |
# OOD
|
| 94 |
+
msp_score = round(msp(features[logits_key]), 4)
|
| 95 |
+
energy_score = round(energy(features[logits_key]), 4)
|
| 96 |
+
igeood_scores = round(igeoodlogits_vec(features[logits_key], 1, centroids), 4)
|
| 97 |
ood_scores = {
|
| 98 |
+
"MSP": msp_score,
|
| 99 |
+
"MSP, is the input OOD?": msp_score < msp_threshold,
|
| 100 |
+
"Energy": energy_score,
|
| 101 |
+
"Energy, is the input OOD?": energy_score < energy_threshold,
|
| 102 |
+
"Igeood": igeood_scores,
|
| 103 |
+
"Igeood, is the input OOD?": igeood_scores < igeood_threshold,
|
| 104 |
}
|
| 105 |
_logger.info(ood_scores)
|
| 106 |
return result, ood_scores
|
|
|
|
| 108 |
|
| 109 |
def main():
|
| 110 |
# image examples for demo shuffled
|
| 111 |
+
examples = glob("images/imagenet/*") + glob("images/ood/*")
|
| 112 |
np.random.seed(42)
|
| 113 |
+
# np.random.shuffle(examples)
|
| 114 |
|
| 115 |
# gradio interface
|
| 116 |
interface = gr.Interface(
|
|
|
|
| 125 |
allow_flagging="never",
|
| 126 |
theme="default",
|
| 127 |
title="OOD Detection 🧐",
|
| 128 |
+
description=(
|
| 129 |
+
"Out-of-distribution (OOD) detection is an essential safety measure for machine learning models. "
|
| 130 |
+
"The objective of an OOD detector is to determine wether the input sample comes from the distribution known by the AI model. "
|
| 131 |
+
"For instance, an input that does not belong to any of the known classes or is from a different domain should be flagged by the detector.\n"
|
| 132 |
+
"In this demo we will display the decision of three OOD detectors on a ResNet-50 model trained to classify on the ImageNet-1K dataset (top-1 accuracy 80%)."
|
| 133 |
+
"This model can classify among 1000 classes from several categories, including `animals`, `vehicles`, `clothing`, `instruments`, `plants`, etc. "
|
| 134 |
+
"For the complete hierarchy of classes, please check the website https://observablehq.com/@mbostock/imagenet-hierarchy. "
|
| 135 |
+
"\n\n"
|
| 136 |
+
"## Instructions:\n"
|
| 137 |
+
"1. Upload an image of your choice or select one from the examples bar.\n"
|
| 138 |
+
"2. The model will predict the top 3 most likely classes for the image.\n"
|
| 139 |
+
"3. The OOD detectors will output their scores and decision on the image. The smaller the score, the least confident the detector is on the sample being in-distribution.\n"
|
| 140 |
+
"4. If the image is OOD, the model will abstain from the prediction and flag it to the practicioner.\n"
|
| 141 |
+
"\n\n\nEnjoy the demo!"
|
| 142 |
+
),
|
| 143 |
+
cache_examples=True,
|
| 144 |
)
|
| 145 |
+
interface.launch(server_port=7860)
|
| 146 |
interface.close()
|
| 147 |
|
| 148 |
|
centroids_resnet50.tv2_in1k_igeood_logits.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f8079b4fc02b6542210d147d98d08b6220372534a18ba7ef9e844b17ab0a1d7e
|
| 3 |
+
size 4000163
|
imagenet_ood.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import os
|
| 3 |
+
from typing import Callable, Optional
|
| 4 |
+
|
| 5 |
+
from torchvision.datasets import ImageFolder
|
| 6 |
+
from torchvision.datasets.utils import check_integrity, download_and_extract_archive, verify_str_arg
|
| 7 |
+
|
| 8 |
+
_logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class ImageNetA(ImageFolder):
|
| 12 |
+
"""ImageNetA dataset.
|
| 13 |
+
|
| 14 |
+
- Paper: [https://arxiv.org/abs/1907.07174](https://arxiv.org/abs/1907.07174).
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
base_folder = "imagenet-a"
|
| 18 |
+
url = "https://people.eecs.berkeley.edu/~hendrycks/imagenet-a.tar"
|
| 19 |
+
filename = "imagenet-a.tar"
|
| 20 |
+
tgz_md5 = "c3e55429088dc681f30d81f4726b6595"
|
| 21 |
+
|
| 22 |
+
def __init__(self, root: str, split=None, transform: Optional[Callable] = None, download: bool = False, **kwargs):
|
| 23 |
+
self.root = root
|
| 24 |
+
|
| 25 |
+
if download:
|
| 26 |
+
self.download()
|
| 27 |
+
|
| 28 |
+
if not self._check_integrity():
|
| 29 |
+
raise RuntimeError("Dataset not found or corrupted." + " You can use download=True to download it")
|
| 30 |
+
|
| 31 |
+
super().__init__(root=os.path.join(root, self.base_folder), transform=transform, **kwargs)
|
| 32 |
+
|
| 33 |
+
def _check_exists(self) -> bool:
|
| 34 |
+
return os.path.exists(os.path.join(self.root, self.base_folder))
|
| 35 |
+
|
| 36 |
+
def _check_integrity(self) -> bool:
|
| 37 |
+
return check_integrity(os.path.join(self.root, self.filename), self.tgz_md5)
|
| 38 |
+
|
| 39 |
+
def download(self) -> None:
|
| 40 |
+
if self._check_integrity() and self._check_exists():
|
| 41 |
+
_logger.debug("Files already downloaded and verified")
|
| 42 |
+
return
|
| 43 |
+
download_and_extract_archive(self.url, self.root, filename=self.filename, md5=self.tgz_md5)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class ImageNetO(ImageNetA):
|
| 47 |
+
"""ImageNetO datasets.
|
| 48 |
+
|
| 49 |
+
Contains unknown classes to ImageNet-1k.
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
- Paper: [https://arxiv.org/abs/1907.07174](https://arxiv.org/abs/1907.07174)
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
base_folder = "imagenet-o"
|
| 56 |
+
url = "https://people.eecs.berkeley.edu/~hendrycks/imagenet-o.tar"
|
| 57 |
+
filename = "imagenet-o.tar"
|
| 58 |
+
tgz_md5 = "86bd7a50c1c4074fb18fc5f219d6d50b"
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class ImageNetR(ImageNetA):
|
| 62 |
+
"""ImageNet-R(endition) dataset.
|
| 63 |
+
|
| 64 |
+
Contains art, cartoons, deviantart, graffiti, embroidery, graphics, origami, paintings,
|
| 65 |
+
patterns, plastic objects,plush objects, sculptures, sketches, tattoos, toys,
|
| 66 |
+
and video game renditions of ImageNet-1k classes.
|
| 67 |
+
|
| 68 |
+
- Paper: [https://arxiv.org/abs/2006.16241](https://arxiv.org/abs/2006.16241)
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
base_folder = "imagenet-r"
|
| 72 |
+
url = "https://people.eecs.berkeley.edu/~hendrycks/imagenet-r.tar"
|
| 73 |
+
filename = "imagenet-r.tar"
|
| 74 |
+
tgz_md5 = "a61312130a589d0ca1a8fca1f2bd3337"
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class NINCOFull(ImageFolder):
|
| 78 |
+
"""`NINCO` Dataset subset.
|
| 79 |
+
|
| 80 |
+
Args:
|
| 81 |
+
root (string): Root directory of dataset where directory
|
| 82 |
+
exists or will be saved to if download is set to True.
|
| 83 |
+
split (string, optional): The dataset split, not used.
|
| 84 |
+
transform (callable, optional): A function/transform that takes in an PIL image
|
| 85 |
+
and returns a transformed version. E.g, `transforms.RandomCrop`.
|
| 86 |
+
download (bool, optional): If true, downloads the dataset from the internet and
|
| 87 |
+
puts it in root directory. If dataset is already downloaded, it is not
|
| 88 |
+
downloaded again.
|
| 89 |
+
**kwargs: Additional arguments passed to :class:`~torchvision.datasets.ImageFolder`.
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
PAPER_URL = "https://arxiv.org/pdf/2306.00826.pdf"
|
| 93 |
+
base_folder = "ninco"
|
| 94 |
+
filename = "NINCO_all.tar.gz"
|
| 95 |
+
file_md5 = "b9ffae324363cd900a81ce3c367cd834"
|
| 96 |
+
url = "https://zenodo.org/record/8013288/files/NINCO_all.tar.gz"
|
| 97 |
+
# size: 15393
|
| 98 |
+
|
| 99 |
+
def __init__(
|
| 100 |
+
self, root: str, split=None, transform: Optional[Callable] = None, download: bool = False, **kwargs
|
| 101 |
+
) -> None:
|
| 102 |
+
self.root = os.path.expanduser(root)
|
| 103 |
+
self.dataset_folder = os.path.join(self.root, self.base_folder)
|
| 104 |
+
self.archive = os.path.join(self.root, self.filename)
|
| 105 |
+
|
| 106 |
+
if download:
|
| 107 |
+
self.download()
|
| 108 |
+
|
| 109 |
+
if not self._check_integrity():
|
| 110 |
+
raise RuntimeError("Dataset not found or corrupted." + " You can use download=True to download it")
|
| 111 |
+
|
| 112 |
+
super().__init__(self.dataset_folder, transform=transform, **kwargs)
|
| 113 |
+
|
| 114 |
+
def _check_integrity(self) -> bool:
|
| 115 |
+
return check_integrity(self.archive, self.file_md5)
|
| 116 |
+
|
| 117 |
+
def _check_exists(self) -> bool:
|
| 118 |
+
return os.path.exists(self.dataset_folder)
|
| 119 |
+
|
| 120 |
+
def download(self) -> None:
|
| 121 |
+
if self._check_integrity() and self._check_exists():
|
| 122 |
+
return
|
| 123 |
+
download_and_extract_archive(
|
| 124 |
+
self.url, download_root=self.root, extract_root=self.dataset_folder, md5=self.file_md5
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
ImageNetR(root="data", download=True)
|
| 130 |
+
ImageNetO(root="data", download=True)
|
| 131 |
+
ImageNetA(root="data", download=True)
|
| 132 |
+
NINCOFull(root="data", download=True)
|
images/imagenet/n02828884_603_bench.jpg
DELETED
|
Binary file (148 kB)
|
|
|
images/imagenet/n02834778_3678_bicycle.jpg
DELETED
|
Binary file (75.3 kB)
|
|
|
images/imagenet/n02880940_17711_bowl.jpg
DELETED
|
Binary file (68.9 kB)
|
|
|
images/imagenet/n03062245_2005_cocktail_shaker.jpg
DELETED
|
Binary file (57.7 kB)
|
|
|
images/imagenet/n03495258_9079_harp.jpg
DELETED
|
Binary file (112 kB)
|
|
|
images/ood/Rademacher_025_Rademacher_02897.jpg
ADDED
|
images/ood/art_2.jpg
ADDED
|
images/ood/bumpy_0140.jpg
DELETED
|
Binary file (105 kB)
|
|
|
images/ood/door_022_00033.jpg
ADDED
|
images/ood/fdb9d2ac3f37c0c80baa7f91775e58ce.jpg
DELETED
|
Binary file (577 kB)
|
|
|
images/ood/fed8bd31654ee16a9cd83c8de72ddb5b.jpg
DELETED
|
Binary file (752 kB)
|
|
|
images/ood/ff7f83dfb2485306b62bf64726f4f932.jpg
DELETED
|
Binary file (191 kB)
|
|
|
images/ood/ffd5b90b142ebcb46cffc96314e6bcd3.jpg
DELETED
|
Binary file (206 kB)
|
|
|
images/ood/fireworks_001_0001.png
ADDED
|
images/ood/i_ice_floe_00002019.jpg
DELETED
|
Binary file (58.3 kB)
|
|
|
images/ood/i_igloo_00002495.jpg
DELETED
|
Binary file (60.6 kB)
|
|
|
images/ood/knitted_0141.jpg
DELETED
|
Binary file (71.9 kB)
|
|
|
images/ood/pyramid_008_image_0011.jpg
ADDED
|
images/ood/scissors_040_scissors_0085_pixabay.jpg
ADDED
|
images/ood/striped_0063.jpg
DELETED
|
Binary file (22.3 kB)
|
|
|
images/ood/sun_awovauomdhnolaul.jpg
DELETED
|
Binary file (33.2 kB)
|
|
|
images/ood/sun_bzrmbfcxyebbxuqu.jpg
DELETED
|
Binary file (361 kB)
|
|
|
images/ood/sun_bzuroamlnffhyuqn.jpg
DELETED
|
Binary file (170 kB)
|
|
|
images/ood/toy_2.jpg
ADDED
|
images/ood/w_waterfall_00004924.jpg
DELETED
|
Binary file (69.3 kB)
|
|
|
images/ood/w_wheat_field_00004628.jpg
DELETED
|
Binary file (105 kB)
|
|
|
images/ood/watermelon_0.9992305.JPEG
ADDED
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,87 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohttp==3.8.3
|
| 3 |
+
aiosignal==1.3.1
|
| 4 |
+
altair==5.1.0
|
| 5 |
+
anyio==3.6.2
|
| 6 |
+
async-timeout==4.0.2
|
| 7 |
+
attrs==23.1.0
|
| 8 |
+
bcrypt==4.0.1
|
| 9 |
+
black==22.10.0
|
| 10 |
+
certifi==2022.9.24
|
| 11 |
+
cffi==1.15.1
|
| 12 |
+
charset-normalizer==2.1.1
|
| 13 |
+
click==8.1.3
|
| 14 |
+
contourpy==1.0.6
|
| 15 |
+
cryptography==38.0.4
|
| 16 |
+
cycler==0.11.0
|
| 17 |
+
fastapi==0.88.0
|
| 18 |
+
ffmpy==0.3.0
|
| 19 |
+
filelock==3.8.1
|
| 20 |
+
fonttools==4.38.0
|
| 21 |
+
frozenlist==1.3.3
|
| 22 |
+
fsspec==2022.11.0
|
| 23 |
+
gradio==3.41.2
|
| 24 |
+
gradio_client==0.5.0
|
| 25 |
+
h11==0.12.0
|
| 26 |
+
httpcore==0.15.0
|
| 27 |
+
httpx==0.23.1
|
| 28 |
+
huggingface-hub==0.16.4
|
| 29 |
+
idna==3.4
|
| 30 |
+
importlib-resources==6.0.1
|
| 31 |
+
Jinja2==3.1.2
|
| 32 |
+
jsonschema==4.19.0
|
| 33 |
+
jsonschema-specifications==2023.7.1
|
| 34 |
+
kiwisolver==1.4.4
|
| 35 |
+
linkify-it-py==1.0.3
|
| 36 |
+
markdown-it-py==2.1.0
|
| 37 |
+
MarkupSafe==2.1.1
|
| 38 |
+
matplotlib==3.6.2
|
| 39 |
+
mdit-py-plugins==0.3.2
|
| 40 |
+
mdurl==0.1.2
|
| 41 |
+
mpmath==1.3.0
|
| 42 |
+
multidict==6.0.3
|
| 43 |
+
mypy-extensions==0.4.3
|
| 44 |
+
networkx==3.1
|
| 45 |
+
numpy==1.23.5
|
| 46 |
+
orjson==3.8.3
|
| 47 |
+
packaging==21.3
|
| 48 |
+
pandas==1.5.2
|
| 49 |
+
paramiko==2.12.0
|
| 50 |
+
pathspec==0.10.2
|
| 51 |
+
Pillow==9.3.0
|
| 52 |
+
platformdirs==2.5.4
|
| 53 |
+
pycparser==2.21
|
| 54 |
+
pycryptodome==3.16.0
|
| 55 |
+
pydantic==1.10.2
|
| 56 |
+
pydub==0.25.1
|
| 57 |
+
PyNaCl==1.5.0
|
| 58 |
+
pyparsing==3.0.9
|
| 59 |
+
python-dateutil==2.8.2
|
| 60 |
+
python-multipart==0.0.5
|
| 61 |
+
pytz==2022.6
|
| 62 |
+
PyYAML==6.0
|
| 63 |
+
referencing==0.30.2
|
| 64 |
+
regex==2022.10.31
|
| 65 |
+
requests==2.28.1
|
| 66 |
+
rfc3986==1.5.0
|
| 67 |
+
rpds-py==0.10.0
|
| 68 |
+
safetensors==0.3.3
|
| 69 |
+
semantic-version==2.10.0
|
| 70 |
+
six==1.16.0
|
| 71 |
+
sniffio==1.3.0
|
| 72 |
+
starlette==0.22.0
|
| 73 |
+
sympy==1.12
|
| 74 |
+
timm==0.9.5
|
| 75 |
+
tokenizers==0.13.2
|
| 76 |
+
tomli==2.0.1
|
| 77 |
+
toolz==0.12.0
|
| 78 |
+
torch==2.0.1
|
| 79 |
+
torchvision==0.15.2
|
| 80 |
+
tqdm==4.64.1
|
| 81 |
+
transformers==4.32.1
|
| 82 |
+
typing_extensions==4.4.0
|
| 83 |
+
uc-micro-py==1.0.1
|
| 84 |
+
urllib3==1.26.13
|
| 85 |
+
uvicorn==0.20.0
|
| 86 |
+
websockets==10.4
|
| 87 |
+
yarl==1.8.2
|