tensorflow笔记1
搭建一个神经网络,训练cifar10数据集,搭建一个一层卷积(卷积核6 * 5 * 5conv,pool(2 * 2 ),步长2 ),2 层全连接的网络(dense128,dense10)
cifar10数据集一共6 万张彩色图片,5 万张32 * 32 像素点的图片核标签用于训练,1 万张用于测试。里面由10 个分类,飞机、汽车、鸟、猫、狗。。。。分别对应标签0 - 9
神经网络的一个基本模型
import tensorflow as tf
import os
import numpy as np
from matplotlib import pyplot as plt
from tensorflow. keras. layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Dropout, Flatten, Dense
from tensorflow. keras import Model
np. set_printoptions( threshold= np. inf)
cifar10 = tf. keras. datasets. cifar10
( x_train, y_train) , ( x_test, y_test) = cifar10. load_data( )
x_train, x_test = x_train / 255.0 , x_test / 255.0
class Baseline ( Model) :
def __init__ ( self) :
super ( Baseline, self) . __init__( )
self. c1 = Conv2D( filters= 6 , kernel_size= ( 5 , 5 ) , padding= 'same' )
self. b1 = BatchNormalization( )
self. a1 = Activation( 'relu' )
self. p1 = MaxPool2D( pool_size= ( 2 , 2 ) , strides= 2 , padding= 'same' )
self. d1 = Dropout( 0.2 )
self. flatten = Flatten( )
self. f1 = Dense( 128 , activation= 'relu' )
self. d2 = Dropout( 0.2 )
self. f2 = Dense( 10 , activation= 'softmax' )
def call ( self, x) :
x = self. c1( x)
x = self. b1( x)
x = self. a1( x)
x = self. p1( x)
x = self. d1( x)
x = self. flatten( x)
x = self. f1( x)
x = self. d2( x)
y = self. f2( x)
return y
model = Baseline( )
model. compile ( optimizer= 'adam' ,
loss= tf. keras. losses. SparseCategoricalCrossentropy( from_logits= False ) ,
metrics= [ 'sparse_categorical_accuracy' ] )
checkpoint_save_path = "./checkpoint/Baseline.ckpt"
if os. path. exists( checkpoint_save_path + '.index' ) :
print ( '-------------load the model-----------------' )
model. load_weights( checkpoint_save_path)
cp_callback = tf. keras. callbacks. ModelCheckpoint( filepath= checkpoint_save_path,
save_weights_only= True ,
save_best_only= True )
history = model. fit( x_train, y_train, batch_size= 32 , epochs= 5 , validation_data= ( x_test, y_test) , validation_freq= 1 , callbacks= [ cp_callback] )
model. summary( )
file = open ( './weights.txt' , 'w' )
for v in model. trainable_variables:
file . write( str ( v. name) + '\n' )
file . write( str ( v. shape) + '\n' )
file . write( str ( v. numpy( ) ) + '\n' )
file . close( )
acc = history. history[ 'sparse_categorical_accuracy' ]
val_acc = history. history[ 'val_sparse_categorical_accuracy' ]
loss = history. history[ 'loss' ]
val_loss = history. history[ 'val_loss' ]
plt. subplot( 1 , 2 , 1 )
plt. plot( acc, label= 'Training Accuracy' )
plt. plot( val_acc, label= 'Validation Accuracy' )
plt. title( 'Training and Validation Accuracy' )
plt. legend( )
plt. subplot( 1 , 2 , 2 )
plt. plot( loss, label= 'Training Loss' )
plt. plot( val_loss, label= 'Validation Loss' )
plt. title( 'Training and Validation Loss' )
plt. legend( )
plt. show( )
class LeNet5 ( Model) :
def __init__ ( self) :
super ( LeNet5, self) . __init__( )
self. c1 = Conv2D( filters= 6 , kernel_size= ( 5 , 5 ) , activation= 'sigmoid' )
self. p1 = MaxPool2D( pool_size= ( 2 , 2 ) , strides= 2 )
self. c2 = Conv2D( filters= 16 , kernel_size= ( 5 , 5 ) , activation= 'sigmoid' )
self. p2 = MaxPool2D( pool_size= ( 2 , 2 ) , strides= 2 )
self. flatten = Flatten( )
self. f1 = Dense( 120 , activation= 'sigmoid' )
self. f2 = Dense( 84 , activation= 'sigmoid' )
self. f3 = Dense( 10 , activation= 'softmax' )
def call ( self, x) :
x = self. c1( x)
x = self. p1( x)
x = self. c2( x)
x = self. p2( x)
x = self. flatten( x)
x = self. f1( x)
x = self. f2( x)
y = self. f3( x)
return y
model = LeNet5( )
class AlexNet8 ( Model) :
def __init__ ( self) :
super ( AlexNet8, self) . __init__( )
self. c1 = Conv2D( filters= 96 , kernel_size= ( 3 , 3 ) )
self. b1 = BatchNormalization( )
self. a1 = Activation( 'relu' )
self. p1 = MaxPool2D( pool_size= ( 3 , 3 ) , strides= 2 )
self. c2 = Conv2D( filters= 256 , kernel_size= ( 3 , 3 ) )
self. b2 = BatchNormalization( )
self. a2 = Activation( 'relu' )
self. p2 = MaxPool2D( pool_size= ( 3 , 3 ) , strides= 2 )
self. c3 = Conv2D( filters= 384 , kernel_size= ( 3 , 3 ) , padding= 'same' , activation= 'relu' )
self. c4 = Conv2D( filters= 384 , kernel_size= ( 3 , 3 ) , padding= 'same' , activation= 'relu' )
self. c5 = Conv2D( filters= 256 , kernel_size= ( 3 , 3 ) , padding= 'same' , activation= 'relu' )
self. p3 = MaxPool2D( pool_size= ( 3 , 3 ) , strides= 2 )
self. flatten = Flatten( )
self. f1 = Dense( 2048 , activation= 'relu' )
self. d1 = Dropout( 0.5 )
self. f2 = Dense( 2048 , activation= 'relu' )
self. d2 = Dropout( 0.5 )
self. f3 = Dense( 10 , activation= 'softmax' )
def call ( self, x) :
x = self. c1( x)
x = self. b1( x)
x = self. a1( x)
x = self. p1( x)
x = self. c2( x)
x = self. b2( x)
x = self. a2( x)
x = self. p2( x)
x = self. c3( x)
x = self. c4( x)
x = self. c5( x)
x = self. p3( x)
x = self. flatten( x)
x = self. f1( x)
x = self. d1( x)
x = self. f2( x)
x = self. d2( x)
y = self. f3( x)
return y
model = AlexNet8( ) VGG 16 / 19 层
基本单元:Inception结构快,在同一层网络中使用了多个尺寸的卷积核,可以提取不同尺寸的特征。通过1 * 1 卷积核,
设定少于输入特征图深度的1 * 1 卷积核个数,起到降维的作用减少了计算量。结构快由4 个分支组成,分别经过1 * 1 卷积核输出到卷积连接器. . . . . . . .
卷积连接器把收到的四路特征数据按深度方向拼接,形成结构快的输出。
class ConvBNRelu ( Model) :
def __init__ ( self, ch, kernelsz= 3 , strides= 1 , padding= 'same' ) :
super ( ConvBNRelu, self) . __init__( )
self. model = tf. keras. models. Sequential( [
Conv2D( ch, kernelsz, strides= strides, padding= padding) ,
BatchNormalization( ) ,
Activation( 'relu' )
] )
def call ( self, x) :
x = self. model( x, training= False )
return x
class InceptionBlk ( Model) :
def __init__ ( self, ch, strides= 1 ) :
super ( InceptionBlk, self) . __init__( )
self. ch = ch
self. strides = strides
self. c1 = ConvBNRelu( ch, kernelsz= 1 , strides= strides)
self. c2_1 = ConvBNRelu( ch, kernelsz= 1 , strides= strides)
self. c2_2 = ConvBNRelu( ch, kernelsz= 3 , strides= 1 )
self. c3_1 = ConvBNRelu( ch, kernelsz= 1 , strides= strides)
self. c3_2 = ConvBNRelu( ch, kernelsz= 5 , strides= 1 )
self. p4_1 = MaxPool2D( 3 , strides= 1 , padding= 'same' )
self. c4_2 = ConvBNRelu( ch, kernelsz= 1 , strides= strides)
def call ( self, x) :
x1 = self. c1( x)
x2_1 = self. c2_1( x)
x2_2 = self. c2_2( x2_1)
x3_1 = self. c3_1( x)
x3_2 = self. c3_2( x3_1)
x4_1 = self. p4_1( x)
x4_2 = self. c4_2( x4_1)
x = tf. concat( [ x1, x2_2, x3_2, x4_2] , axis= 3 )
return x
class Inception10 ( Model) :
def __init__ ( self, num_blocks, num_classes, init_ch= 16 , ** kwargs) :
super ( Inception10, self) . __init__( ** kwargs)
self. in_channels = init_ch
self. out_channels = init_ch
self. num_blocks = num_blocks
self. init_ch = init_ch
self. c1 = ConvBNRelu( init_ch)
self. blocks = tf. keras. models. Sequential( )
for block_id in range ( num_blocks) :
for layer_id in range ( 2 ) :
if layer_id == 0 :
block = InceptionBlk( self. out_channels, strides= 2 )
else :
block = InceptionBlk( self. out_channels, strides= 1 )
self. blocks. add( block)
self. out_channels *= 2
self. p1 = GlobalAveragePooling2D( )
self. f1 = Dense( num_classes, activation= 'softmax' )
def call ( self, x) :
x = self. c1( x)
x = self. blocks( x)
x = self. p1( x)
y = self. f1( x)
return y
model = Inception10( num_blocks= 2 , num_classes= 10 )