微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在python 3.8中加载一个在python 3.7中训练并保存的tensorflow模型

如何解决在python 3.8中加载一个在python 3.7中训练并保存的tensorflow模型

有没有办法在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 举报,一经查实,本站将立刻删除。