# 基础_cifar10_model

1、比较一下序贯和model,为什么要分成两块；
2、同样的条件下，我去比较一下序贯和model。这个例子作为今天的晚间运行。

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

input_tensor = Input((height, width, 3))x = input_tensorfor 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)

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

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

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

2、同样的条件下，我去比较一下序贯和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()
input_shape=x_train.shape[ 1:]))

#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)
print(img.shape)
plt.imshow(img) # 显示图片
plt.axis( 'off') # 不显示坐标轴
plt.show()

model2.summary()
plot_model(model2, to_file= 'model_model.png', show_shapes= True)
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])

sequence

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

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)
_________________________________________________________________
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)

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

|
8天前
|
PyTorch 算法框架/工具
pytorch - swa_model模型保存的问题
pytorch - swa_model模型保存的问题
41 0
|
8天前
|

Keras 的模型（Model）和层（Layers）的介绍
Keras 的模型（Model）和层（Layers）的介绍
27 1
|
8天前
|

123 1
|
8天前
|

Model Inference

90 1
|
9月前
|

【PyTorch】Training Model
【PyTorch】Training Model
65 0
|
9月前
|
API 算法框架/工具

77 0
|
11月前
|
PyTorch 算法框架/工具
134 0
|

model是一个模型网络，model.eval() 、model.train()是什么意思？

651 0
|
PyTorch 算法框架/工具
【pytorch】pytorch代码中实现MNIST、cifar10等数据集本地读取
pytorch代码中实现MNIST、cifar10等数据集本地读取
239 0
|

260 0