3

I would like to re-train a pre-trained ResNet-50 model with TensorFlow slim, and use it later for classifying purposes.

The ResNet-50 is designed to 1000 classes, but I would like just 10 classes (land cover types) as output.

First, I try to code it for only one image, what I can generalize later. So this is my code:

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})

I am a bit confused about what comes next (I should open a graph, or I should load the structure of the network and load the weights, or load batches. There is a problem with the image shape as well. There are a lot of versatile documentations, which aren't easy to interpret :/

Any advice how to correct the code in order to fit my purposes?

The test image: AnnualCrop735

AnnualCrop735

aL_eX
  • 1,453
  • 2
  • 15
  • 30
poetyi
  • 236
  • 4
  • 13

1 Answers1

0

The resnet layer gives you predictions if you provide the num_classes kwargs. Look at the documentation and code for resnet_v1

You need to add a loss function and training operations on top of it to fine-tune the resnet_v1 with reuse

...
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)
Vikas
  • 2,220
  • 1
  • 15
  • 12
  • The problem is there I have errors before calculating classification loss. Like this, and a plenty more: tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. 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)]] – poetyi Mar 18 '18 at 22:20