def res3_block(self, x, ct):
        self.res3a_2 = Conv3D(kernel_size=3, filters=128, strides=1, padding='same', 
                               data_format=self.DATA_FORMAT, trainable=ct, name='res3a_2')
        self.res3a_bn = BatchNormalization(axis=1, trainable=ct, name='res3a_bn')
        self.res3b_1 = Conv3D(kernel_size=3, filters=128, strides=1, padding='same', 
                              data_format=self.DATA_FORMAT, trainable=ct, name='res3b_1')
        self.res3b_1_bn = BatchNormalization(axis=1, trainable=ct, name='res3b_1_bn')
        self.res3b_2 = Conv3D(kernel_size=3, filters=128, strides=1, padding='same', 
                              data_format=self.DATA_FORMAT, trainable=ct, name='res3b_2')
        self.res3b_bn = BatchNormalization(axis=1, trainable=ct, name='res3b_bn')
        
        x1 = self.res3a_2(x)

        x2 = self.res3a_2(x)
        x2 = self.res3a_bn(x2)
        x2 = tf.nn.relu(x2, name='res3a_relu')
        x2 = self.res3b_1(x2)
        x2 = self.res3b_1_bn(x2)
        x2 = tf.nn.relu(x2, name='res3b_1_relu')
        x2 = self.res3b_2(x2)

        x = x1 + x2
        x = self.res3b_bn(x)
        x = tf.nn.relu(x, name='res3b')
        return x
    def inception_block_3c(self, x, ct):
        self.inception_3c_double_3x3_reduce = Conv2D(
            kernel_size=1,
            filters=64,
            padding='valid',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3c_double_3x3_reduce')
        self.inception_3c_double_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3c_double_3x3_reduce_bn')
        self.inception_3c_double_3x3_1 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3c_double_3x3_1')
        self.inception_3c_double_3x3_1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3c_double_3x3_1_bn')

        x = self.inception_3c_double_3x3_reduce(x)
        x = self.inception_3c_double_3x3_reduce_bn(x)
        x = tf.nn.relu(x, name='inception_3c_relu_double_3x3_reduce_inp')

        x = self.inception_3c_double_3x3_1(x)
        x = self.inception_3c_double_3x3_1_bn(x)
        print(x)
        x = tf.nn.relu(x, name='inception_3c_relu_double_3x3_1_inp')
        x = tf.reshape(x, (-1, self.frm_num, 96, 28, 28), name='r2Dto3D')
        x = tf.transpose(x, [0, 2, 1, 3, 4])
        return x
    def inception_part(self, input_x, ct):
        self.conv1_7x7_s2 = Conv2D(kernel_size=(7,7), filters=64, strides=2, padding='same', 
                            data_format=self.DATA_FORMAT, trainable=ct, name='conv1_7x7_s2')

        self.conv1_7x7_s2_bn = BatchNormalization(axis=1, trainable=ct, name='conv1_7x7_s2_bn')

        self.pool1_3x3_s2 = MaxPooling2D(pool_size=3, strides=2, padding='same', 
                                  data_format=self.DATA_FORMAT, name='pool1_3x3_s2')
        
        self.conv2_3x3_reduce = Conv2D(kernel_size=1, filters=64, trainable=ct, 
                                       data_format=self.DATA_FORMAT, name='conv2_3x3_reduce')
        
        self.conv2_3x3_reduce_bn = BatchNormalization(axis=1, trainable=ct, name='conv2_3x3_reduce_bn')
        
        self.conv2_3x3 = Conv2D(kernel_size=3, filters=192, padding='same', 
                                data_format=self.DATA_FORMAT, trainable=ct, name='conv2_3x3')
        
        self.conv2_3x3_bn = BatchNormalization(axis=1, trainable=ct, name='conv2_3x3_bn')
        
        self.pool2_3x3_s2 = MaxPooling2D(pool_size=3, strides=2, padding='same', 
                                         data_format=self.DATA_FORMAT, name='pool2_3x3_s2')

        # x = tf.reshape(input_x, (-1, 3, 224, 224))
        x = self.conv1_7x7_s2(input_x)

        x = self.conv1_7x7_s2_bn(x)
        x = tf.nn.relu(x, name='conv1_relu_7x7_inp')
        x = self.pool1_3x3_s2(x)


        x = self.conv2_3x3_reduce(x)
        x = self.conv2_3x3_reduce_bn(x)
        x = tf.nn.relu(x, name='conv2_relu_3x3_reduce_inp')

        x = self.conv2_3x3(x)
        x = self.conv2_3x3_bn(x)
        x = tf.nn.relu(x, name='conv2_relu_3x3_inp')

        x = self.pool2_3x3_s2(x)
        print(x)

        self.incep_3a = self.inception_block_3a(x, ct)
        print(self.incep_3a)
        self.incep_3b = self.inception_block_3b(self.incep_3a, ct)
        print(self.incep_3b)
        self.incep_3c = self.inception_block_3c(self.incep_3b, ct)
        print(self.incep_3c)
        return self.incep_3c
def conv_block_simple_2d(prevlayer,
                         num_filters,
                         prefix,
                         kernel_size=(3, 3),
                         initializer="he_normal",
                         strides=(1, 1)):

    # conv = Conv2D(filters=num_filters, kernel_size=kernel_size, padding="same", kernel_initializer=initializer, strides=strides, name=prefix + "_conv",
    #      data_format='channels_first')(prevlayer)

    # conv = Activation('relu', name=prefix + "_activation")(conv)

    # return conv

    conv = Conv2D(filters=num_filters,
                  kernel_size=kernel_size,
                  padding="same",
                  kernel_initializer=initializer,
                  strides=strides,
                  name=prefix + "_conv",
                  data_format='channels_first')(prevlayer)

    conv = BatchNormalization(name=prefix + "_bn", axis=1)(conv)

    # conv = Activation('relu', name=prefix + "_activation")(conv)
    conv = tf.nn.relu(conv, name=prefix + "_activation")

    return conv
Exemple #5
0
def batch_normalization(inputs, training=True):
    """
    Batch normalization

    Parameters
    ----------
    inputs: Input tensor
    training: Whether in training phase
    """
    return BatchNormalization()(inputs, training=training)
    def inception_block_3a(self, x, ct):
        self.inception_3a_1x1 = Conv2D(kernel_size=1, filters=64, data_format=self.DATA_FORMAT, trainable=ct, 
                                       name='inception_3a_1x1')
        self.inception_3a_1x1_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_1x1_bn')
        self.inception_3a_3x3_reduce = Conv2D(kernel_size=1, filters=64, data_format=self.DATA_FORMAT, trainable=ct,
                                              name='inception_3a_3x3_reduce')
        self.inception_3a_3x3_reduce_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_3x3_reduce_bn')
        self.inception_3a_3x3 = Conv2D(kernel_size=3, filters=64, padding='same', data_format=self.DATA_FORMAT, 
                                       trainable=ct, name='inception_3a_3x3')
        self.inception_3a_3x3_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_3x3_bn')
        self.inception_3a_double_3x3_reduce = Conv2D(kernel_size=1, filters=64, data_format=self.DATA_FORMAT, 
                                                     trainable=ct, name='inception_3a_double_3x3_reduce')
        self.inception_3a_double_3x3_reduce_bn = BatchNormalization(axis=1, trainable=ct, 
                                                                    name='inception_3a_double_3x3_reduce_bn')
        self.inception_3a_double_3x3_1 = Conv2D(kernel_size=3, filters=96, padding='same', 
                                                data_format=self.DATA_FORMAT, trainable=ct, 
                                                name='inception_3a_double_3x3_1')
        self.inception_3a_double_3x3_1_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_double_3x3_1_bn')
        self.inception_3a_double_3x3_2 = Conv2D(kernel_size=3, filters=96, padding='same', 
                                                data_format=self.DATA_FORMAT, trainable=ct, 
                                                name='inception_3a_double_3x3_2')
        self.inception_3a_double_3x3_2_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_double_3x3_2_bn')
        self.inception_3a_pool = MaxPooling2D(pool_size=3, strides=1, padding='same', data_format=self.DATA_FORMAT, 
                                              name='inception_3a_pool')
        self.inception_3a_pool_proj = Conv2D(kernel_size=1, filters=32, data_format=self.DATA_FORMAT, trainable=ct,
                                             name='inception_3a_pool_proj')
        self.inception_3a_pool_proj_bn = BatchNormalization(axis=1, trainable=ct, name='inception_3a_pool_proj_bn')

        x1 = self.inception_3a_1x1(x)
        x1 = self.inception_3a_1x1_bn(x1)
        x1 = tf.nn.relu(x1)

        x2 = self.inception_3a_3x3_reduce(x)
        x2 = self.inception_3a_3x3_reduce_bn(x2)
        x2 = tf.nn.relu(x2)
        x2 = self.inception_3a_3x3(x2)
        x2 = self.inception_3a_3x3_bn(x2)
        x2 = tf.nn.relu(x2)

        x3 = self.inception_3a_double_3x3_reduce(x)
        x3 = self.inception_3a_double_3x3_reduce_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3a_double_3x3_1(x3)
        x3 = self.inception_3a_double_3x3_1_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3a_double_3x3_2(x3)
        x3 = self.inception_3a_double_3x3_2_bn(x3)
        x3 = tf.nn.relu(x3)

        x4 = self.inception_3a_pool(x)
        x4 = self.inception_3a_pool_proj(x4)
        x4 = self.inception_3a_pool_proj_bn(x4)
        x4 = tf.nn.relu(x4)
        
        x = tf.concat([x1, x2, x3, x4], axis=1, name='inception_3a_output')
        return x
def conv2d_bn(x, nb_filter, kernel_size, strides = (1, 1), padding = "SAME"):
	"""
	conv2d -> batch normalization -> relu.
	"""
	x = Conv2D(
		nb_filter,
		kernel_size,
		strides,
		padding,
		kernel_regularizer = tf.contrib.layers.l1_regularizer(0.001))(x)
	x = BatchNormalization()(x)
	x = relu(x)

	return x
Exemple #8
0
def NN_stacking(X_train, y_train, X_test, y_test, report=True):
    """Returns neural networks  model"""

    from tensorflow.layers import Dense, BatchNormalization
    from tensorflow.keras import Sequential
    from tensorflow import losses
    from tensorflow import set_random_seed
    set_random_seed(rand)
    blender = Sequential()
    blender.add(Dense(3, input_shape=(5, )))
    blender.add(BatchNormalization())
    blender.add(Dense(1, activation='relu'))
    blender.compile(optimizer='adam', loss=losses.mean_squared_error)
    blender.fit(np.array(X_train), np.array(y_train), epochs=3000, verbose=0)
    y_train_pred = blender.predict(np.array(X_train))
    y_pred = blender.predict(np.array(X_test))

    if report:
        print("\n\n========== [Stacking Report: NN stacking] ==========")
        mse = mean_squared_error(y_test, y_pred)
        print("NN Stacking Test Error(RMSE)    : ", np.sqrt(mse))

    return y_test, y_pred
new_x_train = 2 * new_x_train / max3 - 1
new_x_test = 2 * new_x_test / max3 - 1

new_y_train = keras.utils.to_categorical(y_train, 10)
new_y_test = keras.utils.to_categorical(y_test, 10)

inputs = Input(shape=(new_x_train.shape[1], new_x_train.shape[2]))
#these first layers are 'data augmentation' layers
x = MyAddScale(name='scale_augment')(inputs)
x = MyAdd2DRotation(name='rotate_augment')(x)
x = MyAddShift(name='shift_augment')(x)
x = MyAddJitter(name='jitter_augment')(x)
#This is the ursa layer to create a feature vector
x = MyUrsaMin(Nstars, name='cluster')(x)
x = Activation('relu')(x)
x = BatchNormalization()(x)
#these last layers do classification
x = Dense(512, activation='relu', name='dense512')(x)
x = BatchNormalization()(x)
x = Dense(256, activation='relu', name='dense256')(x)
x = BatchNormalization()(x)
x = Dropout(rate=0.3)(x)
x = Dense(10, activation='softmax')(x)

model = Model(inputs=inputs, outputs=x)
if gpus > 1:
    from keras.utils import multi_gpu_model
    model = multi_gpu_model(model, gpus=gpus)

rmsprop = tf.keras.optimizers.RMSprop(lr=.001, rho=.9, decay=.0001)
model.compile(optimizer=rmsprop,
class EcoModel():
    def __init__(self, data_format, cnn_trainable=False, frm_num=16):
        self.cnn_trainable = cnn_trainable
        self.graph = tf.Graph()
        self.frm_num = frm_num
        self.DATA_FORMAT = data_format
        with self.graph.as_default():
            with tf.variable_scope('eco', reuse=tf.AUTO_REUSE):
                self.input_x = tf.placeholder(dtype=tf.float32,
                                              shape=(None, 3, 224, 224),
                                              name='input_x')
                self.input_y = tf.placeholder(dtype=tf.int32,
                                              shape=(None, ),
                                              name='input_y')
                self.construct_model(self.input_x, self.input_y)
                #self.initialize_model()

    def construct_model(self, input_x, input_y):
        ct = self.cnn_trainable
        x = self.inception_part(input_x, ct)
        x = self.resnet_3d_part(x, ct)
        x = AveragePooling3D(pool_size=(self.frm_num // 4, 7, 7),
                             strides=(1, 1, 1),
                             padding='valid',
                             data_format=self.DATA_FORMAT,
                             name='global_pool')(x)
        print(x)
        x = tf.reshape(x, shape=(-1, 512))
        print(x)
        x = Dropout(0.3, name='dropout')(x)
        self.fc8 = Dense(400, trainable=ct, name='fc8')
        self.fc8_output = self.fc8(x)
        print(self.fc8_output)
        self.loss = sparse_softmax_cross_entropy_with_logits(
            logits=x, labels=self.input_y)

        self.top1_acc = in_top_k(predictions=self.fc8_output,
                                 targets=self.input_y,
                                 k=1)
        self.top1_acc = tf.reduce_mean(tf.cast(self.top1_acc, tf.float32),
                                       name='top1_accuracy')
        self.top5_acc = in_top_k(predictions=self.fc8_output,
                                 targets=self.input_y,
                                 k=5)
        self.top5_acc = tf.reduce_mean(tf.cast(self.top5_acc, tf.float32),
                                       name='top5_accuracy')

    def inception_part(self, input_x, ct):
        self.conv1_7x7_s2 = Conv2D(kernel_size=(7, 7),
                                   filters=64,
                                   strides=2,
                                   padding='same',
                                   data_format=self.DATA_FORMAT,
                                   trainable=ct,
                                   name='conv1_7x7_s2')

        self.conv1_7x7_s2_bn = BatchNormalization(axis=1,
                                                  trainable=ct,
                                                  name='conv1_7x7_s2_bn')

        self.pool1_3x3_s2 = MaxPooling2D(pool_size=3,
                                         strides=2,
                                         padding='same',
                                         data_format=self.DATA_FORMAT,
                                         name='pool1_3x3_s2')

        self.conv2_3x3_reduce = Conv2D(kernel_size=1,
                                       filters=64,
                                       trainable=ct,
                                       data_format=self.DATA_FORMAT,
                                       name='conv2_3x3_reduce')

        self.conv2_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='conv2_3x3_reduce_bn')

        self.conv2_3x3 = Conv2D(kernel_size=3,
                                filters=192,
                                padding='same',
                                data_format=self.DATA_FORMAT,
                                trainable=ct,
                                name='conv2_3x3')

        self.conv2_3x3_bn = BatchNormalization(axis=1,
                                               trainable=ct,
                                               name='conv2_3x3_bn')

        self.pool2_3x3_s2 = MaxPooling2D(pool_size=3,
                                         strides=2,
                                         padding='same',
                                         data_format=self.DATA_FORMAT,
                                         name='pool2_3x3_s2')

        # x = tf.reshape(input_x, (-1, 3, 224, 224))
        x = self.conv1_7x7_s2(input_x)

        x = self.conv1_7x7_s2_bn(x)
        x = tf.nn.relu(x, name='conv1_relu_7x7_inp')
        x = self.pool1_3x3_s2(x)

        x = self.conv2_3x3_reduce(x)
        x = self.conv2_3x3_reduce_bn(x)
        x = tf.nn.relu(x, name='conv2_relu_3x3_reduce_inp')

        x = self.conv2_3x3(x)
        x = self.conv2_3x3_bn(x)
        x = tf.nn.relu(x, name='conv2_relu_3x3_inp')

        x = self.pool2_3x3_s2(x)
        print(x)

        self.incep_3a = self.inception_block_3a(x, ct)
        print(self.incep_3a)
        self.incep_3b = self.inception_block_3b(self.incep_3a, ct)
        print(self.incep_3b)
        self.incep_3c = self.inception_block_3c(self.incep_3b, ct)
        print(self.incep_3c)
        return self.incep_3c

    def inception_block_3a(self, x, ct):
        self.inception_3a_1x1 = Conv2D(kernel_size=1,
                                       filters=64,
                                       data_format=self.DATA_FORMAT,
                                       trainable=ct,
                                       name='inception_3a_1x1')
        self.inception_3a_1x1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_1x1_bn')
        self.inception_3a_3x3_reduce = Conv2D(kernel_size=1,
                                              filters=64,
                                              data_format=self.DATA_FORMAT,
                                              trainable=ct,
                                              name='inception_3a_3x3_reduce')
        self.inception_3a_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_3x3_reduce_bn')
        self.inception_3a_3x3 = Conv2D(kernel_size=3,
                                       filters=64,
                                       padding='same',
                                       data_format=self.DATA_FORMAT,
                                       trainable=ct,
                                       name='inception_3a_3x3')
        self.inception_3a_3x3_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_3x3_bn')
        self.inception_3a_double_3x3_reduce = Conv2D(
            kernel_size=1,
            filters=64,
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3a_double_3x3_reduce')
        self.inception_3a_double_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_double_3x3_reduce_bn')
        self.inception_3a_double_3x3_1 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3a_double_3x3_1')
        self.inception_3a_double_3x3_1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_double_3x3_1_bn')
        self.inception_3a_double_3x3_2 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3a_double_3x3_2')
        self.inception_3a_double_3x3_2_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_double_3x3_2_bn')
        self.inception_3a_pool = MaxPooling2D(pool_size=3,
                                              strides=1,
                                              padding='same',
                                              data_format=self.DATA_FORMAT,
                                              name='inception_3a_pool')
        self.inception_3a_pool_proj = Conv2D(kernel_size=1,
                                             filters=32,
                                             data_format=self.DATA_FORMAT,
                                             trainable=ct,
                                             name='inception_3a_pool_proj')
        self.inception_3a_pool_proj_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3a_pool_proj_bn')

        x1 = self.inception_3a_1x1(x)
        x1 = self.inception_3a_1x1_bn(x1)
        x1 = tf.nn.relu(x1)

        x2 = self.inception_3a_3x3_reduce(x)
        x2 = self.inception_3a_3x3_reduce_bn(x2)
        x2 = tf.nn.relu(x2)
        x2 = self.inception_3a_3x3(x2)
        x2 = self.inception_3a_3x3_bn(x2)
        x2 = tf.nn.relu(x2)

        x3 = self.inception_3a_double_3x3_reduce(x)
        x3 = self.inception_3a_double_3x3_reduce_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3a_double_3x3_1(x3)
        x3 = self.inception_3a_double_3x3_1_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3a_double_3x3_2(x3)
        x3 = self.inception_3a_double_3x3_2_bn(x3)
        x3 = tf.nn.relu(x3)

        x4 = self.inception_3a_pool(x)
        x4 = self.inception_3a_pool_proj(x4)
        x4 = self.inception_3a_pool_proj_bn(x4)
        x4 = tf.nn.relu(x4)

        x = tf.concat([x1, x2, x3, x4], axis=1, name='inception_3a_output')
        return x

    def inception_block_3b(self, x, ct):
        self.inception_3b_1x1 = Conv2D(kernel_size=1,
                                       filters=64,
                                       data_format=self.DATA_FORMAT,
                                       trainable=ct,
                                       name='inception_3b_1x1')
        self.inception_3b_1x1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_1x1_bn')
        self.inception_3b_3x3_reduce = Conv2D(kernel_size=1,
                                              filters=64,
                                              data_format=self.DATA_FORMAT,
                                              trainable=ct,
                                              name='inception_3b_3x3_reduce')
        self.inception_3b_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_3x3_reduce_bn')
        self.inception_3b_3x3 = Conv2D(kernel_size=3,
                                       filters=96,
                                       padding='same',
                                       data_format=self.DATA_FORMAT,
                                       trainable=ct,
                                       name='inception_3b_3x3')
        self.inception_3b_3x3_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_3x3_bn')
        self.inception_3b_double_3x3_reduce = Conv2D(
            kernel_size=1,
            filters=64,
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3b_double_3x3_reduce')
        self.inception_3b_double_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_double_3x3_reduce_bn')
        self.inception_3b_double_3x3_1 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3b_double_3x3_1')
        self.inception_3b_double_3x3_1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_double_3x3_1_bn')
        self.inception_3b_double_3x3_2 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3b_double_3x3_2')
        self.inception_3b_double_3x3_2_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_double_3x3_2_bn')
        self.inception_3b_pool = MaxPooling2D(pool_size=3,
                                              strides=1,
                                              padding='same',
                                              data_format=self.DATA_FORMAT,
                                              name='inception_3b_pool')
        self.inception_3b_pool_proj = Conv2D(kernel_size=1,
                                             filters=64,
                                             data_format=self.DATA_FORMAT,
                                             trainable=ct,
                                             name='inception_3b_pool_proj')
        self.inception_3b_pool_proj_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3b_pool_proj_bn')

        x1 = self.inception_3b_1x1(x)
        x1 = self.inception_3b_1x1_bn(x1)
        x1 = tf.nn.relu(x1)

        x2 = self.inception_3b_3x3_reduce(x)
        x2 = self.inception_3b_3x3_reduce_bn(x2)
        x2 = tf.nn.relu(x2)
        x2 = self.inception_3b_3x3(x2)
        x2 = self.inception_3b_3x3_bn(x2)
        x2 = tf.nn.relu(x2)

        x3 = self.inception_3b_double_3x3_reduce(x)
        x3 = self.inception_3b_double_3x3_reduce_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3b_double_3x3_1(x3)
        x3 = self.inception_3b_double_3x3_1_bn(x3)
        x3 = tf.nn.relu(x3)
        x3 = self.inception_3b_double_3x3_2(x3)
        x3 = self.inception_3b_double_3x3_2_bn(x3)
        x3 = tf.nn.relu(x3)

        x4 = self.inception_3b_pool(x)
        x4 = self.inception_3b_pool_proj(x4)
        x4 = self.inception_3b_pool_proj_bn(x4)
        x4 = tf.nn.relu(x4)

        x = tf.concat([x1, x2, x3, x4], axis=1, name='inception_3b_output')
        return x

    def inception_block_3c(self, x, ct):
        self.inception_3c_double_3x3_reduce = Conv2D(
            kernel_size=1,
            filters=64,
            padding='valid',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3c_double_3x3_reduce')
        self.inception_3c_double_3x3_reduce_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3c_double_3x3_reduce_bn')
        self.inception_3c_double_3x3_1 = Conv2D(
            kernel_size=3,
            filters=96,
            padding='same',
            data_format=self.DATA_FORMAT,
            trainable=ct,
            name='inception_3c_double_3x3_1')
        self.inception_3c_double_3x3_1_bn = BatchNormalization(
            axis=1, trainable=ct, name='inception_3c_double_3x3_1_bn')

        x = self.inception_3c_double_3x3_reduce(x)
        x = self.inception_3c_double_3x3_reduce_bn(x)
        x = tf.nn.relu(x, name='inception_3c_relu_double_3x3_reduce_inp')

        x = self.inception_3c_double_3x3_1(x)
        x = self.inception_3c_double_3x3_1_bn(x)
        print(x)
        x = tf.nn.relu(x, name='inception_3c_relu_double_3x3_1_inp')
        x = tf.reshape(x, (-1, self.frm_num, 96, 28, 28), name='r2Dto3D')
        x = tf.transpose(x, [0, 2, 1, 3, 4])
        return x

    def resnet_3d_part(self, x, ct):
        self.res3_output = self.res3_block(x, ct)
        print(self.res3_output)
        self.res4_output = self.res4_block(self.res3_output, ct)
        print(self.res4_output)
        self.res5_output = self.res5_block(self.res4_output, ct)
        print(self.res5_output)
        return self.res5_output

    def res3_block(self, x, ct):
        self.res3a_2 = Conv3D(kernel_size=3,
                              filters=128,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res3a_2')
        self.res3a_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res3a_bn')
        self.res3b_1 = Conv3D(kernel_size=3,
                              filters=128,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res3b_1')
        self.res3b_1_bn = BatchNormalization(axis=1,
                                             trainable=ct,
                                             name='res3b_1_bn')
        self.res3b_2 = Conv3D(kernel_size=3,
                              filters=128,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res3b_2')
        self.res3b_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res3b_bn')

        x1 = self.res3a_2(x)

        x2 = self.res3a_2(x)
        x2 = self.res3a_bn(x2)
        x2 = tf.nn.relu(x2, name='res3a_relu')
        x2 = self.res3b_1(x2)
        x2 = self.res3b_1_bn(x2)
        x2 = tf.nn.relu(x2, name='res3b_1_relu')
        x2 = self.res3b_2(x2)

        x = x1 + x2
        x = self.res3b_bn(x)
        x = tf.nn.relu(x, name='res3b')
        return x

    def res4_block(self, x, ct):
        self.res4a_1 = Conv3D(kernel_size=3,
                              filters=256,
                              strides=2,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res4a_1')
        self.res4a_1_bn = BatchNormalization(axis=1,
                                             trainable=ct,
                                             name='res4a_1_bn')
        self.res4a_2 = Conv3D(kernel_size=3,
                              filters=256,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res4a_2')
        self.res4a_down = Conv3D(kernel_size=3,
                                 filters=256,
                                 strides=2,
                                 padding='same',
                                 data_format=self.DATA_FORMAT,
                                 trainable=ct,
                                 name='res4a_down')
        self.res4a_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res4a_bn')
        self.res4b_1 = Conv3D(kernel_size=3,
                              filters=256,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res4b_1')
        self.res4b_1_bn = BatchNormalization(axis=1,
                                             trainable=ct,
                                             name='res4b_1_bn')
        self.res4b_2 = Conv3D(kernel_size=3,
                              filters=256,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res4b_2')
        self.res4b_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res4b_bn')

        x1 = self.res4a_1(x)
        x1 = self.res4a_1_bn(x1)
        x1 = tf.nn.relu(x1, name='res4a_1_relu')
        x1 = self.res4a_2(x1)
        x2 = self.res4a_down(x)
        x = x1 + x2

        x1 = x
        x2 = self.res4a_bn(x)
        x2 = tf.nn.relu(x2, name='res4a_relu')
        x2 = self.res4b_1(x2)
        x2 = self.res4b_1_bn(x2)
        x2 = tf.nn.relu(x2, name='res4b_1_relu')
        x2 = self.res4b_2(x2)
        x = x1 + x2

        x = self.res4b_bn(x)
        x = tf.nn.relu(x, name='res4b_relu')

        return x

    def res5_block(self, x, ct):
        self.res5a_1 = Conv3D(kernel_size=3,
                              filters=512,
                              strides=2,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res5a_1')
        self.res5a_1_bn = BatchNormalization(axis=1,
                                             trainable=ct,
                                             name='res5a_1_bn')
        self.res5a_2 = Conv3D(kernel_size=3,
                              filters=512,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res5a_2')
        self.res5a_down = Conv3D(kernel_size=3,
                                 filters=512,
                                 strides=2,
                                 padding='same',
                                 data_format=self.DATA_FORMAT,
                                 trainable=ct,
                                 name='res5a_down')
        self.res5a_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res5a_bn')
        self.res5b_1 = Conv3D(kernel_size=3,
                              filters=512,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res5b_1')
        self.res5b_1_bn = BatchNormalization(axis=1,
                                             trainable=ct,
                                             name='res5b_1_bn')
        self.res5b_2 = Conv3D(kernel_size=3,
                              filters=512,
                              strides=1,
                              padding='same',
                              data_format=self.DATA_FORMAT,
                              trainable=ct,
                              name='res5b_2')
        self.res5b_bn = BatchNormalization(axis=1,
                                           trainable=ct,
                                           name='res5b_bn')

        x1 = self.res5a_1(x)
        x1 = self.res5a_1_bn(x1)
        x1 = tf.nn.relu(x1, name='res5a_1_relu')
        x1 = self.res5a_2(x1)

        x2 = self.res5a_down(x)

        x = x1 + x2

        x1 = x
        x2 = self.res5a_bn(x)
        x2 = tf.nn.relu(x2, name='res5a_relu')
        x2 = self.res5b_1(x2)
        x2 = self.res5b_1_bn(x2)
        x2 = tf.nn.relu(x2, name='res5b_1_relu')
        x2 = self.res5b_2(x2)

        x = x1 + x2
        x = self.res5b_bn(x)
        x = tf.nn.relu(x, name='res5b_relu')
        return x

    def init_conv2d_layer(self, layer, resnet, incep):
        if layer.name in resnet:
            weight = resnet[layer.name]
            weight[0] = np.transpose(weight[0], [2, 3, 1, 0])
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        elif layer.name in incep:
            weight = incep[layer.name]
            weight[0] = np.transpose(weight[0], [2, 3, 1, 0])
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        else:
            print(layer.name, "can not find the corresponding weight")

    def init_conv3d_layer(self, layer, resnet, incep):
        if layer.name in resnet:
            weight = resnet[layer.name]
            weight[0] = np.transpose(weight[0], [2, 3, 4, 1, 0])
            if layer.name == "res3a_2":
                weight[0] = weight[0][:, :, :, :96, :]
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        elif layer.name in incep:
            weight = incep[layer.name]
            weight[0] = np.transpose(weight[0], [2, 3, 4, 1, 0])
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        else:
            print(layer.name, 'can not find the corresponding weights')

    def init_bn_layer(self, layer, resnet, incep):
        if layer.name in resnet:
            weight = resnet[layer.name]
            weight = [np.squeeze(w) for w in weight]
            layer.set_weights(weight)
        elif layer.name in incep:
            weight = incep[layer.name]
            weight = [np.squeeze(w) for w in weight]
            layer.set_weights(weight)
        else:
            print(layer.name, "can not find the corresponding weights.")

    def init_dense_layer(self, layer, resnet, incep):
        if layer.name in resnet:
            weight = resnet[layer.name]
            weight[0] = np.transpose(weight[0])
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        elif layer.name in incep:
            weight = incep[layer.name]
            weight[0] = np.transpose(weight[0])
            weight[1] = np.squeeze(weight[1])
            layer.set_weights(weight)
        else:
            print(layer.name, "can not find corresponding weights.")

    def init_weights(self, resnet, incep, sess, saver):
        self.init_conv2d_layer(self.conv1_7x7_s2, resnet, incep)
        self.init_bn_layer(self.conv1_7x7_s2_bn, resnet, incep)
        self.init_conv2d_layer(self.conv2_3x3_reduce, resnet, incep)
        self.init_bn_layer(self.conv2_3x3_reduce_bn, resnet, incep)
        self.init_conv2d_layer(self.conv2_3x3, resnet, incep)
        self.init_bn_layer(self.conv2_3x3_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_1x1, resnet, incep)
        self.init_bn_layer(self.inception_3a_1x1_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_3x3_reduce, resnet, incep)
        self.init_bn_layer(self.inception_3a_3x3_reduce_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_3x3, resnet, incep)
        self.init_bn_layer(self.inception_3a_3x3_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_double_3x3_reduce, resnet,
                               incep)
        self.init_bn_layer(self.inception_3a_double_3x3_reduce_bn, resnet,
                           incep)
        self.init_conv2d_layer(self.inception_3a_double_3x3_1, resnet, incep)
        self.init_bn_layer(self.inception_3a_double_3x3_1_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_double_3x3_2, resnet, incep)
        self.init_bn_layer(self.inception_3a_double_3x3_2_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3a_pool_proj, resnet, incep)
        self.init_bn_layer(self.inception_3a_pool_proj_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_1x1, resnet, incep)
        self.init_bn_layer(self.inception_3b_1x1_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_3x3_reduce, resnet, incep)
        self.init_bn_layer(self.inception_3b_3x3_reduce_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_3x3, resnet, incep)
        self.init_bn_layer(self.inception_3b_3x3_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_double_3x3_reduce, resnet,
                               incep)
        self.init_bn_layer(self.inception_3b_double_3x3_reduce_bn, resnet,
                           incep)
        self.init_conv2d_layer(self.inception_3b_double_3x3_1, resnet, incep)
        self.init_bn_layer(self.inception_3b_double_3x3_1_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_double_3x3_2, resnet, incep)
        self.init_bn_layer(self.inception_3b_double_3x3_2_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3b_pool_proj, resnet, incep)
        self.init_bn_layer(self.inception_3b_pool_proj_bn, resnet, incep)
        self.init_conv2d_layer(self.inception_3c_double_3x3_reduce, resnet,
                               incep)
        self.init_bn_layer(self.inception_3c_double_3x3_reduce_bn, resnet,
                           incep)
        self.init_conv2d_layer(self.inception_3c_double_3x3_1, resnet, incep)
        self.init_bn_layer(self.inception_3c_double_3x3_1_bn, resnet, incep)

        self.init_conv3d_layer(self.res3a_2, resnet, incep)
        self.init_bn_layer(self.res3a_bn, resnet, incep)
        self.init_conv3d_layer(self.res3b_1, resnet, incep)
        self.init_bn_layer(self.res3b_1_bn, resnet, incep)
        self.init_conv3d_layer(self.res3b_2, resnet, incep)
        self.init_bn_layer(self.res3b_bn, resnet, incep)
        self.init_conv3d_layer(self.res4a_1, resnet, incep)
        self.init_bn_layer(self.res4a_1_bn, resnet, incep)
        self.init_conv3d_layer(self.res4a_2, resnet, incep)
        self.init_conv3d_layer(self.res4a_down, resnet, incep)
        self.init_bn_layer(self.res4a_bn, resnet, incep)
        self.init_conv3d_layer(self.res4b_1, resnet, incep)
        self.init_bn_layer(self.res4b_1_bn, resnet, incep)
        self.init_conv3d_layer(self.res4b_2, resnet, incep)
        self.init_bn_layer(self.res4b_bn, resnet, incep)
        self.init_conv3d_layer(self.res5a_1, resnet, incep)
        self.init_bn_layer(self.res5a_1_bn, resnet, incep)
        self.init_conv3d_layer(self.res5a_2, resnet, incep)
        self.init_conv3d_layer(self.res5a_down, resnet, incep)
        self.init_bn_layer(self.res5a_bn, resnet, incep)
        self.init_conv3d_layer(self.res5b_1, resnet, incep)
        self.init_bn_layer(self.res5b_1_bn, resnet, incep)
        self.init_conv3d_layer(self.res5b_2, resnet, incep)
        self.init_bn_layer(self.res5b_bn, resnet, incep)

        self.init_dense_layer(self.fc8, resnet, incep)
        w = self.res3a_bn.get_weights()
        print('res3a_bn equal: ', np.all(w[0] == resnet['res3a_bn'][0]))
        saver = tf.train.Saver()
        saver.save(
            sess,
            '/home/chenhaoran/ECO-efficient-video-understanding/saves/init_model.ckpt'
        )
        w = self.res3a_bn.get_weights()
        print('res3a_bn equal: ', np.all(w[0] == resnet['res3a_bn'][0]))

    def load_save(self, sess, path2save, saver):
        saver.restore(sess, path2save)
        print('The model has been restored.')
Exemple #11
0
def MobilenetV2(inputs, alpha=1.0, num_classes=1000, include_top=True):
    """Implementation of MobilenetV2"""

    with tf.variable_scope('MobilenetV2'):
        with tf.variable_scope('Conv'):
            first_block = _make_divisible(32 * alpha, 8)
            x = Conv2D(first_block,
                       3,
                       strides=2,
                       padding='same',
                       use_bias=False,
                       name='Conv2D')(inputs)
            x = BatchNormalization(epsilon=1e-3,
                                   momentum=0.999,
                                   name='BatchNorm')(x)
            x = Activation(relu6)(x)

        x = inverted_residuals(x,
                               16,
                               3,
                               stride=1,
                               expansion=1,
                               block_id=0,
                               alpha=alpha,
                               residual=False)

        x = inverted_residuals(x,
                               24,
                               3,
                               stride=2,
                               expansion=6,
                               block_id=1,
                               alpha=alpha,
                               residual=False)
        x = inverted_residuals(x,
                               24,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=2,
                               alpha=alpha)

        x = inverted_residuals(x,
                               32,
                               3,
                               stride=2,
                               expansion=6,
                               block_id=3,
                               alpha=alpha,
                               residual=False)
        x = inverted_residuals(x,
                               32,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=4,
                               alpha=alpha)
        x = inverted_residuals(x,
                               32,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=5,
                               alpha=alpha)

        x = inverted_residuals(x,
                               64,
                               3,
                               stride=2,
                               expansion=6,
                               block_id=6,
                               alpha=alpha,
                               residual=False)
        x = inverted_residuals(x,
                               64,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=7,
                               alpha=alpha)
        x = inverted_residuals(x,
                               64,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=8,
                               alpha=alpha)
        x = inverted_residuals(x,
                               64,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=9,
                               alpha=alpha)

        x = inverted_residuals(x,
                               96,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=10,
                               alpha=alpha,
                               residual=False)
        x = inverted_residuals(x,
                               96,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=11,
                               alpha=alpha)
        x = inverted_residuals(x,
                               96,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=12,
                               alpha=alpha)

        x = inverted_residuals(x,
                               160,
                               3,
                               stride=2,
                               expansion=6,
                               block_id=13,
                               alpha=alpha,
                               residual=False)
        x = inverted_residuals(x,
                               160,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=14,
                               alpha=alpha)
        x = inverted_residuals(x,
                               160,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=15,
                               alpha=alpha)

        x = inverted_residuals(x,
                               320,
                               3,
                               stride=1,
                               expansion=6,
                               block_id=16,
                               alpha=alpha,
                               residual=False)

        x = Conv2D(_make_divisible(1280 * alpha, 8), 1, use_bias=False)(x)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999)(x)
        x = Activation(relu6)(x)

        if include_top:
            with tf.variable_scope('Predictions'):
                x = AvgPool2D((7, 7))(x)
                x = Conv2D(num_classes, 1, activation='softmax',
                           use_bias=True)(x)
                x = Reshape((num_classes, ), name='Reshape_1')(x)
        return x
Exemple #12
0
def inverted_residuals(inputs,
                       filters,
                       kernel,
                       stride,
                       expansion=1,
                       alpha=1.0,
                       atrous_rate=1,
                       residual=True,
                       block_id=None):
    """Define Inveterd Residuals Architecture.

  inputs --> 

  1x1 Conv --> Batch Norm --> Relu6 -->
  3x3 DepthWise --> Batch Norm --> Relu6 -->
  1x1 Conv --> Batch Norm --> Relu6
  
  -- > Outputs

  Args:
    inputs - tf.float32 4D Tensor
    filters: number of expected output channels
    strides:
  """
    scope = 'expanded_conv_' + str(block_id) if block_id else 'expanded_conv'
    with tf.variable_scope(scope):
        # #######################################################
        # Expand and Pointwise
        # #######################################################
        if block_id:
            with tf.variable_scope('expand'):
                in_channels = inputs.get_shape().as_list()[-1]
                x = Conv2D(filters=expansion * in_channels,
                           kernel_size=1,
                           padding='SAME',
                           use_bias=False,
                           activation=None)(inputs)
                x = BatchNormalization(epsilon=1e-3, momentum=0.999)(x)
                x = Activation(relu6)(x)
        else:
            x = inputs
        # ########################################################
        # Depthwise
        # ########################################################
        with tf.variable_scope('depthwise'):
            x = DepthwiseConv2D(kernel_size=kernel,
                                strides=stride,
                                activation=None,
                                use_bias=False,
                                dilation_rate=(atrous_rate, atrous_rate),
                                padding='SAME')(x)
            x = BatchNormalization(epsilon=1e-3, momentum=0.999)(x)
            x = Activation(relu6)(x)

        # ########################################################
        # Linear Projection
        # ########################################################
        with tf.variable_scope('project'):
            pointwise_filters = int(filters * alpha)
            pointwise_filters = _make_divisible(pointwise_filters,
                                                8)  # Why 8???
            x = Conv2D(filters=pointwise_filters,
                       kernel_size=1,
                       padding='SAME',
                       use_bias=False,
                       activation=None)(x)
            x = BatchNormalization(epsilon=1e-3, momentum=0.999)(x)
            x = Activation(relu6)(x)
            if residual:
                x = Add()([inputs, x])

            return x
Exemple #13
0
def batch_normalization(
        inputs,
        axis=-1,
        momentum=0.99,
        epsilon=1e-3,
        center=True,
        scale=True,
        beta_initializer=init_ops.zeros_initializer(),
        gamma_initializer=init_ops.ones_initializer(),
        moving_mean_initializer=init_ops.zeros_initializer(),
        moving_variance_initializer=init_ops.ones_initializer(),
        beta_regularizer=None,
        gamma_regularizer=None,
        beta_constraint=None,
        gamma_constraint=None,
        training=False,
        trainable=True,
        name=None,
        reuse=None,
        renorm=False,
        renorm_clipping=None,
        renorm_momentum=0.99,
        fused=None,
        virtual_batch_size=None,
        adjustment=None):
    layer = BatchNormalization(
        axis=axis,
        momentum=momentum,
        epsilon=epsilon,
        center=center,
        scale=scale,
        beta_initializer=beta_initializer,
        gamma_initializer=gamma_initializer,
        moving_mean_initializer=moving_mean_initializer,
        moving_variance_initializer=moving_variance_initializer,
        beta_regularizer=beta_regularizer,
        gamma_regularizer=gamma_regularizer,
        beta_constraint=beta_constraint,
        gamma_constraint=gamma_constraint,
        renorm=renorm,
        renorm_clipping=renorm_clipping,
        renorm_momentum=renorm_momentum,
        fused=fused,
        trainable=trainable,
        virtual_batch_size=virtual_batch_size,
        adjustment=adjustment,
        name=name,
        _reuse=reuse,
        _scope=name)
    res = layer.apply(inputs, training=training)

    if not S("batch_norm.transform"):
        return res

    # get moving mean and variance
    moving_mean, moving_variance = layer.moving_mean, layer.moving_variance
    beta_offset, gamma_scale = layer.beta, layer.gamma

    if GLOBAL["first_layer"]:
        GLOBAL["first_layer"] = False
    else:
        pass
        # print("reformulate batchnorm")

        # apply transformation
        # --------------------
        # moving_mean = variableFromSettings([],hiddenVar=moving_mean)[0]
        # moving_variance = variableFromSettings([],hiddenVar=moving_variance)[0]
        # beta_offset = variableFromSettings([],hiddenVar=beta_offset)[0]
        # gamma_scale = variableFromSettings([],hiddenVar=gamma_scale)[0]

        # apply transformation (no var)
        # --------------------
        # sample_size = S("binom.sample_size")
        # S("binom.sample_size",set=sample_size*4)
        # gamma_scale = gamma_scale/tf.sqrt(moving_variance+layer.epsilon)
        # gamma_scale = variableFromSettings([],hiddenVar=gamma_scale/tf.sqrt(moving_variance+layer.epsilon))[0]
        # moving_variance = 0*moving_variance+1
        # moving_variance = tf.ones_like(moving_variance)
        # S("binom.sample_size",set=sample_size)

        # moving_variance = 1.0/variableFromSettings([],hiddenVar=1.0/moving_variance)[0]
        # moving_mean = fixed_point(moving_mean,8)
        # moving_mean, _ = variableFromSettings([],hiddenVar=moving_mean)
        # moving_variance = next_base2(moving_variance, strict_positive=True)
        # moving_variance = 2**tf.ceil(tf.log(tf.maximum(tf.abs(moving_variance),0))/tf.log(2.0))
    # tf.summary.histogram("bn_mean",moving_mean)
    # tf.summary.histogram("bn_var",moving_variance)

    # set moving mean and variance
    layer.moving_mean, layer.moving_variance = moving_mean, moving_variance
    layer.beta, layer.gamma = beta_offset, gamma_scale

    # reapply
    res = layer.apply(inputs, training=training)

    return res
Exemple #14
0
from tensorflow.layers import Dropout, BatchNormalization, Dense, Conv2D
import tensorflow as tf
"""
args = dotdict({
    'lr': 0.001,
    'dropout': 0.3,
    'epochs': 5,
    'batch_size': 64,
    'num_channels': 64,
})
"""

NUM_CHANNELS = 64

BN1 = BatchNormalization()
BN2 = BatchNormalization()
BN3 = BatchNormalization()
BN4 = BatchNormalization()
BN5 = BatchNormalization()
BN6 = BatchNormalization()

CONV1 = Conv2D(NUM_CHANNELS, kernel_size=3, strides=1, padding='same')
CONV2 = Conv2D(NUM_CHANNELS, kernel_size=3, strides=1, padding='same')
CONV3 = Conv2D(NUM_CHANNELS, kernel_size=3, strides=1)
CONV4 = Conv2D(NUM_CHANNELS, kernel_size=3, strides=1)

FC1 = Dense(128)
FC2 = Dense(64)
FC3 = Dense(7)
Exemple #15
0
    def __init__(self):
        super(Encoder, self).__init__()
        arg = {'activation': tf.nn.relu, 'padding': 'same'}
        self.conv_11 = Conv2D(name='e_conv_11',
                              filters=64,
                              kernel_size=7,
                              strides=(2, 2),
                              **arg)
        self.conv_12 = Conv2D(name='e_conv_12',
                              filters=64,
                              kernel_size=7,
                              strides=(2, 2),
                              **arg)
        self.pool_1 = MaxPooling2D(name='e_pool_1',
                                   pool_size=4,
                                   strides=(2, 2),
                                   padding='same')
        self.compress_11 = AveragePooling2D(name='e_comp_11',
                                            pool_size=5,
                                            strides=(3, 3),
                                            padding='same')
        self.compress_12 = Flatten()
        self.compress_13 = Dense(name='e_comp_13',
                                 units=128,
                                 activation=None,
                                 use_bias=False)
        #  activity_regularizer=tf.keras.regularizers.l2(l=0.01))
        self.batch_norm_1 = BatchNormalization(name='e_bn_1')
        self.drop_1 = Dropout(name='e_drop_1', rate=0.5)

        self.conv_21 = Conv2D(name='e_conv_21',
                              filters=128,
                              kernel_size=5,
                              strides=(1, 1),
                              **arg)
        self.conv_22 = Conv2D(name='e_conv_22',
                              filters=128,
                              kernel_size=5,
                              strides=(1, 1),
                              **arg)
        self.pool_2 = MaxPooling2D(name='e_pool_2',
                                   pool_size=4,
                                   strides=(2, 2),
                                   padding='same')
        self.compress_21 = AveragePooling2D(name='e_comp_21',
                                            pool_size=5,
                                            strides=(3, 3),
                                            padding='same')
        self.compress_22 = Flatten()
        self.compress_23 = Dense(name='e_comp_23',
                                 units=128,
                                 activation=None,
                                 use_bias=False)
        #  activity_regularizer=tf.keras.regularizers.l2(l=0.01))
        self.batch_norm_2 = BatchNormalization(name='e_bn_2')
        self.drop_2 = Dropout(name='e_drop_2', rate=0.5)

        self.conv_31 = Conv2D(name='e_conv_31',
                              filters=256,
                              kernel_size=3,
                              strides=(1, 1),
                              **arg)
        self.conv_32 = Conv2D(name='e_conv_32',
                              filters=256,
                              kernel_size=3,
                              strides=(1, 1),
                              **arg)
        self.pool_3 = MaxPooling2D(name='e_pool_3',
                                   pool_size=2,
                                   strides=(2, 2),
                                   padding='same')
        self.compress_31 = AveragePooling2D(name='e_comp_31',
                                            pool_size=3,
                                            strides=(1, 1),
                                            padding='same')
        self.compress_32 = Flatten()
        self.compress_33 = Dense(name='e_comp_33',
                                 units=128,
                                 activation=None,
                                 use_bias=False)
        #  activity_regularizer=tf.keras.regularizers.l2(l=0.01))
        self.batch_norm_3 = BatchNormalization(name='e_bn_3')
        self.drop_3 = Dropout(name='e_drop_3', rate=0.5)