본문 바로가기

머신러닝,딥러닝

checkpoint , tensorflow1과 2에서의 차이점 (NLP pretrained model) ; difference in checkpoint of Tensorflow version

checkpoint 개념은 텐서플로우를 공부하셨다면 다들 아실 겁니다. 

헌데, tensorflow 1 과 2 에서의 출력되는 파일들의 형태가 조금 다르다는 걸 최근에 알게 됐습니다. 

먼저 tensorflow 1 에서 보겠습니다. 

 

 

The concept of checkpoint is familiar to anyone who has studied TensorFlow.
However, I recently noticed that the output files in tensorflow 1 and 2 are slightly different.
Let's look at tensorflow 1 first.

 

 

 

출처:http://jaynewho.com/post/8

 

 

epoch를 돌 때마다 data , index , meta 확장자를 가진 파일 각각 하나씩  그리고 + checkpoint 하나 해서 4가지를 눈여겨 보셔야합니다.

 

Every time you go through the epoch, you need to pay attention to 4 things, one for each file with data, index and meta extensions and one checkpoint.

 

자, 그러면 Nlp 에서의 pretrained model 을 한 번 보겠습니다. 

한국어 데이터로 albert를 적용해서 배포한 kalbert 라는 오픈소스를 살펴보겠습니다.

 

Now, let's take a look at the pretrained model in Nlp.
Let's take a look at the open source kalbert distributed by applying albert as Korean data.

 

 

https://github.com/MrBananaHuman/KalBert

 

위에 오픈소스 들어가시면 pretrained model 까지 구글드라이브로 공유해주었습니다. 

어떤 소스를 베이스로 했는지 보면, 

 

If you enter open source above, you can see the pretrained model shared by Google Drive.
If you look at what source is based on,

 

https://github.com/brightmart/albert_zh 

 

이 소스를 참고했는데요. 위 소스는 중국어를 가지고 albert 모델을 적용한 소스입니다. 

 

It referenced this source. The source above is the source that applied the albert model in Chinese.

 

https://github.com/google-research/ALBERT 

 

그리고 해당 소스들은 구글에서 공개한 ALBERT 오픈소스를 참고해서 각국의 언어들로 적용된 걸로 보입니다. 

구글에서 배포한 ALBERT 코드는 tensorflow 1.15 로 배포했습니다. 

그리고 readme 를 보시면 누군가 2.0 으로 만들어주길 바란다고 해놓았습니다. 

 

자, 그러면 다시 kalbert 에서 공유해준 구글드라이브에서 output 폴더를 보겠습니다. 

 

And the sources seem to have been applied in the languages ​​of each country by referring to the ALBERT open source released by Google.
The ALBERT code distributed by Google was distributed with tensorflow 1.15.
And if you look at the readme, it says that they want someone to make albert source to convert with tensorflow 2.0.


Now, let's look at the output folder in the Google Drive shared by kalbert again.

 

 

 

 

 

 

4가지 파일이 들어있습니다. 헌데, checkpoint 파일이 안 보입니다. 

There are 4 files. But, we can't see the checkpoint file.

 

"저게 checkpoint 아니야? "

Isn't that a checkpoint?

 

라고 생각할 수 있는데 제가 위에서 4가지 파일이 있어야 된다고 했습니다. (tensorflow 1 기준입니다.) 

You can think of it, but I said that there should be 4 files above. (Based on tensorflow 1)

 

위 파일에서는 checkpoint 파일이 없습니다. 헷갈릴 수 있는데, checkpoint는 4가지 다른 확장자를 가진 파일들로 구성된 셈입니다. 

In the above file, there is no checkpoint file. It can be confusing, but a checkpoint consists of files with 4 different extensions.

 

그러면 위 파일을 기준으로 checkpoint 를 로딩해서 각자의 도메인 데이터로 pretrain 를 하고 싶을 겁니다. 

어떻게 해야될까요? 

checkpoint 파일을 만들어주셔야 합니다. 

 

Then you will want to load a checkpoint based on the above file and pretrain it with your own domain data.
What should we do?
We need to create a checkpoint file.

 

아래 캡쳐를 보겠습니다. 

Let's look at the picture below.

 

 

 

출처: https://monologg.kr/2020/05/01/transformers-porting/

 

 

 

위에서 학습한 Koelectra 모델은 checkpoint 파일이 있고 이를 열어본 그림까지 확인할 수 있습니다. 

그렇다면 kalbert 의 모델을 쓰려면 어떻해야할까요?

The Koelectra model has a checkpoint file and you can check the picture you opened.
So how do We use kalbert's model?

 

 

checkpoint 파일을 만들어주시면 됩니다. 

파일 안의 구성은 아래를 참고해주세요.

 

Create a checkpoint file.
The structure in the file is as follows.

 

model_checkpoint_path: "model.ckpt-191000"

all_model_checkpoint_paths: "model.ckpt-191000"

 

으로 하면 되겠죠? 

 

pretrained model 을 쓸 때 보통은 tensorflow 1 기준으로 생성된 체크포인트들이 들어있을 겁니다.  

그러면 이 파일들은 tensorflow 2 코드에서 돌까요? 

 

When using a pretrained model, there will usually be checkpoints created based on tensorflow 1.
So will these files run in tensorflow 2 code?

 

 

제 짧은 경험으로는 이상없이 도는 것을 확인할 수 있었습니다. (albert 기준으로 말씀드리는 것입니다.)

자, 그리고 한 가지 더! 

In my short experience, I was able to confirm that it was running smoothly. (I am speaking on an albert basis.)
Now, and one more thing!

 

 

 

 

 

 

pretrain 된 모델의 vocab size 를 맞춰주셔야 합니다. 

다를 경우 vocab 크기가 다르다는 에러가 발생합니다. 

 

You need to adjust the vocab size of the pretrained model.
If it is different, you will get an error that the vocab size is different.

 

자, 이제 tensorflow 2 의 체크포인트를 보겠습니다. 

Now, let's look at the checkpoint of tensorflow 2.

 

 

 

출처: https://www.tensorflow.org/tutorials/keras/save_and_load?hl=ko

 

 

 

data 파일 2개, checkpoint 1개, index 파일 1개가  checkpoint 입니다. 

Two data files, one checkpoint, and one index file are checkpoints.

 

 

tensorflow 1 과 조금 다르게 구성된 되었습니다. 물론 checkpoint 의 의미는 똑같지만 생긴게 조금 다르다는걸로 받아들입니다. 

위에서 한 것처럼 만약 tensorflow 2 로 배포된 모델이 위와 같이 checkpoint 가 배포됐다면? 위에서 한 대로 checkpoint 파일을 만들어주시면 됩니다. 

 

 

It has been configured a bit differently from tensorflow 1. Of course, the meaning of the checkpoint is the same, 

but I accept that it is a little different.
As in the above, if the model distributed with tensorflow 2 has the checkpoint deployed as above? Create a checkpoint file as above.

 

이상 pretrained model 에서 checkpoint 에 대한 설명을 마치겠습니다. 

I will finish the explanation.

 

이 내용을 정리한 이유는 checkpoint 가 tensorflow 2 와 1 에서 다르게 생겼다는 걸 최근에 알게 되었습니다.

The reason I organized this is that I recently noticed that checkpoints look different in tensorflow 2 and 1.

 

저처럼 헷갈리지 마시고 잘 정리해서 하고자 하는 딥러닝 프로젝트에 도움이 되길 바랍니다 :)

I hope you don't get confused like me and it will help your deep learning project. :)

 

 

 

 

--참고--

 

https://goodtogreate.tistory.com/entry/Saving-and-Restoring

 

TensorFlow 모델을 저장하고 불러오기 (save and restore)

TensorFlow 모델을 저장하고 불러오기 (save and restore) 해당 튜토리얼에 사용한 코드는 개인 GitHub Link에서 확인 할 수 있다. 저장 복구를 위해서는 두개의 파일이 필요하다. a) Meta graph Tensorflow grap..

goodtogreate.tistory.com

https://monologg.kr/2020/05/01/transformers-porting/

 

내가 만든 ELECTRA를 Huggingface Transformers로 Porting하기

BERT, ALBERT, ELECTRA 등을 직접 Pretrain하게 되면 모델이 Tensorflow의 ckpt 형태로 저장이 된다. 이번 글에서는 tensorflow ckpt를 transformers의 pytorch ckpt로 변환하는 법을 알아보겠다🤗

monologg.kr

https://www.tensorflow.org/tutorials/keras/save_and_load?hl=ko

 

모델 저장과 복원  |  TensorFlow Core

Note: 이 문서는 텐서플로 커뮤니티에서 번역했습니다. 커뮤니티 번역 활동의 특성상 정확한 번역과 최신 내용을 반영하기 위해 노력함에도 불구하고 공식 영문 문서의 내용과 일치하지 않을 수

www.tensorflow.org

https://eehoeskrap.tistory.com/343

 

[TensorFlow] .ckpt vs .pb vs .pbtxt 차이점

간단하게 말하자면 아래와 같다. ckpt 파일 모델의 변수(가중치)인 체크포인트 파일 pb 파일 모델의 변수 + 구조 (즉, 전체 그래프) 로 이루어진 바이너리 파일 pbtxt 파일 pb 파일을 읽을 수 있는 텍��

eehoeskrap.tistory.com

https://stackoverflow.com/questions/42357609/difference-in-checkpoint-of-tensorflow-version-0-11-and-version-0-12

 

difference in checkpoint of Tensorflow version 0.11 and version 0.12?

I developed a CNN algorithm using Python and Tensorflow library. I run the program on two Tensorflow versions as 0.11 and 0.12. In the generated checkpoint folder, there is a difference between two

stackoverflow.com