【发布时间】:2017-01-26 16:17:19
【问题描述】:
我想将图像批次的张量轴从 (batch_size, row, col, ch) 交换为 (batch_size, ch, row, col)。
在 numpy 中,这可以用
来完成X_batch = np.moveaxis( X_batch, 3, 1)
我将如何在 Keras 中做到这一点?
【问题讨论】:
标签: tensorflow keras
我想将图像批次的张量轴从 (batch_size, row, col, ch) 交换为 (batch_size, ch, row, col)。
在 numpy 中,这可以用
来完成X_batch = np.moveaxis( X_batch, 3, 1)
我将如何在 Keras 中做到这一点?
【问题讨论】:
标签: tensorflow keras
您可以使用与np.transpose() 完全相同的K.permute_dimensions()。
例子:
import numpy as np
from keras import backend as K
A = np.random.random((1000,32,64,3))
# B = np.moveaxis( A, 3, 1)
C = np.transpose( A, (0,3,1,2))
print A.shape
print C.shape
A_t = K.variable(A)
C_t = K.permute_dimensions(A_t, (0,3,1,2))
print K.eval(A_t).shape
print K.eval(C_t).shape
【讨论】:
使用keras.layers.Permute(dims)dimsdoes not include the samples dimension
model.add(Permute((2, 1), input_shape=(10, 64)))
【讨论】: