评估预训练模型时遇到问题

时间:2021-01-18 14:20:04

标签: tensorflow machine-learning keras deep-learning conv-neural-network

我有一个微调版本的 inceptionV3 模型,我想在新数据集上测试它。但是,我收到错误 No model found in config file.

这是我的代码,

from tensorflow import keras
model = keras.models.load_model('/home/saved_model/CNN_inceptionv3.h5')

CLASS_1_data = '/home/spectrograms/data/c1'

def label_img(img):
    word_label = img[:5]
    if img[1] == '1':
      return [1,0]
    elif img[1] == '3':
      return [0,1]

def create_data(data,loc): #loads data into a list
    for img in tqdm(os.listdir(loc)):
        label = label_img(img)
        path = os.path.join(loc,img)
        img = Image.open(path) 
        img = ImageOps.grayscale(img) 
        # w,h = img.size
        # img = img.resize((w//3,h//3))
        data.append([np.array(img),np.array(label)])
    return data

def make_X_and_Y(set): #split data into numpy arrays of inputs and outputs
  set_X,set_Y = [],[]
  n = len(set)
  for i in range(n):
    set_X.append(set[i][0])
    set_Y.append(set[i][1])
  return np.array(set_X),np.array(set_Y)

data = []
data = create_data(data,CLASS_1_data)
data = np.array(data)

X_data,Y_data = make_X_and_Y(data)

X_data = X_data.astype('float32')

X_data /= 255 

results = model.evaluate(X-data, Y_data, batch_size=5)

这里的错误是什么?我该如何纠正它并测试我的模型?

1 个答案:

答案 0 :(得分:0)

要加载模型,您必须使用 model.save,以防您只想加载 wieghts model.save_weights。 在您的情况下,使用 model.save 它将上传模型。 让我知道。 或者你可以从 json 加载模型。

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

你有权重所以...加载json并创建模型

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

将权重加载到新模型中

loaded_model.load_weights("model.h5")
print("Loaded model from disk")

告诉我!