Пример #1
0
with strategy.scope():
    max_seq_length = 128
    initializer = tf.keras.initializers.TruncatedNormal(
            stddev=bert_config.initializer_range)
    bert_encoder = bert.bert_models.get_transformer_encoder(
        bert_config, max_seq_length)

    input_word_ids = tf.keras.layers.Input(
      shape=(max_seq_length,), dtype=tf.int32, name='input_word_ids')
    input_mask = tf.keras.layers.Input(
      shape=(max_seq_length,), dtype=tf.int32, name='input_mask')
    input_type_ids = tf.keras.layers.Input(
      shape=(max_seq_length,), dtype=tf.int32, name='input_type_ids')

    bert_model = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_multi_cased_L-12_H-768_A-12/2",
                                trainable=True)
    #bert_model = hub.KerasLayer(hub_url_bert, trainable=True)
    pooled_output, seq_output = bert_model([input_word_ids, input_mask, input_type_ids])
    output1 = tf.keras.layers.Dropout(rate=bert_config.hidden_dropout_prob)(
      pooled_output)

    output1 = tf.keras.layers.Dense(
      2, kernel_initializer=initializer, name='output1')(
          output1)

    output2 = tf.keras.layers.Dropout(rate=bert_config.hidden_dropout_prob)(
      pooled_output)

    output2 = tf.keras.layers.Dense(
      3, kernel_initializer=initializer, name='output2')(
          output2)
    image = tf.image.resize(image, (IMAGE_RES, IMAGE_RES)) / 255.0
    return image, label


num_examples = info.splits['train'].num_examples

BATCH_SIZE = 32
IMAGE_RES = 224

train_batches = train_examples.cache().shuffle(
    num_examples // 4).map(format_image).batch(BATCH_SIZE).prefetch(1)
validation_batches = validation_examples.cache().map(format_image).batch(
    BATCH_SIZE).prefetch(1)

URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
feature_extractor = hub.KerasLayer(URL, input_shape=(IMAGE_RES, IMAGE_RES, 3))

feature_extractor.trainable = False

model = tf.keras.Sequential([feature_extractor, layers.Dense(2)])

model.summary()

model.compile(optimizer='adam',
              loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

EPOCHS = 3
history = model.fit(train_batches,
                    epochs=EPOCHS,
                    validation_data=validation_batches)
Пример #3
0
import math
import random
import re
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
from model import SentimentModel
import bert

# Tokenize
BertTokenizer = bert.bert_tokenization.FullTokenizer
bert_layer = hub.KerasLayer(
    "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1",
    trainable=False)
vocabulary_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
to_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = BertTokenizer(vocabulary_file, to_lower_case)

VOCAB_LENGTH = len(tokenizer.vocab)
EMB_DIM = 200
CNN_FILTERS = 100
DNN_UNITS = 256
OUTPUT_CLASSES = 6

DROPOUT_RATE = 0.2

NB_EPOCHS = 8

model = SentimentModel(vocabulary_size=VOCAB_LENGTH,
                       embedding_dimensions=EMB_DIM,
 def __init__(
         self,
         model_link="https://tfhub.dev/tensorflow/cord-19/swivel-128d/3"):
     self.embedder = hub.KerasLayer(model_link)
     print(f"Loaded pre-trained model {model_link} successfully!")
Пример #5
0
    directory=train_dir,
    shuffle=True,
    target_size=(IMG_SHAPE, IMG_SHAPE),
    class_mode='binary')

val_data_gen = validation_image_generator.flow_from_directory(
    batch_size=BATCH_SIZE,
    directory=validation_dir,
    shuffle=False,
    target_size=(IMG_SHAPE, IMG_SHAPE),
    class_mode='binary')

# getting MobileNet

URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
mobile_net = hub.KerasLayer(URL, input_shape=(IMG_SHAPE, IMG_SHAPE, 3))

mobile_net.trainable = False

model = tf.keras.models.Sequential([
    mobile_net,
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(3, activation='softmax')  # [0, 1] or [1, 0]
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()
Пример #6
0
train_data, validation_data, test_data = tfds.load(name="imdb_reviews",
                                                   split=('train[:60%]',
                                                          'train[60%:]',
                                                          'test'),
                                                   as_supervised=True)

train_examples_batch, train_labels_batch = next(iter(train_data.batch(10)))

#print(train_examples_batch)
#print(train_labels_batch)

embedding = "https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1"

hub_layer = hub.KerasLayer(embedding,
                           input_shape=[],
                           dtype=tf.string,
                           trainable=True)

# print(hub_layer(train_examples_batch[:3]))

model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1))

model.summary()

model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])
Пример #7
0
def squad_model(bert_config,
                max_seq_length,
                float_type,
                initializer=None,
                hub_module_url=None):
    """Returns BERT Squad model along with core BERT model to import weights.

  Args:
    bert_config: BertConfig, the config defines the core Bert model.
    max_seq_length: integer, the maximum input sequence length.
    float_type: tf.dtype, tf.float32 or tf.bfloat16.
    initializer: Initializer for weights in BertSquadLogitsLayer.
    hub_module_url: TF-Hub path/url to Bert module.

  Returns:
    Two tensors, start logits and end logits, [batch x sequence length].
  """
    unique_ids = tf.keras.layers.Input(shape=(1, ),
                                       dtype=tf.int32,
                                       name='unique_ids')
    input_word_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='input_ids')
    input_mask = tf.keras.layers.Input(shape=(max_seq_length, ),
                                       dtype=tf.int32,
                                       name='input_mask')
    input_type_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='segment_ids')

    if hub_module_url:
        core_model = hub.KerasLayer(hub_module_url, trainable=True)
        _, sequence_output = core_model(
            [input_word_ids, input_mask, input_type_ids])
        # Sets the shape manually due to a bug in TF shape inference.
        # TODO(hongkuny): remove this once shape inference is correct.
        sequence_output.set_shape(
            (None, max_seq_length, bert_config.hidden_size))
    else:
        core_model = modeling.get_bert_model(input_word_ids,
                                             input_mask,
                                             input_type_ids,
                                             config=bert_config,
                                             name='bert_model',
                                             float_type=float_type)
        # `BertSquadModel` only uses the sequnce_output which
        # has dimensionality (batch_size, sequence_length, num_hidden).
        sequence_output = core_model.outputs[1]

    if initializer is None:
        initializer = tf.keras.initializers.TruncatedNormal(
            stddev=bert_config.initializer_range)
    squad_logits_layer = BertSquadLogitsLayer(initializer=initializer,
                                              float_type=float_type,
                                              name='squad_logits')
    start_logits, end_logits = squad_logits_layer(sequence_output)

    squad = tf.keras.Model(inputs={
        'unique_ids': unique_ids,
        'input_ids': input_word_ids,
        'input_mask': input_mask,
        'segment_ids': input_type_ids,
    },
                           outputs=[unique_ids, start_logits, end_logits],
                           name='squad_model')
    return squad, core_model
Пример #8
0
def classifier_model(bert_config,
                     num_labels,
                     max_seq_length,
                     final_layer_initializer=None,
                     hub_module_url=None,
                     hub_module_trainable=True):
    """BERT classifier model in functional API style.

  Construct a Keras model for predicting `num_labels` outputs from an input with
  maximum sequence length `max_seq_length`.

  Args:
    bert_config: BertConfig or AlbertConfig, the config defines the core BERT or
      ALBERT model.
    num_labels: integer, the number of classes.
    max_seq_length: integer, the maximum input sequence length.
    final_layer_initializer: Initializer for final dense layer. Defaulted
      TruncatedNormal initializer.
    hub_module_url: TF-Hub path/url to Bert module.
    hub_module_trainable: True to finetune layers in the hub module.

  Returns:
    Combined prediction model (words, mask, type) -> (one-hot labels)
    BERT sub-model (words, mask, type) -> (bert_outputs)
  """
    if final_layer_initializer is not None:
        initializer = final_layer_initializer
    else:
        initializer = tf.keras.initializers.TruncatedNormal(
            stddev=bert_config.initializer_range)

    if not hub_module_url:
        bert_encoder = get_transformer_encoder(bert_config, max_seq_length)
        return models.BertClassifier(
            bert_encoder,
            num_classes=num_labels,
            dropout_rate=bert_config.hidden_dropout_prob,
            initializer=initializer), bert_encoder

    input_word_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='input_word_ids')
    input_mask = tf.keras.layers.Input(shape=(max_seq_length, ),
                                       dtype=tf.int32,
                                       name='input_mask')
    input_type_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='input_type_ids')
    bert_model = hub.KerasLayer(hub_module_url, trainable=hub_module_trainable)
    pooled_output, _ = bert_model([input_word_ids, input_mask, input_type_ids])
    output = tf.keras.layers.Dropout(
        rate=bert_config.hidden_dropout_prob)(pooled_output)

    output = tf.keras.layers.Dense(num_labels,
                                   kernel_initializer=initializer,
                                   name='output')(output)
    return tf.keras.Model(inputs={
        'input_word_ids': input_word_ids,
        'input_mask': input_mask,
        'input_type_ids': input_type_ids
    },
                          outputs=output), bert_model
    keras.layers.Lambda(lambda images: tf.cast(images, tf.float32)),
    keras.layers.Dense(10, activation="softmax")])
model.compile(loss="sparse_categorical_crossentropy",
              optimizer=keras.optimizers.SGD(lr=1e-3),
              metrics=["accuracy"])
model.fit(mnist_train, steps_per_epoch=60000 // 32, epochs=5)

# TensorFlow Hub

keras.backend.clear_session()
np.random.seed(42)
tf.random.set_seed(42)

import tensorflow_hub as hub

hub_layer = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim50/1",
                           output_shape=[50], input_shape=[], dtype=tf.string)

model = keras.Sequential()
model.add(hub_layer)
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.summary()

sentences = tf.constant(["It was a great movie", "The actors were amazing"])
embeddings = hub_layer(sentences)

embeddings

# Exercises
Пример #10
0
AUTOTUNE = tf.data.experimental.AUTOTUNE

train = train.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val = val.cache().prefetch(buffer_size=AUTOTUNE)
# %%
# PARTIE IMAGE NET
IMAGE_SHAPE = (224, 224)

classifier = tf.keras.Sequential([
    #hub.KerasLayer(classifier_model, input_shape=IMAGE_SHAPE+(3,))
    keras.layers.experimental.preprocessing.Rescaling(1. / 255,
                                                      input_shape=(img_size,
                                                                   img_size,
                                                                   3)),
    hub.KerasLayer(classifier_model),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(15)
])

classifier.compile(
    optimizer=keras.optimizers.Adam(),
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=["accuracy"])

#%%
root_logdir = os.path.join(os.curdir, "my_logs")


def get_run_logdir():
    run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S")
Пример #11
0
def main():
    url = "https://tfhub.dev/tensorflow/tfjs-model/deeplab/cityscapes/1/default/1"
    url = "D:/Academic/Computer_Science/Street_Evaluation/deeplabv3_1_default_1.tflite"
    # loaded = tf.saved_model.load("D:\ProgrammeFiles\deeplab_cityscapes_1_default_1.tar")
    deeplab_v3 = hub.KerasLayer(url, trainable=True)
Пример #12
0
    X_strt1d_test = strt1d_test["review"]
    Y_strt1d_test = np.array(strt1d_test['sentiment'].tolist())

    # ******************************************************* #
    #  NEURAL NETWORK LANGUAGE TOKEN EMBEDDINGS MODEL (NNLM)  #
    # ******************************************************* #

    nnlm_stats = Stats()
    # If the module doesn't download automatically, then please download it manualy via the curl request and provide
    # the hub.KerasLayer with the path to the module folder
    # curl -L "https://tfhub.dev/google/nnlm-en-dim128-with-normalization/2?tf-hub-format=compressed" | tar -zxvC tf_hub_modules/nnlm

    nnlm_model = tf.keras.Sequential([
        # hub.KerasLayer("https://tfhub.dev/google/nnlm-en-dim128-with-normalization/2", input_shape=[], dtype=tf.string, trainable=False),
        hub.KerasLayer("tf_hub_modules/nnlm",
                       input_shape=[],
                       dtype=tf.string,
                       trainable=False),
        tf.keras.layers.Dense(128, activation=tf.nn.relu, use_bias=True),
        tf.keras.layers.Dense(64, activation=tf.nn.relu, use_bias=True),
        tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)
    ])

    nnlm_model.compile(loss=losses.binary_crossentropy,
                       optimizer='adam',
                       metrics=['accuracy'])
    print(nnlm_model.summary())
    nnlm_model.fit(X_strt1d_train,
                   Y_strt1d_train,
                   batch_size=batch_size,
                   epochs=epochs,
                   validation_data=(X_strt1d_eval, Y_strt1d_eval),
import tensorflow_hub as hub
import tensorflow_datasets as tfds

# Split the training set into 60% and 40%, so we'll end up with 15,000 examples
# for training, 10,000 examples for validation and 25,000 examples for testing.
train_validation_split = tfds.Split.TRAIN.subsplit([6, 4])

(train_data,
 validation_data), test_data = tfds.load(name="imdb_reviews",
                                         split=(train_validation_split,
                                                tfds.Split.TEST),
                                         as_supervised=True)

handle = 'https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1'
hub_layer = hub.KerasLayer(handle,
                           input_shape=[],
                           dtype=tf.string,
                           trainable=True)

model = tf.keras.models.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

#model.summary()

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

model.fit(train_data.shuffle(1).batch(512),
          epochs=20,
Пример #14
0
    #print(i)
    df = df_list[i]
    for j in range(len(df)):
        #if(type(df['Utterance'][j]) == str and type(df['Type'][j]) == str):
        if (df['Num Type'][j] != 3):
            test_x.append(df['Utterance'][j])
            one_hot_label = [0, 0, 0]
            one_hot_index = int(df['Num Type'][j])
            one_hot_label[one_hot_index] = 1
            test_y.append(one_hot_label)
            #test_y.append(int(df['Num Type'][j]))

#print(train_y)
#print(np.array(train_y).shape)

bert_preprocess_model = hub.KerasLayer(
    'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3')
bert_model = hub.KerasLayer(
    'https://tfhub.dev/tensorflow/small_bert/bert_en_uncased_L-2_H-128_A-2/1')

encoder_inputs = bert_preprocess_model(train_x)
outputs = bert_model(encoder_inputs)
pooled_output = outputs["pooled_output"]
sequence_output = outputs["sequence_output"]

print(pooled_output)
#print(sequence_output)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(40, activation='sigmoid'))
model.add(tf.keras.layers.Dense(20, activation='sigmoid'))
model.add(tf.keras.layers.Dense(3, activation='softmax'))
Пример #15
0
import numpy as np
import os
import sys
sys.path.append(os.getcwd())
from utils.prepareGPU import prepareGPU
from utils.prepareCatDogDataset import prepareDataset, prepareTraingAndValSet
session = prepareGPU()
train_dir, val_dir, total_train, total_val = prepareDataset()

CLASSIFIER_URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"
IMAGE_RES = 224
BATCH_SIZE = 32

URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
all_layers_except_the_last = hub.KerasLayer(URL,
                                            input_shape=(IMAGE_RES, IMAGE_RES,
                                                         3))

all_layers_except_the_last.trainable = False

model = keras.Sequential(
    [all_layers_except_the_last,
     keras.layers.Dense(2, activation="softmax")])

model.summary()

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

train_data_gen, val_data_gen = prepareTraingAndValSet(BATCH_SIZE, IMAGE_RES)
Пример #16
0

# Pre-trained Model auswählen (aus TF-Hub)
model_name = "mobilenet_v2_100_224" 
model_handle_map = {"mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/feature_vector/4",}
model_handle = model_handle_map.get(model_name)
IMAGE_SIZE = (224, 224, 3)
BATCH_SIZE = 32
print(f"\nAusgewaehltes model: {model_name} : {model_handle}")
print(f"\nBild-Groesse {IMAGE_SIZE}")

# CNN zusammenstellen mit ANzahl Klassen wie im Datenset 
print("\nModell erstellen mit", model_handle)
model = tf.keras.Sequential([
    tf.keras.layers.InputLayer(input_shape=(IMAGE_SIZE)),
    hub.KerasLayer(model_handle, trainable=False),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Dense(len(class_names), 
                          kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE)
model.summary()

# Trainieren des Modells 
model.compile(
  optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), 
  loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True, label_smoothing=0.1),
  metrics=['accuracy'])

history = model.fit(
    train_ds,
Пример #17
0
        queries_docs[idd] = doc + max(
            0, args.num_eval_docs - len(doc)) * [('000000000', 'FAKE DOC', 0)]
    writer = tf.io.TFRecordWriter(args.output_folder + "/eval_ds.tf")
    map_ids_file = open(args.output_folder + "/query_doc_ids_eval.txt")
    for i, (query_id, docs) in tqdm(enumerate(queries_docs.items())):
        doc_ids, docs, labels = zip(*docs)
        query = query_map[query_id]
        write_to_tf_record(writer=writer,
                           tokenizer=tokenizer,
                           query=query,
                           docs=docs,
                           labels=labels,
                           ids_file=map_ids_file,
                           query_id=query_id,
                           doc_ids=doc_ids)
    map_ids_file.close()
    writer.close()


if __name__ == "__main__":
    print("downloading layer...")
    bert_layer = hub.KerasLayer(args.bert_model_hub, trainable=True)
    print("Layer downloaded...")
    vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
    do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
    tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case)

    create_dir(args.output_folder)
    convert_train_ds(tokenizer)
    convert_eval_ds(tokenizer)
plt.figure(figsize = (10, 9))
plt.subplots_adjust(hspace = 0.5)

for idx in range(30):

    plt.subplot(6, 5, idx + 1)
    plt.imshow(image_batch[idx])
    plt.title(predicted_class[idx])
    plt.axis('off')
    plt.suptitle('ImageNet Predictions')
plt.show()
'''

#Use a model without the top classification layer
feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=(224, 224, 3))
'''
FREEZE THE VARIABLES IN THE FEATURE EXTRACTOR LAYER, SO THAT THE TRAINING ONLY MODIFIES THE NEW CLASSIFIER LAYER
'''
feature_extractor_layer.trainable = False

#add a calssification head
model = tf.keras.Sequential(
    [feature_extractor_layer,
     layers.Dense(image_data.num_classes)])
model.summary()

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
Пример #19
0
        print(os.path.join(dirname, filename))

print(tf.__version__)
# Any results you write to the current directory are saved as output.

BASE_PATH = "/content/gdrive/My Drive/Colab Notebooks/Disaster/"

train =pd.read_csv(BASE_PATH + "train.csv")
train.head()

test =pd.read_csv(BASE_PATH + "test.csv")
test.head()

%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1"
bert_layer = hub.KerasLayer(module_url, trainable=True)

vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case)

text = "This is a Goat, and I am riding a Boat...."
tokenize_ = tokenizer.tokenize(text)
print("Text after tokenization: ")
print(tokenize_)
max_len = 25

text = tokenize_[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
Пример #20
0
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import PIL.Image

classifier_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/2"  #@param {type:"string"}

IMAGE_SHAPE = (224, 224)

classifier = tf.keras.Sequential(
    [hub.KerasLayer(classifier_url, input_shape=IMAGE_SHAPE + (3, ))])

inputs = np.empty((10, 224, 224, 3))

for i in range(0, 5):
    cat = PIL.Image.open(f'../data/cat.{i}.jpg').resize(IMAGE_SHAPE)
    dog = PIL.Image.open(f'../data/dog.{i}.jpg').resize(IMAGE_SHAPE)

    cat = np.array(cat) / 255.0
    dog = np.array(dog) / 255.0

    inputs[i] = cat
    inputs[i + 5] = dog

results = classifier.predict(inputs)

pred_classes = []
pred_labels = []

labels_path = tf.keras.utils.get_file(
    'ImageNetLabels.txt',
Пример #21
0
    def load_model(model_signature: str, **params) -> object:
        """
        NOTE: Keras常见陷阱:1.TF卷积核与Theano卷积核shape相同,加载时需用测试样本验证其表现,Keras无法区别
        :param model_signature:
        :param params:
        """
        model = None
        inputs, outputs = {}, {}   # {name: shape} dicts
        if model_signature == _ModelSignature.TFSavedModel.signature:
            import tensorflow as tf  # IMPROVE: check availability of ml backends
            # format_ = ModelManager._validate_format(params['format'], _ModelSignature.TFSavedModel)
            path = ModelManager._validate_path(params.get('path', None))
            model = tf.saved_model.load(path, params.get('tags', None))  # == core ==
            if params.get('signature_', None) is not None:
                model = model.signatures[params['signature_']]
            # TODO: append inputs, outputs spec to model object? so that predict() can adapt the fed inputs
            if hasattr(model, 'inputs') and hasattr(model, 'structured_outpus'):
                inputs = {model.inputs[0].name: model.inputs[0].shape}
                outputs = {'default': model.structured_outputs['default']}  # IMPROVE: iterate
            pass
        elif model_signature == _ModelSignature.TLDyamicModel.signature:
            params_model = Params(model_func=None, model_args=None, weights_path=None).update_to(params)
            import tensorflow as tf
            import os.path as osp
            if params_model.model_func is None:
                raise ValueError(f"model_func must be specified for a '{model_signature}' model")
            idx_last_sep = params_model.model_func.rfind('.')
            module = safe_import_module(params_model.model_func[:idx_last_sep])
            model_func = getattr(module, params_model.model_func[idx_last_sep+1:])
            model = model_func(**params_model.model_args)
            if params_model.weights_path is not None:
                weights_path = ModelManager._validate_path(params_model.weights_path)
                model.load_weights(weights_path, skip=True)
        elif model_signature == _ModelSignature.TFHub_KerasLayer.signature:
            import tensorflow_hub as tf_hub
            # format_ = ModelManager._validate_format(params['format'], _ModelSignature.TFSavedModel)
            path = ModelManager._validate_path(params.get('path', None))
            params_model = Params(input_shape=None, trainable=False).update_to(params)
            if params_model.input_shape.__len__() == 4:
                params_model.input_shape = params_model.input_shape[1:]
            # NOTE: it will be delayed-build pattern when `input_shape` is None. no weights info available until build.
            model = tf_hub.KerasLayer(path, input_shape=params_model.input_shape)
            model.trainable = params_model.trainable
            pass
        elif model_signature == _ModelSignature.KerasSequential.signature:
            # IMPROVE: check availability of ml backends
            from tensorflow.keras import Sequential, layers
            name = params['name']
            # IMPROVE:parse name -> layers, or use structural config for iteration
            if name == '{conv-pool}*2-flat-dense-drop-dense':
                # NOTE: only for _test_\TF_1x_to_2x_3, output is len=10 logits
                model = Sequential([
                    # NOTE: 1.TF2.x已无需限定Input层的维度,甚至各层间都能自动衔接
                    #      2.Conv层中无需设定上一层的(h,w),只需设定filter数、kernel维度、padding(使h,w保持)等
                    #      3.但若不指定input_shape,Optimizer将无法加载其之前被保存的参数,只能重新初始化
                    layers.Conv2D(32, (5, 5), strides=(1, 1), padding='same', activation='relu'),
                    layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                    layers.Conv2D(64, (5, 5), strides=(1, 1), padding='same', activation='relu'),
                    layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'),
                    layers.Flatten(),  # 下面的神经网络需要1维的数据
                    layers.Dense(1024, activation='relu'),
                    layers.Dropout(0.5),  # TODO: 关闭Dropout @evluate,predict
                    layers.Dense(10, activation='softmax')
                ])
            elif name == 'dense-dense_softmax':
                params_model = Params(embedding_size=1024, class_count=None).update_to(params)
                if params_model.class_count is None:
                    raise ValueError('class_count must be specified')
                model = Sequential([
                    layers.Dense(params_model.embedding_size, activation='relu'),
                    layers.Dense(params_model.class_count, activation='softmax')
                ])
                # TODO: need to return intermediate tf.Tensor required by embedding, loss calculation and evaluation.
            else:
                raise ValueError(f"Undefined model: {name}")
            pass
        elif model_signature == _ModelSignature.KerasModels_LoadModel.signature:
            import tensorflow as tf  # IMPROVE: check availability of ml backends
            format_ = ModelManager._validate_format(params['format'], _ModelSignature.KerasModels_LoadModel)
            params_model = Params(path='', path_formatted='').update_to(params)
            path = ModelManager._validate_path(params_model.path)
            model = tf.keras.models.load_model(path)        # == core ==
        elif model_signature == _ModelSignature.TF_ImportGraphDef.signature:
            import tensorflow as tf  # IMPROVE: check availability of ml backends
            format_ = ModelManager._validate_format(params['format'], _ModelSignature.TF_ImportGraphDef)
            params_model = Params(inputs='', outputs='').update_to(params)
            path = ModelManager._validate_path(params_model.path)

            # import PB model (frozen) in TF2.x. ref:https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt
            # ref:https://www.tensorflow.org/api_docs/python/tf/compat/v1/wrap_function
            def wrap_frozen_graph(pb_path, inputs, outputs, prefix=""):
                def _imports_graph_def():
                    tf.compat.v1.import_graph_def(graph_def, name=prefix)  # turn off the default prefix "import/"
                graph_def = tf.compat.v1.GraphDef()
                loaded = graph_def.ParseFromString(open(pb_path, 'rb').read())          # == core ==
                wrapped_import = tf.compat.v1.wrap_function(_imports_graph_def, [])     # == core ==
                import_graph = wrapped_import.graph
                return wrapped_import.prune(
                    tf.nest.map_structure(import_graph.as_graph_element, inputs),
                    tf.nest.map_structure(import_graph.as_graph_element, outputs))

            model = wrap_frozen_graph(path, inputs=params_model.inputs, outputs=params_model.outputs)
            test_img = tf.ones([1, 224, 224, 3], dtype=tf.float32)  # fixed shape is for test ONLY
            DEBUG(f"wrap_func test result: {model(test_img).shape}")
        else:
            raise ValueError(f"Unsupported model signature: {model_signature}")
        INFO(f"type of loaded model={type(model)}")
        INFO(f"  inputs={inputs}, outputs={outputs}")
        return model
Пример #22
0
           padding='same',
           kernel_initializer='he_uniform')(x)
block_3_output = add([x, block_2_output])

x = Conv2D(64, 3, activation='relu',
           kernel_initializer='he_uniform')(block_3_output)
x = GlobalAveragePooling2D()(x)
x = Dense(256, activation='relu', kernel_initializer='he_uniform')(x)
x = Dropout(0.5)(x)
outputs = Dense(2)(x)

model_cnnB = tf.keras.Model(inputs, outputs, name='toy_resnet')

resnetv2 = tf.keras.Sequential([
    hub.KerasLayer(
        "https://tfhub.dev/google/imagenet/resnet_v2_101/feature_vector/4",
        trainable=True,
        arguments=dict(batch_norm_momentum=0.99)),  # Can be True, see below.
    tf.keras.layers.Dense(2, activation='softmax')
])
resnetv2.build([None, 100, 100, 3])  # Batch input shape.

inceptionv3nat = tf.keras.Sequential([
    hub.KerasLayer(
        "https://tfhub.dev/google/inaturalist/inception_v3/feature_vector/4",
        trainable=True,
        arguments=dict(batch_norm_momentum=0.99)),  # Can be True, see below.
    tf.keras.layers.Dense(2, activation='softmax')
])
inceptionv3nat.build([None, 100, 100, 3])  # Batch input shape.

inceptionv3 = tf.keras.Sequential([
Пример #23
0
def _create_embedding_layer(hub_url):
    return hub.KerasLayer(hub_url,
                          output_shape=[128],
                          input_shape=[],
                          dtype=tf.string)
Пример #24
0
def analyze_images(images,
                   model_name,
                   layer_name=None,
                   pooling=None,
                   do_crop=False,
                   subsampling=None,
                   do_pca=False):
    if model_name == 'color_lab':
        return analyze_images_colors(images,
                                     colorspace='lab',
                                     subsampling=subsampling)
    elif model_name == 'color' or model_name == 'color_rgb':
        return analyze_images_colors(images,
                                     colorspace='rgb',
                                     subsampling=subsampling)

    num_images = len(images)
    include_top = (layer_name is not None)

    model_lookup_table = {
        'densenet121': {
            'model_class': keras.applications.densenet.DenseNet121,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.densenet.preprocess_input
        },
        'densenet169': {
            'model_class': keras.applications.densenet.DenseNet169,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.densenet.preprocess_input
        },
        'densenet201': {
            'model_class': keras.applications.densenet.DenseNet201,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.densenet.preprocess_input
        },
        'inceptionresnetv2': {
            'model_class':
            keras.applications.inception_resnet_v2.InceptionResNetV2,
            'input_shape': (299, 299),
            'preprocess_input':
            keras.applications.inception_resnet_v2.preprocess_input
        },
        'inceptionv3': {
            'model_class': keras.applications.inception_v3.InceptionV3,
            'input_shape': (299, 299),
            'preprocess_input':
            keras.applications.inception_v3.preprocess_input
        },
        'resnet50': {
            'model_class': keras.applications.resnet.ResNet50,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.resnet.preprocess_input
        },
        'resnet101': {
            'model_class': keras.applications.resnet.ResNet101,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.resnet.preprocess_input
        },
        'resnet152': {
            'model_class': keras.applications.resnet.ResNet152,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.resnet.preprocess_input
        },
        'vgg16': {
            'model_class': keras.applications.vgg16.VGG16,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.vgg16.preprocess_input
        },
        'vgg19': {
            'model_class': keras.applications.vgg19.VGG19,
            'input_shape': (224, 224),
            'preprocess_input': keras.applications.vgg19.preprocess_input
        },
        'inceptionv3': {
            'model_class': keras.applications.xception.Xception,
            'input_shape': (299, 299),
            'preprocess_input': keras.applications.xception.preprocess_input
        },
    }

    is_bit = False
    is_clip = False
    if model_name.startswith("bit"):
        model_url = f"https://tfhub.dev/google/{model_name}/1"
        input_shape = None
        preprocess_input = bit_preprocess_image
        model = hub.KerasLayer(model_url)
        is_bit = True
    elif model_name.startswith("clip"):
        import clip
        import torch
        from torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalize

        model_type = "ViT-B/32"
        if len(model_name) > 4:
            parts = model_name.split(":")
            model_type = parts[1]
        model, preprocess = clip.load(model_type)
        print(preprocess)
        input_size = model.input_resolution.item()
        input_shape = (input_size, input_size)
        preprocess_input = None
        is_clip = True
    elif model_name in model_lookup_table:
        model_class = model_lookup_table[model_name]['model_class']
        input_shape = model_lookup_table[model_name]['input_shape']
        preprocess_input = model_lookup_table[model_name]['preprocess_input']
        model = model_class(weights='imagenet', include_top=include_top)
    else:
        print("Error: model {} not found".format(model_name))
        sys.exit(1)

    if layer_name is None:
        feat_extractor = model
    elif layer_name == "show" or layer_name == "list":
        for i, layer in enumerate(model.layers):
            print("{} layer {:03d}: {}".format(model_name, i, layer.name))
        sys.exit(0)
    else:
        feat_extractor = Model(inputs=model.input,
                               outputs=model.get_layer(layer_name).output)

    # analyze images and grab activations
    activations = []
    for idx in tqdm(range(len(images))):
        file_path = images[idx]
        img = get_image(file_path, input_shape, do_crop, is_bit)
        if img is not None:
            # preprocess
            if preprocess_input is not None:
                img = preprocess_input(img)
            # print("getting activations for %s %d/%d" % (file_path,idx,num_images))
            if is_bit:
                acts = model(img)[0].numpy()
            elif is_clip:
                batch_item = img[0] / 255.0
                transform2 = Compose([
                    ToTensor(),
                    Normalize((0.48145466, 0.4578275, 0.40821073),
                              (0.26862954, 0.26130258, 0.27577711)),
                ])
                zimages = []
                im = transform2(batch_item)
                zimages.append(im)
                im_batch = torch.stack(zimages)
                acts = model.encode_image(im_batch)[0].detach().cpu().numpy()
            else:
                acts = feat_extractor.predict(img)[0]
            if len(activations) == 0:
                print("Collecting vectors of size {}".format(
                    acts.flatten().shape))
            activations.append(acts.flatten())
    # run PCA firt
    features = np.array(activations)
    if do_pca:
        print("Running PCA on features: {}".format(features.shape))
        pca = PCA(n_components=300)
        pca.fit(features)
        pca_features = pca.transform(features)
        return np.asarray(pca_features)
    else:
        return features
Пример #25
0
def classifier_model(bert_config,
                     float_type,
                     num_labels,
                     max_seq_length,
                     final_layer_initializer=None,
                     hub_module_url=None):
    """BERT classifier model in functional API style.

  Construct a Keras model for predicting `num_labels` outputs from an input with
  maximum sequence length `max_seq_length`.

  Args:
    bert_config: BertConfig, the config defines the core BERT model.
    float_type: dtype, tf.float32 or tf.bfloat16.
    num_labels: integer, the number of classes.
    max_seq_length: integer, the maximum input sequence length.
    final_layer_initializer: Initializer for final dense layer. Defaulted
      TruncatedNormal initializer.
    hub_module_url: (Experimental) TF-Hub path/url to Bert module.

  Returns:
    Combined prediction model (words, mask, type) -> (one-hot labels)
    BERT sub-model (words, mask, type) -> (bert_outputs)
  """
    input_word_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='input_word_ids')
    input_mask = tf.keras.layers.Input(shape=(max_seq_length, ),
                                       dtype=tf.int32,
                                       name='input_mask')
    input_type_ids = tf.keras.layers.Input(shape=(max_seq_length, ),
                                           dtype=tf.int32,
                                           name='input_type_ids')
    if hub_module_url:
        bert_model = hub.KerasLayer(hub_module_url, trainable=True)
        pooled_output, _ = bert_model(
            [input_word_ids, input_mask, input_type_ids])
    else:
        bert_model = modeling.get_bert_model(input_word_ids,
                                             input_mask,
                                             input_type_ids,
                                             config=bert_config,
                                             float_type=float_type)
        pooled_output = bert_model.outputs[0]

    if final_layer_initializer is not None:
        initializer = final_layer_initializer
    else:
        initializer = tf.keras.initializers.TruncatedNormal(
            stddev=bert_config.initializer_range)

    output = tf.keras.layers.Dropout(
        rate=bert_config.hidden_dropout_prob)(pooled_output)
    output = tf.keras.layers.Dense(num_labels,
                                   kernel_initializer=initializer,
                                   name='output',
                                   dtype=float_type)(output)
    return tf.keras.Model(inputs={
        'input_word_ids': input_word_ids,
        'input_mask': input_mask,
        'input_type_ids': input_type_ids
    },
                          outputs=output), bert_model
Пример #26
0
    for n in range(25):
        ax = plt.subplot(5, 5, n + 1)
        plt.imshow(batch[0][n])
        plt.title(class_names[batch[1][n].numpy()].title())
        plt.axis('off')


show_batch(batch)

# In[7]:

# building the model
# InceptionV3 model & pre-trained weights
module_url = "https://tfhub.dev/google/tf2-preview/inception_v3/feature_vector/4"
m = tf.keras.Sequential([
    hub.KerasLayer(module_url, output_shape=[2048], trainable=False),
    tf.keras.layers.Dense(1, activation="sigmoid")
])

m.build([None, 299, 299, 3])
m.compile(loss="binary_crossentropy",
          optimizer=optimizer,
          metrics=["accuracy"])
m.summary()

# In[9]:

# model_name = f"benign-vs-malignant_{batch_size}_{optimizer}"
# tensorboard = tf.keras.callbacks.TensorBoard(log_dir=os.path.join("logs", model_name))
# # saves model checkpoint whenever we reach better weights
# modelcheckpoint = tf.keras.callbacks.ModelCheckpoint(model_name + "_{val_loss:.3f}.h5", save_best_only=True, verbose=1)
Пример #27
0
  else:
    data_gen = ImageDataGenerator(rescale=1./255)

  data = data_gen.flow_from_directory(
      data_folder,
      target_size=IMG_SIZE,
      batch_size=BATCH_SIZE,
      class_mode="binary",
  )
  return data

ds_train = gen_new_data(TRAIN_DIR, augmented=True)
ds_val = gen_new_data(VAL_DIR, augmented=False)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, InputLayer

mobilenet_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4" 

mobile = Sequential()
mobile.add(InputLayer(input_shape=INP_SHAPE))
mobile.add(hub.KerasLayer(mobilenet_url, trainable=False))
mobile.add(Dense(1, activation="sigmoid"))
mobile.summary()

mobile.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['acc'])

mobile.fit(ds_train, epochs=3, verbose=1, validation_data=ds_val)
Пример #28
0
while i < len(data['audience']):
    a = data['audience'][i]
    if a == '':
        data['audience'][i] = ['Anyone']
    else:
        data['audience'][i] = a.split(';')
    i += 1

data = field_first_to_row(data)

titles = []
desc = []
i = 0
while i < len(data):
    titles.append(data[i]['title'])
    desc.append(data[i]['desc_clean'])
    i += 1

module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
embed = hub.KerasLayer(module_url)
embeddings_desc = embed(desc)
embeddings_titles = embed(titles)

i = 0
while i < len(data):
    data[i]['embedding_desc'] = embeddings_desc[i]
    data[i]['embedding_title'] = embeddings_titles[i]
    i += 1

pickle.dump(data, open("../processed/courses.p", "wb"))
Пример #29
0
    'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3',
    'experts_wiki_books':
    'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3',
    'talking-heads_base':
    'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3',
}

tfhub_handle_encoder = map_name_to_handle[bert_model_name]
tfhub_handle_preprocess = map_model_to_preprocess[bert_model_name]

print(f'BERT model selected           : {tfhub_handle_encoder}')
print(f'Preprocess model auto-selected: {tfhub_handle_preprocess}')

# 전처리 모델

bert_preprocess_model = hub.KerasLayer(tfhub_handle_preprocess)

text_test = ['this is such an amazing movie!']
text_preprocessed = bert_preprocess_model(text_test)

print(f'Keys       : {list(text_preprocessed.keys())}')
print(f'Shape      : {text_preprocessed["input_word_ids"].shape}')
print(f'Word Ids   : {text_preprocessed["input_word_ids"][0, :12]}')
print(f'Input Mask : {text_preprocessed["input_mask"][0, :12]}')
print(f'Type Ids   : {text_preprocessed["input_type_ids"][0, :12]}')

# bert 모델 사용

bert_model = hub.KerasLayer(tfhub_handle_encoder)

bert_results = bert_model(text_preprocessed)
Пример #30
0
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Conv2D(filters=32, kernel_size=(4, 4), input_shape=(128, 128, 3),activation='relu'))
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Conv2D(filters=16, kernel_size=(3, 3), input_shape=(128, 128, 3), activation='relu'))
    model.add(MaxPool2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(256, activation='relu'))
    model.add(Dense(128, activation='relu'))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(16, activation='relu'))
    model.add(Dense(2, activation='softmax'))
    '''

    model = tf.keras.Sequential([
        hub.KerasLayer(
            "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4",
            output_shape=[1280],
            trainable=False),  # Can be True, see below.
        tf.keras.layers.Dense(2, activation='softmax')
    ])
    model.build([4, 224, 224, 3])

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    # see the summary of the model structure
    print(model.summary())

    # adding early stop
    from tensorflow.keras.callbacks import EarlyStopping