![](/img/trans.png)
[英]fix input_shape of 3D Conv in keras (Tensorflow at the backend)
[英]Input_shape in 3D CNN
我有一個 100000 二進制 3D arrays 的數據集,形狀為 (6, 4, 4),所以我的輸入的形狀是 (10000, 6, 4, 4)。 我正在嘗試使用 Keras 設置 3D 卷積神經網絡 (CNN); 但是,我輸入的 input_shape 似乎有問題。 我的第一層是:
model.add(Conv3D(20, kernel_size=(2, 2, 2), strides=(1, 1, 1), activation='relu', kernel_initializer='he_uniform', input_shape=(None, 6, 4, 4, 1)))
我收到的錯誤是:
ValueError:應定義
Dense
輸入的最后一個維度。 發現None
。
盡管如此,當我用 integer 替換 None 並嘗試將 model 適合我的數據集時,我得到的錯誤是:
ValueError:layersequential_13 的輸入 0 與 layer 不兼容::預期 min_ndim=5,發現 ndim=4。 收到的完整形狀:(10、6、4、4)
這里應該修改什么?
帶有虛擬數據的示例:
import tensorflow as tf
x = tf.random.normal((100000, 6, 4, 4))
x.numpy().shape # (100000, 6, 4, 4) # 4 --> channel size by default
這不適用於 3D 卷積:
y = tf.keras.layers.Conv3D(2, 3, activation='relu',
input_shape=(6, 4, 4, 1))(x)
print('After convolution', y.shape)
它會說expected min_ndim=5, found ndim=4...
。 您需要向該數據添加渠道維度。 這可以通過以下方式簡單地完成:
x_expanded = tf.expand_dims(x, axis = -1)
print('Expand_dims shape', x_expanded.numpy().shape) # (100000, 6, 4, 4, 1)
y = tf.keras.layers.Conv3D(2, 3, activation='relu',
input_shape=(6, 4, 4, 1))(x_expanded)
print('After convolution', y.shape) # (100000, 4, 2, 2, 2)
或者第一次,您可以使用帶有 2D 卷積的數據:
x = tf.random.normal((100000, 6, 4, 4))
y = tf.keras.layers.Conv2D(2, 3, activation='relu',
input_shape=(6, 4, 4))(x)
print('After convolution', y.shape) # (100000, 4, 2, 2)
您還可以更改卷積層中通道的順序:
y = tf.keras.layers.Conv2D(2, 3, activation='relu',
input_shape=(6, 4, 4),
data_format="channels_first")(x) # --> 6 will be channels
print(y.shape) # (100000, 2, 2, 2)
編輯:
model.add(tf.keras.layers.Conv3D(2, 3, activation='relu', input_shape=(6, 4, 4, 1)))
model(x_expanded).shape # TensorShape([100000, 4, 2, 2, 2])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.