def test_expected_values():
    image = get_test_image(tile_factor=0)
    rf = KerasReceptiveField(get_build_func(padding="valid"),
                             init_weights=True)
    rf_params0 = rf.compute(
        input_shape=ImageShape(*image.shape),
        input_layer="input_image",
        output_layers=["conv1"],
    )[0]

    assert_allclose(rf_params0.rf.stride, (2, 2))
    assert_allclose(rf_params0.rf.size,
                    (((2 + 1) * 2 + 2) * 2, ((2 + 1) * 2 + 2) * 2))
    def test_custom_layer(self):

        rf_params = KerasReceptiveField(model_build_func,
                                        init_weights=True).compute(
                                            input_shape=(224, 224, 3),
                                            input_layer="image",
                                            output_layers=["feature_map"],
                                        )
        expected_rf = ReceptiveFieldDescription(offset=(2.5, 2.5),
                                                stride=(1.0, 1.0),
                                                size=Size(w=5, h=5))
        self.assertEqual(rf_params[0].rf, expected_rf)
Exemple #3
0
def test_same():
    image = get_test_image(tile_factor=0)
    rf = KerasReceptiveField(get_build_func(padding='same'), init_weights=True)
    rf_params0 = rf.compute(input_shape=ImageShape(*image.shape),
                            input_layer='input_image',
                            output_layer='conv')

    print(rf_params0)

    image = get_test_image(tile_factor=1)
    rf = KerasReceptiveField(get_build_func(padding='same'), init_weights=True)
    rf_params1 = rf.compute(input_shape=ImageShape(*image.shape),
                            input_layer='input_image',
                            output_layer='conv')

    print(rf_params1)
    assert_allclose(rf_params0, rf_params1)
def test_same():
    image = get_test_image(tile_factor=0)
    rf = KerasReceptiveField(get_build_func(padding="same"), init_weights=True)
    rf_params0 = rf.compute(
        input_shape=ImageShape(*image.shape),
        input_layer="input_image",
        output_layers=["conv1"],
    )

    print(rf_params0)

    image = get_test_image(tile_factor=1)
    rf = KerasReceptiveField(get_build_func(padding="same"), init_weights=True)
    rf_params1 = rf.compute(
        input_shape=ImageShape(*image.shape),
        input_layer="input_image",
        output_layers=["conv1"],
    )

    print(rf_params1)
    assert_allclose(rf_params0[0].rf, rf_params1[0].rf)
# list of model files to inspect
model_files = glob.glob(os.path.join(saved_models_dir, model_name))

receptive_field_size = []
for model_file in model_files:

    print(model_file)

    # estimate receptive field of the model
    def model_build_func(input_shape):
        model = fcn_sherrah2016_regression_and_classifier(input_shape=input_shape, for_receptive_field=True)
        model.load_weights(model_file)
        return model


    rf = KerasReceptiveField(model_build_func, init_weights=False)

    rf_params = rf.compute(
        input_shape=(500, 500, 3),
        input_layer='input_image',
        output_layers=['regression_output', 'classification_output'])
    print(rf_params)

    receptive_field_size.append(rf._rf_params[0].size)

for i, model_file in enumerate(model_files):
    print(model_file)
    print('Receptive field size: ' + str(receptive_field_size[i]))

'''Load model and visualise results
'''
Exemple #6
0
'''Estimate the effective receptive field pre-training
'''

##########################################################################################
# fcn_sherrah2016_regression
##########################################################################################


# estimate receptive field of the model
def model_build_func(input_shape):
    model = fcn_sherrah2016_regression(input_shape=input_shape,
                                       for_receptive_field=True)
    return model


rf = KerasReceptiveField(model_build_func, init_weights=True)

rf_params = rf.compute(input_shape=(512, 512, 3),
                       input_layer='input_image',
                       output_layers=['regression_output'])
print(rf_params)

print('Sherrah 2016 effective receptive field: ' + str(rf._rf_params[0].size))

##########################################################################################
# U-net
##########################################################################################


# estimate receptive field of the model
def model_build_func(input_shape):
rf_size = []
output = {'nblocks': [], 'kernel_len': [], 'dilation_rate': [], 'rf_size': []}
for nblocks in nblocks_vec:
    for kernel_len in kernel_len_vec:
        for dilation_rate in dilation_rate_vec:

            # estimate receptive field of the model
            def model_build_func(input_shape):
                return models.fcn_conv_bnorm_maxpool_regression(
                    input_shape=input_shape,
                    for_receptive_field=True,
                    nblocks=nblocks,
                    kernel_len=kernel_len,
                    dilation_rate=dilation_rate)

            rf = KerasReceptiveField(model_build_func, init_weights=True)

            try:
                rf_params = rf.compute(input_shape=(1000, 1000, 1),
                                       input_layer='input_image',
                                       output_layer='main_output')
                output['nblocks'].append(nblocks)
                output['kernel_len'].append(kernel_len)
                output['dilation_rate'].append(dilation_rate)
                output['rf_size'].append(rf_params[2].w)

            except ResourceExhaustedError:  # if we run out of memory
                output['nblocks'].append(nblocks)
                output['kernel_len'].append(kernel_len)
                output['dilation_rate'].append(dilation_rate)
                output['rf_size'].append(float('nan'))
Exemple #8
0
# tf.config.set_visible_devices([], 'GPU')

shape = (300, 300, 3)

# model = tf.keras.applications.VGG16(input_shape=shape, include_top=False)
model = efn.EfficientNetB3(input_shape=shape, include_top=False, weights=None)
# model = C2D(shape)

# model.summary()

def model_build_func(input_shape):
    return model

# compute receptive field
rf = KerasReceptiveField(model_build_func)

layers = list(filter(lambda y: (y.name.endswith("add") or y.name == "top_conv"), model.layers))
# layers = model.layers[1:]#  list(filter(lambda y: y.name.endswith("project_BN") or y.name.endswith("add"), model.layers))
layers_names = [x.name for x in layers]

rf_params = rf.compute(shape, 'input_1', layers_names)

for i, x in enumerate(rf_params):
    print("%-16s %-16s: %s" % (layers_names[i], layers[i].output_shape[1:], {"offset": x.rf.offset, "stride": x.rf.stride, "size": (x.rf.size.h, x.rf.size.w)}))

# debug receptive field
# rf.plot_rf_grids(get_default_image(shape, name='doge'))
# plt.show()

### EfficientNetB0 (224, 224, 3), RFs berechnet mit (528, 528, 3)