-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
101 lines (75 loc) · 2.18 KB
/
models.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
98
99
100
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_datasets as tfds
import numpy as np
NORM = tf.keras.layers.LayerNormalization
def plain_res_block(x_in, filters, stride, bn):
x = layers.Conv2D(
filters=filters,
kernel_size=3,
strides=stride,
padding="same",
use_bias=False
)(x_in)
if bn:
x = NORM()(x)
x = layers.Activation("relu")(x)
x = layers.Conv2D(
filters=filters,
kernel_size=3,
strides=1,
padding="same",
use_bias=False
)(x)
if bn:
x = NORM()(x)
x = layers.Activation("relu")(x)
if stride > 1:
x_in = layers.Conv2D(
filters=filters,
kernel_size=1,
strides=stride,
padding="same",
use_bias=False
)(x_in)
if bn:
x_in = NORM()(x_in)
x = x + x_in
return x
def resnet20(input_shape, num_classes, bn=True, activation='relu'):
x_in = layers.Input(input_shape)
x = layers.Conv2D(filters=16, kernel_size=3, strides=1, padding="same", use_bias=False)(x_in)
if bn:
x = NORM()(x)
x = layers.Activation("relu")(x)
x = plain_res_block(x, 16, 1, bn)
x = plain_res_block(x, 16, 1, bn)
x = plain_res_block(x, 16, 1, bn)
x = plain_res_block(x, 32, 2, bn)
x = plain_res_block(x, 32, 1, bn)
x = plain_res_block(x, 32, 1, bn)
x = plain_res_block(x, 64, 2, bn)
x = plain_res_block(x, 64, 1, bn)
x = plain_res_block(x, 64, 1, bn)
x = layers.AveragePooling2D(pool_size=8)(x)
x = layers.Flatten()(x)
logits = tf.keras.layers.Dense(num_classes)(x)
y_ = tf.nn.softmax(logits)
return tf.keras.Model(x_in, y_)
def sparse_classification_loss(y, y_):
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)(y, y_)
loss = tf.reduce_mean(loss)
return loss
def accuracy(y, y_):
y_ = tf.argmax(y_, 1)
b = y == y_
b = tf.cast(b, tf.float32)
return tf.reduce_mean(b)
models = {
'resnet20':resnet20,
}
canaries = {
'resnet20':{
'last_layer':(68, -2, 0)
},
}