问题描述
有没有办法在python 3.8中加载一个在python 3.7中训练并保存的tensorflow模型?当我尝试加载模型时,出现值错误:error。我还提供了用于加载模型的代码
import numpy as np
from skimage.transform import resize
from tensorflow.keras.models import load_model
import nibabel as nib
from sklearn.preprocessing import MinMaxScaler
# Set image sizes
IMG_WIDTH = 256
IMG_HEIGHT = 256
def predict_MS(IMAGE_PATH,MODEL_PATH,THRED_STEP):
path_flair = IMAGE_PATH
img_flair = nib.load(path_flair)
image_flair = np.array(img_flair.dataobj)
X_Test = np.zeros((img_flair.shape[2],IMG_HEIGHT,IMG_WIDTH,1),dtype=np.float32)
for counter in range(img_flair.shape[2]):
X_Test[counter,:,0] = resize(image_flair[:,counter],(IMG_HEIGHT,IMG_WIDTH),mode='constant',preserve_range=True)
reshape_X_Test_scale = np.copy(X_Test[:,0].reshape(-1,IMG_HEIGHT))
for counter3 in range(reshape_X_Test_scale.shape[0]):
scaler2 = MinMaxScaler(feature_range=(0,4095))
scaler2.fit(reshape_X_Test_scale[counter3].reshape(-1,1))
flair_scale = scaler2.transform(reshape_X_Test_scale[counter3].reshape(-1,1))
reshape_X_Test_scale[counter3] = flair_scale.reshape(-1,IMG_HEIGHT)
X_Test = np.zeros((img_flair.shape[2],dtype=np.float32)
X_Test[:,0] = reshape_X_Test_scale
del reshape_X_Test_scale,counter,counter3,flair_scale,image_flair,path_flair,scaler2,img_flair
#Load saved model
model = load_model(MODEL_PATH)
preds_test = model.predict(X_Test,verbose=1)
print("Prediction DONE!!!!")
preds_test_t = (preds_test > THRED_STEP).astype(np.bool_)
preds_test_t = preds_test_t.reshape(preds_test_t.shape[:-1])
del preds_test,THRED_STEP
return preds_test_t,X_Test
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)