def test_serialize(): """Tests that the Concat layer correctly serializes itself. """ n_inputs = 125 fullyconnected_outputs = 246 weights = np.random.uniform(size=(n_inputs, fullyconnected_outputs)) biases = np.random.uniform(size=(fullyconnected_outputs)) fullyconnected_layer = FullyConnectedLayer(weights, biases) means = np.random.uniform(size=(n_inputs)).astype(np.float32) stds = np.random.uniform(size=(n_inputs)).astype(np.float32) normalize_layer = NormalizeLayer(means, stds) concat_layer = ConcatLayer([fullyconnected_layer, normalize_layer], ConcatAlong.FLAT) serialized = concat_layer.serialize() assert serialized.WhichOneof("layer_data") == "concat_data" assert (serialized.concat_data.concat_along == transformer_pb.ConcatLayerData.ConcatAlong.Value( "CONCAT_ALONG_FLAT")) assert len(serialized.concat_data.layers) == 2 assert serialized.concat_data.layers[0] == fullyconnected_layer.serialize() assert serialized.concat_data.layers[1] == normalize_layer.serialize() # TODO: This does not check that deserialized.input_layers was done # correctly, but that should be the case as long as their deserialize # methods work (tested in their respective files). deserialized = ConcatLayer.deserialize(serialized) assert deserialized.concat_along == ConcatAlong.FLAT serialized.relu_data.SetInParent() deserialized = ConcatLayer.deserialize(serialized) assert deserialized is None concat_layer.concat_along = ConcatAlong.CHANNELS deserialized = ConcatLayer.deserialize(concat_layer.serialize()) assert deserialized.concat_along == ConcatAlong.CHANNELS try: ConcatAlong.deserialize(5) assert False, "Should have errored on unrecognized serialization." except NotImplementedError: pass
def test_serialize(): """Tests the Network's serialize and deserialize methods. """ input_dims = np.random.randint(1, 32) output_dims = np.random.randint(1, 64) weights = np.random.uniform(size=(input_dims, output_dims)) biases = np.random.uniform(size=(output_dims)) fullyconnected_layer = FullyConnectedLayer(weights, biases) relu_layer = ReluLayer() network = Network([fullyconnected_layer, relu_layer]) serialized = network.serialize() assert len(serialized.layers) == 2 assert serialized.layers[0] == fullyconnected_layer.serialize() assert serialized.layers[1] == relu_layer.serialize() deserialized = Network.deserialize(serialized) assert deserialized.serialize() == serialized
def test_serialize(): """Tests that the Concat layer correctly serializes itself. """ n_inputs = 1025 fullyconnected_outputs = 2046 weights = np.random.uniform(size=(n_inputs, fullyconnected_outputs)) biases = np.random.uniform(size=(fullyconnected_outputs)) fullyconnected_layer = FullyConnectedLayer(weights, biases) means = np.random.uniform(size=(n_inputs)).astype(np.float32) stds = np.random.uniform(size=(n_inputs)).astype(np.float32) normalize_layer = NormalizeLayer(means, stds) concat_layer = ConcatLayer([fullyconnected_layer, normalize_layer], ConcatAlong.FLAT) serialized = concat_layer.serialize() assert serialized.WhichOneof("layer_data") == "concat_data" assert (serialized.concat_data.concat_along == transformer_pb. ConcatLayerData.ConcatAlong.Value("CONCAT_ALONG_FLAT")) assert len(serialized.concat_data.layers) == 2 assert serialized.concat_data.layers[0] == fullyconnected_layer.serialize() assert serialized.concat_data.layers[1] == normalize_layer.serialize()
def test_transform_planes(): open_stub_ = transformer_client.open_stub input_dims = np.random.randint(1, 32) output_dims = np.random.randint(1, 64) weights = np.random.uniform(size=(input_dims, output_dims)) biases = np.random.uniform(size=(output_dims)) fullyconnected_layer = FullyConnectedLayer(weights, biases) relu_layer = ReluLayer() network = Network([fullyconnected_layer, relu_layer]) planes = list(np.random.uniform(size=(100, 3, input_dims))) response_messages = [] for i, plane in enumerate(planes): transformed_polytope = transformer_pb.UPolytope() transformed_polytope.space_dimensions = output_dims transformed_polytope.subspace_dimensions = 2 for j in range(i + 2): polytope = transformer_pb.VPolytope() polytope.vertices.extend(np.matmul(plane, weights).flatten()) polytope.combinations.extend(np.eye(3).flatten()) polytope.num_vertices = 3 transformed_polytope.polytopes.append(polytope) response = transformer_pb.TransformResponse() response.transformed_upolytope.CopyFrom(transformed_polytope) response_messages.append(response) # With include_post = True. stub = ServerStubMock(response_messages) transformer_client.open_stub = lambda: stub transformed = transformer_client.transform_planes(network, planes) assert len(transformed) == len(planes) for i, plane in enumerate(planes): upolytope = transformed[i] assert len(upolytope) == (i + 2) for vpolytope in upolytope: transformed_pre, transformed_post = vpolytope assert len(transformed_pre) == len(transformed_post) == 3 assert np.allclose(transformed_pre, np.eye(3)) assert np.allclose(transformed_post, np.matmul(plane, weights)) assert len(stub.received_messages) == 1 received = stub.received_messages[0] assert len(received) == 102 assert received[0].WhichOneof("request_data") == "layer" assert received[0].layer == fullyconnected_layer.serialize() assert received[1].layer == relu_layer.serialize() for i, request in enumerate(received[2:]): assert request.WhichOneof("request_data") == "upolytope" assert request.upolytope.space_dimensions == input_dims assert request.upolytope.subspace_dimensions == 2 assert len(request.upolytope.polytopes) == 1 assert request.upolytope.polytopes[0].num_vertices == 3 assert np.allclose(request.upolytope.polytopes[0].vertices, planes[i].flatten()) assert np.allclose(request.upolytope.polytopes[0].combinations, np.eye(3).flatten())