tensorflow学习025——常见的预训练网络模型及使用示例
在ImageNet上与训练过的用于图像分类的模型:VGG16, VGG19, ResNet50, InceptionV3, InceptionResNetV2, Xception, MobileNet, MobileNetV2, DenseNet, NASNet
是在ImageNet上1000个分类的准确率
top1——你预测的label取最后概率向量里面最大的那一个作为预测结果,如果你的预测结果中概率最大的那个分类正确,则预测正确,否则预测错误
top5——就是最后概率向量最大的前五名中,只要出现了正确预测,则为预测准确,否则预测错误。
从上面途中我们可以看出,VGG16 VGG19是比较落后的,训练参数多但是准确率低。MobileNet MobileNetV2 仅仅十几M,但是精度也不低,可以部署于手机上。
在ImageNet上预训练的XceptionV1模型,在ImageNet上,该模型取得了验证集top1 0.790和top 5 0.945的准确率。需要注意的是该模型只支持channels_last的维度顺序(高度、宽度、通道),该模型默认输入尺寸是299*299
其它训练网络参数可通过网址(https://keras.io/zh/applications)中查看。
Xception网络训练猫狗数据集代码
数据链接:https://pan.baidu.com/s/1-nLlW6Nng1pAvrxwEfHttA
提取码:vt8p
点击查看代码
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import glob
import os
train_image_path = glob.glob(r'E:\WORK\tensorflow\dataset\dc_2000\train\*\*.jpg')
print(len(train_image_path))
print(train_image_path[:5])
train_image_label = [int(p.split("\\")[-2] == 'cat') for p in train_image_path]
def load_preprosess_image(path,label):
image = tf.io.read_file(path)
image = tf.image.decode_jpeg(image,channels=3)
image = tf.image.resize(image,[256,256])
image = tf.cast(image,tf.float32)
image = image / 255
return image,label
train_image_ds = tf.data.Dataset.from_tensor_slices((train_image_path,train_image_label))
AUTOTUNE = tf.data.experimental.AUTOTUNE
train_image_ds = train_image_ds.map(load_preprosess_image,num_parallel_calls=AUTOTUNE)
BATCH_SZIE = 32
train_count = len(train_image_path)
train_image_ds = train_image_ds.shuffle(train_count).repeat().batch(BATCH_SZIE)
test_image_path = glob.glob(r'E:\WORK\tensorflow\dataset\dc_2000\test\*\*.jpg')
test_image_label = [int(p.split("\\")[-2] == 'cat') for p in test_image_path]
test_image_ds = tf.data.Dataset.from_tensor_slices((test_image_path,test_image_label))
test_image_ds = test_image_ds.map(load_preprosess_image,num_parallel_calls=AUTOTUNE)
test_image_ds = test_image_ds.repeat().batch(BATCH_SZIE)
test_count = len(test_image_path)
conv_base = tf.keras.applications.xception.Xception(weights='imagenet',
include_top=False,
input_shape=(256,256,3),
pooling='avg')
conv_base.trainable = False
model = tf.keras.Sequential()
model.add(conv_base)
model.add(tf.keras.layers.Dense(512,activation='relu'))
model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),
loss='binary_crossentropy',
metrics=['acc'])
initial_epoches = 5
histroy = model.fit(train_image_ds,
steps_per_epoch=train_count//BATCH_SZIE,
epochs=initial_epoches,
validation_data=test_image_ds,
validation_steps=test_count//BATCH_SZIE)
conv_base.trainable = True
for layer in conv_base.layers[:-33]: # 原先弓133层
layer.trainable = False
model.compile(loss='binary_crossentropy',
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005/10),
metrics=['accuracy'])
fine_tune_epoches = 5
total_epoches = initial_epoches + fine_tune_epoches
histroy = model.fit(train_image_ds,
steps_per_epoch=train_count//BATCH_SZIE,
epochs=total_epoches,
initial_epoch=initial_epoches,
validation_data=test_image_ds,
validation_steps=test_count//BATCH_SZIE)
作者:孙建钊
出处:http://www.cnblogs.com/sunjianzhao/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。