Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
pass test_predict
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed Jun 14, 2017
1 parent f479f21 commit e23e5f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
10 changes: 7 additions & 3 deletions chainercv/links/model/vgg/vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,22 @@
from chainer.initializers import normal
import chainer.links as L

from chainercv.transforms import center_crop
from chainercv.transforms import resize
from chainercv.transforms import ten_crop


# RGB order
_imagenet_mean = np.array(
[[[123.68], [116.779], [103.939]]], dtype=np.float32)
[123.68, 116.779, 103.939], dtype=np.float32)[:, np.newaxis, np.newaxis]


class VGG16Layers(chainer.Chain):

def __init__(self, pretrained_model='auto', feature='prob',
initialW=None, initial_bias=None,
mean=_imagenet_mean):
self.mean = mean
if pretrained_model:
# As a sampling process is time-consuming,
# we employ a zero initializer for faster computation.
Expand Down Expand Up @@ -158,15 +160,17 @@ def predict(self, imgs, do_ten_crop=True):
imgs = [self._prepare(img) for img in imgs]
if do_ten_crop:
imgs = [ten_crop(img, (224, 224)) for img in imgs]
else:
imgs = [center_crop(img, (224, 224)) for img in imgs]
imgs = self.xp.asarray(imgs).reshape(-1, 3, 224, 224)

with chainer.function.no_backprop_mode():
imgs = chainer.Variable(imgs)
y = self(imgs).data

if do_ten_crop:
n = y.data.shape[0] // 10
y_shape = y.data.shape[1:]
n = y.shape[0] // 10
y_shape = y.shape[1:]
y = y.reshape((n, 10) + y_shape)
y = self.xp.sum(y, axis=1) / 10
return cuda.to_cpu(y)
Expand Down
10 changes: 4 additions & 6 deletions tests/links_tests/model_tests/vgg_tests/test_vgg16.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def test_call_gpu(self):


@testing.parameterize(
{'feature': 'prob', 'shape': (2, 1000)},
{'feature': 'conv5_3', 'shape': (2, 512, 7, 7)}
{'feature': 'prob', 'shape': (2, 1000), 'do_ten_crop': False},
{'feature': 'prob', 'shape': (2, 1000), 'do_ten_crop': True},
{'feature': 'conv5_3', 'shape': (2, 512, 14, 14), 'do_ten_crop': False}
)
class TestVGG16LayersPredict(unittest.TestCase):

Expand All @@ -50,10 +51,7 @@ def setUp(self):
def check_predict(self):
x1 = np.random.uniform(0, 255, (3, 320, 240)).astype(np.float32)
x2 = np.random.uniform(0, 255, (3, 320, 240)).astype(np.float32)
out = self.link.predict([x1, x2], oversample=False)
self.assertEqual(out.shape, self.shape)
self.assertEqual(out.dtype, np.float32)
result = self.link.predict([x1, x2], oversample=True)
out = self.link.predict([x1, x2], do_ten_crop=self.do_ten_crop)
self.assertEqual(out.shape, self.shape)
self.assertEqual(out.dtype, np.float32)

Expand Down

0 comments on commit e23e5f5

Please sign in to comment.