天天看點

keras vgg16

用官網例子:

from keras.applications.vgg16 import VGG16

from keras.preprocessing import image

from keras.applications.vgg16 import preprocess_input,decode_predictions

import numpy as np

model = VGG16(weights='imagenet',include_top=True)

img_path = './timg.jpg'

img = image.load_img(img_path,target_size=(224,224))

x = image.img_to_array(img)

x = np.expand_dims(x,axis=0)

x = preprocess_input(x)

preds = model.predict(x)

print('Predicted:',decode_predictions(preds,top=3)[0])

會報錯

解決辦法:

1、按報錯提供的網址下載下傳vgg16_weights_tf_dim_ordering_tf_kernels.h5(include_top=True)

和vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5(include_top=False)

以及imagenet_class_index.json(imagenet_utils. decode_predictions)

2、輸入 open .keras/models/ 打開影藏檔案夾models,将上面三個檔案放進去

3、print(keras.__file__),找到安裝路徑,在applications裡面找到vgg.py和imagenet_utils.py,

将WEIGHTS_PATH,WEIGHTS_PATH_NO_TOP,CLASS_INDEX_PATH的路徑改成上面三個檔案的新路徑

完成!

其他模型方法應該是一樣的

繼續閱讀