Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# In[8]:
# Predict using the deployed model
result = service.run(service_input)
# In[9]:
# Plot the results
actual_labels = ["milk_bottle", "water_bottle"]
for k in range(len(result)):
title = f"{actual_labels[k]}/{result[k]['label']} - {round(100.*float(result[k]['probability']), 2)}%"
open_image(local_im_paths[k]).show(title=title)
# ### 3.B Via a raw HTTP request <a id="request">
#
# In the case of AKS, we need to provide an authentication key. So let's look at the 2 examples separately, with the same testing data as before.
# In[10]:
# ---------
# On ACI
# ---------
# Extract service URL
service_uri = aci_service.scoring_uri
print(f"POST requests to url: {service_uri}")</a>
def api_iterate_predict(payload):
instances = json.loads(payload)['instances']
img_bytes = [b64decode(inst['image_bytes']['b64']) for inst in instances]
# batch predict, dummy labels for the second argument
predictions = []
for byts in img_bytes:
fimg = open_image(BytesIO(byts))
predictions.append(learner.predict(fimg)[1])
return predictions
def predict_image_from_string(s):
b = base64.b64decode(s)
img = open_image(BytesIO(b))
pred_class, pred_idx, outputs = learn.predict(img)
return JSONResponse({
"predictions": pred_class.replace('+', ' ').strip()
})
def predict(data):
if data is None or data.file is None:
return ''
img = open_image(data.file)
pclass, idx, outputs = learn.predict(img)
outputs = torch.nn.functional.softmax(np.log(outputs), dim=0).tolist()
logging.debug(f"Predicted class {pclass}. Predictions: {sorted(zip(classes, outputs), key=lambda o: o[1], reverse=True)}")
return {"predictions": {
pclass: outputs[idx]
}}
def classify_frame(_):
""" Classify an image snapshot by using a pretrained model
"""
# Once capturing started, remove the capture widget since we don't need it anymore
if w_imrecorder.layout.display != 'none':
w_imrecorder.layout.display = 'none'
try:
im = open_image(io.BytesIO(w_imrecorder.image.value), convert_mode='RGB')
_, ind, prob = learn.predict(im)
# Show result label and confidence
w_label.value = f"{labels[ind]} ({prob[ind]:.2f})"
except OSError:
# If im_recorder doesn't have valid image data, skip it.
pass
# Taking the next snapshot programmatically
w_imrecorder.recording = True
def compute_feature(
im_or_path: Image, learn: Learner, embedding_layer: Module
) -> List[float]:
"""Compute features for a single image
Args:
im_or_path: Image or path to image
learn: Trained model to use as featurizer
embedding_layer: Number of columns on which to display the images
Returns: DNN feature of the provided image.
"""
if isinstance(im_or_path, str):
im = open_image(im_or_path, convert_mode="RGB")
else:
im = im_or_path
featurizer = SaveFeatures(embedding_layer)
featurizer.features = None
learn.predict(im)
feats = featurizer.features[0][:]
assert len(feats) > 1
featurizer.features = None
return feats
def heatmap_from_string(s):
b = base64.b64decode(s)
img = open_image(BytesIO(b))
pred_class, pred_idx, outputs = learn.predict(img)
img = img.px.reshape(1, 3, 224, 224)
upsampled = run_gradcam(img)
gbp_map = run_gbp(img)
figdata_png = upsampled_to_b64bytes(upsampled, gbp_map)
return JSONResponse({
"predictions": figdata_png.decode('ascii')
})
learn = model_to_learner(models.resnet18(pretrained=True), IMAGENET_IM_SIZE)
# ## 2. Classify Images
#
# ### 2.1 Image file
# First, we prepare a coffee mug image to show an example of how to score a single image by using the model.
# In[5]:
# Download an example image
IM_URL = "https://cvbp.blob.core.windows.net/public/images/cvbp_cup.jpg"
urllib.request.urlretrieve(IM_URL, os.path.join(data_path(), "example.jpg"))
im = open_image(os.path.join(data_path(), "example.jpg"), convert_mode='RGB')
im
# In[6]:
start_time = time.time()
# Use the model to predict the class label
_, ind, prob = learn.predict(im)
print(f"Predicted label: {labels[ind]} (conf = {prob[ind]:.2f})")
# Show prediction time. Note the first prediction usually takes longer because of the model loading
print(f"Took {time.time()-start_time} sec")