{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras import backend as K\n",
    "from collections import OrderedDict"
   ]
  },
  {
   "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": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = OrderedDict()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "random_seed = 1000\n",
    "data_in_shape = (17, 17, 2)\n",
    "\n",
    "layers = [\n",
    "    Conv2D(5, (3,3), activation='relu', padding='same', strides=(2, 2), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(4, (3,3), activation='linear', padding='same', strides=(1, 1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(2, (3,3), activation='relu', padding='valid', strides=(1, 1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(3, (5,5), activation='relu', padding='valid', strides=(1, 1), data_format='channels_last', use_bias=True),\n",
    "    Conv2D(2, (3,3), activation='linear', padding='valid', strides=(1, 1), data_format='channels_last', use_bias=True)\n",
    "]\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = layers[0](input_layer)\n",
    "for layer in layers[1:-1]:\n",
    "    x = layer(x)\n",
    "output_layer = layers[-1](x)\n",
    "model = Model(inputs=input_layer, outputs=output_layer)\n",
    "\n",
    "np.random.seed(random_seed)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(random_seed + i)\n",
    "    weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "data_out_shape = result[0].shape\n",
    "data_in_formatted = format_decimal(data_in.ravel().tolist())\n",
    "data_out_formatted = format_decimal(result[0].ravel().tolist())\n",
    "\n",
    "DATA['pipeline_01'] = {\n",
    "    'input': {'data': data_in_formatted, 'shape': data_in_shape},\n",
    "    'weights': [{'data': format_decimal(w.ravel().tolist()), 'shape': w.shape} for w in weights],\n",
    "    'expected': {'data': data_out_formatted, 'shape': data_out_shape}\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### export for Keras.js tests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "filename = '../../test/data/pipeline/01.json'\n",
    "if not os.path.exists(os.path.dirname(filename)):\n",
    "    os.makedirs(os.path.dirname(filename))\n",
    "with open(filename, 'w') as f:\n",
    "    json.dump(DATA, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"pipeline_01\": {\"input\": {\"data\": [0.307179, -0.769986, 0.900566, -0.035617, 0.744949, -0.575335, -0.918581, -0.205611, -0.533736, 0.683481, -0.585835, 0.484939, -0.215692, -0.635487, 0.487079, -0.860836, 0.770674, 0.905289, 0.862287, -0.169138, -0.942037, 0.964055, -0.320725, 0.413374, -0.276246, -0.929788, 0.710117, 0.314507, 0.531366, 0.108174, 0.770186, 0.808395, -0.979157, -0.850887, -0.510742, -0.73339, 0.39585, -0.20359, 0.766244, -0.637985, -0.135002, -0.963714, 0.382876, -0.060619, -0.743556, 0.782674, 0.836407, -0.853758, -0.909104, -0.122854, 0.203442, -0.379546, 0.363816, -0.581974, 0.039209, 0.131978, -0.117665, -0.724888, -0.572914, -0.733256, -0.355407, -0.532226, 0.054996, 0.131942, -0.123549, -0.356255, 0.119282, 0.730691, 0.694566, -0.784366, -0.367361, -0.181043, 0.374178, 0.404471, -0.107604, -0.159356, 0.605261, 0.077235, 0.847001, -0.876185, -0.264833, 0.940798, 0.398208, 0.781951, -0.492895, 0.451054, -0.593011, 0.075024, -0.526114, -0.127016, 0.596049, -0.383182, 0.243033, -0.120704, 0.826647, 0.317372, 0.307263, -0.283084, 0.045883, -0.909825, -0.811381, 0.843224, -0.853911, 0.858835, 0.43403, 0.244621, 0.509979, -0.71666, 0.587644, 0.450806, 0.082879, -0.034831, -0.047045, -0.107934, 0.63214, -0.451297, -0.876076, 0.920593, -0.083764, 0.515784, 0.362317, 0.083556, -0.734335, -0.493589, -0.112289, 0.1175, 0.627729, -0.364343, -0.20591, 0.780336, 0.650832, -0.786626, -0.680448, 0.302439, 0.138128, 0.968167, 0.498318, 0.604799, -0.54338, 0.037427, 0.260217, -0.995629, 0.02643, -0.10162, -0.301287, -0.452637, 0.804166, -0.938051, -0.351662, -0.426815, 0.914272, -0.966934, -0.085353, -0.350128, 0.392927, 0.3855, 0.365292, 0.784261, 0.891156, -0.836385, -0.766222, -0.926262, 0.84758, 0.963802, -0.246749, -0.873048, -0.744287, 0.759778, -0.091904, -0.601018, 0.252064, 0.552859, -0.070904, -0.919266, -0.252198, 0.733837, -0.793589, -0.774978, 0.560268, -0.729902, -0.742883, -0.518994, -0.416378, -0.333527, 0.584328, -0.511306, -0.78923, -0.765839, -0.281298, -0.532119, 0.65626, 0.221575, -0.420019, 0.994877, -0.161517, 0.789445, -0.423508, -0.37311, 0.961123, -0.821726, 0.760378, 0.920009, -0.318366, 0.831071, 0.797277, 0.998735, -0.764629, 0.190218, 0.571854, 0.451473, 0.659159, -0.961103, 0.893633, -0.673254, -0.945135, 0.958047, -0.630267, 0.668912, -0.97545, -0.932668, -0.855912, 0.273892, -0.508473, -0.147774, 0.92673, -0.589615, -0.529108, -0.026501, -0.442497, 0.043248, -0.097879, -0.32872, -0.976222, 0.516042, 0.77989, -0.275965, -0.066363, -0.878685, 0.477652, -0.531969, -0.738158, -0.719768, -0.598863, 0.459657, -0.136082, 0.397067, -0.716891, -0.552125, 0.645655, -0.992376, -0.547147, 0.675121, 0.359423, -0.452114, 0.642834, -0.65893, 0.084555, 0.198223, 0.947512, 0.347592, -0.30919, 0.017157, -0.504988, 0.975185, -0.18213, -0.827767, 0.962281, 0.793245, 0.066392, -0.929715, 0.675438, 0.807816, 0.260682, -0.437998, 0.865408, -0.964172, -0.358725, -0.297628, 0.698402, -0.991327, 0.296135, 0.111031, -0.325199, -0.829535, -0.706708, 0.315692, -0.908343, -0.876177, -0.084321, 0.042117, -0.96889, 0.065587, -0.761545, -0.771358, 0.370507, -0.371447, 0.567487, 0.906827, 0.306348, 0.991507, -0.251738, -0.179506, -0.13481, 0.009328, 0.50426, -0.478935, -0.23036, 0.482967, 0.108361, 0.323875, -0.107073, 0.484604, 0.976787, -0.757528, 0.55991, -0.087214, -0.45433, 0.964335, -0.710762, -0.223378, 0.338984, -0.566817, -0.423957, -0.133355, 0.771202, -0.348399, 0.698459, -0.553718, 0.362846, 0.765188, -0.810349, 0.415447, -0.261726, 0.141378, 0.394877, -0.236814, -0.136671, -0.605446, -0.816587, -0.834155, -0.867417, -0.005713, 0.832653, -0.74534, 0.015888, -0.859369, 0.263748, -0.514297, -0.724415, -0.519365, -0.26064, 0.562583, 0.556172, -0.894421, 0.485605, 0.068895, -0.574381, 0.952707, 0.854282, -0.816716, 0.566863, -0.851922, -0.304581, 0.940295, -0.092955, -0.252095, -0.366209, 0.8118, 0.609144, 0.66955, 0.296141, -0.522011, -0.547367, -0.046487, -0.141129, -0.683622, -0.608246, -0.942115, 0.200807, -0.171167, 0.913158, -0.576902, -0.177556, -0.085063, 0.942418, -0.803642, -0.231319, 0.110499, 0.216448, 0.554095, -0.775795, -0.858061, -0.894796, -0.177721, 0.812784, 0.032743, 0.14351, 0.759765, -0.70414, -0.310653, -0.70489, -0.174463, -0.927175, -0.788743, -0.979671, 0.48012, 0.132285, -0.671046, -0.958749, 0.945391, 0.040751, -0.738711, 0.822168, 0.910797, 0.662187, 0.325641, -0.134855, 0.310373, -0.499573, 0.135194, 0.376075, 0.742203, -0.185185, 0.681693, -0.614504, 0.345224, 0.663712, 0.014513, 0.42408, 0.833134, -0.260953, -0.078377, -0.933143, -0.000877, -0.478206, 0.430175, -0.840631, 0.428118, 0.846154, -0.40912, 0.509388, 0.669512, 0.756252, -0.375561, -0.551532, 0.88488, 0.436005, -0.751576, -0.563339, 0.004887, 0.137054, 0.19583, -0.215128, -0.232628, -0.257705, 0.010894, -0.638739, 0.98891, 0.761058, 0.692921, 0.183508, 0.590217, -0.769043, 0.707619, -0.393498, 0.806385, -0.468566, -0.96876, 0.760738, 0.436183, -0.535061, -0.5628, 0.886738, 0.816212, 0.004339, -0.891716, 0.782022, 0.107881, 0.594794, 0.171877, -0.817309, 0.675026, 0.824638, -0.890092, -0.45473, -0.371234, 0.087631, -0.569719, -0.864876, 0.215788, 0.716897, 0.727561, -0.569618, -0.850193, 0.307958, -0.477611, 0.660603, 0.779336, 0.535998, -0.280336, -0.701544, -0.055577, 0.706065, 0.288925, -0.635776, -0.431255, 0.831398, 0.454381, 0.062509, 0.856797, -0.675694, -0.783077, 0.829229, 0.012088, 0.298661, -0.092186, 0.103526, 0.322325, -0.303668, 0.574324, -0.879712, 0.924606, -0.743586, -0.737669, -0.977774, -0.04452, 0.338219, -0.214654, -0.549583, 0.314906, -0.659444, -0.128102, 0.719527, 0.643744, 0.405795, 0.500263, -0.968486, -0.103665, 0.381867, -0.002469, -0.078127, 0.806476, -0.87487, -0.409392, 0.632035, -0.159183, -0.01837, -0.464667, 0.171106, 0.491622, -0.939924, 0.929357, 0.889669, 0.693238, -0.425875, -0.792446, 0.992246, -0.714005, 0.477842, 0.014597, 0.088993, 0.478136, -0.63804, -0.777092, -0.704203, 0.141475, -0.285228, 0.287741, 0.136988, 0.121701, 0.481481, 0.098355, -0.759691, 0.258656, 0.925657, 0.56495, 0.004887, 0.934166], \"shape\": [17, 17, 2]}, \"weights\": [{\"data\": [0.307179, -0.769986, 0.900566, -0.035617, 0.744949, -0.575335, -0.918581, -0.205611, -0.533736, 0.683481, -0.585835, 0.484939, -0.215692, -0.635487, 0.487079, -0.860836, 0.770674, 0.905289, 0.862287, -0.169138, -0.942037, 0.964055, -0.320725, 0.413374, -0.276246, -0.929788, 0.710117, 0.314507, 0.531366, 0.108174, 0.770186, 0.808395, -0.979157, -0.850887, -0.510742, -0.73339, 0.39585, -0.20359, 0.766244, -0.637985, -0.135002, -0.963714, 0.382876, -0.060619, -0.743556, 0.782674, 0.836407, -0.853758, -0.909104, -0.122854, 0.203442, -0.379546, 0.363816, -0.581974, 0.039209, 0.131978, -0.117665, -0.724888, -0.572914, -0.733256, -0.355407, -0.532226, 0.054996, 0.131942, -0.123549, -0.356255, 0.119282, 0.730691, 0.694566, -0.784366, -0.367361, -0.181043, 0.374178, 0.404471, -0.107604, -0.159356, 0.605261, 0.077235, 0.847001, -0.876185, -0.264833, 0.940798, 0.398208, 0.781951, -0.492895, 0.451054, -0.593011, 0.075024, -0.526114, -0.127016], \"shape\": [3, 3, 2, 5]}, {\"data\": [-0.387536, -0.469873, -0.60788, -0.138957, -0.953773], \"shape\": [5]}, {\"data\": [-0.742023, -0.077688, -0.167692, 0.205448, -0.633864, -0.164175, -0.731823, 0.313236, 0.613465, -0.723716, -0.299231, 0.229032, 0.102561, 0.384949, -0.90948, -0.294898, -0.916217, -0.699031, -0.323329, -0.673445, 0.521949, -0.306796, -0.476018, -0.628623, 0.808028, -0.585043, -0.307429, -0.234868, -0.897584, 0.741743, 0.320785, 0.709132, -0.978084, 0.601894, -0.228816, -0.069558, -0.522066, -0.399597, -0.916222, 0.161549, -0.211915, 0.823372, -0.6549, -0.30403, 0.677588, -0.431259, 0.219659, -0.091937, -0.101636, -0.595218, -0.815428, 0.502932, 0.775249, 0.624226, 0.622601, -0.091075, 0.763603, 0.472659, 0.621131, -0.504549, -0.270214, 0.492749, 0.643055, -0.290058, -0.752162, 0.758918, 0.011832, -0.183967, 0.768298, 0.764241, 0.906398, 0.872853, -0.292238, 0.16788, -0.447741, 0.679196, 0.566614, 0.867549, -0.011606, -0.252108, 0.165669, -0.509362, 0.620632, -0.32465, -0.071143, -0.823613, 0.331067, -0.016903, -0.76138, -0.491146, 0.106088, -0.641492, 0.234893, 0.658853, -0.475623, 0.269103, 0.935505, -0.577134, 0.985015, -0.405957, -0.325882, 0.849518, -0.589155, 0.378331, -0.753075, 0.711411, 0.04547, 0.398327, -0.665657, 0.531142, -0.410293, -0.526649, 0.860648, 0.32795, -0.197082, -0.095526, -0.391361, 0.785465, -0.267269, -0.020154, -0.95189, -0.580742, 0.788104, -0.092433, 0.320354, 0.070651, 0.045416, 0.99799, 0.583116, -0.708131, -0.104784, -0.838947, -0.598224, 0.209105, 0.824956, 0.10438, 0.692046, -0.091308, 0.884896, 0.730617, 0.244486, -0.415624, -0.397714, -0.647236, -0.816162, 0.001325, 0.593873, -0.243723, 0.168275, 0.192345, -0.522916, 0.458154, -0.333828, -0.014549, -0.744552, -0.203297, 0.771256, -0.703928, 0.998892, -0.947633, 0.566086, -0.274437, -0.218108, -0.800599, 0.504541, 0.233776, -0.111802, -0.03089, 0.761595, 0.537963, 0.217941, -0.910822, 0.531235, -0.018533, -0.161811, -0.200401, -0.742618, -0.35126, -0.954474, -0.071092], \"shape\": [3, 3, 5, 4]}, {\"data\": [0.195612, -0.128132, -0.96626, 0.193375], \"shape\": [4]}, {\"data\": [-0.922097, 0.712992, 0.493001, 0.727856, 0.119969, -0.839034, -0.536727, -0.515472, 0.231, 0.214218, -0.791636, -0.148304, 0.309846, 0.742779, -0.123022, 0.427583, -0.882276, 0.818571, 0.043634, 0.454859, -0.007311, -0.744895, -0.368229, 0.324805, -0.388758, -0.556215, -0.542859, 0.685655, 0.350785, -0.312753, 0.591401, 0.95999, 0.136369, -0.58844, -0.506667, -0.208736, 0.548969, 0.653173, 0.128943, 0.180094, -0.16098, 0.208798, 0.666245, 0.347307, -0.384733, -0.88354, -0.328468, -0.515324, 0.479247, -0.360647, 0.09069, -0.221424, 0.091284, 0.202631, 0.208087, 0.582248, -0.164064, -0.925036, -0.678806, -0.212846, 0.960861, 0.536089, -0.038634, -0.473456, -0.409408, 0.620315, -0.873085, -0.695405, -0.024465, 0.762843, -0.928228, 0.557106], \"shape\": [3, 3, 4, 2]}, {\"data\": [0.318429, -0.858397], \"shape\": [2]}, {\"data\": [0.486255, -0.547151, 0.285068, 0.764711, 0.481398, 0.442527, -0.409304, 0.051033, -0.652471, 0.623918, 0.698811, -0.48696, -0.525531, -0.083229, -0.54216, -0.595979, 0.965361, 0.961457, 0.469608, -0.18139, 0.237622, -0.841546, -0.201479, 0.210842, -0.099026, -0.017468, -0.270985, -0.421947, 0.990418, 0.633556, -0.46994, -0.283905, 0.339371, 0.851372, -0.963439, 0.672347, -0.592494, 0.115008, 0.77155, -0.629049, -0.284972, 0.08256, -0.526964, -0.579017, 0.048964, 0.296374, -0.503246, -0.95555, -0.759658, -0.148746, 0.527992, 0.419541, -0.601167, -0.246472, 0.611566, -0.47989, -0.796678, 0.845136, -0.929013, 0.081316, -0.965527, 0.064677, 0.687209, -0.781686, 0.556524, -0.294628, 0.343468, -0.693394, -0.864068, 0.522942, -0.854592, 0.954066, 0.352462, 0.404271, 0.935993, 0.006064, -0.614327, -0.951249, -0.974544, -0.981322, -0.524456, -0.353175, -0.283883, 0.270072, 0.336334, -0.529043, 0.880513, -0.663035, -0.709319, -0.69236, 0.233949, 0.90419, -0.721928, 0.580281, -0.149192, -0.246252, 0.099723, 0.986128, -0.979644, 0.242715, 0.433547, 0.199869, -0.572331, -0.152181, 0.329916, -0.071652, -0.580827, 0.88984, -0.857622, -0.926973, -0.444937, -0.183938, 0.72548, -0.238406, -0.651195, -0.770945, -0.97797, -0.666038, 0.253825, 0.001102, -0.769608, -0.364219, 0.653122, -0.845224, -0.900383, 0.916435, 0.562575, 0.577639, -0.655935, 0.683806, -0.955929, 0.271965, 0.670582, -0.874893, -0.671992, -0.124948, 0.354001, -0.289044, -0.880824, -0.505697, 0.975131, -0.404046, 0.345771, -0.013626, -0.077943, 0.837888, -0.371654, 0.453362, 0.331138, -0.360725], \"shape\": [5, 5, 2, 3]}, {\"data\": [0.0965, 0.594443, -0.987782], \"shape\": [3]}, {\"data\": [0.228005, 0.859479, -0.49018, 0.232871, -0.303968, -0.799141, 0.621228, 0.850429, 0.029476, -0.583346, 0.889636, -0.128896, 0.067108, -0.1059, -0.788773, -0.559347, 0.674802, 0.513275, -0.95495, -0.230976, -0.430566, 0.607782, -0.292593, -0.362274, -0.825576, 0.904458, 0.531651, 0.139053, -0.797761, 0.905804, -0.875903, 0.04377, -0.704592, 0.203555, -0.083031, 0.321316, 0.334565, 0.965166, 0.31912, 0.987618, 0.11275, 0.755438, 0.133156, -0.271605, -0.739053, 0.930942, 0.723852, -0.399546, 0.365907, 0.680404, 0.302211, 0.481088, -0.254831, -0.719056], \"shape\": [3, 3, 3, 2]}, {\"data\": [0.229761, -0.670851], \"shape\": [2]}], \"expected\": {\"data\": [1.396554, 4.630284], \"shape\": [1, 1, 2]}}}\n"
     ]
    }
   ],
   "source": [
    "print(json.dumps(DATA))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
