コード例 #1
0
 def test_CGAN(self):
     keras_model = CGAN().combined
     batch = 5
     x = np.random.rand(batch, 100).astype(np.float32)
     y = np.random.rand(batch, 1).astype(np.float32)
     expected = keras_model.predict([x, y])
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, {keras_model.input_names[0]: x, keras_model.input_names[1]: y}, expected, self.model_files))
コード例 #2
0
 def test_inception_v4(self):
     K.clear_session()
     keras_model = create_inception_v4()
     data = np.random.rand(2, 299, 299, 3).astype(np.float32)
     expected = keras_model.predict(data)
     onnx_model = mock_keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, data, expected, self.model_files))
コード例 #3
0
 def test_WGAN(self):
     keras_model = WGAN().combined
     x = np.random.rand(5, 100).astype(np.float32)
     expected = keras_model.predict(x)
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))
コード例 #4
0
ファイル: test_aae.py プロジェクト: yiming258/keras-onnx
 def test_AdversarialAutoencoder(self):
     keras_model = AdversarialAutoencoder().adversarial_autoencoder
     x = np.random.rand(5, 28, 28, 1).astype(np.float32)
     expected = keras_model.predict(x)
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))
コード例 #5
0
ファイル: test_tf2_keras.py プロジェクト: vinceyzw/keras-onnx
 def test_mlf(self):
     tf.keras.backend.clear_session()
     mlf = MLP()
     np_input = tf.random.normal((2, 20))
     expected = mlf.predict(np_input)
     oxml = keras2onnx.convert_keras(mlf)
     self.assertTrue(
         run_onnx_runtime('lenet', oxml, np_input.numpy(), expected,
                          self.model_files))
コード例 #6
0
 def test_lenet(self):
     tf.keras.backend.clear_session()
     lenet = LeNet()
     data = np.random.rand(2 * 416 * 416 * 3).astype(np.float32).reshape(2, 416, 416, 3)
     expected = lenet(data)
     lenet._set_inputs(data)
     oxml = keras2onnx.convert_keras(lenet)
     model_files = []
     self.assertTrue(run_onnx_runtime('lenet', oxml, data, expected, model_files))
コード例 #7
0
 def test_TFBertModel(self):
     from transformers import BertTokenizer, TFBertModel
     pretrained_weights = 'bert-base-uncased'
     tokenizer = BertTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFBertModel.from_pretrained(pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx,
                          predictions, self.model_files))
コード例 #8
0
 def test_TFOpenAIGPTDoubleHeadsModel(self):
     from transformers import OpenAIGPTTokenizer, TFOpenAIGPTDoubleHeadsModel
     pretrained_weights = 'openai-gpt'
     tokenizer = OpenAIGPTTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFOpenAIGPTDoubleHeadsModel.from_pretrained(pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx,
                          predictions, self.model_files))
コード例 #9
0
 def test_CRAFT(self):
     # input_image = Input(shape=(None, None, 3)) -- Need fixed input shape
     input_image = Input(shape=(512, 512, 3))
     region, affinity = VGG16_UNet(input_tensor=input_image, weights=None)
     keras_model = Model(input_image, [region, affinity], name='vgg16_unet')
     x = np.random.rand(1, 512, 512, 3).astype(np.float32)
     expected = keras_model.predict(x)
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))
コード例 #10
0
 def test_mlf(self):
     tf.keras.backend.clear_session()
     mlf = MLP()
     input = tf.random.normal((2, 20))
     expected = mlf(input)
     mlf._set_inputs(input)
     oxml = keras2onnx.convert_keras(mlf)
     model_files = []
     self.assertTrue(
         run_onnx_runtime('lenet', oxml, input.numpy(), expected,
                          model_files))
コード例 #11
0
 def test_3layer_gpt2(self):
     from transformers import GPT2Config, TFGPT2Model, BertTokenizer
     keras2onnx.proto.keras.backend.set_learning_phase(0)
     config = GPT2Config(n_layer=3)
     model = TFGPT2Model(config)
     tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     inputs = tokenizer.encode_plus(text, add_special_tokens=True, return_tensors='tf')
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
コード例 #12
0
 def test_TFRobertaForTokenClassification(self):
     from transformers import RobertaTokenizer, TFRobertaForTokenClassification
     pretrained_weights = 'roberta-base'
     tokenizer = RobertaTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFRobertaForTokenClassification.from_pretrained(
         pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx,
                          predictions, self.model_files))
コード例 #13
0
 def test_TFXLMForQuestionAnsweringSimple(self):
     from transformers import XLMConfig, TFXLMForQuestionAnsweringSimple
     keras.backend.clear_session()
     # pretrained_weights = 'xlm-mlm-enfr-1024'
     tokenizer_file = 'xlm_xlm-mlm-enfr-1024.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     config = XLMConfig()
     model = TFXLMForQuestionAnsweringSimple(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
コード例 #14
0
 def test_TFDistilBertForQuestionAnswering(self):
     from transformers import DistilBertConfig, TFDistilBertForQuestionAnswering
     keras.backend.clear_session()
     # pretrained_weights = 'distilbert-base-uncased'
     tokenizer_file = 'distilbert_distilbert-base-uncased.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     config = DistilBertConfig()
     model = TFDistilBertForQuestionAnswering(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
コード例 #15
0
 def test_TFDistilBertForSequenceClassification(self):
     from transformers import DistilBertTokenizer, TFDistilBertForSequenceClassification
     pretrained_weights = 'distilbert-base-uncased'
     tokenizer = DistilBertTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFDistilBertForSequenceClassification.from_pretrained(
         pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx,
                          predictions, self.model_files))
コード例 #16
0
 def test_TFXLMForQuestionAnsweringSimple(self):
     from transformers import XLMTokenizer, TFXLMForQuestionAnsweringSimple
     pretrained_weights = 'xlm-mlm-enfr-1024'
     tokenizer = XLMTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFXLMForQuestionAnsweringSimple.from_pretrained(
         pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx,
                          predictions, self.model_files))
コード例 #17
0
 def test_TFOpenAIGPTModel(self):
     from transformers import OpenAIGPTConfig, TFOpenAIGPTModel
     keras.backend.clear_session()
     # pretrained_weights = 'openai-gpt'
     tokenizer_file = 'openai_openai-gpt.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     config = OpenAIGPTConfig()
     model = TFOpenAIGPTModel(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
コード例 #18
0
 def test_TFRobertaForTokenClassification(self):
     from transformers import RobertaConfig, TFRobertaForTokenClassification
     keras.backend.clear_session()
     # pretrained_weights = 'roberta-base'
     tokenizer_file = 'roberta_roberta-base.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     config = RobertaConfig()
     model = TFRobertaForTokenClassification(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files))
コード例 #19
0
 def test_PixelDA(self):
     keras_model = PixelDA().combined
     x = np.random.rand(5, 32, 32, 3).astype(np.float32)
     expected = keras_model.predict([x])
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name,
                          onnx_model,
                          x,
                          expected,
                          self.model_files,
                          atol=1.e-5))
コード例 #20
0
ファイル: test_ccgan.py プロジェクト: peri044/tensorflow-onnx
 def test_CCGAN(self):
     keras_model = CCGAN().combined
     x = np.random.rand(2, 32, 32, 1).astype(np.float32)
     expected = keras_model.predict([x])
     onnx_model = mock_keras2onnx.convert_keras(keras_model,
                                                keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name,
                          onnx_model,
                          x,
                          expected,
                          self.model_files,
                          rtol=1.e-2,
                          atol=1.e-4))
コード例 #21
0
 def test_SSR_Net_MT(self):
     K.clear_session()
     _IMAGE_SIZE = 64
     stage_num = [3, 3, 3]
     num_classes = 3
     lambda_d = 1
     keras_model = SSR_net_MT(_IMAGE_SIZE, num_classes, stage_num,
                              lambda_d)()
     data = np.random.rand(2, 64, 64, 3).astype(np.float32)
     expected = keras_model.predict(data)
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, data, expected,
                          self.model_files))
コード例 #22
0
 def test_TFBertModel(self):
     from transformers import BertConfig, TFBertModel
     keras.backend.clear_session()
     # pretrained_weights = 'bert-base-uncased'
     tokenizer_file = 'bert_bert-base-uncased.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     config = BertConfig()
     model = TFBertModel(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2,
                          atol=1.e-4))
コード例 #23
0
 def test_OpenFace(self):
     keras_model = loadModel()
     x = np.random.rand(2, 96, 96, 3).astype(np.float32)
     expected = keras_model.predict(x)
     onnx_model = mock_keras2onnx.convert_keras(keras_model,
                                                keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name,
                          onnx_model,
                          x,
                          expected,
                          self.model_files,
                          rtol=5e-3,
                          atol=5e-6))
コード例 #24
0
 def test_lstm_text_generation(self):
     # Generate text from Nietzsche's writings.
     # from https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py
     maxlen = 40
     chars_len = 20
     batch_size = 32
     model = Sequential()
     model.add(LSTM(128, input_shape=(maxlen, chars_len)))
     model.add(Dense(chars_len, activation='softmax'))
     onnx_model = keras2onnx.convert_keras(model, model.name)
     x = np.random.rand(batch_size, maxlen, chars_len).astype(np.float32)
     expected = model.predict(x)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))
コード例 #25
0
 def test_TFOpenAIGPTDoubleHeadsModel(self):
     from transformers import OpenAIGPTConfig, TFOpenAIGPTDoubleHeadsModel
     keras.backend.clear_session()
     # pretrained_weights = 'openai-gpt'
     tokenizer_file = 'openai_openai-gpt.pickle'
     tokenizer = self._get_tokenzier(tokenizer_file)
     # tf.gather(hidden_states, cls_index, batch_dims=len(hidden_shape) - 2), batch_dims = 1 in this case
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer, batch_size=1)
     config = OpenAIGPTConfig()
     model = TFOpenAIGPTDoubleHeadsModel(config)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, inputs_onnx, predictions, self.model_files, rtol=1.e-2,
                          atol=1.e-4))
コード例 #26
0
 def test_imdb_lstm(self):
     # An LSTM model on the IMDB sentiment classification task.
     # from https://github.com/keras-team/keras/blob/master/examples/imdb_lstm.py
     max_features = 20000
     maxlen = 80
     batch_size = 32
     model = Sequential()
     model.add(Embedding(max_features, 128))
     model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
     model.add(Dense(1, activation='sigmoid'))
     onnx_model = keras2onnx.convert_keras(model, model.name)
     x = np.random.rand(batch_size, maxlen).astype(np.float32)
     expected = model.predict(x)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))
コード例 #27
0
 def test_TFXLMWithLMHeadModel(self):
     from transformers import XLMTokenizer, TFXLMWithLMHeadModel
     pretrained_weights = 'xlm-mlm-enfr-1024'
     tokenizer = XLMTokenizer.from_pretrained(pretrained_weights)
     text, inputs, inputs_onnx = self._prepare_inputs(tokenizer)
     model = TFXLMWithLMHeadModel.from_pretrained(pretrained_weights)
     predictions = model.predict(inputs)
     onnx_model = keras2onnx.convert_keras(model, model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name,
                          onnx_model,
                          inputs_onnx,
                          predictions,
                          self.model_files,
                          rtol=1.e-2,
                          atol=1.e-4))
コード例 #28
0
ファイル: test_srgan.py プロジェクト: yiming258/keras-onnx
 def test_SRGAN(self):
     keras_model = SRGAN().combined
     x = np.random.rand(5, 64, 64, 3).astype(np.float32)
     y = np.random.rand(5, 256, 256, 3).astype(np.float32)
     expected = keras_model.predict([x, y])
     onnx_model = keras2onnx.convert_keras(keras_model, keras_model.name)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name,
                          onnx_model, {
                              keras_model.input_names[0]: x,
                              keras_model.input_names[1]: y
                          },
                          expected,
                          self.model_files,
                          rtol=1.e-2,
                          atol=1.e-3))
コード例 #29
0
ファイル: test_tf2_keras.py プロジェクト: vinceyzw/keras-onnx
    def test_tf_ops(self):
        tf.keras.backend.clear_session()

        def op_func(arg_inputs):
            x = tf.math.squared_difference(arg_inputs[0], arg_inputs[1])
            x = tf.matmul(x, x, adjoint_b=True)
            r = tf.rank(x)
            x = x - tf.cast(tf.expand_dims(r, axis=0), tf.float32)
            return x

        dm = DummyModel(op_func)
        inputs = [tf.random.normal((3, 2, 20)), tf.random.normal((3, 2, 20))]
        expected = dm.predict(inputs)
        oxml = keras2onnx.convert_keras(dm)
        self.assertTrue(
            run_onnx_runtime('op_model', oxml, [i_.numpy() for i_ in inputs],
                             expected, self.model_files))
コード例 #30
0
 def test_imdb_bidirectional_lstm(self):
     # A Bidirectional LSTM on the IMDB sentiment classification task.
     # from https://github.com/keras-team/keras/blob/master/examples/imdb_bidirectional_lstm.py
     max_features = 20000
     maxlen = 100
     batch_size = 32
     model = Sequential()
     model.add(Embedding(max_features, 128, input_length=maxlen))
     model.add(Bidirectional(LSTM(64)))
     model.add(Dropout(0.5))
     model.add(Dense(1, activation='sigmoid'))
     onnx_model = mock_keras2onnx.convert_keras(model, model.name)
     x = np.random.rand(batch_size, maxlen).astype(np.float32)
     expected = model.predict(x)
     self.assertTrue(
         run_onnx_runtime(onnx_model.graph.name, onnx_model, x, expected,
                          self.model_files))