| 105 | |
| 106 | |
| 107 | class TFResNet: |
| 108 | def __init__(self): |
| 109 | self.layers = [ |
| 110 | # before conv block |
| 111 | ConvLayer(d=7, mi=3, mo=64, stride=2, padding='SAME'), |
| 112 | BatchNormLayer(64), |
| 113 | ReLULayer(), |
| 114 | MaxPoolLayer(dim=3), |
| 115 | # conv block |
| 116 | ConvBlock(mi=64, fm_sizes=[64, 64, 256], stride=1), |
| 117 | # identity block x 2 |
| 118 | IdentityBlock(mi=256, fm_sizes=[64, 64, 256]), |
| 119 | IdentityBlock(mi=256, fm_sizes=[64, 64, 256]), |
| 120 | # conv block |
| 121 | ConvBlock(mi=256, fm_sizes=[128, 128, 512], stride=2), |
| 122 | # identity block x 3 |
| 123 | IdentityBlock(mi=512, fm_sizes=[128, 128, 512]), |
| 124 | IdentityBlock(mi=512, fm_sizes=[128, 128, 512]), |
| 125 | IdentityBlock(mi=512, fm_sizes=[128, 128, 512]), |
| 126 | # conv block |
| 127 | ConvBlock(mi=512, fm_sizes=[256, 256, 1024], stride=2), |
| 128 | # identity block x 5 |
| 129 | IdentityBlock(mi=1024, fm_sizes=[256, 256, 1024]), |
| 130 | IdentityBlock(mi=1024, fm_sizes=[256, 256, 1024]), |
| 131 | IdentityBlock(mi=1024, fm_sizes=[256, 256, 1024]), |
| 132 | IdentityBlock(mi=1024, fm_sizes=[256, 256, 1024]), |
| 133 | IdentityBlock(mi=1024, fm_sizes=[256, 256, 1024]), |
| 134 | # conv block |
| 135 | ConvBlock(mi=1024, fm_sizes=[512, 512, 2048], stride=2), |
| 136 | # identity block x 2 |
| 137 | IdentityBlock(mi=2048, fm_sizes=[512, 512, 2048]), |
| 138 | IdentityBlock(mi=2048, fm_sizes=[512, 512, 2048]), |
| 139 | # pool / flatten / dense |
| 140 | AvgPool(ksize=7), |
| 141 | Flatten(), |
| 142 | DenseLayer(mi=2048, mo=1000) |
| 143 | ] |
| 144 | self.input_ = tf.placeholder(tf.float32, shape=(None, 224, 224, 3)) |
| 145 | self.output = self.forward(self.input_) |
| 146 | |
| 147 | def copyFromKerasLayers(self, layers): |
| 148 | # conv |
| 149 | self.layers[0].copyFromKerasLayers(layers[1]) |
| 150 | # bn |
| 151 | self.layers[1].copyFromKerasLayers(layers[2]) |
| 152 | # cb |
| 153 | self.layers[4].copyFromKerasLayers(layers[5:17]) # size=12 |
| 154 | # ib x 2 |
| 155 | self.layers[5].copyFromKerasLayers(layers[17:27]) # size=10 |
| 156 | self.layers[6].copyFromKerasLayers(layers[27:37]) |
| 157 | # cb |
| 158 | self.layers[7].copyFromKerasLayers(layers[37:49]) |
| 159 | # ib x 3 |
| 160 | self.layers[8].copyFromKerasLayers(layers[49:59]) |
| 161 | self.layers[9].copyFromKerasLayers(layers[59:69]) |
| 162 | self.layers[10].copyFromKerasLayers(layers[69:79]) |
| 163 | # cb |
| 164 | self.layers[11].copyFromKerasLayers(layers[79:91]) |