环境

tensorflow 2.1
最好用GPU

Cifar10数据集

CIFAR-10 数据集的分类是机器学习中一个公开的基准测试问题。任务的目标对一组32x32 RGB的图像进行分类,这个数据集涵盖了10个类别:飞机, 汽车, 鸟, 猫, 鹿, 狗, 青蛙, 马, 船以及卡车。

下面代码仅仅只是做显示Cifar10数据集用

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tfdef showPic(X_train, y_train):# 看看数据集中的一些样本:每个类别展示一些classes = ['plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']num_classes = len(classes)samples_per_class = 7for y, cls in enumerate(classes):idxs = np.flatnonzero(y_train == y)# 一个类别中挑出一些idxs = np.random.choice(idxs, samples_per_class, replace=False)for i, idx in enumerate(idxs):plt_idx = i * num_classes + y + 1plt.subplot(samples_per_class, num_classes, plt_idx)plt.imshow(X_train[idx].astype('uint8'))plt.axis('off')if i == 0:plt.title(cls)plt.show()
if __name__ == '__main__':(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()showPic(x_train, y_train)

模型

Resnet:把前一层的数据直接加到下一层里。减少数据在传播过程中过多的丢失。
SENet: 学习每一层的通道之间的关系
Inception: 每一层都用不同的核(1x1,3x3,5x5)来学习.防止因为过小的核或者过大的核而学不到图片的特征。
用Resnet ,SENet, Inceptiont网络训练Cifar10 或者Cifar 100.

训练数据:Cifar10 或者 Cifar 100
训练集上准确率:97.11%左右
验证集上准确率:90.22%左右
测试集上准确率:88.6%
训练时间在GPU上:一小时多
权重大小:21.8 MB

训练的历程

普通网络(65%左右)-> 数据增强(70%左右)->模型增强(进入Resnet 和SEnet) 80%左右 -> 模型的结构做了调整(86% )- > 加inception 模型后(88.6%)

训练集与验证集上的测试结果:

347/351 [============================>.] - ETA: 0s - loss: 0.0827 - sparse_categorical_accuracy: 0.9711
348/351 [============================>.] - ETA: 0s - loss: 0.0828 - sparse_categorical_accuracy: 0.9710
349/351 [============================>.] - ETA: 0s - loss: 0.0827 - sparse_categorical_accuracy: 0.9710
350/351 [============================>.] - ETA: 0s - loss: 0.0826 - sparse_categorical_accuracy: 0.9711
351/351 [==============================] - 20s 57ms/step - loss: 0.0826 - sparse_categorical_accuracy: 0.9711 - val_loss: 0.3952 - val_sparse_categorical_accuracy: 0.9022

测试集上的结果

79/79 - 4s - loss: 0.4005 - sparse_categorical_accuracy: 0.8842
[0.40052215495630156, 0.8842]
time 4.130274534225464

下面是完整的代码,运行前建一下这个目录weights4_5,不想写代码自动化建了。
如果要训练Cifar100,直接把cifar10 改成cifar100就可以了。不需要改其它地方

import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
import time as time
import tensorflow.keras.preprocessing.image as image
import matplotlib.pyplot as plt
import osdef senet_block(inputs, ratio):shape = inputs.shapechannel_out = shape[-1]# print(shape)# (2, 28, 28, 32) , [1,28,28,1], [1,28,28,1]squeeze = layers.GlobalAveragePooling2D()(inputs)# [2, 1, 1, 32]# print(squeeze.shape)# 第二层,全连接层# [2,32]# print(squeeze.shape)shape_result = layers.Flatten()(squeeze)# print(shape_result.shape)# [32,2]shape_result = layers.Dense(int(channel_out / ratio), activation='relu')(shape_result)# shape_result = layers.BatchNormalization()(shape_result)# [2,32]shape_result = layers.Dense(channel_out, activation='sigmoid')(shape_result)# shape_result = layers.BatchNormalization()(shape_result)# 第四层,点乘# print('heres2')excitation_output = tf.reshape(shape_result, [-1, 1, 1, channel_out])# print(excitation_output.shape)h_output = excitation_output * inputsreturn h_outputdef inception_block(input, input_filter, output_filter):reception_filter  =   output_filterprint('reception_filter',reception_filter)r1 = layers.Conv2D(filters=reception_filter, kernel_size=(1, 1), activation='relu', padding='same')(input)r1 = layers.BatchNormalization()(r1 )r1 = senet_block(r1, 8)r3 = layers.Conv2D(filters=reception_filter, kernel_size=(3, 3), activation='relu', padding='same')(input)r3 = layers.BatchNormalization()(r3 )r3 = senet_block(r3, 8)r5 = layers.Conv2D(filters=reception_filter, kernel_size=(5, 5), activation='relu', padding='same')(input)r5 = layers.BatchNormalization()(r5 )r5 = senet_block(r5, 8)mx = tf.keras.layers.MaxPool2D(pool_size=(3, 3), strides=1, padding="same")(input)mx = layers.Conv2D(filters=reception_filter, kernel_size=(1, 1), activation='relu', padding='same')(mx)output = tf.keras.layers.concatenate([r1, r3, r5,mx], axis=-1)output = layers.Conv2D(filters=reception_filter, kernel_size=(3, 3), activation='relu', padding='same')(output)print('output',output.shape)return outputdef res_block(input, input_filter, output_filter):# input = 32, output = 96res_x = inception_block(input, input_filter, output_filter)# 96res_x = layers.Conv2D(filters=output_filter, kernel_size=(3, 3), activation=None, padding='same')(res_x )res_x = layers.BatchNormalization()(res_x )res_x = senet_block(res_x, 8)if input_filter == output_filter:identity = inputelse: #需要升维或者降维identity = layers.Conv2D(filters=output_filter, kernel_size=(1,1), padding='same')(input)print(identity.shape)print(res_x.shape)x = layers.Add()([identity, res_x])output = layers.Activation('relu')(x)return outputdef my_model():inputs = keras.Input(shape=(32,32,3), name='img')h1 = layers.Conv2D(filters=16, kernel_size=(3, 3), strides=(1, 1), padding='same', activation='relu')(inputs)h1 = layers.BatchNormalization()(h1)h1 = senet_block(h1, 8)block1_out = res_block(h1, 16, 32)block2_out = layers.MaxPool2D(pool_size=(2, 2))(block1_out)block2_out = res_block(block2_out, 32,64)block3_out = layers.MaxPool2D(pool_size=(2, 2))(block2_out)block3_out = res_block(block3_out, 64,128)block4_out = layers.MaxPool2D(pool_size=(2, 2))(block3_out)block4_out = res_block(block4_out, 128,256)h3 = layers.GlobalAveragePooling2D()(block4_out)h3 = layers.Flatten()(h3)h3 = layers.BatchNormalization()(h3)h3 = layers.Dense(64, activation='relu')(h3)h3 = layers.BatchNormalization()(h3)outputs = layers.Dense(10, activation='softmax')(h3)deep_model = keras.Model(inputs, outputs, name='resnet')deep_model.compile(optimizer=keras.optimizers.Adam(),loss=keras.losses.SparseCategoricalCrossentropy(),#metrics=['accuracy'])metrics=[keras.metrics.SparseCategoricalAccuracy()])deep_model.summary()#keras.utils.plot_model(deep_model, 'my_resNet.png', show_shapes=True)return deep_modelcurrent_max_loss = 9999weight_file='./weights4_5/model.h5'def train_my_model(deep_model):(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()train_datagen = image.ImageDataGenerator(rescale=1 / 255,rotation_range=40,  # 角度值,0-180.表示图像随机旋转的角度范围width_shift_range=0.2,  # 平移比例,下同height_shift_range=0.2,shear_range=0.2,  # 随机错切变换角度zoom_range=0.2,  # 随即缩放比例horizontal_flip=True,  # 随机将一半图像水平翻转fill_mode='nearest'  # 填充新创建像素的方法)test_datagen = image.ImageDataGenerator(rescale=1 / 255)validation_datagen = image.ImageDataGenerator(rescale=1 / 255)train_generator = train_datagen.flow(x_train[:45000], y_train[:45000], batch_size=128)# train_generator = train_datagen.flow(x_train, y_train, batch_size=128)validation_generator = validation_datagen.flow(x_train[45000:], y_train[45000:], batch_size=128)test_generator = test_datagen.flow(x_test, y_test, batch_size=128)begin_time = time.time()if os.path.isfile(weight_file):print('load weight')deep_model.load_weights(weight_file)def save_weight(epoch, logs):global current_max_lossif(logs['val_loss'] is not None and  logs['val_loss']< current_max_loss):current_max_loss = logs['val_loss']print('save_weight', epoch, current_max_loss)deep_model.save_weights(weight_file)batch_print_callback = keras.callbacks.LambdaCallback(on_epoch_end=save_weight)callbacks = [tf.keras.callbacks.EarlyStopping(patience=4, monitor='loss'),batch_print_callback,# keras.callbacks.ModelCheckpoint('./weights/model.h5', save_best_only=True),tf.keras.callbacks.TensorBoard(log_dir='logs4_4')]print(train_generator[0][0].shape)history = deep_model.fit_generator(train_generator, steps_per_epoch=351, epochs=200, callbacks=callbacks,validation_data=validation_generator, validation_steps=39, initial_epoch = 0)if (history.history['val_loss'] is not None and history.history['val_loss'] < current_max_loss):current_max_loss = history['val_loss']print('save_weight', current_max_loss)deep_model.save_weights(weight_file)result = deep_model.evaluate_generator(test_generator, verbose=2)print(result)print('time', time.time() - begin_time)def show_result(history):plt.plot(history.history['loss'])plt.plot(history.history['val_loss'])plt.plot(history.history['sparse_categorical_accuracy'])plt.plot(history.history['val_sparse_categorical_accuracy'])plt.legend(['loss', 'val_loss', 'sparse_categorical_accuracy', 'val_sparse_categorical_accuracy'],loc='upper left')plt.show()print(history)show_result(history)
def test_module(deep_model):(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()test_datagen = image.ImageDataGenerator(rescale=1 / 255)test_generator = test_datagen.flow(x_test, y_test, batch_size=128)begin_time = time.time()if os.path.isfile(weight_file):print('load weight')deep_model.load_weights(weight_file)result = deep_model.evaluate_generator(test_generator, verbose=2)print(result)print('time', time.time() - begin_time)def predict_module(deep_model):(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()import numpy as npif os.path.isfile(weight_file):print('load weight')deep_model.load_weights(weight_file)print(y_test[0:20])for i in range(20):img = x_test[i][np.newaxis, :]/255y_ = deep_model.predict(img)v  = np.argmax(y_)print(v, y_test[i])if __name__ == '__main__':deep_model = my_model()train_my_model(deep_model)#test_module(deep_model)#predict_module(deep_model)

参考训练结果

执行下面命令,访问http://localhost:6006/查看训练过程中,准确率和损失函数变化过程,

tensorboard --logdir=logs4_4


注意点

训练完后,如果你要测试你的训练好的模型就把test_module(deep_model)代码注释打开

if __name__ == '__main__':deep_model = my_model()#train_my_model(deep_model)test_module(deep_model)#predict_module(deep_model)

如果你要进一步预测你的模型就把predict_module(deep_model)代码注释打开

if __name__ == '__main__':deep_model = my_model()#train_my_model(deep_model)#test_module(deep_model)predict_module(deep_model)

[深度学习-TF2实践]应用Tensorflow2.x训练ResNet,SeNet和Inception模型在cifar10,测试集上准确率88.6%相关推荐

  1. [深度学习-TF2实践]应用Tensorflow2.x训练DenseNet模型在Cifar10数据上,测试集准确率90.07%

    所需环境 tensorflow 2.1 最好用GPU import tensorflow as tf print(tf.__version__) 2.1.0 Cifar10数据集 CIFAR-10 数 ...

  2. 深度学习工程实践 6. 使用pytorch训练自己的眼球分割模型

    深度学习工程实践 6. 使用pytorch训练自己的眼球分割模型 1. 概述 2. 目标 3. 工程实践 3.1 数据寻找,数据标注 3.2 训练 3.3 部署应用到桌面程序 4. 总结 1. 概述 ...

  3. [深度学习-实践]Tensorflow 2.x应用ResNet SeNet网络训练cifar10数据集的模型在测试集上准确率 86%-87%,含完整代码

    环境 tensorflow 2.1 最好用GPU Cifar10数据集 CIFAR-10 数据集的分类是机器学习中一个公开的基准测试问题.任务的目标对一组32x32 RGB的图像进行分类,这个数据集涵 ...

  4. 训练集山准确率高测试集上准确率很低_拒绝DNN过拟合,谷歌准确预测训练集与测试集泛化差异,还开源了数据集 | ICLR 2019...

    鱼羊 发自 凹非寺 量子位 报道 | 公众号 QbitAI 深度神经网络(DNN)如今已经无处不在,从下围棋到打星际,DNN已经渗透到图像识别.图像分割.机器翻译等各种领域,并且总是表现惊艳. 然而, ...

  5. 训练集山准确率高测试集上准确率很低_推荐算法改版前的AB测试

    编辑导语:所谓推荐算法就是利用用户的一些行为,通过一些数学算法,推测出用户可能喜欢的东西:如今很多软件都有这样的操作,对于此系统的设计也会进行测试:本文作者分享了关于推荐算法改版前的AB测试,我们一起 ...

  6. 微软开源深度学习优化库 DeepSpeed,可训练 1000 亿参数的模型

    人工智能的最新趋势是,更大的自然语言模型可以提供更好的准确性,但是由于成本.时间和代码集成的障碍,较大的模型难以训练.微软日前开源了一个深度学习优化库 DeepSpeed,通过提高规模.速度.可用性并 ...

  7. 深度学习框架tensorflow二实战(训练一个简单二分类模型)

    导入工具包 import os import warnings warnings.filterwarnings("ignore") import tensorflow as tf ...

  8. [深度学习TF2][RNN-LSTM]文本情感分析包含(数据预处理-训练-预测)

    基于LSTM的文本情感分析 0. 前言 1. 数据下载 2. 训练数据介绍 3. 用到Word2Vector介绍 wordsList.npy介绍 wordVectors.npy介绍 4 数据预处理 4 ...

  9. Inception网络 运行在Cifar10 测试集87.88% Tensorflow 2.1 小白从代码实践中 理解

    环境 tensorflow 2.1 最好用GPU 模型 Inception 训练数据 Cifar10 或者 Cifar 100 训练集上准确率:93%左右 验证集上准确率:88%左右 测试集上准确率: ...

最新文章

  1. 获取子iframe的属性
  2. java this context,java – Spring XML中applicationcontext的“this”引用
  3. Demo4 Slides.js的使用
  4. [智力考题]比尔盖茨只有3分的考题
  5. java获取jsp_JSP、JAVA获取各种路径总结
  6. 数据特征分析-正太分布
  7. Java学习笔记3——集合框架
  8. zemax设计35mm镜头_ZEMAX怎样优化MTF?
  9. linux定义getch函数
  10. 高速公路坐标高程计算软件3.1版发布
  11. pytz 太平洋时区_使用pytz模块打印所有时区的Python程序
  12. 计算机无法添加本地策略组,解决win10找不到本地组策略和本地用户的方法
  13. React 18 新特性-入门教程
  14. 正弦波叠加成方波--Python简易版
  15. [乡土民间故事_徐苟三传奇]第廿三回_吃蒸肉财主受捉弄
  16. 使用电脑过程中突然无法复制粘贴了
  17. Python判断是否为回文数的方法
  18. HDFS读写流程以及多节点、单节点磁盘负载均衡
  19. Github项目分享——Vue随机刷装备小游戏
  20. 【C++】结构体数组

热门文章

  1. TCP连接(Time_Wait、Close_Wait)说明
  2. cacti登录密码忘记解决方法
  3. 面试精讲之面试考点及大厂真题 - 分布式专栏 17 ElasticSearch解决大数据量检索难题
  4. 容器编排技术 -- Kubernetes kubectl create rolebinding 命令详解
  5. C语言,利用条件语句判断数的奇偶性。
  6. 「Flink」使用Managed Keyed State实现计数窗口功能
  7. ajax前端实时获取数据
  8. 200-电影-《The secret life of Walter Mitty》
  9. nginx 1.16 配置反向代理,http,https,ssl
  10. 如何保护移动应用程序安全–移动应用程序安全检查表