tensorflow / keras CNN模型架构

时间:2019-04-27 18:03:54

标签: python tensorflow keras

我想创建某些受图像启发的CNN模型,所以我尝试这样做,但是没有成功:the image here

以及当我尝试实现此体系结构

Layer (type)                 Output Shape              Param #   
=================================================================
reshape_16 (Reshape)         (None, 32, 32, 1)         0         
_________________________________________________________________
conv2d_32 (Conv2D)           (None, 32, 32, 80)        2080      
_________________________________________________________________
max_pooling2d_31 (MaxPooling (None, 16, 16, 80)        0         
_________________________________________________________________
batch_normalization_4 (Batch (None, 16, 16, 80)        320       
_________________________________________________________________
conv2d_33 (Conv2D)           (None, 16, 16, 64)        128064    
_________________________________________________________________
max_pooling2d_32 (MaxPooling (None, 8, 8, 64)          0         
_________________________________________________________________
batch_normalization_5 (Batch (None, 8, 8, 64)          256       
_________________________________________________________________
flatten_15 (Flatten)         (None, 4096)              0         
_________________________________________________________________
dense_29 (Dense)             (None, 1024)              4195328   
_________________________________________________________________
dropout_15 (Dropout)         (None, 1024)              0         
_________________________________________________________________
dense_30 (Dense)             (None, 29)                29725     
=================================================================

和我在python中的代码

model = Sequential()

model.add(Reshape((32,32,1), input_shape=(32,32,1)))
#first layer of cnn
model.add(Conv2D(filters = 80, kernel_size = (5,5),padding = 'Same', 
             activation ='relu'))

model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(BatchNormalization())
#second layer of cnn
model.add(Conv2D(filters = 64, kernel_size = (5,5),padding = 'Same', 
             activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
model.add(BatchNormalization())
#fully connected layer 
model.add(Flatten())
model.add(Dense(units = 1024, activation = "relu"))
model.add(Dropout(0.8))
model.add(Dense(29, activation = "softmax"))  

model.summary()

我想创建图像中的CNN

1 个答案:

答案 0 :(得分:0)

这是在cifar10数据集上实现的CNN,它将提供您要查找的输出@Ishak Barkat

在Tensorflow'2.0.0-alpha0'中实现)

from tensorflow import keras
import tensorflow as tf
import numpy as np

cifar10= tf.keras.datasets.cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = np.reshape(x_train, (x_train.shape[0], 32,32, 3))
x_test = np.reshape(x_test, (x_test.shape[0], 32,32, 3))

model = tf.keras.models.Sequential([
  tf.keras.layers.Reshape((32,32,3), input_shape=(32,32,3)),
  tf.keras.layers.Conv2D(80, 5, activation=tf.nn.relu),
  tf.keras.layers.MaxPool2D(2),
  tf.keras.layers.BatchNormalization(2),

  tf.keras.layers.Conv2D(64, 5, activation=tf.nn.relu),
  tf.keras.layers.MaxPool2D(2),
  tf.keras.layers.BatchNormalization(2),
  tf.keras.layers.Flatten(),

  tf.keras.layers.Dense(1024, activation=tf.nn.relu),
  tf.keras.layers.Dropout(0.8),
  tf.keras.layers.Dense(29, activation=tf.nn.softmax)
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test, y_test)
model.summary()

这是输出

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
reshape (Reshape)            (None, 32, 32, 3)         0
_________________________________________________________________
conv2d (Conv2D)              (None, 28, 28, 80)        6080
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 80)        0
_________________________________________________________________
batch_normalization_v2 (Batc (None, 14, 14, 80)        56
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 10, 10, 64)        128064
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0
_________________________________________________________________
batch_normalization_v2_1 (Ba (None, 5, 5, 64)          20
_________________________________________________________________
flatten (Flatten)            (None, 1600)              0
_________________________________________________________________
dense (Dense)                (None, 1024)              1639424
_________________________________________________________________
dropout (Dropout)            (None, 1024)              0
_________________________________________________________________
dense_1 (Dense)              (None, 29)                29725
=================================================================

我希望这对您有帮助