コード例 #1
0
ファイル: cams.py プロジェクト: ml-edu/high-res-mapping
    def __init__(self,
                 weights=opt.half_weight_dir,
                 num_classes=opt.num_classes,
                 im_size=opt.im_size,
                 channels=opt.channels):
        """
        Wrapper for easier predictions and debugging of a HalfDenseNetFCN model.
        :param weights: Path to a file containing the weights of a pre-trained HalfDenseNetFCN.
        :param num_classes: An integer representing the number of classes.
        :param im_size: An integer representing the size of a single dimension each image.
        :param channels: How many channels does the image have (e.g. 1 -> grayscale, 3 -> RGB)
        """
        print('Instantiating a Half Model:')
        print('weights:     {}'.format(weights))
        print('classes:     {}'.format(num_classes))

        image_dims = (im_size, im_size)
        input_shape = image_dims + (channels, )
        print('input shape: {}'.format(input_shape))

        # Main model
        self.model = custom_models.HalfDenseNetFCN(input_shape, num_classes,
                                                   weights)

        # Ak model
        self.Ak_model = Model(inputs=self.model.input,
                              outputs=self.model.layers[-3].output)

        # Wk array
        self.Wk = self.model.layers[-1].get_weights()[0]
コード例 #2
0
                  order=[0, 1],  # use nearest neighbour or bilinear interpolation (fast)
                  cval=0,
                  mode='constant'
                  )),
      iaa.Fliplr(0.5)
      ])

# Initialize Image Generators for batching
tg = ImageDataGenerator(preprocessing_function=seq.augment_image)
vg = ImageDataGenerator()

train_gen = tg.flow_from_directory(train_path, target_size=img_dims, batch_size=batch_size)
test_gen = vg.flow_from_directory(test_path, target_size=img_dims, batch_size=batch_size)

# Model
model = custom_models.HalfDenseNetFCN(input_shape, num_classes)

# Load previously trained model
if half_weight_dir:
    previous_weights = half_weight_dir
    model.load_weights(previous_weights)

# Define keras callbacks
lr_reducer = ReduceLROnPlateau(monitor='loss', factor=np.sqrt(0.1), patience=10, cooldown=0, min_lr=1e-5, verbose=1)
tb = TensorBoard(log_dir=log_dir, batch_size=batch_size)
model_chp = ModelCheckpoint(os.path.join(results_dir, 'best_weights.h5'), monitor='val_acc',
                            save_best_only=True, save_weights_only=True)

# Train the model
model.fit_generator(train_gen, epochs=epochs, callbacks=[tb, model_chp, lr_reducer], validation_data=test_gen)
コード例 #3
0
                               factor=np.sqrt(0.1),
                               patience=10,
                               cooldown=0,
                               min_lr=1e-5)
model_chp = ModelCheckpoint(os.path.join(results_dir, 'best_weights.h5'),
                            monitor='loss',
                            save_best_only=True,
                            save_weights_only=True)
tb = TensorBoard(log_dir=log_dir, batch_size=batch_size)

# Instantiate full and half models
model = custom_models.AuxiliaryDenseNetFCN(input_shape,
                                           num_classes,
                                           weights=None)
pretrained = custom_models.HalfDenseNetFCN(input_shape,
                                           num_classes,
                                           weights=weight_dir)

# Transfer weights from half to full and freeze them
transfer_weights(pretrained, model)
del pretrained

for layer in model.layers[:121] + [
        model.layers[-5], model.layers[-3], model.layers[-1]
]:
    layer.trainable = False

# Re-compile the model
model.compile(optimizer='adadelta',
              loss={
                  'main_output': 'mean_squared_error',