Esempio n. 1
0
    def test_keras_2_image_bias(self):
        # define Keras model and get prediction
        input_shape1 = (100, 60, 3)
        input_shape2 = (23, 45, 3)

        data1 = Input(shape=input_shape1)
        data2 = Input(shape=input_shape2)
        a_pool = GlobalMaxPooling2D()(data1)
        b_pool = GlobalMaxPooling2D()(data2)
        output = keras.layers.add([a_pool, b_pool])
        model = Model(inputs=[data1, data2], outputs=output)

        data1 = np.ones(input_shape1)
        data2 = np.ones(input_shape2)
        keras_input1 = np.ones(input_shape1)
        keras_input2 = np.ones(input_shape2)

        data1[:, :, 0] = 100.0
        data1[:, :, 1] = 79.0
        data1[:, :, 2] = 194.0

        data2[:, :, 0] = 130.0
        data2[:, :, 1] = 91.0
        data2[:, :, 2] = 11.0

        red_bias1 = -88.0
        green_bias1 = -2
        blue_bias1 = -40

        red_bias2 = -100.0
        green_bias2 = -29
        blue_bias2 = -15

        keras_input1[:, :, 0] = data1[:, :, 2] + blue_bias1
        keras_input1[:, :, 1] = data1[:, :, 1] + green_bias1
        keras_input1[:, :, 2] = data1[:, :, 0] + red_bias1

        keras_input2[:, :, 0] = data2[:, :, 0] + red_bias2
        keras_input2[:, :, 1] = data2[:, :, 1] + green_bias2
        keras_input2[:, :, 2] = data2[:, :, 2] + blue_bias2

        keras_preds = model.predict([
            np.expand_dims(keras_input1, axis=0),
            np.expand_dims(keras_input2, axis=0)
        ])
        keras_preds = keras_preds.flatten()

        # convert to coreml and get predictions
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, "keras.mlmodel")
        from coremltools.converters import keras as keras_converter

        coreml_model = keras_converter.convert(
            model,
            input_names=["data1", "data2"],
            output_names=["output"],
            image_input_names=["data1", "data2"],
            red_bias={
                "data1": red_bias1,
                "data2": red_bias2
            },
            green_bias={
                "data1": green_bias1,
                "data2": green_bias2
            },
            blue_bias={
                "data1": blue_bias1,
                "data2": blue_bias2
            },
            is_bgr={
                "data1": True,
                "data2": False
            },
        )

        if _is_macos() and _macos_version() >= (10, 13):
            coreml_input_dict = dict()
            coreml_input_dict["data1"] = PIL.Image.fromarray(
                data1.astype(np.uint8))
            coreml_input_dict["data2"] = PIL.Image.fromarray(
                data2.astype(np.uint8))
            coreml_preds = coreml_model.predict(
                coreml_input_dict)["output"].flatten()

            # compare
            self.assertEqual(len(keras_preds), len(coreml_preds))
            max_relative_error = compare_models(keras_preds, coreml_preds)
            self.assertAlmostEqual(max(max_relative_error, 0.001),
                                   0.001,
                                   delta=1e-6)

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 2
0
# load data
x_u, _ = load_data.load_wetland_samples(SUBSET_PATH)
np.random.shuffle(x_u)
x_l, target_name = load_data.load_wetland_samples(TARGET_SAMPLE_DIR)
x_l_aug = data_augmentation.data_aug(x_l, SHIFT_LIST, ROTATION_ANGLE)

np.random.shuffle(x_l_aug)

x_l = np.reshape(x_l, [-1, IMG_SIZE * IMG_SIZE * 3])
x_l_aug = np.reshape(x_l_aug, [-1, IMG_SIZE * IMG_SIZE * 3])
x_u = np.reshape(x_u, [-1, IMG_SIZE * IMG_SIZE * 3])
image_size = x_u.shape[1]
original_dim = image_size

x_u = x_u.astype('float32') / 255
x_l = x_l.astype('float32') / 255
x_l_aug = x_l_aug.astype('float32') / 255

np.random.shuffle(x_l_aug)
x_l_aug = np.vstack((x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug, x_l_aug,\
                    x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,\
                     x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,\
                    x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug,x_l_aug))

print('target samples shape: ', x_l_aug.shape)
print('all samples shape: ', x_u.shape)

vae.compile(optimizer=optimizer, loss=None)
checkpoint = ModelCheckpoint('./logs/weights{epoch:08d}.h5',
                             save_weights_only=True,