Пример #1
0
def _detail_keras_model(worker, model_tuple):
    """
    This function converts a serialized model into a local
    model.

    Args:
        modeltuple (bin): serialized obj of Keras model.
        It's a tuple where the first value is the binary of the model.
        The second is the model id.

    Returns:
        tf.keras.models.Model: a deserialized Keras model
    """
    model_ser, model_id = model_tuple
    bio = io.BytesIO(model_ser)
    with TemporaryDirectory() as model_location:
        with zipfile.ZipFile(bio, 'r', zipfile.ZIP_DEFLATED) as model_file:
            # WARNING: zipped archives can potentially deposit extra files onto
            #  the system, although Python's zipfile offers some protection
            #  more info: https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.extractall
            # TODO: further investigate security, find better option if needed
            model_file.extractall(model_location)
        model = tf.keras.models.load_model(model_location)

    initialize_object(
        hook=syft.tensorflow.hook,
        obj=model,
        owner=worker,
        reinitialize=False,
        id=model_id,
        init_args=[],
        init_kwargs={},
    )

    return model
Пример #2
0
def initialize_tensor(hook,
                      obj,
                      owner=None,
                      id=None,
                      init_args=tuple(),
                      init_kwargs={}):
    """Initializes the tensor.

    Args:
        hook: A reference to TorchHook class.
        cls: An object to keep track of id, owner and whether it is a native
            tensor or a wrapper over pytorch.
        is_tensor: A boolean parameter (default False) to indicate whether
            it is torch tensor or not.
        owner: The owner of the tensor being initialised, leave it blank
            to if you have already provided a reference to TorchHook class.
        id: The id of tensor, a random id will be generated if there is no id
            specified.
    """
    initialize_object(
        hook,
        obj,
        owner=owner,
        reinitialize=False,
        id=id,
        init_args=init_args,
        init_kwargs=init_kwargs,
    )
Пример #3
0
 def new___init__(self, *args, owner=None, id=None, register=True, **kwargs):
     initialize_object(
         hook=hook_self,
         obj=self,
         id=id,
         reinitialize=not is_tensor,
         init_args=args,
         init_kwargs=kwargs,
     )
Пример #4
0
def _detail_tf_keras_layers(worker, layer_tuple) -> tf.Tensor:
    """
    This function converts a serialized keras layer into a local keras layer

    Args:
        layer_tuple (bin): serialized obj of TF layer. It's a tuple where
            the first value is the ID, the second value is the binary for the
            layer object, the third value is the layer weights, and
            the fourth value is the batch input shape.

    Returns:
        tf.Tensor: a deserialized TF tensor
    """

    layer_id, layer_bin, weights_bin, batch_input_shape_bin = layer_tuple

    layer_dict = syft.serde.serde._detail(worker, layer_bin)

    layer = tf.keras.layers.deserialize(layer_dict)

    weights = syft.serde.serde._detail(worker, weights_bin)

    batch_input_shape = syft.serde.serde._detail(worker, batch_input_shape_bin)

    layer.build(batch_input_shape)

    layer.set_weights(weights)

    initialize_object(
        hook=syft.tensorflow.hook,
        obj=layer,
        owner=worker,
        reinitialize=False,
        id=layer_id,
        init_args=[],
        init_kwargs={},
    )

    return layer