- Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsklearn_mnist_classification.py
138 lines (118 loc) · 4.91 KB
/
sklearn_mnist_classification.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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
"""A pipeline that uses RunInference API to classify MNIST data.
This pipeline takes a text file in which data is comma separated ints. The first
column would be the true label and the rest would be the pixel values. The data
is processed and then a model trained on the MNIST data would be used to perform
the inference. The pipeline writes the prediction to an output file in which
users can then compare against the true label.
"""
importargparse
importlogging
importos
fromcollections.abcimportIterable
importapache_beamasbeam
fromapache_beam.ml.inference.baseimportKeyedModelHandler
fromapache_beam.ml.inference.baseimportPredictionResult
fromapache_beam.ml.inference.baseimportRunInference
fromapache_beam.ml.inference.sklearn_inferenceimportModelFileType
fromapache_beam.ml.inference.sklearn_inferenceimportSklearnModelHandlerNumpy
fromapache_beam.options.pipeline_optionsimportPipelineOptions
fromapache_beam.options.pipeline_optionsimportSetupOptions
fromapache_beam.runners.runnerimportPipelineResult
defprocess_input(row: str) ->tuple[int, list[int]]:
data=row.split(',')
label, pixels=int(data[0]), data[1:]
pixels= [int(pixel) forpixelinpixels]
returnlabel, pixels
classPostProcessor(beam.DoFn):
"""Process the PredictionResult to get the predicted label.
Returns a comma separated string with true label and predicted label.
"""
defprocess(self, element: tuple[int, PredictionResult]) ->Iterable[str]:
label, prediction_result=element
prediction=prediction_result.inference
yield'{},{}'.format(label, prediction)
defparse_known_args(argv):
"""Parses args for the workflow."""
parser=argparse.ArgumentParser()
parser.add_argument(
'--input',
dest='input',
required=True,
help='text file with comma separated int values.')
parser.add_argument(
'--output',
dest='output',
required=True,
help='Path to save output predictions.')
parser.add_argument(
'--model_path',
dest='model_path',
required=True,
help='Path to load the Sklearn model for Inference.')
parser.add_argument(
'--large_model',
action='store_true',
dest='large_model',
default=False,
help='Set to true if your model is large enough to run into memory '
'pressure if you load multiple copies.')
returnparser.parse_known_args(argv)
defrun(
argv=None, save_main_session=True, test_pipeline=None) ->PipelineResult:
"""
Args:
argv: Command line arguments defined for this example.
save_main_session: Used for internal testing.
test_pipeline: Used for internal testing.
"""
known_args, pipeline_args=parse_known_args(argv)
pipeline_options=PipelineOptions(pipeline_args)
pipeline_options.view_as(SetupOptions).save_main_session=save_main_session
requirements_dir=os.path.dirname(os.path.realpath(__file__))
# Pin to the version that we trained the model on.
# Sklearn doesn't guarantee compatability between versions.
pipeline_options.view_as(
SetupOptions
).requirements_file=f'{requirements_dir}/sklearn_examples_requirements.txt'
# In this example we pass keyed inputs to RunInference transform.
# Therefore, we use KeyedModelHandler wrapper over SklearnModelHandlerNumpy.
model_loader=KeyedModelHandler(
SklearnModelHandlerNumpy(
model_file_type=ModelFileType.PICKLE,
model_uri=known_args.model_path,
large_model=known_args.large_model))
pipeline=test_pipeline
ifnottest_pipeline:
pipeline=beam.Pipeline(options=pipeline_options)
label_pixel_tuple= (
pipeline
|"ReadFromInput">>beam.io.ReadFromText(known_args.input)
|"PreProcessInputs">>beam.Map(process_input))
predictions= (
label_pixel_tuple
|"RunInference">>RunInference(model_loader)
|"PostProcessOutputs">>beam.ParDo(PostProcessor()))
_=predictions|"WriteOutput">>beam.io.WriteToText(
known_args.output, shard_name_template='', append_trailing_newlines=True)
result=pipeline.run()
result.wait_until_finish()
returnresult
if__name__=='__main__':
logging.getLogger().setLevel(logging.INFO)
run()