C3D-tensorflow icon indicating copy to clipboard operation
C3D-tensorflow copied to clipboard

Fine Tuning mismatching of shape errror.

Open zeynepgokce opened this issue 7 years ago • 9 comments

Hi, I am new in this area as i mentioned before. I am trying to apply fine tuning to this model. In pre-trained model there are 101 classes but i want to change it to 2 classes by applying fine tuning, but when i restore the model ( saver.restore(sess, model_filename) ) i have encountered error something like this " InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [101] " I guess, i shouldn't restore the out layer from the pretrained model. Am i wrong? If i am wrong what should i do to solve this problem? If i am right, how can i restore the model without out layer?

Can you help me ?

The error : -- By the way, fine_tuning.py is the same with the train code in this project. İ just change the name. 1. Traceback (most recent call last): 2. File "fine_tuning.py", line 285, in 3. tf.app.run() 4. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run 5. _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 6. File "fine_tuning.py", line 282, in main 7. run_training() 8. File "fine_tuning.py", line 221, in run_training 9. saver.restore(sess, model_filename) 10. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1560, in restore 11. {self.saver_def.filename_tensor_name: save_path}) 12. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run 13. run_metadata_ptr) 14. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run 15. feed_dict_tensor, options, run_metadata) 16. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run 17. options, run_metadata) 18. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call 19. raise type(e)(node_def, op, message) 20. tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [101] 21. [[Node: save/Assign_10 = Assign[T=DT_FLOAT, _class=["loc:@var_name/bout"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](var_name/bout, save/RestoreV2_10)]] 22. 23. Caused by op u'save/Assign_10', defined at: 24. File "fine_tuning.py", line 285, in 25. tf.app.run() 26. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run 27. _sys.exit(main(_sys.argv[:1] + flags_passthrough)) 28. File "fine_tuning.py", line 282, in main 29. run_training() 30. File "fine_tuning.py", line 211, in run_training 31. saver = tf.train.Saver(weights.values() + biases.values()) 32. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1140, in init 33. self.build() 34. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1172, in build 35. filename=self._filename) 36. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 688, in build 37. restore_sequentially, reshape) 38. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 419, in _AddRestoreOps 39. assign_ops.append(saveable.restore(tensors, shapes)) 40. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 155, in restore 41. self.op.get_shape().is_fully_defined()) 42. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 274, in assign 43. validate_shape=validate_shape) 44. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 43, in assign 45. use_locking=use_locking, name=name) 46. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op 47. op_def=op_def) 48. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op 49. original_op=self._default_original_op, op_def=op_def) 50. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in init 51. self._traceback = self._graph._extract_stack() # pylint: disable=protected-access 52. 53. InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [101] 54. [[Node: save/Assign_10 = Assign[T=DT_FLOAT, _class=["loc:@var_name/bout"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](var_name/bout, save/RestoreV2_10)]] 55.

zeynepgokce avatar Nov 02 '17 08:11 zeynepgokce

hello, when you call saver.restore(sess, model_filename) ),the model_filename parameter can be assigned by: 'model_filename=tf.trainable_variables()' for all trainable parameters in the checkpoint, in your case,: 'model_filename= [v for v in tf.trainable_variables() if v.name.find("conv") != -1]' this means only restore the conv layer. and you can print all layer name like this: 'for i in tf.trainable_variables(): print(i.name) '
strongly advise you to get more familiar with tensorflow.

LiangXu123 avatar Nov 03 '17 04:11 LiangXu123

But model_filename is a path and it is pretrained model as following part in the python file. **model_filename = "./sports1m_finetuning_ucf101.model"

if os.path.isfile(model_filename) and use_pretrained_model: saver.restore(sess, model_filename)**

then i couldn't use the restore method according to code part which you gave.

zeynepgokce avatar Nov 06 '17 09:11 zeynepgokce

checkout https://github.com/hx173149/C3D-tensorflow/blob/master/C3D-tensorflow-1.0/Random_clip_valid.py,

my bad to ignore that model_filename is a path. when you define the saver by : saver = tf.train.Saver()

you can also do it by assign variables name: var_list = [v for v in tf.trainable_variables() if v.name.find("conv") != -1] saver = tf.train.Saver(var_list )

and then when you call: saver.restore(sess, model_filename) this will only resotre the variables in var_list instaead of all variables in the "./sports1m_finetuning_ucf101.model".

just like what i did in the repo:C3D-tensorflow-1.0/Random_clip_valid.py--more useful code here.

LiangXu123 avatar Nov 06 '17 09:11 LiangXu123

Ok, thank you so much, i got this part. but now, I got different error. I couldnt solve, I just tried to train your model with 2 video and just changed parameters to batch size = 1 and gpu_num =1 in original code and the other parts are same , again i got the same error

  1. sess.run(train_op, feed_dict={
  2.                   images_placeholder: train_images,
    
  3.                   labels_placeholder: train_labels
    
  4.                   })
    

It gives the error in 3. line of the above code part as following :

% (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape()))) ValueError: Cannot feed value of shape (1, 0) for Tensor u'Placeholder:0', which has shape '(1, 16, 112, 112, 3)'

train_labels shape should be (1) , so why placeholder shape is (1, 16, 112, 112, 3) ? how can i solve it ?

zeynepgokce avatar Nov 06 '17 11:11 zeynepgokce

This is because your input is video,however the code's input is frame.

zzy123abc avatar Jan 11 '18 09:01 zzy123abc

@zeynepgokce @zzy123abc Hi, I have already achieved better precision on UCF101 according to the tutorial, but I now want to use my own video data to fine tune with the provided pre-training model (I have a very big doubt: train_c3d_ucf101.py in the tensorflow folder actually That is, fine-tuning the code, can you modify the pre-training model you need in the code?). My video data has 20 categories. Do I need to modify softmax to correspond to my own number of categories? Looking forward to your reply! Thank you!

953585130 avatar Sep 27 '18 13:09 953585130

of course,you need to modify fc to 20.

zzy123abc avatar Sep 27 '18 14:09 zzy123abc

@zzy123abc Thank you for your reply! I still have a question. I want to use my own video data for training and prediction. The author of this paper has given four pre-training models. Which model will I use better?

953585130 avatar Sep 28 '18 01:09 953585130

@zeynepgokce Hi, I am training a new model with the HMDB51 dataset, which has only 51 classes. Through the pre-training model given by the author, I want to fine-tune the parameters behind fc, but how the code should be modified, I tried it many times without success.

saver = tf.train.Saver(weights.values() + biases.values()) init = tf.global_variables_initializer()

Create a session for running Ops on the Graph.

sess = tf.Session( config=tf.ConfigProto(allow_soft_placement=True) ) sess.run(init) if os.path.isfile(model_filename) and use_pretrained_model: saver.restore(sess, model_filename)

I hope to get your help! Thanks.

953585130 avatar Sep 29 '18 08:09 953585130