Exemple #1
0
def resnet_stem(input, bn, model_weight, optimizer):
    conv_stem = _conv2d("conv_stem", input, 64, 7, 2)
    if bn:
        conv_stem = _batch_norm(conv_stem, "conv_stem_bn")
    conv_stem = flow.nn.relu(conv_stem)
    pool1 = flow.nn.max_pool2d(
        conv_stem,
        ksize=3,
        strides=2,
        padding="VALID",
        data_format="NCHW",
        name="pool1",
    )
    #最初的卷积层添加model weight
    if model_weight == True:
        modelWeight.addConv(index="_stem",
                            dtype=input.dtype,
                            shape1=(64, input.shape[1], 7, 7),
                            shape2=(64, ),
                            optimizer=optimizer)
    return pool1
def _conv_block(in_blob,
                index,
                filters,
                conv_times,
                optimizer,
                model_weight,
                bn=True):
    conv_block = []
    conv_block.insert(0, in_blob)
    for i in range(conv_times):
        conv_i = conv2d_layer(
            name="conv{}".format(index),
            input=conv_block[i],
            filters=filters[index],
            kernel_size=3,
            strides=1,
            bn=bn,
        )
        if model_weight == True:
            modelWeight.addConv(index=index,
                                dtype=conv_block[i].dtype,
                                shape1=(filters[index], conv_block[i].shape[1],
                                        3, 3),
                                shape2=(filters[index], ),
                                optimizer=optimizer)


#            shape_weight=(filters[index], conv_block[i].shape[1], 3, 3)
#            modelWeight.add("conv{}".format(index)+'-weight',conv_block[i].dtype,shape_weight)
#            modelWeight.add("conv{}".format(index)+'-bias',conv_block[i].dtype,(filters,))
#            modelWeight.add("conv{}".format(index)+'_bn-gamma',conv_block[i].dtype,(filters,))
#            modelWeight.add("conv{}".format(index)+'_bn-beta',conv_block[i].dtype,(filters,))
#            modelWeight.add("conv{}".format(index)+'_bn-moving_variance',conv_block[i].dtype,(filters,))
#            modelWeight.add("conv{}".format(index)+'_bn-moving_mean',conv_block[i].dtype,(filters,))
        conv_block.append(conv_i)
        index += 1

    return conv_block
Exemple #3
0
def residual_block(input, index, i, filter1, filter2, filter3, strides_init,
                   bn, model_weight, optimizer):
    #    if strides_init != 1 or block_name == "res2_0":
    #        #一个conv2d_affine(conv, bn, relu层)
    #        shortcut = conv2d_affine(input, block_name + "_branch1", 1, 1, filter3, 1, strides_init)
    #    else:
    #        shortcut = input
    #对输入做变换,使得和三层oncv的输出shape相同,可以相加
    shortcut = conv2d_affine(input,
                             "conv_shortcut" + str(index) + "_" + str(i),
                             filter3, 3, strides_init, bn)
    #shortcut层添加model weight
    if model_weight == True:
        modelWeight.addConv(index="_shortcut" + str(index) + "_" + str(i),
                            dtype=input.dtype,
                            shape1=(filter3, input.shape[1], 3, 3),
                            shape2=(filter3, ),
                            optimizer=optimizer)
    #三个conv2d_affine(conv, bn, relu层)
    bottleneck = bottleneck_transformation(input, filter1, filter2, filter3,
                                           strides_init, bn, model_weight,
                                           optimizer)
    #    print(bottleneck.shape, shortcut.shape, strides_init, i)
    return flow.nn.relu(bottleneck + shortcut)
Exemple #4
0
def bottleneck_transformation(input, filter1, filter2, filter3, strides, bn,
                              model_weight, optimizer):
    global NAME_NUMBER
    a = conv2d_affine(
        input,
        "conv" + str(NAME_NUMBER),
        filter1,
        1,
        1,
        bn,
        activation="Relu",
    )
    #添加conv的model weight
    if model_weight == True:
        modelWeight.addConv(index=NAME_NUMBER,
                            dtype=input.dtype,
                            shape1=(filter1, input.shape[1], 1, 1),
                            shape2=(filter1, ),
                            optimizer=optimizer)
    NAME_NUMBER += 1

    b = conv2d_affine(
        a,
        "conv" + str(NAME_NUMBER),
        filter2,
        3,
        strides,
        bn,
        activation="Relu",
    )
    #添加conv的model weight
    if model_weight == True:
        modelWeight.addConv(index=NAME_NUMBER,
                            dtype=a.dtype,
                            shape1=(filter2, a.shape[1], 3, 3),
                            shape2=(filter2, ),
                            optimizer=optimizer)
    NAME_NUMBER += 1

    c = conv2d_affine(b, "conv" + str(NAME_NUMBER), filter3, 1, 1, bn)
    #添加conv的model weight
    if model_weight == True:
        modelWeight.addConv(index=NAME_NUMBER,
                            dtype=b.dtype,
                            shape1=(filter3, b.shape[1], 1, 1),
                            shape2=(filter3, ),
                            optimizer=optimizer)
    NAME_NUMBER += 1
    #    print(a.shape, b.shape, c.shape, strides)
    return c
def lenet(images, cfg, optimizer, trainable=True, need_transpose=False, 
          training=True, wd=1.0/32768, model_weight=True, bn=True):
    if need_transpose:
        images = flow.transpose(images, name="transpose", perm=[0, 3, 1, 2])
    conv0 = conv2d_layer(name="conv0", input=images, filters=cfg[0], kernel_size=5,
                         padding="VALID", strides=1, bn=bn)
    pool0 = flow.nn.max_pool2d(conv0, 2, 2, "VALID", "NCHW", name="pool0")

    conv1 = conv2d_layer(name="conv1", input=pool0, filters=cfg[1], kernel_size=5,
                         padding="VALID", strides=1, bn=bn)    
    pool1 = flow.nn.max_pool2d(conv1, 2, 2, "VALID", "NCHW", name="pool1")
       
    pool1 = flow.reshape(pool1, [pool1.shape[0], -1])
    # pool1 = flow.reshape(images, [images.shape[0], -1])
    dense0 = flow.layers.dense(
        inputs=pool1,
        units=cfg[2],
        activation=flow.nn.relu,
        use_bias=True,
        kernel_initializer=flow.random_normal_initializer(mean=0, stddev=0.1),
        trainable=trainable,
        name="dense0")

    dense1 = flow.layers.dense(
        inputs=dense0,
        units=cfg[3],
        activation=flow.nn.relu,
        use_bias=True,
        kernel_initializer=flow.random_normal_initializer(mean=0, stddev=0.1),
        trainable=trainable,
        name="dense1")
    
    dense2 = flow.layers.dense(
        inputs=dense1,
        units=cfg[4],
        use_bias=True,
        kernel_initializer=flow.random_normal_initializer(mean=0, stddev=0.1),
        trainable=trainable,
        name="dense2")
#    flow.watch(fc8)
    
    def getTypeAndShape(inputs,units):
        in_shape = inputs.shape
        in_num_axes = len(in_shape)
        inputs = (flow.reshape(inputs, (-1, in_shape[-1])) if in_num_axes > 2 else inputs)
        shape=(units, inputs.shape[1])
        dtype=inputs.dtype
        return shape,dtype
    
    if model_weight == True:
        modelWeight.addConv(index=0, dtype=conv0.dtype,
                            shape1=(cfg[0], images.shape[1], 5, 5), shape2=(cfg[0],),
                            optimizer=optimizer)
        modelWeight.addConv(index=1, dtype=conv1.dtype,
                            shape1=(cfg[1], conv0.shape[1], 5, 5), shape2=(cfg[1],),
                            optimizer=optimizer)      
        shape_list = []
        dtype_list = []
        shape_weight, dtype = getTypeAndShape(pool1, cfg[2])
        shape_list.append(shape_weight)
        dtype_list.append(dtype)
        shape_weight, dtype = getTypeAndShape(dense0, cfg[3])
        shape_list.append(shape_weight)
        dtype_list.append(dtype)
        shape_weight, dtype = getTypeAndShape(dense1, cfg[4])
        shape_list.append(shape_weight)
        dtype_list.append(dtype)
        modelWeight.addDense(dtype_old=dtype_list, shape=shape_list,
                             optimizer=optimizer, dense_num=3)

    return dense2