用官网例子:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input,decode_predictions
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的路径改成上面三个文件的新路径
完成!
其他模型方法应该是一样的