---
# System prepended metadata

title: GCP - 建立Detectron2 AI環境
tags: [AI]

---

GCP如果是乾淨環境或是qwiklab會出現類似以下內容於CLI
執行步驟如下
```bash
Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to qwiklabs-gcp-01-9e010417e853.
Use “gcloud config set project [PROJECT_ID]” to change to a different project.
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud auth list
Credentialed Accounts

ACTIVE: *
ACCOUNT: student-00-75195726d0cd@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`

student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud config list project
[core]
project = qwiklabs-gcp-01-9e010417e853

Your active configuration is: [cloudshell-31071]
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud services enable run.googleapis.com
Operation "operations/acf.p2-57217125899-3dd872cd-af01-4cea-bdca-c59bedf879db" finished successfully.
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  LOCATION=us-west1
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud config set compute/region $LOCATION
Updated property [compute/region].
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ echo $LOCATION
us-west1
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud run deploy store-service \
  --image gcr.io/qwiklabs-resources/gsp724-store-service \
  --region $LOCATION \
  --allow-unauthenticated
Deploying container to Cloud Run service [store-service] in project [qwiklabs-gcp-01-9e010417e853] region [us-west1]
OK Deploying new service... Done.                                                                                                                                                                                                          
  OK Creating Revision...                                                                                                                                                                                                                  
  OK Routing traffic...                                                                                                                                                                                                                    
  OK Setting IAM Policy...                                                                                                                                                                                                                 
Done.                                                                                                                                                                                                                                      
Service [store-service] revision [store-service-00001-jvc] has been deployed and is serving 100 percent of traffic.
Service URL: https://store-service-7zhtweufua-uw.a.run.app
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud run deploy order-service \
  --image gcr.io/qwiklabs-resources/gsp724-order-service \
  --region $LOCATION \
  --no-allow-unauthenticated
Deploying container to Cloud Run service [order-service] in project [qwiklabs-gcp-01-9e010417e853] region [us-west1]
OK Deploying new service... Done.                                                                                                                                                                                                          
  OK Creating Revision...                                                                                                                                                                                                                  
  OK Routing traffic...                                                                                                                                                                                                                    
Done.                                                                                                                                                                                                                                      
Service [order-service] revision [order-service-00001-z9x] has been deployed and is serving 100 percent of traffic.
Service URL: https://order-service-7zhtweufua-uw.a.run.app
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud pubsub topics create ORDER_PLACED
Created topic [projects/qwiklabs-gcp-01-9e010417e853/topics/ORDER_PLACED].
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud iam service-accounts create pubsub-cloud-run-invoker \
Created service account [pubsub-cloud-run-invoker].
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud iam service-accounts list --filter="Order Initiator"
DISPLAY NAME: Order Initiator
EMAIL: pubsub-cloud-run-invoker@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
DISABLED: False
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud pubsub topics create ORDER_PLACED
ERROR: Failed to create topic [projects/qwiklabs-gcp-01-9e010417e853/topics/ORDER_PLACED]: Resource already exists in the project (resource=ORDER_PLACED).
ERROR: (gcloud.pubsub.topics.create) Failed to create the following: [ORDER_PLACED].
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud iam service-accounts list --filter="Order Initiator"                                                                                                            
DISPLAY NAME: Order Initiator
EMAIL: pubsub-cloud-run-invoker@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
DISABLED: False
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud iam service-accounts create pubsub-cloud-run-invoker \
  --display-name "Order Initiator"
ERROR: (gcloud.iam.service-accounts.create) Resource in projects [qwiklabs-gcp-01-9e010417e853] is the subject of a conflict: Service account pubsub-cloud-run-invoker already exists within project projects/qwiklabs-gcp-01-9e010417e853.
- '@type': type.googleapis.com/google.rpc.ResourceInfo
  resourceName: projects/qwiklabs-gcp-01-9e010417e853/serviceAccounts/pubsub-cloud-run-invoker@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud iam service-accounts list
DISPLAY NAME: Compute Engine default service account
EMAIL: 57217125899-compute@developer.gserviceaccount.com
DISABLED: False

DISPLAY NAME: Order Initiator
EMAIL: pubsub-cloud-run-invoker@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
DISABLED: False

DISPLAY NAME: Qwiklabs User Service Account
EMAIL: qwiklabs-gcp-01-9e010417e853@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
DISABLED: False
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud run services add-iam-policy-binding order-service --region $LOCATION \
  --member=serviceAccount:pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com \
  --role=roles/run.invoker --platform managed
Updated IAM policy for service [order-service].
bindings:
- members:
  - serviceAccount:pubsub-cloud-run-invoker@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
  role: roles/run.invoker
etag: BwYLxglnXlQ=
version: 1
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud pubsub 
lite-operations      lite-reservations    lite-subscriptions   lite-topics          schemas              snapshots            subscriptions        topics               
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ gcloud pubsub ^C
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  PROJECT_NUMBER=$(gcloud projects list \
  --filter="qwiklabs-gcp" \
  --format='value(PROJECT_NUMBER)')
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
   --member=serviceAccount:service-$PROJECT_NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com \
   --role=roles/iam.serviceAccountTokenCreator
Updated IAM policy for project [qwiklabs-gcp-01-9e010417e853].
bindings:
- members:
  - serviceAccount:qwiklabs-gcp-01-9e010417e853@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
  role: roles/bigquery.admin
- members:
  - serviceAccount:57217125899@cloudbuild.gserviceaccount.com
{
  role: roles/cloudbuild.builds.builder
- members:
  - serviceAccount:service-57217125899@gcp-sa-cloudbuild.iam.gserviceaccount.com
  role: roles/cloudbuild.serviceAgent
- members:
  - serviceAccount:service-57217125899@compute-system.iam.gserviceaccount.com
  role: roles/compute.serviceAgent
- members:
  - serviceAccount:service-57217125899@container-engine-robot.iam.gserviceaccount.com
  role: roles/container.serviceAgent
- members:
  - serviceAccount:service-57217125899@containerregistry.iam.gserviceaccount.com
  role: roles/containerregistry.ServiceAgent
- members:
  - serviceAccount:57217125899-compute@developer.gserviceaccount.com
  - serviceAccount:57217125899@cloudservices.gserviceaccount.com
  role: roles/editor
- members:
  - serviceAccount:service-57217125899@gcp-sa-pubsub.iam.gserviceaccount.com
  role: roles/iam.serviceAccountTokenCreator
- members:
  - serviceAccount:admiral@qwiklabs-services-prod.iam.gserviceaccount.com
  - serviceAccount:qwiklabs-gcp-01-9e010417e853@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
  - user:student-00-75195726d0cd@qwiklabs.net
  role: roles/owner
- members:
  - serviceAccount:service-57217125899@gcp-sa-pubsub.iam.gserviceaccount.com
  role: roles/pubsub.serviceAgent
- members:
  - serviceAccount:service-57217125899@serverless-robot-prod.iam.gserviceaccount.com
  role: roles/run.serviceAgent
- members:
  - serviceAccount:qwiklabs-gcp-01-9e010417e853@qwiklabs-gcp-01-9e010417e853.iam.gserviceaccount.com
  role: roles/storage.admin
- members:
  - user:student-00-75195726d0cd@qwiklabs.net
  role: roles/viewer
etag: BwYLxg0bW4o=
version: 1
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  ORDER_SERVICE_URL=$(gcloud run services describe order-service \
   --region $LOCATION \
   --format="value(status.address.url)")
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ ^C
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ echo $ORDER_SERVICE_URL
https://order-service-7zhtweufua-uw.a.run.app
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  gcloud pubsub subscriptions create order-service-sub \
   --topic ORDER_PLACED \
   --push-endpoint=$ORDER_SERVICE_URL \
   --push-auth-service-account=pubsub-cloud-run-invoker@$GOOGLE_CLOUD_PROJECT.iam.gserviceaccount.com
Created subscription [projects/qwiklabs-gcp-01-9e010417e853/subscriptions/order-service-sub].
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ vi test.json
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ vim test.json
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  STORE_SERVICE_URL=$(gcloud run services describe store-service \
   --region $LOCATION \
   --format="value(status.address.url)")
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$  curl -X POST -H "Content-Type: application/json" -d @test.json $STORE_SERVICE_URL
student_00_75195726d0cd@cloudshell:~ (qwiklabs-gcp-01-9e010417e853)$ 
```

![圖片](https://hackmd.io/_uploads/B1IcIA3Hp.png)
![Hcr1aEF3D4QfghnerR6TTSS0djo7UpGyzSsidGSp9vA=](https://hackmd.io/_uploads/HkWnUR2Ba.png)
test.json
```bash=
{
 "billing_address": {
   "name": "Kylie Scull",
   "address": "6471 Front Street",
   "city": "Mountain View",
   "state_province": "CA",
   "postal_code": "94043",
   "country": "US"
 },
 "shipping_address": {
   "name": "Kylie Scull",
   "address": "9902 Cambridge Grove",
   "city": "Martinville",
   "state_province": "BC",
   "postal_code": "V1A",
   "country": "Canada"
 },
 "items": [
   {
     "id": "RW134",
     "quantity": 1,
     "sub-total": 12.95
   },
   {
     "id": "IB541",
     "quantity": 2,
     "sub-total": 24.5
   }
 ]
}
```

```bash=
# 使用基於 PyTorch 的映像，支援 CUDA 11.1、CuDNN 8，並提供開發工具
FROM pytorch/pytorch:1.9.1-cuda11.1-cudnn8-devel

# 移除 CUDA 相關的 apt 存儲庫，並更新 NVIDIA 的 GPG 金鑰
RUN rm /etc/apt/sources.list.d/cuda.list
RUN rm /etc/apt/sources.list.d/nvidia-ml.list
RUN apt-key del 7fa2af80
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/7fa2af80.pub

# 設定工作目錄
WORKDIR /workplace

# 添加模型資料夾
ADD model /model

# 設定環境變數
ENV UAV_SERVICE=/workplace/5G-AI-rescue-proj-api

# 設定非交互式 Debian Frontend 和時區
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Taipei

# 安裝相關套件
RUN apt-get update && apt-get install -y \
	build-essential \
	pkg-config \
    autoconf \
    automake \
	libtool \
	cmake \
	git \
    vim \
    wget

# 安裝 Python 開發相關套件和必要工具
RUN apt-get -y update && apt-get -y install \
    python3-dev \
    python3-pip \
    python3-numpy

# 安裝額外的相依性
RUN apt-get -y install libglib2.0-0 libsm6 libxext6 libxrender-dev libgl1

# 升級 pip，並安裝 Python 套件
RUN pip3 install --upgrade pip
RUN pip3 install pandas opencv-python matplotlib pillow paho-mqtt psycopg2-binary jupyter pyyaml watchdog

# 下載 Detectron2 並安裝
RUN \
    git clone https://github.com/facebookresearch/detectron2.git && \
    cd detectron2  && \
    git checkout 5aeb252b194b93dc2879b4ac34bc51a31b5aee13 && \
    cd ..  && \
    python -m pip install -e detectron2

```