Skip to content

Commit

Permalink
Fix tensorflow installation (cvat-ai#1129)
Browse files Browse the repository at this point in the history
* Make tf dependency optional

* Reduce opencv dependency

* Import tf eagerly as it is a plugin

* Do not install TF with Datumaro
  • Loading branch information
zhiltsov-max committed Feb 7, 2020
1 parent 2848f1d commit 29d65b0
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ COPY cvat-core/ ${HOME}/cvat-core
COPY tests ${HOME}/tests
COPY datumaro/ ${HOME}/datumaro

RUN sed -r "s/^(.*)#.*$/\1/g" ${HOME}/datumaro/requirements.txt | xargs -n 1 -L 1 python3 -m pip install --no-cache-dir
RUN python3 -m pip install --no-cache-dir -r ${HOME}/datumaro/requirements.txt

# Binary option is necessary to correctly apply the patch on Windows platform.
# https://unix.stackexchange.com/questions/239364/how-to-fix-hunk-1-failed-at-1-different-line-endings-message
Expand Down
7 changes: 3 additions & 4 deletions datumaro/datumaro/plugins/openvino_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

# pylint: disable=exec-used

import cv2
import numpy as np
import os
import os.path as osp
import numpy as np
import subprocess
import platform
import subprocess

from openvino.inference_engine import IENetwork, IEPlugin

Expand Down Expand Up @@ -141,8 +142,6 @@ def _load_executable_net(self, batch_size=1):
self._net = plugin.load(network=network, num_requests=1)

def infer(self, inputs):
import cv2

assert len(inputs.shape) == 4, \
"Expected an input image in (N, H, W, C) format, got %s" % \
(inputs.shape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from datumaro.util.tf_util import import_tf as _import_tf

from .format import DetectionApiPath
tf = _import_tf()


# we need it to filter out non-ASCII characters, otherwise training will crash
Expand All @@ -25,8 +26,6 @@ def _make_printable(s):
return ''.join(filter(lambda x: x in _printable, s))

def _make_tf_example(item, get_label_id, get_label, save_images=False):
tf = _import_tf()

def int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))

Expand Down Expand Up @@ -118,8 +117,6 @@ def __init__(self, save_images=False):
self._save_images = save_images

def __call__(self, extractor, save_dir):
tf = _import_tf()

os.makedirs(save_dir, exist_ok=True)

subsets = extractor.subsets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from datumaro.util.tf_util import import_tf as _import_tf

from .format import DetectionApiPath
tf = _import_tf()


def clamp(value, _min, _max):
Expand Down Expand Up @@ -92,8 +93,6 @@ def _parse_labelmap(cls, text):

@classmethod
def _parse_tfrecord_file(cls, filepath, subset_name, images_dir):
tf = _import_tf()

dataset = tf.data.TFRecordDataset(filepath)
features = {
'image/filename': tf.io.FixedLenFeature([], tf.string),
Expand Down
1 change: 0 additions & 1 deletion datumaro/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ pycocotools>=2.0.0
PyYAML>=5.1.1
scikit-image>=0.15.0
tensorboardX>=1.8
tensorflow==1.13.1
5 changes: 4 additions & 1 deletion datumaro/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ def find_version(file_path=None):
'pycocotools',
'scikit-image',
'tensorboardX',
'tensorflow',
],
extras_require={
'tf': ['tensorflow'],
'tf-gpu': ['tensorflow-gpu'],
},
entry_points={
'console_scripts': [
'datum=datumaro.cli.__main__:main',
Expand Down
3 changes: 2 additions & 1 deletion datumaro/tests/test_RISE.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections import namedtuple
import cv2
import numpy as np

from unittest import TestCase
Expand Down Expand Up @@ -129,6 +128,7 @@ def _process(self, image):
heatmaps_class_count = len(set([roi.label for roi in rois]))
self.assertEqual(heatmaps_class_count + len(rois), len(heatmaps))

# import cv2
# roi_image = image.copy()
# for i, roi in enumerate(rois):
# cv2.rectangle(roi_image, (roi.x, roi.y), (roi.x + roi.w, roi.y + roi.h), (32 * i) * 3)
Expand Down Expand Up @@ -203,6 +203,7 @@ def DISABLED_test_roi_nms():
label=cls, attributes={'score': cls_conf})
)

import cv2
image = np.zeros((100, 100, 3))
for i, det in enumerate(detections):
roi = ROI(det.attributes['score'], *det.get_bbox(), det.label)
Expand Down
13 changes: 5 additions & 8 deletions datumaro/tests/test_command_targets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cv2
import numpy as np
import os.path as osp

Expand All @@ -7,15 +6,15 @@
from datumaro.components.project import Project
from datumaro.util.command_targets import ProjectTarget, \
ImageTarget, SourceTarget
from datumaro.util.test_utils import current_function_name, TestDir
from datumaro.util.image import save_image
from datumaro.util.test_utils import TestDir


class CommandTargetsTest(TestCase):
def test_image_false_when_no_file(self):
path = '%s.jpg' % current_function_name()
target = ImageTarget()

status = target.test(path)
status = target.test('somepath.jpg')

self.assertFalse(status)

Expand All @@ -34,8 +33,7 @@ def test_image_false_when_false(self):
def test_image_true_when_true(self):
with TestDir() as test_dir:
path = osp.join(test_dir, 'test.jpg')
image = np.random.random_sample([10, 10, 3])
cv2.imwrite(path, image)
save_image(path, np.ones([10, 7, 3]))

target = ImageTarget()

Expand All @@ -44,10 +42,9 @@ def test_image_true_when_true(self):
self.assertTrue(status)

def test_project_false_when_no_file(self):
path = '%s.jpg' % current_function_name()
target = ProjectTarget()

status = target.test(path)
status = target.test('somepath.jpg')

self.assertFalse(status)

Expand Down
8 changes: 4 additions & 4 deletions datumaro/tests/test_voc_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import cv2
import numpy as np
import os
import os.path as osp
Expand Down Expand Up @@ -28,6 +27,7 @@
)
from datumaro.plugins.voc_format.importer import VocImporter
from datumaro.components.project import Project
from datumaro.util.image import save_image
from datumaro.util.test_utils import TestDir, compare_datasets


Expand Down Expand Up @@ -155,17 +155,17 @@ def generate_dummy_voc(path):
subset_name = 'train'
subset = subsets[subset_name]
for item in subset:
cv2.imwrite(osp.join(segm_dir, item + '.png'),
save_image(osp.join(segm_dir, item + '.png'),
np.tile(VOC.VocColormap[2][::-1], (5, 10, 1))
)
cv2.imwrite(osp.join(inst_dir, item + '.png'),
save_image(osp.join(inst_dir, item + '.png'),
np.tile(1, (5, 10, 1)))

# Test images
subset_name = 'test'
subset = subsets[subset_name]
for item in subset:
cv2.imwrite(osp.join(img_dir, item + '.jpg'),
save_image(osp.join(img_dir, item + '.jpg'),
np.ones([10, 20, 3]))

return subsets
Expand Down

0 comments on commit 29d65b0

Please sign in to comment.