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)
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))
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 ''' # list of model files to inspect model_files = glob.glob(os.path.join(saved_models_dir, model_name))
########################################################################################## # 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): model = unet(input_shape=input_shape, for_receptive_field=True) return model
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 = models.fcn_sherrah2016_regression(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=['main_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 ''' for fold_i, model_file in enumerate(model_files): # select test data (data not used for training) idx_test = idx_test_all[fold_i]
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')) # convert to arrays, so that we can index with a vector output['nblocks'] = np.array(output['nblocks']) output['kernel_len'] = np.array(output['kernel_len'])
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) # block2b_add (56, 56, 24): {'offset': (3.5, 3.5), 'stride': (4.0, 4.0), 'size': (19, 19)} ### -> EfficientNetB0_Block2 (75264) ? Keine guten Ergebnisse zu erwarten # block3b_add (28, 28, 40): {'offset': (7.5, 7.5), 'stride': (8.0, 8.0), 'size': (67, 67)} ### -> EfficientNetB0_Block3 (31360) # block4b_add (14, 14, 80): {'offset': (15.5, 15.5), 'stride': (16.0, 16.0), 'size': (115, 115)} # block4c_add (14, 14, 80): {'offset': (15.5, 15.5), 'stride': (16.0, 16.0), 'size': (147, 147)} ### -> EfficientNetB0_Block4 (15680) # block5b_add (14, 14, 112): {'offset': (15.5, 15.5), 'stride': (16.0, 16.0), 'size': (275, 275)} # block5c_add (14, 14, 112): {'offset': (15.5, 15.5), 'stride': (16.0, 16.0), 'size': (339, 339)} ### -> EfficientNetB0_Block5 (21952)