efficientnet 在keras中的使用

环境配置

python3.6 64bit
keras2.2.4
tensorflow
1.9
efficientnet==1.1.0

参考https://keras.io/api/applications/efficientnet/#efficientnetb4-functionv

连接全连接层图像分类

import efficientnet.tfkeras as efn

model = Sequential()

model.add(efn.EfficientNetB4(
    weights='imagenet',
    input_shape=self.input_shape,
    include_top=False,
    # classes=1000,
))
model.add(GlobalAveragePooling2D())
model.add(Dense(self.captcha_class * self.captcha_length, activation='softmax'))  # classnumber 代表类别个数
model.compile(loss='categorical_crossentropy', optimizer=optimizers.Adam(lr=0.001),
                      metrics=['accuracy', custom_accuracy])
    def custom_accuracy(self, y_true, y_pred):
        predict = tf.reshape(y_pred, [-1, self.captcha_length, self.captcha_class])
        max_idx_p = tf.argmax(predict, 2)  # 这个做法牛逼,不用再做stack和reshape了,2,是在Charset那个维度上
        max_idx_l = tf.argmax(tf.reshape(y_true, [-1, self.captcha_length, self.captcha_class]), 2)
        correct_pred = tf.equal(max_idx_p, max_idx_l)
        _result = tf.map_fn(fn=lambda e: tf.reduce_all(e), elems=correct_pred, dtype=tf.bool)
        return tf.reduce_mean(tf.cast(_result, tf.float32))

欢迎加入小白交流群1135165504,一起学习共同进步,只交流深度学习相关不吹水,有资源大家一起分享

你可能感兴趣的:(深度学习,ocr,验证码破解,深度学习,python,神经网络)