forked from X-beagle/X-beagle-Mlops-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
356dbdd
commit e55003b
Showing
3 changed files
with
2,705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
train: /home/ec2-user/SageMaker/aws_data/aws_dataset/train/ | ||
val: /home/ec2-user/SageMaker/aws_data/aws_dataset/val/ | ||
test: /home/ec2-user/SageMaker/aws_data/aws_dataset/test/ | ||
nc: 42 | ||
names: ['Hammer','SSD','Alcohol','Spanner','Axe','Awl','Throwing Knife','Firecracker','Thinner','Plier','Match', | ||
'Smart Phone','Scissors','Tablet PC','Solid Fuel','Bat','Portable Gas','Nail Clippers','Knife','Metal Pipe', | ||
'Electronic Cigarettes(Liquid)','Supplymentary Battery','Bullet','Gun Parts','USB','Liquid','Aerosol','Screwdriver', | ||
'Chisel','Handcuffs','Lighter','HDD','Electronic Cigarettes','Battery','Gun','Laptop','Saw','Zippo Oil','Stun Gun', | ||
'Camera','Camcorder','SD Card'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,359 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"id": "a83e7018", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os, sagemaker, subprocess, boto3\n", | ||
"from datetime import datetime\n", | ||
"from sagemaker import s3\n", | ||
"from sagemaker import get_execution_role\n", | ||
"from sagemaker.pytorch import PyTorchModel\n", | ||
"from sagemaker.deserializers import JSONDeserializer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"id": "1872fad5", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Overwriting code/requirements.txt\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%writefile code/requirements.txt\n", | ||
"nvgpu\n", | ||
"opencv-python\n", | ||
"torchvision\n", | ||
"seaborn\n", | ||
"ultralytics\n", | ||
"omegaconf==2.3.0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"id": "3f63b246", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Overwriting code/inference.py\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%%writefile code/inference.py\n", | ||
"import numpy as np\n", | ||
"import torch, os, json, io, cv2, time\n", | ||
"from ultralytics import YOLO\n", | ||
"\n", | ||
"def model_fn(model_dir):\n", | ||
" print(\"Executing model_fn from inference.py ...\")\n", | ||
" env = os.environ\n", | ||
" model = YOLO(os.path.join(model_dir, env['YOLOV8_MODEL']))\n", | ||
" return model\n", | ||
"\n", | ||
"def input_fn(request_body, request_content_type):\n", | ||
" print(\"Executing input_fn from inference.py ...\")\n", | ||
" if request_content_type:\n", | ||
" jpg_original = np.load(io.BytesIO(request_body), allow_pickle=True)\n", | ||
" jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)\n", | ||
" img = cv2.imdecode(jpg_as_np, flags=-1)\n", | ||
" else:\n", | ||
" raise Exception(\"Unsupported content type: \" + request_content_type)\n", | ||
" return img\n", | ||
" \n", | ||
"def predict_fn(input_data, model):\n", | ||
" print(\"Executing predict_fn from inference.py ...\")\n", | ||
" device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", | ||
" model.to(device)\n", | ||
" with torch.no_grad():\n", | ||
" result = model(input_data)\n", | ||
" return result\n", | ||
" \n", | ||
"def output_fn(prediction_output, content_type):\n", | ||
" print(\"Executing output_fn from inference.py ...\")\n", | ||
" infer = {}\n", | ||
" for result in prediction_output:\n", | ||
" if 'boxes' in result._keys and result.boxes is not None:\n", | ||
" infer['boxes'] = result.boxes.numpy().data.tolist()\n", | ||
" if 'masks' in result._keys and result.masks is not None:\n", | ||
" infer['masks'] = result.masks.numpy().data.tolist()\n", | ||
" if 'keypoints' in result._keys and result.keypoints is not None:\n", | ||
" infer['keypoints'] = result.keypoints.numpy().data.tolist()\n", | ||
" if 'probs' in result._keys and result.probs is not None:\n", | ||
" infer['probs'] = result.probs.numpy().data.tolist()\n", | ||
" return json.dumps(infer)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"id": "cf1b2eca", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Requirement already satisfied: ultralytics in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (8.0.196)\n", | ||
"Requirement already satisfied: matplotlib>=3.3.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (3.8.3)\n", | ||
"Requirement already satisfied: numpy>=1.22.2 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (1.26.4)\n", | ||
"Requirement already satisfied: opencv-python>=4.6.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (4.9.0.80)\n", | ||
"Requirement already satisfied: pillow>=7.1.2 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (10.2.0)\n", | ||
"Requirement already satisfied: pyyaml>=5.3.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (6.0.1)\n", | ||
"Requirement already satisfied: requests>=2.23.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (2.31.0)\n", | ||
"Requirement already satisfied: scipy>=1.4.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (1.12.0)\n", | ||
"Requirement already satisfied: torch>=1.8.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (2.1.0)\n", | ||
"Requirement already satisfied: torchvision>=0.9.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (0.16.0)\n", | ||
"Requirement already satisfied: tqdm>=4.64.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (4.66.2)\n", | ||
"Requirement already satisfied: pandas>=1.1.4 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (1.5.3)\n", | ||
"Requirement already satisfied: seaborn>=0.11.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (0.13.2)\n", | ||
"Requirement already satisfied: psutil in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (5.9.8)\n", | ||
"Requirement already satisfied: py-cpuinfo in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (9.0.0)\n", | ||
"Requirement already satisfied: thop>=0.1.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from ultralytics) (0.1.1.post2209072238)\n", | ||
"Requirement already satisfied: contourpy>=1.0.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (1.2.0)\n", | ||
"Requirement already satisfied: cycler>=0.10 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (0.12.1)\n", | ||
"Requirement already satisfied: fonttools>=4.22.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (4.50.0)\n", | ||
"Requirement already satisfied: kiwisolver>=1.3.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (1.4.5)\n", | ||
"Requirement already satisfied: packaging>=20.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (21.3)\n", | ||
"Requirement already satisfied: pyparsing>=2.3.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (3.1.2)\n", | ||
"Requirement already satisfied: python-dateutil>=2.7 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from matplotlib>=3.3.0->ultralytics) (2.9.0)\n", | ||
"Requirement already satisfied: pytz>=2020.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from pandas>=1.1.4->ultralytics) (2024.1)\n", | ||
"Requirement already satisfied: charset-normalizer<4,>=2 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (3.3.2)\n", | ||
"Requirement already satisfied: idna<4,>=2.5 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (3.6)\n", | ||
"Requirement already satisfied: urllib3<3,>=1.21.1 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (2.2.1)\n", | ||
"Requirement already satisfied: certifi>=2017.4.17 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from requests>=2.23.0->ultralytics) (2024.2.2)\n", | ||
"Requirement already satisfied: filelock in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.13.3)\n", | ||
"Requirement already satisfied: typing-extensions in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (4.10.0)\n", | ||
"Requirement already satisfied: sympy in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (1.12)\n", | ||
"Requirement already satisfied: networkx in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.2.1)\n", | ||
"Requirement already satisfied: jinja2 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (3.1.3)\n", | ||
"Requirement already satisfied: fsspec in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from torch>=1.8.0->ultralytics) (2024.3.1)\n", | ||
"Requirement already satisfied: six>=1.5 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->ultralytics) (1.16.0)\n", | ||
"Requirement already satisfied: MarkupSafe>=2.0 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from jinja2->torch>=1.8.0->ultralytics) (2.1.5)\n", | ||
"Requirement already satisfied: mpmath>=0.19 in /home/ec2-user/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages (from sympy->torch>=1.8.0->ultralytics) (1.3.0)\n" | ||
] | ||
}, | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Downloading https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m.pt to 'yolov8m.pt'...\n", | ||
"100%|██████████| 49.7M/49.7M [00:00<00:00, 399MB/s]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"!pip3 install ultralytics\n", | ||
"from ultralytics import YOLO\n", | ||
"\n", | ||
"## Choose a model:\n", | ||
"model_name = 'yolov8m.pt'\n", | ||
"\n", | ||
"YOLO(model_name)\n", | ||
"os.system(f'mv {model_name} code/.')\n", | ||
"\n", | ||
"bashCommand = \"tar -cpzf model.tar.gz code/\"\n", | ||
"process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)\n", | ||
"output, error = process.communicate()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"id": "6caba3c5", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Bucket: x-beagle\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"bucket = 'x-beagle'\n", | ||
"print(f'Bucket: {bucket}')\n", | ||
"\n", | ||
"sess = sagemaker.Session(default_bucket=bucket)\n", | ||
"\n", | ||
"prefix = \"yolov8/final-endpoint\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"id": "c879a119", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"model.tar.gz uploaded to S3 bucket x-beagle\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import boto3\n", | ||
"\n", | ||
"# S3 버킷 이름\n", | ||
"bucket_name = 'x-beagle'\n", | ||
"# 업로드할 파일 이름\n", | ||
"file_name = 'model.tar.gz'\n", | ||
"# 업로드할 로컬 경로 및 파일 이름\n", | ||
"local_file_path = '/home/ec2-user/SageMaker/model.tar.gz' \n", | ||
"\n", | ||
"# S3 클라이언트 생성\n", | ||
"s3_client = boto3.client('s3')\n", | ||
"\n", | ||
"# model.tar.gz 파일을 S3 버킷에 업로드\n", | ||
"s3_client.upload_file(local_file_path, bucket_name, file_name)\n", | ||
"\n", | ||
"print(f'{file_name} uploaded to S3 bucket {bucket_name}')\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 33, | ||
"id": "a559681f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import boto3\n", | ||
"from sagemaker import get_execution_role\n", | ||
"from sagemaker.pytorch import PyTorchModel\n", | ||
"from sagemaker.deserializers import JSONDeserializer\n", | ||
"\n", | ||
"# S3 버킷 이름과 업로드된 모델의 위치\n", | ||
"bucket_name = 'x-beagle'\n", | ||
"model_artifact = 'model.tar.gz'\n", | ||
"\n", | ||
"# SageMaker 실행 역할 가져오기\n", | ||
"role = get_execution_role()\n", | ||
"\n", | ||
"# SageMaker 세션 및 클라이언트 생성\n", | ||
"import sagemaker\n", | ||
"sagemaker_session = sagemaker.Session()\n", | ||
"sagemaker_client = boto3.client('sagemaker')\n", | ||
"\n", | ||
"# 모델 데이터의 S3 경로\n", | ||
"model_data = f's3://{bucket_name}/{model_artifact}'\n", | ||
"\n", | ||
"# PyTorch 모델 생성\n", | ||
"model = PyTorchModel(entry_point='inference.py',\n", | ||
" model_data=model_data, \n", | ||
" framework_version='1.12', \n", | ||
" py_version='py38',\n", | ||
" role=role,\n", | ||
" env={'TS_MAX_RESPONSE_SIZE':'20000000', 'YOLOV8_MODEL': model_name},\n", | ||
" sagemaker_session=sagemaker_session,\n", | ||
" name='xbeagle-model-pytorch')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 34, | ||
"id": "47cf2334", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"-------!Endpoint Name: x-beagle-yolov8-2024-05-24-13-22-32-582715\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"# 엔드포인트 이름 생성\n", | ||
"ENDPOINT_NAME = 'x-beagle-yolov8-' + str(datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-%f'))\n", | ||
"\n", | ||
"# 엔드포인트 배포\n", | ||
"predictor = model.deploy(initial_instance_count=1, \n", | ||
" instance_type='ml.m5.4xlarge',\n", | ||
" deserializer=JSONDeserializer(),\n", | ||
" endpoint_name=ENDPOINT_NAME)\n", | ||
"\n", | ||
"print(f'Endpoint Name: {ENDPOINT_NAME}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 35, | ||
"id": "ba65c03f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"predictor_reuse=sagemaker.predictor.Predictor(\n", | ||
" endpoint_name=\"xbeagle-yolov8-YYYY-MM-DD-HH-MM-SS-SSS\",\n", | ||
" sagemaker_session=sagemaker.Session(),\n", | ||
" serializer=sagemaker.serializers.CSVSerializer()\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "2f5e8ebe", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Stored 'ENDPOINT_NAME' (str)\n", | ||
"Endpoint Name: xbeagle-yolov8-2024-05-24-05-31-12-296466\n", | ||
"---------!" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"INSTANCE_TYPE = 'ml.g4dn.4xlarge'\n", | ||
"ENDPOINT_NAME = 'xbeagle-yolov8-' + str(datetime.utcnow().strftime('%Y-%m-%d-%H-%M-%S-%f'))\n", | ||
"%store ENDPOINT_NAME\n", | ||
"print(f'Endpoint Name: {ENDPOINT_NAME}')\n", | ||
"\n", | ||
"predictor = model.deploy(initial_instance_count=1, \n", | ||
" instance_type=INSTANCE_TYPE,\n", | ||
" deserializer=JSONDeserializer(),\n", | ||
" endpoint_name=ENDPOINT_NAME)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "conda_pytorch_p310", | ||
"language": "python", | ||
"name": "conda_pytorch_p310" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.14" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.