implement UpSampling1D/2D/3D layers, with tests

This commit is contained in:
Leon Chen
2016-08-26 22:29:22 -04:00
parent 032e290607
commit 9b07e511bf
15 changed files with 1185 additions and 6 deletions
+6
View File
@@ -49,6 +49,12 @@
<script src="/test/convolutional/Convolution1D.js"></script>
<script src="/test/convolutional/data_Convolution2D.js"></script>
<script src="/test/convolutional/Convolution2D.js"></script>
<script src="/test/convolutional/data_UpSampling1D.js"></script>
<script src="/test/convolutional/UpSampling1D.js"></script>
<script src="/test/convolutional/data_UpSampling2D.js"></script>
<script src="/test/convolutional/UpSampling2D.js"></script>
<script src="/test/convolutional/data_UpSampling3D.js"></script>
<script src="/test/convolutional/UpSampling3D.js"></script>
<script>
// mocha.checkLeaks();
+5 -5
View File
@@ -51,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"metadata": {
"collapsed": false
},
@@ -109,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 16,
"metadata": {
"collapsed": false
},
@@ -163,7 +163,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 17,
"metadata": {
"collapsed": false
},
@@ -216,12 +216,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.Convolution1D.4] 2 length 7 filters on 8x3 input, activation='tanh', border_mode='same', subsample_length=1, bias=True**"
"**[convolutional.Convolution1D.3] 2 length 7 filters on 8x3 input, activation='tanh', border_mode='same', subsample_length=1, bias=True**"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 18,
"metadata": {
"collapsed": false
},
+158
View File
@@ -0,0 +1,158 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import UpSampling1D\n",
"from keras import backend as K"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def format_decimal(arr, places=6):\n",
" return [round(x * 10**places) / 10**places for x in arr]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### UpSampling1D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling1D.0] length 2 upsampling on 3x5 input**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (3, 5)\n",
"in: [0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778]\n",
"out shape: (6, 5)\n",
"out: [0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778]\n"
]
}
],
"source": [
"data_in_shape = (3, 5)\n",
"L = UpSampling1D(length=2)\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(230)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling1D.0] length 3 upsampling on 4x4 input**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (4, 4)\n",
"in: [0.562988, 0.168418, -0.14658, -0.369311, 0.653777, 0.806859, -0.922124, 0.830445, -0.878989, -0.638546, -0.855401, -0.082476, 0.416718, -0.03353, -0.949107, -0.866195]\n",
"out shape: (12, 4)\n",
"out: [0.562988, 0.168418, -0.14658, -0.369311, 0.562988, 0.168418, -0.14658, -0.369311, 0.562988, 0.168418, -0.14658, -0.369311, 0.653777, 0.806859, -0.922124, 0.830445, 0.653777, 0.806859, -0.922124, 0.830445, 0.653777, 0.806859, -0.922124, 0.830445, -0.878989, -0.638546, -0.855401, -0.082476, -0.878989, -0.638546, -0.855401, -0.082476, -0.878989, -0.638546, -0.855401, -0.082476, 0.416718, -0.03353, -0.949107, -0.866195, 0.416718, -0.03353, -0.949107, -0.866195, 0.416718, -0.03353, -0.949107, -0.866195]\n"
]
}
],
"source": [
"data_in_shape = (4, 4)\n",
"L = UpSampling1D(length=3)\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(231)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
+256
View File
@@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import UpSampling2D\n",
"from keras import backend as K"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def format_decimal(arr, places=6):\n",
" return [round(x * 10**places) / 10**places for x in arr]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### UpSampling2D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.0] size 2x2 upsampling on 3x3x3 input, dim_ordering='tf'**"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (3, 3, 3)\n",
"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]\n",
"out shape: (6, 6, 3)\n",
"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]\n"
]
}
],
"source": [
"data_in_shape = (3, 3, 3)\n",
"L = UpSampling2D(size=(2, 2), dim_ordering='tf')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(250)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.0] size 2x2 upsampling on 3x3x3 input, dim_ordering='th'**"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (3, 3, 3)\n",
"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]\n",
"out shape: (3, 6, 6)\n",
"out: [-0.570441, -0.570441, -0.454673, -0.454673, -0.285321, -0.285321, -0.570441, -0.570441, -0.454673, -0.454673, -0.285321, -0.285321, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094]\n"
]
}
],
"source": [
"data_in_shape = (3, 3, 3)\n",
"L = UpSampling2D(size=(2, 2), dim_ordering='th')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(250)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.2] size 3x2 upsampling on 4x2x2 input, dim_ordering='tf'**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (4, 2, 2)\n",
"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]\n",
"out shape: (12, 4, 2)\n",
"out: [0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008]\n"
]
}
],
"source": [
"data_in_shape = (4, 2, 2)\n",
"L = UpSampling2D(size=(3, 2), dim_ordering='tf')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(251)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.3] size 1x3 upsampling on 4x3x2 input, dim_ordering='th'**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (4, 3, 2)\n",
"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, -0.253526, -0.136544, -0.295772, 0.207565, -0.517246, -0.686963, -0.176235, -0.354111]\n",
"out shape: (4, 3, 6)\n",
"out: [-0.989173, -0.989173, -0.989173, -0.133618, -0.133618, -0.133618, -0.505338, -0.505338, -0.505338, 0.023259, 0.023259, 0.023259, 0.503982, 0.503982, 0.503982, -0.303769, -0.303769, -0.303769, -0.436321, -0.436321, -0.436321, 0.793911, 0.793911, 0.793911, 0.416102, 0.416102, 0.416102, 0.806405, 0.806405, 0.806405, -0.098342, -0.098342, -0.098342, -0.738022, -0.738022, -0.738022, -0.982676, -0.982676, -0.982676, 0.805073, 0.805073, 0.805073, 0.741244, 0.741244, 0.741244, -0.941634, -0.941634, -0.941634, -0.253526, -0.253526, -0.253526, -0.136544, -0.136544, -0.136544, -0.295772, -0.295772, -0.295772, 0.207565, 0.207565, 0.207565, -0.517246, -0.517246, -0.517246, -0.686963, -0.686963, -0.686963, -0.176235, -0.176235, -0.176235, -0.354111, -0.354111, -0.354111]\n"
]
}
],
"source": [
"data_in_shape = (4, 3, 2)\n",
"L = UpSampling2D(size=(1, 3), dim_ordering='th')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(252)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
+256
View File
@@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
]
}
],
"source": [
"import numpy as np\n",
"from keras.models import Model\n",
"from keras.layers import Input\n",
"from keras.layers.convolutional import UpSampling3D\n",
"from keras import backend as K"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def format_decimal(arr, places=6):\n",
" return [round(x * 10**places) / 10**places for x in arr]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### UpSampling3D"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dim_ordering='tf'**"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (2, 2, 2, 3)\n",
"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]\n",
"out shape: (4, 4, 4, 3)\n",
"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]\n"
]
}
],
"source": [
"data_in_shape = (2, 2, 2, 3)\n",
"L = UpSampling3D(size=(2, 2, 2), dim_ordering='tf')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(260)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dim_ordering='th'**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (2, 2, 2, 3)\n",
"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]\n",
"out shape: (2, 4, 4, 6)\n",
"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]\n"
]
}
],
"source": [
"data_in_shape = (2, 2, 2, 3)\n",
"L = UpSampling3D(size=(2, 2, 2), dim_ordering='th')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(260)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.2] size 1x3x2 upsampling on 2x1x3x2 input, dim_ordering='tf'**"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (2, 1, 3, 2)\n",
"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]\n",
"out shape: (2, 3, 6, 2)\n",
"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]\n"
]
}
],
"source": [
"data_in_shape = (2, 1, 3, 2)\n",
"L = UpSampling3D(size=(1, 3, 2), dim_ordering='tf')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(252)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[convolutional.UpSampling2D.2] size 2x1x2 upsampling on 2x1x3x3 input, dim_ordering='th'**"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"in shape: (2, 1, 3, 3)\n",
"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]\n",
"out shape: (2, 2, 3, 6)\n",
"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]\n"
]
}
],
"source": [
"data_in_shape = (2, 1, 3, 3)\n",
"L = UpSampling3D(size=(2, 1, 2), dim_ordering='th')\n",
"\n",
"layer_0 = Input(shape=data_in_shape)\n",
"layer_1 = L(layer_0)\n",
"model = Model(input=layer_0, output=layer_1)\n",
"\n",
"# set weights to random (use seed for reproducibility)\n",
"np.random.seed(253)\n",
"data_in = 2 * np.random.random(data_in_shape) - 1\n",
"print('')\n",
"print('in shape:', data_in_shape)\n",
"print('in:', format_decimal(data_in.ravel().tolist()))\n",
"result = model.predict(np.array([data_in]))\n",
"print('out shape:', result[0].shape)\n",
"print('out:', format_decimal(result[0].ravel().tolist()))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
+36
View File
@@ -0,0 +1,36 @@
import Layer from '../../engine/Layer'
import Tensor from '../../Tensor'
import ops from 'ndarray-ops'
/**
* UpSampling1D layer class
*/
export default class UpSampling1D extends Layer {
/**
* Creates a UpSampling1D activation layer
* @param {number} length - upsampling factor
*/
constructor (length = 2, attrs = {}) {
super(attrs)
this.length = length
}
/**
* Method for layer computational logic
* @param {Tensor} x
* @returns {Tensor} x
*/
call = x => {
const inputShape = x.tensor.shape
const outputShape = [inputShape[0] * this.length, inputShape[1]]
let y = new Tensor([], outputShape)
for (let i = 0; i < this.length; i++) {
ops.assign(
y.tensor.lo(i, 0).step(this.length, 1),
x.tensor
)
}
x.tensor = y.tensor
return x
}
}
+58
View File
@@ -0,0 +1,58 @@
import Layer from '../../engine/Layer'
import Tensor from '../../Tensor'
import ops from 'ndarray-ops'
/**
* UpSampling2D layer class
*/
export default class UpSampling2D extends Layer {
/**
* Creates a UpSampling2D activation layer
* @param {number} size - upsampling factor
*/
constructor (size = [2, 2], attrs = {}) {
super(attrs)
const {
dimOrdering = 'tf'
} = attrs
this.size = size
this.dimOrdering = dimOrdering
}
/**
* Method for layer computational logic
* @param {Tensor} x
* @returns {Tensor} x
*/
call = x => {
// convert to tf ordering
if (this.dimOrdering === 'th') {
x.tensor = x.tensor.transpose(1, 2, 0)
}
const inputShape = x.tensor.shape
const outputShape = [
inputShape[0] * this.size[0],
inputShape[1] * this.size[1],
inputShape[2]
]
let y = new Tensor([], outputShape)
for (let i = 0; i < this.size[0]; i++) {
for (let j = 0; j < this.size[1]; j++) {
ops.assign(
y.tensor.lo(i, j, 0).step(this.size[0], this.size[1], 1),
x.tensor
)
}
}
x.tensor = y.tensor
// convert back to th ordering if necessary
if (this.dimOrdering === 'th') {
x.tensor = x.tensor.transpose(2, 0, 1)
}
return x
}
}
+61
View File
@@ -0,0 +1,61 @@
import Layer from '../../engine/Layer'
import Tensor from '../../Tensor'
import ops from 'ndarray-ops'
/**
* UpSampling3D layer class
*/
export default class UpSampling3D extends Layer {
/**
* Creates a UpSampling3D activation layer
* @param {number} size - upsampling factor
*/
constructor (size = [2, 2, 2], attrs = {}) {
super(attrs)
const {
dimOrdering = 'tf'
} = attrs
this.size = size
this.dimOrdering = dimOrdering
}
/**
* Method for layer computational logic
* @param {Tensor} x
* @returns {Tensor} x
*/
call = x => {
// convert to tf ordering
if (this.dimOrdering === 'th') {
x.tensor = x.tensor.transpose(1, 2, 3, 0)
}
const inputShape = x.tensor.shape
const outputShape = [
inputShape[0] * this.size[0],
inputShape[1] * this.size[1],
inputShape[2] * this.size[2],
inputShape[3]
]
let y = new Tensor([], outputShape)
for (let i = 0; i < this.size[0]; i++) {
for (let j = 0; j < this.size[1]; j++) {
for (let k = 0; k < this.size[2]; k++) {
ops.assign(
y.tensor.lo(i, j, k, 0).step(this.size[0], this.size[1], this.size[2], 1),
x.tensor
)
}
}
}
x.tensor = y.tensor
// convert back to th ordering if necessary
if (this.dimOrdering === 'th') {
x.tensor = x.tensor.transpose(3, 0, 1, 2)
}
return x
}
}
+7 -1
View File
@@ -1,7 +1,13 @@
import Convolution1D from './Convolution1D'
import Convolution2D from './Convolution2D'
import UpSampling1D from './UpSampling1D'
import UpSampling2D from './UpSampling2D'
import UpSampling3D from './UpSampling3D'
export {
Convolution1D,
Convolution2D
Convolution2D,
UpSampling1D,
UpSampling2D,
UpSampling3D
}
+48
View File
@@ -0,0 +1,48 @@
/* eslint-env browser, mocha */
describe('convolutional layer: UpSampling1D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
const stringifyCondensed = testGlobals.stringifyCondensed
const approxEquals = KerasJS.testUtils.approxEquals
const layers = KerasJS.layers
before(function () {
console.log('\n%cconvolutional layer: UpSampling1D', styles.h1)
})
it(`[convolutional.UpSampling1D.0] length 2 upsampling on 3x5 input`, function () {
const key = `convolutional.UpSampling1D.0`
console.log(`\n%c[${key}] length 2 upsampling on 3x5 input`, styles.h3)
let testLayer = new layers.UpSampling1D(2)
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling1D.1] length 3 upsampling on 4x4 input`, function () {
const key = `convolutional.UpSampling1D.1`
console.log(`\n%c[${key}] length 3 upsampling on 4x4 input`, styles.h3)
let testLayer = new layers.UpSampling1D(3)
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
+82
View File
@@ -0,0 +1,82 @@
/* eslint-env browser, mocha */
describe('convolutional layer: UpSampling2D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
const stringifyCondensed = testGlobals.stringifyCondensed
const approxEquals = KerasJS.testUtils.approxEquals
const layers = KerasJS.layers
before(function () {
console.log('\n%cconvolutional layer: UpSampling2D', styles.h1)
})
it(`[convolutional.UpSampling2D.0] size 2x2 upsampling on 3x3x3 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling2D.0`
console.log(`\n%c[${key}] size 2x2 upsampling on 3x3x3 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling2D([2, 2], { dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling2D.1] size 2x2 upsampling on 3x3x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling2D.1`
console.log(`\n%c[${key}] size 2x2 upsampling on 3x3x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling2D([2, 2], { dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling2D.2] size 3x2 upsampling on 4x2x2 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling2D.2`
console.log(`\n%c[${key}] size 3x2 upsampling on 4x2x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling2D([3, 2], { dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling2D.3] size 1x3 upsampling on 4x3x2 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling2D.3`
console.log(`\n%c[${key}] size 1x3 upsampling on 4x3x2 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling2D([1, 3], { dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
+82
View File
@@ -0,0 +1,82 @@
/* eslint-env browser, mocha */
describe('convolutional layer: UpSampling3D', function () {
const assert = chai.assert
const styles = testGlobals.styles
const logTime = testGlobals.logTime
const stringifyCondensed = testGlobals.stringifyCondensed
const approxEquals = KerasJS.testUtils.approxEquals
const layers = KerasJS.layers
before(function () {
console.log('\n%cconvolutional layer: UpSampling3D', styles.h1)
})
it(`[convolutional.UpSampling3D.0] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling3D.0`
console.log(`\n%c[${key}] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 2, 2], { dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling3D.1] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling3D.1`
console.log(`\n%c[${key}] size 2x2x2 upsampling on 2x2x2x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 2, 2], { dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling3D.2] size 1x3x2 upsampling on 2x1x3x2 input, dimOrdering=tf`, function () {
const key = `convolutional.UpSampling3D.2`
console.log(`\n%c[${key}] size 1x3x2 upsampling on 2x1x3x2 input, dimOrdering=tf`, styles.h3)
let testLayer = new layers.UpSampling3D([1, 3, 2], { dimOrdering: 'tf' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
it(`[convolutional.UpSampling3D.3] 2x1x2 upsampling on 2x1x3x3 input, dimOrdering=th`, function () {
const key = `convolutional.UpSampling3D.3`
console.log(`\n%c[${key}] 2x1x2 upsampling on 2x1x3x3 input, dimOrdering=th`, styles.h3)
let testLayer = new layers.UpSampling3D([2, 1, 2], { dimOrdering: 'th' })
let t = new KerasJS.Tensor(TEST_DATA[key].input.data, TEST_DATA[key].input.shape)
console.log('%cin', styles.h4, stringifyCondensed(t.tensor))
const startTime = performance.now()
t = testLayer.call(t)
const endTime = performance.now()
console.log('%cout', styles.h4, stringifyCondensed(t.tensor))
logTime(startTime, endTime)
const dataExpected = new Float32Array(TEST_DATA[key].expected.data)
const shapeExpected = TEST_DATA[key].expected.shape
assert.deepEqual(t.tensor.shape, shapeExpected)
assert.isTrue(approxEquals(t.tensor, dataExpected))
})
})
+30
View File
@@ -0,0 +1,30 @@
// TEST DATA
// Keyed by mocha test ID
// Python code for generating test data can be found in the matching jupyter notebook in folder `notebooks/`.
(function () {
var DATA = {
'convolutional.UpSampling1D.0': {
input: {
data: [0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778],
shape: [3, 5]
},
expected: {
data: [0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.262, 0.764609, -0.482897, -0.371755, 0.871769, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.490033, -0.986894, -0.960468, 0.373039, 0.911356, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778, 0.00298, 0.270652, 0.749006, 0.692235, 0.471778],
shape: [6, 5]
}
},
'convolutional.UpSampling1D.1': {
input: {
data: [0.562988, 0.168418, -0.14658, -0.369311, 0.653777, 0.806859, -0.922124, 0.830445, -0.878989, -0.638546, -0.855401, -0.082476, 0.416718, -0.03353, -0.949107, -0.866195],
shape: [4, 4]
},
expected: {
data: [0.562988, 0.168418, -0.14658, -0.369311, 0.562988, 0.168418, -0.14658, -0.369311, 0.562988, 0.168418, -0.14658, -0.369311, 0.653777, 0.806859, -0.922124, 0.830445, 0.653777, 0.806859, -0.922124, 0.830445, 0.653777, 0.806859, -0.922124, 0.830445, -0.878989, -0.638546, -0.855401, -0.082476, -0.878989, -0.638546, -0.855401, -0.082476, -0.878989, -0.638546, -0.855401, -0.082476, 0.416718, -0.03353, -0.949107, -0.866195, 0.416718, -0.03353, -0.949107, -0.866195, 0.416718, -0.03353, -0.949107, -0.866195],
shape: [12, 4]
}
}
}
window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)
})()
+50
View File
@@ -0,0 +1,50 @@
// TEST DATA
// Keyed by mocha test ID
// Python code for generating test data can be found in the matching jupyter notebook in folder `notebooks/`.
(function () {
var 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.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.570441, -0.454673, -0.454673, -0.285321, -0.285321, -0.570441, -0.570441, -0.454673, -0.454673, -0.285321, -0.285321, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.237249, 0.237249, 0.282682, 0.282682, 0.428035, 0.428035, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.160547, 0.160547, -0.332203, -0.332203, 0.546391, 0.546391, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, 0.272735, 0.272735, 0.010827, 0.010827, -0.763164, -0.763164, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, -0.442696, -0.442696, 0.381948, 0.381948, -0.676994, -0.676994, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, 0.753553, 0.753553, -0.031788, -0.031788, 0.915329, 0.915329, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, -0.738844, -0.738844, 0.269075, 0.269075, 0.434091, 0.434091, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.991585, 0.991585, -0.944288, -0.944288, 0.258834, 0.258834, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094, 0.162138, 0.162138, 0.565201, 0.565201, -0.492094, -0.492094],
shape: [3, 6, 6]
}
},
'convolutional.UpSampling2D.2': {
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],
shape: [4, 2, 2]
},
expected: {
data: [0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, 0.275222, -0.793967, 0.275222, -0.793967, -0.468107, -0.841484, -0.468107, -0.841484, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.295362, 0.78175, -0.295362, 0.78175, 0.068787, -0.261747, 0.068787, -0.261747, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, -0.625733, -0.042907, -0.625733, -0.042907, 0.861141, 0.85267, 0.861141, 0.85267, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008, 0.956439, 0.717838, 0.956439, 0.717838, -0.99869, -0.963008, -0.99869, -0.963008],
shape: [12, 4, 2]
}
},
'convolutional.UpSampling2D.3': {
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, -0.253526, -0.136544, -0.295772, 0.207565, -0.517246, -0.686963, -0.176235, -0.354111],
shape: [4, 3, 2]
},
expected: {
data: [-0.989173, -0.989173, -0.989173, -0.133618, -0.133618, -0.133618, -0.505338, -0.505338, -0.505338, 0.023259, 0.023259, 0.023259, 0.503982, 0.503982, 0.503982, -0.303769, -0.303769, -0.303769, -0.436321, -0.436321, -0.436321, 0.793911, 0.793911, 0.793911, 0.416102, 0.416102, 0.416102, 0.806405, 0.806405, 0.806405, -0.098342, -0.098342, -0.098342, -0.738022, -0.738022, -0.738022, -0.982676, -0.982676, -0.982676, 0.805073, 0.805073, 0.805073, 0.741244, 0.741244, 0.741244, -0.941634, -0.941634, -0.941634, -0.253526, -0.253526, -0.253526, -0.136544, -0.136544, -0.136544, -0.295772, -0.295772, -0.295772, 0.207565, 0.207565, 0.207565, -0.517246, -0.517246, -0.517246, -0.686963, -0.686963, -0.686963, -0.176235, -0.176235, -0.176235, -0.354111, -0.354111, -0.354111],
shape: [4, 3, 6]
}
}
}
window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)
})()
+50
View File
@@ -0,0 +1,50 @@
// TEST DATA
// Keyed by mocha test ID
// Python code for generating test data can be found in the matching jupyter notebook in folder `notebooks/`.
(function () {
var DATA = {
'convolutional.UpSampling3D.0': {
input: {
data: [-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],
shape: [2, 2, 2, 3]
},
expected: {
data: [-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],
shape: [4, 4, 4, 3]
}
},
'convolutional.UpSampling3D.1': {
input: {
data: [-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],
shape: [2, 2, 2, 3]
},
expected: {
data: [-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],
shape: [2, 4, 4, 6]
}
},
'convolutional.UpSampling3D.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],
shape: [2, 1, 3, 2]
},
expected: {
data: [-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],
shape: [2, 3, 6, 2]
}
},
'convolutional.UpSampling3D.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],
shape: [2, 1, 3, 3]
},
expected: {
data: [-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],
shape: [2, 2, 3, 6]
}
}
}
window.TEST_DATA = Object.assign({}, window.TEST_DATA, DATA)
})()