コード例 #1
0
 def _forward_coreml(self, torch_model):
     from _torch_converter import convert
     output_names = ['output']
     if self.output_count > 1:
         output_names = [
             'output_' + str(i)
             for i in range(self.output_count)
         ]
     if isinstance(self.input, list):
         input_shapes = [inp.shape for inp in self.input]
         input_names = ['input_' + str(i) for i in range(len(self.input))]
         coreml_model = convert(
             torch_model,
             input_shapes,
             input_names=input_names,
             output_names=output_names
         )
         result = coreml_model.predict(
             dict(zip(input_names, self.input)), useCPUOnly=True
         )
     else:
         coreml_model = convert(
             torch_model,
             [self.input.shape],
             output_names=output_names
         )
         result = coreml_model.predict(
             {'input': self.input}, useCPUOnly=True
         )
     if self.output_count > 1:
         return [result[name] for name in output_names]
     else:
         return result['output']
コード例 #2
0
    def test_image_deprocess_scale(self):
        from _torch_converter import convert
        coreml_model = convert(self.model, [self.input.shape],
                               input_names=['image'],
                               output_names=['output'],
                               image_input_names=['image'],
                               preprocessing_args={
                                   'is_bgr': False,
                                   'red_bias': 0.0,
                                   'green_bias': 0.0,
                                   'blue_bias': 0.0,
                                   'image_scale': 0.5
                               },
                               image_output_names=['output'],
                               deprocessing_args={
                                   'is_bgr': False,
                                   'red_bias': 0.0,
                                   'green_bias': 0.0,
                                   'blue_bias': 0.0,
                                   'image_scale': 2.0
                               })

        input_array = (np.random.rand(224, 224, 3) * 255).astype('uint8')
        input_image = Image.fromarray(input_array).convert('RGBA')

        output_image = coreml_model.predict({"image": input_image})["output"]
        output_array = np.array(output_image.convert('RGB'))

        npt.assert_array_equal(output_array, input_array)
コード例 #3
0
    def test_classifier(self):
        from _torch_converter import convert

        class_labels = ['class1', 'class2', 'class3']

        coreml_model = convert(self.model, [(3, )],
                               mode='classifier',
                               class_labels=class_labels,
                               predicted_feature_name='class')

        input_array = [0.0, 1.0, 0.0]

        predicted_class = coreml_model.predict({'input': input_array})['class']
        self.assertEqual(predicted_class, class_labels[1])
コード例 #4
0
ファイル: test_models.py プロジェクト: hbcbh1999/torch2coreml
    def test_resnet18(self):
        torch_net = load_lua(self.model_path)
        torch_net.evaluate()

        input_0 = np.random.ranf((3, 224, 224))

        input_tensor = torch.from_numpy(np.asarray([input_0])).float()

        torch_output = torch_net.forward(input_tensor).numpy().flatten()

        from _torch_converter import convert
        coreml_net = convert(self.model_path, [(3, 224, 224)])
        coreml_output = coreml_net.predict({'input': input_0})['output']

        corrcoef = np.corrcoef(coreml_output, torch_output).flatten()[1]
        self.assertAlmostEqual(corrcoef, 1.0, delta=1e-4)
コード例 #5
0
import sys
import os
sys.path.append(
    os.path.dirname(os.path.realpath(__file__)) + "/../../torch2coreml")

from _torch_converter import convert
import _layers as layers
from torch.utils.serialization import load_lua
import torch as th
import torch.tensor
import torch.nn as nn
from collections import OrderedDict
from torch.autograd import Variable


def convert_unknown(builder, name, layer, input_names, output_names):
    print("!! No converter yet for layer: " + name)
    return output_names


coreml_model = convert("openface.t7", [(3, 96, 96)],
                       image_input_names=['input'],
                       output_shapes=[[128]],
                       mode=None,
                       unknown_layer_converter_fn=convert_unknown)

coreml_model.author = 'Leonardo Galli feat. OpenFace'
coreml_model.license = 'Free for personal or research use'
coreml_model.save("openface.mlmodel")