首页 > 其他分享 >基础_cifar10_model

基础_cifar10_model

时间:2022-12-26 23:08:23浏览次数:38  
标签:None cifar10 32 基础 train test model ____________________________________________

今天进一步在cifar10数据集上解决几个问题:


1、比较一下序贯和model,为什么要分成两块;


2、同样的条件下,我去比较一下序贯和model。这个例子作为今天的晚间运行。



1、比较一下序贯和model,为什么要分成两块;


我认为比较 能够说明问题 的是前期那个验证码的model

​​= Input((height, width, 
3))

x
= input_tensor

for i
in
range(
4)
:

x
= Convolution2D(
32
*
2
**i,
3,
3, activation
=
'relu')(x)

x
= Convolution2D(
32
*
2
**i,
3,
3, activation
=
'relu')(x)

x
= MaxPooling2D((
2,
2))(x)


x
= Flatten()(x)

x
= Dropout(
0.
25)(x)

x
= [Dense(n_class, activation
=
'softmax', name
=
'c%d'
%(i
+
1))(x)
for i
in
range(
4)]

model
= Model(
input
=input_tensor, output
=x)
​​


一个非常显著的不同,就是最后分为了4个部分(这也是这里选择model的原因),这个 4分类直接最后导致了验证码4个部分的区分。这是验证码识别最为有技术的地方。应该说,从网络结构上来看,这个分叉,在cnn网络 中,就是model和sequence最大的不同之处,也是着重要研究的地方。



model  =  Model( input = input_tensor, output =



model最大的不同就在于它首先定义了网络结构,然后最后才生成这个模型。从编码方式上来看,只是顺序的不同而已。



=  [Dense(n_class, activation = 'softmax' , name = 'c%d' % (i + 1 ))(x)  for  i  in   range ( 4



这句之所以能够将网络1分4,是因为采用的列表的形式。然后就能够进行验证?我将这个问题留给明天。另一个方面,在这个验证码的例子中,其网络就是不断地卷积-卷积-maxpooling,非常简单,是否需要进一步优化?



2、同样的条件下,我去比较一下序贯和model


'''


同样的数据集和epochs,同样的模型构造。


一个采用序贯、一个采用model方法


对生成结果进行比较

'''



from
__future__
import print_function



#!apt-get -qq install -y graphviz && pip install -q pydot



import pydot



import keras



import cv2



from keras.datasets
import cifar10



from keras.preprocessing.image
import ImageDataGenerator



from keras.models
import Sequential



from keras.layers
import Dense, Dropout, Activation, Flatten



from keras.layers
import Conv2D, MaxPooling2D



from keras.layers
import Input



from keras.models
import Model



from keras.utils.vis_utils
import plot_model



import matplotlib.image
as image
# image 用于读取图片



import matplotlib.pyplot
as plt



import os




batch_size =
32



num_classes =
10



#epochs = 100



epochs =
3



data_augmentation =
True



num_predictions =
20



save_dir = os.path.join(os.getcwd(),
'saved_models')



model_name =
'keras_cifar10_trained_model.h5'



# The data, split between train and test sets:



(x_train, y_train), (x_test, y_test) = cifar10.load_data()



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, (
3,
3),
padding=
'same',



input_shape=x_train.shape[
1:]))



model.add(Activation(
'relu'))



model.add(Conv2D(
32, (
3,
3)))



model.add(Activation(
'relu'))



model.add(MaxPooling2D(
pool_size=(
2,
2)))



model.add(Dropout(
0.25))



model.add(Conv2D(
64, (
3,
3),
padding=
'same'))



model.add(Activation(
'relu'))



model.add(Conv2D(
64, (
3,
3)))



model.add(Activation(
'relu'))



model.add(MaxPooling2D(
pool_size=(
2,
2)))



model.add(Dropout(
0.25))



model.add(Flatten())



model.add(Dense(
512))



model.add(Activation(
'relu'))



model.add(Dropout(
0.5))



model.add(Dense(num_classes))



model.add(Activation(
'softmax'))



#model模型



inputs = Input(
shape=(x_train.shape[
1:]))



x = Conv2D(
32, (
3,
3),
activation=
'relu')(inputs)



x = Conv2D(
32, (
3,
3),
activation=
'relu')(x)



x = MaxPooling2D((
2,
2))(x)



x = Dropout(
0.25)(x)



x = Conv2D(
64, (
3,
3),
activation=
'relu')(x)



x = Conv2D(
64, (
3,
3),
activation=
'relu')(x)



x = MaxPooling2D((
2,
2))(x)



x = Dropout(
0.25)(x)



x = Flatten()(x)



x = Dense(
512)(x)



x = Activation(
'relu')(x)



x = Dropout(
0.5)(x)



x = Dense(num_classes)(x)



x = Activation(
'softmax')(x)



model2 = Model(
inputs=inputs,
outputs=x)



#显示模型



model.summary()



plot_model(model,
to_file=
'model_sequence.png',
show_shapes=
True)



img = image.imread(
'model_sequence.png')



print(img.shape)



plt.imshow(img)
# 显示图片



plt.axis(
'off')
# 不显示坐标轴



plt.show()



model2.summary()



plot_model(model2,
to_file=
'model_model.png',
show_shapes=
True)



img2 = image.imread(
'model_model.png')



print(img2.shape)



plt.imshow(img2)
# 显示图片



plt.axis(
'off')
# 不显示坐标轴



plt.show()



# initiate RMSprop optimizer



opt = keras.optimizers.rmsprop(
lr=
0.0001,
decay=
1e-6)



# Let's train the model using RMSprop



model.compile(
loss=
'categorical_crossentropy',



optimizer=opt,



metrics=[
'accuracy'])



model2.compile(
loss=
'categorical_crossentropy',



optimizer=opt,



metrics=[
'accuracy'])



x_train = x_train.astype(
'float32')



x_test = x_test.astype(
'float32')



x_train /=
255



x_test /=
255



if
not data_augmentation:



print(
'Not using data augmentation.')



model.fit(x_train, y_train,



batch_size=batch_size,



epochs=epochs,



validation_data=(x_test, y_test),



shuffle=
True)



else:



print(
'Using real-time data augmentation.')



# This will do preprocessing and realtime data augmentation:



datagen = ImageDataGenerator(



featurewise_center=
False,
# set input mean to 0 over the dataset



samplewise_center=
False,
# set each sample mean to 0



featurewise_std_normalization=
False,
# divide inputs by std of the dataset



samplewise_std_normalization=
False,
# divide each input by its std



zca_whitening=
False,
# apply ZCA whitening



rotation_range=
0,
# randomly rotate images in the range (degrees, 0 to 180)



width_shift_range=
0.1,
# randomly shift images horizontally (fraction of total width)



height_shift_range=
0.1,
# randomly shift images vertically (fraction of total height)



horizontal_flip=
True,
# randomly flip images



vertical_flip=
False)
# randomly flip images



# Compute quantities required for feature-wise normalization



# (std, mean, and principal components if ZCA whitening is applied).



datagen.fit(x_train)



# Fit the model on the batches generated by datagen.flow().



model.fit_generator(datagen.flow(x_train, y_train,



batch_size=batch_size),



epochs=epochs,



validation_data=(x_test, y_test),



workers=
4)



model2.fit_generator(datagen.flow(x_train, y_train,



batch_size=batch_size),



epochs=epochs,



validation_data=(x_test, y_test),



workers=
4)



# Save model and weights



if
not os.path.isdir(save_dir):



os.makedirs(save_dir)



model_path = os.path.join(save_dir, model_name)



model.save(model_path)



print(
'Saved trained model at
%s
' % model_path)



# Score trained model.



scores = model.evaluate(x_test, y_test,
verbose=
1)



print(
'Test loss:', scores[
0])



print(
'Test accuracy:', scores[
1])



模型打印出来比较:



基础_cifar10_model_Test


sequence


主要是由于在


x = Conv2D( 32 , ( 3 , 3 ), activation = 'relu' )(inputs)


似乎是将activation合并了,所以model_model更短一些。但是层的结果似乎也是不一样的。这个等到训练结果出来后进一步研究。


Using TensorFlow backend.


x_train shape: (50000, 32, 32, 3)


50000 train samples


10000 test samples


_________________________________________________________________


Layer (type)                 Output Shape              Param #  


=================================================================


conv2d_1 (Conv2D)            (None, 32, 32, 32)        896      


_________________________________________________________________


activation_1 (Activation)    (None, 32, 32, 32)        0        


_________________________________________________________________


conv2d_2 (Conv2D)            (None, 30, 30, 32)        9248     


_________________________________________________________________


activation_2 (Activation)    (None, 30, 30, 32)        0        


_________________________________________________________________


max_pooling2d_1 (MaxPooling2 (None, 15, 15, 32)        0        


_________________________________________________________________


dropout_1 (Dropout)          (None, 15, 15, 32)        0        


_________________________________________________________________


conv2d_3 (Conv2D)            (None, 15, 15, 64)        18496    


_________________________________________________________________


activation_3 (Activation)    (None, 15, 15, 64)        0        


_________________________________________________________________


conv2d_4 (Conv2D)            (None, 13, 13, 64)        36928    


_________________________________________________________________


activation_4 (Activation)    (None, 13, 13, 64)        0        


_________________________________________________________________


max_pooling2d_2 (MaxPooling2 (None, 6, 6, 64)          0        


_________________________________________________________________


dropout_2 (Dropout)          (None, 6, 6, 64)          0        


_________________________________________________________________


flatten_1 (Flatten)          (None, 2304)              0        


_________________________________________________________________


dense_1 (Dense)              (None, 512)               1180160  


_________________________________________________________________


activation_5 (Activation)    (None, 512)               0        


_________________________________________________________________


dropout_3 (Dropout)          (None, 512)               0        


_________________________________________________________________


dense_2 (Dense)              (None, 10)                5130     


_________________________________________________________________


activation_6 (Activation)    (None, 10)                0        


=================================================================


Total params: 1,250,858


Trainable params: 1,250,858


Non-trainable params: 0


_________________________________________________________________


(2065, 635, 4)



基础_cifar10_model_ci_02


_________________________________________________________________


Layer (type)                 Output Shape              Param #  


=================================================================


input_1 (InputLayer)         (None, 32, 32, 3)         0        


_________________________________________________________________


conv2d_5 (Conv2D)            (None, 30, 30, 32)        896      


_________________________________________________________________


conv2d_6 (Conv2D)            (None, 28, 28, 32)        9248     


_________________________________________________________________


max_pooling2d_3 (MaxPooling2 (None, 14, 14, 32)        0        


_________________________________________________________________


dropout_4 (Dropout)          (None, 14, 14, 32)        0        


_________________________________________________________________


conv2d_7 (Conv2D)            (None, 12, 12, 64)        18496    


_________________________________________________________________


conv2d_8 (Conv2D)            (None, 10, 10, 64)        36928    


_________________________________________________________________


max_pooling2d_4 (MaxPooling2 (None, 5, 5, 64)          0        


_________________________________________________________________


dropout_5 (Dropout)          (None, 5, 5, 64)          0        


_________________________________________________________________


flatten_2 (Flatten)          (None, 1600)              0        


_________________________________________________________________


dense_3 (Dense)              (None, 512)               819712   


_________________________________________________________________


activation_7 (Activation)    (None, 512)               0        


_________________________________________________________________


dropout_6 (Dropout)          (None, 512)               0        


_________________________________________________________________


dense_4 (Dense)              (None, 10)                5130     


_________________________________________________________________


activation_8 (Activation)    (None, 10)                0        


=================================================================


Total params: 890,410


Trainable params: 890,410


Non-trainable params: 0


_________________________________________________________________


(1623, 635, 4)



基础_cifar10_model_验证码_03


model消耗的时间更少,而且正确率更高、loss更低。那么以后我优先建立model模型!


10000/10000 [==============================] - 2s 187us/step


Test loss: 0.9320432889938355


Test accuracy: 0.682


10000/10000 [==============================] - 2s 173us/step


Test loss2: 0.8202090420722962


Test accuracy2: 0.7091



标签:None,cifar10,32,基础,train,test,model,____________________________________________
From: https://blog.51cto.com/jsxyhelu2017/5971109

相关文章

  • 尝试解决cifar10问题
    注意事项:1、我要用kaggle的数据集,而不是用其它的数据集;2、最终得到的结果要以test为导向;1、先打开jupyter,并且把数据集传到dl_machine上去。想办......
  • 在layerModelBlending的PR过程中出现的问题和解决方法
    一、情况说明layerModelBlending是属于photo模块下的算法,我很大程度上参考seamlessclone进行实现。这是我第一次编写功能模块,不同于之前的教程或者是某些参数的修改。......
  • HTML,CSS 基础知识整理
    Day01html基础1.网站三要素html===结构===w3c指定语法规则css===表现===w3c指定语法规则js===行为===ECMA指定语法规则2.各种的标签1)文章标题标签h1-h6特点:双标签......
  • Python学习笔记--PySpark的相关基础学习(一)
    PySpark包的下载下载PySpark第三方包:构建PySpark的执行环境入口对象PySpark的编程模型数据输入对于SparkContext对象里面的成员方法parallelize,支持:示例:读......
  • 爬虫学习笔记 -- requests库基础
    0x01requests库安装1、通过控制台运行下面代码pip3installrequests2、通过Pycharm安装,点击+号,搜索requests,然后点击安装 0x02GET请求1、普通请求importrequestsurl="h......
  • Android基础入门教程
    一、Android介绍Android是一种基于Linux的自由及开放源代码的操作系统,Android分为四个层,从高层到低层分别是应用程序层、应用程序框架层、系统运行库层和Linux内核层。Andr......
  • 批处理文件基础
    有的人在等一个消息的回复,有的人在等一个好友的请求,还有的人在等一个熟悉的电话,你呢?在等什么。。。。---- 网易云热评一、DOS的基础命令1、echo:在DOS窗口输出语句2、>aiy......
  • windows编程基础
    散伙是人间常态,你我又不是什么例外,只是很遗憾,没有成为你的偏爱,你却成为我爱不得的遗憾。。。---- 网易云热评一、Windows应用程序的类型1、控制台程序Console    DOS......
  • Java Web基础概述
    文章目录​​一.JavaWeb基本概念​​​​1.前言​​​​2.web应用程序​​​​3.静态web​​​​4.动态web​​​​二.Web结构​​​​1.什么是后端开发​​​​2.......
  • 数据库 -- 基础操作
    不知道少了什么,总觉得没有以前快乐,不后悔遇见谁,只后悔变成现在的模样。。。---- 网易云热评一、环境安装PHPstudy下载地址,一路下一步,安装的时候路径不要有中文或者空格​......