Deploy YOLOv5 in sagemaker - ModelError: InvokeEndpoint operation: Received server error (0)
I'm trying to deploy custom trained Yolov5 model in Sagemaker for inference. (Note : The model was not trained in sagemaker).
Followed this doc for deploying the model and inference script - [Sagemaker docs](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#bring-your-own-model)
```
ModelError Traceback (most recent call last)
<ipython-input-7-063ca701eab7> in <module>
----> 1 result1=predictor.predict("FILE0032.JPG")
2 print(result1)
~/anaconda3/envs/python3/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant, inference_id)
159 data, initial_args, target_model, target_variant, inference_id
160 )
--> 161 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
162 return self._handle_response(response)
163
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
399 "%s() only accepts keyword arguments." % py_operation_name)
400 # The "self" in this scope is referring to the BaseClient.
--> 401 return self._make_api_call(operation_name, kwargs)
402
403 _api_call.__name__ = str(py_operation_name)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
729 error_code = parsed_response.get("Error", {}).get("Code")
730 error_class = self.exceptions.from_code(error_code)
--> 731 raise error_class(parsed_response, operation_name)
732 else:
733 return parsed_response
ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (0) from primary with message "Your invocation timed out while waiting for a response from container primary. Review the latency metrics for each container in Amazon CloudWatch, resolve the issue, and try again.". See https://ap-south-1.console.aws.amazon.com/cloudwatch/home?region=ap-south-1#logEventViewer:group=/aws/sagemaker/Endpoints/pytorch-inference-2022-06-14-11-58-04-086 in account 772044684908 for more information.
```
After researching about `InvokeEndpoint`, tried this
```
import boto3
sagemaker_runtime = boto3.client("sagemaker-runtime", region_name='ap-south-1')
endpoint_name='pytorch-inference-2022-06-14-11-58-04-086'
response = sagemaker_runtime.invoke_endpoint(
EndpointName=endpoint_name,
Body=bytes('{"features": ["This is great!"]}', 'utf-8') # Replace with your own data.
)
print(response['Body'].read().decode('utf-8'))
```
But this didn't help as well,
detailed output :
```
ReadTimeoutError Traceback (most recent call last)
<ipython-input-8-b5ca204734c4> in <module>
12 response = sagemaker_runtime.invoke_endpoint(
13 EndpointName=endpoint_name,
---> 14 Body=bytes('{"features": ["This is great!"]}', 'utf-8') # Replace with your own data.
15 )
16
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
399 "%s() only accepts keyword arguments." % py_operation_name)
400 # The "self" in this scope is referring to the BaseClient.
--> 401 return self._make_api_call(operation_name, kwargs)
402
403 _api_call.__name__ = str(py_operation_name)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
716 apply_request_checksum(request_dict)
717 http, parsed_response = self._make_request(
--> 718 operation_model, request_dict, request_context)
719
720 self.meta.events.emit(
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/client.py in _make_request(self, operation_model, request_dict, request_context)
735 def _make_request(self, operation_model, request_dict, request_context):
736 try:
--> 737 return self._endpoint.make_request(operation_model, request_dict)
738 except Exception as e:
739 self.meta.events.emit(
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in make_request(self, operation_model, request_dict)
105 logger.debug("Making request for %s with params: %s",
106 operation_model, request_dict)
--> 107 return self._send_request(request_dict, operation_model)
108
109 def create_request(self, params, operation_model=None):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _send_request(self, request_dict, operation_model)
182 request, operation_model, context)
183 while self._needs_retry(attempts, operation_model, request_dict,
--> 184 success_response, exception):
185 attempts += 1
186 self._update_retries_context(
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _needs_retry(self, attempts, operation_model, request_dict, response, caught_exception)
306 event_name, response=response, endpoint=self,
307 operation=operation_model, attempts=attempts,
--> 308 caught_exception=caught_exception, request_dict=request_dict)
309 handler_response = first_non_none_response(responses)
310 if handler_response is None:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in emit(self, event_name, **kwargs)
356 def emit(self, event_name, **kwargs):
357 aliased_event_name = self._alias_event_name(event_name)
--> 358 return self._emitter.emit(aliased_event_name, **kwargs)
359
360 def emit_until_response(self, event_name, **kwargs):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in emit(self, event_name, **kwargs)
227 handlers.
228 """
--> 229 return self._emit(event_name, kwargs)
230
231 def emit_until_response(self, event_name, **kwargs):
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/hooks.py in _emit(self, event_name, kwargs, stop_on_response)
210 for handler in handlers_to_call:
211 logger.debug('Event %s: calling handler %s', event_name, handler)
--> 212 response = handler(**kwargs)
213 responses.append((handler, response))
214 if stop_on_response and response is not None:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempts, response, caught_exception, **kwargs)
192 checker_kwargs.update({'retries_context': retries_context})
193
--> 194 if self._checker(**checker_kwargs):
195 result = self._action(attempts=attempts)
196 logger.debug("Retry needed, action of: %s", result)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception, retries_context)
266
267 should_retry = self._should_retry(attempt_number, response,
--> 268 caught_exception)
269 if should_retry:
270 if attempt_number >= self._max_attempts:
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in _should_retry(self, attempt_number, response, caught_exception)
292 # If we've exceeded the max attempts we just let the exception
293 # propogate if one has occurred.
--> 294 return self._checker(attempt_number, response, caught_exception)
295
296
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception)
332 for checker in self._checkers:
333 checker_response = checker(attempt_number, response,
--> 334 caught_exception)
335 if checker_response:
336 return checker_response
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in __call__(self, attempt_number, response, caught_exception)
232 elif caught_exception is not None:
233 return self._check_caught_exception(
--> 234 attempt_number, caught_exception)
235 else:
236 raise ValueError("Both response and caught_exception are None.")
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/retryhandler.py in _check_caught_exception(self, attempt_number, caught_exception)
374 # the MaxAttemptsDecorator is not interested in retrying the exception
375 # then this exception just propogates out past the retry code.
--> 376 raise caught_exception
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _do_get_response(self, request, operation_model, context)
247 http_response = first_non_none_response(responses)
248 if http_response is None:
--> 249 http_response = self._send(request)
250 except HTTPClientError as e:
251 return (None, e)
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/endpoint.py in _send(self, request)
319
320 def _send(self, request):
--> 321 return self.http_session.send(request)
322
323
~/anaconda3/envs/python3/lib/python3.6/site-packages/botocore/httpsession.py in send(self, request)
449 raise ConnectTimeoutError(endpoint_url=request.url, error=e)
450 except URLLib3ReadTimeoutError as e:
--> 451 raise ReadTimeoutError(endpoint_url=request.url, error=e)
452 except ProtocolError as e:
453 raise ConnectionClosedError(
ReadTimeoutError: Read timeout on endpoint URL: "https://runtime.sagemaker.ap-south-1.amazonaws.com/endpoints/pytorch-inference-2022-06-14-11-58-04-086/invocations"
```