mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-09 11:25:25 +08:00
12 KiB
12 KiB
In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.convolutional import UpSampling3D
from keras import backend as KUsing TensorFlow backend.
In [2]:
def format_decimal(arr, places=6):
return [round(x * 10**places) / 10**places for x in arr]In [6]:
data_in_shape = (2, 2, 2, 3)
L = UpSampling3D(size=(2, 2, 2), dim_ordering='tf')
layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)
# set weights to random (use seed for reproducibility)
np.random.seed(260)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))in shape: (2, 2, 2, 3) in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858] out shape: (4, 4, 4, 3) out: [-0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.806777, -0.564841, -0.481331, -0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, -0.178541, 0.689453, -0.028873, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, 0.776897, -0.700858, -0.802179, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858, -0.080606, -0.850593, -0.795971, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858, 0.860487, -0.90685, 0.89858]
In [7]:
data_in_shape = (2, 2, 2, 3)
L = UpSampling3D(size=(2, 2, 2), dim_ordering='th')
layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)
# set weights to random (use seed for reproducibility)
np.random.seed(260)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))in shape: (2, 2, 2, 3) in: [-0.806777, -0.564841, -0.481331, 0.559626, 0.274958, -0.659222, -0.178541, 0.689453, -0.028873, 0.053859, -0.446394, -0.53406, 0.776897, -0.700858, -0.802179, -0.616515, 0.718677, 0.303042, -0.080606, -0.850593, -0.795971, 0.860487, -0.90685, 0.89858] out shape: (2, 4, 4, 6) out: [-0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, -0.806777, -0.806777, -0.564841, -0.564841, -0.481331, -0.481331, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, 0.559626, 0.559626, 0.274958, 0.274958, -0.659222, -0.659222, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, -0.178541, -0.178541, 0.689453, 0.689453, -0.028873, -0.028873, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.053859, 0.053859, -0.446394, -0.446394, -0.53406, -0.53406, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, 0.776897, 0.776897, -0.700858, -0.700858, -0.802179, -0.802179, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.616515, -0.616515, 0.718677, 0.718677, 0.303042, 0.303042, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, -0.080606, -0.080606, -0.850593, -0.850593, -0.795971, -0.795971, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858, 0.860487, 0.860487, -0.90685, -0.90685, 0.89858, 0.89858]
In [8]:
data_in_shape = (2, 1, 3, 2)
L = UpSampling3D(size=(1, 3, 2), dim_ordering='tf')
layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)
# set weights to random (use seed for reproducibility)
np.random.seed(252)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))in shape: (2, 1, 3, 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] out shape: (2, 3, 6, 2) out: [-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.989173, -0.133618, -0.989173, -0.133618, -0.505338, 0.023259, -0.505338, 0.023259, 0.503982, -0.303769, 0.503982, -0.303769, -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.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022, -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.436321, 0.793911, -0.436321, 0.793911, 0.416102, 0.806405, 0.416102, 0.806405, -0.098342, -0.738022, -0.098342, -0.738022]
In [10]:
data_in_shape = (2, 1, 3, 3)
L = UpSampling3D(size=(2, 1, 2), dim_ordering='th')
layer_0 = Input(shape=data_in_shape)
layer_1 = L(layer_0)
model = Model(input=layer_0, output=layer_1)
# set weights to random (use seed for reproducibility)
np.random.seed(253)
data_in = 2 * np.random.random(data_in_shape) - 1
print('')
print('in shape:', data_in_shape)
print('in:', format_decimal(data_in.ravel().tolist()))
result = model.predict(np.array([data_in]))
print('out shape:', result[0].shape)
print('out:', format_decimal(result[0].ravel().tolist()))in shape: (2, 1, 3, 3) 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] out shape: (2, 2, 3, 6) out: [-0.47588, -0.47588, 0.366985, 0.366985, 0.040173, 0.040173, 0.015578, 0.015578, -0.906159, -0.906159, 0.241982, 0.241982, -0.771299, -0.771299, -0.443554, -0.443554, -0.56404, -0.56404, -0.47588, -0.47588, 0.366985, 0.366985, 0.040173, 0.040173, 0.015578, 0.015578, -0.906159, -0.906159, 0.241982, 0.241982, -0.771299, -0.771299, -0.443554, -0.443554, -0.56404, -0.56404, -0.17751, -0.17751, 0.541277, 0.541277, -0.233327, -0.233327, 0.024369, 0.024369, 0.858275, 0.858275, 0.496191, 0.496191, 0.980574, 0.980574, -0.59522, -0.59522, 0.480899, 0.480899, -0.17751, -0.17751, 0.541277, 0.541277, -0.233327, -0.233327, 0.024369, 0.024369, 0.858275, 0.858275, 0.496191, 0.496191, 0.980574, 0.980574, -0.59522, -0.59522, 0.480899, 0.480899]
In [ ]: