Keras Hub,您的一站式预训练模型库
作者 / 软件工程师 Divyashree Sreepathihalli 和 Google AI 开发技术推广工程师 Luciano Martins
KerasHub https://keras.io/keras_hub/
统一的开发者体验
KerasHub 入门步骤
现成的模型 https://keras.io/api/keras_hub/models/
安装 KerasHub
$ pip install --upgrade keras-hub
import os# Define the Keras 3 backend you want to use - "jax", "tensorflow" or "torch"os.environ["KERAS_BACKEND"] = "jax"# Import Keras 3 and KerasHub modulesimport kerasimport keras_hub
通过 KerasHub 使用
计算机视觉和自然语言模型
Gemma
Gemma https://ai.google.dev/gemma/docs/base
# Load Gemma 2 2B preset from Kaggle modelsgemma_lm = keras_hub.models.GemmaCausalLM.from_preset("gemma_2b_en")# Start generating contents with Gemma 2 2Bgemma_lm.generate("Keras is a", max_length=32)
Gemma 模型卡 https://www.kaggle.com/models/google/gemma/
PaliGemma
import os# Define the Keras 3 backend you want to use - "jax", "tensorflow" or "torch"os.environ["KERAS_BACKEND"] = "jax"# Import Keras 3 and KerasHub modulesimport kerasimport keras_hubfrom keras.utils import get_file, load_img, img_to_array# Import PaliGemma 3B fine tuned with 224x224 imagespali_gemma_lm = keras_hub.models.PaliGemmaCausalLM.from_preset("pali_gemma_3b_mix_224")# Download a test image and prepare it for usage with KerasHuburl = 'https://storage.googleapis.com/keras-cv/models/paligemma/cow_beach_1.png'img_path = get_file(origin=url)img = img_to_array(load_img(image_path))# Create the prompt with the question about the imageprompt = 'answer where is the cow standing?'# Generate the contents with PaliGemmaoutput = pali_gemma_lm.generate(inputs={"images": img,"prompts": prompt,})
PaliGemma https://ai.google.dev/gemma/docs/paligemma PaLI-3 https://arxiv.org/abs/2310.09199 SigLIP 视觉模型 https://arxiv.org/abs/2303.15343 Gemma 语言模型 https://arxiv.org/abs/2403.08295
Kaggle 上查看 Keras 中的模型列表 https://www.kaggle.com/organizations/keras/models
Stability.ai Stable Diffusion 3
from PIL import Imagefrom keras.utils import array_to_imgfrom keras_hub.models import StableDiffusion3TextToImagetext_to_image = StableDiffusion3TextToImage.from_preset("stable_diffusion_3_medium",height=1024,width=1024,dtype="float16",)# Generate images with SD3image = text_to_image.generate("photograph of an astronaut riding a horse, detailed, 8k",)# Display the generated imageimg = array_to_img(image)img
Stable Diffusion 3 https://stability.ai/news/stable-diffusion-3
Keras 中的模型列表 https://keras.io/api/keras_hub/models/
对于 KerasNLP 开发者而言,
有哪些变化?
import keras_nlp# Load a BERT modelclassifier = keras_nlp.models.BertClassifier.from_preset("bert_base_en_uncased",num_classes=2,)
import keras_hub# Load a BERT modelclassifier = keras_hub.models.BertClassifier.from_preset("bert_base_en_uncased",num_classes=2,)
对于 KerasCV 开发者而言,
有哪些变化?
如果您当前是 KerasCV 用户,更新到 KerasHub 能够为您带来以下好处:
简化模型加载:KerasHub 为加载模型提供了统一的 API,如果您同时使用 KerasCV 和 KerasNLP,这可以简化您的代码。 框架灵活性:如果您有兴趣探索 JAX 或 PyTorch 等不同框架,KerasHub 可以让您更轻松地将这些框架与 KerasCV 和 KerasNLP 模型结合起来使用。 集中式存储库:借助 KerasHub 的统一模型存储库,您可以更轻松地查找和访问模型,未来还可以在其中添加新架构。
如何使我的代码适配 KerasHub?
模型
import keras_hub# Load a model using presetModel = keras_hub.models.<model_name>.from_preset('preset_name`)# or load a custom model by specifying the backbone and preprocessorModel = keras_hub.models.<model_name>(backbone=backbone, preprocessor=preprocessor)
Centerpillar https://www.kaggle.com/models/keras/centerpillar
KerasHub 为 KerasCV 开发者带来了激动人心的新功能,提供了更高的灵活性和扩展能力。其中包括:
内置预处理
# Preprocess inputs for exampledef preprocess_inputs(image, label):# Resize rescale or do more preprocessing on inputsreturn preprocessed_inputsbackbone = keras_cv.models.ResNet50V2Backbone.from_preset("resnet50_v2_imagenet",)model = keras_cv.models.ImageClassifier(backbone=backbone,num_classes=4,)output = model(preprocessed_input)
classifier = keras_hub.models.ImageClassifier.from_preset('resnet_18_imagenet')classifier.predict(inputs)
损失函数
import kerasimport keras_cvkeras_cv.losses.FocalLoss(alpha=0.25, gamma=2, from_logits=False, label_smoothing=0, **kwargs)
FocalLoss 函数 https://keras.io/api/keras_cv/losses/focal_loss/
import keraskeras.losses.FocalLoss(alpha=0.25, gamma=2, from_logits=False, label_smoothing=0, **kwargs)
开始使用 KerasHub
查看官方文档,开始使用 KerasHub https://keras.io/keras_hub/ 查看 KerasHub 入门指南 https://keras.io/guides/keras_hub/ 试用预训练模型 https://keras.io/api/keras_hub/models/ 探索源代码,期待看到您做出的贡献 https://github.com/keras-team/keras-hub/ 在 Kaggle 上深入了解 Keras https://www.kaggle.com/organizations/keras
谷歌开发者特别招募活动进行中
诚邀热爱技术的你加入
通过多种形式 (文章/视频/coding 等) 创作与 Google 技术相关的讲解分享、实践案例或活动感受等内容,以及分享您应用 AI 技术的故事经历与成果。我们将为您提供平台和资源,助力您在分享中提升技能。更有惊喜权益等您领取,快来报名参与吧!