[英]tensorflow/keras CNN model Architecture
我想創建一個受圖像啟發的某些CNN模型,所以我試圖這樣做,但沒有成功: 這里的圖像
當我試圖實現這種架構時
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
這是在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
=================================================================
我希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.