我正在按照我在Multi-Input Convolutional Neural Network for Flower Grading上阅读的体系结构,尝试开发多输入CNN。
我有一个csv文件,其中存储了每个数据项的值,并且对于每个项,我从不同角度捕获了4张图片。当我运行以下代码时,网络可以正确打印,但是似乎没有任何反应,因为使用nvidia-smi的GPU使用率低于5%。
kilograms_trees = tf.data.experimental.CsvDataset(
filenames='dataset/agrumeto.csv',
record_defaults=[tf.float32],
field_delim=",",
header=True)
kilo_train = kilograms_trees.take(35)
kilo_test = kilograms_trees.skip(35)
def create_conv_layer(input):
x = tf.keras.layers.Conv2D(32, (7, 7), activation='relu')(input)
x = tf.keras.layers.MaxPooling2D((2, 2), (2,2))(x)
x = tf.keras.Model(inputs=input, outputs=x)
return x
inputA = tf.keras.Input(shape=(size,size,3))
inputB = tf.keras.Input(shape=(size,size,3))
inputC = tf.keras.Input(shape=(size,size,3))
inputD = tf.keras.Input(shape=(size,size,3))
x = create_conv_layer(inputA)
y = create_conv_layer(inputB)
w = create_conv_layer(inputC)
z = create_conv_layer(inputD)
# combine the output of the two branches
combined = tf.keras.layers.concatenate([x.output, y.output, w.output, z.output])
layer_1 = tf.keras.layers.Conv2D(16, (3,3), activation="relu")(combined)
layer_1 = tf.keras.layers.MaxPooling2D((2, 2))(layer_1)
layer_2 = tf.keras.layers.Conv2D(16, (3,3), activation="relu")(layer_1)
layer_2 = tf.keras.layers.MaxPooling2D((2, 2), (2,2))(layer_2)
layer_3 = tf.keras.layers.Conv2D(32, (3,3), activation="relu")(layer_2)
layer_3 = tf.keras.layers.MaxPooling2D((2, 2), (2,2))(layer_3)
layer_4 = tf.keras.layers.Conv2D(32, (3,3), activation="relu")(layer_3)
layer_4 = tf.keras.layers.MaxPooling2D((2, 2), (2,2))(layer_4)
flatten = tf.keras.layers.Flatten()(layer_4)
hidden1 = tf.keras.layers.Dense(10, activation='relu')(flatten)
output = tf.keras.layers.Dense(1, activation='relu')(hidden1)
model = tf.keras.Model(inputs=[x.input, y.input, w.input, z.input], outputs=output)
print(model.summary())
model.compile(optimizer='adam',
loss="mean_absolute_percentage_error")
print("[INFO] training model...")
model.fit([trainA, trainB, trainC, trainD], kilo_train, epochs=5, batch_size=4)
test_loss, test_acc = model.evaluate([testA, testB, testC, testD], kilo_test)
print(test_acc)
以下是nvidia-smi输出:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.40.04 Driver Version: 418.40.04 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 1050 On | 00000000:01:00.0 Off | N/A |
| N/A 54C P0 N/A / N/A | 3830MiB / 4042MiB | 8% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 909 C ...ycharmProjects/agrumeto/venv/bin/python 3159MiB |
| 0 1729 G /usr/lib/xorg/Xorg 27MiB |
| 0 1870 G /usr/bin/gnome-shell 69MiB |
| 0 6290 G /usr/lib/xorg/Xorg 273MiB |
| 0 6420 G /usr/bin/gnome-shell 127MiB |
| 0 6834 G ...quest-channel-token=6261236721362009153 85MiB |
| 0 8806 G ...pycharm-professional/132/jre64/bin/java 2MiB |
| 0 12830 G ...-token=60E939FEF0A8E3D5C46B3D6911048536 31MiB |
| 0 27478 G ...-token=ECA4D3D9ADD8448674D34492E89E40E3 51MiB |
+-----------------------------------------------------------------------------+
这些是输出控制台的最后几行:
conv2d_7 (Conv2D) (None, 14, 14, 32) 9248 max_pooling2d_6[0][0]
__________________________________________________________________________________________________
max_pooling2d_7 (MaxPooling2D) (None, 7, 7, 32) 0 conv2d_7[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (None, 1568) 0 max_pooling2d_7[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 10) 15690 flatten[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 1) 11 dense[0][0]
==================================================================================================
Total params: 69,301
Trainable params: 69,301
Non-trainable params: 0
__________________________________________________________________________________________________
None
[INFO] training model...
最佳答案
我忘了禁用Tensorflow 2.0中默认启用的Eager Execution。那就是问题所在。
关于python - Tensorflow 2.0中的多输入CNN无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56163315/