-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResNet by Model subclassing.py
97 lines (59 loc) · 2.84 KB
/
ResNet by Model subclassing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'''ResNet18 by Model-subclassing and Functional API:
'''
import tensorflow as tf
import tensorflow_datasets as tfds
#Object Oriented Programming:
class IdentityBlock(tf.keras.Model):
def __init__(self, filters, kernel_size): #this means► IdentityBlock(filters, kernel_size)
super(IdentityBlock, self).__init__(name='')
self.conv1 = tf.keras.layers.Conv2D(filters, kernel_size, padding='same')
self.bn1 = tf.keras.layers.BatchNormalization()
self.conv2 = tf.keras.layers.Conv2D(filters, kernel_size, padding='same')
self.bn2 = tf.keras.layers.BatchNormalization()
self.act = tf.keras.layers.Activation('relu')
self.add = tf.keras.layers.Add() #to add the input data (input tensor) with the output of main path (x)
def call(self, input_tensor): #Functional API in order from init:
x = self.conv1(input_tensor)
x = self.bn1(x)
x = self.act(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.add([x, input_tensor])
x = self.act(x)
return x
class ResNet(tf.keras.Model):
def __init__(self, num_classes): #► Resnet(num_classes)
super(ResNet, self).__init__()
self.conv = tf.keras.layers.Conv2D(64, 7, padding='same') #1) initial layers
self.bn = tf.keras.layers.BatchNormalization()
self.act = tf.keras.layers.Activation('relu')
self.max_pool = tf.keras.layers.MaxPool2D((3, 3)) #downsampling
# Use the Identity blocks that you just defined
self.id1a = IdentityBlock(64, 3) #2) IdentityBlock layers
self.id1b = IdentityBlock(64, 3)
self.global_pool = tf.keras.layers.GlobalAveragePooling2D()
self.classifier = tf.keras.layers.Dense(num_classes, activation='softmax')
def call(self, inputs):
x = self.conv(inputs)
x = self.bn(x)
x = self.act(x)
x = self.max_pool(x)
# insert the identity blocks in the middle of the network
x = self.id1a(x)
x = self.id1b(x)
x = self.global_pool(x)
return self.classifier(x)
#_________________________________________________________________________________________
'''Training the Model:
'''
# utility function to normalize the images and return (image, label) pairs.
def preprocess(features):
return tf.cast(features['image'], tf.float32) / 255., features['label']
# create a ResNet instance with 10 output units for MNIST
resnet = ResNet(10)
resnet.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# load and preprocess the dataset
dataset = tfds.load('mnist', split=tfds.Split.TRAIN, data_dir='./data')
dataset = dataset.map(preprocess).batch(32)
# train the model.
resnet.fit(dataset, epochs=1)