(4运行例子)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

简介: ​1、联通ColaB2、运行最基础mnist例子,并且打印图表结果 # https://pypi.python.org/pypi/pydot#!apt-get -qq install -y graphviz && pip install -q pydot#import pydotfrom _...
​1、联通ColaB
2、运行最基础mnist例子,并且打印图表结果 
# https://pypi.python.org/pypi/pydot
#!apt-get -qq install -y graphviz && pip install -q pydot
#import pydot

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.utils import plot_model
import matplotlib.pyplot as plt

batch_size = 128
num_classes = 10
epochs = 12
#epochs = 2

# input image dimensions
img_rows, img_cols = 28, 28

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

if K.image_data_format() == 'channels_first' :
    x_train = x_train.reshape(x_train.shape[ 0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[ 0], 1, img_rows, img_cols)
    input_shape = ( 1, img_rows, img_cols)
else :
    x_train = x_train.reshape(x_train.shape[ 0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[ 0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype( 'float32')
x_test = x_test.astype( 'float32')
x_train /= 255
x_test /= 255
print( 'x_train shape:', x_train.shape)
print(x_train.shape[ 0], 'train samples')
print(x_test.shape[ 0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D( 32, kernel_size =( 3, 3),
                 activation = 'relu',
                 input_shape =input_shape))
model.add(Conv2D( 64, ( 3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size =( 2, 2)))
model.add(Dropout( 0. 25))
model.add(Flatten())
model.add(Dense( 128, activation = 'relu'))
model.add(Dropout( 0. 5))
model.add(Dense(num_classes, activation = 'softmax'))

model. compile(loss =keras.losses.categorical_crossentropy,
              optimizer =keras.optimizers.Adadelta(),
              metrics =[ 'accuracy'])

#log = model.fit(X_train, Y_train,   
#          batch_size=batch_size, nb_epoch=num_epochs,  
#          verbose=1, validation_split=0.1)  

log = model.fit(x_train, y_train,
          batch_size =batch_size,
          epochs =epochs,
          verbose = 1,
          validation_data =(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose = 0)
print( 'Test loss:', score[ 0])
print( 'Test accuracy:', score[ 1])

plt.figure( 'acc')  
plt.subplot( 2, 1, 1)  
plt.plot(log.history[ 'acc'], 'r--',label = 'Training Accuracy')  
plt.plot(log.history[ 'val_acc'], 'r-',label = 'Validation Accuracy')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs, 0. 9, 1])  
plt.figure( 'loss')  
plt.subplot( 2, 1, 2)  
plt.plot(log.history[ 'loss'], 'b--',label = 'Training Loss')  
plt.plot(log.history[ 'val_loss'], 'b-',label = 'Validation Loss')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs, 0, 1])  
  
plt.show() 
3、两句修改成fasion模式 
# https://pypi.python.org/pypi/pydot
#!apt-get -qq install -y graphviz && pip install -q pydot
#import pydot

from __future__ import print_function
import keras
from keras.datasets import fashion_mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.utils import plot_model
import matplotlib.pyplot as plt

batch_size = 128
num_classes = 10
epochs = 12
#epochs = 2

# input image dimensions
img_rows, img_cols = 28, 28

# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

if K.image_data_format() == 'channels_first' :
    x_train = x_train.reshape(x_train.shape[ 0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[ 0], 1, img_rows, img_cols)
    input_shape = ( 1, img_rows, img_cols)
else :
    x_train = x_train.reshape(x_train.shape[ 0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[ 0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype( 'float32')
x_test = x_test.astype( 'float32')
x_train /= 255
x_test /= 255
print( 'x_train shape:', x_train.shape)
print(x_train.shape[ 0], 'train samples')
print(x_test.shape[ 0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Conv2D( 32, kernel_size =( 3, 3),
                 activation = 'relu',
                 input_shape =input_shape))
model.add(Conv2D( 64, ( 3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size =( 2, 2)))
model.add(Dropout( 0. 25))
model.add(Flatten())
model.add(Dense( 128, activation = 'relu'))
model.add(Dropout( 0. 5))
model.add(Dense(num_classes, activation = 'softmax'))

model. compile(loss =keras.losses.categorical_crossentropy,
              optimizer =keras.optimizers.Adadelta(),
              metrics =[ 'accuracy'])

#log = model.fit(X_train, Y_train,   
#          batch_size=batch_size, nb_epoch=num_epochs,  
#          verbose=1, validation_split=0.1)  

log = model.fit(x_train, y_train,
          batch_size =batch_size,
          epochs =epochs,
          verbose = 1,
          validation_data =(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose = 0)
print( 'Test loss:', score[ 0])
print( 'Test accuracy:', score[ 1])

plt.figure( 'acc')  
plt.subplot( 2, 1, 1)  
plt.plot(log.history[ 'acc'], 'r--',label = 'Training Accuracy')  
plt.plot(log.history[ 'val_acc'], 'r-',label = 'Validation Accuracy')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs, 0. 9, 1])  
plt.figure( 'loss')  
plt.subplot( 2, 1, 2)  
plt.plot(log.history[ 'loss'], 'b--',label = 'Training Loss')  
plt.plot(log.history[ 'val_loss'], 'b-',label = 'Validation Loss')  
plt.legend(loc = 'best')  
plt.xlabel( 'Epochs')  
plt.axis([ 0, epochs, 0, 1])  
plt.show() 

4、VGG16&Mnist

5、VGG16迁移学习






目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
目录
相关文章
|
11月前
|
机器学习/深度学习 小程序 算法
基于bp神经网络的adp程序
基于bp神经网络的adp小程序
245 0
|
机器学习/深度学习 编解码 自动驾驶
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
621 3
RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
用MASM32按Time Protocol(RFC868)协议编写网络对时程序中的一些有用的函数代码
用MASM32按Time Protocol(RFC868)协议编写网络对时程序中的一些有用的函数代码
|
机器学习/深度学习 编解码 自动驾驶
YOLOv11改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
YOLOv11改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
457 16
YOLOv11改进策略【模型轻量化】| 替换骨干网络为MoblieNetV1,用于移动视觉应用的高效卷积神经网络
|
机器学习/深度学习 存储 大数据
RT-DETR改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
RT-DETR改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
423 11
RT-DETR改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
|
机器学习/深度学习 存储 大数据
YOLOv11改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
YOLOv11改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
469 0
YOLOv11改进策略【Backbone/主干网络】| ICLR-2023 替换骨干网络为:RevCol 一种新型神经网络设计范式
|
数据采集 监控 安全
公司网络监控软件:Zig 语言底层优化保障系统高性能运行
在数字化时代,Zig 语言凭借出色的底层控制能力和高性能特性,为公司网络监控软件的优化提供了有力支持。从数据采集、连接管理到数据分析,Zig 语言确保系统高效稳定运行,精准处理海量网络数据,保障企业信息安全与业务连续性。
285 4
|
网络协议 物联网 数据处理
C语言在网络通信程序实现中的应用,介绍了网络通信的基本概念、C语言的特点及其在网络通信中的优势
本文探讨了C语言在网络通信程序实现中的应用,介绍了网络通信的基本概念、C语言的特点及其在网络通信中的优势。文章详细讲解了使用C语言实现网络通信程序的基本步骤,包括TCP和UDP通信程序的实现,并讨论了关键技术、优化方法及未来发展趋势,旨在帮助读者掌握C语言在网络通信中的应用技巧。
488 2
|
机器学习/深度学习 自然语言处理 前端开发
前端神经网络入门:Brain.js - 详细介绍和对比不同的实现 - CNN、RNN、DNN、FFNN -无需准备环境打开浏览器即可测试运行-支持WebGPU加速
本文介绍了如何使用 JavaScript 神经网络库 **Brain.js** 实现不同类型的神经网络,包括前馈神经网络(FFNN)、深度神经网络(DNN)和循环神经网络(RNN)。通过简单的示例和代码,帮助前端开发者快速入门并理解神经网络的基本概念。文章还对比了各类神经网络的特点和适用场景,并简要介绍了卷积神经网络(CNN)的替代方案。
2085 1
|
机器学习/深度学习 存储 自然语言处理
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)
深度学习入门:循环神经网络------RNN概述,词嵌入层,循环网络层及案例实践!(万字详解!)

热门文章

最新文章

下一篇
开通oss服务