1. 程式人生 > >《錯誤手記-01》 facenet使用預訓練模型fine-tune重新訓練自己資料集報錯

《錯誤手記-01》 facenet使用預訓練模型fine-tune重新訓練自己資料集報錯

環境資訊: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)