Kerasで事前に訓練されたモデルを使用する方法は何ですか?

Kerasで事前学習済みモデルを使用する方法は2つあります:Kerasで提供されている事前学習済みモデル(VGG16、ResNet50、InceptionV3など)を使用するか、他のディープラーニングフレームワーク(TensorFlow、PyTorchなど)で訓練されたモデルを使用するかです。

  1. Kerasが提供する事前学習済みモデルを使用します。
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input, decode_predictions
from keras.preprocessing import image
import numpy as np

# 加载预训练的VGG16模型
model = VGG16(weights='imagenet')

# 加载要预测的图片,并进行预处理
img_path = 'path_to_your_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)

# 预测图像的类别
preds = model.predict(x)
predictions = decode_predictions(preds, top=3)[0]
for i, (imagenetID, label, score) in enumerate(predictions):
    print("{}. {}: {:.2f}%".format(i + 1, label, score * 100))
  1. 他の深層学習フレームワークで訓練されたモデルを使用します。
import tensorflow as tf
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
import numpy as np

# 加载其他深度学习框架中训练好的模型
model = tf.keras.models.load_model('path_to_your_model.h5')

# 加载要预测的图片,并进行预处理
img_path = 'path_to_your_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)

# 预测图像的类别
preds = model.predict(x)

Kerasを使って、画像分類や物体検出などのタスクに事前トレーニング済みのモデルを使用する方法が2つあります。

广告
広告は10秒後に閉じます。
bannerAds