forked from opencv/opencv_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add evaluation framework and imagenet evaluation (opencv#69)
- Loading branch information
1 parent
64b3eda
commit 004a627
Showing
12 changed files
with
311 additions
and
27 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
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
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
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
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
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
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
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
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,55 @@ | ||
# Accuracy evaluation of models in OpenCV Zoo | ||
|
||
Make sure you have the following packages installed: | ||
|
||
```shell | ||
pip install tqdm | ||
``` | ||
|
||
Generally speaking, evaluation can be done with the following command: | ||
|
||
```shell | ||
python eval.py -m model_name -d dataset_name -dr dataset_root_dir | ||
``` | ||
|
||
Supported datasets: | ||
- [ImageNet](./datasets/imagenet.py) | ||
|
||
## ImageNet | ||
|
||
### Prepare data | ||
|
||
Please visit https://image-net.org/ to download the ImageNet dataset and [the labels from caffe](http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz). Organize files as follow: | ||
|
||
```shell | ||
$ tree -L 2 /path/to/imagenet | ||
. | ||
├── caffe_ilsvrc12 | ||
│ ├── det_synset_words.txt | ||
│ ├── imagenet.bet.pickle | ||
│ ├── imagenet_mean.binaryproto | ||
│ ├── synsets.txt | ||
│ ├── synset_words.txt | ||
│ ├── test.txt | ||
│ ├── train.txt | ||
│ └── val.txt | ||
├── caffe_ilsvrc12.tar.gz | ||
├── ILSVRC | ||
│ ├── Annotations | ||
│ ├── Data | ||
│ └── ImageSets | ||
├── imagenet_object_localization_patched2019.tar.gz | ||
├── LOC_sample_submission.csv | ||
├── LOC_synset_mapping.txt | ||
├── LOC_train_solution.csv | ||
└── LOC_val_solution.csv | ||
``` | ||
|
||
### Evaluation | ||
|
||
Run evaluation with the following command: | ||
|
||
```shell | ||
python eval.py -m mobilenet -d imagenet -dr /path/to/imagenet | ||
``` | ||
|
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,15 @@ | ||
from .imagenet import ImageNet | ||
|
||
class Registery: | ||
def __init__(self, name): | ||
self._name = name | ||
self._dict = dict() | ||
|
||
def get(self, key): | ||
return self._dict[key] | ||
|
||
def register(self, item): | ||
self._dict[item.__name__] = item | ||
|
||
DATASETS = Registery("Datasets") | ||
DATASETS.register(ImageNet) |
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,64 @@ | ||
import os | ||
|
||
import numpy as np | ||
import cv2 as cv | ||
|
||
from tqdm import tqdm | ||
|
||
class ImageNet: | ||
def __init__(self, root, size=224): | ||
self.root = root | ||
self.size = size | ||
self.top1_acc = -1 | ||
self.top5_acc = -1 | ||
|
||
self.root_val = os.path.join(self.root, "ILSVRC", "Data", "CLS-LOC", "val") | ||
self.val_label_file = os.path.join(self.root, "caffe_ilsvrc12", "val.txt") | ||
|
||
self.val_label = self.load_label(self.val_label_file) | ||
|
||
@property | ||
def name(self): | ||
return self.__class__.__name__ | ||
|
||
def load_label(self, label_file): | ||
label = list() | ||
with open(label_file, "r") as f: | ||
for line in f: | ||
line = line.strip() | ||
key, value = line.split() | ||
|
||
key = os.path.join(self.root_val, key) | ||
value = int(value) | ||
|
||
label.append([key, value]) | ||
|
||
return label | ||
|
||
def eval(self, model): | ||
top_1_hits = 0 | ||
top_5_hits = 0 | ||
pbar = tqdm(self.val_label) | ||
for fn, label in pbar: | ||
pbar.set_description("Evaluating {} with {} val set".format(model.name, self.name)) | ||
|
||
img = cv.imread(fn) | ||
img = cv.cvtColor(img, cv.COLOR_BGR2RGB) | ||
img = cv.resize(img, dsize=(256, 256)) | ||
img = img[16:240, 16:240, :] | ||
|
||
pred = model.infer(img) | ||
if label == pred[0][0]: | ||
top_1_hits += 1 | ||
if label in pred[0]: | ||
top_5_hits += 1 | ||
|
||
self.top1_acc = top_1_hits/(len(self.val_label) * 1.0) | ||
self.top5_acc = top_5_hits/(len(self.val_label) * 1.0) | ||
|
||
def get_result(self): | ||
return self.top1_acc, self.top5_acc | ||
|
||
def print_result(self): | ||
print("Top-1 Accuracy: {:.2f}%; Top-5 Accuracy: {:.2f}%".format(self.top1_acc*100, self.top5_acc*100)) | ||
|
Oops, something went wrong.