SageMaker Clarify with imported models - amazon-sagemaker-clarify

is it possible to use SageMaker Clarify bias and explainability analysis with imported models, like TensorFlow models?Given that the Tensor flow model has been trained from elsewhere?

Yes, it is possible. All you need to do is to bring your model to SageMaker in the required model.tar.gz format and host it on a RealTime/ Batch endpoint with Clarify.
Refer to the FAQs on bringing your model to SageMaker here

Related

ExampleGen on production

I was wondering how is ExampleGen used in production? I understand that their outputs can be feeded into the TFDV components of TFX to validate schema, skews, and others.
But I get lost since ExampleGen generates a train & eval split, and I don’t find why you would split the data in production into train & eval.
As far as I know, TFX is more suitable for deploying models into production, if I'm going to make a non-productive model maybe just using Tensorflow could work.
So ym questions are:
Is TFX are used for the modeling/dev part? i.e. before deploying your model.
Is it suitable to develop a model in Tensorflow and then migrate it to TFX for the production part?
Thanks!
The ExampleGen TFX Pipeline component ingests data into TFX pipelines.
In simple words, ExampleGen fetches the data from external data sources such as CSV, TFRecord, Avro, Parquet and BigQuery and generates tf.Example and tf.SequenceExample records which can be read by other TFX components. For more info, Please refer The ExampleGen TFX Pipeline Component
Is TFX are used for the modeling/dev part? i.e. before deploying your model.
Yes TFX can be used for modeling, training, serving inference, and managing deployments to online, native mobile, and JavaScript targets. Once you model is trained on TFX, you can deploy your model using TF serving and other deployment targets.
Is it suitable to develop a model in Tensorflow and then migrate it to TFX for the production part?
Once you have developed and trained a model using TFX pipeline, you can deploy it using TF serving system. You can also serve tensorflow models using TF serving. Please refer Serving a TensorFlow Model

How to extract weights of DQN agent in TF-Agents framework?

I am using TF-Agents for a custom reinforcement learning problem, where I train a DQN (constructed using DqnAgents from the TF-Agents framework) on some features from my custom environment, and separately use a keras convolutional model to extract these features from images. Now I want to combine these two models into a single model and use transfer learning, where I want to initialize the weights of the first part of the network (images-to-features) as well as the second part which would have been the DQN layers in the previous case.
I am trying to build this combined model using keras.layers and compiling it with the Tf-Agents tf.networks.sequential class to bring it to the necessary form required when passing it to the DqnAgent() class. (Let's call this statement (a)).
I am able to initialize the image feature extractor network's layers with the weights since I saved it as a .h5 file and am able to obtain numpy arrays of the same. So I am able to do the transfer learning for this part.
The problem is with the DQN layers, where I saved the policy from the previous example using the prescribed Tensorflow Saved Model Format (pb) which gives me a folder containing model attributes. However, I am unable to view/extract the weights of my DQN in this way, and the recommended tf.saved_model.load('policy_directory') is not really transparent with respect to what data I can see regarding the policy. If I have to follow the transfer learning as I do in statement (a), I need to extract the weights of my DQN and assign them to the new network. The documentation seems to be quite sparse for this case where transfer learning needs to be applied.
Can anyone help me in this, by explaining how I can extract weights from the Saved Model method (from the pb file)? Or is there a better way to go about this problem?

Extracting representations from different layers of a network in TensorFlow 2

I have the weights of a custom pre-trained model. I need to extract the representations for different inputs that I pass through the model, across its different layers. What would be the best way of doing this?
I am using TensorFlow 2.1.0 and currently load in the weights of the model using either hub.KerasLayer() or tf.saved_model.load()
Any help would be greatly appreciated! I am very new to TensorFlow and have no choice but to use it since the weights were acquired from another source.
tf.saved_model.load() and its wrapper hub.KerasLayer load both the computation graph and the pre-trained weights. I suppose you're dealing with a TF2-style SavedModel that has its computation packaged in TensorFlow functions. If so, there's no easy way to extract intermediate results from within a function. If possible, you could ask the model creator to provide more outputs, or, if you have the model's Python source, build the model from source and initialize its weights with those from the SavedModel (some plumbing required).

How to train a Keras model on GCP with fit_generator

I have an ML model developed in Keras and I can train it locally by calling its fit_generator and providing it with my custom generator. Now I want to use GCP to train this model. I've been following this article that shows how I can train a Keras model on GCP but it does not say what should I do if I need to load all my data into memory, process it and then feed it to the model through a generator.
Does anyone know how I can use GCP if I have a generator?
In the example you are following, the Keras model gets converted into an estimator, using the function model_to_estimator; this step is not necessary in order to use GCP, as GCP supports compiled Keras models. If you keep the model as a Keras model, you can call either its function fit (which supports the use of generators since TensorFlow 1.12) or fit_generator and pass them your generator as the first argument. If it works locally for you, then it should also be able to work in GCP. I have been able to run models in GCP similar to the one in the url you shared and using generators without any problems.
Also be advised that the gcloud ml-engine commands are being replaced by gcloud ai-platform. I recommend you follow this guide, as it is more updated than the one you linked to.

How to save a tensorflow model trained in google datalab notebook for offline prediction?

I am using Google Cloud Datalab notebook to train my tensorflow model. I want to save the trained model for offline prediction. However, I am clueless on how to save the model. Should I use any tensorflow model saving method or is there any datalab/google cloud storage specific method to do so? Any help in this regard is highly appreciated.
You can use any tensorflow model saving method, but I would suggest that you save it into a Google Cloud Storage bucket and not to local disk. Most tensorflow methods accept Google Cloud Storage paths in place of file names, using the gs:// prefix.
I would suggest using the SavedModelBuilder as it is currently the most portable. There is an example here: https://github.com/GoogleCloudPlatform/cloudml-samples/blob/master/flowers/trainer/model.py#L393