hx173149 / C3D-tensorflow

C3D is a modified version of BVLC tensorflow to support 3D ConvNets.
MIT License
588 stars 262 forks source link

Fine Tuning mismatching of shape errror. #34

Open zeynepgokce opened 7 years ago

zeynepgokce commented 7 years ago

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

  1. File "fine_tuning.py", line 285, in
  2. tf.app.run()
  3. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
  4. _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  5. File "fine_tuning.py", line 282, in main
  6. run_training()
  7. File "fine_tuning.py", line 221, in run_training
  8. saver.restore(sess, model_filename)
  9. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1560, in restore
  10. {self.saver_def.filename_tensor_name: save_path})
  11. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run
  12. run_metadata_ptr)
  13. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run
  14. feed_dict_tensor, options, run_metadata)
  15. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
  16. options, run_metadata)
  17. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call
  18. raise type(e)(node_def, op, message)
  19. tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [101]
  20. [[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)]]
  21. Caused by op u'save/Assign_10', defined at:
  22. File "fine_tuning.py", line 285, in
  23. tf.app.run()
  24. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 48, in run
  25. _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  26. File "fine_tuning.py", line 282, in main
  27. run_training()
  28. File "fine_tuning.py", line 211, in run_training
  29. saver = tf.train.Saver(weights.values() + biases.values())
  30. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1140, in init
  31. self.build()
  32. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1172, in build
  33. filename=self._filename)
  34. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 688, in build
  35. restore_sequentially, reshape)
  36. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 419, in _AddRestoreOps
  37. assign_ops.append(saveable.restore(tensors, shapes))
  38. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 155, in restore
  39. self.op.get_shape().is_fully_defined())
  40. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/state_ops.py", line 274, in assign
  41. validate_shape=validate_shape)
  42. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 43, in assign
  43. use_locking=use_locking, name=name)
  44. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
  45. op_def=op_def)
  46. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op
  47. original_op=self._default_original_op, op_def=op_def)
  48. File "/home/hp/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in init
  49. self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
  50. InvalidArgumentError (see above for traceback): Assign requires shapes of both tensors to match. lhs shape= [2] rhs shape= [101]
  51. [[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.**
LiangXu123 commented 7 years ago

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.

zeynepgokce commented 7 years ago

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.

LiangXu123 commented 7 years ago

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.

zeynepgokce commented 7 years ago

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 ?

zzy123abc commented 6 years ago

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

953585130 commented 6 years ago

@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!

zzy123abc commented 6 years ago

of course,you need to modify fc to 20.

953585130 commented 6 years ago

@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 commented 6 years ago

@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.