CNN无法使用功能性API训练TF 2.0

时间:2020-02-14 15:09:00

标签: python tensorflow machine-learning keras deep-learning

我正在尝试在this数据集上训练CNN。

我已经使用以下功能解析了图像:

def parse_image(img, lbl):
image = tf.io.read_file(img)
image = tf.image.decode_jpeg(image)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [50, 50])
image = tf.reshape(image, [-1, 50, 50, 3])
return image, tf.reshape(lbl, [-1, 1])

我的模型如下:

_input = tf.keras.Input(shape=(50, 50, 3), name='img')
x = tf.keras.layers.Conv2D(32, 3, activation='relu')(_input)
x = tf.keras.layers.Conv2D(64, 3, activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(2)(x)
x = tf.keras.layers.Conv2D(64, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(64, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(128, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(128, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(128, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(128, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(256, 3, activation='relu')(x)
x = tf.keras.layers.Conv2D(256, 3, activation='relu')(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(100, activation='relu', use_bias= True, bias_initializer='he_normal')(x)
_output = tf.keras.layers.Dense(1, activation='softmax',  use_bias= True, bias_initializer='he_normal')(x)
model = keras.Model(_input, _output, name='cnn')
model.summary()
Model: "cnn"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
img (InputLayer)             [(None, 50, 50, 3)]       0         
_________________________________________________________________
conv2d_32 (Conv2D)           (None, 48, 48, 32)        896       
_________________________________________________________________
conv2d_33 (Conv2D)           (None, 46, 46, 64)        18496     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 23, 23, 64)        0         
_________________________________________________________________
conv2d_34 (Conv2D)           (None, 21, 21, 64)        36928     
_________________________________________________________________
conv2d_35 (Conv2D)           (None, 19, 19, 64)        36928     
_________________________________________________________________
conv2d_36 (Conv2D)           (None, 17, 17, 128)       73856     
_________________________________________________________________
conv2d_37 (Conv2D)           (None, 15, 15, 128)       147584    
_________________________________________________________________
conv2d_38 (Conv2D)           (None, 13, 13, 128)       147584    
_________________________________________________________________
conv2d_39 (Conv2D)           (None, 11, 11, 128)       147584    
_________________________________________________________________
conv2d_40 (Conv2D)           (None, 9, 9, 256)         295168    
_________________________________________________________________
conv2d_41 (Conv2D)           (None, 7, 7, 256)         590080    
_________________________________________________________________
flatten_4 (Flatten)          (None, 12544)             0         
_________________________________________________________________
dense_7 (Dense)              (None, 100)               1254500   
_________________________________________________________________
dense_8 (Dense)              (None, 1)                 101       
=================================================================
Total params: 2,749,705
Trainable params: 2,749,705

尝试训练模型时,损耗保持恒定,并且精度为零。

model.compile(tf.keras.optimizers.Adam(lr=0.001), loss='binary_crossentropy', 
              metrics=['accuracy'])
model.fit(train_data,
          epochs=2)
Epoch 1/2
1/1 [==============================] - 1s 727ms/step - loss: 15.3332 - accuracy: 0.0000e+00
Epoch 2/2
1/1 [==============================] - 0s 43ms/step - loss: 15.3332 - accuracy: 0.0000e+00

我不确定我做错了什么,因为我在相同的CNN设置和Subclassing API中使用了相同的parse_image函数,但仍能够训练模型。

谢谢。

1 个答案:

答案 0 :(得分:0)

当只有一个输出用于二进制分类时,请勿使用Softmax激活,而应使用Sigmoid:

_output = tf.keras.layers.Dense(1, activation='sigmoid',  use_bias= True, bias_initializer='he_normal')(x)