• 카테고리

    질문 & 답변
  • 세부 분야

    딥러닝 · 머신러닝

  • 해결 여부

    미해결

train_isbi_2012.py 실행중 epoch첫번째 끝나고 모델을 저장할때 뜨는 에러입니다.

22.03.23 17:11 작성 조회수 1.2k

0

안녕하세요 수업을 들으면서 train_isbi_2012.py 를 돌려보았는데요~ 에포크 첫번째까지는 잘 돌다가 에포크 1 끝나자마
 
아래와같은 에러가 뜨면서 종료되는데, 인터넷을 찾아봐도 해결하기 어려워서 여쭙습니다.
 
Epoch 1/5
2000/2000 [==============================] - ETA: 0s - loss: 0.1751 - accuracy: 0.9234
Epoch 1: loss improved from inf to 0.17507, saving model to saved_model_isbi_2012/unet_model.h5
Traceback (most recent call last):
File "train_isbi_2012.py", line 186, in <module>
app.run(main)
File "/media/jjaysohn/jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 312, in run
_run_main(main, args)
File "/media/jjaysohn/jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 258, in _run_main
sys.exit(main(argv)) File "train_isbi_2012.py", line 180, in main unet_model.fit_generator(train_generator,
File "/media/jjaysohn/jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/engine/training.py", line 2209, in fit_generator
return self.fit( File "/media/jjaysohn/jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/media/jjaysohn/jangjayPydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/saving/save.py", line 140, in save_model raise NotImplementedError( NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`. 2022-03-23 23:59:58.391685: W tensorflow/core/kernels/data/generator_dataset_op.cc:107]
Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated. [[{{node PyFunc}}]]

 

도움주시면 감사하겠습니다. ㅠㅠ

답변 3

·

답변을 작성해보세요.

1

안녕하세요~. 반갑습니다.

1번의 epoch이 끝날때마다 saved_model_isbi_2012라는 폴더안에 unet_model.h5라는 파일로 학습된 모델을 저장하는 로직이 있는데요.

폴더 안에 파일을 저장하는 부분에서 에러가 발생한 것 같습니다.

flags.DEFINE_string('checkpoint_path', default='saved_model_isbi_2012/unet_model.h5', help='path to a directory to save model checkpoints during training')


위 부분의 코드를 아래와 같이 변경하고

flags.DEFINE_string('checkpoint_path', default='unet_model.h5', help='path to a directory to save model checkpoints during training')

 

폴더를 생성하는 아래 코드를 아래와 같이 주석처리한뒤에

#if not os.path.exists(FLAGS.checkpoint_path.split('/')[0]):

#  os.mkdir(FLAGS.checkpoint_path.split('/')[0])

saved_model_isbi_2012 폴더 안에 h5 파일을 저장하지 말고 실행하는 스크립트 경로에 h5 파일을 저장하도록 변경해서 다시 실행해보시겠어요?

감사합니다.

0

자꾸 질문드려 죄송합니다. 

말씀해주신대로 h5 저장방식에서 바뀌었다 보니까, 

 

evaluate에서 저장된 모델을 다시 불러오는 부분도 바꿔서 진행해야할것같아

공식홈페이지 참고하여 

flags.DEFINE_string('checkpoint_path', default='/unet_model', help='path to a directory to restore checkpoint file')

위와같이 저장할때의 마찬가지의 경로로 바꾸어 주었구

 

로드하는 부분도 공식홈피에 설명에따라 원래 코드 주석처리하고 

  # unet_model.load_weights(FLAGS.checkpoint_path)

  unet_model.load_model(FLAGS.checkpoint_path)

 위와같이 바꾸었는데,

 

 

Traceback (most recent call last):

  File "evaluate_isbi_2012.py", line 90, in <module>

    app.run(main)

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 312, in run

    _run_main(main, args)

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 258, in _run_main

    sys.exit(main(argv))

  File "evaluate_isbi_2012.py", line 67, in main

    unet_model.load_model(FLAGS.checkpoint_path)

AttributeError: 'UNET_ISBI_2012' object has no attribute 'load_model'

load_model이라는 속성이 없다고 나오더라구요(공식홈피는 아래 링크를 참조했습니다.) 

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

UNET_ISBI_2012가 모델 속성을 받은 함수여서 그대로 사용하면 될줄알았는데,

위와같은 오류가뜨면 어떻게 해결해야하는지 여쭙습니다.

 

안녕하세요~.

load_model 함수 말고 기존의 load_weights 함수를 이용해서 evaluate를 진행해보시겠어요?

감사합니다.

그렇게 하니까 해결되었습니다. 감사합니다~ 

0

train_isbi_2012.py:180: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.

  unet_model.fit_generator(train_generator,

Epoch 1/5

2022-03-28 15:40:59.839414: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1207959552 exceeds 10% of free system memory.

2022-03-28 15:40:59.851804: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1207959552 exceeds 10% of free system memory.

   1/2000 [..............................] - ETA: 8:28:49 - loss: 0.5343 - accuracy: 0.75872022-03-28 15:41:14.463919: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1207959552 exceeds 10% of free system memory.

2022-03-28 15:41:14.464060: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1207959552 exceeds 10% of free system memory.

   2/2000 [..............................] - ETA: 8:31:23 - loss: 0.6406 - accuracy: 0.77262022-03-28 15:41:29.882777: W tensorflow/core/framework/cpu_allocator_impl.cc:82] Allocation of 1207959552 exceeds 10% of free system memory.

2000/2000 [==============================] - ETA: 0s - loss: 0.1751 - accuracy: 0.9234     

Epoch 1: loss improved from inf to 0.17507, saving model to unet_model.h5

Traceback (most recent call last):

  File "train_isbi_2012.py", line 186, in <module>

    app.run(main)

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 312, in run

    _run_main(main, args)

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/absl/app.py", line 258, in _run_main

    sys.exit(main(argv))

  File "train_isbi_2012.py", line 180, in main

    unet_model.fit_generator(train_generator,

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/engine/training.py", line 2209, in fit_generator

    return self.fit(

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler

    raise e.with_traceback(filtered_tb) from None

  File "/media/jjaysohn/Jangjay/Pydicom_read/U-Netpractice/U-net/lib/python3.8/site-packages/keras/saving/save.py", line 140, in save_model

    raise NotImplementedError(

NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using `save_weights`.

2022-03-28 23:02:14.273774: W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.

[[{{node PyFunc}}]]

말씀주신대로 실행해보았는데, 마찬가지 오류가 떴어요 ㅠ. 

좀더 찾아볼텐데, 혹시 추가 해결법 아시는법 있으면 알려주시면 감사하겠습니다.

감사합니다.  

 

 

 

 

안녕하세요~. 

최신 tensorflow 버전에서 h5 파일로 저장하는 부분의 API가 변경된 것 같습니다.

flags.DEFINE_string('checkpoint_path', default='saved_model_isbi_2012/unet_model.h5', help='path to a directory to save model checkpoints during training')

위 코드를 아래와 같이 변경해서 파일이 아니라 폴더가 최종 경로가 되게 변경해서 실행해보시겠어요?

flags.DEFINE_string('checkpoint_path', default='saved_model_isbi_2012/unet_model', help='path to a directory to save model checkpoints during training')

감사합니다.

말씀해주신대로 하니까 해결되었습니다 감사합니다.