|
10 | 10 | import numpy as np
|
11 | 11 |
|
12 | 12 |
|
| 13 | +def resnet_cifar10(input, depth=32, program=None, init_program=None): |
| 14 | + def conv_bn_layer(input, |
| 15 | + ch_out, |
| 16 | + filter_size, |
| 17 | + stride, |
| 18 | + padding, |
| 19 | + act='relu', |
| 20 | + program=None, |
| 21 | + init_program=None): |
| 22 | + tmp = layers.conv2d( |
| 23 | + input=input, |
| 24 | + filter_size=filter_size, |
| 25 | + num_filters=ch_out, |
| 26 | + stride=stride, |
| 27 | + padding=padding, |
| 28 | + act=None, |
| 29 | + bias_attr=False, |
| 30 | + program=program, |
| 31 | + init_program=init_program) |
| 32 | + return layers.batch_norm( |
| 33 | + input=tmp, act=act, program=program, init_program=init_program) |
| 34 | + |
| 35 | + def shortcut(input, ch_in, ch_out, stride, program, init_program): |
| 36 | + if ch_in != ch_out: |
| 37 | + return conv_bn_layer(input, ch_out, 1, stride, 0, None, program, |
| 38 | + init_program) |
| 39 | + else: |
| 40 | + return input |
| 41 | + |
| 42 | + def basicblock(input, |
| 43 | + ch_in, |
| 44 | + ch_out, |
| 45 | + stride, |
| 46 | + program=program, |
| 47 | + init_program=init_program): |
| 48 | + tmp = conv_bn_layer( |
| 49 | + input, |
| 50 | + ch_out, |
| 51 | + 3, |
| 52 | + stride, |
| 53 | + 1, |
| 54 | + program=program, |
| 55 | + init_program=init_program) |
| 56 | + tmp = conv_bn_layer( |
| 57 | + tmp, |
| 58 | + ch_out, |
| 59 | + 3, |
| 60 | + 1, |
| 61 | + 1, |
| 62 | + act=None, |
| 63 | + program=program, |
| 64 | + init_program=init_program) |
| 65 | + short = shortcut(input, ch_in, ch_out, stride, program, init_program) |
| 66 | + return layers.elementwise_add( |
| 67 | + x=tmp, |
| 68 | + y=short, |
| 69 | + act='relu', |
| 70 | + program=program, |
| 71 | + init_program=init_program) |
| 72 | + |
| 73 | + def layer_warp(block_func, input, ch_in, ch_out, count, stride, program, |
| 74 | + init_program): |
| 75 | + tmp = block_func(input, ch_in, ch_out, stride, program, init_program) |
| 76 | + for i in range(1, count): |
| 77 | + tmp = block_func(tmp, ch_out, ch_out, 1, program, init_program) |
| 78 | + return tmp |
| 79 | + |
| 80 | + assert (depth - 2) % 6 == 0 |
| 81 | + n = (depth - 2) / 6 |
| 82 | + conv1 = conv_bn_layer( |
| 83 | + input=input, |
| 84 | + ch_out=16, |
| 85 | + filter_size=3, |
| 86 | + stride=1, |
| 87 | + padding=1, |
| 88 | + program=program, |
| 89 | + init_program=init_program) |
| 90 | + res1 = layer_warp( |
| 91 | + basicblock, |
| 92 | + conv1, |
| 93 | + 16, |
| 94 | + 16, |
| 95 | + n, |
| 96 | + 1, |
| 97 | + program=program, |
| 98 | + init_program=init_program) |
| 99 | + res2 = layer_warp( |
| 100 | + basicblock, |
| 101 | + res1, |
| 102 | + 16, |
| 103 | + 32, |
| 104 | + n, |
| 105 | + 2, |
| 106 | + program=program, |
| 107 | + init_program=init_program) |
| 108 | + res3 = layer_warp( |
| 109 | + basicblock, |
| 110 | + res2, |
| 111 | + 32, |
| 112 | + 64, |
| 113 | + n, |
| 114 | + 2, |
| 115 | + program=program, |
| 116 | + init_program=init_program) |
| 117 | + pool = layers.pool2d( |
| 118 | + input=res3, |
| 119 | + pool_size=8, |
| 120 | + pool_type='avg', |
| 121 | + pool_stride=1, |
| 122 | + program=program, |
| 123 | + init_program=init_program) |
| 124 | + return pool |
| 125 | + |
| 126 | + |
13 | 127 | def vgg16_bn_drop(input, program, init_program):
|
14 | 128 | def conv_block(input,
|
15 | 129 | num_filter,
|
@@ -75,8 +189,16 @@ def conv_block(input,
|
75 | 189 | data_type='int64',
|
76 | 190 | program=program,
|
77 | 191 | init_program=init_program)
|
78 |
| -vgg_net = vgg16_bn_drop(images, program, init_program) |
79 |
| -predict = layers.fc(input=vgg_net, |
| 192 | + |
| 193 | +# Add neural network config |
| 194 | +# option 1. resnet |
| 195 | +net = resnet_cifar10(images, 32, program, init_program) |
| 196 | +# option 2. vgg |
| 197 | +# net = vgg16_bn_drop(images, program, init_program) |
| 198 | + |
| 199 | +# print(program) |
| 200 | + |
| 201 | +predict = layers.fc(input=net, |
80 | 202 | size=classdim,
|
81 | 203 | act='softmax',
|
82 | 204 | program=program,
|
@@ -123,8 +245,8 @@ def conv_block(input,
|
123 | 245 | fetch_list=[avg_cost])
|
124 | 246 |
|
125 | 247 | loss = np.array(outs[0])
|
126 |
| - # print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) + |
127 |
| - # " loss:" + str(loss)) |
| 248 | + print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) + |
| 249 | + " loss:" + str(loss)) |
128 | 250 | batch_id = batch_id + 1
|
129 | 251 |
|
130 | 252 | if batch_id > 1:
|
|
0 commit comments