如何基于开源数据在GCP Vertex AI快速创建玩具图像数据集?
将Keras图像数据集导入Vertex AI的方法
以下以CIFAR-10数据集为例,介绍两种便捷导入Vertex AI图像数据集的方式:
一、Python脚本方式
步骤1:安装依赖
首先安装所需的Python库:
pip install tensorflow google-cloud-aiplatform google-cloud-storage pillow
步骤2:加载并预处理Keras数据集
将CIFAR-10的图像按类别保存到本地目录,符合Vertex AI的图像分类数据集格式要求:
import tensorflow as tf import os from PIL import Image # 加载CIFAR-10数据集 (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() # CIFAR-10类别名称 class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] # 创建本地存储目录 base_dir = "./cifar10_images" os.makedirs(base_dir, exist_ok=True) # 保存训练集图像 for idx in range(len(x_train)): class_label = class_names[y_train[idx][0]] class_dir = os.path.join(base_dir, "train", class_label) os.makedirs(class_dir, exist_ok=True) img = Image.fromarray(x_train[idx]) img.save(os.path.join(class_dir, f"train_{idx}.png")) # 保存测试集图像(可选) for idx in range(len(x_test)): class_label = class_names[y_test[idx][0]] class_dir = os.path.join(base_dir, "test", class_label) os.makedirs(class_dir, exist_ok=True) img = Image.fromarray(x_test[idx]) img.save(os.path.join(class_dir, f"test_{idx}.png"))
步骤3:上传图像到Google Cloud Storage(GCS)
将本地预处理好的图像文件夹上传到GCS:
from google.cloud import storage def upload_local_dir_to_gcs(local_dir, gcs_bucket_name, gcs_target_path): storage_client = storage.Client() bucket = storage_client.bucket(gcs_bucket_name) for root, _, files in os.walk(local_dir): for file in files: local_file_path = os.path.join(root, file) relative_path = os.path.relpath(local_file_path, local_dir) gcs_file_path = os.path.join(gcs_target_path, relative_path) blob = bucket.blob(gcs_file_path) blob.upload_from_filename(local_file_path) # 替换为你的GCS桶名和目标路径 GCS_BUCKET = "your-gcs-bucket-name" GCS_TARGET_PATH = "datasets/cifar10" upload_local_dir_to_gcs(base_dir, GCS_BUCKET, GCS_TARGET_PATH)
步骤4:创建Vertex AI图像数据集
通过Vertex AI SDK创建图像分类数据集:
from google.cloud import aiplatform # 初始化Vertex AI客户端 aiplatform.init( project="your-google-cloud-project-id", location="us-central1" # 可根据需求替换为其他区域 ) # 创建图像分类数据集 dataset = aiplatform.ImageDataset.create( display_name="cifar10-image-dataset", gcs_source=f"gs://{GCS_BUCKET}/{GCS_TARGET_PATH}/train/**", import_schema_uri=aiplatform.schema.dataset.ioformat.image.classification ) print(f"数据集创建成功,资源ID:{dataset.resource_name}")
二、gcloud命令方式
步骤1:预处理并上传数据到GCS
先完成上述Python脚本中的步骤1-2,将图像保存到本地目录,再用gsutil命令上传到GCS:
gsutil cp -r ./cifar10_images gs://your-gcs-bucket-name/datasets/cifar10
步骤2:创建Vertex AI数据集
使用gcloud ai datasets create命令创建图像分类数据集:
gcloud ai datasets create \ --display-name=cifar10-image-dataset \ --project=your-google-cloud-project-id \ --location=us-central1 \ --image-classification \ --gcs-source=gs://your-gcs-bucket-name/datasets/cifar10/train/**
注意事项
- 确保已配置好Google Cloud认证(本地可通过
gcloud auth application-default login完成),并拥有AI Platform Admin、Storage Admin等相关权限。 - 其他Keras图像数据集(如Fashion-MNIST、MNIST)可采用相同流程,仅需调整类别名称和图像保存逻辑。
- 若需导入测试集,可在创建数据集时添加
--gcs-source参数指定测试集路径,或后续通过Vertex AI控制台补充导入。
内容的提问来源于stack exchange,提问作者Maxime Oriol
相关产品推荐
相关产品推荐

