【问题标题】:tensorflow/keras CNN model Architecturetensorflow/keras CNN 模型架构
【发布时间】:2019-04-27 18:03:53
【问题描述】:

我想创建某些受图像启发的 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

【问题讨论】:

  • 如果您能详细说明您的疑问或问题,将会很有帮助
  • @ShubhamPanchal 我花了 2 天时间才发帖!!!
  • 在知道您的问题之前,我们如何才能给出答案?

标签: python tensorflow keras


【解决方案1】:

这是在 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
=================================================================

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-04
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    相关资源
    最近更新 更多