Creating Deeper Bottleneck ResNet from Scratch using Tensorflow
大家好,最近我一直在学习如何根据上面给出的链接使用 tf.keras 创建 ResNet50。我的问题是,要训练这台机器,我应该用我希望训练的数据集分配“输入”变量(第 75 行)吗?如果是这样,我该怎么做?
#import the needed libraries
from tensorflow.python.keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
import pydot
import graphviz
from tensorflow.keras.layers import Input, Conv2D, BatchNormalization
from tensorflow.keras.layers import MaxPool2D, GlobalAvgPool2D
from tensorflow.keras.layers import Add, ReLU, Dense
from tensorflow.keras import Model
'''import tensorflow
print(tensorflow.__version__)'''
#Conv-BatchNorm-ReLU block
def conv_batchnorm_relu(x, filters, kernel_size, strides=1):
x = Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same')(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
#identity block
def identity_block(tensor, filters):
x = conv_batchnorm_relu(tensor, filters = filters, kernel_size = 1, strides = 1)
x = conv_batchnorm_relu(x, filters = filters, kernel_size = 3, strides = 1)
x = Conv2D(filters = 4 * filters, kernel_size = 1, strides = 1)(x)
x = BatchNormalization()(x)
x = Add()([tensor, x]) #skip connection
x = ReLU()(x)
return x
#Projection block
def projection_block(tensor, filters, strides):
#left stream
x = conv_batchnorm_relu(tensor, filters = filters, kernel_size = 1, strides = strides)
x = conv_batchnorm_relu(x, filters = filters, kernel_size = 3, strides = 1)
x = Conv2D(filters = 4 * filters, kernel_size = 1, strides = 1)(x)
x = BatchNormalization()(x)
#right stream
shortcut = Conv2D(filters = 4 * filters, kernel_size = 1, strides = strides)(tensor)
shortcut = BatchNormalization()(shortcut)
x = Add()([shortcut, x]) #skip connection
x = ReLU()(x)
return x
#Resnet Block
#First block is always projection. While others are identity blocks
def resnet_block(x, filters, reps, strides):
x = projection_block(x, filters, strides)
for i in range(reps-1):
x = identity_block(x, filters)
return x
#Create Model
input = Input(shape = (244,244,3)) #Should I assign this input variable with dataset? If so, how do I do that?
#################
x = conv_batchnorm_relu(input, filters = 64, kernel_size = 7, strides = 2)
x = MaxPool2D(pool_size = 3, strides = 2)(x)
x = resnet_block(x, filters = 64, reps = 3, strides = 1)
x = resnet_block(x, filters=128, reps =4, strides=2)
x = resnet_block(x, filters=256, reps =6, strides=2)
x = resnet_block(x, filters=512, reps =3, strides=2)
x = GlobalAvgPool2D()(x)
#################
output = Dense(1000, activation = 'softmax')(x)
model = Model(inputs=input, outputs=output)
model.summary()
SVG(model_to_dot(model, show_shapes=True, show_layer_names=True, rankdir='TB',expand_nested=False,
dpi=60, subgraph=False).create(prog='dot',format='svg'))