Ejemplo n.º 1
0
    def test_forward1(self):
        x = np.random.randn(1, 3, 224, 224).astype('f')
        _model = chainer.links.VGG16Layers(None)
        _model.to_gpu()

        with chainer.using_config('train', False):
            with chainer.using_config('enable_backprop', False):
                out_layer_name = 'fc8'
                _y = _model.forward(x, [out_layer_name])[out_layer_name]

        model = VGG16()
        layers = _model.available_layers
        for l in layers:
            if "conv" in l or "fc" in l:
                m1 = getattr(model, l)
                m2 = getattr(_model, l)
                m1.W.data = m2.W.data
                m1.b.data = m2.b.data
                if "fc" in l:
                    m1.W.data = m1.W.data.T
        model.to_gpu()

        with dezero.test_mode():
            y = model(x)

        self.assertTrue(array_allclose(y.data, _y.data))
Ejemplo n.º 2
0
    def test_backward1(self):
        x = np.random.randn(2, 3, 224, 224).astype('f')
        _model = chainer.links.VGG16Layers(None)

        with chainer.using_config('train', False):
            out_layer_name = 'fc8'
            _y = _model.forward(x, [out_layer_name])[out_layer_name]
            _y.grad = np.ones_like(_y.data)
            _y.backward()

        model = VGG16()
        layers = _model.available_layers
        for l in layers:
            if "conv" in l or "fc" in l:
                m1 = getattr(model, l)
                m2 = getattr(_model, l)
                m1.W.data = m2.W.data
                m1.b.data = m2.b.data
                if "fc" in l:
                    m1.W.data = m1.W.data.T

        with dezero.test_mode():
            y = model(x)
            y.backward()

        layers = _model.available_layers
        for l in layers:
            if "conv" in l:
                m1 = getattr(model, l)
                m2 = getattr(_model, l)
                self.assertTrue(array_allclose(m1.W.data, m2.W.data))
                self.assertTrue(array_allclose(m1.b.data, m2.b.data))
            elif "fc" in l:
                m1 = getattr(model, l)
                m2 = getattr(_model, l)
                self.assertTrue(array_allclose(m1.W.data, m2.W.data.T))
                self.assertTrue(array_allclose(m1.b.data, m2.b.data))
Ejemplo n.º 3
0
if '__file__' in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import numpy as np
from PIL import Image
import dezero
from dezero.models import VGG16

url = 'https://github.com/oreilly-japan/deep-learning-from-scratch-3/raw/images/zebra.jpg'
img_path = dezero.utils.get_file(url)
img = Image.open(img_path)

x = VGG16.preprocess(img)
x = x[np.newaxis]

model = VGG16(pretrained=True)
with dezero.test_mode():
    y = model(x)
predict_id = np.argmax(y.data)

model.plot(x, to_file='vgg.pdf')
labels = dezero.datasets.ImageNet.labels()
print(labels[predict_id])
import numpy as np
from PIL import Image
import dezero
from dezero.models import VGG16
from dezero.dataset import preprocess_vgg
from dezero.datasets import get_imagenet_labels

url = 'https://github.com/oreilly-japan/deep-learning-from-scratch-3/raw/images/zebra.jpg'
img_path = dezero.utils.get_file(url)
img = Image.open(img_path)
x = preprocess_vgg(img)
x = x[np.newaxis]

model = VGG16(pretrained=True)
with dezero.test_mode():
    y = model(x)
predict_id = np.argmax(y.data)
model.plot(x, to_file='vgg.pdf')
labels = get_imagenet_labels()
print(labels[predict_id])
Ejemplo n.º 5
0
 def test_forward2(self):
     x = np.random.randn(1, 3, 224, 224).astype('f')
     model = VGG16()
     y = model(x)
     self.assertTrue(y.dtype == np.float32)
Ejemplo n.º 6
0
"""
Simple implementation of Grad-CAM (https://arxiv.org/pdf/1610.02391.pdf)
"""
import numpy as np
from PIL import Image
import cv2
import dezero
import dezero.functions as F
from dezero.models import VGG16

url = 'https://github.com/oreilly-japan/deep-learning-from-scratch-3/raw/images/zebra.jpg'
img_path = dezero.utils.get_file(url)
img = Image.open(img_path)
img_size = img.size

model = VGG16(pretrained=True)
x = VGG16.preprocess(img)[np.newaxis]  # preprocess for VGG
y = model(x)
last_conv_output = model.conv5_3.outputs[0]()
predict_id = np.argmax(y.data)
predict_output = y[0, predict_id]

predict_output.backward(retain_grad=True)
grads = last_conv_output.grad
pooled_grads = F.average(grads, axis=(0, 2, 3))

heatmap = last_conv_output.data[0]
for c in range(heatmap.shape[0]):
    heatmap[c] *= pooled_grads[c].data

heatmap = np.mean(heatmap, axis=0)
Ejemplo n.º 7
0
            'conv3_2': c3_2,
            'conv3_3': c3_3,
            'conv4_1': c4_1,
            'conv5_1': c5_1,
            'conv5_2': c5_2,
            'conv5_3': c5_3
        }


# Setup for content & style image
content_path = dezero.utils.get_file(content_url)
style_path = dezero.utils.get_file(style_url)
content_img = Image.open(content_path)
content_size = content_img.size
style_img = Image.open(style_path)
content_img = VGG16.preprocess(
    content_img, size=model_input_size)[np.newaxis]  # preprocess for VGG
style_img = VGG16.preprocess(style_img, size=model_input_size)[np.newaxis]
content_img, style_img = Variable(content_img), Variable(style_img)

model = VGG16(pretrained=True)
#gen_data = np.random.uniform(-20, 20, (1, 3, img_resize[0], img_resize[1])).astype(np.float32)
gen_data = content_img.data.copy()
gen_img = dezero.Parameter(gen_data)
gen_model = dezero.models.Model()
gen_model.param = gen_img
optimizer = dezero.optimizers.AdaGrad(lr=lr).setup(gen_model)

if use_gpu:
    model.to_gpu()
    gen_img.to_gpu()
    content_img.to_gpu()