def resnet_op(self, x, if_avg_pool): ''' Args: x: input data if_avg_pool:if not 0 , the filter size to make avg_pool ''' net = {} print('layer\t input\t branch1_f\t branch2a_f\t branch2b_f\t branch2c\t output') for i in range(self.num_blocks): print('---------------------------------------------------------') #start from res2a block_info = self.blocks[i] print('-------------------%s----------------------' % (block_info['name'])) scope_name = '%s_a' % block_info['name'] x = self.bottleneck_unit(x, scope_name, block_info['first_stride'], channel_equal=False) net[scope_name] = x for j in range(1, block_info['num_units']): scope_name = '%s_b%d' % (block_info['name'], j) self.x = self.bottleneck_unit(x, scope_name) net[scope_name] = x if if_avg_pool: x = utils.avg_pool(x, if_avg_pool, 1) return x, net
def res_net(weights, image): layers = ('conv1', 'res2a', 'res2b', 'res2c', 'res3a', 'res3b1', 'res3b2', 'res3b3', 'res4a', 'res4b1', 'res4b2', 'res4b3', 'res4b4', 'res4b5', 'res4b6', 'res4b7', 'res4b8', 'res4b9', 'res4b10', 'res4b11', 'res4b12', 'res4b13', 'res4b14', 'res4b15', 'res4b16', 'res4b17', 'res4b18', 'res4b19', 'res4b20', 'res4b21', 'res4b22', 'res5a', 'res5b', 'res5c') res_branch1_parm = { #branch1:padding(if 0 then 'VALID' else 'SMAE'),stride 'res2a': [0, 1, 0, 1, 1, 1, 0, 1], 'res3a': [0, 2, 0, 2, 1, 1, 0, 1], 'res4a': [0, 2, 0, 2, 1, 1, 0, 1], 'res5a': [0, 2, 0, 2, 1, 1, 0, 1] } net = {} current = image n = 0 for i, name in enumerate(layers): kind = name[:3] print('-----------------layer: %s-----------------------' % name) print('Input: ', current.shape) #convolutional if kind == 'con': kernels, bias, n = get_kernel_bias(n, weights, name) print('kernel size: ', kernels.shape, 'bias size', bias.shape) # matconvnet: weights are [width, height, in_channels, out_channels] # tensorflow: weights are [height, width, in_channels, out_channels] current = utils.conv2d_strided(current, kernels, bias) current = utils.max_pool_2x2(current, 3) #resnet elif kind == 'res': sub_kind = name[4] #not blockneck if sub_kind == 'a': res_param = res_branch1_parm[name] branch1_name = '%s_%s' % (name, 'branch1') branch1_w, branch1_b, out_chan_t, n = get_kernel_bias_res( n, weights, branch1_name, 2) print('branch1:kernel size: ', branch1_w.shape, 'bias size', branch1_b.shape) else: res_param = None branch1_w = None branch2a_name = '%s_%s' % (name, 'branch2a') branch2a_w, branch2a_b, out_chan_t2, n = get_kernel_bias_res( n, weights, branch2a_name, 0) print('branch2a:kernel size: ', branch2a_w.shape, 'bias size', branch2a_b.shape) branch2b_name = '%s_%s' % (name, 'branch2b') branch2b_w, branch2b_b, _, n = get_kernel_bias_res( n, weights, branch2b_name, 0) print('branch2b:kernel size: ', branch2b_w.shape, 'bias size', branch2b_b.shape) branch2c_name = '%s_%s' % (name, 'branch2c') branch2c_w, branch2c_b, out_chan2, n = get_kernel_bias_res( n, weights, branch2c_name, 3) print('branch2c:kernel size: ', branch2c_w.shape, 'bias size', branch2c_b.shape) if sub_kind == 'a': out_chan1 = out_chan_t else: out_chan1 = out_chan_t2 current = utils.bottleneck_unit(current, res_param, branch1_w, branch1_b, branch2a_w, branch2a_b, branch2b_w, branch2b_b, branch2c_w, branch2c_b, out_chan1, out_chan2, False, False, name) print('layer output ', current.shape) net[name] = current current = utils.avg_pool(current, 7, 1) print('resnet final sz ', current.shape) #return net return current