环境信息:windows10+python3.5+tensorflow1.6.0
问题描述:
在自己的训练集上跑train_softmax.py.
参数:
--logs_base_dir F:/work/runspace/log/ --models_base_dir F:/dataset/facenet/model/my/ --data_dir F:/dataset/vggface2_test/my_vgg_mtcnnpy_160 --image_size 160 --model_def models.inception_resnet_v1 --lfw_dir F:/dataset/lfw/lfw_mtcnnpy_160 --optimizer ADAM --learning_rate -1 --max_nrof_epochs 500 --batch_size 50 --keep_probability 0.4 --random_flip --use_fixed_image_standardization --learning_rate_schedule_file data/learning_rate_schedule_classifier_vggface2.txt --weight_decay 5e-4 --embedding_size 512 --lfw_distance_metric 1 --lfw_use_flipped_images --lfw_subtract_mean --validation_set_split_ratio 0.01 --validate_every_n_epochs 5 --gpu_memory_fraction 0.6
没问题。
但如果加上pretrainmodel, 使用最新模型20180402-114759/model-20180402-114759.ckpt-275,指定了embsize =512
会报错 :
Assign requires shapes of both tensors to match. lhs shape= [500] rhs shape= [8631]
原因分析:自己数据集里的类别数量与模型20180402-114759不一样
解决方法:saver只加载与最后几层类别无关的参数
打印t_variables.删除与类别数有关的层,让saver 只加载前面层。
t_variables = tf.trainable_variables()
print("t_variables", t_variables)
# var_to_restore = [v for v in t_variables if not v.name.startswith('InceptionResnetV1/Bottleneck')]
var_to_restore2 = [v for v in t_variables if not v.name.startswith('Logits')]
print('----------------------')
print("new var_to_restore", var_to_restore2)