Convolutional layer
- class layers.convolutional_layer.Convolutional_layer(filters, size, stride=None, input_shape=None, weights=None, bias=None, pad=False, activation=<class 'NumPyNet.activations.Activations'>, **kwargs)[source]
Bases:
NumPyNet.layers.base.BaseLayer
Convolutional Layer
- Parameters
filters (int) – Number of filters to be slided over the input, and also the number of channels of the output (channels_out)
size (tuple) – Size of the kernel of shape (kx, ky).
stride (tuple (default=None)) – Step of the kernel, with shape (st1, st2). If None, stride is assigned size values.
input_shape (tuple (default=None)) – Shape of the input in the format (batch, w, h, c), None is used when the layer is part of a Network model.
weights (array-like (default=None)) – Filters of the convolutionanl layer, with shape (kx, ky, channels_in, filters). If None, random weights are initialized
bias (array-like (default=None)) – Bias of the convolutional layer. If None, bias init is random with shape (filters, )
pad (bool (default=False)) – If False the image is cutted along the last raws and columns, if True the input is padded following keras SAME padding
activation (str or Activation object) – Activation function of the layer.
Example
>>> import os >>> from PIL import Image >>> import pylab as plt >>> from NumPyNet import activations >>> >>> img_2_float = lambda im : ((im - im.min()) * (1./(im.max() - im.min()) * 1.)).astype(float) >>> float_2_img = lambda im : ((im - im.min()) * (1./(im.max() - im.min()) * 255.)).astype(np.uint8) >>> >>> filename = os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'dog.jpg') >>> inpt = np.asarray(Image.open(filename), dtype=float) >>> inpt.setflags(write=1) >>> inpt = img_2_float(inpt) >>> # Relu activation constrain >>> inpt = inpt * 2 - 1 >>> >>> inpt = np.expand_dims(inpt, axis=0) # shape from (w, h, c) to (1, w, h, c) >>> >>> channels_out = 10 >>> size = (3, 3) >>> stride = (1, 1) >>> pad = False >>> >>> layer_activation = activations.Relu() >>> >>> np.random.seed(123) >>> >>> b, w, h, c = inpt.shape >>> filters = np.random.uniform(-1., 1., size = (size[0], size[1], c, channels_out)) >>> # bias = np.random.uniform(-1., 1., size = (channels_out,)) >>> bias = np.zeros(shape=(channels_out,)) >>> >>> layer = Convolutional_layer(input_shape=inpt.shape, >>> filters=channels_out, >>> weights=filters, >>> bias=bias, >>> activation=layer_activation, >>> size=size, >>> stride=stride, >>> pad=pad) >>> >>> # FORWARD >>> >>> layer.forward(inpt) >>> forward_out = layer.output.copy() >>> >>> # after the forward to load all the attribute >>> print(layer) >>> >>> # BACKWARD >>> >>> layer.delta = np.ones(layer.out_shape, dtype=float) >>> delta = np.zeros(shape=inpt.shape, dtype=float) >>> layer.backward(delta) >>> >>> # layer.update() >>> >>> # Visualization >>> >>> fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(10, 5)) >>> fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.15) >>> >>> fig.suptitle('Convolutional Layer') >>> >>> ax1.imshow(float_2_img(inpt[0])) >>> ax1.set_title('Original image') >>> ax1.axis('off') >>> # here every filter effect on the image can be shown >>> ax2.imshow(float_2_img(forward_out[0, :, :, 1])) >>> ax2.set_title('Forward') >>> ax2.axis('off') >>> >>> ax3.imshow(float_2_img(delta[0])) >>> ax3.set_title('Backward') >>> ax3.axis('off') >>> >>> fig.tight_layout() >>> plt.show()
References
- backward(delta, copy=False)[source]
Backward function of the Convolutional layer. Source: https://arxiv.org/abs/1603.07285
- Parameters
delta (array-like) – delta array of shape (batch, w, h, c). Global delta to be backpropagated.
copy (bool (default=False)) – States if the activation function have to return a copy of the input or not.
- Return type
self
- forward(inpt, copy=False)[source]
Forward function of the Convolutional Layer: it convolves an image with ‘channels_out’ filters with dimension (kx, ky, channels_in). In doing so, it creates a view of the image with shape (batch, out_w, out_h, in_c, kx, ky) in order to perform a single matrix multiplication with the reshaped filters array, which shape is (in_c * kx * ky, out_c).
- Parameters
inpt (array-like) – Input batch of images in format (batch, in_w, in_h, in _c)
copy (bool (default=False)) – If False the activation function modifies its input, if True make a copy instead
- Return type
self
- load_weights(chunck_weights, pos=0)[source]
Load weights from full array of model weights
- Parameters
chunck_weights (array-like) – model weights and bias
pos (int (default=0)) – Current position of the array
- Returns
pos – Updated stream position.
- Return type
int
- property out_shape
Get the output shape as (batch, out_w, out_h, out_channels)