手写汉字笔迹识别模型汇总
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | 手写汉字笔迹识别模型: 第一名用的是googleNet,准确率 97.3 % def GoogleLeNetSlim(x, num_classes, keep_prob = 0.5 ): with tf.variable_scope( 'main' ): t = slim.conv2d(x, 64 , [ 3 , 3 ], [ 1 , 1 ], padding = 'SAME' , activation_fn = relu, normalizer_fn = slim.batch_norm, scope = 'conv1' ) t = slim.max_pool2d(t, [ 2 , 2 ], [ 2 , 2 ], padding = 'SAME' ) t = slim.conv2d(t, 96 , [ 3 , 3 ], [ 1 , 1 ], padding = 'SAME' , activation_fn = relu, normalizer_fn = slim.batch_norm, scope = 'conv2' ) t = slim.conv2d(t, 192 , [ 3 , 3 ], [ 1 , 1 ], padding = 'SAME' , activation_fn = relu, normalizer_fn = slim.batch_norm, scope = 'conv3' ) t = slim.max_pool2d(t, [ 2 , 2 ], [ 2 , 2 ], padding = 'SAME' ) with tf.variable_scope( 'block1' ): t = block_slim(t, [ 64 , 96 , 128 , 16 , 32 , 32 ], name = 'block1' ) # [?, 16, 16, 256] with tf.variable_scope( 'block2' ): t = block_slim(t, [ 128 , 128 , 192 , 32 , 96 , 64 ], name = 'block1' ) # [?, 16, 16, 480] t = tf.nn.max_pool(t, ksize = [ 1 , 2 , 2 , 1 ], strides = [ 1 , 2 , 2 , 1 ], padding = 'SAME' ) with tf.variable_scope( 'block3' ): t = block_slim(t, [ 192 , 96 , 208 , 16 , 48 , 64 ], name = 'block1' ) t = block_slim(t, [ 160 , 112 , 224 , 24 , 64 , 64 ], name = 'block2' ) t = block_slim(t, [ 128 , 128 , 256 , 24 , 64 , 64 ], name = 'block3' ) t = block_slim(t, [ 112 , 144 , 288 , 32 , 64 , 64 ], name = 'block4' ) t = block_slim(t, [ 256 , 160 , 320 , 32 , 128 , 128 ], name = 'block5' ) # [?, 8, 8, 832] t = tf.nn.max_pool(t, ksize = [ 1 , 2 , 2 , 1 ], strides = [ 1 , 2 , 2 , 1 ], padding = 'SAME' ) with tf.variable_scope( 'block4' ): t = block_slim(t, [ 256 , 160 , 320 , 32 , 128 , 128 ], name = 'block1' ) t = block_slim(t, [ 384 , 192 , 384 , 48 , 128 , 128 ], name = 'block2' ) # [?, 8, 8, 1024] t = tf.nn.max_pool(t, ksize = [ 1 , 2 , 2 , 1 ], strides = [ 1 , 2 , 2 , 1 ], padding = 'SAME' ) with tf.variable_scope( 'fc' ): t = slim.flatten(t) t = slim.fully_connected(slim.dropout(t, keep_prob), 1024 , activation_fn = relu, normalizer_fn = slim.batch_norm, scope = 'fc1' ) t = slim.fully_connected(slim.dropout(t, keep_prob), num_classes, activation_fn = None , scope = 'logits' ) return t TODO:实验下,https: / / github.com / tflearn / tflearn / blob / master / examples / images / googlenet.py 还有使用inception v3的!!! def build_graph_all(top_k,scope = None ): keep_prob = tf.placeholder(dtype = tf.float32, shape = [], name = 'keep_prob' ) images = tf.placeholder(dtype = tf.float32, shape = [ None , image_size, image_size, 1 ], name = 'image_batch' ) labels = tf.placeholder(dtype = tf.int64, shape = [ None ], name = 'label_batch' ) with tf.variable_scope(scope, 'Incept_Net' ,[images]): with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride = 1 ,padding = 'VALID' ): net = slim.conv2d(images, 32 ,[ 3 , 3 ],scope = 'conv2d_1a_3x3' ) print ( 'tensor 1:' + str (net.get_shape().as_list())) net = slim.conv2d(net, 32 ,[ 3 , 3 ],scope = 'conv2d_2a_3x3' ) print ( 'tensor 2:' + str (net.get_shape().as_list())) net = slim.conv2d(net, 64 ,[ 3 , 3 ],padding = 'SAME' ,scope = 'conv2d_2b_3x3' ) print ( 'tensor 3:' + str (net.get_shape().as_list())) net = slim.max_pool2d(net,[ 3 , 3 ],stride = 2 ,scope = 'maxpool_3a_3x3' ) print ( 'tensor 4:' + str (net.get_shape().as_list())) net = slim.conv2d(net, 80 ,[ 1 , 1 ],scope = 'conv2d_3b_1x1' ) print ( 'tensor 5:' + str (net.get_shape().as_list())) net = slim.conv2d(net, 192 ,[ 3 , 3 ],scope = 'conv2d_4a_3x3' ) print ( 'tensor 6:' + str (net.get_shape().as_list())) net = slim.max_pool2d(net,[ 3 , 3 ],stride = 2 ,scope = 'maxpool_5a_3x3' ) print ( 'tensor 7:' + str (net.get_shape().as_list())) with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride = 1 ,padding = 'SAME' ): with tf.variable_scope( 'mixed_5b' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 48 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 64 ,[ 5 , 5 ],scope = 'conv2d_0b_5x5' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0c_3x3' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 32 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 8:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_5c' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 48 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) branch_1 = slim.conv2d(branch_1, 64 ,[ 5 , 5 ],scope = 'conv2d_0c_5x5' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0c_3x3' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 64 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 9:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_5d' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 48 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 64 ,[ 5 , 5 ],scope = 'conv2d_0b_5x5' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_2 = slim.conv2d(branch_2, 96 ,[ 3 , 3 ],scope = 'conv2d_0c_3x3' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 64 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 10:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_6a' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 384 ,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'conv2d_1a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 64 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 96 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_1 = slim.conv2d(branch_1, 96 ,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'conv2d_1a_1x1' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.max_pool2d(net,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'maxpool_1a_3x3' ) net = tf.concat([branch_0,branch_1,branch_2], 3 ) print ( 'tensor 11:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_6b' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 128 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 128 ,[ 1 , 7 ],scope = 'conv2d_0b_1x7' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 7 , 1 ],scope = 'conv2d_0c_7x1' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 128 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 128 ,[ 7 , 1 ],scope = 'conv2d_0b_7x1' ) branch_2 = slim.conv2d(branch_2, 128 ,[ 1 , 7 ],scope = 'conv2d_0c_1x7' ) branch_2 = slim.conv2d(branch_2, 128 ,[ 7 , 1 ],scope = 'conv2d_0d_7x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 1 , 7 ],scope = 'conv2d_0e_1x7' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 12:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_6c' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 160 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 160 ,[ 1 , 7 ],scope = 'conv2d_0b_1x7' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 7 , 1 ],scope = 'conv2d_0c_7x1' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 160 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 7 , 1 ],scope = 'conv2d_0b_7x1' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 1 , 7 ],scope = 'conv2d_0c_1x7' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 7 , 1 ],scope = 'conv2d_0d_7x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 1 , 7 ],scope = 'conv2d_0e_1x7' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 13:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_6d' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 160 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 160 ,[ 1 , 7 ],scope = 'conv2d_0b_1x7' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 7 , 1 ],scope = 'conv2d_0c_7x1' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 160 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 7 , 1 ],scope = 'conv2d_0b_7x1' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 1 , 7 ],scope = 'conv2d_0c_1x7' ) branch_2 = slim.conv2d(branch_2, 160 ,[ 7 , 1 ],scope = 'conv2d_0d_7x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 1 , 7 ],scope = 'conv2d_0e_1x7' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 14:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_6e' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 1 , 7 ],scope = 'conv2d_0b_1x7' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 7 , 1 ],scope = 'conv2d_0c_7x1' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 7 , 1 ],scope = 'conv2d_0b_7x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 1 , 7 ],scope = 'conv2d_0c_1x7' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 7 , 1 ],scope = 'conv2d_0d_7x1' ) branch_2 = slim.conv2d(branch_2, 192 ,[ 1 , 7 ],scope = 'conv2d_0e_1x7' ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 15:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_7a' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_0 = slim.conv2d(branch_0, 320 ,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'conv2d_1a_3x3' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 192 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 1 , 7 ],scope = 'conv2d_0b_1x7' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 7 , 1 ],scope = 'conv2d_0c_7x1' ) branch_1 = slim.conv2d(branch_1, 192 ,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'conv2d_1a_3x3' ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.max_pool2d(net,[ 3 , 3 ],stride = 2 ,padding = 'VALID' ,scope = 'maxpool_1a_3x3' ) net = tf.concat([branch_0,branch_1,branch_2], 3 ) print ( 'tensor 16:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_7b' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 320 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 384 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = tf.concat([ slim.conv2d(branch_1, 384 ,[ 1 , 3 ],scope = 'conv2d_0b_1x3' ), slim.conv2d(branch_1, 384 ,[ 3 , 1 ],scope = 'conv2d_0b_3x1' ) ], 3 ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 448 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 384 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_2 = tf.concat([ slim.conv2d(branch_2, 384 ,[ 1 , 3 ],scope = 'conv2d_0c_1x3' ), slim.conv2d(branch_2, 384 ,[ 3 , 1 ],scope = 'conv2d_0d_3x1' ) ], 3 ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 17:' + str (net.get_shape().as_list())) with tf.variable_scope( 'mixed_7c' ): with tf.variable_scope( 'branch_0' ): branch_0 = slim.conv2d(net, 320 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) with tf.variable_scope( 'branch_1' ): branch_1 = slim.conv2d(net, 384 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_1 = tf.concat([ slim.conv2d(branch_1, 384 ,[ 1 , 3 ],scope = 'conv2d_0b_1x3' ), slim.conv2d(branch_1, 384 ,[ 3 , 1 ],scope = 'conv2d_0c_3x1' )], 3 ) with tf.variable_scope( 'branch_2' ): branch_2 = slim.conv2d(net, 448 ,[ 1 , 1 ],scope = 'conv2d_0a_1x1' ) branch_2 = slim.conv2d(branch_2, 384 ,[ 3 , 3 ],scope = 'conv2d_0b_3x3' ) branch_2 = tf.concat([ slim.conv2d(branch_2, 384 ,[ 1 , 3 ],scope = 'conv2d_0c_1x3' ), slim.conv2d(branch_2, 384 ,[ 3 , 1 ],scope = 'conv2d_0d_3x1' )], 3 ) with tf.variable_scope( 'branch_3' ): branch_3 = slim.avg_pool2d(net,[ 3 , 3 ],scope = 'avgpool_0a_3x3' ) branch_3 = slim.conv2d(branch_3, 192 ,[ 1 , 1 ],scope = 'conv2d_0b_1x1' ) net = tf.concat([branch_0,branch_1,branch_2,branch_3], 3 ) print ( 'tensor 18:' + str (net.get_shape().as_list())) with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],stride = 1 ,padding = 'SAME' ): with tf.variable_scope( 'logits' ): net = slim.avg_pool2d(net,[ 3 , 3 ],padding = 'VALID' ,scope = 'avgpool_1a_3x3' ) print ( 'tensor 19:' + str (net.get_shape().as_list())) net = slim.dropout(net,keep_prob = keep_prob,scope = 'dropout_1b' ) logits = slim.conv2d(net, char_size,[ 2 , 2 ],padding = 'VALID' ,activation_fn = None ,normalizer_fn = None , scope = 'conv2d_1c_2x2' ) print ( 'logits 1:' + str (logits.get_shape().as_list())) logits = tf.squeeze(logits,[ 1 , 2 ],name = 'spatialsqueeze' ) print ( 'logits 2:' + str (logits.get_shape().as_list())) regularization_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)) loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits, labels = labels)) total_loss = loss + regularization_loss print ( 'get total_loss' ) accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(logits, 1 ), labels), tf.float32)) global_step = tf.get_variable( "step" , [], initializer = tf.constant_initializer( 0.0 ), trainable = False ) rate = tf.train.exponential_decay( 2e - 3 , global_step, decay_steps = 2000 , decay_rate = 0.97 , staircase = True ) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = tf.train.AdamOptimizer(learning_rate = rate).minimize(total_loss, global_step = global_step) probabilities = tf.nn.softmax(logits) tf.summary.scalar( 'loss' , loss) tf.summary.scalar( 'accuracy' , accuracy) merged_summary_op = tf.summary.merge_all() predicted_val_top_k, predicted_index_top_k = tf.nn.top_k(probabilities, k = top_k) accuracy_in_top_k = tf.reduce_mean(tf.cast(tf.nn.in_top_k(probabilities, labels, top_k), tf.float32)) return { 'images' : images, 'labels' : labels, 'keep_prob' : keep_prob, 'top_k' : top_k, 'global_step' : global_step, 'train_op' : train_op, 'loss' : total_loss, 'accuracy' : accuracy, 'accuracy_top_k' : accuracy_in_top_k, 'merged_summary_op' : merged_summary_op, 'predicted_distribution' : probabilities, 'predicted_index_top_k' : predicted_index_top_k, 'predicted_val_top_k' : predicted_val_top_k} 用resnet v2的: resnet_v2.default_image_size = 128 def resnet_v2_50(inputs, num_classes = None , is_training = True , global_pool = True , output_stride = None , spatial_squeeze = True , reuse = None , scope = 'resnet_v2_50' ): """ResNet-50 model of [1]. See resnet_v2() for arg and return description.""" blocks = [ resnet_v2_block( 'block1' , base_depth = 64 , num_units = 3 , stride = 2 ), resnet_v2_block( 'block2' , base_depth = 128 , num_units = 4 , stride = 2 ), resnet_v2_block( 'block3' , base_depth = 256 , num_units = 6 , stride = 2 ), resnet_v2_block( 'block4' , base_depth = 512 , num_units = 3 , stride = 1 ), ] return resnet_v2(inputs, blocks, num_classes, is_training = is_training, global_pool = global_pool, output_stride = output_stride, include_root_block = True , spatial_squeeze = spatial_squeeze, reuse = reuse, scope = scope) resnet_v2_50.default_image_size = resnet_v2.default_image_size def resnet_v2_101(inputs, num_classes = None , is_training = True , global_pool = True , output_stride = None , spatial_squeeze = True , reuse = None , scope = 'resnet_v2_101' ): """ResNet-101 model of [1]. See resnet_v2() for arg and return description.""" blocks = [ resnet_v2_block( 'block1' , base_depth = 64 , num_units = 3 , stride = 2 ), resnet_v2_block( 'block2' , base_depth = 128 , num_units = 4 , stride = 2 ), resnet_v2_block( 'block3' , base_depth = 256 , num_units = 23 , stride = 2 ), resnet_v2_block( 'block4' , base_depth = 512 , num_units = 3 , stride = 1 ), ] return resnet_v2(inputs, blocks, num_classes, is_training = is_training, global_pool = global_pool, output_stride = output_stride, include_root_block = True , spatial_squeeze = spatial_squeeze, reuse = reuse, scope = scope) def build_graph(top_k, is_training): # with tf.device('/cpu:0'): keep_prob = tf.placeholder(dtype = tf.float32, shape = [], name = 'keep_prob' ) images = tf.placeholder(dtype = tf.float32, shape = [ None , 128 , 128 , 1 ], name = 'image_batch' ) labels = tf.placeholder(dtype = tf.int64, shape = [ None ], name = 'label_batch' ) logits, _ = resnet_v2_50(images, num_classes = 3755 , is_training = is_training, global_pool = True , output_stride = None , spatial_squeeze = True , reuse = None ) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」