美文网首页
卷积神经网络

卷积神经网络

作者: poteman | 来源:发表于2019-08-04 09:13 被阅读0次
  • 模型
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.models import load_model

# 回调函数
filepath = 'best_model.h5'
callbacks_list = [
  EarlyStopping(
  monitor='val_acc',
  patience=3,
  ),
  ModelCheckpoint(
  filepath=filepath,
  monitor='val_acc',
  save_best_only=True,
  save_weights_only=False
  )
]

# 数据加载
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

# 数据预处理
# 数据维度预处理
training_images=training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)
# 归一化
training_images=training_images / 255.0
test_images=test_images/255.0

# 定义模型
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])

# 编译and训练
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=10, validation_data=(test_images, test_labels), callbacks=callbacks_list)

# 加载最优模型
model = load_model(filepath)

# 评价
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(test_acc)

# 使用模型预测
classifications = model.predict(test_images)
print(classifications[0])
print(test_labels[0])

# 查看
model.summary()
  • 将卷积网络中间层显示出来
import matplotlib.pyplot as plt
from tensorflow.keras import models

f, axarr = plt.subplots(3,4)
FIRST_IMAGE=0
SECOND_IMAGE=7
THIRD_IMAGE=26
CONVOLUTION_NUMBER = 1

layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)

for x in range(0,4):
  f1 = activation_model.predict(test_images[FIRST_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[0,x].imshow(f1[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[0,x].grid(False)
  f2 = activation_model.predict(test_images[SECOND_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[1,x].imshow(f2[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[1,x].grid(False)
  f3 = activation_model.predict(test_images[THIRD_IMAGE].reshape(1, 28, 28, 1))[x]
  axarr[2,x].imshow(f3[0, : , :, CONVOLUTION_NUMBER], cmap='inferno')
  axarr[2,x].grid(False)

相关文章

  • CS231n 卷积神经网络: 架构, 卷积/池化层(上)

    卷积神经网络: 架构, 卷积/池化层(上) 卷积神经网络: 架构, 卷积/池化层(上) 卷积神经网络(CNNs/C...

  • 视觉

    卷积神经网络整理 各种卷积神经网络变形

  • datawhale-task05(卷积神经网络基础;leNet;

    卷积神经网络基础 LeNet和其他进阶卷积神经网络

  • 卷积神经网络

    第七章 卷积神经网络 卷积层(Convolution Layer) 卷积神经网络(Convolutional Ne...

  • 再战机器学习—卷积神经网络

    卷积神经网络 卷积神经网络可能是离我们最近的神经网络,遍布在计算机视觉应用。通常卷积神经网络是由卷积层、池化层和全...

  • 卷积神经网络

    卷积神经网络   卷积神经网络(Convolutional Neural Network,CNN或ConvNet)...

  • 二维卷积运算

    卷积神经网络是含有卷积层(convolutional layer)的神经网络。本章中卷积神经网络均使用最常见的二维...

  • CNN

    参考:CNN卷积神经网络原理讲解+图片识别应用(附源码)卷积神经网络 – CNN深入学习卷积神经网络(CNN)的原...

  • CNN基本算子与操作

    卷积神经网络(Convolutional Neural Networks,CNN)是一种前馈神经网络。卷积神经网络...

  • 卷积层(Convolution Layer)

    卷积神经网络(Convolutional Neural Networks,CNN)是一种前馈神经网络。卷积神经网络...

网友评论

      本文标题:卷积神经网络

      本文链接:https://www.haomeiwen.com/subject/qdyedctx.html