【问题标题】:Re-train pre-trained ResNet-50 model with tf slim for classification purposes使用 tf slim 重新训练预训练的 ResNet-50 模型以进行分类
【发布时间】:2018-08-03 11:08:51
【问题描述】:

我想用 TensorFlow slim 重新训练一个预训练的 ResNet-50 模型,然后将其用于分类目的。

ResNet-50 设计为 1000 个类别,但我只想要 10 个类别(土地覆盖类型)作为输出。

首先,我尝试只为一张图片编写代码,稍后我可以对其进行概括。 所以这是我的代码:

from tensorflow.contrib.slim.nets import resnet_v1
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

batch_size = 1
height, width, channels = 224, 224, 3
# Create graph
inputs = tf.placeholder(tf.float32, shape=[batch_size, height, width, channels])
with slim.arg_scope(resnet_v1.resnet_arg_scope()):
    logits, end_points = resnet_v1.resnet_v1_50(inputs, is_training=False)

saver = tf.train.Saver()    

with tf.Session() as sess:
    saver.restore(sess, 'd:/bitbucket/cnn-lcm/data/ckpt/resnet_v1_50.ckpt')
    representation_tensor = sess.graph.get_tensor_by_name('resnet_v1_50/pool5:0')
    #  list of files to read
    filename_queue = tf.train.string_input_producer(['d:/bitbucket/cnn-lcm/data/train/AnnualCrop/AnnualCrop_735.jpg']) 
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    img = tf.image.decode_jpeg(value, channels=3)    

    im = np.array(img)
    im = im.reshape(1,224,224,3)
    predict_values, logit_values = sess.run([end_points, logits], feed_dict= {inputs: im})
    print (np.max(predict_values), np.max(logit_values))
    print (np.argmax(predict_values), np.argmax(logit_values))

    #img = ...  #load image here with size [1, 224,224, 3]
    #features = sess.run(representation_tensor, {'Placeholder:0': img})

我有点困惑接下来会发生什么(我应该打开一个图表,或者我应该加载网络结构并加载权重,或者加载批次。图像形状也有问题。有很多通用的文档,不容易解释:/

任何建议如何更正代码以符合我的目的?

测试图片:AnnualCrop735

【问题讨论】:

    标签: python tensorflow resnet pre-trained-model tensorflow-slim


    【解决方案1】:

    如果您提供num_classes kwargs,resnet 层会为您提供预测。查看resnet_v1的文档和代码

    您需要在其之上添加一个损失函数和训练操作,以通过重用来微调 resnet_v1

    ...
    with slim.arg_scope(resnet_v1.resnet_arg_scope()):
        logits, end_points = resnet_v1.resnet_v1_50(
            inputs,
            num_classes=10,
            is_training=True,
            reuse=tf.AUTO_REUSE)
    ...
    ...
        classification_loss = slim.losses.softmax_cross_entropy(
            predict_values, im_label)
    
        regularization_loss = tf.add_n(slim.losses.get_regularization_losses())
        total_loss = classification_loss + regularization_loss
    
        train_op = slim.learning.create_train_op(classification_loss, optimizer)
        optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    
        slim.learning.train(
            train_op,
            logdir='/tmp/',
            number_of_steps=1000,
            save_summaries_secs=300,
            save_interval_secs=600)
    

    【讨论】:

    • 问题是在计算分类损失之前我有错误。像这样,还有更多: tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign 需要两个张量的形状匹配。 lhs shape= [10] rhs shape= [1000] [[Node: save/Assign_265 = Assign[T=DT_FLOAT, _class=["loc:@resnet_v1_50/logits/biases"], use_locking=true, validate_shape=true, _device ="/job:localhost/replica:0/task:0/device:CPU:0"](resnet_v1_50/logits/biases, save/RestoreV2_265)]]
    猜你喜欢
    • 2017-09-22
    • 2018-08-12
    • 2018-07-25
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多