我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用keras.layers.LeakyReLU()。
def model_generator(): model = Sequential() nch = 256 reg = lambda: l1l2(l1=1e-7, l2=1e-7) h = 5 model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg())) model.add(BatchNormalization(mode=0)) model.add(Reshape(dim_ordering_shape((nch, 4, 4)))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg())) model.add(BatchNormalization(mode=0, axis=1)) model.add(LeakyReLU(0.2)) model.add(UpSampling2D(size=(2, 2))) model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg())) model.add(Activation('sigmoid')) return model
def model_discriminator(input_shape=(1, 28, 28), dropout_rate=0.5): d_input = dim_ordering_input(input_shape, name="input_x") nch = 512 # nch = 128 H = Convolution2D(int(nch / 2), 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(d_input) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Convolution2D(nch, 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Flatten()(H) H = Dense(int(nch / 2))(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) d_V = Dense(1, activation='sigmoid')(H) return Model(d_input, d_V)
def basic_D(input_shape, ndf, n_layers=3, kw=4, dropout=0.0, use_sigmoid=False, **kwargs): padw = (kw-1)/2 input = Input(input_shape) x = Conv2D(ndf, (kw,kw), strides=(2,2), padding='same')(input) x = LeakyReLU(0.2)(x) for i in range(n_layers-1): x = Conv2D(ndf*min(2**(i+1), 8), (kw,kw), strides=(2,2), padding='same')(x) x = normalize()(x) if dropout > 0.: x = Dropout(dropout)(x) x = LeakyReLU(0.2)(x) x = Conv2D(ndf*min(2**(n_layers+1), 8), (kw,kw), strides=(1,1), padding='same')(x) x = normalize()(x) x = LeakyReLU(0.2)(x) x = Conv2D(1, (kw,kw), strides=(1,1), padding='same')(x) if use_sigmoid: x = Activation('sigmoid')(x) model = Model(input, x, name=kwargs.get('name',None)) print('Model basic D:') model.summary() return model
def _upscale_block(self, ip, id): ''' As per suggestion from http://distill.pub/2016/deconv-checkerboard/, I am swapping out SubPixelConvolution to simple Nearest Neighbour Upsampling ''' init = ip x = Convolution2D(128, 3, 3, activation="linear", border_mode='same', name='sr_res_upconv1_%d' % id, init=self.init)(init) x = LeakyReLU(alpha=0.25, name='sr_res_up_lr_%d_1_1' % id)(x) x = UpSampling2D(name='sr_res_upscale_%d' % id)(x) #x = SubPixelUpscaling(r=2, channels=32)(x) x = Convolution2D(128, 3, 3, activation="linear", border_mode='same', name='sr_res_filter1_%d' % id, init=self.init)(x) x = LeakyReLU(alpha=0.3, name='sr_res_up_lr_%d_1_2' % id)(x) return x
def residual_block(nb_filter, repetition): '''(down dample ->) residual blocks ....... -> BatchNormalization -> LeakyReLU''' from keras.layers import merge def f(x): for i in xrange(repetition): if i == 0: y = conv2d(nb_filter, downsample=True, k_size=1)(x) z = conv2d(nb_filter, downsample=True)(x) else: y = x z = bn_lrelu(0.01)(x) z = conv2d(nb_filter)(z) z = bn_lrelu(0.01)(z) z = conv2d(nb_filter)(z) x = merge([y, z], mode='sum') return bn_lrelu(0.01)(x) return f
def build_discriminator(img_shape=(1, 28, 28), dropout_rate=0.25, l2_reg=0.): # Build Discriminative model ... d_input = Input(shape=img_shape) H = Convolution2D(256, 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(d_input) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Convolution2D(512, 5, 5, subsample=(2, 2), border_mode='same', activation='relu')(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) H = Flatten()(H) H = Dense(256)(H) H = LeakyReLU(0.2)(H) H = Dropout(dropout_rate)(H) d_output = Dense(2, activation='softmax')(H) model = Model(d_input, d_output) return model
def model_discriminator(): nch = 256 h = 5 reg = lambda: l1l2(l1=1e-7, l2=1e-7) c1 = Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg(), input_shape=dim_ordering_shape((3, 32, 32))) c2 = Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()) c3 = Convolution2D(nch, h, h, border_mode='same', W_regularizer=reg()) c4 = Convolution2D(1, h, h, border_mode='same', W_regularizer=reg()) model = Sequential() model.add(c1) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c2) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c3) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(LeakyReLU(0.2)) model.add(c4) model.add(AveragePooling2D(pool_size=(4, 4), border_mode='valid')) model.add(Flatten()) model.add(Activation('sigmoid')) return model
def build_critic(c=0.01): # build a relatively standard conv net with LeakyReLUs cnn = Sequential() cnn.add(Conv2D(32, 3, padding='same', strides=(2, 2), input_shape=(1, 28, 28))) cnn.add(LeakyReLU()) cnn.add(Dropout(0.3)) cnn.add(Conv2D(64, 3, padding='same', strides=(1, 1))) cnn.add(LeakyReLU()) cnn.add(Dropout(0.3)) cnn.add(Conv2D(128, 3, padding='same', strides=(2, 2))) cnn.add(LeakyReLU()) cnn.add(Dropout(0.3)) cnn.add(Conv2D(256, 3, padding='same', strides=(1, 1))) cnn.add(LeakyReLU()) cnn.add(Dropout(0.3)) cnn.add(Flatten()) image = Input(shape=(1, 28, 28)) features = cnn(image) fake = Dense(1, activation='linear', name='critic')(features) return Model(inputs=image, outputs=fake)
def discriminator(self): if self.D: return self.D self.D = Sequential() depth = 64 dropout = 0.4 # In: 28 x 28 x 1, depth = 1 # Out: 14 x 14 x 1, depth=64 input_shape = (self.img_rows, self.img_cols, self.channel) self.D.add(Conv2D(depth*1, 5, strides=2, input_shape=input_shape,\ padding='same')) self.D.add(LeakyReLU(alpha=0.2)) self.D.add(Dropout(dropout)) self.D.add(Conv2D(depth*2, 5, strides=2, padding='same')) self.D.add(LeakyReLU(alpha=0.2)) self.D.add(Dropout(dropout)) self.D.add(Conv2D(depth*4, 5, strides=2, padding='same')) self.D.add(LeakyReLU(alpha=0.2)) self.D.add(Dropout(dropout)) self.D.add(Conv2D(depth*8, 5, strides=1, padding='same')) self.D.add(LeakyReLU(alpha=0.2)) self.D.add(Dropout(dropout)) # Out: 1-dim probability self.D.add(Flatten()) self.D.add(Dense(1)) self.D.add(Activation('sigmoid')) self.D.summary() return self.D
def Discriminator(y_dash, dropout=0.4, lr=0.00001, PATH="Dis.h5"): """Creates a discriminator model that takes an image as input and outputs a single value, representing whether the input is real or generated. Unlike normal GANs, the output is not sigmoid and does not represent a probability! Instead, the output should be as large and negative as possible for generated inputs and as large and positive as possible for real inputs.""" model = Sequential() model.add(Conv1D(input_shape=(y_dash.shape[1], y_dash.shape[2]), nb_filter=25, filter_length=4, border_mode='same')) model.add(LeakyReLU()) model.add(Dropout(dropout)) model.add(MaxPooling1D()) model.add(Conv1D(nb_filter=10, filter_length=4, border_mode='same')) model.add(LeakyReLU()) model.add(Dropout(dropout)) model.add(MaxPooling1D()) model.add(Flatten()) model.add(Dense(64)) model.add(LeakyReLU()) model.add(Dropout(dropout)) model.add(Dense(1)) model.add(Activation('linear')) opt = Adam(lr, beta_1=0.5, beta_2=0.9) #reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.9, patience=30, min_lr=0.000001, verbose=1) checkpoint_D = ModelCheckpoint( filepath=PATH, verbose=1, save_best_only=True) model.compile(optimizer=opt, loss=wasserstein_loss, metrics=['accuracy']) return model, checkpoint_D
def test_delete_channels_advanced_activations(channel_index, data_format): layer_test_helper_flatten_2d(LeakyReLU(), channel_index, data_format) layer_test_helper_flatten_2d(ELU(), channel_index, data_format) layer_test_helper_flatten_2d(ThresholdedReLU(), channel_index, data_format)
def build(features_shape, audio_spectrogram_size): model = Sequential() model.add(Flatten(input_shape=features_shape)) model.add(BatchNormalization()) model.add(Dense(1024, kernel_initializer='he_normal', name='dense1')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(1024, kernel_initializer='he_normal', name='dense2')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(1024, kernel_initializer='he_normal', name='dense3')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(audio_spectrogram_size, name='output')) model.summary() return VideoToSpeechNet(model)
def append_gan_network(self, true_X_input): # Normalize the inputs via custom VGG Normalization layer x = Normalize(type="gan", value=127.5, name="gan_normalize")(true_X_input) x = Convolution2D(64, self.k, self.k, border_mode='same', name='gan_conv1_1')(x) x = LeakyReLU(0.3, name="gan_lrelu1_1")(x) x = Convolution2D(64, self.k, self.k, border_mode='same', name='gan_conv1_2', subsample=(2, 2))(x) x = LeakyReLU(0.3, name='gan_lrelu1_2')(x) x = BatchNormalization(mode=self.mode, axis=channel_axis, name='gan_batchnorm1_1')(x) filters = [128, 256] if self.small_model else [128, 256, 512] for i, nb_filters in enumerate(filters): for j in range(2): subsample = (2, 2) if j == 1 else (1, 1) x = Convolution2D(nb_filters, self.k, self.k, border_mode='same', subsample=subsample, name='gan_conv%d_%d' % (i + 2, j + 1))(x) x = LeakyReLU(0.3, name='gan_lrelu_%d_%d' % (i + 2, j + 1))(x) x = BatchNormalization(mode=self.mode, axis=channel_axis, name='gan_batchnorm%d_%d' % (i + 2, j + 1))(x) x = Flatten(name='gan_flatten')(x) output_dim = 128 if self.small_model else 1024 x = Dense(output_dim, name='gan_dense1')(x) x = LeakyReLU(0.3, name='gan_lrelu5')(x) gan_regulrizer = AdversarialLossRegularizer(weight=self.adversarial_loss_weight) x = Dense(2, activation="softmax", activity_regularizer=gan_regulrizer, name='gan_output')(x) return x
def create_sr_model(self, ip): x = Convolution2D(self.filters, 5, 5, activation='linear', border_mode='same', name='sr_res_conv1', init=self.init)(ip) x = BatchNormalization(axis=channel_axis, mode=self.mode, name='sr_res_bn_1')(x) x = LeakyReLU(alpha=0.25, name='sr_res_lr1')(x) # x = Convolution2D(self.filters, 5, 5, activation='linear', border_mode='same', name='sr_res_conv2')(x) # x = BatchNormalization(axis=channel_axis, mode=self.mode, name='sr_res_bn_2')(x) # x = LeakyReLU(alpha=0.25, name='sr_res_lr2')(x) nb_residual = 5 if self.small_model else 15 for i in range(nb_residual): x = self._residual_block(x, i + 1) for scale in range(self.nb_scales): x = self._upscale_block(x, scale + 1) scale = 2 ** self.nb_scales tv_regularizer = TVRegularizer(img_width=self.img_width * scale, img_height=self.img_height * scale, weight=self.tv_weight) #self.tv_weight) x = Convolution2D(3, 5, 5, activation='tanh', border_mode='same', activity_regularizer=tv_regularizer, init=self.init, name='sr_res_conv_final')(x) x = Denormalize(name='sr_res_conv_denorm')(x) return x
def _residual_block(self, ip, id): init = ip x = Convolution2D(self.filters, 3, 3, activation='linear', border_mode='same', name='sr_res_conv_' + str(id) + '_1', init=self.init)(ip) x = BatchNormalization(axis=channel_axis, mode=self.mode, name='sr_res_bn_' + str(id) + '_1')(x) x = LeakyReLU(alpha=0.25, name="sr_res_activation_" + str(id) + "_1")(x) x = Convolution2D(self.filters, 3, 3, activation='linear', border_mode='same', name='sr_res_conv_' + str(id) + '_2', init=self.init)(x) x = BatchNormalization(axis=channel_axis, mode=self.mode, name='sr_res_bn_' + str(id) + '_2')(x) m = merge([x, init], mode='sum', name="sr_res_merge_" + str(id)) return m
def bn_lrelu(alpha): '''BatchNormalization -> LeakyReLU''' from keras.layers import BatchNormalization, LeakyReLU def f(x): return LeakyReLU(alpha)(BatchNormalization(mode=0, axis=1)(x)) return f
def __init__(self, obs_shape, act_shape, h_size=64): input_dim = np.prod(obs_shape) + np.prod(act_shape) self.model = Sequential() self.model.add(Dense(h_size, input_dim=input_dim)) self.model.add(LeakyReLU()) self.model.add(Dropout(0.5)) self.model.add(Dense(h_size)) self.model.add(LeakyReLU()) self.model.add(Dropout(0.5)) self.model.add(Dense(1))
def build(video_shape, audio_spectrogram_size): model = Sequential() model.add(ZeroPadding3D(padding=(1, 2, 2), name='zero1', input_shape=video_shape)) model.add(Convolution3D(32, (3, 5, 5), strides=(1, 2, 2), kernel_initializer='he_normal', name='conv1')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max1')) model.add(Dropout(0.25)) model.add(ZeroPadding3D(padding=(1, 2, 2), name='zero2')) model.add(Convolution3D(64, (3, 5, 5), strides=(1, 1, 1), kernel_initializer='he_normal', name='conv2')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max2')) model.add(Dropout(0.25)) model.add(ZeroPadding3D(padding=(1, 1, 1), name='zero3')) model.add(Convolution3D(128, (3, 3, 3), strides=(1, 1, 1), kernel_initializer='he_normal', name='conv3')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name='max3')) model.add(Dropout(0.25)) model.add(TimeDistributed(Flatten(), name='time')) model.add(Dense(1024, kernel_initializer='he_normal', name='dense1')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(1024, kernel_initializer='he_normal', name='dense2')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(2048, kernel_initializer='he_normal', name='dense3')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(2048, kernel_initializer='he_normal', name='dense4')) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.25)) model.add(Dense(audio_spectrogram_size, name='output')) model.summary() return VideoToSpeechNet(model)
def TinyYOLO(input_shape=(3,416,416),num_classes=80,num_priors=5): """Tiny YOLO (v2) architecture # Arguments input_shape: Shape of the input image num_classes: Number of classes (excluding background) # References https://arxiv.org/abs/1612.08242 https://arxiv.org/abs/1506.02640 """ K.set_image_dim_ordering('th') net={} input_tensor = Input(shape=input_shape) net['input'] = input_tensor net['conv1'] = (YOLOConvolution2D(16, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['input']) net['relu1'] = (LeakyReLU(alpha=0.1))(net['conv1']) net['pool1'] = (MaxPooling2D(pool_size=(2, 2),border_mode='valid'))(net['relu1']) net['conv2'] = (YOLOConvolution2D(32, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool1']) net['relu2'] = (LeakyReLU(alpha=0.1))(net['conv2']) net['pool2'] = (MaxPooling2D(pool_size=(2, 2),border_mode='valid'))(net['relu2']) net['conv3'] = (YOLOConvolution2D(64, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool2']) net['relu3'] = (LeakyReLU(alpha=0.1))(net['conv3']) net['pool3'] = (MaxPooling2D(pool_size=(2, 2),border_mode='valid'))(net['relu3']) net['conv4'] = (YOLOConvolution2D(128, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool3']) net['relu4'] = (LeakyReLU(alpha=0.1))(net['conv4']) net['pool4'] = (MaxPooling2D(pool_size=(2, 2),border_mode='valid'))(net['relu4']) net['conv5'] = (YOLOConvolution2D(256, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool4']) net['relu5'] = (LeakyReLU(alpha=0.1))(net['conv5']) net['pool5'] = (MaxPooling2D(pool_size=(2, 2),border_mode='valid'))(net['relu5']) net['conv6'] = (YOLOConvolution2D(512, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool5']) net['relu6'] = (LeakyReLU(alpha=0.1))(net['conv6']) net['pool6'] = (MaxPooling2D(pool_size=(2, 2),strides=(1,1),border_mode='same'))(net['relu6']) net['conv7'] = (YOLOConvolution2D(1024, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['pool6']) net['relu7'] = (LeakyReLU(alpha=0.1))(net['conv7']) net['conv8'] = (YOLOConvolution2D(1024, 3, 3, border_mode='same',subsample=(1,1), epsilon=0.000001))(net['relu7']) net['relu8'] = (LeakyReLU(alpha=0.1))(net['conv8']) net['conv9'] = (Convolution2D(num_priors*(4+num_classes+1), 1, 1, border_mode='same', subsample=(1,1)))(net['relu8']) model = Model(net['input'], net['conv9']) return model