keras模拟resnet实现带shortcut模型

1.模型代码

import keras
from keras import Input, Model
from keras.datasets import cifar10, cifar100
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Add
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import Adam

num_classes = 20
model_name = 'cifar10.h5'

# The data, shuffled and split between train and test sets:
# (x_train, y_train), (x_test, y_test) = cifar10.load_data()
(x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='coarse')

print(x_train.shape)
x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255

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

inputs = Input(shape=x_train.shape[1:], dtype='float32')

cov1 = Conv2D(32, (3, 3), padding='same')(inputs)
act1 = Activation('relu')(cov1)

# max_pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
# drop1 = Dropout(0.25)(max_pool1)

cov2 = Conv2D(3, (3, 3), padding='same')(act1)
act2 = Activation('relu')(cov2)

# max_pool2 = MaxPooling2D(pool_size=(2, 2))(act2)
# drop2 = Dropout(0.25)(max_pool2)

add = Add()([inputs, act2])

flat = Flatten()(add)

dense1 = Dense(512)(flat)
act3 = Activation('relu')(dense1)
drop3 = Dropout(0.5)(act3)

dense2 = Dense(num_classes)(drop3)
outputs = Activation('softmax')(dense2)

model = Model(input=inputs, output=outputs)
model.summary()

# initiate RMSprop optimizer
# opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6)
opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
# train the model using RMSprop
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

model.fit(x_train, y_train, epochs=1000, batch_size=50000, shuffle=True, validation_data=(x_test, y_test))
model.save(model_name)

# evaluate
loss, accuracy = model.evaluate(x_test, y_test)
print('loss: %f, accurcy: %f' % (loss, accuracy))

2.模型结构

Layer (type)                    Output Shape         Param #     Connected to                     
  model = Model(input=inputs, output=outputs)
==================================================================================================
input_1 (InputLayer)            (None, 32, 32, 3)    0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 32, 32, 32)   896         input_1[0][0]                    
__________________________________________________________________________________________________
activation_1 (Activation)       (None, 32, 32, 32)   0           conv2d_1[0][0]                   
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 32, 32, 3)    867         activation_1[0][0]               
__________________________________________________________________________________________________
activation_2 (Activation)       (None, 32, 32, 3)    0           conv2d_2[0][0]                   
__________________________________________________________________________________________________
add_1 (Add)                     (None, 32, 32, 3)    0           input_1[0][0]                    
                                                                 activation_2[0][0]               
__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 3072)         0           add_1[0][0]                      
__________________________________________________________________________________________________
dense_1 (Dense)                 (None, 512)          1573376     flatten_1[0][0]                  
__________________________________________________________________________________________________
activation_3 (Activation)       (None, 512)          0           dense_1[0][0]                    
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 512)          0           activation_3[0][0]               
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 20)           10260       dropout_1[0][0]                  
__________________________________________________________________________________________________
activation_4 (Activation)       (None, 20)           0           dense_2[0][0]                    
==================================================================================================
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WitsMakeMen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值