In [1]:
import numpy as np
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Permute
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()

Permute

[core.Permute.0] shape [3, 2] -> [2, 3]


In [4]:
data_in_shape = (3, 2)

layer_0 = Input(shape=data_in_shape)
layer_1 = Permute((2, 1))(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2]
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Permute.0'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2]
in shape: (3, 2)
out shape: (2, 3)
out: [0.0, 0.5, 1.0, 0.2, -0.1, 2.0]

[core.Permute.1] shape [2, 3, 4] -> [4, 3, 2]


In [5]:
data_in_shape = (2, 3, 4)

layer_0 = Input(shape=data_in_shape)
layer_1 = Permute((3, 2, 1))(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Permute.1'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
in shape: (2, 3, 4)
out shape: (4, 3, 2)
out: [0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.2, 0.2, 2.0, 2.0, -0.1, -0.1, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.1, -0.1, 0.2, 0.2, 2.0, 2.0]

[core.Permute.2] shape [1, 6, 4] -> [6, 1, 4]


In [6]:
data_in_shape = (1, 6, 4)

layer_0 = Input(shape=data_in_shape)
layer_1 = Permute((2, 1, 3))(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Permute.2'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
in shape: (1, 6, 4)
out shape: (6, 1, 4)
out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]

[core.Permute.3] shape [1, 3, 4, 2] -> [4, 1, 3, 2]


In [7]:
data_in_shape = (1, 3, 4, 2)

layer_0 = Input(shape=data_in_shape)
layer_1 = Permute((3, 1, 2, 4))(layer_0)
model = Model(inputs=layer_0, outputs=layer_1)

data_in = [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
print('in:', data_in)
print('in shape:', data_in_shape)
arr_in = np.array(data_in, dtype='float32').reshape(data_in_shape)
result = model.predict(np.array([arr_in]))
arr_out = result[0]
data_out_shape = arr_out.shape
print('out shape:', data_out_shape)
data_out = format_decimal(arr_out.ravel().tolist())
print('out:', data_out)

DATA['core.Permute.3'] = {
    'input': {'data': data_in, 'shape': data_in_shape},
    'expected': {'data': data_out, 'shape': data_out_shape}
}


in: [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2]
in shape: (1, 3, 4, 2)
out shape: (4, 1, 3, 2)
out: [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0]

export for Keras.js tests


In [8]:
import os

filename = '../../../test/data/layers/core/Permute.json'
if not os.path.exists(os.path.dirname(filename)):
    os.makedirs(os.path.dirname(filename))
with open(filename, 'w') as f:
    json.dump(DATA, f)

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


{"core.Permute.0": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2], "shape": [3, 2]}, "expected": {"data": [0.0, 0.5, 1.0, 0.2, -0.1, 2.0], "shape": [2, 3]}}, "core.Permute.1": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], "shape": [2, 3, 4]}, "expected": {"data": [0.0, 0.0, 1.0, 1.0, 0.5, 0.5, 0.2, 0.2, 2.0, 2.0, -0.1, -0.1, 0.5, 0.5, 0.0, 0.0, 1.0, 1.0, -0.1, -0.1, 0.2, 0.2, 2.0, 2.0], "shape": [4, 3, 2]}}, "core.Permute.2": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], "shape": [1, 6, 4]}, "expected": {"data": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0], "shape": [6, 1, 4]}}, "core.Permute.3": {"input": {"data": [0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2, 0, 0.2, 0.5, -0.1, 1, 2], "shape": [1, 3, 4, 2]}, "expected": {"data": [0.0, 0.2, 0.5, -0.1, 1.0, 2.0, 0.5, -0.1, 1.0, 2.0, 0.0, 0.2, 1.0, 2.0, 0.0, 0.2, 0.5, -0.1, 0.0, 0.2, 0.5, -0.1, 1.0, 2.0], "shape": [4, 1, 3, 2]}}}

In [ ]: