簡體   English   中英

從張量流中的預訓練模型推斷

[英]Inference from a pre-trained model in tensorflow

我有一個經過預訓練的模型,我正在嘗試從中進行張量流推斷。 但是我想我缺少了一些東西。 這是我的架構:

# Define a function to create a conv layer
def add_conv_layer(incoming=None, num_filters=None, kSize=None, pad='SAME', train=False, s=1,
                   layer_name=None, weights_param=None, bias_param=None, activation_fn=None):

    with tf.variable_scope(layer_name):

        previous_layer_output_shape = incoming.get_shape().as_list()[-1]
        tshape  = [kSize, kSize, previous_layer_output_shape, num_filters]

        # Set weights for the layer
        if weights_param is not None:
            weights = tf.Variable(weights_param, name='weights', dtype=tf.float32)
            weights = tf.reshape(weights, tshape)
        else:
            weights = init_weights(tshape)
            print weights.shape

        # Set bias for the layer
        if bias_param is not None:
            bias = tf.Variable(bias_param, name='bias', dtype=tf.float32)
        else:
            bias = init_bias(num_filters)


        layer = tf.nn.conv2d(incoming, weights, strides=[1,s,s,1], padding=pad, name='layer_name') + bias

        if activation_fn is not None:
            layer = activation_fn(layer)

        return layer    



# Define a function to add a dense layer
def add_dense_layer(input_tensor=None, num_units=None, layer_name=None,
                   weight_params=None, bias_params=None, activation_fn=None):

    with tf.variable_scope(layer_name):

        previous_layer_output_shape = input_tensor.get_shape().as_list()[-1]
        tshape = [previous_layer_output_shape, num_units]

        # Add weights for the layer
        if weight_params is not None:
            weights = tf.Variable(weight_params, name='weights', dtype=tf.float32)
            weights = tf.reshape(weights, tshape)    
        else:
            weights = init_weights(tshape)

        # Add bias to the layer    
        if bias_params is not None:
            bias = tf.Variable(bias_params, name='bias', dtype=tf.float32)
        else:
            bias = init_bias(num_units)


        layer = tf.matmul(input_tensor, weights) + bias

        if activation_fn is not None:
            layer = activation_fn(layer)

        return layer


def build_network(features):
    features = tf.reshape(features, [-1,224,224,3])

    # Conv block 1
    conv1_1 = add_conv_layer(features, num_filters=64, kSize=3,  activation_fn=tf.nn.relu, 
                             weights_param=f['conv1_1']['weights'].value,
                             bias_param=f['conv1_1']['bias'].value,
                             layer_name='conv1_1')

    conv1_2 = add_conv_layer(conv1_1, num_filters=64, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv1_2']['weights'].value,
                             bias_param=f['conv1_2']['bias'].value,
                             layer_name='conv1_2')

    pool1 = tf.layers.max_pooling2d(inputs=conv1_2,pool_size=(2,2), strides=(2,2), name='pool1')



    # Conv block 2
    conv2_1 = add_conv_layer(pool1, num_filters=128, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv2_1']['weights'].value,
                             bias_param=f['conv2_1']['bias'].value,
                             layer_name='conv2_1')

    conv2_2 = add_conv_layer(conv2_1, num_filters=128, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv2_2']['weights'].value,
                             bias_param=f['conv2_2']['bias'].value,
                             layer_name='conv2_2')

    pool2 = tf.layers.max_pooling2d(inputs=conv2_2,pool_size=(2,2), strides=(2,2), name='pool2')



    # Conv block 3
    conv3_1 = add_conv_layer(pool2, num_filters=256, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv3_1']['weights'].value,
                             bias_param=f['conv3_1']['bias'].value,
                             layer_name='conv3_1')

    conv3_2 = add_conv_layer(conv3_1, num_filters=256, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv3_2']['weights'].value,
                             bias_param=f['conv3_2']['bias'].value,
                             layer_name='conv3_2')

    conv3_3 = add_conv_layer(conv3_2, num_filters=256, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv3_3']['weights'].value,
                             bias_param=f['conv3_3']['bias'].value,
                             layer_name='conv3_3')

    pool3 = tf.layers.max_pooling2d(inputs=conv3_3,pool_size=(2,2), strides=(2,2), name='pool3')


    # Conv block 4
    conv4_1 = add_conv_layer(pool3, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv4_1']['weights'].value,
                             bias_param=f['conv4_1']['bias'].value,
                             layer_name='conv4_1')

    conv4_2 = add_conv_layer(conv4_1, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv4_2']['weights'].value,
                             bias_param=f['conv4_2']['bias'].value,
                             layer_name='conv4_2')

    conv4_3 = add_conv_layer(conv4_2, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv4_3']['weights'].value,
                             bias_param=f['conv4_3']['bias'].value,
                             layer_name='conv4_3')

    pool4 = tf.layers.max_pooling2d(inputs=conv4_3,pool_size=(2,2), strides=(2,2), name='pool4')



    # Conv block 5
    conv5_1 = add_conv_layer(pool4, num_filters=512, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv5_1']['weights'].value,
                             bias_param=f['conv5_1']['bias'].value,
                             layer_name='conv5_1')

    conv5_2 = add_conv_layer(conv5_1, num_filters=512, kSize=3, activation_fn=tf.nn.relu, 
                             weights_param=f['conv5_2']['weights'].value,
                             bias_param=f['conv5_2']['bias'].value,
                             layer_name='conv5_2')

    conv5_3 = add_conv_layer(conv5_2, num_filters=512, kSize=3, activation_fn=tf.nn.relu,
                             weights_param=f['conv5_3']['weights'].value,
                             bias_param=f['conv5_3']['bias'].value,
                             layer_name='conv5_3')

    pool5 = tf.layers.max_pooling2d(inputs=conv5_3,  pool_size=(7,7),strides=(7,7), name='img1_pool5')

    flatten_layer = tf.contrib.layers.flatten(inputs=pool5)

    fc6 = add_dense_layer(flatten_layer, num_units=1024, activation_fn=tf.nn.relu, 
                               weight_params=f['fc6']['weights'].value, 
                               bias_params= f['fc6']['bias'].value,
                               layer_name='fc6')


    fc7 = add_dense_layer(fc6, num_units=256, activation_fn=tf.nn.relu,
                          weight_params=f['fc7']['weights'].value, 
                          bias_params= f['fc7']['bias'].value,
                          layer_name='fc7')
    output = add_dense_layer(fc7, num_units=2, activation_fn=tf.nn.softmax,
                             weight_params=f['out']['weights'].value, 
                             bias_params= f['out']['bias'].value,
                             layer_name='output')

    return output

現在,我要運行build_network函數一個新的輸入並進行推斷。 我正在這樣做,如圖所示(我認為這是錯誤的):

init = tf.global_variables_initializer()
out = build_network(features)
with tf.Session() as sess:
     sess.run(init)
     print(out.eval())

但是,有了這個,我為每個輸入得到相同的輸出。 請幫忙

從原始文件進行編輯 build_network函數僅創建圖形。 sess.run(init)行使用每個權重的默認初始值設定項初始化網絡的權重,即該行之后的網絡 未經訓練。

在將代碼中的te add_*_layer函數實現添加到代碼中之后,我現在看到您實際上在初始化權重。 該答案的注釋中的更多信息表明錯誤可能在於權重從Theano到Tensorflow的轉換,或者如@Sebastian所建議的,由於數據布局的差異。 但是,如果沒有更多信息,我很難確定地說更多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM