def make_discriminator(): """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. Note that the improved WGAN paper suggests that BatchNormalization should not be used in the discriminator.""" model = Sequential() if K.image_data_format() == 'channels_first': model.add( Convolution2D(64, (5, 5), padding='same', input_shape=(1, 28, 28))) else: model.add( Convolution2D(64, (5, 5), padding='same', input_shape=(28, 28, 1))) model.add(LeakyReLU()) model.add( Convolution2D(128, (5, 5), kernel_initializer='he_normal', strides=[2, 2])) model.add(LeakyReLU()) model.add( Convolution2D(128, (5, 5), kernel_initializer='he_normal', padding='same', strides=[2, 2])) model.add(LeakyReLU()) model.add(Flatten()) model.add(Dense(1024, kernel_initializer='he_normal')) model.add(LeakyReLU()) model.add(Dense(1, kernel_initializer='he_normal')) return model
def build_discriminator(self): model = Sequential() model.add( Conv2D(32, kernel_size=3, strides=2, input_shape=self.img_shape, padding="same")) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(64, kernel_size=3, strides=2, padding="same")) model.add(ZeroPadding2D(padding=((0, 1), (0, 1)))) model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(128, kernel_size=3, strides=2, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Conv2D(256, kernel_size=3, strides=1, padding="same")) model.add(BatchNormalization(momentum=0.8)) model.add(LeakyReLU(alpha=0.2)) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.img_shape) validity = model(img) return Model(img, validity)
def make_generator(): """Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images of size 28x28x1.""" model = Sequential() model.add(Dense(1024, input_dim=100)) model.add(LeakyReLU()) model.add(Dense(128 * 7 * 7)) model.add(BatchNormalization()) model.add(LeakyReLU()) if K.image_data_format() == 'channels_first': model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7, ))) bn_axis = 1 else: model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7, ))) bn_axis = -1 model.add(Conv2DTranspose(128, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Convolution2D(64, (5, 5), padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same')) model.add(BatchNormalization(axis=bn_axis)) model.add(LeakyReLU()) # Because we normalized training inputs to lie in the range [-1, 1], # the tanh function should be used for the output of the generator to ensure # its output also lies in this range. model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh')) return model
def discriminator(fixed_bn=False, discr_drop_out=0.2): image = Input(shape=(25, 25, 25, 1), name='image') bnm = 2 if fixed_bn else 0 f = (5, 5, 5) x = _Conv3D(32, 5, 5, 5, border_mode='same', name='disc_c1')(image) x = LeakyReLU()(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((2, 2, 2))(x) x = _Conv3D(8, 5, 5, 5, border_mode='valid', name='disc_c2')(x) x = LeakyReLU()(x) x = _BatchNormalization( name='disc_bn1', mode=bnm, )(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((2, 2, 2))(x) x = _Conv3D(8, 5, 5, 5, border_mode='valid', name='disc_c3')(x) x = LeakyReLU()(x) x = _BatchNormalization( name='disc_bn2', #momentum = 0.00001 mode=bnm, )(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((1, 1, 1))(x) x = _Conv3D(8, 5, 5, 5, border_mode='valid', name='disc_c4')(x) x = LeakyReLU()(x) x = _BatchNormalization( name='disc_bn3', mode=bnm, )(x) x = Dropout(discr_drop_out)(x) x = AveragePooling3D((2, 2, 2))(x) h = Flatten()(x) dnn = _Model(input=image, output=h, name='dnn') dnn_out = dnn(image) fake = _Dense(1, activation='sigmoid', name='classification')(dnn_out) aux = _Dense(1, activation='linear', name='energy')(dnn_out) ecal = Lambda(lambda x: K.sum(x, axis=(1, 2, 3)), name='sum_cell')(image) return _Model(output=[fake, aux, ecal], input=image, name='discriminator_model')
def build_discriminator(self): model = Sequential() model.add(Flatten(input_shape=self.img_shape)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(256)) model.add(LeakyReLU(alpha=0.2)) model.add(Dense(1, activation='sigmoid')) model.summary() img = Input(shape=self.img_shape) validity = model(img) return Model(img, validity)
def conv2d_unit(x, filters, kernels, strides=1): """Convolution Unit This function defines a 2D convolution operation with BN and LeakyReLU. # Arguments x: Tensor, input tensor of conv layer. filters: Integer, the dimensionality of the output space. kernels: An integer or tuple/list of 2 integers, specifying the width and height of the 2D convolution window. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the width and height. Can be a single integer to specify the same value for all spatial dimensions. # Returns Output tensor. """ x = Conv2D(filters, kernels, padding='same', strides=strides, activation='linear', kernel_regularizer=l2(5e-4))(x) x = BatchNormalization()(x) x = LeakyReLU(alpha=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 discriminator_model(shape=None): discriminator = Sequential() # discriminator.add(Conv(64, kernel_size=(5, 5), strides=(2, 2), padding='same', input_shape=(28,28, 1), kernel_initializer=initializers.RandomNormal(stddev=0.02))) discriminator.add( Conv(64, kernel_size=5, strides=2, padding='same', input_shape=shape, kernel_initializer=initializers.RandomNormal(stddev=0.02))) discriminator.add(LeakyReLU(0.2)) discriminator.add(Dropout(0.3)) discriminator.add(Conv(128, kernel_size=5, strides=2, padding='same')) discriminator.add(LeakyReLU(0.2)) discriminator.add(Dropout(0.3)) discriminator.add(Flatten()) discriminator.add(Dense(1, activation='sigmoid')) discriminator.compile(loss='binary_crossentropy', optimizer='adam') return discriminator
def generator_model(shape=None): generator = Sequential() # generator.add(Dense(128*7*7, input_dim=100, kernel_initializer=initializers.RandomNormal(stddev=0.02))) # generator.add(LeakyReLU(0.2)) # generator.add(Reshape((7, 7, 128))) generator.add( Dense(128 * shape[0] // 2 // 2 * shape[0] // 2 // 2, input_dim=100, kernel_initializer=initializers.RandomNormal(stddev=0.02))) generator.add(LeakyReLU(0.2)) generator.add(Reshape((*shape[0] // 2 // 2, 128))) generator.add(UpSampling(size=2)) generator.add(Conv(64, kernel_size=5, padding='same')) generator.add(LeakyReLU(0.2)) generator.add(UpSampling(size=2)) generator.add(Conv(1, kernel_size=5, padding='same', activation='tanh')) generator.compile(loss='binary_crossentropy', optimizer='adam') return generator
def build_generator(self): model = Sequential() model.add(Dense(256, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(1024)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() noise = Input(shape=(self.latent_dim, )) img = model(noise) return Model(noise, img)
def generator(latent_size=200, return_intermediate=False, with_bn=True): latent = Input(shape=(latent_size, )) bnm = 0 x = _Dense(64 * 7 * 7, init='glorot_normal', name='gen_dense1')(latent) x = Reshape((7, 7, 8, 8))(x) x = _Conv3D(64, 6, 6, 8, border_mode='same', init='he_uniform', name='gen_c1')(x) x = LeakyReLU()(x) if with_bn: x = _BatchNormalization(name='gen_bn1', mode=bnm)(x) x = UpSampling3D(size=(2, 2, 2))(x) x = ZeroPadding3D((2, 2, 0))(x) x = _Conv3D(6, 6, 5, 8, init='he_uniform', name='gen_c2')(x) x = LeakyReLU()(x) if with_bn: x = _BatchNormalization(name='gen_bn2', mode=bnm)(x) x = UpSampling3D(size=(2, 2, 3))(x) x = ZeroPadding3D((1, 0, 3))(x) x = _Conv3D(6, 3, 3, 8, init='he_uniform', name='gen_c3')(x) x = LeakyReLU()(x) x = _Conv3D(1, 2, 2, 2, bias=False, init='glorot_normal', name='gen_c4')(x) x = Activation('relu')(x) loc = _Model(input=latent, output=x) fake_image = loc(latent) _Model(input=[latent], output=fake_image) return _Model(input=[latent], output=fake_image, name='generator_model')
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("maxpool"): size = int(cfg_parser[section]["size"]) stride = int(cfg_parser[section]["stride"]) all_layers.append( MaxPooling2D(pool_size=(size, size), strides=(stride, stride), padding="same")(prev_layer)) prev_layer = all_layers[-1] 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. if len(out_index) == 0: out_index.append(len(all_layers) - 1) model = Model(inputs=input_layer, outputs=[all_layers[i] for i in out_index]) print(model.summary()) if args.weights_only: model.save_weights("{}".format(output_path)) print("Saved Keras weights to {}".format(output_path)) else: 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 _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 big_assemble_models(self): image = Input(shape=(25, 25, 25, 1), name='image') x = _Conv3D(32, 5, 5, 5, border_mode='same')(image) x = LeakyReLU()(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((2, 2, 2))(x) x = _Conv3D(8, 5, 5, 5, border_mode='valid')(x) x = LeakyReLU()(x) x = BatchNormalization()(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((2, 2, 2))(x) x = Conv3D(8, 5, 5, 5, border_mode='valid')(x) x = LeakyReLU()(x) x = BatchNormalization()(x) x = Dropout(discr_drop_out)(x) x = ZeroPadding3D((1, 1, 1))(x) x = Conv3D(8, 5, 5, 5, border_mode='valid')(x) x = LeakyReLU()(x) x = BatchNormalization()(x) x = Dropout(discr_drop_out)(x) x = AveragePooling3D((2, 2, 2))(x) h = Flatten()(x) fake = Dense(1, activation='sigmoid', name='generation')(h) aux = Dense(1, activation='linear', name='auxiliary')(h) ecal = Lambda(lambda x: K.sum(x, axis=(1, 2, 3)), name='sum_cell')(image) self.discriminator = Model(output=[fake, aux, ecal], input=image, name='discriminator_model') latent = Input(shape=(self.latent_size, )) x = Dense(64 * 7 * 7, init='he_uniform')(latent) x = Reshape((7, 7, 8, 8))(x) x = Conv3D(64, 6, 6, 8, border_mode='same', init='he_uniform')(x) x = LeakyReLU()(x) x = BatchNormalization()(x) x = UpSampling3D(size=(2, 2, 2))(x) x = ZeroPadding3D((2, 2, 0))(x) x = Conv3D(6, 6, 5, 8, init='he_uniform')(x) x = LeakyReLU()(x) x = BatchNormalization()(x) x = UpSampling3D(size=(2, 2, 3))(x) x = ZeroPadding3D((1, 0, 3))(x) x = Conv3D(6, 3, 3, 8, init='he_uniform')(x) x = LeakyReLU()(x) x = Conv3D(1, 2, 2, 2, bias=False, init='glorot_normal')(x) x = Activation('relu')(x) loc = Model(latent, x) #fake_image = loc(latent) self.generator = Model(input=latent, output=x, name='generator_model') c_fake, c_aux, c_ecal = self.discriminator(x) self.combined = Model(input=latent, output=[c_fake, c_aux, c_ecal], name='combined_model')
def DCGAN_discriminator(img_shape, disc_img_shape, patch_num, model_name='DCGAN_discriminator'): disc_raw_img_shape = (disc_img_shape[0], disc_img_shape[1], img_shape[-1]) list_input = [ Input(shape=disc_img_shape, name='disc_input_' + str(i)) for i in range(patch_num) ] list_raw_input = [ Input(shape=disc_raw_img_shape, name='disc_raw_input_' + str(i)) for i in range(patch_num) ] axis_num = -1 filters_num = 64 conv_num = int(np.floor(np.log(disc_img_shape[1]) / np.log(2))) list_filters = [filters_num * min(8, (2**i)) for i in range(conv_num)] # First Conv generated_patch_input = Input(shape=disc_img_shape, name='discriminator_input') xg = Conv2D(list_filters[0], (3, 3), strides=(2, 2), name='disc_conv2d_1', padding='same')(generated_patch_input) xg = BatchNormalization(axis=axis_num)(xg) xg = LeakyReLU(0.2)(xg) # First Raw Conv raw_patch_input = Input(shape=disc_raw_img_shape, name='discriminator_raw_input') xr = Conv2D(list_filters[0], (3, 3), strides=(2, 2), name='raw_disc_conv2d_1', padding='same')(raw_patch_input) xr = BatchNormalization(axis=axis_num)(xr) xr = LeakyReLU(0.2)(xr) # Next Conv for i, f in enumerate(list_filters[1:]): name = 'disc_conv2d_' + str(i + 2) x = Concatenate(axis=axis_num)([xg, xr]) x = Conv2D(f, (3, 3), strides=(2, 2), name=name, padding='same')(x) x = BatchNormalization(axis=axis_num)(x) x = LeakyReLU(0.2)(x) x_flat = Flatten()(x) x = Dense(2, activation='softmax', name='disc_dense')(x_flat) PatchGAN = Model(inputs=[generated_patch_input, raw_patch_input], outputs=[x], name='PatchGAN') x = [ PatchGAN([list_input[i], list_raw_input[i]]) for i in range(patch_num) ] if len(x) > 1: x = Concatenate(axis=axis_num)(x) else: x = x[0] x_out = Dense(2, activation='softmax', name='disc_output')(x) discriminator_model = Model(inputs=(list_input + list_raw_input), outputs=[x_out], name=model_name) return discriminator_model
def conv_block_unet(x, f, name, bn_axis, bn=True, strides=(2, 2)): x = LeakyReLU(0.2)(x) x = Conv2D(f, (3, 3), strides=strides, name=name, padding='same')(x) if bn: x = BatchNormalization(axis=bn_axis)(x) return x
def set(inputs, include_input=False): """ input should be HxWx3 input Default shape 416x416x3 """ if include_input: input_image = Input(shape=(IMAGE_H, IMAGE_W, 3), name='input') #true_boxes = Input(shape=(1, 1, 1, TRUE_BOX_BUFFER , 4)) idx = 0 conv_n = 'conv_%d' norm_n = 'norm_%d' idx += 1 # Layer 1 (Dim:416x416x3, /1) x = Conv2D(32, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(input_image) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) idx += 1 # Layer 2 (Dim:208x208x32, /2) x = Conv2D(64, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) idx += 1 # Layer 3 (Dim:104x104x64, /4) x = Conv2D(128, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 4 (Dim:104x104x128, /4) x = Conv2D(64, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 5 (Dim:104x104x64, /4) x = Conv2D(128, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) idx += 1 # Layer 6 (Dim:52x52x128, /8) x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 7 (Dim:52x52x256, /8) x = Conv2D(128, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 8 (Dim:52x52x128, /8) x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False, input_shape=(416, 416, 3))(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) x = MaxPooling2D(pool_size=(2, 2))(x) idx += 1 # Layer 9 (Dim:26x26x256, /16) x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 10 (Dim:26x26x512, /16) x = Conv2D(256, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 11 (Dim:26x26x128, /16) x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 12 (Dim:26x26x512, /16) x = Conv2D(256, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 13 (Dim:26x26x256, /16) x = Conv2D(512, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) skip_connection = x x = MaxPooling2D(pool_size=(2, 2))(x) idx += 1 # Layer 14 (Dim:13x13x512, /32) x = Conv2D(1024, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 15 (Dim:13x13x1024, /32) x = Conv2D(512, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 16 (Dim:13x13x512, /32) x = Conv2D(1024, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 17 (Dim:13x13x1024, /32) x = Conv2D(512, (1, 1), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) idx += 1 # Layer 18 (Dim:13x13x512, /32) x = Conv2D(1024, (3, 3), strides=(1, 1), padding='same', name=conv_n % idx, use_bias=False)(x) x = BatchNormalization(name=norm_n % idx)(x) x = LeakyReLU(alpha=0.1)(x) return x, skip_connection, idx