Example #1
0
File: GAN.py Project: mmyeong/Ai
test_images = test_images.reshape(test_images.shape[0], 28, 28,
                                  1).astype('float32')
test_images = (test_images - 127.5) / 127.5

BUFFER_SIZE = 60000
BATCH_SIZE = 128
train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(
    BUFFER_SIZE).batch(BATCH_SIZE)
train_dataset = tf.data.Dataset.from_tensor_slices(train_images).shuffle(
    BUFFER_SIZE).batch(BATCH_SIZE)

# Generator 모델 작성
# 모델은 Noise로 부터 원래의 데이터를 생성해내는 모델
# Input 100차원의 noise
# Output Mnist 이미지 크기인 28*28
inputs = keras.Input(shape=(100, ))
x = inputs
x = layers.Dense(256)(x)
x = layers.LeakyReLU()(x)
x = layers.Dense(28 * 28, activation='tanh')(x)
outputs = layers.Reshape((28, 28))(x)

G = keras.Model(inputs, outputs)
G.summary()

# Discriminaotr 모델 작성
# 모델 이미지 실제 데이터인지 만들어진 데이터인지 판별
# Input Mnist 이미지
# Output 실제 데이터 일 확률 (0~1 사이의 값)
inputs = keras.Input(shape=(28, 28))
x = layers.Flatten()(inputs)
Example #2
0
scaler = StandardScaler()
scaler.fit(raw_features)
scaled_features = scaler.transform(raw_features)

# Save scaler
joblib.dump(scaler, "model/scaler")

# split to train and test sets
X_train, X_test, y_train, y_test = train_test_split(scaled_features,
                                                    labels,
                                                    test_size=0.3,
                                                    random_state=101)

# Sequential model with 4 layers
model = keras.models.Sequential(name="MPG_predictor")
model.add(keras.Input(shape=(scaled_features.shape[1], )))
model.add(layers.Dense(scaled_features.shape[1], activation="relu"))
model.add(layers.Dense(5, activation="relu"))
model.add(layers.Dense(3, activation="relu"))
model.add(layers.Dense(1))

# Compile and fit model
model.compile(loss='mean_squared_error')
model.fit(X_train, y_train, epochs=200)  # verbose=0 jak ma być cicho

# Test model
mae = round(mean_absolute_error(y_test, model.predict(X_test)), 2)

# Save model
model.save('model/')
    test_ds = test_ds.prefetch(100)

    return train_ds, val_ds, test_ds


if __name__ == "__main__":
    start_time = time.time()

    # Hyper Parameters
    RANGE = len(ALL_SERIAL_NUMBERS)
    DROPOUT = 0.5  # [0,1], the chance to drop an input
    set_seeds(1337)

    train_ds, val_ds, test_ds = get_shuffled_and_windowed_from_pregen_ds()

    inputs = keras.Input(shape=(2, ORIGINAL_PAPER_SAMPLES_PER_CHUNK))

    x = keras.layers.Convolution1D(filters=50,
                                   kernel_size=7,
                                   strides=1,
                                   activation="relu",
                                   kernel_initializer='glorot_uniform',
                                   data_format="channels_first",
                                   name="classifier_3")(inputs)

    x = keras.layers.Convolution1D(filters=50,
                                   kernel_size=7,
                                   strides=2,
                                   activation="relu",
                                   kernel_initializer='glorot_uniform',
                                   data_format="channels_first",
Example #4
0
    def fcnn(
        self,
        tasknum=0,
        lr=0.5,
        lrfactor=0.2,
        mom=0.1,
        epochs=5
    ):  #lr=learning rate, lrd=learning rate decay, mom=momentum,tasknum=what to classify
        self.tasknum = tasknum
        self.lr = lr
        self.lrfactor = lrfactor
        self.mom = mom
        self.epochs = epochs

        #initializing values
        self.clean = cleandata()
        self.ytrain = self.clean.training(self.ytrain[:, self.tasknum + 1])
        self.yval = self.clean.training(self.yval[:, self.tasknum + 1])
        self.yvaltruth, self.cm_plot_labels = self.clean.cmatrix(
            self.yvaltruth[:, self.tasknum + 1])

        #network set-up
        model = keras.Sequential()
        model.add(keras.Input(shape=(1024, )))
        model.add(layers.Dense(1024, activation="tanh"))
        model.add(layers.Dense(512, activation="sigmoid"))
        model.add(layers.Dense(100, activation="relu"))
        if self.tasknum == 0:  #classify based on age
            model.add(layers.Dense(9, activation="softmax"))
        elif self.tasknum == 1:  #classify based on gender
            model.add(layers.Dense(2, activation="softmax"))
        else:  #classify based on race
            model.add(layers.Dense(7, activation="softmax"))
        model.summary()

        model.compile(
            optimizer=keras.optimizers.SGD(
                learning_rate=self.lr,
                momentum=self.mom),  #default lr=0.01, default mom=0.0
            loss=keras.losses.CategoricalCrossentropy(),
            metrics=[keras.metrics.CategoricalAccuracy()],
        )

        #training and validating
        print('training and validating')
        reduce_lr = keras.callbacks.ReduceLROnPlateau(
            monitor='val_loss',
            factor=self.lrfactor,
            patience=1,
            min_lr=0.0001,
        )
        history = model.fit(
            self.xtrain,
            self.ytrain,
            validation_split=0.2,
            epochs=self.epochs,
            callbacks=[reduce_lr]
        )  #if you don't specify a batch size, it uses 32 for mini-batch GD
        #testing
        print('evalauting')
        results = model.evaluate(self.xval, self.yval)
        print('predicting')
        #predicting
        predictions = model.predict_classes(self.xval)

        #plotting/final results summary, from https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/
        plt.plot(history.history['categorical_accuracy'])
        plt.plot(history.history['val_categorical_accuracy'])
        if self.tasknum == 0:  #classify based on age
            plt.title('model accuracy for age classification')
        elif self.tasknum == 1:  #classify based on gender
            plt.title('model accuracy for gender classification')
        else:  #classify based on race
            plt.title('model accuracy for race classification')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()

        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        if self.tasknum == 0:  #classify based on age
            plt.title('model loss for age classification')
        elif self.tasknum == 1:  #classify based on gender
            plt.title('model loss for gender classification')
        else:  #classify based on race
            plt.title('model loss for race classification')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()
        trainingloss = history.history['loss']
        validationloss = history.history['val_loss']
        print('final validation accuracy is %f' % results[1])

        confmatrix = sklearn.metrics.confusion_matrix(self.yvaltruth,
                                                      predictions)
        self.tasktypes = [
            'age confusion matrix', 'Gender confusion matrix',
            'race confusion matrix'
        ]
        self.tasktypes = self.tasktypes[self.tasknum]
        cmdisp = sklearn.metrics.ConfusionMatrixDisplay(
            confusion_matrix=confmatrix, display_labels=self.cm_plot_labels)
        cmdisp.plot()
        plt.show()
        return [trainingloss, validationloss, history, results, confmatrix]
Example #5
0
print(x_test.shape[0], "test samples")

input_shape = (28, 28, 1)

# Convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# Check the variable type
print(type(y_train), type(y_test))

# Model Selection
# Model Preparation
with strategy.scope():
    model = keras.Sequential([
        keras.Input(shape=input_shape),
        layers.Conv2D(32, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
        layers.MaxPooling2D(pool_size=(2, 2)),
        layers.Flatten(),
        layers.Dropout(0.5),
        layers.Dense(num_classes, activation="softmax"),
    ])
    model.compile(loss="categorical_crossentropy",
                  optimizer="adam",
                  metrics=["accuracy"])

model.summary()

# Model Training
Example #6
0
### Create a sampling layer ###############
class Sampling(layers.Layer):
    """Uses (z_mean, z_log_var) to sample z, the vector encoding a digit."""
    def call(self, inputs):
        z_mean, z_log_var = inputs
        batch = tf.shape(z_mean)[0]
        dim = tf.shape(z_mean)[1]
        epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
        return z_mean + tf.exp(0.5 * z_log_var) * epsilon


latent_dim = 2

### Build the encoder ############
encoder_inputs = keras.Input(shape=(28, 28, 1))
x = layers.Conv2D(32, 3, activation="relu", strides=2,
                  padding="same")(encoder_inputs)
x = layers.Conv2D(64, 3, activation="relu", strides=2, padding="same")(x)
x = layers.Flatten()(x)
x = layers.Dense(16, activation="relu")(x)
z_mean = layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(encoder_inputs, [z_mean, z_log_var, z], name="encoder")
encoder.summary()

### Build the decoder #############
latent_inputs = keras.Input(shape=(latent_dim, ))
x = layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
x = layers.Reshape((7, 7, 64))(x)
Example #7
0
Now, if you want very low-level control over training & evaluation, you should write
your own training & evaluation loops from scratch. This is what this guide is about.
"""
"""
## Using the `GradientTape`: a first end-to-end example

Calling a model inside a `GradientTape` scope enables you to retrieve the gradients of
the trainable weights of the layer with respect to a loss value. Using an optimizer
instance, you can use these gradients to update these variables (which you can
retrieve using `model.trainable_weights`).

Let's consider a simple MNIST model:

"""

inputs = keras.Input(shape=(784, ), name="digits")
x1 = layers.Dense(64, activation="relu")(inputs)
x2 = layers.Dense(64, activation="relu")(x1)
outputs = layers.Dense(10, name="predictions")(x2)
model = keras.Model(inputs=inputs, outputs=outputs)
"""
Let's train it using mini-batch gradient with a custom training loop.

First, we're going to need an optimizer, a loss function, and a dataset:
"""

# Instantiate an optimizer.
optimizer = keras.optimizers.SGD(learning_rate=1e-3)
# Instantiate a loss function.
loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
Example #8
0
def get_model(numb_faceid):
    # make share weight net first
    vgg16 = tf.keras.applications.VGG16(
        include_top=False,
        weights="imagenet",
    )
    last_layer = vgg16.get_layer('block5_pool')  # remove classify layer at top
    last_output = last_layer.output
    x = keras.layers.Flatten()(last_output)
    share_weight_net = keras.Model(vgg16.input, x)
    print(share_weight_net.summary())

    # input1 for anti spoofing
    as_input = keras.Input(shape=(224, 224, 3), name="as_input")
    as_flatten_1 = share_weight_net(as_input)
    as_fc = keras.Sequential([
        keras.layers.Dense(4096, activation="relu", name="as_fc1"),
        keras.layers.Dense(4096, activation="relu", name="as_fc2"),
    ])
    as_fc_i1 = as_fc(as_flatten_1)
    as_output = keras.layers.Dense(2, name="as_output",
                                   activation='softmax')(as_fc_i1)

    # input2 for anti spoofing
    lpc_input_1 = keras.Input(shape=(224, 224, 3), name="lpc_input_1")
    lpc_flatten_1 = share_weight_net(lpc_input_1)
    lpc_fc_o1 = as_fc(lpc_flatten_1)

    lpc_input_2 = keras.Input(shape=(224, 224, 3), name="lpc_input_2")
    lpc_flatten_2 = share_weight_net(lpc_input_2)
    lpc_fc_o2 = as_fc(lpc_flatten_2)

    lpc_layer = keras.layers.Lambda(lambda x: tf.math.square(x[0] - x[1]),
                                    name='lpc')
    lpc = lpc_layer([lpc_fc_o1, lpc_fc_o2])

    # second branch for face recognition
    fr_input = keras.Input(shape=(224, 224, 3), name="facerecog_input")
    fr_flatten = share_weight_net(fr_input)
    fr_fc = keras.Sequential([
        keras.layers.Dense(4096, activation="relu", name="fr_fc1"),
        keras.layers.Dense(4096, activation="relu", name="fr_fc2"),
    ])
    fr_fc_i = fr_fc(fr_flatten)
    fr_output = keras.layers.Dense(numb_faceid,
                                   name="fr_output",
                                   activation='softmax')(fr_fc_i)

    model = keras.Model(
        inputs=[as_input, lpc_input_1, lpc_input_2, fr_input],
        outputs=[as_output, lpc, fr_output],
    )
    # print(model.summary())

    model.compile(optimizer='adam',
                  loss={
                      "as_output":
                      keras.losses.SparseCategoricalCrossentropy(),
                      "lpc": tpc_loss,
                      "fr_output":
                      keras.losses.SparseCategoricalCrossentropy(),
                  },
                  loss_weights=[1, 2.5 * math.exp(-5), 0.1],
                  metrics=['accuracy'])

    # dot_img_file = 'model_graph.png'
    # keras.utils.plot_model(model, to_file=dot_img_file, show_shapes=True)
    return model
Example #9
0
def unet_with_resize(input_shape=(300, 300, 3), num_classes = 2):

    contraction_conv2d_out_shapes = [[64, 64], [128, 128], [256, 256], [512, 512]]
    bottleneck_conv2d_out_shapes = [1024, 1024]
    expansion_conv2d_out_shapes = [[512, 512], [256, 256], [128, 128]]
    resize_coeff = 5

    unet_input = keras.Input(shape=input_shape)

    x = unet_input

    # contraction blocks
    contraction_feature_maps = []
    contraction_counter = 1
    conv_counter = 1
    for cur_conv_block in contraction_conv2d_out_shapes:
        for cur_conv in cur_conv_block:
            x = keras.layers.Conv2D(cur_conv, 3, padding="valid", activation="relu",
                                    name=f"contraction{contraction_counter}_conv{conv_counter}")(x)
            x = keras.layers.BatchNormalization(name=f"contraction{contraction_counter}_batchnorm{conv_counter}")(x)
            conv_counter += 1
        contraction_feature_maps.append(x)
        x = keras.layers.MaxPool2D(pool_size=(2, 2))(x)
        contraction_counter += 1

    # bottleneck_block
    conv_counter = 1
    for cur_bottleneck_conv in bottleneck_conv2d_out_shapes:
        x = keras.layers.Conv2D(cur_bottleneck_conv, 3, padding="valid", activation="relu",
                                name=f"bottleneck_conv{conv_counter}")(x)
        x = keras.layers.BatchNormalization(name=f"bottleneck_batchnorm{conv_counter}")(x)
        conv_counter += 1
    x = keras.layers.UpSampling2D(size=(2, 2), name = "bottleneck_upsampling")(x)
    x = keras.layers.Conv2D(x.shape[-1] // 2, padding="same", kernel_size=(2, 2), name = "bottleneck_upconv")(x)

    # expansion blocks
    expansion_counter = 1
    conv_counter = 1
    for cur_conv_block in expansion_conv2d_out_shapes:
        residual = contraction_feature_maps.pop()
        height_diff = residual.shape[1] - x.shape[1]
        width_diff = residual.shape[1] - x.shape[1]

        # DEBUG
        #print("width_diff ", width_diff)

        tf.debugging.assert_greater(height_diff, 0)
        tf.debugging.assert_greater(width_diff, 0)

        residual = keras.layers.Cropping2D(cropping=((int(np.ceil(height_diff / 2)), int(np.floor(height_diff / 2))),
                                                     (int(np.ceil(width_diff / 2)), int(np.floor(width_diff / 2)))),
                                           name=f"expansion{expansion_counter}_crop")(residual)

        x = keras.layers.Concatenate(name=f"expansion{expansion_counter}_concat")([residual, x])
        for cur_conv in cur_conv_block:
            x = keras.layers.Conv2D(cur_conv, 3, padding="valid", activation="relu",
                                    name=f"expansion{expansion_counter}_conv{conv_counter}")(x)
            x = keras.layers.BatchNormalization(name=f"expansion{expansion_counter}_batchnorm{conv_counter}")(x)
            conv_counter += 1

        if expansion_counter != len(expansion_conv2d_out_shapes):
            x = keras.layers.UpSampling2D(size=(2, 2), name=f"expansion{expansion_counter}_upsampling")(x)
            x = keras.layers.Conv2D(x.shape[-1] // 2, kernel_size=(2, 2), padding="same", name=f"expansion{expansion_counter}_upconv")(x)

        expansion_counter += 1

    # resize layer
    x = keras.layers.experimental.preprocessing.Resizing(x.shape[1] * resize_coeff, x.shape[2] * resize_coeff, name="resize")(x)

    # final layer
    output = keras.layers.Conv2D(num_classes, kernel_size=(1, 1), strides = (1, 1), padding="valid", name="final_layer")(x)

    return keras.Model(unet_input, output)
Example #10
0
def directCNN(nCategories, inputShape, name="directCNN"):
    inputs = keras.Input(shape=inputShape)
    # dim Input:(16000, 1)

    #regu=regularizers.l2(1e-5)

    x = layers.Conv1D(filters=16,
                      kernel_size=64,
                      strides=2,
                      activation='relu',
                      padding='valid',
                      kernel_initializer="he_normal")(inputs)
    #kernel_regularizer=regu)(inputs)
    # dim: (7969,16)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling1D(pool_size=8, strides=8)(x)
    # dim: (996,16)
    x = layers.Conv1D(filters=32,
                      kernel_size=32,
                      strides=2,
                      activation='relu',
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    #kernel_regularizer=regu)(x)
    # dim: (483,32)
    x = layers.BatchNormalization()(x)
    x = layers.MaxPooling1D(pool_size=8, strides=8)(x)
    # dim: (60,32)
    x = layers.Conv1D(filters=64,
                      kernel_size=16,
                      strides=2,
                      activation='relu',
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    #kernel_regularizer=regu)(x)
    # dim: (23,64)
    x = layers.BatchNormalization()(x)
    x = layers.Conv1D(filters=128,
                      kernel_size=8,
                      strides=2,
                      activation='relu',
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    #kernel_regularizer=regu)(x)
    # dim: (8,128)
    x = layers.BatchNormalization()(x)

    x = layers.Flatten()(x)

    x = layers.Dense(128, activation='relu', kernel_initializer="he_normal")(x)
    #kernel_regularizer=regu)(x)
    x = layers.Dropout(rate=0.25)(x)

    x = layers.Dense(64, activation='relu', kernel_initializer="he_normal")(x)
    #kernel_regularizer=regu)(x)
    x = layers.Dropout(rate=0.25)(x)

    output = layers.Dense(nCategories,
                          activation="softmax",
                          kernel_initializer="glorot_uniform")(x)
    #kernel_regularizer=regu)(x)

    model = keras.Model(inputs=inputs, outputs=output, name=name)

    return model
Example #11
0
from tensorflow import keras
import numpy as np
from tensorflow.python.keras.layers import TextVectorization

from DataProcessor import DataProcessor
import json

model = keras.models.load_model('model4')

string_input = keras.Input(shape=(1, ), dtype="string")
vectorizer = TextVectorization(output_sequence_length=150)
x = vectorizer(string_input)
preds = model(x)
end_to_end_model = keras.Model(string_input, preds)

test_sample = [
    'Baile W, Buckman R, Lenzi R, Glober G, Beale E, Kudelka A. SPIKES—A Six-Step Protocol for Delivering Bad News: Application to the Patient with Cancer. The Oncologist. 2000;5(4):302-11.',
    'Patel K, Tatham K. Complete OSCE Skills for Medical and Surgical Finals. London: Hodder Arnold; 2010.',
    'Burton N, Birdi K. Clinical Skills for OSCEs. London: Informa; 2006.'
]

p = end_to_end_model.predict(np.array(test_sample))
p = np.argmax(p, axis=-1)
print(p)
#for sNumber, sentence in enumerate(p):
Example #12
0
def DSConvModelMedium(nCategories, inputShape, name="DSConvModelMedium"):
    inputs = keras.Input(shape=inputShape)
    # dim Input:(40, 126, 1)

    #regu=regularizers.l2(1e-5)

    x = layers.Conv2D(filters=172,
                      kernel_size=(10, 4),
                      strides=(1, 2),
                      activation='relu',
                      padding='same',
                      kernel_initializer="he_normal")(inputs)
    x = layers.BatchNormalization()(x)

    #DSCONV 1
    x = layers.DepthwiseConv2D(kernel_size=(3, 3),
                               strides=(2, 2),
                               activation=None,
                               padding="valid",
                               depthwise_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)
    x = layers.Conv2D(filters=172,
                      kernel_size=1,
                      strides=(1, 1),
                      activation=None,
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)

    #DSCONV 2
    x = layers.DepthwiseConv2D(kernel_size=(3, 3),
                               strides=(1, 1),
                               activation=None,
                               padding="valid",
                               depthwise_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)
    x = layers.Conv2D(filters=172,
                      kernel_size=1,
                      strides=(1, 1),
                      activation=None,
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)

    #DSCONV 3
    x = layers.DepthwiseConv2D(kernel_size=(3, 3),
                               strides=(1, 1),
                               activation=None,
                               padding="valid",
                               depthwise_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)
    x = layers.Conv2D(filters=172,
                      kernel_size=1,
                      strides=(1, 1),
                      activation=None,
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)

    #DSCONV 4
    x = layers.DepthwiseConv2D(kernel_size=(3, 3),
                               strides=(1, 1),
                               activation=None,
                               padding="valid",
                               depthwise_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)
    x = layers.Conv2D(filters=172,
                      kernel_size=1,
                      strides=(1, 1),
                      activation=None,
                      padding='valid',
                      kernel_initializer="he_normal")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation(activation='relu')(x)

    x = layers.AveragePooling2D(pool_size=2, strides=2)(x)

    x = layers.Flatten()(x)

    output = layers.Dense(nCategories,
                          activation="softmax",
                          kernel_initializer="glorot_uniform")(x)
    #kernel_regularizer=regu)(x)

    model = keras.Model(inputs=inputs, outputs=output, name=name)

    return model
def autoencoder(input_dims, hidden_layers, latent_dims):
    """that creates a variational autoencoder:

    Arg:
        - input_dims: is an integer containing the dims of the model input
        - hidden_layers: is a list containing the number of nodes for each
                        hidden layer in the encoder, respectively
        - latent_dims: is an integer containing the dimensions of the latent
                    space representation

    Returns: encoder, decoder, auto
        - encoder: is the encoder model
        - decoder: is the decoder model
        - auto: is the full autoencoder model
    """
    # creating the variational autoencoder model

    # encoded part of the model
    input_x = K.Input(shape=(input_dims,))
    for i, layer in enumerate(hidden_layers):
        if i == 0:
            encoded = K.layers.Dense(layer, activation='relu')(input_x)
        else:
            encoded = K.layers.Dense(layer, activation='relu')(encoded)
    # the botneckle layer: as result as a distribution prob, 2 arrays
    # the z mean of the prob distribution array and the standar desviation
    z_mean = K.layers.Dense(latent_dims)(encoded)
    z_stand_des = K.layers.Dense(latent_dims)(encoded)

    def sampling(args):
        z_mean, z_stand_des = args
        epsilon = K.backend.random_normal(shape=(latent_dims,),
                                          mean=0.0, stddev=1.0)
        return z_mean + K.backend.exp(z_stand_des) * epsilon

    # sampling the data from the data set using the z_mean and z_stand_dev
    z = K.layers.Lambda(sampling, output_shape=(
        latent_dims,))([z_mean, z_stand_des])
    # encoder part of the model take and image and get a sample based
    # on themean, standard desv
    encoder = K.models.Model(input_x, z)
    encoder.summary()

    # decoded part of the model
    input_z = K.Input(shape=(latent_dims,))
    for i in range(len(hidden_layers)-1, -1, -1):
        if i == len(hidden_layers)-1:
            decoded = K.layers.Dense(
                hidden_layers[i], activation='relu')(input_z)
        else:
            decoded = K.layers.Dense(
                hidden_layers[i], activation='relu')(decoded)
    decoded = K.layers.Dense(input_dims, activation='sigmoid')(decoded)
    # decoder: generating an image based in the dataset
    decoder = K.models.Model(input_z, decoded)
    decoder.summary()

    x = K.Input(shape=(input_dims,))
    z_encoder = encoder(x)
    x_decoder_mean = decoder(z_encoder)
    # mapping the complete autoencoded model, reconstruc the image
    autoencoder = K.models.Model(
        inputs=x, outputs=x_decoder_mean)
    autoencoder.summary()

    def vae_loss(x, x_decoder_mean):
        x_loss = K.backend.binary_crossentropy(x, x_decoder_mean)
        kl_loss = - 0.5 * K.backend.mean(1 + z_stand_des -
                                         K.backend.square(z_mean) -
                                         K.backend.exp(z_stand_des), axis=-1)
        return x_loss + kl_loss

    autoencoder.compile(optimizer='Adam', loss=vae_loss)

    return (encoder, decoder, autoencoder)
# datetime:1993/12/01
# filename:train_keras.py
# software: PyCharm

import tensorflow.keras as keras
from retinanet import retinanet
from config.configs import config
from dataset.get_dataset import DataGenerator
from core.loss import retina_loss
import tensorflow as tf

if __name__ == '__main__':

    tf.executing_eagerly = False

    inputs = keras.Input(shape=(416, 416, 3))
    retina_model = retinanet(inputs=inputs,
                             out_channels=256,
                             num_classes=6,
                             num_anchors=9)
    retina_model.load_weights('./datas/resnet50_coco_best_v2.1.0.h5',
                              by_name=True,
                              skip_mismatch=True)
    print('load weights successfully!!')

    outputs = retina_model.outputs
    y_true = [keras.Input(shape=(None, 5)), keras.Input(shape=(None, 7))]
    loss_input = [y_true, outputs]
    model_loss = keras.layers.Lambda(retina_loss,
                                     output_shape=(1, ),
                                     name='retina_loss')(loss_input)
mnist_digits = np.concatenate([x_train, x_test], axis=0)
mnist_digits = np.expand_dims(mnist_digits, -1).astype("float32") / 255

# %% Q(Z|X)  encoding
class Sampling(keras.layers.Layer):
    """Uses (z_mean, z_log_var) to sample z, the vector encoding a digit."""

    def call(self, inputs):
        z_mean, z_log_var = inputs
        batch = tf.shape(z_mean)[0]
        dim = tf.shape(z_mean)[1]
        epsilon = tf.keras.backend.random_normal(shape=(batch, dim))
        return z_mean + tf.exp(0.5 * z_log_var) * epsilon   #re-parametric trick!

latent_dim = 2
encoder_inputs = keras.Input(shape=input_shape)
x = keras.layers.Conv2D(32, 3, activation="relu", strides=2, padding="same")(encoder_inputs)
x = keras.layers.Conv2D(64, 3, activation="relu", strides=2, padding="same")(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(16, activation="relu")(x)
z_mean = keras.layers.Dense(latent_dim, name="z_mean")(x)
z_log_var = keras.layers.Dense(latent_dim, name="z_log_var")(x)
z = Sampling()([z_mean, z_log_var])
encoder = keras.Model(encoder_inputs, [z_mean, z_log_var, z], name="encoder")
encoder.summary()

# %% P(X|Z)  decoding
#latent_inputs = keras.Input(shape=(latent_dim,))
#x = keras.layers.Dense(7 * 7 * 64, activation="relu")(latent_inputs)
#x = keras.layers.Reshape((7, 7, 64))(x)
#x = keras.layers.Conv2DTranspose(64, 3, activation="relu", strides=2, padding="same")(x)
Example #16
0
def vanilla_unet(input_shape = (300, 300, 3), num_classes = 2):

    contraction_conv2d_out_shapes = [[64, 64], [128, 128], [256, 256], [512, 512]]
    bottleneck_conv2d_out_shapes = [1024, 1024]
    expansion_conv2d_out_shapes = [[512, 512], [256, 256], [128, 128], [64, 64]]
    resize_coeff = 5

    unet_input = keras.Input(shape=input_shape)

    x = unet_input

    # contraction blocks
    contraction_feature_maps = []
    contraction_counter = 1
    conv_counter = 1
    for cur_conv_block in contraction_conv2d_out_shapes:
        for cur_conv in cur_conv_block:
            x = keras.layers.Conv2D(cur_conv, 3, padding="same", activation="relu",
                                    name=f"contraction{contraction_counter}_conv{conv_counter}")(x)
            x = keras.layers.BatchNormalization(name=f"contraction{contraction_counter}_batchnorm{conv_counter}")(x)
            conv_counter += 1
        contraction_feature_maps.append(x)
        x = keras.layers.MaxPool2D(pool_size=(2, 2))(x)
        contraction_counter += 1

    # bottleneck_block
    conv_counter = 1
    for cur_bottleneck_conv in bottleneck_conv2d_out_shapes:
        x = keras.layers.Conv2D(cur_bottleneck_conv, 3, padding="same", activation="relu",
                                name=f"bottleneck_conv{conv_counter}")(x)
        x = keras.layers.BatchNormalization(name=f"bottleneck_batchnorm{conv_counter}")(x)
        conv_counter += 1
    x = keras.layers.UpSampling2D(size=(2, 2), name = "bottleneck_upsampling")(x)
    x = keras.layers.Conv2D(x.shape[-1] // 2, padding="same", kernel_size=(2, 2), name = "bottleneck_upconv")(x)

    # expansion blocks
    expansion_counter = 1
    conv_counter = 1
    for cur_conv_block in expansion_conv2d_out_shapes:
        residual = contraction_feature_maps.pop()

        # if residual.shape[1] > x.shape[1] or residual.shape[2] > x.shape[2]:
        #     height_diff = residual.shape[1] - x.shape[1]
        #     width_diff = residual.shape[2] - x.shape[2]
        #     residual = keras.layers.Cropping2D(cropping=((tf.math.ceil(height_diff/2), tf.math.floor(height_diff/2)),
        #                                                  (tf.math.ceil(width_diff/2), tf.math.floor(width_diff/2))))(residual)

        height_diff = residual.shape[1] - x.shape[1]
        width_diff = residual.shape[2] - x.shape[2]
        residual = keras.layers.Cropping2D(cropping=((-(-height_diff // 2), (height_diff // 2)),
                                                     (-(-width_diff // 2), (width_diff // 2))))(residual)

        x = keras.layers.Concatenate(name=f"expansion{expansion_counter}_concat")([residual, x])
        for cur_conv in cur_conv_block:
            x = keras.layers.Conv2D(cur_conv, 3, padding="same", activation="relu",
                                    name=f"expansion{expansion_counter}_conv{conv_counter}")(x)
            x = keras.layers.BatchNormalization(name=f"expansion{expansion_counter}_batchnorm{conv_counter}")(x)
            conv_counter += 1

        if expansion_counter != len(expansion_conv2d_out_shapes):
            x = keras.layers.UpSampling2D(size=(2, 2), name=f"expansion{expansion_counter}_upsampling")(x)
            x = keras.layers.Conv2D(x.shape[-1] // 2, kernel_size=(2, 2), padding="same", name=f"expansion{expansion_counter}_upconv")(x)

        expansion_counter += 1

    # final layer
    output = keras.layers.Conv2D(num_classes, kernel_size=(3, 3), strides = (1, 1), activation="softmax", padding="same", name="final_layer")(x)

    # resize the output to the desired shape
    output = keras.layers.experimental.preprocessing.Resizing(input_shape[0], input_shape[1])(output)

    # channel-wise softmax layer for determining the class of each output pixel
    # output = keras.layers.Softmax(axis = -1)(final_layer)

    return keras.Model(unet_input, output)
Example #17
0
This guide doesn't cover distributed training. For distributed training, see
our [guide to multi-gpu & distributed training](/guides/distributed_training/).
"""
"""
## API overview: a first end-to-end example

When passing data to the built-in training loops of a model, you should either use
**NumPy arrays** (if your data is small and fits in memory) or **`tf.data Dataset`
objects**. In the next few paragraphs, we'll use the MNIST dataset as NumPy arrays, in
order to demonstrate how to use optimizers, losses, and metrics.

Let's consider the following model (here, we build in with the Functional API, but it
could be a Sequential model or a subclassed model as well):
"""

inputs = keras.Input(shape=(784, ), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)

model = keras.Model(inputs=inputs, outputs=outputs)
"""
Here's what the typical end-to-end workflow looks like, consisting of:

- Training
- Validation on a holdout set generated from the original training data
- Evaluation on the test data

We'll use MNIST data for this example.
"""
Example #18
0
def create_model():
    INP_SHAPE = (28, 28, 1)
    img_input = keras.Input(shape=INP_SHAPE)
    CONCAT_AXIS = 3
    NB_CLASS = 10

    # module 1
    x = keras.layers.Convolution2D(filters=64,
                                   kernel_size=(7, 7),
                                   strides=2,
                                   padding='same')(img_input)
    x = keras.layers.MaxPooling2D(pool_size=(3, 3), strides=2)(x)
    # 为防止梯度弥散,在网络的第一模块最后加入批量规范化层可以避免随机性可能造成的梯度弥散,使精度停留在0.1左右
    x = keras.layers.BatchNormalization()(x)

    # module 2
    x = keras.layers.Convolution2D(filters=64, kernel_size=(1, 1))(x)
    x = keras.layers.Convolution2D(filters=192,
                                   kernel_size=(3, 3),
                                   padding='same')(x)
    x = keras.layers.MaxPooling2D(pool_size=(3, 3), strides=2,
                                  padding='same')(x)

    # module 3
    x = inception_module(x,
                         params=[(64, ), (96, 128), (16, 32), (32, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(128, ), (128, 192), (32, 96), (64, )],
                         concat_axis=CONCAT_AXIS)
    x = keras.layers.MaxPooling2D(pool_size=(3, 3), strides=2)(x)

    # module 4
    x = inception_module(x,
                         params=[(192, ), (96, 208), (16, 48), (64, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(160, ), (112, 224), (24, 64), (64, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(128, ), (128, 256), (24, 64), (64, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(112, ), (144, 288), (32, 64), (64, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(256, ), (160, 320), (32, 128), (128, )],
                         concat_axis=CONCAT_AXIS)
    x = keras.layers.MaxPooling2D(pool_size=(3, 3), strides=2,
                                  padding='same')(x)

    # module 5
    x = inception_module(x,
                         params=[(256, ), (160, 320), (32, 128), (128, )],
                         concat_axis=CONCAT_AXIS)
    x = inception_module(x,
                         params=[(384, ), (192, 384), (48, 128), (128, )],
                         concat_axis=CONCAT_AXIS)
    x = keras.layers.AveragePooling2D(pool_size=(2, 2), padding='same')(x)

    x = keras.layers.Flatten()(x)
    x = keras.layers.Dense(units=NB_CLASS, activation='softmax')(x)

    return x, img_input, CONCAT_AXIS, INP_SHAPE
Example #19
0
def build_graph(input_shape, num_classes=10, weight_decay=1e-4):
    inputs = keras.Input(shape=input_shape)
    outputs = MobileNetCifar(num_classes=num_classes,
                             weight_decay=weight_decay)(inputs)
    return keras.Model(inputs=inputs, outputs=outputs)
Example #20
0
def get_stft_mag_phase(
    input_shape,
    n_fft=2048,
    win_length=None,
    hop_length=None,
    window_fn=None,
    pad_begin=False,
    pad_end=False,
    return_decibel=False,
    db_amin=1e-5,
    db_ref_value=1.0,
    db_dynamic_range=80.0,
    input_data_format='default',
    output_data_format='default',
):
    """A function that returns magnitude and phase of input audio.

    Args:
        input_shape (None or tuple of integers): input shape of the stft layer.
            Because this mag_phase is based on keras.Functional model, it is required to specify the input shape.
            E.g., (44100, 2) for 44100-sample stereo audio with `input_data_format=='channels_last'`.
        n_fft (int): number of FFT points in `STFT`
        win_length (int): window length of `STFT`
        hop_length (int): hop length of `STFT`
        window_fn (function or `None`): windowing function of `STFT`.
            Defaults to `None`, which would follow tf.signal.stft default (hann window at the moment)
        pad_begin(bool): Whether to pad with zeros along time axis (legnth: win_length - hop_length). Defaults to `False`.
        pad_end (bool): whether to pad the input signal at the end in `STFT`.
        return_decibel (bool): whether to apply decibel scaling at the end
        db_amin (float): noise floor of decibel scaling input. See `MagnitudeToDecibel` for more details.
        db_ref_value (float): reference value of decibel scaling. See `MagnitudeToDecibel` for more details.
        db_dynamic_range (float): dynamic range of the decibel scaling result.
        input_data_format (str): the audio data format of input waveform batch.
            `'channels_last'` if it's `(batch, time, channels)`
            `'channels_first'` if it's `(batch, channels, time)`
            Defaults to the setting of your Keras configuration. (tf.keras.backend.image_data_format())
        output_data_format (str): the data format of output mel spectrogram.
            `'channels_last'` if you want `(batch, time, frequency, channels)`
            `'channels_first'` if you want `(batch, channels, time, frequency)`
            Defaults to the setting of your Keras configuration. (tf.keras.backend.image_data_format())

    Example:
        ::

            input_shape = (2048, 3)  # stereo and channels_last
            model = Sequential()
            model.add(
                get_stft_mag_phase(input_shape=input_shape, return_decibel=True, n_fft=1024)
            )
            # now output shape is (batch, n_frame=3, freq=513, ch=6). 6 channels = [3 mag ch; 3 phase ch]

    """
    backend.validate_data_format_str(input_data_format)
    backend.validate_data_format_str(output_data_format)

    waveform_to_stft = STFT(
        n_fft=n_fft,
        win_length=win_length,
        hop_length=hop_length,
        window_fn=window_fn,
        pad_begin=pad_begin,
        pad_end=pad_end,
        input_data_format=input_data_format,
        output_data_format=output_data_format,
    )

    stft_to_stftm = Magnitude()
    stft_to_stftp = Phase()

    waveforms = keras.Input(shape=input_shape)

    stfts = waveform_to_stft(waveforms)
    mag_stfts = stft_to_stftm(stfts)  # magnitude
    phase_stfts = stft_to_stftp(stfts)  # phase

    if return_decibel:
        mag_to_decibel = MagnitudeToDecibel(ref_value=db_ref_value,
                                            amin=db_amin,
                                            dynamic_range=db_dynamic_range)
        mag_stfts = mag_to_decibel(mag_stfts)

    ch_axis = 1 if output_data_format == _CH_FIRST_STR else 3

    concat_layer = keras.layers.Concatenate(axis=ch_axis)

    stfts_mag_phase = concat_layer([mag_stfts, phase_stfts])

    model = Model(inputs=waveforms, outputs=stfts_mag_phase)
    return model
model.compile(
    optimizer=keras.optimizers.RMSprop(1e-3),
    loss={'class_output': keras.losses.CategoricalCrossentropy(from_logits=True)})

Passing data to a multi-input or multi-output model in fit works in a similar way as specifying
a loss function in compile:
- you can pass lists of Numpy arrays
(with 1:1 mapping to the outputs that received a loss function)
- or dicts mapping output names to Numpy arrays of training data.
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import numpy as np
import tensorflow as tf
from tensorflow import keras

image_input = keras.Input(shape=(32, 32, 3), name='img_input')
timeseries_input = keras.Input(shape=(None, 10), name='ts_input')

x1 = keras.layers.Conv2D(3, 3)(image_input)
x1 = keras.layers.GlobalMaxPooling2D()(x1)

x2 = keras.layers.Conv1D(3, 3)(timeseries_input)
x2 = keras.layers.GlobalMaxPooling1D()(x2)

x = keras.layers.concatenate([x1, x2])

score_output = keras.layers.Dense(1, name='score_output')(x)
class_output = keras.layers.Dense(5, name='class_output')(x)

model = keras.Model(inputs=[image_input, timeseries_input],
                    outputs=[score_output, class_output])
import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.mixed_precision import experimental as mixed_precision
physical_devices = tf.config.experimental.list_physical_devices(
    device_type='GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)

policy = mixed_precision.Policy('mixed_float16')
mixed_precision.set_policy(policy)
print('Compute dtype: %s' % policy.compute_dtype)
print('Variable dtype: %s' % policy.variable_dtype)

inputs = keras.Input(shape=(784, ), name='digits')
num_units = 4096
dense1 = layers.Dense(num_units, activation='relu', name='dense_1')
x = dense1(inputs)
dense2 = layers.Dense(num_units, activation='relu', name='dense_2')
x = dense2(x)

print('x.dtype: %s' % x.dtype.name)
# 'kernel' is dense1's variable
print('dense1.kernel.dtype: %s' % dense1.kernel.dtype.name)

# INCORRECT: softmax and model output will be float16, when it should be float32
outputs = layers.Dense(10, activation='softmax', name='predictions')(x)
print('Outputs dtype: %s' % outputs.dtype.name)

# CORRECT: softmax and model output are float32
x = layers.Dense(10, name='dense_logits')(x)
Example #23
0
    def cnntwo(self, lr=0.5, lrfactor=0.2, mom=0.1, epochs=2):
        self.lr = lr
        self.lrfactor = lrfactor
        self.mom = mom
        self.epochs = epochs
        self.xtrain = np.expand_dims(self.xtrain.reshape(86744, 32, 32),
                                     axis=3)  #reshaping for convolution
        self.xval = np.expand_dims(self.xval.reshape(10954, 32, 32),
                                   axis=3)  #reshaping for convolution

        # setting up to task specific inputs for gender and race
        self.clean = cleandata()
        self.ytgender = self.clean.training(self.ytrain[:, 2])
        self.ytrace = self.clean.training(self.ytrain[:, 3])
        self.yvgender = self.clean.training(self.yval[:, 2])
        self.yvrace = self.clean.training(self.yval[:, 3])
        self.yvgendertruth, self.genderlabels = self.clean.cmatrix(
            self.yvaltruth[:, 2])
        self.yvracetruth, self.racelabels = self.clean.cmatrix(
            self.yvaltruth[:, 3])

        #two task specific reworks

        inputs = keras.Input(shape=(32, 32, 1))
        x = layers.Conv2D(filters=40, kernel_size=5, activation="relu")(inputs)
        x = layers.MaxPooling2D()(x)
        x = layers.Flatten()(x)
        y = layers.Dense(100, activation="relu")(x)
        z = layers.Dense(100, activation="relu")(x)
        gender = layers.Dense(2, activation="softmax")(y)  #classify gender
        race = layers.Dense(7, activation="softmax")(z)
        model = keras.Model(inputs=[inputs], outputs=[gender, race])
        model.summary()

        model.compile(
            optimizer=keras.optimizers.SGD(
                learning_rate=self.lr,
                momentum=self.mom),  #default lr=0.01, default mom=0.0
            loss=keras.losses.CategoricalCrossentropy(),
            metrics=[keras.metrics.CategoricalAccuracy()],
        )

        #training and validating
        print('training and validating')
        reduce_lr = keras.callbacks.ReduceLROnPlateau(
            monitor='val_loss',
            factor=self.lrfactor,
            patience=1,
            min_lr=0.0001,
        )
        history = model.fit(
            self.xtrain, [self.ytgender, self.ytrace],
            validation_split=0.2,
            epochs=self.epochs,
            callbacks=[reduce_lr]
        )  #if you don't specify a batch size, it uses 32 for mini-batch GD
        #testing
        print('evalauting')
        results = model.evaluate(self.xval, [self.yvgender, self.yvrace])
        print('predicting')
        #predicting
        predictions = model.predict(self.xval)

        #plotting/final results summary, from https://machinelearningmastery.com/display-deep-learning-model-training-history-in-keras/
        plt.plot(history.history['categorical_accuracy'])
        plt.plot(history.history['val_categorical_accuracy'])
        plt.title('model accuracy for gender classification')
        plt.ylabel('accuracy')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()

        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('model loss for gender classification')
        plt.ylabel('loss')
        plt.xlabel('epoch')
        plt.legend(['train', 'validation'], loc='upper left')
        plt.show()
        trainingloss = history.history['loss']
        validationloss = history.history['val_loss']
        print('final validation accuracy for gender is %f' % results[1])

        genderconfmatrix = sklearn.metrics.confusion_matrix(
            self.yvgendertruth, predictions)
        cmdisp = sklearn.metrics.ConfusionMatrixDisplay(
            confusion_matrix=genderconfmatrix,
            display_labels=self.genderlabels)
        cmdisp.plot()
        plt.show()
        return [trainingloss, validationloss, history, results, confmatrix]
Example #24
0
def make_physics_model_r2b(position_model: keras.Model, traj_size: int):
    """Create a physics model for the restricted two body problem from a position model"""
    # Create input layers
    t = keras.Input(shape=(traj_size, ), name='t')
    q0 = keras.Input(shape=(3, ), name='q0')
    v0 = keras.Input(shape=(3, ), name='v0')
    mu = keras.Input(shape=(1, ), name='mu')

    # Wrap these up into one tuple of inputs for the model
    inputs = (t, q0, v0, mu)

    # Check sizes of inputs
    batch_size = t.shape[0]
    tf.debugging.assert_shapes(shapes={
        t: (batch_size, traj_size),
        q0: (batch_size, 3),
        v0: (batch_size, 3),
        mu: (batch_size, 1),
    },
                               message='make_physics_model_r2b_math / inputs')

    # Return row 0 of a position or velocity for q0_rec and v0_rec
    initial_row_func = lambda q: q[:, 0, :]

    # Compute the motion from the specified position layer; inputs are the same for position and physics model
    q, v, a = Motion_R2B(position_model=position_model, name='motion')(inputs)

    # Name the outputs of the motion
    # These each have shape (batch_size, traj_size, 3)
    q = Identity(name='q')(q)
    v = Identity(name='v')(v)
    a = Identity(name='a')(a)

    # Check sizes
    tf.debugging.assert_shapes(
        shapes={
            q: (batch_size, traj_size, 3),
            v: (batch_size, traj_size, 3),
            a: (batch_size, traj_size, 3),
        },
        message='make_physics_model_r2b / outputs q, v, a')

    # Compute q0_rec and v0_rec
    # These each have shape (batch_size, 2)
    q0_rec = keras.layers.Lambda(initial_row_func, name='q0_rec')(q)
    v0_rec = keras.layers.Lambda(initial_row_func, name='v0_rec')(v)

    # Check sizes
    tf.debugging.assert_shapes(
        shapes={
            q0_rec: (batch_size, 3),
            v0_rec: (batch_size, 3),
        },
        message='make_physics_model_r2b / outputs q0_rec, v0_rec')

    # Compute kinetic energy T and potential energy U
    T = KineticEnergy_R2B(name='T')(v)
    U = PotentialEnergy_R2B(name='U')((q, mu))

    # Compute the total energy H
    H = keras.layers.add(inputs=[T, U], name='H')

    # Compute angular momentum L
    # This has shape (batch_size, traj_size, 3)
    L = AngularMomentum_R2B(name='L')([q, v])

    # Check sizes
    tf.debugging.assert_shapes(
        shapes={
            T: (batch_size, traj_size),
            U: (batch_size, traj_size),
            H: (batch_size, traj_size),
            L: (batch_size, traj_size, 3),
        },
        message='make_physics_model_r2bc_math / outputs H, L')

    # Wrap this up into a model
    outputs = [q, v, a, q0_rec, v0_rec, H, L]
    model_name = position_model.name.replace('model_r2b_position_',
                                             'model_r2b_physics_')
    model = keras.Model(inputs=inputs, outputs=outputs, name=model_name)
    return model
 def get_functional(self) -> keras.Model:
     x = keras.Input(shape=self._input_shape)
     return keras.Model(inputs=[x], outputs=self.call(x), name=self.name)
Example #26
0
File: model.py Project: ZNHU/X3D-tf
 def summary(self, input_shape):
     x = K.Input(shape=input_shape)
     model = K.Model(inputs=x, outputs=self.call(x), name='X3D')
     return model.summary()
Example #27
0
eval_size = ds_size * EVAL_SPLIT
test_size = ds_size * TEST_SPLIT

train_ds = ds.take(train_size)

eval_ds = ds.skip(train_size)
eval_ds = eval_ds.take(eval_size)

test_ds = ds.skip(train_size + eval_size)
test_ds = ds.take(test_size)

train_ds = train_ds.batch(BATCH)
eval_ds = eval_ds.batch(BATCH)
test_ds = test_ds.batch(BATCH)

inputs = keras.Input(shape=(1, ))
x = keras.layers.Dense(100)(inputs)
outputs = keras.layers.Dense(RANGE, activation="softmax")(x)

model = keras.Model(inputs=inputs, outputs=outputs, name="steves_model")
model.summary()

model.compile(
    optimizer='adam',
    loss=tf.keras.losses.MeanSquaredError(),
    metrics=[keras.metrics.CategoricalAccuracy()
             ],  # Categorical is needed for one hot encoded data
)

callbacks = [
    keras.callbacks.ModelCheckpoint(
Example #28
0
model.add(K.layers.GlobalAveragePooling2D(name='keras_mobilenet_head_pool'))
model.add(K.layers.Reshape((1, 1, 4), name='keras_mobilenet_head_reshape'))
sess = K.backend.get_session()
sess.as_default()
save(sess.graph.get_tensor_by_name('keras_mobilenet_head_conv_input:0'),
     sess.graph.get_tensor_by_name('keras_mobilenet_head_reshape/Reshape:0'),
     'keras_mobilenet_head',
     optimize=False)


################################################################################
def keras_relu6(x):
    return K.activations.relu(x, max_value=6)


inp = K.Input(shape=(2, 3, 4), name='keras_relu6_input')
relu = K.layers.Activation(keras_relu6, name='keras_relu6')(inp)
model = K.Model(inp, relu)
sess = K.backend.get_session()
sess.as_default()
save(sess.graph.get_tensor_by_name('keras_relu6_input:0'),
     sess.graph.get_tensor_by_name('keras_relu6/clip_by_value:0'),
     'keras_relu6',
     optimize=False)
################################################################################
inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input')
reduced = tf.reduce_mean(inp, axis=[1, 2], keepdims=True)
save(inp, reduced, 'reduce_mean')
################################################################################
inp = tf.placeholder(tf.float32, [2, 3, 4, 5], 'input')
pool = tf.layers.average_pooling2d(inp, pool_size=1, strides=1, padding='SAME')
#!/usr/bin/env python3

import tensorflow.keras as K
projection_block = __import__('3-projection_block').projection_block

if __name__ == '__main__':
    X = K.Input(shape=(224, 224, 3))
    Y = projection_block(X, [64, 64, 256])
    model = K.models.Model(inputs=X, outputs=Y)
    model.summary()
# We are NOT shuffling and cahcing in ds_test
ds_test = ds_test.map(normalize_image, num_parallel_calls=AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.prefetch(AUTOTUNE)

# TF >= 2.3.0 ise asagidaki sekilde Data Augmentation yapilabilir
# Bu method'da data augmentation modelin bir parcasidir
# Yukaridaki gibi paralel bir islem olmadigi icin performans kaybi olabilir ama daha simpledir
data_augmentation = keras.Sequential([
    layers.experimental.preprocessing.Resizing(height=32, width=32),
    layers.experimental.preprocessing.RandomFlip(mode='horizontal'),
    layers.experimental.preprocessing.RandomContrast(factor=0.1),
])

model = keras.Sequential([
    keras.Input((32, 32, 3)),
    data_augmentation,  # 2. data augmentation method devrede iken kullanilir
    layers.Conv2D(4, 3, padding='same', activation='relu'),
    layers.Conv2D(8, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(16, 3, padding='same', activation='relu'),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10),
])

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer=keras.optimizers.Adam(lr=3e-4),
    metrics=['accuracy'])