吴恩达作业11:残差网络实现手势数字的识别(基于 keras)+tensorbord显示loss值和acc值

一,残差网络实现手写数字识别

数据集地址:https://download.csdn.net/download/fanzonghao/10551018

首先来resnets_utils.py,里面有手势数字的数据集载入函数和随机产生mini-batch的函数,代码如下:

import os
import numpy as np
import tensorflow as tf
import h5py
import mathdef load_dataset():train_dataset = h5py.File('datasets/train_signs.h5', "r")train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set featurestrain_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels# print(train_set_x_orig.shape)# print(train_set_y_orig.shape)test_dataset = h5py.File('datasets/test_signs.h5', "r")test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set featurestest_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels# print(test_set_x_orig.shape)# print(test_set_y_orig.shape)classes = np.array(test_dataset["list_classes"][:]) # the list of classestrain_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes#load_dataset()
def random_mini_batches(X, Y, mini_batch_size = 64, seed = 0):"""Creates a list of random minibatches from (X, Y)Arguments:X -- input data, of shape (input size, number of examples) (m, Hi, Wi, Ci)Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples) (m, n_y)mini_batch_size - size of the mini-batches, integerseed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.Returns:mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)"""m = X.shape[0]                  # number of training examplesmini_batches = []np.random.seed(seed)# Step 1: Shuffle (X, Y)permutation = list(np.random.permutation(m))shuffled_X = X[permutation,:,:,:]shuffled_Y = Y[permutation,:]# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.num_complete_minibatches = math.floor(m/mini_batch_size) # number of mini batches of size mini_batch_size in your partitionningfor k in range(0, num_complete_minibatches):mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:,:,:]mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)# Handling the end case (last mini-batch < mini_batch_size)if m % mini_batch_size != 0:mini_batch_X = shuffled_X[num_complete_minibatches * mini_batch_size : m,:,:,:]mini_batch_Y = shuffled_Y[num_complete_minibatches * mini_batch_size : m,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)return mini_batchesdef convert_to_one_hot(Y, C):Y = np.eye(C)[Y.reshape(-1)].Treturn Ydef forward_propagation_for_predict(X, parameters):"""Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAXArguments:X -- input dataset placeholder, of shape (input size, number of examples)parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"the shapes are given in initialize_parametersReturns:Z3 -- the output of the last LINEAR unit"""# Retrieve the parameters from the dictionary "parameters" W1 = parameters['W1']b1 = parameters['b1']W2 = parameters['W2']b2 = parameters['b2']W3 = parameters['W3']b3 = parameters['b3'] # Numpy Equivalents:Z1 = tf.add(tf.matmul(W1, X), b1)                      # Z1 = np.dot(W1, X) + b1A1 = tf.nn.relu(Z1)                                    # A1 = relu(Z1)Z2 = tf.add(tf.matmul(W2, A1), b2)                     # Z2 = np.dot(W2, a1) + b2A2 = tf.nn.relu(Z2)                                    # A2 = relu(Z2)Z3 = tf.add(tf.matmul(W3, A2), b3)                     # Z3 = np.dot(W3,Z2) + b3return Z3def predict(X, parameters):W1 = tf.convert_to_tensor(parameters["W1"])b1 = tf.convert_to_tensor(parameters["b1"])W2 = tf.convert_to_tensor(parameters["W2"])b2 = tf.convert_to_tensor(parameters["b2"])W3 = tf.convert_to_tensor(parameters["W3"])b3 = tf.convert_to_tensor(parameters["b3"])params = {"W1": W1,"b1": b1,"W2": W2,"b2": b2,"W3": W3,"b3": b3}x = tf.placeholder("float", [12288, 1])z3 = forward_propagation_for_predict(x, params)p = tf.argmax(z3)sess = tf.Session()prediction = sess.run(p, feed_dict = {x: X})return prediction

测试数据集,代码如下:

import resnets_utils
import cv2
train_x, train_y, test_x, test_y, classes=resnets_utils.load_dataset()
print('训练样本={}'.format(train_x.shape))
print('训练样本标签={}'.format(train_y.shape))
print('测试样本={}'.format(test_x.shape))
print('测试样本标签={}'.format(test_y.shape))
print('第五个样本={}'.format(train_y[0,5]))
cv2.imshow('1.jpg',train_x[5,:,:,:]/255)
cv2.waitKey()

打印结果:可看出训练样本有1080个,size为(64,64,3),测试样本有120个,手势四是用4代替。

先测试第一个残差学习单元,模型如下:

代码如下:

from keras.layers import Dense,Flatten,Input,Activation,ZeroPadding2D,AveragePooling2D,BatchNormalization,Conv2D,Add,MaxPooling2D
from keras.models import Model
import matplotlib.pyplot as  plt
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import resnets_utils
import keras.backend as K
import numpy as np
from keras.initializers import glorot_uniform
import tensorflow as tf
def identity_block(X,f,filters,stage,block):conv_name_base='res'+str(stage)+block+'_branch'bn_name_base='bn'+str(stage)+block+'_branch'F1,F2,F3=filtersX_shortcut=Xprint('输入尺寸={}'.format(X.shape))#first convX=Conv2D(filters=F1,kernel_size=(1,1),strides=(1,1),padding='valid',name=conv_name_base+'2a',kernel_initializer=glorot_uniform(seed=0))(X)print('输出尺寸={}'.format(X.shape))X=BatchNormalization(axis=3,name=bn_name_base+'2a')(X)X=Activation('relu')(X)#second convX = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b',kernel_initializer=glorot_uniform(seed=0))(X)print('输出尺寸={}'.format(X.shape))X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)X = Activation('relu')(X)#third convX = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)print('输出尺寸={}'.format(X.shape))#ResNetX=Add()([X,X_shortcut])X = Activation('relu')(X)print('最终输出尺寸={}'.format(X.shape))return X
def test_identity_block():with tf.Session() as sess:np.random.seed(1)A_prev=tf.placeholder('float',[3,4,4,6])X=np.random.randn(3,4,4,6)A=identity_block(A_prev,f=2,filters=[2,4,6],stage=1,block='a')init=tf.global_variables_initializer()sess.run(init)out=sess.run([A],feed_dict={A_prev:X,K.learning_phase():0})
if __name__=='__main__':test_identity_block()

打印结果:由此可见经过三层卷积,该残差单元的输出size和维度不变,因为原始输入未进行卷积,故只能这样才能进行特征融合。

下面是输出维度会发生变化的,对原始输入X做了卷积变换再融合输出得到最终的输出,模型如下

代码如下:

from keras.layers import Dense,Flatten,Input,Activation,ZeroPadding2D,AveragePooling2D,BatchNormalization,Conv2D,Add,MaxPooling2D
from keras.models import Model
import matplotlib.pyplot as  plt
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import resnets_utils
import keras.backend as K
import numpy as np
from keras.initializers import glorot_uniform
import tensorflow as tf
def convolutional_block(X,f,filters,stage,block,s=2):conv_name_base = 'res' + str(stage) + block + '_branch'bn_name_base = 'bn' + str(stage) + block + '_branch'F1, F2, F3 = filtersX_shortcut = Xprint('输入尺寸={}'.format(X.shape))# first convX = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '2a',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)X = Activation('relu')(X)print('输出尺寸={}'.format(X.shape))# second convX = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)X = Activation('relu')(X)print('输出尺寸={}'.format(X.shape))#third convX = Conv2D(filters=8, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)X = Activation('relu')(X)print('输出尺寸={}'.format(X.shape))#ResNetX_shortcut=Conv2D(filters=8, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '1',kernel_initializer=glorot_uniform(seed=0))(X_shortcut)X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut)print('原始输入X经过变化的输出尺寸={}'.format(X.shape))X = Add()([X, X_shortcut])X = Activation('relu')(X)print('最终输出尺寸={}'.format(X.shape))return X
def test_convolutional_block():#tf.reset_default_graph()with tf.Session() as sess:np.random.seed(1)A_prev=tf.placeholder('float',[3,4,4,6])X=np.random.randn(3,4,4,6)A=convolutional_block(A_prev,f=2,filters=[2,4,6],stage=1,block='a',s=2)init = tf.global_variables_initializer()sess.run(init)out=sess.run(A,feed_dict={A_prev:X})
if __name__=='__main__':#test_identity_block()test_convolutional_block()

打印结果:可看出原始输入改变size为(3,2,2,8)最终融合的输出也是(3,2,2,8),故此种残差单元能够解决输出尺寸和维度的问题。

总体模型:其中BLOCK2值得是输出尺度和维度会变化的,BLOCK1指的是不会变化的。

下面用开始调用数据集:其中convolutional_block表示输出尺寸和维度会变化,identity_block表示输出与输入一样,模型如下,

代码如下:

from keras.layers import Dense,Flatten,Input,Activation,ZeroPadding2D,AveragePooling2D,BatchNormalization,Conv2D,Add,MaxPooling2D
from keras.models import Model
import matplotlib.pyplot as  plt
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import resnets_utils
import keras.backend as K
import numpy as np
from keras.initializers import glorot_uniform
import tensorflow as tf
import time
"""
获取数据  并将标签转换成one-hot
"""
def convert_data():train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes=resnets_utils.load_dataset()train_x=train_set_x_orig/255test_x = test_set_x_orig / 255train_y=resnets_utils.convert_to_one_hot(train_set_y_orig,6).Ttest_y = resnets_utils.convert_to_one_hot(test_set_y_orig, 6).T#print(train_y.shape)return train_x,train_y,test_x,test_y
"""
三层卷积的 残差单元 输出尺寸和维度不会变化
"""
def identity_block(X,f,filters,stage,block):conv_name_base='res'+str(stage)+block+'_branch'bn_name_base='bn'+str(stage)+block+'_branch'F1,F2,F3=filtersX_shortcut=X# print('输入尺寸={}'.format(X.shape))#first convX=Conv2D(filters=F1,kernel_size=(1,1),strides=(1,1),padding='valid',name=conv_name_base+'2a',kernel_initializer=glorot_uniform(seed=0))(X)# print('输出尺寸={}'.format(X.shape))X=BatchNormalization(axis=3,name=bn_name_base+'2a')(X)X=Activation('relu')(X)#second convX = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b',kernel_initializer=glorot_uniform(seed=0))(X)# print('输出尺寸={}'.format(X.shape))X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)X = Activation('relu')(X)#third convX = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)# print('输出尺寸={}'.format(X.shape))#ResNetX=Add()([X,X_shortcut])X = Activation('relu')(X)# print('最终输出尺寸={}'.format(X.shape))return X"""
三层卷积的 残差单元 输出尺寸和维度会变化
"""
def convolutional_block(X,f,filters,stage,block,s=2):conv_name_base = 'res' + str(stage) + block + '_branch'bn_name_base = 'bn' + str(stage) + block + '_branch'F1, F2, F3 = filtersX_shortcut = X# print('输入尺寸={}'.format(X.shape))# first convX = Conv2D(filters=F1, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '2a',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)X = Activation('relu')(X)# print('输出尺寸={}'.format(X.shape))# second convX = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)X = Activation('relu')(X)# print('输出尺寸={}'.format(X.shape))#third convX = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c',kernel_initializer=glorot_uniform(seed=0))(X)X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)X = Activation('relu')(X)# print('输出尺寸={}'.format(X.shape))#ResNetX_shortcut=Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '1',kernel_initializer=glorot_uniform(seed=0))(X_shortcut)X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut)# print('原始输入X经过变化的输出尺寸={}'.format(X.shape))X = Add()([X, X_shortcut])X = Activation('relu')(X)# print('最终输出尺寸={}'.format(X.shape))return X"""
50层残差网络
"""
def ResNet50(input_shape=(64,64,3),classes=6):X_input=Input(input_shape)print('输入尺寸={}'.format(X_input.shape))X=ZeroPadding2D((3,3))(X_input)print('补完零尺寸={}'.format(X.shape))#Stage 1X=Conv2D(filters=64,kernel_size=(7,7),strides=(2,2),name='conv1',kernel_initializer=glorot_uniform(seed=0))(X)print('第一次卷积尺寸={}'.format(X.shape))X=BatchNormalization(axis=3,name='bn_conv1')(X)X=Activation('relu')(X)X=MaxPooling2D(pool_size=(3,3),strides=(2,2))(X)print('第一次池化尺寸={}'.format(X.shape))#Stage 2X=convolutional_block(X,f=3,filters=[64,64,256],stage=2,block='a',s=1)print('第一次convolutional_block尺寸={}'.format(X.shape))X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block='b')X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block='c')print('两次identity_block尺寸={}'.format(X.shape))#Stage 3X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2)print('第二次convolutional_block尺寸={}'.format(X.shape))X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='b')X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='c')X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block='d')print('三次identity_block尺寸={}'.format(X.shape))#Stage 4X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2)print('第三次convolutional_block尺寸={}'.format(X.shape))X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block='b')X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block='c')X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block='d')X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block='e')X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block='f')print('五次identity_block尺寸={}'.format(X.shape))#Stage 5X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2)print('第四次convolutional_block尺寸={}'.format(X.shape))X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block='b')X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block='c')print('两次identity_block尺寸={}'.format(X.shape))#PoolX=AveragePooling2D(pool_size=(2,2))(X)print('最后一次平均池化尺寸={}'.format(X.shape))#OutPut Flatten+FULLYCONNECTEDX=Flatten()(X)X=Dense(units=classes,activation='softmax',name='fc'+str(classes),kernel_initializer=glorot_uniform(seed=0))(X)#create modelmodel=Model(inputs=X_input,outputs=X,name='ResNet50')return model
def test_identity_block():with tf.Session() as sess:np.random.seed(1)A_prev=tf.placeholder('float',[3,4,4,6])X=np.random.randn(3,4,4,6)A=identity_block(A_prev,f=2,filters=[2,4,6],stage=1,block='a')init=tf.global_variables_initializer()sess.run(init)out=sess.run([A],feed_dict={A_prev:X,K.learning_phase():0})# print('out=',out[0][1][1][0])
def test_convolutional_block():#tf.reset_default_graph()with tf.Session() as sess:np.random.seed(1)A_prev=tf.placeholder('float',[3,4,4,6])X=np.random.randn(3,4,4,6)A=convolutional_block(A_prev,f=2,filters=[2,4,6],stage=1,block='a',s=2)init = tf.global_variables_initializer()sess.run(init)out=sess.run(A,feed_dict={A_prev:X})print('out=',out[0][0][0])
def test_ResNet50():#定义好模型结构Resnet50_model=ResNet50(input_shape=(64,64,3),classes=6)#选定训练参数Resnet50_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])#获取训练集和测试集train_x, train_y, test_x, test_y=convert_data()#训练集上训练start_time=time.time()print('============开始训练===============')Resnet50_model.fit(x=train_x,y=train_y,batch_size=32,epochs=2)end_time=time.time()print('train_time={}'.format(end_time-start_time))#测试集上测试preds=Resnet50_model.evaluate(x=test_x,y=test_y,batch_size=32,)print('loss={}'.format(preds[0]))print('Test Accuracy={}'.format(preds[1]))if __name__=='__main__':#test_identity_block()#test_convolutional_block()#convert_data()test_ResNet50()

打印结果:

其中问号代表的是样本数,可看出最终卷积输出是1×1×2048

训练样本为1080个,第一个Epoch每个样本时间为175ms,所以共189s.第一次epoch训练精度为0.27。

第二个Epoch每个样本时间为165ms,所以共178s.训练两次epoch时间为376S,不等于两次epoch时间之和,应该是有别的开支。第二次epoch训练精度为0.40提高了。

经过两次epoch的模型来测试120个样本,测试精度为0.19,恩很低,所以还要多训练嘛。

二,tensorboard显示

首先安装graphviz用于可视化网络

apt-get install graphviz

pip install graphviz

pip install pydot

可视化网络如下:

调节学习率的clr_callback.py文件:

from keras.callbacks import *class CyclicLR(Callback):"""This callback implements a cyclical learning rate policy (CLR).The method cycles the learning rate between two boundaries withsome constant frequency, as detailed in this paper (https://arxiv.org/abs/1506.01186).The amplitude of the cycle can be scaled on a per-iteration orper-cycle basis.This class has three built-in policies, as put forth in the paper."triangular":A basic triangular cycle w/ no amplitude scaling."triangular2":A basic triangular cycle that scales initial amplitude by half each cycle."exp_range":A cycle that scales initial amplitude by gamma**(cycle iterations) at eachcycle iteration.For more detail, please see paper.# Example```pythonclr = CyclicLR(base_lr=0.001, max_lr=0.006,step_size=2000., mode='triangular')model.fit(X_train, Y_train, callbacks=[clr])```Class also supports custom scaling functions:```pythonclr_fn = lambda x: 0.5*(1+np.sin(x*np.pi/2.))clr = CyclicLR(base_lr=0.001, max_lr=0.006,step_size=2000., scale_fn=clr_fn,scale_mode='cycle')model.fit(X_train, Y_train, callbacks=[clr])```# Argumentsbase_lr: initial learning rate which is thelower boundary in the cycle.max_lr: upper boundary in the cycle. Functionally,it defines the cycle amplitude (max_lr - base_lr).The lr at any cycle is the sum of base_lrand some scaling of the amplitude; thereforemax_lr may not actually be reached depending onscaling function.step_size: number of training iterations perhalf cycle. Authors suggest setting step_size2-8 x training iterations in epoch.mode: one of {triangular, triangular2, exp_range}.Default 'triangular'.Values correspond to policies detailed above.If scale_fn is not None, this argument is ignored.gamma: constant in 'exp_range' scaling function:gamma**(cycle iterations)scale_fn: Custom scaling policy defined by a singleargument lambda function, where0 <= scale_fn(x) <= 1 for all x >= 0.mode paramater is ignoredscale_mode: {'cycle', 'iterations'}.Defines whether scale_fn is evaluated oncycle number or cycle iterations (trainingiterations since start of cycle). Default is 'cycle'."""def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',gamma=1., scale_fn=None, scale_mode='cycle'):super(CyclicLR, self).__init__()self.base_lr = base_lrself.max_lr = max_lrself.step_size = step_sizeself.mode = modeself.gamma = gammaif scale_fn == None:if self.mode == 'triangular':self.scale_fn = lambda x: 1.self.scale_mode = 'cycle'elif self.mode == 'triangular2':self.scale_fn = lambda x: 1 / (2. ** (x - 1))self.scale_mode = 'cycle'elif self.mode == 'exp_range':self.scale_fn = lambda x: gamma ** (x)self.scale_mode = 'iterations'else:self.scale_fn = scale_fnself.scale_mode = scale_modeself.clr_iterations = 0.self.trn_iterations = 0.self.history = {}self._reset()def _reset(self, new_base_lr=None, new_max_lr=None,new_step_size=None):"""Resets cycle iterations.Optional boundary/step size adjustment."""if new_base_lr != None:self.base_lr = new_base_lrif new_max_lr != None:self.max_lr = new_max_lrif new_step_size != None:self.step_size = new_step_sizeself.clr_iterations = 0.def clr(self):cycle = np.floor(1 + self.clr_iterations / (2 * self.step_size))x = np.abs(self.clr_iterations / self.step_size - 2 * cycle + 1)if self.scale_mode == 'cycle':return self.base_lr + (self.max_lr - self.base_lr) * np.maximum(0, (1 - x)) * self.scale_fn(cycle)else:return self.base_lr + (self.max_lr - self.base_lr) * np.maximum(0, (1 - x)) * self.scale_fn(self.clr_iterations)def on_train_begin(self, logs={}):logs = logs or {}if self.clr_iterations == 0:K.set_value(self.model.optimizer.lr, self.base_lr)else:K.set_value(self.model.optimizer.lr, self.clr())def on_batch_end(self, epoch, logs=None):logs = logs or {}self.trn_iterations += 1self.clr_iterations += 1self.history.setdefault('lr', []).append(K.get_value(self.model.optimizer.lr))self.history.setdefault('iterations', []).append(self.trn_iterations)for k, v in logs.items():self.history.setdefault(k, []).append(v)K.set_value(self.model.optimizer.lr, self.clr())

main.py文件

import keras
from keras.models import Model
import matplotlib.pyplot as  plt
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import resnets_utils
import keras.backend as K
import numpy as np
from keras.optimizers import Adam
from keras.initializers import glorot_uniform
from clr_callback import CyclicLR"""
获取数据  并将标签转换成one-hot
"""
def convert_data():train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes=resnets_utils.load_dataset()train_x=train_set_x_orig/255test_x = test_set_x_orig / 255train_y=resnets_utils.convert_to_one_hot(train_set_y_orig,6).Ttest_y = resnets_utils.convert_to_one_hot(test_set_y_orig, 6).T#print(train_y.shape)return train_x[:320,...],train_y[:320,...],test_x,test_y
if __name__=='__main__':train_x, train_y, test_x, test_y=convert_data()print(train_x.shape)print(train_y.shape)print(test_x.shape)print(test_y.shape)best_score=0classes=6Epcoh=50Batch=32input_shape=(64,64,3)X_input = keras.layers.Input(input_shape)X = keras.layers.Conv2D(filters=6, kernel_size=(3, 3), strides=(2, 2), name='conv1',kernel_initializer=glorot_uniform(seed=0))(X_input)print('第一次卷积尺寸={}'.format(X.shape))X=keras.layers.GlobalAveragePooling2D()(X)print(X.shape)Y=keras.layers.Dense(classes, activation='softmax', name='fc_class')(X)model = Model(inputs=X_input, outputs=Y)model.summary()keras.utils.plot_model(model,to_file='./model.jpg')lr = 1e-2clr = CyclicLR(base_lr=1e-5, max_lr=lr, step_size= Epcoh/ Batch * 2, mode='triangular2')adam = Adam(lr=lr, beta_1=0.9, beta_2=0.999, epsilon=1e-08, amsgrad=True, )model.compile(optimizer=adam, loss='categorical_crossentropy',metrics=['acc']) #metrics=['mae', 'acc'])tb_callback=keras.callbacks.TensorBoard(log_dir='./logs/keras',histogram_freq=1,write_graph=True,write_images=1,write_grads=True)history = model.fit(x=train_x, y=train_y,batch_size=Batch,validation_data=(test_x, test_y),epochs=Epcoh,callbacks=[tb_callback,clr])acc=history.history['acc']loss = history.history['loss']val_acc = history.history['val_acc']val_loss = history.history['val_loss']print('acc=', acc)print('loss=', loss)print('val_acc=',val_acc)print('val_loss=',val_loss)score = model.evaluate(x=test_x, y=test_y,batch_size=Batch)print('loss=',score[0])print('test_acc',score[1])if score[1] > best_score:best_score = score[1]model.save('./model.h5', overwrite=True)

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/493600.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

通过SVD求解单应矩阵

我们现在知道原则上4对匹配点对就可以唯一确定单应矩阵&#xff0c;但是在实际应用中我们无法保证两个视图严格满足使用条件&#xff08;只有旋转变换&#xff1b;远景&#xff1b;平面场景&#xff09;&#xff0c;所以要使用拟合的方法求一个最优解。现在就来以SIFT算法源码为…

注意力机制(Attention)最新综述论文及相关源码

来源&#xff1a;专知注意力机制(Attention)起源于模仿人类的思维方式&#xff0c;后被广泛应用于机器翻译、情感分类、自动摘要、自动问答等、依存分析等机器学习应用中。专知编辑整理了Arxiv上一篇关于注意力机制在NLP中应用的综述《An Introductory Survey on Attention Mec…

橙子楼的猥琐大叔

故事要从暑假开始说起&#xff0c;那时我还在准备考研&#xff0c;每天往返于教室、宿舍和食堂&#xff0c;单调但不会无趣&#xff0c;常常会有故事发生&#xff0c;生活也很充实。 考研的一般都会在固定的教室有个自己的位子。 坐我正前面的是一个妹子&#xff0c;准确的说是…

Pycharm下安装Tensorflow

趁着帮师妹看Github上的一个项目&#xff0c;督促自己学习一下Python下训练神经网络的一整套流程。没想到在一开头就遇到了不少问题。首先是Pycharm中导入Github项目的问题&#xff0c;还有安装tensorflow的问题&#xff0c;之后又遇到了多种版本的Python共存的问题。在这里记录…

吴恩达作业9:卷积神经网络实现手势数字的识别(基于tensorflow)

数据集链接&#xff1a;https://download.csdn.net/download/fanzonghao/10551018 提供数据集代码放在cnn_utils.py里。 import math import numpy as np import h5py import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.python.framework import ops…

AI洞观 | 戴上红帽 看IBM冲杀云计算市场

来源 | 网易智能&#xff08;公众号 smartman163&#xff09;摘要&#xff1a;可以预计&#xff0c;未来的云计算市场将越来越比拼生态综合服务能力&#xff0c;云计算行业进入下半场&#xff0c;谁在裸泳不久见分晓。IBM豪掷340亿美元收购红帽&#xff08;Red Hat&#xff09;…

基于visual Studio2013解决面试题之0608找出两个只出现一次的数

&#xfeff;&#xfeff;&#xfeff;题目解决代码及点评/*已知数组中有两个数只出现一次&#xff0c;其他成对出现&#xff0c;请找出这两个数解决办法&#xff1a;1&#xff09;简化问题&#xff0c;如果数组中只有一个数出现一次&#xff0c;那么只要对这个数组做异或即可2…

工业富联:左手工业AI,右手“雾小脑”

来源&#xff1a;先进制造业摘要&#xff1a;11月14日&#xff0c;第二十届中国国际高新技术成果交易会在深圳会展中心开幕。11月14日&#xff0c;第二十届中国国际高新技术成果交易会在深圳会展中心开幕。备受瞩目的是&#xff0c;在分论坛“2018第九届中国信息通信论坛”上&a…

吴恩达作业10:用卷积神经网络识别人脸happy(基于Keras)

数据集提供的代码放在kt_utils.py&#xff1a; import keras.backend as K import math import numpy as np import h5py import matplotlib.pyplot as pltdef mean_pred(y_true, y_pred):return K.mean(y_pred)def load_dataset():train_dataset h5py.File(datasets/train_h…

清华大学:智能驾驶背景下转向系统发展趋势

来源&#xff1a;智车科技这里近几年&#xff0c;自动驾驶汽车的研发与推广发展迅速&#xff0c;根据国家工业和信息化部等发布的《汽车产业中长期发展规划》&#xff0c;2025 年&#xff0c;高度和完全自动驾驶汽车开始进入市场&#xff0c;在此背景下&#xff0c;线控转向技术…

TensorFlow学习之——checkpoints

在看别人的训练网络中一开头就遇到这样一行代码&#xff1a; ckpt tf.train.get_checkpoint_state(directories.checkpoints) 鼠标放在函数名上&#xff0c;ctrlB&#xff0c;或者ctrl点击函数名&#xff0c;可以跳转到函数的定义&#xff0c;可以知道tf.train.get_checkpoi…

jQuery.ajax

(摘录)http://www.cnblogs.com/XuebinDing/archive/2012/03/01/2376041.html 情况一、使用WebService验证 1.新建demo.aspx页面。2.首先在该页面的后台文件demos.aspx.cs中添加引用。 using System.Web.Services; 3.无参数的方法调用. 大家注意了&#xff0c;这个版本不能低于.…

基于Keras的卷积神经网络用于猫狗分类(未进行数据增强)+卷积层可视化

首先看数据集路径&#xff1a; cats和dogs存放的就是各种大小的猫狗图片。 读取数据集代码&#xff1a; import os import matplotlib.pyplot as plt """ 读取数据 返回数据的文件夹名字&#xff0c;和具体的猫狗的路径 """ def read_data(): …

芯片植入:“增强人类”的生物黑科技

来源&#xff1a;资本实验室摘要&#xff1a;当医疗、电子、生物科技快速发展&#xff0c;并紧密融合的时候&#xff0c;许多科幻电影中的场景正在不断变为现实&#xff0c;而“增强人类”、”赛博格“、“电子人”、“生化人”正在成为这个时代最值得期待&#xff0c;又多少让…

如何通过VC的 CHttpFile 抓取网页内容

在点击一个按钮时开始请求你输入的地址。void CHttpFileDlg::OnButton1() {   CString url;   GetDlgItemText(IDC_EDIT1,url); // IDC_EDIT1 是一个输入框的名字。   char* headers"Accept:*/*\r\nAccept-Language:zh-cn\r\nUser-Agent:VCTestClient\r\n"; …

windows安装ubuntu16.04LTS 更换系统源为阿里源 安装ubuntu找不到windows 删除内核 更换pip源 升级pip源报错 ubuntu命令

一&#xff0c;安装ubuntu ubuntu镜像Index of /ubuntu-releases/18.04/ 首先在windows下硬盘划分出100G的空间&#xff0c;并且制作一个ubuntu的启动盘。在windows安装easy BCD用于开机启动ubuntu。下面看安装过程&#xff1a; 选择语言&#xff0c;我选择是英语 我选择的是…

基本系统部署完成!北斗三号闪耀中国智慧

▲ 第四十二、四十三颗北斗导航卫星乘长三乙火箭升空来源&#xff1a;航天501部2018年11月19日&#xff0c;第四十二、四十三颗北斗导航卫星在西昌卫星发射中心腾空而起。作为北斗三号第十八颗、第十九颗卫星&#xff0c;此次双星的成功发射&#xff0c;标志着北斗三号全球组网…

基于Keras的卷积神经网络用于猫狗分类(进行了数据增强)+卷积层可视化

接着我上一篇博客&#xff0c;https://blog.csdn.net/fanzonghao/article/details/81149153。 在上一篇基础上对数据集进行数据增强。函数如下&#xff1a; """ 查看图像增强是否发生作用 """ def see_pic_aug():train_datagen ImageDataGene…

深圳神经科学研究院院长谭力海: AI取代人脑? 不, 必须向人脑“学习”!

来源&#xff1a;读创科技摘要&#xff1a;从“深蓝”到“阿尔法狗”&#xff0c;人工智能技术日益成熟&#xff0c;“AI何时替代人脑”的争论也在不断升级。人工智能真的能超越人脑吗&#xff1f;11月15日在高交会“颠覆性创新技术”主题论坛上&#xff0c;深圳神经科学研究院…

好的PPT——准备工作

首先介绍PPT的一些基本技巧。 在选项界面&#xff0c;我们需要调整一些选项&#xff1a;控制最大可回退次数&#xff1b;语法自动检查&#xff1b;字体嵌入PPT&#xff0c;调整自动保存的时间间隔。 对于一些常用操作&#xff0c;可以右键添加到常用工具栏。 可以很方便地调整不…