Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pipeline.py 6.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  1. # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  2. # SPDX-License-Identifier: MIT-0
  3. import os
  4. import numpy as np
  5. import boto3
  6. import time
  7. import sagemaker
  8. import sagemaker.session
  9. from sagemaker.workflow.parameters import ParameterInteger, ParameterString
  10. from sagemaker.sklearn.processing import SKLearnProcessor
  11. from sagemaker.processing import ProcessingInput, ProcessingOutput
  12. from sagemaker.workflow.steps import ProcessingStep, TrainingStep, CacheConfig
  13. from sagemaker.workflow.properties import PropertyFile
  14. from sagemaker.inputs import TrainingInput
  15. from sagemaker.workflow.step_collections import RegisterModel
  16. from sagemaker.workflow.pipeline import Pipeline
  17. BASE_DIR = os.path.dirname(os.path.realpath(__file__))
  18. def get_session(region, default_bucket):
  19. """Gets the sagemaker session based on the region.
  20. Args:
  21. region: the aws region to start the session
  22. default_bucket: the bucket to use for storing the artifacts
  23. Returns:
  24. `sagemaker.session.Session instance
  25. """
  26. boto_session = boto3.Session(region_name=region)
  27. sagemaker_client = boto_session.client("sagemaker")
  28. runtime_client = boto_session.client("sagemaker-runtime")
  29. return sagemaker.session.Session(
  30. boto_session=boto_session,
  31. sagemaker_client=sagemaker_client,
  32. sagemaker_runtime_client=runtime_client,
  33. default_bucket=default_bucket,
  34. )
  35. def get_pipeline(
  36. region,
  37. dagshub_token,
  38. role=None,
  39. default_bucket="kvasir-segmentation",
  40. pipeline_name="Testing-Sagemaker",
  41. base_job_prefix="kvasir",
  42. ):
  43. """Gets a SageMaker ML Pipeline instance working with on DefectDetection data.
  44. Args:
  45. region: AWS region to create and run the pipeline.
  46. role: IAM role to create and run steps and pipeline.
  47. default_bucket: the bucket to use for storing the artifacts
  48. Returns:
  49. an instance of a pipeline
  50. """
  51. sagemaker_session = get_session(region, default_bucket)
  52. if role is None:
  53. role = sagemaker.session.get_execution_role(sagemaker_session)
  54. ## By enabling cache, if you run this pipeline again, without changing the input
  55. ## parameters it will skip the training part and reuse the previous trained model
  56. cache_config = CacheConfig(enable_caching=True, expire_after="30d")
  57. ts = time.strftime('%Y-%m-%d-%H-%M-%S')
  58. dagshub_user = ParameterString(
  59. name="DagsHubUserName",
  60. default_value="Nikitha-Narendra"
  61. )
  62. dagshub_repo = ParameterString(
  63. name="DagsHubRepo",
  64. default_value="Kvasir-Image-Segmentation-Training"
  65. )
  66. experiment_name = ParameterString(
  67. name="ExperimentName",
  68. default_value="kvasir-segmentation"
  69. )
  70. registered_model_name = ParameterString(
  71. name='RegisteredModelName',
  72. default_value='kvasir-segmentation'
  73. )
  74. # Data prep
  75. processing_instance_type = ParameterString( # instance type for data preparation
  76. name="ProcessingInstanceType",
  77. default_value="ml.m5.xlarge"
  78. )
  79. processing_instance_count = ParameterInteger( # number of instances used for data preparation
  80. name="ProcessingInstanceCount",
  81. default_value=1
  82. )
  83. # Training
  84. training_instance_type = ParameterString( # instance type for training the model
  85. name="TrainingInstanceType",
  86. default_value="ml.g4dn.xlarge"
  87. )
  88. training_instance_count = ParameterInteger( # number of instances used to train your model
  89. name="TrainingInstanceCount",
  90. default_value=1
  91. )
  92. training_epochs = ParameterString(
  93. name="TrainingEpochs",
  94. default_value="1"
  95. )
  96. # Dataset input data: S3 path
  97. input_data = ParameterString(
  98. name="InputData",
  99. default_value="s3://kvasir-segmentation/kvasir-segmentation/",
  100. )
  101. # Model Approval State
  102. model_approval_status = ParameterString(
  103. name="ModelApprovalStatus",
  104. default_value="PendingManualApproval"
  105. )
  106. # Model package group name for registering in model registry
  107. model_package_group_name = ParameterString(
  108. name="ModelPackageGroupName",
  109. default_value="Kvasir-image-segmentation-model-group"
  110. )
  111. # The preprocessor
  112. preprocessor = SKLearnProcessor(
  113. framework_version="0.23-1",
  114. role=role,
  115. instance_type=processing_instance_type,
  116. instance_count=processing_instance_count,
  117. max_runtime_in_seconds=1200,
  118. )
  119. # Preprocessing Step
  120. step_process = ProcessingStep(
  121. name="KvasirSegmentationPreprocessing",
  122. code=os.path.join(BASE_DIR, 'preprocessing.py'),
  123. processor=preprocessor,
  124. inputs=[
  125. ProcessingInput(source=input_data, destination='/opt/ml/processing/input')
  126. ],
  127. outputs=[
  128. ProcessingOutput(output_name='train_data', source='/opt/ml/processing/train')
  129. ],
  130. )
  131. from sagemaker.tensorflow.estimator import TensorFlow
  132. model_dir = '/opt/ml/model'
  133. hyperparameters = {'epochs': training_epochs,
  134. 'batch_size': 64,
  135. 'experiment_name': experiment_name,
  136. 'dagshub_token':dagshub_token,
  137. 'dagshub_user':dagshub_user,
  138. 'dagshub_repo':dagshub_repo
  139. }
  140. estimator = TensorFlow(source_dir=BASE_DIR,
  141. entry_point='train_tf.py',
  142. model_dir=model_dir,
  143. instance_type=training_instance_type,
  144. instance_count=training_instance_count,
  145. hyperparameters=hyperparameters,
  146. role=role,
  147. output_path='s3://{}/{}/{}/{}'.format(default_bucket, 'models',
  148. base_job_prefix, 'training-output'),
  149. framework_version='2.12',
  150. py_version='py310',
  151. script_mode=True
  152. )
  153. step_train = TrainingStep(
  154. name="KvasirImageSegmentationTrain",
  155. estimator=estimator,
  156. inputs={
  157. "train": TrainingInput(
  158. s3_data=step_process.properties.ProcessingOutputConfig.Outputs["train_data"].S3Output.S3Uri,
  159. content_type='image/jpg',
  160. s3_data_type='S3Prefix'
  161. )
  162. },
  163. cache_config=cache_config
  164. )
  165. pipeline = Pipeline(
  166. name=pipeline_name,
  167. parameters=[
  168. dagshub_user,
  169. dagshub_repo,
  170. experiment_name,
  171. processing_instance_type,
  172. processing_instance_count,
  173. training_instance_type,
  174. training_instance_count,
  175. training_epochs,
  176. input_data,
  177. model_approval_status,
  178. model_package_group_name
  179. ],
  180. steps=[step_process, step_train],
  181. sagemaker_session=sagemaker_session,
  182. )
  183. return pipeline
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...