У меня проблемы с компиляцией моей модели, у меня, похоже, эта проблема довольно частая, и вывод ошибок не особенно полезен:
ожидается, что Geo будет иметь форму (504 ,) но получил массив с формой (1,)
Я понимаю, что форма где-то не соответствует, но эта ошибка не указывает мне на какую-то конкретную точку в коде. Это из слоя Input (), первого плотного слоя или из самого генератора?
Моя модель - модель с несколькими входами и одним выходом, она принимает массив numpy из 504 входов с плавающей запятой в 'Geo 'вход и SDF 64,64,64,1 на входе' SDF '. Они объединяются, чтобы вывести результат 12 float.
Мои логики c следующие:
# The Data Generator
# Input shape is 42, 12, 1
# SDF shape is 64, 64, 64
def data_generator(train_path, test_path):
"""
Generator for loading geo data.
"""
imgListTestSDF = test_path.parent
imgListTestSDF = imgListTestSDF / "test_sdf"
imgListTrain = sorted(train_path.iterdir() )
imgListTest = sorted(test_path.iterdir() )
imgListTestSDF = sorted(imgListTestSDF.iterdir() )
for dist, norm, norm_sdf in zip(imgListTrain, imgListTest, imgListTestSDF):
norm = np.load(norm)['array']
norm_sdf = np.load(norm_sdf)['array']
dist = np.load(dist)['array']
outshape = norm.shape[0] * 3 * 12
print ("Out shape: %s" % (outshape,)) # Out shape: 504
#norm = np.reshape(norm, (504))
norm = norm.flatten()
print ("Norm reshaped: %s" % (norm.shape,)) # Norm reshaped: (504,)
norm_sdf = np.reshape(norm_sdf, IN_SDF_SHAPE)
# Cast to float32, loaded as float64
norm = norm.astype(np.float32)
dist = dist.astype(np.float32)
norm_combined = {}
norm_combined['Geo'] = norm
norm_combined['SDF'] = norm_sdf
yield norm_combined, dist
NN Logi c:
EPOCHS = 100
BATCH_SIZE = 10
IN_GEO_SHAPE = [504]
inGeoTensorShape = tf.TensorShape(IN_GEO_SHAPE)
IN_SDF_SHAPE = [64, 64, 64, 1]
inSDFTensorShape = tf.TensorShape(IN_SDF_SHAPE)
outTypes = ({'Geo': tf.dtypes.float32 , 'SDF' : tf.dtypes.float32}, tf.dtypes.float32)
outTensorShape = ({'Geo': inGeoTensorShape , 'SDF' : inSDFTensorShape }, tf.TensorShape([12,]))
trainGen = partial(data_generator, train, test)
dataset = tf.data.Dataset.from_generator(trainGen, outTypes, output_shapes=outTensorShape )
# Geo Dense layers.
inGeo = tf.keras.Input(shape=inGeoTensorShape, batch_size=BATCH_SIZE, name="Geo" )
print ("In Geo layer shape: %s" % (inGeo.shape,)) # In Geo layer shape: (10, 504)
dense1 = tf.keras.layers.Dense(512)(inGeo)
dense2 = tf.keras.layers.Dense(512)(dense1)
dense3 = tf.keras.layers.Dense(512)(dense2)
geoOut = tf.keras.layers.Flatten()(dense3)
print ("Geo Out Shape: %s" % (geoOut.shape, )) # Geo Out Shape: (10, 512)
# SDF CNN Net
initializer = tf.random_normal_initializer(0.0, 1.0)
inSDF = tf.keras.Input(shape=inSDFTensorShape, batch_size=BATCH_SIZE, name="SDF")
print ("In SDF Shape: %s" % (inSDF.shape,) ) # In SDF Shape: (10, 64, 64, 64, 1)
conv1 = tf.keras.layers.Conv3D(32, 4, padding='same', kernel_initializer=initializer, input_shape=inSDFTensorShape)(inSDF) # 32
max1 = tf.keras.layers.MaxPool3D((2, 2, 2))(conv1)
... Many CNN layers ...
conv6 = tf.keras.layers.Conv3D(512, 4, padding='same', kernel_initializer=initializer)(max5) # 1
max6 = tf.keras.layers.MaxPool3D((2, 2, 2))(conv6)
sdfOut = tf.keras.layers.Flatten()(max6)
print ("SDF Out Shape: %s" % (sdfOut.shape, )) # SDF Out Shape: (10, 512)
# Concatenation and Output
concat = tf.keras.layers.concatenate([geoOut, sdfOut])
decode1 = tf.keras.layers.Dense(512)(concat)
decode2 = tf.keras.layers.Dense(128)(decode1)
output = tf.keras.layers.Dense(12)(decode2)
print ("Creating Model")
model = tf.keras.Model(inputs=[inGeo, inSDF], outputs=output )
model.summary()
optimiser = tf.keras.optimizers.Adam()
#loss = tf.keras.losses.MSE()
print("Compiling Model")
model.compile(optimizer=optimiser, loss='mse', metrics=['accuracy'])
print("Training Model")
#model.fit(data_gener#ator(train, test) , epochs=EPOCHS, steps_per_epoch=30 )
model.fit(dataset, epochs=EPOCHS, steps_per_epoch=10 )
Вот краткое описание модели:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
SDF (InputLayer) [(10, 64, 64, 64, 1) 0
__________________________________________________________________________________________________
conv3d (Conv3D) (10, 64, 64, 64, 32) 2080 SDF[0][0]
__________________________________________________________________________________________________
max_pooling3d (MaxPooling3D) (10, 32, 32, 32, 32) 0 conv3d[0][0]
__________________________________________________________________________________________________
conv3d_1 (Conv3D) (10, 32, 32, 32, 128 262272 max_pooling3d[0][0]
__________________________________________________________________________________________________
max_pooling3d_1 (MaxPooling3D) (10, 16, 16, 16, 128 0 conv3d_1[0][0]
__________________________________________________________________________________________________
conv3d_2 (Conv3D) (10, 16, 16, 16, 256 2097408 max_pooling3d_1[0][0]
__________________________________________________________________________________________________
max_pooling3d_2 (MaxPooling3D) (10, 8, 8, 8, 256) 0 conv3d_2[0][0]
__________________________________________________________________________________________________
conv3d_3 (Conv3D) (10, 8, 8, 8, 512) 8389120 max_pooling3d_2[0][0]
__________________________________________________________________________________________________
max_pooling3d_3 (MaxPooling3D) (10, 4, 4, 4, 512) 0 conv3d_3[0][0]
__________________________________________________________________________________________________
Geo (InputLayer) [(10, 504)] 0
__________________________________________________________________________________________________
conv3d_4 (Conv3D) (10, 4, 4, 4, 512) 16777728 max_pooling3d_3[0][0]
__________________________________________________________________________________________________
dense (Dense) (10, 512) 258560 Geo[0][0]
__________________________________________________________________________________________________
max_pooling3d_4 (MaxPooling3D) (10, 2, 2, 2, 512) 0 conv3d_4[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (10, 512) 262656 dense[0][0]
__________________________________________________________________________________________________
conv3d_5 (Conv3D) (10, 2, 2, 2, 512) 16777728 max_pooling3d_4[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (10, 512) 262656 dense_1[0][0]
__________________________________________________________________________________________________
max_pooling3d_5 (MaxPooling3D) (10, 1, 1, 1, 512) 0 conv3d_5[0][0]
__________________________________________________________________________________________________
flatten (Flatten) (10, 512) 0 dense_2[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (10, 512) 0 max_pooling3d_5[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (10, 1024) 0 flatten[0][0]
flatten_1[0][0]
__________________________________________________________________________________________________
dense_3 (Dense) (10, 512) 524800 concatenate[0][0]
__________________________________________________________________________________________________
dense_4 (Dense) (10, 128) 65664 dense_3[0][0]
__________________________________________________________________________________________________
dense_5 (Dense) (10, 12) 1548 dense_4[0][0]
==================================================================================================
Total params: 45,682,220
Trainable params: 45,682,220
Non-trainable params: 0
__________________________________________________________________________________________________
И вот ошибка full'i sh (не включая трассировку полного стека):
ValueError: in converted code:
C:\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py:677 map_fn
batch_size=None)
C:\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py:2410 _standardize_tensors
exception_prefix='input')
C:\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_utils.py:582 standardize_input_data
str(data_shape))
ValueError: Error when checking input: expected Geo to have shape (504,) but got array with shape (1,)
Итак, наконец, если кто-то может указать мне, где я иду неправильно, это будет оценено. Если бы они также могли помочь мне понять, почему я получаю эту ошибку, я был бы вечно благодарен. Понимание форм в NN и TF до сих пор сбивает меня с толку.
Спасибо!