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

#9733: Extend RemoteMonitor to send data as application/json #9734

Merged
merged 4 commits into from
Mar 27, 2018
Merged
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
19 changes: 14 additions & 5 deletions keras/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,25 +547,31 @@ class RemoteMonitor(Callback):
Events are sent to `root + '/publish/epoch/end/'` by default. Calls are
HTTP POST, with a `data` argument which is a
JSON-encoded dictionary of event data.
If send_as_json is set to True, the content type of the request will be application/json.
Otherwise the serialized JSON will be send within a form

# Arguments
root: String; root url of the target server.
path: String; path relative to `root` to which the events will be sent.
field: String; JSON field under which the data will be stored.
field: String; JSON field under which the data will be stored. The field is used only if the payload is sent
within a form (i.e. send_as_json is set to False).
headers: Dictionary; optional custom HTTP headers.
send_as_json: Boolean; whether the request should be send as application/json.
"""

def __init__(self,
root='http://localhost:9000',
path='/publish/epoch/end/',
field='data',
headers=None):
headers=None,
send_as_json=False):
super(RemoteMonitor, self).__init__()

self.root = root
self.path = path
self.field = field
self.headers = headers
self.send_as_json = send_as_json

def on_epoch_end(self, epoch, logs=None):
if requests is None:
Expand All @@ -580,9 +586,12 @@ def on_epoch_end(self, epoch, logs=None):
else:
send[k] = v
try:
requests.post(self.root + self.path,
{self.field: json.dumps(send)},
headers=self.headers)
if self.send_as_json:
requests.post(self.root + self.path, json=send, headers=self.headers)
else:
requests.post(self.root + self.path,
{self.field: json.dumps(send)},
headers=self.headers)
except requests.exceptions.RequestException:
warnings.warn('Warning: could not reach RemoteMonitor '
'root server at ' + str(self.root))
Expand Down
22 changes: 22 additions & 0 deletions tests/keras/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,5 +806,27 @@ def tests_RemoteMonitor():
validation_data=(X_test, y_test), callbacks=cbks, epochs=1)


@keras_test
def tests_RemoteMonitorWithJsonPayload():
(X_train, y_train), (X_test, y_test) = get_test_data(num_train=train_samples,
num_test=test_samples,
input_shape=(input_dim,),
classification=True,
num_classes=num_classes)
y_test = np_utils.to_categorical(y_test)
y_train = np_utils.to_categorical(y_train)
model = Sequential()
model.add(Dense(num_hidden, input_dim=input_dim, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
cbks = [callbacks.RemoteMonitor(send_as_json=True)]

with patch('requests.post'):
model.fit(X_train, y_train, batch_size=batch_size,
validation_data=(X_test, y_test), callbacks=cbks, epochs=1)


if __name__ == '__main__':
pytest.main([__file__])