문서 를 읽은 후에 모델을 저장했습니다 TensorFlow
. 여기에 데모 코드가 있습니다.
# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.global_variables_initializer()
# Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.
with tf.Session() as sess:
sess.run(init_op)
# Do some work with the model.
..
# Save the variables to disk.
save_path = saver.save(sess, "/tmp/model.ckpt")
print("Model saved in file: %s" % save_path)
하지만 그 후 3 개의 파일이 있다는 것을 알게되었습니다.
model.ckpt.data-00000-of-00001
model.ckpt.index
model.ckpt.meta
그리고 model.ckpt
그런 파일이 없기 때문에 파일을 복원하여 모델을 복원 할 수 없습니다 . 내 코드는 다음과 같습니다.
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, "/tmp/model.ckpt")
그렇다면 왜 3 개의 파일이 있습니까?
답변
이 시도:
with tf.Session() as sess:
saver = tf.train.import_meta_graph('/tmp/model.ckpt.meta')
saver.restore(sess, "/tmp/model.ckpt")
TensorFlow 저장 방법은 그래프 구조를 변수 값 과 별도로 저장하므로 세 종류의 파일을 저장 합니다 . 이 .meta
파일은 저장된 그래프 구조를 설명하므로 체크 포인트를 복원하기 전에 가져와야합니다 (그렇지 않으면 저장된 체크 포인트 값이 어떤 변수에 해당하는지 알 수 없음).
또는 다음을 수행 할 수 있습니다.
# Recreate the EXACT SAME variables
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Now load the checkpoint variable values
with tf.Session() as sess:
saver = tf.train.Saver()
saver.restore(sess, "/tmp/model.ckpt")
라는 파일이 없더라도 model.ckpt
복원 할 때 해당 이름으로 저장된 체크 포인트를 참조합니다. 로부터 saver.py
소스 코드 :
사용자는 물리적 경로 이름 대신 사용자가 지정한 접두사 …와 상호 작용하기 만하면됩니다.
답변
-
메타 파일 : 저장된 그래프 구조를 설명하고 GraphDef, SaverDef 등을 포함합니다. 그런 다음 적용
tf.train.import_meta_graph('/tmp/model.ckpt.meta')
, 복원Saver
및Graph
. -
index 파일 : 문자열 문자열 불변 테이블 (tensorflow :: table :: Table)입니다. 각 키는 텐서의 이름이고 값은 직렬화 된 BundleEntryProto입니다. 각 BundleEntryProto는 텐서의 메타 데이터를 설명합니다. “데이터”파일 중 텐서의 내용, 해당 파일의 오프셋, 체크섬, 일부 보조 데이터 등을 포함하는 파일이 무엇인지 설명합니다.
-
데이터 파일 : TensorBundle 컬렉션으로 모든 변수의 값을 저장합니다.
답변
Word2Vec tensorflow 튜토리얼 에서 훈련 된 단어 임베딩을 복원하고 있습니다.
여러 체크 포인트를 생성 한 경우 :
예를 들어 생성 된 파일은 다음과 같습니다.
model.ckpt-55695.data-00000-of-00001
model.ckpt-55695.index
model.ckpt-55695.meta
이 시도
def restore_session(self, session):
saver = tf.train.import_meta_graph('./tmp/model.ckpt-55695.meta')
saver.restore(session, './tmp/model.ckpt-55695')
restore_session () 호출시 :
def test_word2vec():
opts = Options()
with tf.Graph().as_default(), tf.Session() as session:
with tf.device("/cpu:0"):
model = Word2Vec(opts, session)
model.restore_session(session)
model.get_embedding("assistance")
답변
예를 들어 드롭 아웃으로 CNN을 훈련했다면 다음과 같이 할 수 있습니다.
def predict(image, model_name):
"""
image -> single image, (width, height, channels)
model_name -> model file that was saved without any extensions
"""
with tf.Session() as sess:
saver = tf.train.import_meta_graph('./' + model_name + '.meta')
saver.restore(sess, './' + model_name)
# Substitute 'logits' with your model
prediction = tf.argmax(logits, 1)
# 'x' is what you defined it to be. In my case it is a batch of RGB images, that's why I add the extra dimension
return prediction.eval(feed_dict={x: image[np.newaxis,:,:,:], keep_prob_dnn: 1.0})
답변
