Exemple #1
0
    def forward(self, x):
        """Compute the forward convolution."""
        self.cached_input_shape = x.shape
        self.cache = x

        if not self.channels_first:
            x = tfe.transpose(x, perm=[0, 3, 1, 2])

        out = tfe.conv2d(x, self.weights, self.strides, self.padding)
        if self.bias is not None:
            out = out + self.bias

        if not self.channels_first:
            out = tfe.transpose(out, perm=[0, 2, 3, 1])

        return out
    def call(self, inputs):

        if self.data_format != 'channels_first':
            inputs = tfe.transpose(inputs, perm=[0, 3, 1, 2])

        outputs = tfe.conv2d(inputs, self.kernel, self.strides[0],
                             self.padding)

        if self.use_bias:
            outputs = outputs + self.bias

        if self.data_format != 'channels_first':
            outputs = tfe.transpose(outputs, perm=[0, 2, 3, 1])

        if self.activation is not None:
            return self.activation(outputs)
        return outputs

model_trainer = ModelTrainer()
prediction_client = PredictionClient()

# get model parameters as private tensors from model owner
params = tfe.define_private_input('model-trainer', model_trainer.provide_input, masked=True)  # pylint: disable=E0632

# we'll use the same parameters for each prediction so we cache them to avoid re-training each time
params = tfe.cache(params)

# get prediction input from client
x, y = tfe.define_private_input('prediction-client', prediction_client.provide_input, masked=True)  # pylint: disable=E0632

# helpers
conv = lambda x, w, s: tfe.conv2d(x, w, s, 'VALID')
pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID')

# compute prediction
Wconv1, bconv1, Wfc1, bfc1, Wfc2, bfc2 = params
bconv1 = tfe.reshape(bconv1, [-1, 1, 1])
layer1 = pool(tfe.relu(conv(x, Wconv1, ModelTrainer.STRIDE) + bconv1))
layer1 = tfe.reshape(layer1, [-1, ModelTrainer.HIDDEN_FC1])
layer2 = tfe.matmul(layer1, Wfc1) + bfc1
logits = tfe.matmul(layer2, Wfc2) + bfc2

# send prediction output back to client
prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output)


with tfe.Session() as sess:
Exemple #4
0
# get model parameters as private tensors from model owner
params = tfe.define_private_input('model-trainer',
                                  model_trainer.provide_input,
                                  masked=True)  # pylint: disable=E0632

# we'll use the same parameters for each prediction so we cache them to avoid re-training each time
params = tfe.cache(params)

# get prediction input from client
x, y = tfe.define_private_input('prediction-client',
                                prediction_client.provide_input,
                                masked=True)  # pylint: disable=E0632

# helpers
conv = lambda x, w: tfe.conv2d(x, w, 1, 'VALID')
pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID')

# compute prediction
Wconv1, bconv1, Wconv2, bconv2, Wfc1, bfc1, Wfc2, bfc2 = params
bconv1 = tfe.reshape(bconv1, [-1, 1, 1])
bconv2 = tfe.reshape(bconv2, [-1, 1, 1])
layer1 = pool(tfe.relu(conv(x, Wconv1) + bconv1))
layer2 = pool(tfe.relu(conv(layer1, Wconv2) + bconv2))
layer2 = tfe.reshape(layer2, [-1, ModelTrainer.HIDDEN_FC1])
layer3 = tfe.matmul(layer2, Wfc1) + bfc1
logits = tfe.matmul(layer3, Wfc2) + bfc2

# send prediction output back to client
prediction_op = tfe.define_output('prediction-client', [logits, y],
                                  prediction_client.receive_output)
Exemple #5
0
 def conv(x, w, s):
     return tfe.conv2d(x, w, s, 'VALID')
model_trainer = ModelTrainer()
prediction_client = PredictionClient()


# get model parameters as private tensors from model owner
params = tfe.define_private_input('model-trainer', model_trainer.provide_input, masked=True)  # pylint: disable=E0632

# we'll use the same parameters for each prediction so we cache them to avoid re-training each time
params = tfe.cache(params)

# get prediction input from client
x, y = tfe.define_private_input('prediction-client', prediction_client.provide_input, masked=True)  # pylint: disable=E0632

# helpers
conv = lambda x, w: tfe.conv2d(x, w, ModelTrainer.STRIDE, 'VALID')
pool = lambda x: tfe.avgpool2d(x, (2, 2), (2, 2), 'VALID')

# compute prediction
Wconv1, bconv1, Wconv2, bconv2, Wfc1, bfc1, Wfc2, bfc2, Wfc3, bfc3 = params
bconv1 = tfe.reshape(bconv1, [-1, 1, 1])
bconv2 = tfe.reshape(bconv2, [-1, 1, 1])
layer1 = pool(tfe.relu(conv(x, Wconv1) + bconv1))
layer2 = pool(tfe.relu(conv(layer1, Wconv2) + bconv2))
layer2 = tfe.reshape(layer2, [-1, ModelTrainer.HIDDEN_FC1])
layer3 = tfe.matmul(layer2, Wfc1) + bfc1
layer4 = tfe.matmul(layer3, Wfc2) + bfc2
logits = tfe.matmul(layer4, Wfc3) + bfc3

# send prediction output back to client
prediction_op = tfe.define_output('prediction-client', [logits, y], prediction_client.receive_output)