前言:
因為內存不足以在訓練的時候存 SavedModel模型
所以我分離出來
以.h5檔案先做訓練
再以.h5檔案載入
並用成.pb檔案類型的儲存方式
來達成目的
程式碼:
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Flatten, Dense, Dropout
from tensorflow.python.keras.applications.resnet import ResNet50
from tensorflow.python.keras.optimizer_v2.adam import Adam
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.models import Sequential, load_model
from tensorflow.python.keras.layers import LSTM, Dropout, Dense
from tensorflow.python.keras.callbacks import ModelCheckpoint
from tensorflow.python.framework.ops import disable_eager_execution
#解決內存配置不足的問題
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Allow memory growth for the GPU
physical_devices = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
disable_eager_execution()
tf.compat.v1.experimental.output_all_intermediates(True)
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config)
net_final = load_model('model-resnet50-final.h5')
net_final.save('saved_model/my_model')