Skip to content

Commit

Permalink
Merge pull request #28 from albertomontesg/feature/davis
Browse files Browse the repository at this point in the history
Improved DAVIS class
  • Loading branch information
albertomontesg committed Apr 26, 2018
2 parents 6437442 + d1fceb2 commit 0d1c01b
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 28 deletions.
13 changes: 13 additions & 0 deletions davisinteractive/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Python2/3 Compatibility

# pathlib
try:
from pathlib2 import Path
except ImportError:
from pathlib import Path

# Testing
try:
from unittest.mock import patch
except ImportError:
from mock import patch
53 changes: 30 additions & 23 deletions davisinteractive/dataset/davis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import os
import tempfile
import zipfile
from pathlib import Path

import numpy as np
from PIL import Image
from six.moves import urllib

from .. import logging
from ..common import Path

# Python2/3 Compatibility
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError # pylint: disable=redefined-builtin

with Path(__file__).parent.joinpath('davis.json').open() as fp:
_DATASET = json.load(fp)
Expand Down Expand Up @@ -82,7 +88,7 @@ def __init__(self, davis_root=None):
'Davis root dir not especified. Please specify it in the '
'environmental variable DAVIS_DATASET or give it as parameter '
'in davis_root.')
self.davis_root = Path(self.davis_root)
self.davis_root = Path(self.davis_root).expanduser()
if self.davis_root.name != 'DAVIS':
raise ValueError('Davis root folder must be named "DAVIS"')

Expand All @@ -96,7 +102,7 @@ def _download_scribbles(self):

# Downloading
logging.info('Downloading Scribbles')
urllib.request.urlretrieve(self.SCRIBBLES_URL, download_file)
urllib.request.urlretrieve(self.SCRIBBLES_URL, str(download_file))

# Check integrity
logging.info('Checking hash')
Expand All @@ -108,7 +114,9 @@ def _download_scribbles(self):

# Extract file
logging.info('Extracting file')
zipfile.ZipFile(download_file).extractall(self.davis_root.parent)
print(download_file)
zipfile.ZipFile(download_file.open(mode='rb')).extractall(
str(self.davis_root.parent))
download_file.unlink()
logging.info('Download completed')

Expand All @@ -127,32 +135,27 @@ def check_files(self, sequences):
FileNotFoundError: if any required files is not found.
"""
for seq in sequences:
seq_scribbles_path = os.path.join(self.davis_root,
Davis.SCRIBBLES_SUBDIR, seq)
seq_annotations_path = os.path.join(self.davis_root,
Davis.ANNOTATIONS_SUBDIR,
Davis.RESOLUTION, seq)
seq_scribbles_path = self.davis_root.joinpath(
Davis.SCRIBBLES_SUBDIR, seq)
seq_annotations_path = self.davis_root.joinpath(
Davis.ANNOTATIONS_SUBDIR, Davis.RESOLUTION, seq)

# Check scribbles files needed to give them as base for the user
nb_scribbles = self.dataset[seq]['num_scribbles']
for i in range(1, nb_scribbles):
if not os.path.exists(
os.path.join(seq_scribbles_path,
'{:03d}.json'.format(i))):
for i in range(1, nb_scribbles + 1):
scribble_file = seq_scribbles_path / '{:03d}.json'.format(i)
if not scribble_file.exists():
self._download_scribbles()
if not os.path.exists(
os.path.join(seq_scribbles_path,
'{:03d}.json'.format(i))):
if not scribble_file.exists():
raise FileNotFoundError(
('Scribble file not found for sequence '
'{} and scribble {}').format(seq, i))

# Check annotations files required for the evaluation
nb_frames = self.dataset[seq]['num_frames']
for i in range(nb_frames):
if not os.path.exists(
os.path.join(seq_annotations_path,
'{:05d}.png'.format(i))):
annotation_file = seq_annotations_path / '{:05}.png'.format(i)
if not annotation_file.exists():
raise FileNotFoundError(('Annotations file not found for '
'sequence {} and frame {}').format(
seq, i))
Expand Down Expand Up @@ -207,7 +210,8 @@ def load_annotations(self, sequence, dtype=np.int):
(num_frames, img_size[1], img_size[0]), dtype=dtype)

for f in range(num_frames):
mask = Image.open(os.path.join(root_path, '{:05d}.png'.format(f)))
ann_path = root_path / '{:05d}.png'.format(f)
mask = Image.open(ann_path)
mask = np.asarray(mask)
assert mask.shape == tuple(img_size[::-1])
annotations[f] = mask
Expand Down Expand Up @@ -236,12 +240,15 @@ def load_images(self, sequence, dtype=np.uint8):
num_frames = self.dataset[sequence]['num_frames']
img_size = self.dataset[sequence]['image_size']

images = np.empty((num_frames, img_size[1], img_size[0]), dtype=dtype)
images = np.empty(
(num_frames, img_size[1], img_size[0], 3), dtype=dtype)

for f in range(num_frames):
img = Image.open(os.path.join(root_path, '{:05d}.png'.format(f)))
img_path = root_path / '{:05d}.jpg'.format(f)
img = Image.open(img_path)
img = np.asarray(img)
assert img.shape == tuple(img_size[::-1])
assert img.shape[:2] == tuple(img_size[::-1])
assert img.shape[-1] == 3
images[f] = img

logging.verbose('Loaded images for sequence %s' % sequence, 1)
Expand Down
64 changes: 61 additions & 3 deletions davisinteractive/dataset/davis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
import tempfile
import unittest

import numpy as np
import pytest

from davisinteractive.common import Path, patch
from davisinteractive.dataset import Davis
from davisinteractive.utils.scribbles import annotated_frames, is_empty

# Python2/3 Compatibility
try:
from unittest.mock import patch
except ImportError:
from mock import patch
FileNotFoundError
except NameError:
FileNotFoundError = IOError # pylint: disable=redefined-builtin

FIXTURE_DIR = os.path.join(tempfile.mkdtemp(), 'DAVIS')

Expand Down Expand Up @@ -48,3 +52,57 @@ def test_checking_fail(self):
davis = Davis(FIXTURE_DIR)
with pytest.raises(FileNotFoundError):
davis.check_files(Davis.sets['train'])

def test_load_scribble(self):
dataset_dir = Path(__file__).parent / 'test_data' / 'DAVIS'

davis = Davis(dataset_dir)
scribble = davis.load_scribble('bear', 1)

assert scribble['sequence'] == 'bear'
assert not is_empty(scribble)
assert annotated_frames(scribble) == [39]

def test_load_annotations(self):
dataset_dir = Path(__file__).parent / 'test_data' / 'DAVIS'

num_frames = Davis.dataset['bear']['num_frames']
Davis.dataset['bear']['num_frames'] = 1

davis = Davis(dataset_dir)
ann = davis.load_annotations('bear')

assert ann.shape == (1, 480, 854)
assert ann.dtype == np.int
assert np.all(np.unique(ann) == np.asarray([0, 1]))

ann2 = davis.load_annotations('bear', dtype=np.uint8)
assert ann2.shape == (1, 480, 854)
assert ann2.dtype == np.uint8
assert np.all(np.unique(ann2) == np.asarray([0, 1]))
assert np.all(ann2.astype(np.int) == ann)

Davis.dataset['bear']['num_frames'] = num_frames

def test_load_images(self):
dataset_dir = Path(__file__).parent / 'test_data' / 'DAVIS'

num_frames = Davis.dataset['bear']['num_frames']
Davis.dataset['bear']['num_frames'] = 1

davis = Davis(dataset_dir)
img = davis.load_images('bear')

assert img.shape == (1, 480, 854, 3)
assert img.dtype == np.uint8
assert img.min() >= 0
assert img.max() <= 255

img2 = davis.load_images('bear', np.int)
assert img2.shape == (1, 480, 854, 3)
assert img2.dtype == np.int
assert img2.min() >= 0
assert img2.max() <= 255
assert np.all(img.astype(np.int) == img2)

Davis.dataset['bear']['num_frames'] = num_frames
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"scribbles": [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [{"path": [[0.27049180327868855, 0.8767123287671232], [0.2694672131147541, 0.8751902587519026], [0.26741803278688525, 0.8736681887366818], [0.26741803278688525, 0.8721461187214612], [0.26741803278688525, 0.8706240487062404], [0.26741803278688525, 0.8691019786910198], [0.26844262295081966, 0.867579908675799], [0.2694672131147541, 0.8645357686453576], [0.27049180327868855, 0.863013698630137], [0.27151639344262296, 0.8599695585996956], [0.2725409836065574, 0.8569254185692542], [0.2735655737704918, 0.8538812785388128], [0.27561475409836067, 0.8493150684931506], [0.2766393442622951, 0.8432267884322678], [0.2776639344262295, 0.8386605783866058], [0.2776639344262295, 0.8340943683409436], [0.2786885245901639, 0.8280060882800608], [0.2797131147540984, 0.821917808219178], [0.2797131147540984, 0.8158295281582952], [0.2807377049180328, 0.8112633181126332], [0.2807377049180328, 0.8051750380517504], [0.2807377049180328, 0.7990867579908676], [0.2817622950819672, 0.7945205479452054], [0.2817622950819672, 0.7884322678843226], [0.2817622950819672, 0.7823439878234398], [0.2817622950819672, 0.776255707762557], [0.2807377049180328, 0.7701674277016742], [0.2807377049180328, 0.7640791476407914], [0.2807377049180328, 0.7579908675799086], [0.2797131147540984, 0.7519025875190258], [0.2797131147540984, 0.745814307458143], [0.2786885245901639, 0.7397260273972602], [0.2776639344262295, 0.7351598173515982], [0.2776639344262295, 0.7290715372907154], [0.2766393442622951, 0.7245053272450532], [0.27561475409836067, 0.7199391171993912], [0.27459016393442626, 0.7168949771689498], [0.2725409836065574, 0.7123287671232876], [0.27151639344262296, 0.7077625570776256], [0.27049180327868855, 0.7047184170471842], [0.26844262295081966, 0.700152207001522], [0.26639344262295084, 0.69558599695586], [0.26536885245901637, 0.6925418569254186], [0.26331967213114754, 0.6864535768645358], [0.2612704918032787, 0.6834094368340944], [0.25922131147540983, 0.6788432267884322], [0.257172131147541, 0.6757990867579908], [0.2551229508196721, 0.6727549467275494], [0.2530737704918033, 0.669710806697108], [0.2510245901639344, 0.6666666666666666], [0.24795081967213115, 0.6636225266362252], [0.2459016393442623, 0.6621004566210046], [0.24385245901639344, 0.6605783866057838], [0.24180327868852458, 0.6575342465753424], [0.2387295081967213, 0.6560121765601218], [0.23668032786885246, 0.654490106544901], [0.2336065573770492, 0.654490106544901], [0.23155737704918034, 0.654490106544901], [0.22848360655737704, 0.6529680365296804], [0.22540983606557377, 0.654490106544901], [0.2223360655737705, 0.654490106544901], [0.2192622950819672, 0.6560121765601218], [0.21721311475409835, 0.6575342465753424], [0.21413934426229508, 0.6590563165905632], [0.21311475409836064, 0.6605783866057838], [0.20901639344262296, 0.6636225266362252], [0.20799180327868852, 0.6636225266362252], [0.20491803278688525, 0.6681887366818874], [0.2028688524590164, 0.6712328767123288], [0.20081967213114754, 0.6742770167427702], [0.19979508196721313, 0.6773211567732116], [0.1987704918032787, 0.6818873668188736], [0.19774590163934427, 0.6834094368340944], [0.19569672131147542, 0.6864535768645358], [0.19569672131147542, 0.6910197869101978], [0.19467213114754098, 0.6925418569254186], [0.19467213114754098, 0.6971080669710806], [0.19467213114754098, 0.700152207001522], [0.19467213114754098, 0.7031963470319634], [0.19569672131147542, 0.7062404870624048], [0.19672131147540983, 0.7077625570776256], [0.19774590163934427, 0.710806697108067], [0.19979508196721313, 0.7123287671232876], [0.20081967213114754, 0.7138508371385084], [0.2038934426229508, 0.715372907153729], [0.20594262295081966, 0.715372907153729], [0.20901639344262296, 0.7168949771689498], [0.21106557377049182, 0.7168949771689498], [0.21413934426229508, 0.7184170471841704], [0.21721311475409835, 0.7184170471841704], [0.2192622950819672, 0.7184170471841704], [0.22336065573770492, 0.7184170471841704], [0.22643442622950818, 0.7168949771689498], [0.22950819672131148, 0.715372907153729], [0.23258196721311475, 0.7138508371385084], [0.23668032786885246, 0.710806697108067], [0.23975409836065573, 0.7077625570776256], [0.24282786885245902, 0.7062404870624048], [0.24692622950819673, 0.7031963470319634], [0.25, 0.700152207001522], [0.2530737704918033, 0.6971080669710806], [0.25614754098360654, 0.6940639269406392], [0.26024590163934425, 0.6894977168949772], [0.26331967213114754, 0.684931506849315], [0.26741803278688525, 0.6818873668188736], [0.27049180327868855, 0.6757990867579908], [0.27459016393442626, 0.6712328767123288], [0.2776639344262295, 0.665144596651446], [0.2807377049180328, 0.6605783866057838], [0.2848360655737705, 0.6560121765601218], [0.28790983606557374, 0.6529680365296804], [0.29098360655737704, 0.6484018264840182], [0.29508196721311475, 0.6423135464231354], [0.29815573770491804, 0.6377473363774734], [0.3012295081967213, 0.6331811263318112], [0.3043032786885246, 0.6270928462709284], [0.3073770491803279, 0.6225266362252664], [0.3094262295081967, 0.6179604261796042], [0.3114754098360656, 0.6118721461187214], [0.3135245901639344, 0.6073059360730594], [0.3155737704918033, 0.604261796042618], [0.3176229508196721, 0.6012176560121766], [0.319672131147541, 0.5981735159817352], [0.32172131147540983, 0.5951293759512938], [0.3237704918032787, 0.5920852359208524], [0.32581967213114754, 0.589041095890411], [0.32786885245901637, 0.5859969558599696], [0.32991803278688525, 0.5844748858447488], [0.33299180327868855, 0.5829528158295282], [0.3350409836065574, 0.5814307458143074], [0.33709016393442626, 0.578386605783866], [0.3401639344262295, 0.578386605783866], [0.3422131147540984, 0.5768645357686454], [0.3452868852459016, 0.5753424657534246], [0.3483606557377049, 0.5753424657534246], [0.3514344262295082, 0.573820395738204], [0.35450819672131145, 0.573820395738204], [0.35758196721311475, 0.573820395738204], [0.36065573770491804, 0.5722983257229832], [0.3637295081967213, 0.5722983257229832], [0.367827868852459, 0.5707762557077626], [0.3709016393442623, 0.5707762557077626], [0.3739754098360656, 0.5707762557077626], [0.3780737704918033, 0.5707762557077626], [0.38114754098360654, 0.5707762557077626], [0.38524590163934425, 0.5707762557077626], [0.38831967213114754, 0.5707762557077626], [0.39139344262295084, 0.5722983257229832], [0.3944672131147541, 0.573820395738204], [0.3985655737704918, 0.573820395738204], [0.4016393442622951, 0.5753424657534246], [0.4057377049180328, 0.5768645357686454], [0.4088114754098361, 0.578386605783866], [0.41188524590163933, 0.5814307458143074], [0.4139344262295082, 0.5814307458143074], [0.4180327868852459, 0.5859969558599696], [0.42110655737704916, 0.589041095890411], [0.42418032786885246, 0.5920852359208524], [0.42725409836065575, 0.5951293759512938], [0.4293032786885246, 0.5981735159817352], [0.4323770491803279, 0.6012176560121766], [0.4344262295081967, 0.604261796042618], [0.4364754098360656, 0.6073059360730594], [0.4395491803278688, 0.6103500761035008], [0.4415983606557377, 0.6133942161339422], [0.44364754098360654, 0.6179604261796042], [0.4456967213114754, 0.6210045662100456], [0.44774590163934425, 0.6255707762557078], [0.44979508196721313, 0.6301369863013698], [0.45081967213114754, 0.634703196347032], [0.45286885245901637, 0.6407914764079148], [0.45389344262295084, 0.6453576864535768], [0.45491803278688525, 0.6484018264840182], [0.45594262295081966, 0.6575342465753424], [0.4569672131147541, 0.6605783866057838], [0.45799180327868855, 0.6681887366818874], [0.45901639344262296, 0.6742770167427702], [0.4600409836065574, 0.6788432267884322], [0.4610655737704918, 0.6834094368340944], [0.4610655737704918, 0.6879756468797564], [0.46209016393442626, 0.6940639269406392], [0.46209016393442626, 0.700152207001522], [0.46311475409836067, 0.7062404870624048], [0.4641393442622951, 0.7123287671232876], [0.4651639344262295, 0.7184170471841704], [0.4661885245901639, 0.7245053272450532], [0.4672131147540984, 0.730593607305936], [0.4682377049180328, 0.7351598173515982], [0.4692622950819672, 0.7397260273972602], [0.4702868852459016, 0.7427701674277016], [0.4713114754098361, 0.7488584474885844], [0.4733606557377049, 0.7534246575342466], [0.4733606557377049, 0.7579908675799086], [0.47438524590163933, 0.7640791476407914], [0.47438524590163933, 0.7701674277016742], [0.47540983606557374, 0.776255707762557], [0.47540983606557374, 0.7823439878234398], [0.47540983606557374, 0.7884322678843226], [0.47540983606557374, 0.7929984779299848], [0.47540983606557374, 0.7960426179604262], [0.47540983606557374, 0.7990867579908676], [0.47540983606557374, 0.802130898021309], [0.4764344262295082, 0.8051750380517504], [0.4764344262295082, 0.8082191780821918], [0.47848360655737704, 0.8127853881278538]], "object_id": 1, "start_time": 0, "end_time": 1712}, {"path": [[0.36270491803278687, 0.8082191780821918], [0.36270491803278687, 0.806697108066971], [0.35963114754098363, 0.8036529680365296], [0.35860655737704916, 0.8006088280060882], [0.35758196721311475, 0.7975646879756468], [0.35758196721311475, 0.7929984779299848], [0.35655737704918034, 0.786910197869102], [0.35758196721311475, 0.7808219178082192], [0.35758196721311475, 0.7732115677321156], [0.35758196721311475, 0.7686453576864536], [0.35860655737704916, 0.756468797564688], [0.35860655737704916, 0.7473363774733638], [0.35963114754098363, 0.7397260273972602], [0.35963114754098363, 0.7336377473363774], [0.35963114754098363, 0.726027397260274], [0.36065573770491804, 0.7184170471841704], [0.36168032786885246, 0.710806697108067], [0.36270491803278687, 0.7031963470319634], [0.3637295081967213, 0.69558599695586], [0.36577868852459017, 0.6864535768645358], [0.367827868852459, 0.680365296803653], [0.3698770491803279, 0.6727549467275494], [0.3729508196721312, 0.665144596651446], [0.3770491803278688, 0.6590563165905632], [0.3801229508196721, 0.6529680365296804], [0.38422131147540983, 0.6468797564687976], [0.38934426229508196, 0.6423135464231354], [0.3944672131147541, 0.639269406392694], [0.39959016393442626, 0.6362252663622526], [0.4057377049180328, 0.6331811263318112], [0.4088114754098361, 0.6316590563165906]], "object_id": 1, "start_time": 2275, "end_time": 2518}], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []], "sequence": "bear"}
3 changes: 2 additions & 1 deletion davisinteractive/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class DavisInteractiveSession:
evaluate per sample.
report_save_dir: String. Path to the directory where the report will
be stored during the evaluation. By default is the current working
directory.
directory. A temporal file will be storing snapshots of the results
on this same directory with a suffix `.tmp`.
progbar: Boolean. Wether to show a progbar to show the evolution
of the evaluation. If `True`, `tqdm` Python package will be
required.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
'numpy>=1.12.1', 'scikit-learn>=0.18', 'scikit-image>=0.13.1',
'networkx>=2.0', 'scipy>=1.0.0', 'pandas>=0.21.1', 'absl-py>=0.1.13',
'Pillow>=4.1.1', 'mock;python_version<"3.0"',
'pathlib;python_version<"3.5"', 'six>=1.10.0'
'pathlib2;python_version<"3.5"', 'six>=1.10.0'
])

0 comments on commit 0d1c01b

Please sign in to comment.