0

I am new to Python and I am trying to use the function 'one_hot_to_label_batch' which can be found on line 115 from this website.

However, directly above this function there is a '@expand_dims'. This is the first time I have encountered this. I know 'expand_dims' is within Numpy but I do not know why it is defined as '@expand_dims' here. Any clarification would be appreciated.

user121
  • 849
  • 3
  • 21
  • 39
  • 1
    I found it at that website. It's a wrapper for the numpy function. You'll find the code in a `utils.py` file up a level or two. – hpaulj Jan 24 '18 at 06:30
  • 1
    https://github.com/SUZhaoyu/keras-semantic-segmentation/blob/develop/src/semseg/data/utils.py – hpaulj Jan 24 '18 at 06:33

1 Answers1

2

This is a decorator. See the top of the file:

from .util import expand_dims

From this line, we can tell the decorator is defined in the util.py file in the same directory. We search in the util.py file, you can find the following function:

def expand_dims(func):
    def wrapper(self, batch):
        ndim = batch.ndim
        if ndim == 3:
            batch = np.expand_dims(batch, axis=0)
        batch = func(self, batch)
        if ndim == 3:
            batch = np.squeeze(batch, axis=0)
        return batch
    return wrapper

The function above is taken from this source

You should check out decorators first if you are not familiar with that.

Tai
  • 7,684
  • 3
  • 29
  • 49
  • if I want to import the `one_hot_to_label_batch` function on line 115 from [here](https://github.com/SUZhaoyu/keras-semantic-segmentation/blob/develop/src/semseg/data/isprs.py) within a new python file in a different directory, how can I? – user121 Jan 24 '18 at 06:48
  • @unknown121 it will depend on the path that you are in. Try to search on import on relative/absolute path. Like this one https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path – Tai Jan 24 '18 at 06:53