def Pooling(op, # PoolingType_Max or _Average filter_shape, # e.g. (3,3) strides=1, pad=False): x = Placeholder(name='pooling_arg') apply_x = pooling (x, op, filter_shape, strides=_as_tuple(strides), auto_padding=_as_tuple(pad)) if op == PoolingType_Average: op_name = 'AveragePooling' elif op == PoolingType_Max: op_name = 'MaxPooling' else: raise ValueError('Pooling: op must be PoolingType_Max or PoolingType_average') return Block(apply_x, op_name)
def create_resnet_model(input, num_classes): bn_time_const = 4096 c_map1 = 16 feat_scale = 0.00390625 input_norm = element_times(feat_scale, input) conv = conv_bn_relu_layer(input_norm, c_map1, [3, 3], [1, 1], bn_time_const) r1_1 = resnet_basic_stack3(conv, c_map1, bn_time_const) c_map2 = 32 r2_1 = resnet_basic_inc(r1_1, c_map2, [2, 2], bn_time_const) r2_2 = resnet_basic_stack2(r2_1, c_map2, bn_time_const) c_map3 = 64 r3_1 = resnet_basic_inc(r2_2, c_map3, [2, 2], bn_time_const) r3_2 = resnet_basic_stack2(r3_1, c_map3, bn_time_const) # Global average pooling poolw = 8 poolh = 8 poolh_stride = 1 poolv_stride = 1 pool = pooling(r3_2, AVG_POOLING, (1, poolh, poolw), (1, poolv_stride, poolh_stride)) return linear_layer(pool, num_classes)
def create_resnet_model(input, num_classes): bn_time_const = 4096 c_map1 = 16 feat_scale = 0.00390625 input_norm = element_times(feat_scale, input) conv = conv_bn_relu_layer(input, c_map1, [3, 3], [1, 1], bn_time_const) r1_1 = resnet_basic_stack3(conv, c_map1, bn_time_const) c_map2 = 32 r2_1 = resnet_basic_inc(r1_1, c_map2, [2, 2], bn_time_const) r2_2 = resnet_basic_stack2(r2_1, c_map2, bn_time_const) c_map3 = 64 r3_1 = resnet_basic_inc(r2_2, c_map3, [2, 2], bn_time_const) r3_2 = resnet_basic_stack2(r3_1, c_map3, bn_time_const) # Global average pooling poolw = 8 poolh = 8 poolh_stride = 1 poolv_stride = 1 pool = pooling(r3_2, AVG_POOLING, (1, poolh, poolw), (1, poolv_stride, poolh_stride)) return linear_layer(pool, num_classes)
def resnet_classifer(input, num_classes): conv_w_scale = 7.07 conv_b_value = 0 fc1_w_scale = 0.4 fc1_b_value = 0 sc_value = 1 bn_time_const = 4096 kernel_width = 3 kernel_height = 3 conv1_w_scale = 0.26 c_map1 = 16 conv1 = conv_bn_relu_layer(input, c_map1, kernel_width, kernel_height, 1, 1, conv1_w_scale, conv_b_value, sc_value, bn_time_const) rn1_1 = resnet_node2(conv1, c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) rn1_2 = resnet_node2(rn1_1, c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) rn1_3 = resnet_node2(rn1_2, c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) c_map2 = 32 rn2_1_wProj = get_projection_map(c_map2, c_map1) rn2_1 = resnet_node2_inc(rn1_3, c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, rn2_1_wProj) rn2_2 = resnet_node2(rn2_1, c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) rn2_3 = resnet_node2(rn2_2, c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) c_map3 = 64 rn3_1_wProj = get_projection_map(c_map3, c_map2) rn3_1 = resnet_node2_inc(rn2_3, c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, rn3_1_wProj) rn3_2 = resnet_node2(rn3_1, c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) rn3_3 = resnet_node2(rn3_2, c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const) # Global average pooling poolw = 8 poolh = 8 poolh_stride = 1 poolv_stride = 1 pool = pooling(rn3_3, AVG_POOLING, (1, poolh, poolw), (1, poolv_stride, poolh_stride)) out_times_params = parameter(shape=(c_map3, 1, 1, num_classes), init=glorot_uniform()) out_bias_params = parameter(shape=(num_classes), init=0) t = times(pool, out_times_params) return t + out_bias_params
def pooling(cntk_layer, inputs): ''' Setup pooling op with given parameters Args: cntk_layer (:class:`~cntk.contrib.crosstalkcaffe.unimodel.cntkmodel.CntkLayersDefinition`): the layer definition of pooling op inputs (list): a list contains all :class:`~cntk.ops.functions.Function` or :class:`~cntk.input` Return: :func:`~cntk.ops.functions.Function`: instaced cntk pooling op ''' sanitize_input = internal.sanitize_input(inputs[0]) pooling_type = ops.PoolingType_Average if cntk_layer.parameters.pooling_type else ops.PoolingType_Max return ops.pooling(sanitize_input, pooling_type, tuple(cntk_layer.parameters.kernel), strides=tuple(cntk_layer.parameters.stride), auto_padding=[cntk_layer.parameters.auto_pad], ceil_out_dim=True, name=cntk_layer.op_name)
def resnet_classifer(input, num_classes, device, output_name): conv_w_scale = 7.07 conv_b_value = 0 fc1_w_scale = 0.4 fc1_b_value = 0 sc_value = 1 bn_time_const = 4096 kernel_width = 3 kernel_height = 3 conv1_w_scale = 0.26 c_map1 = 16 conv1 = conv_bn_relu_layer(input, c_map1, kernel_width, kernel_height, 1, 1, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) rn1_1 = resnet_node2(conv1.output(), c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) rn1_2 = resnet_node2(rn1_1.output(), c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) rn1_3 = resnet_node2(rn1_2.output(), c_map1, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) c_map2 = 32 rn2_1_wProj = get_projection_map(c_map2, c_map1, device) rn2_1 = resnet_node2_inc(rn1_3.output(), c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, rn2_1_wProj, device) rn2_2 = resnet_node2(rn2_1.output(), c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) rn2_3 = resnet_node2(rn2_2.output(), c_map2, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) c_map3 = 64 rn3_1_wProj = get_projection_map(c_map3, c_map2, device) rn3_1 = resnet_node2_inc(rn2_3.output(), c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, rn3_1_wProj, device) rn3_2 = resnet_node2(rn3_1.output(), c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) rn3_3 = resnet_node2(rn3_2.output(), c_map3, kernel_width, kernel_height, conv1_w_scale, conv_b_value, sc_value, bn_time_const, device) # Global average pooling poolw = 8 poolh = 8 poolh_stride = 1 poolv_stride = 1 pool = pooling(rn3_3.output(), AVG_POOLING, (1, poolh, poolw), (1, poolv_stride, poolh_stride)) out_times_params = parameter(shape=(c_map3, 1, 1, num_classes), device_id=device) out_bias_params = parameter(shape=(num_classes, ), device_id=device) t = times(pool.output(), out_times_params) return plus(t.output(), out_bias_params, output_name)
def pooling(input, **kwargs): dim = len(input.output.shape) input = cntk.transpose(input, [dim - 1] + list(range(0, dim - 1))) layer = ops.pooling(input, **kwargs) layer = cntk.transpose(layer, list(range(1, dim)) + [0]) return layer
def max_pool_layer(input, pool_size, stride): return pooling(input, PoolingType_Max, (1, pool_size[0], pool_size[1]), (1, stride[0], stride[1]))