在运行 aws lambda 函数并通过 lambda 中运行的容器对 DL 模型进行推理时,当我从 s3 下载模型时,它正在运行,但是当我实例化 StarDist2D(conf) 对象时,它会给出此错误。不确定是否是由于 Stardist 或某些实例对象或其他原因造成的,因为错误前面有时间戳,正如您在问题标题中看到的那样。
lambda_handler 的代码
def download_model_from_s3(bucket_name, model_key, local_model_path):
try:
s3_client.download_file(bucket_name, model_key, local_model_path)
return True
except Exception as e:
print(f"Error downloading model file from S3: {e}")
return False
local_model_path = '/tmp/' + model_file_name
def download_model_from_s3(bucket_name, model_key, local_model_path):
try:
s3_client.download_file(bucket_name, model_key, local_model_path)
return True
except Exception as e:
print(f"Error downloading model file from S3: {e}")
return False
def lambda_handler(event, context):
# Check if the event is an S3 event
print("this is event",event)
if 'Records' in event and len(event['Records']) > 0 and 's3' in event['Records'][0]:
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
print("this is key",key)
image_name = key.split("/")[-1]
print("this is image_name",image_name)
# Check if the uploaded object is in the validationimages folder
if key.startswith('ValidationImages/'):
# Process the uploaded image here
print("line no 67")
np.random.seed(42)
image_path = '/tmp/'+image_name
s3_client.download_file(bucket_name, key, image_path)
def imreadReshape(key):
if ".tif" in image_name:
imageRead = imread(image_path)
if np.ndim(imageRead) == 2:
return imageRead
imageRead = np.array(imageRead)
imageRead = cv2.resize(imageRead,(768,768))
return imageRead[:,:,0]
else:
print("line no 80")
imageRead = cv2.imread(image_path)
print("line no 82")
if np.ndim(imageRead) == 2:
return imageRead
imageRead = cv2.resize(imageRead,(768,768))
return imageRead[:,:,0]
X_val = [image_name]
X_val = list(map(imreadReshape,X_val))
n_channel = 1 if X_val[0].ndim == 2 else X_val[0].shape[-1] #If no third dim. then number of channels = 1. Otherwise get the num channels from the last dim.
axis_norm = (0,1)
if n_channel > 1:
print("Normalizing image channels %s." % ('jointly' if axis_norm is None or 2 in axis_norm else 'independently'))
sys.stdout.flush()
X_val = [x/255 for x in X_val]
rng = np.random.RandomState(42)
print(Config2D.__doc__)
gputools_available()
n_rays = 32 #ok
use_gpu = True and gputools_available() #ok
grid = (2,2) # ok
conf = Config2D (
n_rays = n_rays,
grid = grid,
use_gpu = use_gpu,
n_channel_in = n_channel,
train_patch_size = (768,768)
)
if download_model_from_s3(bucket_name, model_key, local_model_path):
## Load the model
new_model = tf.keras.models.load_model(local_model_path)
print("Load Model Complete")
model_load = StarDist2D(conf)
我解决了。实际上我们需要将代码开头的lambda环境的默认目录更改为/tmp/,以便将所有内容写入tmp。因此,只需添加一条语句即可解决问题。
os.chdir("/tmp/")