keras 模型.预测yields更多的预测超过这数字的输出

omjgkv6w  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(111)

我使用CNN创建了一个多类图像分类器。我专门使用keras模块,并使用生成器拟合和预测4个不同类别的图像。我的test_generator有394个示例(所有4个类别的组合),但我的model.predict产生(6304,4)个预测。
以下是模型总结:

Model: "sequential_2"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 IP (Conv2D)                 (None, 64, 64, 32)        320       
                                                                 
 Convolution0 (Conv2D)       (None, 64, 64, 64)        18496     
                                                                 
 PL0 (MaxPooling2D)          (None, 32, 32, 64)        0         
                                                                 
 Convolution1 (Conv2D)       (None, 32, 32, 128)       73856     
                                                                 
 PL1 (MaxPooling2D)          (None, 16, 16, 128)       0         
                                                                 
 Convolution2 (Conv2D)       (None, 16, 16, 256)       295168    
                                                                 
 PL2 (MaxPooling2D)          (None, 8, 8, 256)         0         
                                                                 
 FL (Flatten)                (None, 16384)             0         
                                                                 
 FC (Dense)                  (None, 128)               2097280   
                                                                 
 OP (Dense)                  (None, 4)                 516       
                                                                 
=================================================================
Total params: 2,485,636
Trainable params: 2,485,636
Non-trainable params: 0
_________________________________________________________________

下面是我如何创建test_generator的:test_generator = core_imageDataGenerator(test_directory),并且len(test_generator.classes)的结果是394。
以下是我的预测:predictions = model.predict(test_generator),而predictions.shape的结果是[6304,4],而不是[394,4]。这可能是什么原因?我做错了什么吗?这里我有什么选择,因为我的下一步是创建一个包含各种度量的分类报告。

qv7cva1a

qv7cva1a1#

也就是输入和层数,我看到你使用了平面层,后面跟着密集矩阵,这表示输出层矩阵。
存在返回相同大小的卷积层和密集层,320 -〉18496(32 * 578)-〉MaxPool。
你可以创建一个引擎类似于所附的图片,输出是敏感的你的问题是相似性和比率。(6304,4),(394,4)。
它们在统计方法中很重要,简单地说,您不需要我们为您做的密集层!!!

[样品]:

import tensorflow as tf

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
def predict_action ( dataset ) :
    predictions = model.predict( dataset )
    return predictions

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
input = tf.constant( tf.random.uniform(shape=[5, 2], minval=5, maxval=10, dtype=tf.int64), shape=( 1, 1, 5, 2 ) )
label = tf.constant( [0], shape=( 1, 1, 1 )  )
dataset = tf.data.Dataset.from_tensor_slices(( input, label ))

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
    tf.keras.layers.InputLayer(input_shape=( 5, 2 )),
    tf.keras.layers.Dense( 4 ),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense( 128 ),
    tf.keras.layers.Dense( 4 ),
])  
model.summary()

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
    learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07,
    name='Nadam'
)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit(dataset, epochs=1 ,validation_data=(dataset))
print( predict_action( dataset ) )

[输出]:

Layer (type)                Output Shape              Param #
=================================================================
 dense (Dense)               (None, 5, 4)              12

 flatten (Flatten)           (None, 20)                0

 dense_1 (Dense)             (None, 16)                336

 dense_2 (Dense)             (None, 4)                 68

=================================================================
Total params: 416
Trainable params: 416
Non-trainable params: 0
_________________________________________________________________
1/1 [==============================] - 1s 1s/step - loss: 0.2335 - accuracy: 0.0000e+00 - val_loss: 0.2291 - val_accuracy: 0.0000e+00
[[ 0.07219216 -2.9527428   1.5981569  -5.590222  ]]

相关问题