1

I am reading the following code segment, this function was once invoked as generator = image_gen.get_image_gen_rgb(nx, ny, cnt=20). I am not very clear about the usage of **kwargs in the function of get_image_gen_rgb. Does cnt=20 was used to setup the parameter of n_image in create_batch

def get_image_gen_rgb(nx, ny, **kwargs):
    def create_batch(n_image):
        print("n_image: ",n_image)
        X = np.zeros((n_image, nx, ny, 3))
        Y = np.zeros((n_image, nx, ny,2))

        for i in range(n_image):
            x, Y[i,:,:,1] = create_image_and_label(nx,ny, **kwargs)
            X[i] = to_rgb(x)
            Y[i,:,:,0] = 1-Y[i,:,:,1]

        return X, Y
  create_batch.channels = 3
  create_batch.n_class = 2
  return create_batch
user288609
  • 12,465
  • 26
  • 85
  • 127

1 Answers1

0

cnt=20 was used for the derivation of x and second slice of the last axis of Y via create_image_and_label:

x, Y[i,:,:,1] = create_image_and_label(nx,ny, **kwargs) # kwargs = {'cnt': 20}
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Hi Moses, in the context of this code segment, which one is used to setup n_image. I added a print out function, and found n_image = 19. But the way, what do you mean the second slice of the last axis of Y? Thanks – user288609 Sep 26 '16 at 22:20
  • `n_image` is not setup in that code. You're returning the function `create_batch` which when called, `n_image` is passed; you should loop up closures in Python. First slice of last axis: `Y[i,:,:,0]` and second slice `Y[i,:,:,1]`; a numpy thing – Moses Koledoye Sep 26 '16 at 22:24
  • Thanks for the reply. if n_image is not setup, where does it come from? I added a print function in the original code, and found that it keeps output 19. But I just could not find where does it come from. – user288609 Sep 26 '16 at 22:28
  • That function will be called as `image_gen.get_image_gen_rgb(nx, ny, cnt=20)(19)`. Have a look at closures in Python – Moses Koledoye Sep 26 '16 at 22:29