Basic Residual module
ResNet model
code
import tensorflow as tf
import os
import numpy as np
import datetime
from tensorflow.keras import layers,Model
from tensorflow.keras.layers import Conv2D,BatchNormalization,Activation,MaxPool2D,Dropout,Flatten,Dense,GlobalAveragePooling2D
cifar10=tf.keras.datasets.cifar10
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
x_train,x_test=x_train/255.0,x_test/255.0
super().__init__()
self.filter=filters
self.strides=strides
self.residual_path=residual_path
self.input_layer=Conv2D(filters,kernel_size=(3,3),strides=strides,padding='same',activation='relu')
self.middle_layers=[
BatchNormalization(),
Conv2D(filters,kernel_size=(3,3),strides=1,padding='same')
]
self.output_layer=BatchNormalization()
if residual_path:
self.down_c1=Conv2D(filters,kernel_size=(1,1),strides=strides,padding='same')
self.down_b1=BatchNormalization()
self.a1=Activation('relu')
def call(self,x):
residual=x
x=input_layer(x)
for layer in self.middle_layers:
x=layer(x)
y=self.output_layer(x)
if self.residual_path:
residual=self.down_c1(residual)
residual=self.down_b1(residual)
return self.a1(y+residual)
class ResNet(Model):
def __init__(self,block_list,filters=64):
super().__init__()
self.num_block=len(block_list)
self.block_list=block_list
self.filters=filters
self.c1=Conv2D(filters,(3,3),strides=1,padding='same',activation='relu')
self.b1=BatchNormalization()
self.blocks=tf.keras.models.Sequential()
self.out_filters=filters
for block_id in range(len(block_list)):
for layers_id in range(block_list[block_id]):
if block_id !=0 and layers_id==0:
self.blocks.add(ResidualBlock(self.out_filters,strides=2,residual_path=True))
else:
self.blocks.add(ResidualBlock(self.out_filters,residual_path=False))
self.out_filters*=2
self.p2=GlobalAveragePooling2D()
self.f2=Dense(10)
def call(self,x):
x=self.c1(x)
x=self.b1(x)
x=self.blocks(x)
x=self.p2(x)
y=self.f2(x)
return y
model=ResNet([2,2,2,2,])
checkpoint_path="./save_path/mnist.ckpt"
if os.path.exists(checkpoints_path):
model.load_weights(checkpoint_path)
cp_callback=tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
save_weights_only=True,
save_best_only=True)
model.compile(optimizer="adam",loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),metrics=['sparse_categorical_accuracy']')
history=model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=1,callbacks=[cp_callback])
model.summary()