在Keras中使用预先训练好的GoogLeNet和AlexNet,可以通过以下步骤实现:
from keras.applications import googlenet, alexnet
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
from keras.models import Model
googlenet_model = googlenet.InceptionV1(weights='imagenet')
alexnet_model = alexnet.AlexNet(weights='imagenet')
img_path = 'path_to_image.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)
googlenet_predictions = googlenet_model.predict(x)
alexnet_predictions = alexnet_model.predict(x)
googlenet_decoded_predictions = googlenet.decode_predictions(googlenet_predictions, top=5)
alexnet_decoded_predictions = alexnet.decode_predictions(alexnet_predictions, top=5)
print('GoogLeNet Predictions:')
for _, class_name, prob in googlenet_decoded_predictions[0]:
print(f'{class_name}: {prob}')
print('AlexNet Predictions:')
for _, class_name, prob in alexnet_decoded_predictions[0]:
print(f'{class_name}: {prob}')
这样,你就可以在Keras中使用预先训练好的GoogLeNet和AlexNet进行图像分类预测了。
推荐的腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云