Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add caffe2 training example #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cloud_ml_samples/caffe2/linear_regression/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2017 Xiaomi, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import setuptools
setuptools.setup(name='caffe2-trainer', version='1.0', packages=['trainer'])
Empty file.
87 changes: 87 additions & 0 deletions cloud_ml_samples/caffe2/linear_regression/trainer/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python

# Copyright 2017 Xiaomi, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from caffe2.python import core, cnn, workspace


def main():
init_net = core.Net("init")
# The ground truth parameters.
W_gt = init_net.GivenTensorFill(
[], "W_gt", shape=[1, 2], values=[2.0, 1.5])
B_gt = init_net.GivenTensorFill([], "B_gt", shape=[1], values=[0.5])
# Constant value ONE is used in weighted sum when updating parameters.
ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.)
# ITER is the iterator count.
ITER = init_net.ConstantFill([], "ITER", shape=[1], value=0, dtype=core.DataType.INT32)

# For the parameters to be learned: we randomly initialize weight
# from [-1, 1] and init bias with 0.0.
W = init_net.UniformFill([], "W", shape=[1, 2], min=-1., max=1.)
B = init_net.ConstantFill([], "B", shape=[1], value=0.0)
print('Created init net.')


train_net = core.Net("train")
# First, we generate random samples of X and create the ground truth.
X = train_net.GaussianFill([], "X", shape=[64, 2], mean=0.0, std=1.0, run_once=0)
Y_gt = X.FC([W_gt, B_gt], "Y_gt")
# We add Gaussian noise to the ground truth
noise = train_net.GaussianFill([], "noise", shape=[64, 1], mean=0.0, std=1.0, run_once=0)
Y_noise = Y_gt.Add(noise, "Y_noise")
# Note that we do not need to propagate the gradients back through Y_noise,
# so we mark StopGradient to notify the auto differentiating algorithm
# to ignore this path.
Y_noise = Y_noise.StopGradient([], "Y_noise")

# Now, for the normal linear regression prediction, this is all we need.
Y_pred = X.FC([W, B], "Y_pred")

# The loss function is computed by a squared L2 distance, and then averaged
# over all items in the minibatch.
dist = train_net.SquaredL2Distance([Y_noise, Y_pred], "dist")
loss = dist.AveragedLoss([], ["loss"])

# Get gradients for all the computations above.
gradient_map = train_net.AddGradientOperators([loss])

# Increment the iteration by one.
train_net.Iter(ITER, ITER)
# Compute the learning rate that corresponds to the iteration.
LR = train_net.LearningRate(ITER, "LR", base_lr=-0.1,
policy="step", stepsize=20, gamma=0.9)

# Weighted sum
train_net.WeightedSum([W, ONE, gradient_map[W], LR], W)
train_net.WeightedSum([B, ONE, gradient_map[B], LR], B)

workspace.RunNetOnce(init_net)
workspace.CreateNet(train_net)

print("Before training, W is: {}".format(workspace.FetchBlob("W")))
print("Before training, B is: {}".format(workspace.FetchBlob("B")))

for i in range(100):
workspace.RunNet(train_net.Proto().name)

print("After training, W is: {}".format(workspace.FetchBlob("W")))
print("After training, B is: {}".format(workspace.FetchBlob("B")))
print("Ground truth W is: {}".format(workspace.FetchBlob("W_gt")))
print("Ground truth B is: {}".format(workspace.FetchBlob("B_gt")))


if __name__ == "__main__":
main()
16 changes: 13 additions & 3 deletions cloud_ml_sdk/cloud_ml_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,30 @@ def update_model_service(self, model_name, model_version, update_json, org_id=No
else:
return response.content

def get_model_service_logs(self, model_name, model_version, org_id=None):
def get_model_service_logs(self, model_name, model_version, org_id=None, replica_index=None):
"""Get logs of the model service.

Args:
model_name: The name of the model service.
model_version: The version of the model service.
org_id: the client's orgid
replica_index: the replica's index,

Returns:
The logs of the model service.
"""
if org_id:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs" + "?org_id=" + org_id
if replica_index:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs" + "?org_id=" + org_id + \
"&replica=" + replica_index
else:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs" + "?org_id=" + org_id
else:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs"
if replica_index:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs" + "?replica=" + replica_index
else:
url = self._model_url + "/" + model_name + "/" + model_version + "/logs"

response = requests.get(url, auth=self._auth)
if response.ok:
return json.loads(response.content.decode("utf-8"))
Expand Down
6 changes: 6 additions & 0 deletions cloud_ml_sdk/cloud_ml_sdk/command/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ def main():
models_logs_parser.add_argument("model_name", help="The name of the model")
models_logs_parser.add_argument(
"model_version", help="The version of the model")
models_logs_parser.add_argument(
"-ri",
"--replica",
dest="replica_index",
help="The replica index"
)
models_logs_parser.set_defaults(func=util.get_model_logs)

# subcommand of models: metrics
Expand Down
17 changes: 12 additions & 5 deletions cloud_ml_sdk/cloud_ml_sdk/command/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,13 +776,20 @@ def get_model_logs(args):

client = CloudMlClient()
if "org_id" in args:
response = client.get_model_service_logs(args.model_name, args.model_version, args.org_id)
if args.replica_index:
response = client.get_model_service_logs(args.model_name, args.model_version, args.org_id, args.replica_index)
else:
response = client.get_model_service_logs(args.model_name, args.model_version, args.org_id)
else:
response = client.get_model_service_logs(args.model_name, args.model_version)
if args.replica_index:
response = client.get_model_service_logs(args.model_name, args.model_version, replica_index=args.replica_index)
else:
response = client.get_model_service_logs(args.model_name, args.model_version)
if not isinstance(response, str):
print(response["logs"])
else:
print("response: {}".format(response))
if "error" in response:
print(response['message'])
else:
print(response["logs"])


def get_model_metrics(args):
Expand Down