In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import UpSampling2D
from keras import backend as K
import json
from collections import OrderedDict


Using TensorFlow backend.

In [2]:
def format_decimal(arr, places=6):
    return [round(x * 10**places) / 10**places for x in arr]

In [3]:
DATA = OrderedDict()

UpSampling2D

[convolutional.UpSampling2D.0] size 2x2 upsampling on 3x3x3 input, data_format='channels_last'


In [4]:
data_in_shape = (3, 3, 3)
L = UpSampling2D(size=(2, 2), data_format='channels_last')

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(250)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.UpSampling2D.0'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (3, 3, 3)
in: [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094]
out shape: (6, 6, 3)
out: [-0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, -0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094]

[convolutional.UpSampling2D.1] size 2x2 upsampling on 3x3x3 input, data_format='channels_first'


In [5]:
data_in_shape = (3, 3, 3)
L = UpSampling2D(size=(2, 2), data_format='channels_first')

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(251)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.UpSampling2D.1'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (3, 3, 3)
in: [0.275222, -0.793967, -0.468107, -0.841484, -0.295362, 0.78175, 0.068787, -0.261747, -0.625733, -0.042907, 0.861141, 0.85267, 0.956439, 0.717838, -0.99869, -0.963008, 0.013277, -0.180306, 0.832137, -0.385252, -0.524308, 0.659706, -0.905127, 0.526292, 0.832569, 0.084455, 0.23838]
out shape: (3, 6, 6)
out: [0.275222, 0.275222, -0.793967, -0.793967, -0.468107, -0.468107, 0.275222, 0.275222, -0.793967, -0.793967, -0.468107, -0.468107, -0.841484, -0.841484, -0.295362, -0.295362, 0.78175, 0.78175, -0.841484, -0.841484, -0.295362, -0.295362, 0.78175, 0.78175, 0.068787, 0.068787, -0.261747, -0.261747, -0.625733, -0.625733, 0.068787, 0.068787, -0.261747, -0.261747, -0.625733, -0.625733, -0.042907, -0.042907, 0.861141, 0.861141, 0.85267, 0.85267, -0.042907, -0.042907, 0.861141, 0.861141, 0.85267, 0.85267, 0.956439, 0.956439, 0.717838, 0.717838, -0.99869, -0.99869, 0.956439, 0.956439, 0.717838, 0.717838, -0.99869, -0.99869, -0.963008, -0.963008, 0.013277, 0.013277, -0.180306, -0.180306, -0.963008, -0.963008, 0.013277, 0.013277, -0.180306, -0.180306, 0.832137, 0.832137, -0.385252, -0.385252, -0.524308, -0.524308, 0.832137, 0.832137, -0.385252, -0.385252, -0.524308, -0.524308, 0.659706, 0.659706, -0.905127, -0.905127, 0.526292, 0.526292, 0.659706, 0.659706, -0.905127, -0.905127, 0.526292, 0.526292, 0.832569, 0.832569, 0.084455, 0.084455, 0.23838, 0.23838, 0.832569, 0.832569, 0.084455, 0.084455, 0.23838, 0.23838]

[convolutional.UpSampling2D.2] size 3x2 upsampling on 4x2x2 input, data_format='channels_last'


In [6]:
data_in_shape = (4, 2, 2)
L = UpSampling2D(size=(3, 2), data_format='channels_last')

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(252)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.UpSampling2D.2'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (4, 2, 2)
in: [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022, -0.982676, 0.805073, 0.741244, -0.941634]
out shape: (12, 4, 2)
out: [-0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634]

[convolutional.UpSampling2D.3] size 1x3 upsampling on 4x3x2 input, data_format='channels_first'


In [7]:
data_in_shape = (4, 3, 2)
L = UpSampling2D(size=(1, 3), data_format='channels_first')

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(253)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.UpSampling2D.3'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (4, 3, 2)
in: [-0.47588, 0.366985, 0.040173, 0.015578, -0.906159, 0.241982, -0.771299, -0.443554, -0.56404, -0.17751, 0.541277, -0.233327, 0.024369, 0.858275, 0.496191, 0.980574, -0.59522, 0.480899, 0.392553, -0.191718, 0.055121, 0.289836, -0.498339, 0.800408]
out shape: (4, 3, 6)
out: [-0.47588, -0.47588, -0.47588, 0.366985, 0.366985, 0.366985, 0.040173, 0.040173, 0.040173, 0.015578, 0.015578, 0.015578, -0.906159, -0.906159, -0.906159, 0.241982, 0.241982, 0.241982, -0.771299, -0.771299, -0.771299, -0.443554, -0.443554, -0.443554, -0.56404, -0.56404, -0.56404, -0.17751, -0.17751, -0.17751, 0.541277, 0.541277, 0.541277, -0.233327, -0.233327, -0.233327, 0.024369, 0.024369, 0.024369, 0.858275, 0.858275, 0.858275, 0.496191, 0.496191, 0.496191, 0.980574, 0.980574, 0.980574, -0.59522, -0.59522, -0.59522, 0.480899, 0.480899, 0.480899, 0.392553, 0.392553, 0.392553, -0.191718, -0.191718, -0.191718, 0.055121, 0.055121, 0.055121, 0.289836, 0.289836, 0.289836, -0.498339, -0.498339, -0.498339, 0.800408, 0.800408, 0.800408]

[convolutional.UpSampling2D.4] size 2 upsampling on 1x3x2 input, data_format='channels_last'


In [8]:
data_in_shape = (1, 3, 2)
L = UpSampling2D(size=2, data_format='channels_last')

layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

# set weights to random (use seed for reproducibility)
np.random.seed(254)
data_in = 2 * np.random.random(data_in_shape) - 1
result = model.predict(np.array([data_in]))
data_out_shape = result[0].shape
data_in_formatted = format_decimal(data_in.ravel().tolist())
data_out_formatted = format_decimal(result[0].ravel().tolist())
print('')
print('in shape:', data_in_shape)
print('in:', data_in_formatted)
print('out shape:', data_out_shape)
print('out:', data_out_formatted)

DATA['convolutional.UpSampling2D.4'] = {
    'input': {'data': data_in_formatted, 'shape': data_in_shape},
    'expected': {'data': data_out_formatted, 'shape': data_out_shape}
}


in shape: (1, 3, 2)
in: [0.024124, 0.280236, -0.680013, -0.042458, -0.164273, 0.358409]
out shape: (2, 6, 2)
out: [0.024124, 0.280236, 0.024124, 0.280236, -0.680013, -0.042458, -0.680013, -0.042458, -0.164273, 0.358409, -0.164273, 0.358409, 0.024124, 0.280236, 0.024124, 0.280236, -0.680013, -0.042458, -0.680013, -0.042458, -0.164273, 0.358409, -0.164273, 0.358409]

export for Keras.js tests


In [9]:
print(json.dumps(DATA))


{"convolutional.UpSampling2D.0": {"input": {"data": [-0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094], "shape": [3, 3, 3]}, "expected": {"data": [-0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, -0.570441, -0.454673, -0.285321, -0.570441, -0.454673, -0.285321, 0.237249, 0.282682, 0.428035, 0.237249, 0.282682, 0.428035, 0.160547, -0.332203, 0.546391, 0.160547, -0.332203, 0.546391, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, 0.272735, 0.010827, -0.763164, 0.272735, 0.010827, -0.763164, -0.442696, 0.381948, -0.676994, -0.442696, 0.381948, -0.676994, 0.753553, -0.031788, 0.915329, 0.753553, -0.031788, 0.915329, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094, -0.738844, 0.269075, 0.434091, -0.738844, 0.269075, 0.434091, 0.991585, -0.944288, 0.258834, 0.991585, -0.944288, 0.258834, 0.162138, 0.565201, -0.492094, 0.162138, 0.565201, -0.492094], "shape": [6, 6, 3]}}, "convolutional.UpSampling2D.1": {"input": {"data": [0.275222, -0.793967, -0.468107, -0.841484, -0.295362, 0.78175, 0.068787, -0.261747, -0.625733, -0.042907, 0.861141, 0.85267, 0.956439, 0.717838, -0.99869, -0.963008, 0.013277, -0.180306, 0.832137, -0.385252, -0.524308, 0.659706, -0.905127, 0.526292, 0.832569, 0.084455, 0.23838], "shape": [3, 3, 3]}, "expected": {"data": [0.275222, 0.275222, -0.793967, -0.793967, -0.468107, -0.468107, 0.275222, 0.275222, -0.793967, -0.793967, -0.468107, -0.468107, -0.841484, -0.841484, -0.295362, -0.295362, 0.78175, 0.78175, -0.841484, -0.841484, -0.295362, -0.295362, 0.78175, 0.78175, 0.068787, 0.068787, -0.261747, -0.261747, -0.625733, -0.625733, 0.068787, 0.068787, -0.261747, -0.261747, -0.625733, -0.625733, -0.042907, -0.042907, 0.861141, 0.861141, 0.85267, 0.85267, -0.042907, -0.042907, 0.861141, 0.861141, 0.85267, 0.85267, 0.956439, 0.956439, 0.717838, 0.717838, -0.99869, -0.99869, 0.956439, 0.956439, 0.717838, 0.717838, -0.99869, -0.99869, -0.963008, -0.963008, 0.013277, 0.013277, -0.180306, -0.180306, -0.963008, -0.963008, 0.013277, 0.013277, -0.180306, -0.180306, 0.832137, 0.832137, -0.385252, -0.385252, -0.524308, -0.524308, 0.832137, 0.832137, -0.385252, -0.385252, -0.524308, -0.524308, 0.659706, 0.659706, -0.905127, -0.905127, 0.526292, 0.526292, 0.659706, 0.659706, -0.905127, -0.905127, 0.526292, 0.526292, 0.832569, 0.832569, 0.084455, 0.084455, 0.23838, 0.23838, 0.832569, 0.832569, 0.084455, 0.084455, 0.23838, 0.23838], "shape": [3, 6, 6]}}, "convolutional.UpSampling2D.2": {"input": {"data": [-0.989173, -0.133618, -0.505338, 0.023259, 0.503982, -0.303769, -0.436321, 0.793911, 0.416102, 0.806405, -0.098342, -0.738022, -0.982676, 0.805073, 0.741244, -0.941634], "shape": [4, 2, 2]}, "expected": {"data": [-0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, -0.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.503982, -0.303769, 0.503982, -0.303769, -0.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634, -0.982676, 0.805073, -0.982676, 0.805073, 0.741244, -0.941634, 0.741244, -0.941634], "shape": [12, 4, 2]}}, "convolutional.UpSampling2D.3": {"input": {"data": [-0.47588, 0.366985, 0.040173, 0.015578, -0.906159, 0.241982, -0.771299, -0.443554, -0.56404, -0.17751, 0.541277, -0.233327, 0.024369, 0.858275, 0.496191, 0.980574, -0.59522, 0.480899, 0.392553, -0.191718, 0.055121, 0.289836, -0.498339, 0.800408], "shape": [4, 3, 2]}, "expected": {"data": [-0.47588, -0.47588, -0.47588, 0.366985, 0.366985, 0.366985, 0.040173, 0.040173, 0.040173, 0.015578, 0.015578, 0.015578, -0.906159, -0.906159, -0.906159, 0.241982, 0.241982, 0.241982, -0.771299, -0.771299, -0.771299, -0.443554, -0.443554, -0.443554, -0.56404, -0.56404, -0.56404, -0.17751, -0.17751, -0.17751, 0.541277, 0.541277, 0.541277, -0.233327, -0.233327, -0.233327, 0.024369, 0.024369, 0.024369, 0.858275, 0.858275, 0.858275, 0.496191, 0.496191, 0.496191, 0.980574, 0.980574, 0.980574, -0.59522, -0.59522, -0.59522, 0.480899, 0.480899, 0.480899, 0.392553, 0.392553, 0.392553, -0.191718, -0.191718, -0.191718, 0.055121, 0.055121, 0.055121, 0.289836, 0.289836, 0.289836, -0.498339, -0.498339, -0.498339, 0.800408, 0.800408, 0.800408], "shape": [4, 3, 6]}}, "convolutional.UpSampling2D.4": {"input": {"data": [0.024124, 0.280236, -0.680013, -0.042458, -0.164273, 0.358409], "shape": [1, 3, 2]}, "expected": {"data": [0.024124, 0.280236, 0.024124, 0.280236, -0.680013, -0.042458, -0.680013, -0.042458, -0.164273, 0.358409, -0.164273, 0.358409, 0.024124, 0.280236, 0.024124, 0.280236, -0.680013, -0.042458, -0.680013, -0.042458, -0.164273, 0.358409, -0.164273, 0.358409], "shape": [2, 6, 2]}}}

In [ ]: