def create_example(image_path): assert type(image_path) is types.StringType, 'image_path: passed object of incorrect type' image_data = open(image_path, 'rb').read() class_label, class_name = _get_tf_class(os.path.split(image_path)[0]) return tft.Example(features=tft.Features(feature={ 'image/label': _int64_feature(class_label), 'image/encoded': _bytes_feature(tfc.as_bytes(image_data)), }))
def export(self, session, path, description, version=1): export_path_base = sys.argv[-1] export_path = os.path.join( compat.as_bytes(path), compat.as_bytes('models'), compat.as_bytes(str(description + '_%d' % version))) while os.path.exists(export_path): version += 1 export_path = os.path.join( compat.as_bytes(path), compat.as_bytes('models'), compat.as_bytes(str(description + '_%d' % version))) logger.info('Exporting trained model to %s' % export_path) builder = saved_model_builder.SavedModelBuilder(export_path) classification_signature = signature_def_utils.classification_signature_def( self.feature_data, self.classes, self.scores) legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op') builder.add_meta_graph_and_variables( session, [tag_constants.SERVING], signature_def_map={ 'classify': classification_signature, signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: classification_signature }) builder.save()
def _send_json_response(self, obj, code=200): """Writes out the given object as JSON using the given HTTP status code. This also replaces special float values with stringified versions. Args: obj: The object to respond with. code: The numeric HTTP status code to use. """ output = json.dumps(float_wrapper.WrapSpecialFloats(obj)) self.send_response(code) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", len(output)) self.end_headers() self.wfile.write(compat.as_bytes(output))
def _send_json_response(self, obj, code=200): """Writes out the given object as JSON using the given HTTP status code. This also replaces special float values with stringified versions. Args: obj: The object to respond with. code: The numeric HTTP status code to use. """ output = json.dumps(float_wrapper.WrapSpecialFloats(obj)) self.send_response(code) self.send_header('Content-Type', 'application/json') self.send_header('Content-Length', len(output)) self.end_headers() self.wfile.write(compat.as_bytes(output))
def _send_gzip_response(self, content, content_type, code=200): """Writes the given content as gzip response using the given content type. Args: content: The content to respond with. content_type: The mime type of the content. code: The numeric HTTP status code to use. """ out = BytesIO() f = gzip.GzipFile(fileobj=out, mode="wb") f.write(compat.as_bytes(content)) f.close() gzip_content = out.getvalue() self.send_response(code) self.send_header("Content-Type", content_type) self.send_header("Content-Length", len(gzip_content)) self.send_header("Content-Encoding", "gzip") self.end_headers() self.wfile.write(gzip_content)
def _send_gzip_response(self, content, content_type, code=200): """Writes the given content as gzip response using the given content type. Args: content: The content to respond with. content_type: The mime type of the content. code: The numeric HTTP status code to use. """ out = BytesIO() f = gzip.GzipFile(fileobj=out, mode='wb') f.write(compat.as_bytes(content)) f.close() gzip_content = out.getvalue() self.send_response(code) self.send_header('Content-Type', content_type) self.send_header('Content-Length', len(gzip_content)) self.send_header('Content-Encoding', 'gzip') self.end_headers() self.wfile.write(gzip_content)
def read_record(self, csv_string): """Reads out bytes for PY2 and Unicode for PY3.""" self._line_generator.push_line( as_bytes(csv_string) if six.PY2 else as_text(csv_string)) output = next(self._reader) return [as_bytes(x) for x in output] # pytype: disable=attribute-error