Creating a serving graph separately from training in tensorflow for Google CloudML deployment? - json

I am trying to deploy a tf.keras image classification model to Google CloudML Engine. Do I have to include code to create serving graph separately from training to get it to serve my models in a web app? I already have my model in SavedModel format (saved_model.pb & variable files), so I'm not sure if I need to do this extra step to get it to work.
e.g. this is code directly from GCP Tensorflow Deploying models documentation
def json_serving_input_fn():
"""Build the serving inputs."""
inputs = {}
for feat in INPUT_COLUMNS:
inputs[feat.name] = tf.placeholder(shape=[None], dtype=feat.dtype)
return tf.estimator.export.ServingInputReceiver(inputs, inputs)

You are probably training your model with actual image files, while it is best to send images as encoded byte-string to a model hosted on CloudML. Therefore you'll need to specify a ServingInputReceiver function when exporting the model, as you mention. Some boilerplate code to do this for a Keras model:
# Convert keras model to TF estimator
tf_files_path = './tf'
estimator =\
tf.keras.estimator.model_to_estimator(keras_model=model,
model_dir=tf_files_path)
# Your serving input function will accept a string
# And decode it into an image
def serving_input_receiver_fn():
def prepare_image(image_str_tensor):
image = tf.image.decode_png(image_str_tensor,
channels=3)
return image # apply additional processing if necessary
# Ensure model is batchable
# https://stackoverflow.com/questions/52303403/
input_ph = tf.placeholder(tf.string, shape=[None])
images_tensor = tf.map_fn(
prepare_image, input_ph, back_prop=False, dtype=tf.float32)
return tf.estimator.export.ServingInputReceiver(
{model.input_names[0]: images_tensor},
{'image_bytes': input_ph})
# Export the estimator - deploy it to CloudML afterwards
export_path = './export'
estimator.export_savedmodel(
export_path,
serving_input_receiver_fn=serving_input_receiver_fn)
You can refer to this very helpful answer for a more complete reference and other options for exporting your model.
Edit: If this approach throws a ValueError: Couldn't find trained model at ./tf. error, you can try it the workaround solution that I documented in this answer.

Related

How to retrain a pretrained model from PyTorchVideo?

I am using the pretrained PyTorchVideo model slowfast_r50_detection as shown here. I want to retrain this model with a different private dataset that I have and use it in a similar way as shown in the example. I am new to PyTorch and am not sure how to start retraining such a model. Any pointers would be very helpful.
You can simply load your model first, and than use load_state_dict() function to load the pre-trained model
path_to_saved_model = "Directory/directory/your_saved_model.tar"
video_model = slow_r50_detection(True)
video_model.load_state_dict(torch.load(path_to_saved_model)['model_state_dict'])
device = "cuda:0" if torch.cuda.is_available() else "cpu"
video_model = video_model.to(device)
The model loads the pre-trained weights from the saved model, and anything you run post the load_state_dict() line, the model uses previously trained weights.

Is there a method to serialize a machine learning model in within tidymodels (similar to pickling a model in Python)?

I am aware that the in Python you can serialize a ML model using the pickle module; however, is there a method to do something similar in the tidymodel space? My goal would be to be able to save a trained model to be deployed later.
In R, you can use saveRDS & readRDS to save/load any R object, just like Python's pickle. Those functions are not specific to Tidymodels, they are basic R functions that can be used to serialize any object.
Usage
saveRDS(any_r_object, "filename.rds")
object_name <- readRDS("filename.rds")
There is also the save() & load() functions, they serve the same function are are mostly similar to saveRDS() & readRDS(). There are many online discussions/blogs comparing the two approaches.
Following up after a good while:
Some model fits in R, e.g. those fitted with the "xgboost" or "keras" engine in tidymodels, require native serialization methods to be saved and reloaded in a new R session properly. saveRDS and readRDS will do the trick most of the time, though fall short for model objects from packages that require native serialization.
The folks from the tidymodels team put together a new package, bundle, to provide a consistent interface for native serialization of model objects. The bundle() verb prepares a model object for serialization, and then you can safely saveRDS() + readRDS() and pass between R sessions as you wish, and then unbundle() in the new session:
mod_bundle <- bundle(mod)
saveRDS(mod_bundle, file = "path/to/file.rds")
# in a new R session:
mod_bundle <- readRDS("path/to/file.rds")
mod_new <- unbundle(mod_bundle)

How can I save my session or my GAN model into a js file

I want to deploy my GANs model on a web-based UI for this I need to convert my model's checkpoints into js files to be called by web code. There are functions for saved_model and Keras to convert into pb files but none for js
my main concern is that I am confused about how to dump a session or variable weights in js files
You can save a keras model from python. There is a full tutorial here but basically it amounts to calling this after training:
tfjs.converters.save_keras_model(model, tfjs_target_dir)
then hosting the result somewhere publicly accessible (or on the same server as your web UI) then you can load your model into tensorflow.js as follows:
import * as tf from '#tensorflow/tfjs';
const model = await tf.loadLayersModel('https://foo.bar/tfjs_artifacts/model.json');

Clienterror: An error occured when calling the CreateModel operation

I want to deploy sklearn model in sagemaker. I created a training script.
scripPath=' sklearn.py'
sklearn=SKLearn(entry_point=scripPath,
train_instance_type='ml.m5.xlarge',
role=role, output_path='s3://{}/{}/output'.format(bucket,prefix), sagemaker_session=session)
sklearn.fit({"train-dir' : train_input})
When I deploy it
predictor=sklearn.deploy(initial_count=1,instance_type='ml.m5.xlarge')
It throws,
Clienterror: An error occured when calling the CreateModel operation:Could not find model data at s3://tree/sklearn/output/model.tar.gz
Can anyone say how to solve this issue?
When deploying models, SageMaker looks up S3 to find your trained model artifact. It seems that there is no trained model artifact at s3://tree/sklearn/output/model.tar.gz. Make sure to persist your model artifact in your training script at the appropriate local location in docker which is /opt/ml/model.
for example, in your training script this could look like:
joblib.dump(model, /opt/ml/model/mymodel.joblib)
After training, SageMaker will copy the content of /opt/ml/model to s3 at the output_path location.
If you deploy in the same session a model.deploy() will map automatically to the artifact path. If you want to deploy a model that you trained elsewhere, possibly during a different session or in a different hardware, you need to explicitly instantiate a model before deploying
from sagemaker.sklearn.model import SKLearnModel
model = SKLearnModel(
model_data='s3://...model.tar.gz', # your artifact
role=get_execution_role(),
entry_point='script.py') # script containing inference functions
model.deploy(
instance_type='ml.m5.xlarge',
initial_instance_count=1,
endpoint_name='your_endpoint_name')
See more about Sklearn in SageMaker here https://sagemaker.readthedocs.io/en/stable/using_sklearn.html

"Initializing geometry from JSON input requires GDAL" Error

I'm doing a Django project and I want to save polygons that represent areas of interest in a map. I am trying to use django-leaflet and django-geojson. The model for the shapes is:
#models.py
...
from django.contrib.gis.db import models as gismodels
...
class MushroomShape(gismodels.Model):
name = models.CharField(max_length=256)
geom = gismodels.PolygonField()
objects = gismodels.GeoManager()
def __unicode__(self):
return self.name
def __str__(self):
return self.name
I'm trying to create the polygon shapes in the admin, using a leaflet widget, to be added to the Database:
#admin.py
...
from leaflet.admin import LeafletGeoAdmin
from .models import MushroomShape
...
admin.site.register(MushroomShape, LeafletGeoAdmin)
Running the server in my computer, when I draw a polygon in the admin form and try to submit it:
The client side reports "Invalid geometry value." and the server side reports:
Error creating geometry from value
'{"type":"Polygon","coordinates":[[[-87.58575439453125,41.83375828633243],[-87.58575439453125,42.002366213375524],[-86.74942016601562,42.002366213375524],[-86.74942016601562,41.83375828633243],[-87.58575439453125,41.83375828633243]]]}'
(Initializing geometry from JSON input requires GDAL.)
A little push to help understand where I have to look, to solve this error, would be really awesome.
Sorry if this is bad etiquette (posting an answer to my question instead of deleting), but I've found my answer in the official Django page for geo libraries:
https://docs.djangoproject.com/el/1.10/ref/contrib/gis/install/geolibs/
I didn't know GDAL is necessary for some geojson features that I tried to use to work. I've followed their instructions and installed it with
sudo apt-get install binutils libproj-dev gdal-bin
and my error is gone.