日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sagemaker快速学习

發(fā)布時間:2024/9/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sagemaker快速学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Sagemaker學(xué)習(xí)

1. 使用boto3對S3操作

學(xué)sagemaker前先學(xué)習(xí)一下boto3庫。因為使用Sagemaker要配合S3服務(wù),使用這里 先學(xué)習(xí)boto3對S3服務(wù)的控制。

導(dǎo)入

import boto3

指定使用AWS上哪種服務(wù)

#獲取s3服務(wù)資源對象(相比client對象擁有更高級的抽象調(diào)用) s3 = boto3.resource('s3',region_name=region) #獲取S3客戶端對象(原生) s3_client = boto3.client('s3',region_name=region)

存儲桶的三個操作對象

#通過資源對象 s3 = boto3.resource('s3',region_name=region) #通過桶對象 s3.Bucket('mybucket') #S3客戶端對象 s3_client = boto3.client('s3',region_name=region)

創(chuàng)造一個S3存儲桶

#定義區(qū)域,因為S3是全球服務(wù),所以要聲明要使用哪個區(qū)域的S3服務(wù) region = 'us-west-2' bucket_name = 'my-bucket' #獲取一個S3客戶端對象 s3_client = boto3.client('s3',region_name=region) s3_client.create_bucket(Bucket=bucket_name)

打印桶名

#s3 s3對象 #s3.buckets.all() 獲得s3中的所有桶對象 #bucket.name 桶名 s3 = boto3.resource('s3') for bucket in s3.buckets.all():print(bucket.name)

刪除存儲桶

s3_client = boto3.client('s3',region_name=region) s3_client.delete_bucket(Bucket='mybucket')

上傳文件

#文件方式 s3 = boto3.resource('s3') s3.Bucket('mybucket').upload_file('/tmp/hello.txt', 'hello.txt')#對象方式 s3 = boto3.resource('s3') with open('filename', 'rb') as data:s3.Bucket('mybucket').upload_fileobj(data, 'mykey')#s3.Bucket()方法返回指定的桶對象 #put_object() key:保存的文件名, Body:對象數(shù)據(jù) s3 = boto3.resource('s3') with open('filename', 'rb') as data:s3.Bucket('my-bucket').put_object(Key='test.jpg', Body=data)#使用客戶端對象上傳文件 s3_client = boto3.client('s3',region_name=region) s3_client.upload_file('myfile', Bucket='my-bucket', Key='mys3file')#使用客戶端對象上傳對象 s3_client = boto3.client('s3',region_name=region) with open('filename', 'rb') as data:s3_client.upload_fileobj(data, 'mybucket', 'mykey')

分段上傳

關(guān)于分段上傳這里解析一下:分段上傳是將一個大型對象,切分為若干個中小片段,并對這若干個片段進行并行上傳,從上傳速度來將,分段上傳可以明顯比普通上傳要快。

分段上傳是一個三步過程:啟動上傳,上傳對象部分,然后在上傳所有部分之后,完成分段上傳。

分段上傳都有一個對應(yīng)的ID號來識別唯一的上傳操作。

s3_client = boto3.client('s3',region_name=region) #創(chuàng)建分段上傳 response = s3_client.create_multipart_upload(Bucket='my-bucket',Key='myfile') #拿到上傳ID ID = response['UploadId'] #讀取一個大型文件 data = open('file', 'rb') #進行多段上傳 s3_client.upload_part(Body=data,Bucket='my-bucket',Key='myfile',PartNumber=5,UploadId=ID) #對上傳到服務(wù)端多段數(shù)據(jù)發(fā)起合并請求 response2 = client.complete_multipart_upload(Bucket='examplebucket',Key='myfile',UploadId=ID)

從s3中下載文件

  • bucket S3存儲桶名字
  • path 桶文件路徑
  • localhost_path 本地路徑名
s3 = boto3.resource('s3') #下載文件 s3.download_file(bucket, path, localhost_path)#通過桶對象下載文件 s3.Bucket('mybucket').download_file('hello.txt', '/tmp/hello.txt')#通過客戶端下載文件對象 s3_client = boto3.client('s3') with open('filename', 'wb') as data:s3_client.download_fileobj('mybucket', 'mykey', data)#通過客戶端下載文件 s3_client.download_file('mybucket', 'hello.txt', '/tmp/hello.txt')#通過get_object獲取一個對象 #返回response,response包含對象的描述信息和數(shù)據(jù)體 response = s3_client.get_object_acl(Bucket='examplebucket',Key='HappyFace.jpg', ) data = response['Body']

刪除一個S3中的對象

#通過客戶端刪除 s3_client = boto3.client('s3',region_name=region) s3_client.delete_object(Bucket='mybucket',Key='cat.jpg')#通過桶對象刪除 s3 = boto3.resource('s3') s3.Bucket('mybucket').download_file('hello.txt', '/tmp/hello.txt')

獲取區(qū)域名

boto3.Session.region_name

2.boto3調(diào)用sagemaker

2.1使用sagemakerRuntime

SageMakerRuntime.Client是一個運行時低級的客戶端類,提供一些簡易的方法供程序員調(diào)用sagemaker上的資源。

client = boto3.client('sagemaker-runtime')

SageMakerRuntime.Client提供了以下方法:

  • can_paginate()
  • generate_presigned_url()
  • get_paginator()
  • get_waiter()
  • invoke_endpoint()

can_paginate()和get_paginator()

can_paginate(operation_name)用于判斷一個方法是否允許分頁。

get_paginator()用于個一個操作創(chuàng)建分頁。

關(guān)于分頁請求這個概念,這里簡單描述一下:有些AWS操作返回的結(jié)果不完整,需要后續(xù)請求才能獲得整個結(jié)果集。在前一個請求未處理的情況下發(fā)送后續(xù)請求以繼續(xù)的過程稱為分頁。例如,amazon s3的list objects操作一次最多返回1000個對象,您必須使用適當?shù)臉擞洶l(fā)送后續(xù)請求,以便檢索下一頁的結(jié)果。

這里引用官方的案例:

import botocore.session# Create a session and a client session = botocore.session.get_session() client = session.create_client('s3', region_name='us-west-2')# Create a reusable Paginator paginator = client.get_paginator('list_objects')# Create a PageIterator from the Paginator page_iterator = paginator.paginate(Bucket='my-bucket')for page in page_iterator:print(page['Contents'])

invoke_endpoint()

這個方法比較常用,該方法用于調(diào)用一個sagemaker終端節(jié)點去做模型的結(jié)果推斷。

  • sagemaker終端節(jié)點是sagemaker的一項服務(wù),用于將訓(xùn)練好的模型進行快速的部署并用于對數(shù)據(jù)集進行結(jié)果推斷。(一個容器化部署的模型)

invoke_endpoint()所有參數(shù)如下:

response = client.invoke_endpoint(EndpointName='string', #終端節(jié)點名字Body=b'bytes'|file, #推斷的數(shù)據(jù)ContentType='string', #數(shù)據(jù)的格式Accept='string', #推斷結(jié)果的格式CustomAttributes='string',TargetModel='string',TargetVariant='string',InferenceId='string' )

一個小示例:

假設(shè)你有一個終端節(jié)點叫myEndPoint,用來做貓狗分類的推斷。

ENDPOINT_NAME = 'myEndPoint' with open('cat.jpg', 'wb') as data:response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,ContentType='image/jpeg',Body=data)#輸出結(jié)果print(response) #response的結(jié)構(gòu) #{ # 'Body': StreamingBody(), 結(jié)果 # 'ContentType': 'string', 結(jié)果格式 # 'InvokedProductionVariant': 'string', # 'CustomAttributes': 'string' #}

3.sagemaker庫

3.1EstimatorBase類

sagemaker有一個Estimator Base類,用于執(zhí)行端對端的sagemaker的訓(xùn)練和部署任務(wù)

class sagemaker.estimator.Estimator

其構(gòu)造參數(shù):

sagemaker.estimator.EstimatorBase(role, instance_count=None, instance_type=None, volume_size=30, volume_kms_key=None, max_run=86400, input_mode='File', output_path=None, output_kms_key=None, base_job_name=None, sagemaker_session=None, tags=None, subnets=None, security_group_ids=None, model_uri=None, model_channel_name='model', metric_definitions=None, encrypt_inter_container_traffic=False, use_spot_instances=False, max_wait=None, checkpoint_s3_uri=None, checkpoint_local_path=None, rules=None, debugger_hook_config=None, tensorboard_output_config=None, enable_sagemaker_metrics=None, enable_network_isolation=False, profiler_config=None, disable_profiler=False, **kwargs)
3.1.1 具有以下方法:

獲取訓(xùn)練鏡像

#返回為訓(xùn)練用的一個docker鏡像路徑 training_image_uri() #如: training_image_uri(sess.boto_region_name, 'object-detection', repo_version="latest")

設(shè)置超參

#設(shè)置算法的參數(shù)(這根據(jù)你用的是哪個算法) hyperparameters()

判斷Estimator是否需要網(wǎng)絡(luò)隔離

#判斷Estimator是否需要網(wǎng)絡(luò)隔離,返回bool類型 enable_network_isolation()

指定訓(xùn)練名

#指定訓(xùn)練名 prepare_workflow_for_training(job_name=None)

執(zhí)行訓(xùn)練

#執(zhí)行訓(xùn)練任務(wù), #inputs:訓(xùn)練通道 logs:日志 job_name:訓(xùn)練任務(wù)名 fit(inputs=None, wait=True, logs='All', job_name=None, experiment_config=None)

Neo編譯

#使用Neo編譯一個模型 #target_instance_family:標識編譯后要運行模型的設(shè)備 #input_shape:通過dict的形象指定輸入通道, #如:{‘data’:[1,3,1024,1024]}, or {‘var1’: [1,1,28,28], ‘var2’:[1,1,28,28]} #output_path:編譯后的模型存儲路徑 #framework:框架 #framework_version:版本 #compile_max_run:設(shè)置編譯超時秒數(shù) #target_platform_os:捆綁的操作系統(tǒng),如:linux #target_platform_arch:系統(tǒng)位數(shù),如:X86_64 #target_platform_accelerator:硬件加速器平臺,如NVIDIA compile_model(target_instance_family, input_shape, output_path, framework=None, framework_version=None, compile_max_run=900, tags=None, target_platform_os=None, target_platform_arch=None, target_platform_accelerator=None, compiler_options=None, **kwargs)

附加訓(xùn)練任務(wù)

#附加一個訓(xùn)練任務(wù) #作用:綁定某個訓(xùn)練任務(wù),可以獲取對應(yīng)的任務(wù)的配置,如果綁定的訓(xùn)練任務(wù)還在訓(xùn)練,那么綁定操作將被阻塞。 # 返回結(jié)果是:Estimator實例,通過該實例可以實現(xiàn)deploy()等操作 #training_job_name:綁定的訓(xùn)練任務(wù)名 #sagemaker_session: sagemaker的session #model_channel_name:模型路徑 classmethod attach(training_job_name, sagemaker_session=None, model_channel_name='model')

打印log

#打印log logs()

部署終端節(jié)點

#部署終端節(jié)點 #只展示部分參數(shù),具體看官方文檔 #initial_instance_count:部署實例個數(shù) #instance_type:實例類型 #返回:sagemaker.predictor.Predictor對象 #在sagemaker2.0中 RealTimePredictor已經(jīng)被更名為Predictor deploy(initial_instance_count, instance_type) deploy(initial_instance_count, instance_type,endpoint_name=None)

注冊模型包

#創(chuàng)建用于創(chuàng)建SageMaker模型或在市場上上市的模型包 #content_types:模型輸入MIME類型(模型輸入類型) #response_types:模型輸出MIME類型(模型輸出類型) #inference_instances:推斷實例允許的實例類型列表 #transform_instances:批轉(zhuǎn)換實例允許的實例類型 register(content_types, response_types, inference_instances, transform_instances)

創(chuàng)建模型對象

#創(chuàng)建一個模型 #返回的是:sagemaker.model.Model對象 create_model(**kwargs) #接受以下參數(shù): #image_uri:一個docker容器鏡像 #model_data:模型,一般是存放在s3中的模型(.tar.gz包) #role:角色(與IAM服務(wù)有關(guān)) #env:環(huán)境變量,dict類型,與image_uri指定的容器中有關(guān)變量 #name:模型名稱 #vpc_config:vpc配置,部署時會用到 #enable_network_isolation:網(wǎng)絡(luò)隔離,一般為false #model_kms_key:kms的密鑰(與kms服務(wù)有關(guān))
3.1.2 使用小示例

本示例使用的是官方的示例,由于這個示例 有點久遠,有些方法調(diào)用的還是sagemaker sdk v1版本的API,所有使用的時候可能會有提示警告。本人做了些許修改,完整代碼請查閱:https://github.com/aws/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/object_detection_incremental_training.ipynb

第一步:獲取執(zhí)行role

#本示例在jupyter notebook下運行 import sagemaker from sagemaker import get_execution_role#第一步獲取執(zhí)行權(quán)限 role = get_execution_role()

第二部:獲取數(shù)據(jù)并進行處理

#從官方指定的地址下載2007年和2012年P(guān)ascal VOC 數(shù)據(jù)集 # Download the dataset !wget -P /tmp http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar !wget -P /tmp http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar !wget -P /tmp http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar # # Extract the data. !tar -xf /tmp/VOCtrainval_11-May-2012.tar && rm /tmp/VOCtrainval_11-May-2012.tar !tar -xf /tmp/VOCtrainval_06-Nov-2007.tar && rm /tmp/VOCtrainval_06-Nov-2007.tar !tar -xf /tmp/VOCtest_06-Nov-2007.tar && rm /tmp/VOCtest_06-Nov-2007.tar#如果你打算使用自己放在s3中的數(shù)據(jù)可以運行以下代碼 #!aws s3 cp s3://my-bucket/data-path ./localhost-path#運行官方提供的腳本文件 #這幾行腳本代碼是用于生成rec序列文件 #該文件在上面提供的網(wǎng)址的github目錄下 !python tools/prepare_dataset.py --dataset pascal --year 2007,2012 --set trainval --target VOCdevkit/train.lst !python tools/prepare_dataset.py --dataset pascal --year 2007 --set test --target VOCdevkit/val.lst --no-shuffle#關(guān)于使用自己只定義的數(shù)據(jù) #在使用官方提供的腳本時應(yīng)該注意:如果你的數(shù)據(jù)集是按照Pascal VOC2007和Pascal VOC2012數(shù)據(jù)集 #構(gòu)建的目錄結(jié)構(gòu),請到腳本目錄下的pascal_voc.names,更改你數(shù)據(jù)集中對應(yīng)的便簽名,官方提供的腳 #本將會讀取該文件的label,用作生成.rec .lst等文件。更多的Pascal VOC目錄結(jié)構(gòu)請根據(jù)需要自行 #查找。

第三步:上傳處理的數(shù)據(jù)到S3存儲桶中

#上傳到S3有很多方式,請參考上面boto3對s3的控制 import boto3 s3 = boto3.resource('s3')#訓(xùn)練集的上傳 train_localhost_path = '' train_S3_path = '' s3.Bucket('mybucket').upload_file(train_localhost_path, train_S3_path)#驗證集的上傳 val_localhost_path = '' val_S3_path = '' s3.Bucket('mybucket').upload_file(val_localhost_path, val_S3_path)

第四步:獲取內(nèi)置算法鏡像

#舊的API #from sagemaker.amazon.amazon_estimator import get_image_uri #training_image = get_image_uri(sess.boto_region_name, 'object-detection', repo_version="latest")#新的API sess = sagemaker.Session() training_image = sagemaker.image_uris.retrieve("object-detection", sess.boto_region_name)

第五步:創(chuàng)建Estimator實例

s3_output_location = 's3的輸出路徑' model = sagemaker.estimator.Estimator(training_image,role, instance_count=1, instance_type='ml.p3.2xlarge',volume_size = 50,max_run = 360000,input_mode= 'File',output_path=s3_output_location,sagemaker_session=sess)

第六步:設(shè)置超參數(shù)

關(guān)于sagemaker內(nèi)置算法:目標檢測算法,其是用那種方式實現(xiàn)的。sagemaker中的目標檢測算法是使用SSD (Single Shot multibox Detector)框架實現(xiàn)的。并支持兩個基本網(wǎng)絡(luò): VGG 和 ResNet。 可以從頭開始訓(xùn)練網(wǎng)絡(luò),也可以使用已在 ImageNet 數(shù)據(jù)集上預(yù)先訓(xùn)練的模型進行訓(xùn)練。

#不同的內(nèi)置算法會有不同的超參數(shù) #目標檢測的超參數(shù)可以在以下網(wǎng)址中找到: #https://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/object-detection.html model.set_hyperparameters(base_network='resnet-50',use_pretrained_model=1,num_classes=20,mini_batch_size=32,epochs=1,learning_rate=0.001,lr_scheduler_step='3,6',lr_scheduler_factor=0.1,optimizer='sgd',momentum=0.9,weight_decay=0.0005,overlap_threshold=0.5,nms_threshold=0.45,image_shape=300,label_width=350,num_training_samples=16551)

第七步:訓(xùn)練

#舊的API #train_data = sagemaker.session.s3_input(s3_train_data, distribution='FullyReplicated', # content_type='application/x-recordio', s3_data_type='S3Prefix') #validation_data = sagemaker.session.s3_input(s3_validation_data, distribution='FullyReplicated', # content_type='application/x-recordio', s3_data_type='S3Prefix') #data_channels = {'train': train_data, 'validation': validation_data}#新的API如下: from sagemaker.inputs import TrainingInput train_data = '訓(xùn)練集S3路徑' validation_data = '驗證集的S3路徑' train_channel = TrainingInput(train_data, content_type='application/x-recordio') valid_channel = TrainingInput(validation_data, content_type='application/x-recordio') data_channels = {'train': train_channel, 'validation': valid_channel} #開啟訓(xùn)練 model.fit(inputs=data_channels, logs=True)

第八步:部署

object_detector = model.deploy(initial_instance_count = 1,instance_type = 'ml.m4.xlarge')

第九步:驗證模型

驗證模型之前,請下載一張用于驗證的圖片。

file_name = 'test.jpg' with open(file_name, 'rb') as image:f = image.read()b = bytearray(f)ne = open('n.txt','wb')ne.write(b)import json #預(yù)測 object_detector.content_type = 'image/jpeg' results = object_detector.predict(b) detections = json.loads(results) print (detections) #結(jié)果格式 #https://docs.aws.amazon.com/zh_cn/sagemaker/latest/dg/object-detection-in-formats.html#可視化函數(shù) def visualize_detection(img_file, dets, classes=[], thresh=0.6):"""visualize detections in one imageParameters:----------img : numpy.arrayimage, in bgr formatdets : numpy.arrayssd detections, numpy.array([[id, score, x1, y1, x2, y2]...])each row is one objectclasses : tuple or list of strclass namesthresh : floatscore threshold"""import randomimport matplotlib.pyplot as pltimport matplotlib.image as mpimgimg=mpimg.imread(img_file)plt.imshow(img)height = img.shape[0]width = img.shape[1]colors = dict()for det in dets:(klass, score, x0, y0, x1, y1) = detif score < thresh:continuecls_id = int(klass)if cls_id not in colors:colors[cls_id] = (random.random(), random.random(), random.random())xmin = int(x0 * width)ymin = int(y0 * height)xmax = int(x1 * width)ymax = int(y1 * height)rect = plt.Rectangle((xmin, ymin), xmax - xmin,ymax - ymin, fill=False,edgecolor=colors[cls_id],linewidth=3.5)plt.gca().add_patch(rect)class_name = str(cls_id)if classes and len(classes) > cls_id:class_name = classes[cls_id]plt.gca().text(xmin, ymin - 2,'{:s} {:.3f}'.format(class_name, score),bbox=dict(facecolor=colors[cls_id], alpha=0.5),fontsize=12, color='white')plt.show()object_categories = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']# Setting a threshold 0.20 will only plot detection results that have a confidence score greater than 0.20. threshold = 0.20# Visualize the detections. visualize_detection(file_name, detections['prediction'], object_categories, threshold)

最后一步:關(guān)閉終端節(jié)點

sagemaker.Session().delete_endpoint(object_detector.endpoint)

總結(jié)

以上是生活随笔為你收集整理的Sagemaker快速学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。