Exemplo n.º 1
0
def test_CIN(layer_size, activation, split_half):
    with CustomObjectScope({'CIN': layers.CIN}):
        layer_test(layers.CIN, kwargs={"layer_size": layer_size, "activation":
                                       activation, "split_half": split_half}, input_shape=(
            BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 2
0
def test_BiLSTM(merge_mode):
    with CustomObjectScope({'BiLSTM': sequence.BiLSTM}):
        layer_test(sequence.BiLSTM, kwargs={'merge_mode': merge_mode, 'units': EMBEDDING_SIZE, 'dropout_rate': 0.0},
                   # todo 0.5
                   input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE))
Exemplo n.º 3
0
def test_test_CIN_invalid(layer_size):
    with pytest.raises(ValueError):
        with CustomObjectScope({'CIN': layers.CIN}):
            layer_test(layers.CIN,
                       kwargs={"layer_size": layer_size},
                       input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 4
0
def test_test_PredictionLayer_invalid():
    # with pytest.raises(ValueError):
    with CustomObjectScope({'PredictionLayer': layers.PredictionLayer}):
        layer_test(layers.PredictionLayer, kwargs={'use_bias': True,
                                                   }, input_shape=(BATCH_SIZE, 2, 1))
Exemplo n.º 5
0
def test_AttentionSequencePoolingLayer(weight_normalization):
    with CustomObjectScope({'AttentionSequencePoolingLayer': sequence.AttentionSequencePoolingLayer}):
        layer_test(sequence.AttentionSequencePoolingLayer, kwargs={'weight_normalization': weight_normalization},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE),
                                (BATCH_SIZE, 1)])
Exemplo n.º 6
0
def test_FM():
    with CustomObjectScope({'FM': layers.FM}):
        layer_test(layers.FM,
                   kwargs={},
                   input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 7
0
def test_DNN(hidden_units, use_bn):
    with CustomObjectScope({'DNN': layers.DNN}):
        layer_test(layers.DNN, kwargs={'hidden_units': hidden_units, 'use_bn': use_bn, 'dropout_rate': 0.5},
                   input_shape=(
                       BATCH_SIZE, EMBEDDING_SIZE))
from tfutils import Recorder
from tensorflow.python.keras.models import Model, load_model, Sequential
from tensorflow.python.keras import optimizers
from tensorflow.python.keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D
from tensorflow.python.keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.utils import CustomObjectScope
from tensorflow.python.keras.backend import relu

tf.logging.set_verbosity(tf.logging.INFO)


def relu6(x):
    return relu(x, max_value=6)


with CustomObjectScope({'relu6': relu6}):
    base_model = MobileNet(weights='imagenet',
                           include_top=False,
                           input_shape=(224, 224, 3))
    mid_start = 5
    all_layers = base_model.layers
    for i in range(0, mid_start):
        print(i)
        # all_layers[i].add(Flatten())
        all_layers[i].trainable = False

    #x=base_model.output
    #x=Flatten()(x)
    #x=Dense(128,activation='relu')(x)
    #x=Dropout(0.5)(x)
    #pred = Dense(2,activation='softmax')(x)
Exemplo n.º 9
0
def test_FEFMLayer():
    with CustomObjectScope({'FEFMLayer': layers.FEFMLayer}):
        layer_test(layers.FEFMLayer,
                   kwargs={'regularizer': 0.000001},
                   input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 10
0
def test_LocalActivationUnit(hidden_size, activation):
    with CustomObjectScope({'LocalActivationUnit': layers.LocalActivationUnit}):
        layer_test(layers.LocalActivationUnit, kwargs={'hidden_size': hidden_size, 'activation': activation},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE)])
Exemplo n.º 11
0
def test_MLP(hidden_size, use_bn):
    with CustomObjectScope({'MLP': layers.MLP}):
        layer_test(layers.MLP, kwargs={'hidden_size': hidden_size, 'use_bn': use_bn}, input_shape=(
            BATCH_SIZE, EMBEDDING_SIZE))
Exemplo n.º 12
0
def test_CrossNet_invalid():
    with pytest.raises(ValueError):
        with CustomObjectScope({'CrossNet': layers.CrossNet}):
            layer_test(layers.CrossNet, kwargs={
                'layer_num': 1, 'l2_reg': 0}, input_shape=(2, 3, 4))
Exemplo n.º 13
0
def test_CrossNet(layer_num, l2_reg,):
    with CustomObjectScope({'CrossNet': layers.CrossNet}):
        layer_test(layers.CrossNet, kwargs={
                   'layer_num': layer_num, 'l2_reg': l2_reg}, input_shape=(2, 3))
Exemplo n.º 14
0
def test_InteractingLayer(head_num, use_res,):
    with CustomObjectScope({'InteractingLayer': layers.InteractingLayer}):
        layer_test(layers.InteractingLayer, kwargs={"head_num": head_num, "use_res":
                                                    use_res, }, input_shape=(
            BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 15
0
def test_OutterProductLayer(kernel_type):
    with CustomObjectScope({'OutterProductLayer': layers.OutterProductLayer}):
        layer_test(layers.OutterProductLayer,
                   kwargs={'kernel_type': kernel_type},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
Exemplo n.º 16
0
def test_SequencePoolingLayer(mode, supports_masking, input_shape):
    with CustomObjectScope({'SequencePoolingLayer': sequence.SequencePoolingLayer}):
        layer_test(sequence.SequencePoolingLayer, kwargs={'mode': mode, 'supports_masking': supports_masking},
                   input_shape=input_shape, supports_masking=supports_masking)
Exemplo n.º 17
0
def test_BiInteractionPooling():
    with CustomObjectScope(
        {'BiInteractionPooling': layers.BiInteractionPooling}):
        layer_test(layers.BiInteractionPooling,
                   kwargs={},
                   input_shape=(BATCH_SIZE, FIELD_SIZE, EMBEDDING_SIZE))
Exemplo n.º 18
0
def test_Transformer():
    with CustomObjectScope({'Transformer': sequence.Transformer}):
        layer_test(sequence.Transformer, kwargs={'att_embedding_size': 1, 'head_num': 8, 'use_layer_norm': True, 'supports_masking': False,'dropout_rate':0.5},
                   input_shape=[(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE), (BATCH_SIZE, 1), (BATCH_SIZE, 1)])
Exemplo n.º 19
0
def test_AFMLayer():
    with CustomObjectScope({'AFMLayer': layers.AFMLayer}):
        layer_test(layers.AFMLayer,
                   kwargs={'dropout_rate': 0.5},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
Exemplo n.º 20
0
def qt_InceptionV3(weights):
    with CustomObjectScope({'softmax': softmax}):
        json_string = open("/content/InceptionV3.json", "r").read()
        model = model_from_json(json_string)
        model.load_weights(weights)
        return model
Exemplo n.º 21
0
def test_PredictionLayer(task, use_bias):
    with CustomObjectScope({'PredictionLayer': layers.PredictionLayer}):
        layer_test(layers.PredictionLayer, kwargs={'task': task, 'use_bias': use_bias
                                                   }, input_shape=(BATCH_SIZE, 1))
Exemplo n.º 22
0
import tensorflow as tf
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.utils import CustomObjectScope


def Hswish(x):
    return x * tf.nn.relu6(x + 3) / 6


def relu6(x):
    return tf.nn.relu6(x)


if __name__ == "__main__":

    with CustomObjectScope({'Hswish': Hswish, 'relu6': relu6}):

        # 需要转换的h5模型
        h5file = 'trained_89_1530.h5'

        kerasmodel = load_model(h5file)
        tflite_model = tf.lite.TFLiteConverter.from_keras_model(kerasmodel)

        # 参数量化
        # tflite_model.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]

        # 整数量化
        # tflite_model.optimizations = [tf.lite.Optimize.DEFAULT]
        # tflite_model.representative_dataset = representative_data_gen

        model = tflite_model.convert()
Exemplo n.º 23
0
def test_PositionEncoding(pos_embedding_trainable, zero_pad):
    with CustomObjectScope({'PositionEncoding': sequence.PositionEncoding}):
        layer_test(sequence.PositionEncoding,
                   kwargs={'pos_embedding_trainable': pos_embedding_trainable, 'zero_pad': zero_pad},
                   input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE))
Exemplo n.º 24
0
import tensorflow.keras.backend as K

from tensorflow.python.keras.utils import CustomObjectScope
from os.path import join

QUANTIZE = True
custom_loss = K.categorical_crossentropy

# convert teacher
converter = tf.lite.TFLiteConverter.from_keras_model_file(
    join("Best", "teacher_model9278475.h5"))
tflite_model = converter.convert()
open(join("Best", "teacher.tflite"), "wb").write(tflite_model)

# convert student
with CustomObjectScope({'custom_loss': custom_loss}):
    converter = tf.lite.TFLiteConverter.from_keras_model_file(
        join("Best", "student_model2830427.h5"))
tflite_model = converter.convert()
open(join("Best", "student.tflite"), "wb").write(tflite_model)

# convert student
bs = 1
if QUANTIZE:
    with CustomObjectScope({'custom_loss': custom_loss}):
        converter = tf.lite.TFLiteConverter.from_keras_model_file(
            join("Best", "student_model2830427.h5"),
            input_shapes={'reshape_input': [bs, 2, 128]})
        converter.post_training_quantize = True
tflite_model = converter.convert()
open(join("Best", "student8-{0}.tflite".format(bs)), "wb").write(tflite_model)
Exemplo n.º 25
0
def test_SequencePoolingLayer(mode, supports_masking, input_shape):
    if version.parse(tf.__version__) >= version.parse('1.14.0') and mode != 'sum':  # todo check further version
        return
    with CustomObjectScope({'SequencePoolingLayer': sequence.SequencePoolingLayer}):
        layer_test(sequence.SequencePoolingLayer, kwargs={'mode': mode, 'supports_masking': supports_masking},
                   input_shape=input_shape, supports_masking=supports_masking)
Exemplo n.º 26
0
 def load_model(self):
     with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
         self.model = load_model(self.model_path)
     self.graph = get_default_graph()
Exemplo n.º 27
0
def test_KMaxPooling():
    with CustomObjectScope({'KMaxPooling': sequence.KMaxPooling}):
        layer_test(sequence.KMaxPooling, kwargs={'k': 3, 'axis': 1},
                   input_shape=(BATCH_SIZE, SEQ_LENGTH, EMBEDDING_SIZE, 2))
Exemplo n.º 28
0
def test_BilinearInteraction(bilinear_type):
    with CustomObjectScope({'BilinearInteraction':
                            layers.BilinearInteraction}):
        layer_test(layers.BilinearInteraction,
                   kwargs={'bilinear_type': bilinear_type},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
Exemplo n.º 29
0
def test_InnerProductLayer(reduce_sum):
    with CustomObjectScope({'InnerProductLayer': layers.InnerProductLayer}):
        layer_test(layers.InnerProductLayer,
                   kwargs={'reduce_sum': reduce_sum},
                   input_shape=[(BATCH_SIZE, 1, EMBEDDING_SIZE)] * FIELD_SIZE)
Exemplo n.º 30
0
def test_dice():
    with CustomObjectScope({'Dice': activation.Dice}):
        layer_test(activation.Dice, kwargs={},
                   input_shape=(2, 3))