def test_leaky_relu(): np.random.seed(1337) from keras.layers.advanced_activations import LeakyReLU inp = get_standard_values() for alpha in [0., .5, -1.]: layer = LeakyReLU(alpha=alpha) layer.input = K.variable(inp) for train in [True, False]: outp = K.eval(layer.get_output(train)) assert_allclose(outp, inp) layer.input = K.variable(-inp) for train in [True, False]: outp = K.eval(layer.get_output(train)) assert_allclose(outp, -inp*alpha) config = layer.get_config() assert config['alpha'] == alpha
import keras from keras.models import Sequential,Input,Model from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.layers.normalization import BatchNormalization from keras.layers.advanced_activations import LeakyReLU batch_size = 64 epochs = 3 num_classes = 10 #The Model with some Dropout fashion_model = Sequential() fashion_model.add(Conv2D(32, kernel_size=(3, 3), activation='linear', padding='same', input_shape=(28, 28, 1))) fashion_model.add(LeakyReLU(alpha=0.1)) fashion_model.add(MaxPooling2D((2, 2), padding='same')) fashion_model.add(Dropout(0.25)) fashion_model.add(Conv2D(64, (3, 3), activation='linear', padding='same')) fashion_model.add(LeakyReLU(alpha=0.1)) fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same')) fashion_model.add(Dropout(0.25)) fashion_model.add(Conv2D(128, (3, 3), activation='linear', padding='same')) fashion_model.add(LeakyReLU(alpha=0.1)) fashion_model.add(MaxPooling2D(pool_size=(2, 2), padding='same')) fashion_model.add(Dropout(0.4)) fashion_model.add(Flatten()) fashion_model.add(Dense(128, activation='linear'))
def _main(args): config_path = os.path.expanduser(args.config_path) weights_path = os.path.expanduser(args.weights_path) assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format( config_path) assert weights_path.endswith( '.weights'), '{} is not a .weights file'.format(weights_path) output_path = os.path.expanduser(args.output_path) assert output_path.endswith( '.h5'), 'output path {} is not a .h5 file'.format(output_path) output_root = os.path.splitext(output_path)[0] # Load weights and config. print('Loading weights.') weights_file = open(weights_path, 'rb') major, minor, revision = np.ndarray( shape=(3, ), dtype='int32', buffer=weights_file.read(12)) if (major*10+minor)>=2 and major<1000 and minor<1000: seen = np.ndarray(shape=(1,), dtype='int64', buffer=weights_file.read(8)) else: seen = np.ndarray(shape=(1,), dtype='int32', buffer=weights_file.read(4)) print('Weights Header: ', major, minor, revision, seen) print('Parsing Darknet config.') unique_config_file = unique_config_sections(config_path) cfg_parser = configparser.ConfigParser() cfg_parser.read_file(unique_config_file) print('Creating Keras model.') input_layer = Input(shape=(None, None, 3)) prev_layer = input_layer all_layers = [] weight_decay = float(cfg_parser['net_0']['decay'] ) if 'net_0' in cfg_parser.sections() else 5e-4 count = 0 out_index = [] for section in cfg_parser.sections(): print('Parsing section {}'.format(section)) if section.startswith('convolutional'): filters = int(cfg_parser[section]['filters']) size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) pad = int(cfg_parser[section]['pad']) activation = cfg_parser[section]['activation'] batch_normalize = 'batch_normalize' in cfg_parser[section] padding = 'same' if pad == 1 and stride == 1 else 'valid' # Setting weights. # Darknet serializes convolutional weights as: # [bias/beta, [gamma, mean, variance], conv_weights] prev_layer_shape = K.int_shape(prev_layer) weights_shape = (size, size, prev_layer_shape[-1], filters) darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) print('conv2d', 'bn' if batch_normalize else ' ', activation, weights_shape) conv_bias = np.ndarray( shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) count += filters if batch_normalize: bn_weights = np.ndarray( shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) count += 3 * filters bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] conv_weights = np.ndarray( shape=darknet_w_shape, dtype='float32', buffer=weights_file.read(weights_size * 4)) count += weights_size # DarkNet conv_weights are serialized Caffe-style: # (out_dim, in_dim, height, width) # We would like to set these to Tensorflow order: # (height, width, in_dim, out_dim) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) conv_weights = [conv_weights] if batch_normalize else [ conv_weights, conv_bias ] # Handle activation. act_fn = None if activation == 'leaky': pass # Add advanced activation later. elif activation != 'linear': raise ValueError( 'Unknown activation function `{}` in section {}'.format( activation, section)) # Create Conv2D layer if stride>1: # Darknet uses left and top padding instead of 'same' mode prev_layer = ZeroPadding2D(((1,0),(1,0)))(prev_layer) conv_layer = (Conv2D( filters, (size, size), strides=(stride, stride), kernel_regularizer=l2(weight_decay), use_bias=not batch_normalize, weights=conv_weights, activation=act_fn, padding=padding))(prev_layer) if batch_normalize: conv_layer = (BatchNormalization( weights=bn_weight_list))(conv_layer) prev_layer = conv_layer if activation == 'linear': all_layers.append(prev_layer) elif activation == 'leaky': act_layer = LeakyReLU(alpha=0.1)(prev_layer) prev_layer = act_layer all_layers.append(act_layer) elif section.startswith('route'): ids = [int(i) for i in cfg_parser[section]['layers'].split(',')] layers = [all_layers[i] for i in ids] if len(layers) > 1: print('Concatenating route layers:', layers) concatenate_layer = Concatenate()(layers) all_layers.append(concatenate_layer) prev_layer = concatenate_layer else: skip_layer = layers[0] # only one layer to route all_layers.append(skip_layer) prev_layer = skip_layer elif section.startswith('shortcut'): index = int(cfg_parser[section]['from']) activation = cfg_parser[section]['activation'] assert activation == 'linear', 'Only linear activation supported.' all_layers.append(Add()([all_layers[index], prev_layer])) prev_layer = all_layers[-1] elif section.startswith('upsample'): stride = int(cfg_parser[section]['stride']) assert stride == 2, 'Only stride=2 supported.' all_layers.append(UpSampling2D(stride)(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('yolo'): out_index.append(len(all_layers)-1) all_layers.append(None) prev_layer = all_layers[-1] elif section.startswith('net'): pass else: raise ValueError( 'Unsupported section header type: {}'.format(section)) # Create and save model. model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index]) print(model.summary()) model.save('{}'.format(output_path)) print('Saved Keras model to {}'.format(output_path)) # Check to see if all weights have been read. remaining_weights = len(weights_file.read()) / 4 weights_file.close() print('Read {} of {} from Darknet weights.'.format(count, count + remaining_weights)) if remaining_weights > 0: print('Warning: {} unused weights'.format(remaining_weights)) if args.plot_model: plot(model, to_file='{}.png'.format(output_root), show_shapes=True) print('Saved model plot to {}.png'.format(output_root))
def block(x): x = Conv2D(filters, kernel_size=5, strides=2, padding='same')(x) x = LeakyReLU(0.1)(x) return x
def DarknetConv2D_BN_Leaky(*args, **kwargs): """Darknet Convolution2D followed by BatchNormalization and LeakyReLU.""" no_bias_kwargs = {'use_bias': False} no_bias_kwargs.update(kwargs) return compose(DarknetConv2D(*args, **no_bias_kwargs), BatchNormalization(), LeakyReLU(alpha=0.1))
def create_CNN_model(): model = Sequential() # ................................................................................................................ model.add( Conv1D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( Conv1D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( BatchNormalization(epsilon=1e-06, mode=0, momentum=0.9, weights=None)) # Batch Normalization model.add(LeakyReLU(alpha=.01)) # advanced activation layer model.add(MaxPool1D(pool_size=2, strides=None, padding='valid')) # ................................................................................................................ model.add( Conv1D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( Conv1D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( BatchNormalization(epsilon=1e-06, mode=0, momentum=0.9, weights=None)) # Batch Normalization model.add(LeakyReLU(alpha=.01)) # advanced activation layer model.add(MaxPool1D(pool_size=2, strides=None, padding='valid')) # ................................................................................................................ model.add( Conv1D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( Conv1D(filters=64, kernel_size=3, strides=1, padding='same', activation='relu', use_bias=False, input_shape=(lenSignal, nSensors))) model.add( BatchNormalization(epsilon=1e-06, mode=0, momentum=0.9, weights=None)) # Batch Normalization model.add(LeakyReLU(alpha=.01)) # advanced activation layer model.add(MaxPool1D(pool_size=2, strides=None, padding='valid')) # ................................................................................................................ model.add(Flatten()) model.add(BatchNormalization()) # model.add(Dense(nClasses * 1)) model.add(Dense(nClasses * 1)) model.add(Dense(nClasses, activation='softmax')) ################################################################################################################### learnignRate = 0.00005 opt = keras.optimizers.Adam(lr=learnignRate, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['acc', 'mse', 'mae', 'categorical_crossentropy']) plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) model.summary() # ................................................................................................................... return model
def generator( input_gen, kernel, feature_size, img_zdepth, img_height, img_width, batch_size=1, subpixel_NN=True, nn=True, upscaling_factor=2, num_blocks=6, reuse=None, is_train=True, ): w_init = tf.random_normal_initializer(stddev=0.02) a_init = tf.compat.v1.truncated_normal_initializer(stddev=0.02) w_init_subpixel1 = np.random.normal(scale=0.02, size=(3, 3, 3, 64, feature_size)) w_init_subpixel1 = zoom(w_init_subpixel1, (2, 2, 2, 1, 1), order=0) w_init_subpixel1_last = tf.constant_initializer(w_init_subpixel1) w_init_subpixel2 = np.random.normal(scale=0.02, size=(3, 3, 3, 64, 64)) w_init_subpixel2 = zoom(w_init_subpixel2, (2, 2, 2, 1, 1), order=0) w_init_subpixel2_last = tf.constant_initializer(w_init_subpixel2) with tf.compat.v1.variable_scope("SRGAN_g", reuse=reuse): tl.layers.set_name_reuse(reuse) x = InputLayer(input_gen, name='in') x = Conv3dLayer(x, shape=(kernel, kernel, kernel, 1, feature_size), strides=(1, 1, 1, 1, 1), padding='SAME', W_init=w_init, name='conv1') x = LeakyReLU(alpha=0.15)(x.outputs) x = tl.layers.InputLayer(x) inputRB = x inputadd = x # residual blocks for i in range(num_blocks): x = Conv3dLayer( x, shape=(kernel, kernel, kernel, feature_size, feature_size), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1-rb/%s" % i, ) x = LeakyReLU(alpha=0.15)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, feature_size, feature_size), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv2-rb/%s" % i, ) # short skip connection x = ElementwiseLayer([x, inputadd], tf.add, name="add-rb/%s" % i) inputadd = x # large skip connection x = Conv3dLayer( x, shape=(kernel, kernel, kernel, feature_size, feature_size), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv2", ) x = ElementwiseLayer([x, inputRB], tf.add, name="add-conv2") # ____________SUBPIXEL-NN______________# if subpixel_NN: # upscaling block 1 if upscaling_factor == 4: img_zdepth_deconv = int(img_zdepth / 2) img_height_deconv = int(img_height / 2) img_width_deconv = int(img_width / 2) else: img_zdepth_deconv = img_zdepth img_height_deconv = img_height img_width_deconv = img_width x = DeConv3dLayer( x, shape=(kernel * 2, kernel * 2, kernel * 2, 64, feature_size), act=lrelu1, strides=(1, 2, 2, 2, 1), output_shape=( tf.shape(input_gen)[0], img_zdepth_deconv, img_height_deconv, img_width_deconv, 64, ), padding="SAME", W_init=w_init_subpixel1_last, name="conv1-ub-subpixelnn/1", ) # upscaling block 2 if upscaling_factor == 4: x = DeConv3dLayer( x, shape=(kernel * 2, kernel * 2, kernel * 2, 64, 64), act=lrelu1, strides=(1, 2, 2, 2, 1), padding="SAME", output_shape=( tf.shape(input_gen)[0], img_zdepth, img_height, img_width, 64, ), W_init=w_init_subpixel2_last, name="conv1-ub-subpixelnn/2", ) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 64, 1), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="convlast-subpixelnn", ) # ____________RC______________# elif nn: # upscaling block 1 x = Conv3dLayer( x, shape=(kernel, kernel, kernel, feature_size, 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1-ub/1", ) x = UpSampling3D(name="UpSampling3D_1")(x.outputs) x = Conv3dLayer( InputLayer(x, name="in ub1 conv2"), shape=(kernel, kernel, kernel, 64, 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv2-ub/1", ) # upscaling block 2 if upscaling_factor == 4: x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 64, 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1-ub/2", ) x = UpSampling3D(name="UpSampling3D_1")(x.outputs) x = Conv3dLayer( InputLayer(x, name="in ub2 conv2"), shape=(kernel, kernel, kernel, 64, 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv2-ub/2", ) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 64, 1), strides=(1, 1, 1, 1, 1), act=tf.nn.tanh, padding="SAME", W_init=w_init, name="convlast", ) # ____________SUBPIXEL - BASELINE______________# else: if upscaling_factor == 4: steps_to_end = 2 else: steps_to_end = 1 # upscaling block 1 x = Conv3dLayer( x, shape=(kernel, kernel, kernel, feature_size, 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1-ub/1", ) arguments = { "img_zdepth": img_zdepth, "img_height": img_height, "img_width": img_width, "stepsToEnd": steps_to_end, "n_out_channel": int(64 / 8), } x = LambdaLayer(x, fn=subPixelConv3d, fn_args=arguments, name="SubPixel1") # upscaling block 2 if upscaling_factor == 4: x = Conv3dLayer( x, shape=(kernel, kernel, kernel, int((64) / 8), 64), act=lrelu1, strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1-ub/2", ) arguments = { "img_zdepth": img_zdepth, "img_height": img_height, "img_width": img_width, "stepsToEnd": 1, "n_out_channel": int(64 / 8), } x = LambdaLayer( x, fn=subPixelConv3d, fn_args=arguments, name="SubPixel2" ) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, int(64 / 8), 1), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="convlast", ) return x
def DCGAN_discriminator(cat_dim, cont_dim, img_dim, bn_mode, model_name="DCGAN_discriminator", dset="mnist", use_mbd=False): """ Discriminator model of the DCGAN args : img_dim (tuple of int) num_chan, height, width pretr_weights_file (str) file holding pre trained weights returns : model (keras NN) the Neural Net model """ if K.image_dim_ordering() == "th": bn_axis = 1 else: bn_axis = -1 disc_input = Input(shape=img_dim, name="discriminator_input") if dset == "mnist": list_f = [128] else: list_f = [64, 128, 256] # First conv x = Convolution2D(64, 3, 3, subsample=(2, 2), name="disc_convolution2d_1", border_mode="same")(disc_input) x = LeakyReLU(0.2)(x) # Next convs for i, f in enumerate(list_f): name = "disc_convolution2d_%s" % (i + 2) x = Convolution2D(f, 3, 3, subsample=(2, 2), name=name, border_mode="same")(x) x = BatchNormalization(mode=bn_mode, axis=bn_axis)(x) x = LeakyReLU(0.2)(x) x = Flatten()(x) x = Dense(1024)(x) x = BatchNormalization(mode=1)(x) x = LeakyReLU(0.2)(x) def linmax(x): return K.maximum(x, -16) def linmax_shape(input_shape): return input_shape # More processing for auxiliary Q x_Q = Dense(128)(x) x_Q = BatchNormalization(mode=1)(x_Q) x_Q = LeakyReLU(0.2)(x_Q) x_Q_Y = Dense(cat_dim[0], activation='softmax', name="Q_cat_out")(x_Q) x_Q_C_mean = Dense(cont_dim[0], activation='linear', name="dense_Q_cont_mean")(x_Q) x_Q_C_logstd = Dense(cont_dim[0], name="dense_Q_cont_logstd")(x_Q) x_Q_C_logstd = Lambda(linmax, output_shape=linmax_shape)(x_Q_C_logstd) # Reshape Q to nbatch, 1, cont_dim[0] x_Q_C_mean = Reshape((1, cont_dim[0]))(x_Q_C_mean) x_Q_C_logstd = Reshape((1, cont_dim[0]))(x_Q_C_logstd) x_Q_C = merge([x_Q_C_mean, x_Q_C_logstd], mode="concat", name="Q_cont_out", concat_axis=1) def minb_disc(z): diffs = K.expand_dims(z, 3) - K.expand_dims( K.permute_dimensions(z, [1, 2, 0]), 0) abs_diffs = K.sum(K.abs(diffs), 2) z = K.sum(K.exp(-abs_diffs), 2) return z def lambda_output(input_shape): return input_shape[:2] num_kernels = 300 dim_per_kernel = 5 M = Dense(num_kernels * dim_per_kernel, bias=False, activation=None) MBD = Lambda(minb_disc, output_shape=lambda_output) if use_mbd: x_mbd = M(x) x_mbd = Reshape((num_kernels, dim_per_kernel))(x_mbd) x_mbd = MBD(x_mbd) x = merge([x, x_mbd], mode='concat') # Create discriminator model x_disc = Dense(2, activation='softmax', name="disc_out")(x) discriminator_model = Model(input=[disc_input], output=[x_disc, x_Q_Y, x_Q_C], name=model_name) return discriminator_model
def buildModel(self): from keras.models import Model from keras.layers import Convolution2D, MaxPooling2D, Input, Dense, Flatten, Dropout, Reshape, TimeDistributed, BatchNormalization, Concatenate from keras.layers.advanced_activations import LeakyReLU dr_rate = 0.0 B = Input(shape=(3, )) b = Dense(5, activation="relu")(B) inputs = [B] merges = [b] for i in range(1): S = Input(shape=[2, 60, 1]) inputs.append(S) h = Convolution2D(64, 3, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(128, 5, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(256, 10, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(512, 20, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(1024, 40, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Flatten()(h) h = Dense(2048)(h) h = LeakyReLU(0.001)(h) h = Dropout(dr_rate)(h) merges.append(h) h = Convolution2D(2048, 60, 1, border_mode='valid')(S) h = LeakyReLU(0.001)(h) h = Flatten()(h) h = Dense(4096)(h) h = LeakyReLU(0.001)(h) h = Dropout(dr_rate)(h) merges.append(h) m = Concatenate()(merges) m = Dense(1024)(m) m = LeakyReLU(0.001)(m) m = Dropout(dr_rate)(m) m = Dense(512)(m) m = LeakyReLU(0.001)(m) m = Dropout(dr_rate)(m) m = Dense(256)(m) m = LeakyReLU(0.001)(m) m = Dropout(dr_rate)(m) V = Dense(2, activation='linear', init='zero')(m) model = Model(input=inputs, output=V) return model
def buildModel(self): from keras.models import Model from keras.layers import Convolution2D, MaxPooling2D, Input, Dense, Flatten, Dropout, Reshape, TimeDistributed, BatchNormalization, Concatenate from keras.layers.advanced_activations import LeakyReLU B = Input(shape=(3, )) b = Dense(5, activation="relu")(B) inputs = [B] merges = [b] for i in range(1): S = Input(shape=[2, 60, 1]) inputs.append(S) h = Convolution2D(2048, (3, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(2048, (5, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(2048, (10, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(2048, (20, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Convolution2D(2048, (40, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Flatten()(h) h = Dense(512)(h) h = LeakyReLU(0.001)(h) merges.append(h) h = Convolution2D(2048, (60, 1), border_mode='valid', data_format='channels_first')(S) h = LeakyReLU(0.001)(h) h = Flatten()(h) h = Dense(512)(h) h = LeakyReLU(0.001)(h) merges.append(h) m = Concatenate()(merges) m = Dense(1024)(m) m = LeakyReLU(0.001)(m) m = Dense(512)(m) m = LeakyReLU(0.001)(m) m = Dense(256)(m) m = LeakyReLU(0.001)(m) V = Dense(2, activation='softmax')(m) model = Model(input=inputs, output=V) return model
def create_model(self, n_dim, r): # load inputs X, _, _ = self.inputs K.set_session(self.sess) with tf.name_scope('generator'): x = X L = self.layers # dim/layer: 4096, 2048, 1024, 512, 256, 128, 64, 32, # n_filters = [ 64, 128, 256, 384, 384, 384, 384, 384] n_filters = [ 128, 256, 512, 512, 512, 512, 512, 512] # n_filters = [ 256, 512, 512, 512, 512, 1024, 1024, 1024] # n_filtersizes = [129, 65, 33, 17, 9, 9, 9, 9] # n_filtersizes = [31, 31, 31, 31, 31, 31, 31, 31] n_filtersizes = [65, 33, 17, 9, 9, 9, 9, 9, 9] downsampling_l = [] print('building model...') # downsampling layers for l, nf, fs in zip(list(range(L)), n_filters, n_filtersizes): with tf.name_scope('downsc_conv%d' % l): x = (Convolution1D(nb_filter=nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x) # if l > 0: x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) print('D-Block: ', x.get_shape()) downsampling_l.append(x) # bottleneck layer with tf.name_scope('bottleneck_conv'): x = (Convolution1D(nb_filter=n_filters[-1], filter_length=n_filtersizes[-1], activation=None, border_mode='same', init=orthogonal_init, subsample_length=2))(x) x = Dropout(p=0.5)(x) # x = BatchNormalization(mode=2)(x) x = LeakyReLU(0.2)(x) # upsampling layers for l, nf, fs, l_in in reversed(list(zip(list(range(L)), n_filters, n_filtersizes, downsampling_l))): with tf.name_scope('upsc_conv%d' % l): # (-1, n/2, 2f) x = (Convolution1D(nb_filter=2*nf, filter_length=fs, activation=None, border_mode='same', init=orthogonal_init))(x) # x = BatchNormalization(mode=2)(x) x = Dropout(p=0.5)(x) x = Activation('relu')(x) # (-1, n, f) x = SubPixel1D(x, r=2) # (-1, n, 2f) x = merge([x, l_in], mode='concat', concat_axis=-1) print('U-Block: ', x.get_shape()) # final conv layer with tf.name_scope('lastconv'): x = Convolution1D(nb_filter=2, filter_length=9, activation=None, border_mode='same', init=normal_init)(x) x = SubPixel1D(x, r=2) print(x.get_shape()) g = merge([x, X], mode='sum') return g
def main(job_id, params): conv_layers = params['conv'] dense_layer = params['dense'][0] opt = params['optimizer'][0] model = Sequential() model.add( Conv3D(conv_layers[0], kernel_size=(7, 7, 7), input_shape=(176, 32, 30, 1), padding="same")) model.add(LeakyReLU()) model.add(Conv3D(conv_layers[0], padding="same", kernel_size=(7, 7, 7))) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same")) model.add(Dropout(0.25)) model.add(Conv3D(conv_layers[1], padding="same", kernel_size=(5, 5, 5))) model.add(LeakyReLU()) model.add(Conv3D(conv_layers[1], padding="same", kernel_size=(5, 5, 5))) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same")) model.add(Dropout(0.25)) model.add(Conv3D(conv_layers[2], padding="same", kernel_size=(5, 5, 5))) model.add(LeakyReLU()) model.add(Conv3D(conv_layers[2], padding="same", kernel_size=(5, 5, 5))) model.add(LeakyReLU()) model.add(MaxPooling3D(pool_size=(2, 2, 2), padding="same")) model.add(Dropout(0.25)) model.add(Flatten()) model.add(BatchNormalization()) model.add(Dense(dense_layer, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.5)) model.add(Dense(2, activation='softmax')) model.compile(loss=categorical_crossentropy, optimizer=opt, metrics=['accuracy']) model.summary() #plot_model(model, show_shapes=True, show_layer_names=False, rankdir = 'LR', to_file='Conv_3D.png') # Data Loading y = np.zeros((466 + 148, 2)) x = np.concatenate( [np.load("/data2/3D/PD.npy"), np.load("/data2/3D/Control.npy")]) print("LOADED") for i in range(466): y[i][0] = 1 for i in range(466, 466 + 148): y[i][1] = 1 print("MADE Y") x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42) del x del y # Model Testing history = model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=1, epochs=15, verbose=1, shuffle=True) #import pickle #pickle.dump(history, open("history.pkl", "wb")) #model.save_weights("7312018.h5") return min(history['loss'])
def build_discriminator(self): # This model replaced any pooling layers with strided convolutions # Allowing it to learn its own spatial downsampling img_shape = (self.img_rows, self.img_cols, self.channels) # A Sequential model is a linear stack of layers. model = Sequential() # Create a Sequential model by simply adding layers via the .add() method # 32 filters, 3x3 kernel size, stride 2, input_shape is 28x28x1, same: pad so the output and input size are equal model.add( Conv2D(32, kernel_size=3, strides=2, input_shape=img_shape, padding='same')) # f(x) = alpha * x for x < 0, f(x) = x for x >= 0. # Leaky rectified activation worked well, especially for higher resolution modeling. # This is in contrast to the original GAN paper, which used the maxout activation model.add(LeakyReLU(alpha=0.2)) # drops 25% of the input units model.add(Dropout(0.25)) model.add(Conv2D(64, kernel_size=3, strides=2, padding='same')) #A zero-padding layer. Adds rows and columns of zeros to the image model.add(ZeroPadding2D(padding=((0, 1), (0, 1)))) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) # Normalize the activations of the previous layer at each batch to reduce its covariance shift, # i.e., the amount that the distribution of each layer shift around. # This helps deal with training problems that arise due to poor initialization and helps gradient flow in deeper models. # This proved critical to get deep generators to begin learning, preventing the generator from collapsing all samples # to a single point which is a common failure mode observed in GANs. # # Directly applying batchnorm to all layers, however, resulted in sample oscillation and model instability. # This was avoided by not applying batchnorm to the generator output layer and the discriminator input layer model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(128, kernel_size=3, strides=2, padding='same')) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(BatchNormalization(momentum=0.8)) model.add(Conv2D(256, kernel_size=3, strides=1, padding='same')) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Flatten()) #model.summary() # plot_path = 'discriminator.png' # plot_model(model, to_file=plot_path, show_shapes=True, show_layer_names=True) # instantiate a Keras tensor img = Input(shape=img_shape) features = model(img) # valid indicates if the image is real or fake valid = Dense(1, activation='sigmoid')(features) # iff the image is real, label indicates which type of image it is label = Dense(self.num_classes + 1, activation='softmax')(features) # Given an img (x) and a label(y), instantiate a Model. # Once instantiated, this model will include all layers required in the computation of y given x. return Model(img, [valid, label])
def parse_convolutional(all_layers, cfg_parser, section, prev_layer, weights_file, count, weight_decay): filters = int(cfg_parser[section]['filters']) size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) pad = int(cfg_parser[section]['pad']) activation = cfg_parser[section]['activation'] batch_normalize = 'batch_normalize' in cfg_parser[section] padding = 'same' if pad == 1 and stride == 1 else 'valid' # Setting weights. # Darknet serializes convolutional weights as: # [bias/beta, [gamma, mean, variance], conv_weights] prev_layer_shape = K.int_shape(prev_layer) weights_shape = (size, size, prev_layer_shape[-1], filters) darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) s_bn = 'bn' if batch_normalize else ' ' logging.info('conv2d: %s, %s, %s', s_bn, activation, repr(weights_shape)) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) count += filters if batch_normalize: bn_weights = np.ndarray(shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) count += 3 * filters bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read(weights_size * 4)) count += weights_size # DarkNet conv_weights are serialized Caffe-style: # (out_dim, in_dim, height, width) # We would like to set these to Tensorflow order: # (height, width, in_dim, out_dim) conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) conv_weights = [conv_weights ] if batch_normalize else [conv_weights, conv_bias] # Handle activation. act_fn = None if activation == 'leaky': pass # Add advanced activation later. elif activation != 'linear': raise ValueError( 'Unknown activation function `{}` in section {}'.format( activation, section)) # Create Conv2D layer if stride > 1: # Darknet uses left and top padding instead of 'same' mode prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer) conv_layer = (Conv2D(filters, (size, size), strides=(stride, stride), kernel_regularizer=l2(weight_decay), use_bias=not batch_normalize, weights=conv_weights, activation=act_fn, padding=padding))(prev_layer) if batch_normalize: conv_layer = (BatchNormalization(weights=bn_weight_list))(conv_layer) prev_layer = conv_layer if activation == 'linear': all_layers.append(prev_layer) elif activation == 'leaky': act_layer = LeakyReLU(alpha=0.1)(prev_layer) prev_layer = act_layer all_layers.append(act_layer) return all_layers, prev_layer, count
def unet3D(x_in, img_shape, out_im_chans, nf_enc=[64, 64, 128, 128, 256, 256, 512], nf_dec=None, regularizer=None, initializer=None, layer_prefix='unet', n_convs_per_stage=1, include_residual=False, use_maxpool=True, max_time_downsample=None, n_tasks=1, use_dropout=False, do_unpool=False, do_last_conv=True, ): ks = 3 if max_time_downsample is None: max_time_downsample = len(nf_enc) # downsample in time all the way down encoding_im_sizes = np.asarray([( int(np.ceil(img_shape[0] / 2.0 ** i)), int(np.ceil(img_shape[1] / 2.0 ** i)), int(np.ceil(img_shape[2] / 2.0 ** i)), ) for i in range(0, len(nf_enc) + 1)]) else: encoding_im_sizes = np.asarray([( int(np.ceil(img_shape[0] / 2.0 ** i)), int(np.ceil(img_shape[1] / 2.0 ** i)), max(int(np.ceil(img_shape[2] / 2.0 ** (max_time_downsample))), int(np.ceil(img_shape[2] / 2.0 ** i))), ) for i in range(0, len(nf_enc) + 1)]) reg_params = {} if regularizer == 'l1': reg = regularizers.l1(1e-6) else: reg = None if initializer == 'zeros': reg_params['kernel_initializer'] = initializers.Zeros() x = x_in encodings = [] encoding_im_sizes = [] for i in range(len(nf_enc)): if not use_maxpool and i > 0: x = LeakyReLU(0.2)(x) for j in range(n_convs_per_stage): if nf_enc[i] is not None: # in case we dont want to convovle at max resolution x = Conv3D( nf_enc[i], kernel_regularizer=reg, kernel_size=ks, strides=(1, 1, 1), padding='same', name='{}_enc_conv3D_{}_{}'.format(layer_prefix, i, j + 1))(x) #if use_dropout: # x = Dropout(0.2)(x) if j == 0 and include_residual: residual_input = x elif j == n_convs_per_stage - 1 and include_residual: x = Add()([residual_input, x]) x = LeakyReLU(0.2)(x) encodings.append(x) encoding_im_sizes.append(np.asarray(x.get_shape().as_list()[1:-1])) # only downsample if we haven't reached the max if i >= max_time_downsample: ds = (2, 2, 1) else: ds = (2, 2, 2) if i < len(nf_enc) - 1: if use_maxpool: x = MaxPooling3D(pool_size=ds, padding='same', name='{}_enc_maxpool_{}'.format(layer_prefix, i))(x) #x, pool_idxs = Lambda(lambda x:._max_pool_3d_with_argmax(x, ksize=ks, strides=(2, 2, 2), padding='same'), name='{}_enc_maxpool3dwithargmax_{}'.format(layer_prefix, i))(x) else: x = Conv3D(nf_enc[i], kernel_size=ks, strides=ds, padding='same', name='{}_enc_conv3D_{}'.format(layer_prefix, i))(x) if nf_dec is None: nf_dec = list(reversed(nf_enc[1:])) decoder_outputs = [] x_encoded = x print(encoding_im_sizes) print(nf_dec) for ti in range(n_tasks): decoding_im_sizes = [] x = x_encoded for i in range(len(nf_dec)): curr_shape = x.get_shape().as_list()[1:-1] print('Current shape {}, img shape {}'.format(x.get_shape().as_list(), img_shape)) # only do upsample if we are not yet at max resolution if np.any(curr_shape < list(img_shape[:len(curr_shape)])): # TODO: fix this for time ''' if i < len(nf_dec) - max_time_downsample + 1 \ or curr_shape[-1] >= encoding_im_sizes[-i-2][-1]: # if we are already at the correct time scale us = (2, 2, 1) else: ''' us = (2, 2, 2) #decoding_im_sizes.append(encoding_im_sizes[-i-1] * np.asarray(us)) x = UpSampling3D(size=us, name='{}_dec{}_upsamp_{}'.format(layer_prefix, ti, i))(x) # just concatenate the final layer here if i <= len(encodings) - 2: x = _pad_or_crop_to_shape_3D(x, np.asarray(x.get_shape().as_list()[1:-1]), encoding_im_sizes[-i-2]) x = Concatenate(axis=-1)([x, encodings[-i-2]]) #x = LeakyReLU(0.2)(x) residual_input = x for j in range(n_convs_per_stage): x = Conv3D(nf_dec[i], kernel_regularizer=reg, kernel_size=ks, strides=(1, 1, 1), padding='same', name='{}_dec{}_conv3D_{}_{}'.format(layer_prefix, ti, i, j))(x) if use_dropout and i < 2: x = Dropout(0.2)(x) if j == 0 and include_residual: residual_input = x elif j == n_convs_per_stage - 1 and include_residual: x = Add()([residual_input, x]) x = LeakyReLU(0.2)(x) if do_last_conv: y = Conv3D(out_im_chans, kernel_size=1, padding='same', kernel_regularizer=reg, name='{}_dec{}_conv3D_final'.format(layer_prefix, ti))(x) # add your own activation after this model else: y = x decoder_outputs.append(y) # add your own activation after this model if n_tasks == 1: return y else: return decoder_outputs
def main(): # read in the input and output data X = np.loadtxt('input.csv', delimiter=",", skiprows=1) y = np.loadtxt('output.csv', delimiter=",", skiprows=1) # eliminate the first column of input data, which is date t = X[:,0:1] X = X[:,1:] # reshape y y = y.reshape(y.shape[0],1) # save output statistics for scaling back to absolute values y_max = y.max(axis=0) y_min = y.min(axis=0) # data standardization and normalization X = stand_data(X) y = norm_data(y, -1, 1) # split into train data sets and test sets (X_train, y_train, t_train), (X_test, y_test, t_test) = split_train_test(X, y, t) # set the model epoch to be 500, which is enough for our task nb_epoch = 500 batch_size = 200 # multilayer perceptron model model = Sequential() model.add(Dense(240, input_dim=X_train.shape[1])) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.5)) model.add(Dense(120)) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.5)) model.add(Dense(60)) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.5)) model.add(Dense(30)) model.add(BatchNormalization()) model.add(LeakyReLU()) model.add(Dropout(0.5)) model.add(Dense(1, activation="tanh")) # output was scaled to [-1, 1] model.summary() # plot the model diagram and save to file plot_model(model,to_file='model_diagram.png',show_shapes=True) # model fitting model.compile(loss="mae", optimizer='rmsprop') model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2) # prediction prediction = model.predict(X_test, verbose=1) # use the min and max data to scale back to absolute values prediction = norm_data_reverse(prediction, -1, 1, y_min, y_max) y_test = norm_data_reverse(y_test, -1, 1, y_min, y_max) # output the results result = np.concatenate((t_test, prediction, y_test), axis=1) np.savetxt('result_mlp.csv', result, delimiter=',', header='Date,PredictedPrice,RealPrice', fmt='%1.3f', comments='') # show the training and saving is done print("Training done, data saved to result_mlp.csv")
def load_lstm(LSTM_PATH): """ Load LSTM :return: """ nb_tsteps = 10 nb_feats = 40 model = Sequential() model.add( TimeDistributed(Dense(120, activation='linear'), input_shape=(nb_tsteps, nb_feats))) model.add(LeakyReLU(alpha=.01)) model.add(Dropout(0.2)) model.add(LSTM(250, return_sequences=False, consume_less='gpu')) model.add(Dense(100, activation='linear')) model.add(LeakyReLU(alpha=.01)) model.add(Dropout(0.2)) model.add(Dense(60, activation='linear')) model.add(LeakyReLU(alpha=.01)) model.add(Dropout(0.2)) model.add(Dense(30, activation='linear')) model.add(LeakyReLU(alpha=.01)) model.add(Dropout(0.2)) model.add(Dense(10, activation='linear')) model.add(LeakyReLU(alpha=.01)) model.add(Dropout(0.1)) model.add(Dense(3, activation='linear')) model.add(LeakyReLU(alpha=.01)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) #path = "/Users/mh/Documents/CSML/Masterarbeit/Python/LSTM_v2/m03/" with h5py.File(LSTM_PATH + 'modelweights_LSTM_v2.h5', 'r+') as f: timedistributed_1 = model.layers[0] g_td1 = f["timedistributed_1"] td1_weights = [g_td1["dense_1_W"], g_td1["dense_1_b"]] timedistributed_1.set_weights(td1_weights) lstm_1 = model.layers[3] g_lstm1 = f["lstm_1"] lstm1_weights = [ g_lstm1["lstm_1_W"], g_lstm1["lstm_1_U"], g_lstm1["lstm_1_b"] ] lstm_1.set_weights(lstm1_weights) dense_2 = model.layers[4] g_dense2 = f["dense_2"] dense2_weights = [g_dense2["dense_2_W"], g_dense2["dense_2_b"]] dense_2.set_weights(dense2_weights) dense_3 = model.layers[7] g_dense3 = f["dense_3"] dense3_weights = [g_dense3["dense_3_W"], g_dense3["dense_3_b"]] dense_3.set_weights(dense3_weights) dense_4 = model.layers[10] g_dense4 = f["dense_4"] dense4_weights = [g_dense4["dense_4_W"], g_dense4["dense_4_b"]] dense_4.set_weights(dense4_weights) dense_5 = model.layers[13] g_dense5 = f["dense_5"] dense5_weights = [g_dense5["dense_5_W"], g_dense5["dense_5_b"]] dense_5.set_weights(dense5_weights) dense_6 = model.layers[16] g_dense6 = f["dense_6"] dense6_weights = [g_dense6["dense_6_W"], g_dense6["dense_6_b"]] dense_6.set_weights(dense6_weights) dense_7 = model.layers[18] g_dense7 = f["dense_7"] dense7_weights = [g_dense7["dense_7_W"], g_dense7["dense_7_b"]] dense_7.set_weights(dense7_weights) return model
sumX4val = sumX4val + X4testList[i].toarray() Y_val = np_utils.to_categorical(Y_test - 1, 1000) tensorboard = TensorBoard(log_dir='./exp7', histogram_freq=1, write_graph=True, write_images=False) nb_hidden = 200 input1 = Input(shape=(1000, )) input2 = Input(shape=(1000, )) input3 = Input(shape=(1000, )) input4 = Input(shape=(1000, )) concatL = Concatenate()([input1, input2, input3, input4]) layer1 = Dense(nb_hidden, bias_initializer='zeros')(concatL) layer1 = LeakyReLU(alpha=0.01)(layer1) out = Dense(nb_classes, bias_initializer='zeros', activation='softmax')(layer1) model = Model([input1, input2, input3, input4], out) #model=load_model('genFit_ens450.h5'); model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy', top3_acc, top5_acc]) G = myGeneratorTrain() K = next(G) X_i = K[0] Y_i = K[1] #model.fit(X_i,Y_i) debugModel = Model(inputs=model.input, outputs=model.layers[0].output) #history=model.fit_generator(myGeneratorTrain(), steps_per_epoch = 400 , epochs = 100, verbose=2, validation_data=myGeneratorVal(), validation_steps=100 ) history = model.fit_generator(myGeneratorTrain(),
def get_unet(self, nf): input_n = Input((self.sz, self.sz, self.nch)) ##### Encoder part conv1 = [] # nch x 512 x 512 conv = Conv2D(nf, (3, 3), padding='same')(input_n) conv = LeakyReLU(alpha=0.01)(conv) conv = BatchNormalization()(conv) conv = Dropout(self.drop)(conv) # nfxnch x 512 x 512 conv1 = Conv2D(nf * 2, (3, 3), padding='same', strides=(2, 2))(conv) # nfxnch x 256 x 256 conv1 = LeakyReLU(alpha=0.01)(conv1) conv1 = BatchNormalization()(conv1) conv1 = Dropout(self.drop)(conv1) # nfxnch x 256 x 256 conv2 = Conv2D(nf * 4, (3, 3), padding='same', strides=(2, 2))(conv1) # nfxnch x 128 x 128 conv2 = LeakyReLU(alpha=0.01)(conv2) conv2 = BatchNormalization()(conv2) conv2 = Dropout(self.drop)(conv2) # nfxnch x 128 x 128 conv3 = Conv2D(nf * 8, (3, 3), padding='same', strides=(2, 2))(conv2) # nfxnch x 64 x 64 conv3 = LeakyReLU(alpha=0.01)(conv3) conv3 = BatchNormalization()(conv3) conv3 = Dropout(self.drop)(conv3) # nfxnch x 64 x 64 conv4 = Conv2D(nf * 8, (3, 3), padding='same', strides=(2, 2))(conv3) # nfxnch x 32 x 32 conv4 = LeakyReLU(alpha=0.01)(conv4) conv4 = BatchNormalization()(conv4) conv4 = Dropout(self.drop)(conv4) ##### Upsample path up4 = Concatenate(axis=-1)([UpSampling2D(size=(2, 2))(conv4), conv3]) # nfxnch x 64 x 64 upconv1 = Conv2D(nf * 8, (3, 3), padding='same')(up4) upconv1 = LeakyReLU(alpha=0.01)(upconv1) upconv1 = BatchNormalization()(upconv1) upconv1 = Dropout(self.drop)(upconv1) upconv1 = Conv2D(nf * 8, (3, 3), padding='same')(upconv1) upconv1 = LeakyReLU(alpha=0.01)(upconv1) upconv1 = BatchNormalization()(upconv1) upconv1 = Dropout(self.drop)(upconv1) up3 = Concatenate(axis=-1)([UpSampling2D(size=(2, 2))(upconv1), conv2]) # nfxnch x 128 x 128 upconv2 = Conv2D(nf, (3, 3), padding='same')(up3) upconv2 = LeakyReLU(alpha=0.01)(upconv2) upconv2 = BatchNormalization()(upconv2) upconv2 = Dropout(self.drop)(upconv2) upconv2 = Conv2D(nf, (3, 3), padding='same')(upconv2) upconv2 = LeakyReLU(alpha=0.01)(upconv2) upconv2 = BatchNormalization()(upconv2) upconv2 = Dropout(self.drop)(upconv2) up2 = Concatenate(axis=-1)([UpSampling2D(size=(2, 2))(upconv2), conv1]) # nfxnch x 256 x 256 upconv3 = Conv2D(nf, (3, 3), padding='same')(up2) upconv3 = LeakyReLU(alpha=0.01)(upconv3) upconv3 = BatchNormalization()(upconv3) upconv3 = Dropout(self.drop)(upconv3) upconv3 = Conv2D(nf, (3, 3), padding='same')(upconv3) upconv3 = LeakyReLU(alpha=0.01)(upconv3) upconv3 = BatchNormalization()(upconv3) upconv3 = Dropout(self.drop)(upconv3) up1 = Concatenate(axis=-1)([UpSampling2D(size=(2, 2))(upconv3), conv]) # nfxnch x 512 x 512 upconv4 = Conv2D(nf, (3, 3), padding='same')(up1) upconv4 = LeakyReLU(alpha=0.01)(upconv4) upconv4 = BatchNormalization()(upconv4) upconv4 = Dropout(self.drop)(upconv4) upconv4 = Conv2D(nf, (3, 3), padding='same')(upconv4) upconv4 = LeakyReLU(alpha=0.01)(upconv4) upconv4 = BatchNormalization()(upconv4) upconv4 = Dropout(self.drop)(upconv4) conv_final = Conv2D(1, (3, 3), padding='same')(upconv4) act = 'sigmoid' out_distances = Activation(act)(conv_final) model_distances = Model(inputs=input_n, outputs=out_distances) return model_distances
def discriminator( input_disc, kernel, img_zdepth, img_width, img_height, batch_size=1, reuse=None, is_train=True, ): w_init = tf.random_normal_initializer(stddev=0.02) with tf.compat.v1.variable_scope("SRGAN_d", reuse=reuse): # tl.layers.set_name_reuse(reuse) input_disc.set_shape( [batch_size, img_zdepth, img_height, img_width, 1], ) # switched width height x = InputLayer(input_disc, name="in") # in Conv3dLayer the shape= argument defines sahpe of filters: # (filter_depth, filter_height, filter_width, in_channels, out_channels) # strides defines the sliding window for corresponding input dimensions x = Conv3dLayer( x, act=lrelu2, shape=(kernel, kernel, kernel, 1, 32), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv1", ) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 32, 32), strides=(1, 2, 2, 2, 1), padding="SAME", W_init=w_init, name="conv2", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 32, 64), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv3", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 64, 64), strides=(1, 2, 2, 2, 1), padding="SAME", W_init=w_init, name="conv4", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 64, 128), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv5", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 128, 128), strides=(1, 2, 2, 2, 1), padding="SAME", W_init=w_init, name="conv6", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 128, 256), strides=(1, 1, 1, 1, 1), padding="SAME", W_init=w_init, name="conv7", ) x = LeakyReLU(alpha=0.25)(x.outputs) x = tl.layers.InputLayer(x) x = Conv3dLayer( x, shape=(kernel, kernel, kernel, 256, 256), strides=(1, 2, 2, 2, 1), padding="SAME", W_init=w_init, name="conv8", )
def _main_(args): config_path = args.conf with open(config_path) as config_buffer: config = json.loads(config_buffer.read()) if config['backup']['create_backup']: config = create_backup(config) keras.backend.tensorflow_backend.set_session(get_session()) #path for the training and validation dataset datasetTrainPath = os.path.join(args.folder,"train") datasetValPath = os.path.join(args.folder,"val") for folder in [datasetTrainPath, datasetValPath]: if not os.path.isdir(folder): raise Exception("{} doesn't exist!".format(folder)) classesTrain = next(os.walk(datasetTrainPath))[1] classesVal = next(os.walk(datasetValPath))[1] if not classesVal == classesTrain: raise Exception("The training and validation classes must be the same!") else: folders = classesTrain #training configuration epochs = config['train']['nb_epochs'] batchSize = config['train']['batch_size'] width = config['model']['input_size_w'] height = config['model']['input_size_h'] depth = 3 if config['model']['gray_mode'] == False else 1 #config keras generators if len(folders) == 2: #if just have 2 classes, the model will have a binary output classes = 1 else: classes = len(folders) #count all samples imagesTrainPaths = [] imagesValPaths = [] for folder in folders: imagesTrainPaths+=list(list_images(os.path.join(datasetTrainPath, folder))) imagesValPaths+=list(list_images(os.path.join(datasetValPath, folder))) generator_config = { 'IMAGE_H' : height, 'IMAGE_W' : width, 'IMAGE_C' : depth, 'BATCH_SIZE' : batchSize } #callbacks model_name = config['train']['saved_weights_name'] checkPointSaverBest=ModelCheckpoint(model_name, monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) ckp_model_name = os.path.splitext(model_name)[1]+"_ckp.h5" checkPointSaver=ModelCheckpoint(ckp_model_name, verbose=1, save_best_only=False, save_weights_only=False, period=10) tb=TensorBoard(log_dir=config['train']['tensorboard_log_dir'], histogram_freq=0, batch_size=batchSize, write_graph=True, write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None) #create the classification model # make the feature extractor layers if depth == 1: input_size = (height, width, 1) input_image = Input(shape=input_size) else: input_size = (height, width, 3) input_image = Input(shape=input_size) feature_extractor = import_feature_extractor(config['model']['backend'], input_size) train_generator = BatchGenerator(imagesTrainPaths, generator_config, norm=feature_extractor.normalize, jitter=True) val_generator = BatchGenerator(imagesValPaths, generator_config, norm=feature_extractor.normalize, jitter=False) features = feature_extractor.extract(input_image) # make the model head output = Conv2D(classes, (1, 1), padding="same")(features) output = BatchNormalization()(output) output = LeakyReLU(alpha=0.1)(output) output = GlobalAveragePooling2D()(output) output = Activation("sigmoid")(output) if classes == 1 else Activation("softmax")(output) if config['train']['pretrained_weights'] != "": model = load_model(config['model']['pretrained_weights'] ) else: model = Model(input_image, output) opt = Adam() model.compile(loss="binary_crossentropy" if classes == 1 else "categorical_crossentropy", optimizer=opt,metrics=["accuracy"]) model.summary() model.fit_generator( train_generator, steps_per_epoch=len(imagesTrainPaths)//batchSize, epochs=epochs, validation_data=val_generator, validation_steps=len(imagesValPaths)//batchSize, callbacks=[checkPointSaverBest,checkPointSaver,tb], workers=12, max_queue_size=40)
def build_model(self): inputs = Input((self.patch_height, self.patch_width, 1)) conv1 = Conv2D(32, (3, 3), padding='same')(inputs) # 'valid' conv1 = LeakyReLU(alpha=0.3)(conv1) conv1 = Dropout(0.2)(conv1) conv1 = normalization.BatchNormalization( epsilon=2e-05, axis=1, momentum=0.9, weights=None, beta_initializer='RandomNormal', gamma_initializer='one')(conv1) conv1 = Conv2D(32, (3, 3), dilation_rate=2, padding='same')(conv1) conv1 = LeakyReLU(alpha=0.3)(conv1) conv1 = Conv2D(32, (3, 3), dilation_rate=4, padding='same')(conv1) conv1 = LeakyReLU(alpha=0.3)(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) # pool1 = normalization.BatchNormalization(epsilon=1e-06, mode=1, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')(pool1) conv2 = Conv2D(64, (3, 3), padding='same')( pool1) # ,activation='relu', padding='same')(pool1) conv2 = normalization.BatchNormalization( epsilon=2e-05, axis=1, momentum=0.9, weights=None, beta_initializer='RandomNormal', gamma_initializer='one')(conv2) conv2 = LeakyReLU(alpha=0.3)(conv2) conv2 = Dropout(0.2)(conv2) conv2 = Conv2D(64, (3, 3), dilation_rate=2, padding='same')(conv2) conv2 = LeakyReLU(alpha=0.3)(conv2) conv2 = Conv2D(64, (3, 3), dilation_rate=4, padding='same')( conv2) # ,W_regularizer=l2(0.01), b_regularizer=l2(0.01))(conv2) conv2 = LeakyReLU(alpha=0.3)(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) # crop = Cropping2D(cropping=((int(3 * patch_height / 8), int(3 * patch_height / 8)), (int(3 * patch_width / 8), int(3 * patch_width / 8))))(conv1) # conv3 = concatenate([crop,pool2], axis=1) conv3 = Conv2D(128, (3, 3), padding='same')( pool2) # , activation='relu', padding='same')(conv3) conv3 = normalization.BatchNormalization( epsilon=2e-05, axis=1, momentum=0.9, weights=None, beta_initializer='RandomNormal', gamma_initializer='one')(conv3) conv3 = LeakyReLU(alpha=0.3)(conv3) conv3 = Dropout(0.2)(conv3) conv3 = Conv2D(128, (3, 3), dilation_rate=2, padding='same')( conv3) # ,W_regularizer=l2(0.01), b_regularizer=l2(0.01))(conv3) conv3 = normalization.BatchNormalization( epsilon=2e-05, axis=1, momentum=0.9, weights=None, beta_initializer='RandomNormal', gamma_initializer='one')(conv3) conv3 = LeakyReLU(alpha=0.3)(conv3) conv3 = Conv2D(128, (3, 3), dilation_rate=4, padding='same')(conv3) conv3 = normalization.BatchNormalization( epsilon=2e-05, axis=1, momentum=0.9, weights=None, beta_initializer='RandomNormal', gamma_initializer='one')(conv3) conv3 = LeakyReLU(alpha=0.3)(conv3) # up1 = UpSampling2D(size=(2, 2))(conv3) up1 = concatenate([UpSampling2D(size=(2, 2))(conv3), conv2], axis=3) conv4 = Conv2D(64, (3, 3), padding='same')(up1) conv4 = LeakyReLU(alpha=0.3)(conv4) conv4 = Dropout(0.2)(conv4) conv4 = Conv2D(64, (3, 3), padding='same')(conv4) conv4 = LeakyReLU(alpha=0.3)(conv4) # conv4 = normalization.BatchNormalization(epsilon=1e-06, mode=1, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')(conv4) # # up2 = UpSampling2D(size=(2, 2))(conv4) up2 = concatenate([UpSampling2D(size=(2, 2))(conv4), conv1], axis=3) conv5 = Conv2D(32, (3, 3), padding='same')(up2) conv5 = LeakyReLU(alpha=0.3)(conv5) conv5 = Dropout(0.2)(conv5) conv5 = Conv2D(32, (3, 3), padding='same')(conv5) conv5 = LeakyReLU(alpha=0.3)(conv5) conv6 = Conv2D(self.num_seg_class + 1, (1, 1), padding='same')(conv5) conv6 = LeakyReLU(alpha=0.3)(conv6) # conv6 = normalization.BatchNormalization(epsilon=1e-06, mode=1, axis=-1, momentum=0.9, weights=None, beta_init='zero', gamma_init='one')(conv6) # for tensorflow # conv6 = core.Reshape((patch_height*patch_width,num_lesion_class+1))(conv6) # for theano conv6 = core.Reshape((self.patch_height * self.patch_width, self.num_seg_class + 1))(conv6) #conv6 = core.Permute((2, 1))(conv6) ############ act = Activation('softmax')(conv6) model = Model(inputs=inputs, outputs=act) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy']) plot_model(model, to_file=os.path.join(self.config.checkpoint, "model.png"), show_shapes=True) self.model = model
def create_model(): input = Input(shape=(1, img_rows, img_cols)) conv1 = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(input) conv1 = LeakyReLU()(conv1) conv1 = SpatialDropout2D(0.2)(conv1) conv1 = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(conv1) conv1 = LeakyReLU()(conv1) conv1 = SpatialDropout2D(0.2)(conv1) pool1 = AveragePooling2D(pool_size=(2, 2))(conv1) conv2 = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(pool1) conv2 = LeakyReLU()(conv2) conv2 = SpatialDropout2D(0.2)(conv2) conv2 = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(conv2) conv2 = LeakyReLU()(conv2) conv2 = SpatialDropout2D(0.2)(conv2) pool2 = AveragePooling2D(pool_size=(2, 2))(conv2) conv3 = Convolution2D(128, 3, 3, border_mode='same', init='he_normal')(pool2) conv3 = LeakyReLU()(conv3) conv3 = SpatialDropout2D(0.2)(conv3) conv3 = Convolution2D(128, 3, 3, border_mode='same', init='he_normal')(conv3) conv3 = LeakyReLU()(conv3) conv3 = SpatialDropout2D(0.2)(conv3) comb1 = merge([conv2, UpSampling2D(size=(2, 2))(conv3)], mode='concat', concat_axis=1) conv4 = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(comb1) conv4 = LeakyReLU()(conv4) conv4 = SpatialDropout2D(0.2)(conv4) conv4 = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(conv4) conv4 = LeakyReLU()(conv4) conv4 = SpatialDropout2D(0.2)(conv4) comb2 = merge([conv1, UpSampling2D(size=(2, 2))(conv4)], mode='concat', concat_axis=1) conv5 = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(comb2) conv5 = LeakyReLU()(conv5) conv5 = SpatialDropout2D(0.2)(conv5) conv5 = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(conv5) conv5 = LeakyReLU()(conv5) conv5 = SpatialDropout2D(0.2)(conv5) output = Convolution2D(1, 1, 1, activation='sigmoid')(conv5) model = Model(input=input, output=output) model.compile(optimizer=Adam(lr=3e-4), loss='binary_crossentropy') return model
input_shape = (img_rows, img_cols, 3) loss_weights_1 = Input(shape=(1, ), name='disc_1') loss_weights_2 = Input(shape=(1, ), name='disc_2') loss_weights_3 = Input(shape=(1, ), name='disc_3') targets1 = Input(shape=(1, ), name='disc_4') targets2 = Input(shape=(num_pp, ), name='disc_5') targets3 = Input(shape=(num_ep, ), name='disc_6') d_input = Input(input_shape, name='disc_7') rep_field = 8 x = Conv2D(32, (rep_field, rep_field), strides=(2, 2), padding='same', name='id_conv1')(d_input) # x = BatchNormalization(momentum=0.9)(x) x = LeakyReLU(0.2)(x) # x = AveragePooling2D((2, 2), padding='same')(x) # x = Dropout(0.3)(x) x = Conv2D(64, (rep_field, rep_field), strides=(2, 2), padding='same', name='id_conv2')(x) # x = BatchNormalization(momentum=0.9)(x) x = LeakyReLU(0.2)(x) # x = AveragePooling2D((2, 2), padding='same')(x) # x = Dropout(0.3)(x) x = Conv2D(128, (rep_field, rep_field), strides=(2, 2), padding='same',
# the function to implement the orgnization layer (thanks to github.com/allanzelener/YAD2K) def space_to_depth_x2(x): return tf.space_to_depth(x, block_size=2) input_image = Input(shape=(IMAGE_H, IMAGE_W, 3)) true_boxes = Input(shape=(1, 1, 1, TRUE_BOX_BUFFER, 4)) # Layer 1 x = Conv2D(32, (3, 3), strides=(1, 1), padding='same', name='conv_1', use_bias=False)(input_image) x = BatchNormalization(name='norm_1')(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) # Layer 2 x = Conv2D(64, (3, 3), strides=(1, 1), padding='same', name='conv_2', use_bias=False)(x) x = BatchNormalization(name='norm_2')(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) # Layer 3 x = Conv2D(128, (3, 3), strides=(1, 1),
def block(x): x = Conv2D(filters * 4, kernel_size=3, padding='same')(x) x = LeakyReLU(0.1)(x) x = PixelShuffler()(x) return x
model.add(Reshape(target_shape=(-1, ), input_shape=sampleX.shape[1:])) #model.add(Dense(1024,kernel_regularizer=regularizers.l2(regul))) #model.add(LeakyReLU(alpha=0.1)) #model.add(Dense(512,kernel_regularizer=regularizers.l2(regul))) #model.add(LeakyReLU(alpha=0.1)) #model.add(BatchNormalization()) #model.add(Dense(256,kernel_regularizer=regularizers.l2(regul))) #model.add(LeakyReLU(alpha=0.1)) #model.add(BatchNormalization()) #model.add(Dropout(0.5)) model.add(Dense(128, kernel_regularizer=regularizers.l2(regul))) model.add(LeakyReLU(alpha=0.1)) model.add(BatchNormalization()) model.add(Dense(32, kernel_regularizer=regularizers.l2(regul))) model.add(LeakyReLU(alpha=0.1)) model.add(Dense(args.zdim, kernel_regularizer=regularizers.l2(regul))) model.add(LeakyReLU(alpha=0.1)) model.add(Dense(sampleT.shape[1], kernel_regularizer=regularizers.l2(regul))) model.add(Activation('linear')) def eachError(n): def rel_error_at(t, y): v1 = K.sqrt(K.mean(K.square(t[:, n] - y[:, n])))
def _main(args): config_path = os.path.expanduser(args.config_path) weights_path = os.path.expanduser(args.weights_path) assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format( config_path) assert weights_path.endswith( '.weights'), '{} is not a .weights file'.format(weights_path) output_path = os.path.expanduser(args.output_path) assert output_path.endswith( '.h5'), 'output path {} is not a .h5 file'.format(output_path) output_root = os.path.splitext(output_path)[0] # Load weights and config. print('Loading weights.') weights_file = open(weights_path, 'rb') weights_header = np.ndarray(shape=(4, ), dtype='int32', buffer=weights_file.read(16)) print('Weights Header: ', weights_header) # TODO: Check transpose flag when implementing fully connected layers. # transpose = (weight_header[0] > 1000) or (weight_header[1] > 1000) print('Parsing Darknet config.') unique_config_file = unique_config_sections(config_path) cfg_parser = configparser.ConfigParser() cfg_parser.read_file(unique_config_file) print('Creating Keras model.') if args.fully_convolutional: image_height, image_width = None, None else: image_height = int(cfg_parser['net_0']['height']) image_width = int(cfg_parser['net_0']['width']) prev_layer = Input(shape=(image_height, image_width, 3)) all_layers = [prev_layer] weight_decay = float(cfg_parser['net_0']['decay'] ) if 'net_0' in cfg_parser.sections() else 5e-4 count = 0 for section in cfg_parser.sections(): print('Parsing section {}'.format(section)) if section.startswith('convolutional'): filters = int(cfg_parser[section]['filters']) size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) pad = int(cfg_parser[section]['pad']) activation = cfg_parser[section]['activation'] batch_normalize = 'batch_normalize' in cfg_parser[section] # border_mode='same' is equivalent to Darknet pad=1 border_mode = 'same' if pad == 1 else 'valid' # Setting weights. # Darknet serializes convolutional weights as: # [bias/beta, [gamma, mean, variance], conv_weights] prev_layer_shape = K.int_shape(prev_layer) # TODO: This assumes channel last dim_ordering. weights_shape = (size, size, prev_layer_shape[-1], filters) darknet_w_shape = (filters, weights_shape[2], size, size) weights_size = np.product(weights_shape) print('conv2d', 'bn' if batch_normalize else ' ', activation, weights_shape) conv_bias = np.ndarray(shape=(filters, ), dtype='float32', buffer=weights_file.read(filters * 4)) count += filters if batch_normalize: bn_weights = np.ndarray(shape=(3, filters), dtype='float32', buffer=weights_file.read(filters * 12)) count += 3 * filters # TODO: Keras BatchNormalization mistakenly refers to var # as std. bn_weight_list = [ bn_weights[0], # scale gamma conv_bias, # shift beta bn_weights[1], # running mean bn_weights[2] # running var ] conv_weights = np.ndarray(shape=darknet_w_shape, dtype='float32', buffer=weights_file.read(weights_size * 4)) count += weights_size # DarkNet conv_weights are serialized Caffe-style: # (out_dim, in_dim, height, width) # We would like to set these to Tensorflow order: # (height, width, in_dim, out_dim) # TODO: Add check for Theano dim ordering. conv_weights = np.transpose(conv_weights, [2, 3, 1, 0]) conv_weights = [conv_weights] if batch_normalize else [ conv_weights, conv_bias ] # Handle activation. act_fn = None if activation == 'leaky': pass # Add advanced activation later. elif activation != 'linear': raise ValueError( 'Unknown activation function `{}` in section {}'.format( activation, section)) # Create Conv2D layer conv_layer = (Convolution2D( filters, size, size, border_mode=border_mode, subsample=(stride, stride), bias=not batch_normalize, weights=conv_weights, activation=act_fn, W_regularizer=l2(weight_decay)))(prev_layer) if batch_normalize: conv_layer = (BatchNormalization( weights=bn_weight_list))(conv_layer) prev_layer = conv_layer if activation == 'linear': all_layers.append(prev_layer) elif activation == 'leaky': act_layer = LeakyReLU(alpha=0.1)(prev_layer) prev_layer = act_layer all_layers.append(act_layer) elif section.startswith('maxpool'): size = int(cfg_parser[section]['size']) stride = int(cfg_parser[section]['stride']) all_layers.append( MaxPooling2D(pool_size=(size, size), strides=(stride, stride), border_mode='same')(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('avgpool'): if cfg_parser.items(section) != []: raise ValueError('{} with params unsupported.'.format(section)) all_layers.append(GlobalAveragePooling2D()(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('route'): ids = [int(i) for i in cfg_parser[section]['layers'].split(',')] layers = [all_layers[i] for i in ids] if len(layers) > 1: print('Merging layers:', layers) merge_layer = merge(layers, mode='concat') all_layers.append(merge_layer) prev_layer = merge_layer else: skip_layer = layers[0] # only one layer to route all_layers.append(skip_layer) prev_layer = skip_layer elif section.startswith('reorg'): block_size = int(cfg_parser[section]['stride']) assert block_size == 2, 'Only reorg with stride 2 supported.' all_layers.append( Lambda(space_to_depth_x2, output_shape=space_to_depth_x2_output_shape, name='space_to_depth_x2')(prev_layer)) prev_layer = all_layers[-1] elif section.startswith('region'): with open('{}_anchors.txt'.format(output_root), 'w') as f: print(cfg_parser[section]['anchors'], file=f) elif (section.startswith('net') or section.startswith('cost') or section.startswith('softmax')): pass # Configs not currently handled during model definition. else: raise ValueError( 'Unsupported section header type: {}'.format(section)) # Create and save model. model = Model(input=all_layers[0], output=all_layers[-1]) print(model.summary()) model.save('{}'.format(output_path)) print('Saved Keras model to {}'.format(output_path)) # Check to see if all weights have been read. remaining_weights = len(weights_file.read()) / 4 weights_file.close() print('Read {} of {} from Darknet weights.'.format( count, count + remaining_weights)) if remaining_weights > 0: print('Warning: {} unused weights'.format(len(remaining_weights))) if args.plot_model: plot(model, to_file='{}.png'.format(output_root), show_shapes=True) print('Saved model plot to {}.png'.format(output_root))
def UNETGenerator(input_img_dim, num_output_channels): """ Creates the generator according to the specs in the paper below. It's basically a skip layer AutoEncoder Generator does the following: 1. Takes in an image 2. Generates an image from this image Differs from a standard GAN because the image isn't random. This model tries to learn a mapping from a suboptimal image to an optimal image. [https://arxiv.org/pdf/1611.07004v1.pdf][5. Appendix] :param input_img_dim: (channel, height, width) :param output_img_dim: (channel, height, width) :return: """ # ------------------------------- # ENCODER # C64-C128-C256-C512-C512-C512-C512-C512 # 1 layer block = Conv - BN - LeakyRelu # ------------------------------- stride = 2 input_layer = Input(shape=input_img_dim, name="unet_input") filters_en = [64, 128, 256, 512, 512, 512]#, 512, 512] layers = [] en_i = input_layer for (i, nb_filters) in enumerate(filters_en): en_i = Conv2D(filters=nb_filters, kernel_size = 4, padding='same', strides=stride)(en_i) # skip batchnorm on first layer on purpose (from paper) if i > 0: en_i = BatchNormalization()(en_i) en_i = LeakyReLU(alpha=0.2)(en_i) layers += [en_i] # 1 encoder C64 # skip batchnorm on this layer on purpose (from paper) #en_1 = Conv2D(filters=64, kernel_size = 4, padding='same', strides=stride)(input_layer) #en_1 = LeakyReLU(alpha=0.2)(en_1) # 2 encoder C128 #en_2 = Conv2D(filters=128, kernel_size = 4, padding='same', strides=stride)(en_1) #en_2 = BatchNormalization(name='gen_en_bn_2')(en_2) #en_2 = LeakyReLU(alpha=0.2)(en_2) # 3 encoder C256 #en_3 = Conv2D(filters=256, kernel_size = 4, padding='same', strides=stride)(en_2) #en_3 = BatchNormalization(name='gen_en_bn_3')(en_3) #en_3 = LeakyReLU(alpha=0.2)(en_3) # 4 encoder C512 #en_4 = Conv2D(filters=512, kernel_size = 4, padding='same', strides=stride)(en_3) #en_4 = BatchNormalization(name='gen_en_bn_4')(en_4) #en_4 = LeakyReLU(alpha=0.2)(en_4) # 5 encoder C512 #en_5 = Conv2D(filters=512, kernel_size = 4, padding='same', strides=stride)(en_4) #en_5 = BatchNormalization(name='gen_en_bn_5')(en_5) #en_5 = LeakyReLU(alpha=0.2)(en_5) # 6 encoder C512 #en_6 = Conv2D(filters=512, kernel_size = 4, padding='same', strides=stride)(en_5) #en_6 = BatchNormalization(name='gen_en_bn_6')(en_6) #en_6 = LeakyReLU(alpha=0.2)(en_6) # 7 encoder C512 #en_7 = Conv2D(filters=512, kernel_size = 4, padding='same', strides=stride)(en_6) #en_7 = BatchNormalization(name='gen_en_bn_7')(en_7) #en_7 = LeakyReLU(alpha=0.2)(en_7) # 8 encoder C512 #en_8 = Conv2D(filters=512, kernel_size = 4, padding='same', strides=stride)(en_7) #en_8 = BatchNormalization(name='gen_en_bn_8')(en_8) #en_8 = LeakyReLU(alpha=0.2)(en_8) # ------------------------------- # DECODER # CD512-CD1024-CD1024-C1024-C1024-C512-C256-C128 # 1 layer block = Conv - Upsample - BN - DO - Relu # also adds skip connections (merge). Takes input from previous layer matching encoder layer # ------------------------------- de_i = en_i filters_dec = [512, 1024, 1024, #1024, 1024, 512, 256] for (i, nb_filters) in enumerate(filters_dec): de_i = UpSampling2D(size=(2, 2))(de_i) de_i = Conv2D(filters=nb_filters, kernel_size = 4, padding='same')(de_i) de_i = BatchNormalization()(de_i) de_i = Dropout(rate=0.5)(de_i) de_i = Concatenate(axis = -1)([de_i, layers[-(i + 2)]]) de_i = Activation('relu')(de_i) # 1 decoder CD512 (decodes en_8) #de_1 = UpSampling2D(size=(2, 2))(en_8) #de_1 = Conv2D(filters=512, kernel_size = 4, padding='same')(de_1) #de_1 = BatchNormalization(name='gen_de_bn_1')(de_1) #de_1 = Dropout(rate=0.5)(de_1) #de_1 = Concatenate(axis = -1)([de_1, en_7]) #de_1 = Activation('relu')(de_1) # 2 decoder CD1024 (decodes en_7) #de_2 = UpSampling2D(size=(2, 2))(de_1) #de_2 = Conv2D(filters=1024, kernel_size = 4, padding='same')(de_2) #de_2 = BatchNormalization(name='gen_de_bn_2')(de_2) #de_2 = Dropout(rate=0.5)(de_2) #de_2 = Concatenate(axis = -1)([de_2, en_6]) #de_2 = Activation('relu')(de_2) # 3 decoder CD1024 (decodes en_6) #de_3 = UpSampling2D(size=(2, 2))(de_2) #de_3 = Conv2D(filters=1024, kernel_size = 4, padding='same')(de_3) #de_3 = BatchNormalization(name='gen_de_bn_3')(de_3) #de_3 = Dropout(rate=0.5)(de_3) #de_3 = Concatenate(axis = -1)([de_3, en_5]) #de_3 = Activation('relu')(de_3) # 4 decoder CD1024 (decodes en_5) #de_4 = UpSampling2D(size=(2, 2))(de_3) #de_4 = Conv2D(filters=1024, kernel_size = 4, padding='same')(de_4) #de_4 = BatchNormalization(name='gen_de_bn_4')(de_4) #de_4 = Dropout(rate=0.5)(de_4) #de_4 = Concatenate(axis = -1)([de_4, en_4]) #de_4 = Activation('relu')(de_4) # 5 decoder CD1024 (decodes en_4) #de_5 = UpSampling2D(size=(2, 2))(de_4) #de_5 = Conv2D(filters=1024, kernel_size = 4, padding='same')(de_5) #de_5 = BatchNormalization(name='gen_de_bn_5')(de_5) #de_5 = Dropout(rate=0.5)(de_5) #de_5 = Concatenate(axis = -1)([de_5, en_3]) #de_5 = Activation('relu')(de_5) # 6 decoder C512 (decodes en_3) #de_6 = UpSampling2D(size=(2, 2))(de_5) #de_6 = Conv2D(filters=512, kernel_size = 4, padding='same')(de_6) #de_6 = BatchNormalization(name='gen_de_bn_6')(de_6) #de_6 = Dropout(rate=0.5)(de_6) #de_6 = Concatenate(axis = -1)([de_6, en_2]) #de_6 = Activation('relu')(de_6) # 7 decoder CD256 (decodes en_2) #de_7 = UpSampling2D(size=(2, 2))(de_6) #de_7 = Conv2D(filters=256, kernel_size = 4, padding='same')(de_7) #de_7 = BatchNormalization(name='gen_de_bn_7')(de_7) #de_7 = Dropout(rate=0.5)(de_7) #de_7 = Concatenate(axis = -1)([de_7, en_1]) #de_7 = Activation('relu')(de_7) # After the last layer in the decoder, a convolution is applied # to map to the number of output channels (3 in general, # except in colorization, where it is 2), followed by a Tanh # function. de_i = UpSampling2D(size=(2, 2))(de_i)#(de_7) de_i = Conv2D(filters=num_output_channels, kernel_size = 4, padding='same')(de_i) de_i = Activation('tanh')(de_i) unet_generator = Model(inputs=input_layer, outputs=de_i, name='unet_generator') return unet_generator
def unet2D(x_in, input_shape, out_im_chans, nf_enc=[64, 64, 128, 128, 256, 256, 512], nf_dec=None, regularizer=None, initializer=None, layer_prefix='unet', n_convs_per_stage=1, use_residuals=False, use_maxpool=False, concat_at_stages=None, do_last_conv=True, ks=3, ): reg_params = {} if regularizer == 'l1': reg = regularizers.l1(1e-6) else: reg = None if initializer == 'zeros': reg_params['kernel_initializer'] = initializers.Zeros() x = x_in encodings = [] for i in range(len(nf_enc)): if not use_maxpool and i > 0: x = LeakyReLU(0.2)(x) for j in range(n_convs_per_stage): if nf_enc[i] is not None: # in case we dont want to convolve at the first resolution x = Conv2D(nf_enc[i], kernel_regularizer=reg, kernel_size=ks, strides=(1, 1), padding='same', name='{}_enc_conv2D_{}_{}'.format(layer_prefix, i, j + 1))(x) if concat_at_stages and concat_at_stages[i] is not None: x = Concatenate(axis=-1)([x, concat_at_stages[i]]) if j == 0 and use_residuals: residual_input = x elif j == n_convs_per_stage - 1 and use_residuals: x = Add()([residual_input, x]) x = LeakyReLU(0.2)(x) if i < len(nf_enc) - 1: encodings.append(x) if use_maxpool: x = MaxPooling2D(pool_size=(2, 2), padding='same', name='{}_enc_maxpool_{}'.format(layer_prefix, i))(x) else: x = Conv2D(nf_enc[i], kernel_size=ks, strides=(2, 2), padding='same', name='{}_enc_conv2D_{}'.format(layer_prefix, i))(x) print('Encodings to concat later: {}'.format(encodings)) if nf_dec is None: nf_dec = list(reversed(nf_enc[1:])) print('Decoder channels: {}'.format(nf_dec)) for i in range(len(nf_dec)): curr_shape = x.get_shape().as_list()[1:-1] # if we're not at full resolution, keep upsampling if np.any(list(curr_shape[:2]) < list(input_shape[:2])): x = UpSampling2D(size=(2, 2), name='{}_dec_upsamp_{}'.format(layer_prefix, i))(x) # if we still have things to concatenate, do that if (i + 1) <= len(encodings): curr_shape = x.get_shape().as_list()[1:-1] concat_with_shape = encodings[-i - 1].get_shape().as_list()[1:-1] x = _pad_or_crop_to_shape(x, curr_shape, concat_with_shape) x = Concatenate()([x, encodings[-i - 1]]) residual_input = x for j in range(n_convs_per_stage): x = Conv2D(nf_dec[i], kernel_regularizer=reg, kernel_size=ks, strides=(1, 1), padding='same', name='{}_dec_conv2D_{}_{}'.format(layer_prefix, i, j))(x) if j == 0 and use_residuals: residual_input = x elif j == n_convs_per_stage - 1 and use_residuals: x = Add()([residual_input, x]) x = LeakyReLU(0.2)(x) #x = Concatenate()([x, encodings[0]]) ''' for j in range(n_convs_per_stage - 1): x = Conv2D(out_im_chans, kernel_regularizer=reg, kernel_size=ks, strides=(1, 1), padding='same', name='{}_dec_conv2D_last_{}'.format(layer_prefix, j))(x) x = LeakyReLU(0.2)(x) ''' if do_last_conv: y = Conv2D(out_im_chans, kernel_size=1, padding='same', kernel_regularizer=reg, name='{}_dec_conv2D_final'.format(layer_prefix))(x) # add your own activation after this model else: y = x # add your own activation after this model return y
strides=1)) model.add(BatchNormalization()) model.add(Dense(num_filters, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.3)) model.add( Conv1D(num_filters, kernel_size2, padding='same', activation='relu', strides=1)) model.add(BatchNormalization()) model.add(Dense(num_filters, activation='linear')) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=.001)) model.add(Dropout(0.3)) model.add(Bidirectional(GRU(256, return_sequences=True, activation='relu'))) model.add(BatchNormalization()) model.add(Dense(256, activation='linear')) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=.001)) model.add(Dropout(0.3)) model.add(Dense(128, activation='relu')) model.add(BatchNormalization()) model.add(Dense(128, activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.2)) model.add(Dense(64, activation='relu')) model.add(BatchNormalization())
def model(self): input_layer = Input(self.SHAPE) # Incoder down_1 = Convolution2D(64, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(input_layer) down_2 = Convolution2D(64 * 2, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(down_1) norm_2 = BatchNormalization()(down_2) down_3 = Convolution2D(64 * 4, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(norm_2) norm_3 = BatchNormalization()(down_3) down_4 = Convolution2D(64 * 8, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(norm_3) norm_4 = BatchNormalization()(down_4) down_5 = Convolution2D(64 * 8, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(norm_4) norm_5 = BatchNormalization()(down_5) down_6 = Convolution2D(64 * 8, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(norm_5) norm_6 = BatchNormalization()(down_6) down_7 = Convolution2D(64 * 8, kernel_size=4, strides=2, padding='same', activation=LeakyReLU(alpha=0.2))(norm_6) norm_7 = BatchNormalization()(down_7) # Decoder upsample_1 = UpSampling2D(size=2)(norm_7) up_conv_1 = Convolution2D(64 * 8, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_1) norm_up_1 = BatchNormalization(momentum=0.8)(up_conv_1) add_skip_1 = Concatenate()([norm_up_1, norm_6]) upsample_2 = UpSampling2D(size=2)(add_skip_1) up_conv_2 = Convolution2D(64 * 8, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_2) norm_up_2 = BatchNormalization(momentum=0.8)(up_conv_2) add_skip_2 = Concatenate()([norm_up_2, norm_5]) upsample_3 = UpSampling2D(size=2)(add_skip_2) up_conv_3 = Convolution2D(64 * 8, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_3) norm_up_3 = BatchNormalization(momentum=0.8)(up_conv_3) add_skip_3 = Concatenate()([norm_up_3, norm_4]) # top of decoder upsample_4 = UpSampling2D(size=2)(add_skip_3) up_conv_4 = Convolution2D(64 * 4, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_4) norm_up_4 = BatchNormalization(momentum=0.8)(up_conv_4) add_skip_4 = Concatenate()([norm_up_4, norm_3]) # top of decoder upsample_5 = UpSampling2D(size=2)(add_skip_4) up_conv_5 = Convolution2D(64 * 2, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_5) norm_up_5 = BatchNormalization(momentum=0.8)(up_conv_5) add_skip_5 = Concatenate()([norm_up_5, norm_2]) # top of decoder upsample_6 = UpSampling2D(size=2)(add_skip_5) up_conv_6 = Convolution2D(64 * 2, kernel_size=4, strides=1, padding='same', activation='relu')(upsample_6) norm_up_6 = BatchNormalization(momentum=0.8)(up_conv_6) add_skip_6 = Concatenate()([norm_up_6, down_1]) # last upsample and output layer last_upsample = UpSampling2D(size=2)(add_skip_6) output_layer = Convolution2D(3, kernel_size=4, strides=1, padding='same', activation='tanh')(last_upsample) model = Model(input_layer, output_layer) return model
def build(width=224, height=224, depth=3, classes=1000, weightsPath=None): input_shape = (height, width, depth) model = Sequential() model.add(Conv2D(32, (3, 3), padding="same", input_shape=input_shape)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(64, (3, 3), padding="same", input_shape=input_shape)) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(128, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(64, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(128, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(128, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(256, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(512, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(Conv2D(1024, (3, 3), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) '''model.add(Conv2D(classes, (1, 1), padding="same")) model.add(BatchNormalization()) model.add(LeakyReLU(alpha=0.1))''' model.add(GlobalAveragePooling2D(data_format="channels_last")) model.add(Dense(classes)) model.add(BatchNormalization()) model.add(Activation("sigmoid")) # model.add(Dropout(0.5)) #Não há dropout no paper original # model.add(Dense(classes)) '''if classes == 1: model.add(Activation("sigmoid")) else: model.add(Activation("softmax"))''' if weightsPath is not None: model.load_weights(weightsPath) return model