forked from GoogleCloudPlatform/cloudml-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredict.py
188 lines (157 loc) · 5.91 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
#
# Copyright 2018 Google Inc. All Rights Reserved. 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.
# This tool does either batch or streaming predictions on a trained model.
from __future__ import print_function
import argparse
import json
import os
import sys
import tempfile
import pubchem
import apache_beam as beam
import tensorflow as tf
from apache_beam.options.pipeline_options import GoogleCloudOptions
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.options.pipeline_options import SetupOptions
from apache_beam.options.pipeline_options import StandardOptions
from tensorflow.python.framework import ops
from tensorflow.python.saved_model import loader
class Predict(beam.DoFn):
def __init__(self,
model_dir,
id_key,
meta_tag='serve',
meta_signature='predict',
meta_predictions='predictions'):
super(Predict, self).__init__()
self.model_dir = model_dir
self.id_key = id_key
self.meta_tag = meta_tag
self.meta_signature = meta_signature
self.meta_predictions = meta_predictions
self.session = None
self.graph = None
self.feed_tensors = None
self.fetch_tensors = None
def process(self, inputs):
# Create a session for every worker only once. The session is not
# pickleable, so it can't be created at the DoFn constructor.
if not self.session:
self.graph = ops.Graph()
with self.graph.as_default():
self.session = tf.Session()
metagraph_def = loader.load(
self.session, {self.meta_tag}, self.model_dir)
signature_def = metagraph_def.signature_def[self.meta_signature]
# inputs
self.feed_tensors = {
k: self.graph.get_tensor_by_name(v.name)
for k, v in signature_def.inputs.items()
}
# outputs/predictions
self.fetch_tensors = {
k: self.graph.get_tensor_by_name(v.name)
for k, v in signature_def.outputs.items()
}
# Create a feed_dict for a single element.
feed_dict = {
tensor: [inputs[key]]
for key, tensor in self.feed_tensors.items()
if key in inputs
}
results = self.session.run(self.fetch_tensors, feed_dict)
yield {
'id': inputs[self.id_key],
'predictions': results[self.meta_predictions][0].tolist()
}
# [START dataflow_molecules_run_definition]
def run(model_dir, feature_extraction, sink, beam_options=None):
with beam.Pipeline(options=beam_options) as p:
_ = (p
| 'Feature extraction' >> feature_extraction
| 'Predict' >> beam.ParDo(Predict(model_dir, 'ID'))
| 'Format as JSON' >> beam.Map(json.dumps)
| 'Write predictions' >> sink)
# [END dataflow_molecules_run_definition]
if __name__ == '__main__':
"""Main function"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--work-dir',
type=str,
default=os.path.join(
tempfile.gettempdir(), 'cloudml-samples', 'molecules'),
help='Directory for temporary files and preprocessed datasets to. '
'This can be a Google Cloud Storage path.')
parser.add_argument(
'--model-dir',
type=str,
required=True,
help='Path to the exported TensorFlow model. '
'This can be a Google Cloud Storage path.')
verbs = parser.add_subparsers(dest='verb')
batch_verb = verbs.add_parser('batch', help='Batch prediction')
batch_verb.add_argument(
'--inputs-dir',
type=str,
required=True,
help='Input directory where SDF data files are read from. '
'This can be a Google Cloud Storage path.')
batch_verb.add_argument(
'--outputs-dir',
type=str,
required=True,
help='Directory to store prediction results. '
'This can be a Google Cloud Storage path.')
stream_verb = verbs.add_parser('stream', help='Streaming prediction')
stream_verb.add_argument(
'--inputs-topic',
type=str,
default='molecules-inputs',
help='PubSub topic to subscribe for molecules.')
stream_verb.add_argument(
'--outputs-topic',
type=str,
default='molecules-predictions',
help='PubSub topic to publish predictions.')
args, pipeline_args = parser.parse_known_args()
beam_options = PipelineOptions(pipeline_args)
beam_options.view_as(SetupOptions).save_main_session = True
project = beam_options.view_as(GoogleCloudOptions).project
# [START dataflow_molecules_batch_or_stream]
if args.verb == 'batch':
data_files_pattern = os.path.join(args.inputs_dir, '*.sdf')
results_prefix = os.path.join(args.outputs_dir, 'part')
source = beam.io.Read(pubchem.ParseSDF(data_files_pattern))
sink = beam.io.WriteToText(results_prefix)
elif args.verb == 'stream':
if not project:
parser.print_usage()
print('error: argument --project is required for streaming')
sys.exit(1)
beam_options.view_as(StandardOptions).streaming = True
source = beam.io.ReadFromPubSub(topic='projects/{}/topics/{}'.format(
project, args.inputs_topic))
sink = beam.io.WriteStringsToPubSub(topic='projects/{}/topics/{}'.format(
project, args.outputs_topic))
# [END dataflow_molecules_batch_or_stream]
else:
parser.print_usage()
sys.exit(1)
# [START dataflow_molecules_call_run]
run(
args.model_dir,
pubchem.SimpleFeatureExtraction(source),
sink,
beam_options)
# [END dataflow_molecules_call_run]