<jupyter_start><jupyter_text>Author: <NAME> (akc at apkc.net) / www.apkc.net  Image Processing  Imports<jupyter_code>%run '../../../imports.py'<jupyter_output><empty_output><jupyter_text>Configuration<jupyter_code>%run '../../../config.py'
_ = mpl.rcParams['figure.figsize'] = (8, 8)<jupyter_output><empty_output><jupyter_text>Environment variables<jupyter_code>%run '../../../env_variables.py'<jupyter_output><empty_output><jupyter_text>Download Image<jupyter_code>img_ori = utilities.Utilities.read_image(url='http://www.apkc.net/data/cv_images/0012.jpeg')
img_ori = cv.cvtColor(img_ori, cv.COLOR_BGR2RGB) # Convert from BGR to RGB.
img_ori.shape<jupyter_output><empty_output><jupyter_text>Reading an image using OpenCV<jupyter_code>_ = plt.imshow(img_ori, aspect='equal')
img_ori[:, :, 0]<jupyter_output><empty_output><jupyter_text>Separate Channels<jupyter_code>fig = plt.figure(figsize=(8, 12))
gs = fig.add_gridspec(2, 2)
ax1 = fig.add_subplot(gs[0, 0])
ax2 = fig.add_subplot(gs[0, 1])
ax3 = fig.add_subplot(gs[1, 0])
ax4 = fig.add_subplot(gs[1, 1])

channel_1 = img_ori[: ,: ,0] # Red channel.
channel_2 = img_ori[: ,: ,1] # Green channel.
channel_3 = img_ori[: ,: ,2] # Blue channel.

# Use different colors maps to paint the image.
_ = ax1.imshow(channel_1, aspect='equal', cmap='Reds_r')
_ = ax2.imshow(channel_2, aspect='equal', cmap='Greens_r')
_ = ax3.imshow(channel_3, aspect='equal', cmap='Blues_r')
_ = ax4.imshow(channel_1, aspect='equal', cmap='Greys_r')<jupyter_output><empty_output><jupyter_text>Convolutions  Very Simple Example<jupyter_code># Hyper-parameters
padding = 1

# Images
img = img_ori[:, :, 0] # Grab only the first channel.
# Pad original image.
img_pad = np.pad(img, (padding, padding), mode='constant', constant_values=0)

# Output image.
out = convolution.Convolution().convolution2d(
    img=img_pad, 
    kernel=convolution.Convolution.KERNELS[convolution.Convolution.IDENTITY], 
    padding=padding
)

# Verify that both images are equal. Must be True with the identity kernel.
print('===')
print('* Images are equal: {}'.format(np.array_equal(img, out)))
print('* Input img size: {}'.format(img.shape))
print('* Output img size: {}'.format(out.shape))
print('===')

# Print output image.
_ = plt.imshow(out, aspect='equal', cmap='Greys_r')<jupyter_output><empty_output><jupyter_text>Detect Edges<jupyter_code># Hyper-parameters
padding = 1

# Images
img = img_ori[:, :, 0] # Grab only the first channel.
# Pad original image.
img_pad = np.pad(img, (padding, padding), mode='constant', constant_values=0)
# Output image.
out = convolution.Convolution().convolution2d(
    img=img_pad, 
    kernel=convolution.Convolution.KERNELS[convolution.Convolution.EDGES_3], 
    padding=padding
)

# Verify that both images are equal. Must be True with the identity kernel.
print('===')
print('* Images are equal: {}'.format(np.array_equal(img, out)))
print('* Input img size: {}'.format(img.shape))
print('* Output img size: {}'.format(out.shape))
print('===')

# Print output image.
_ = plt.imshow(out, aspect='equal', cmap='Greys_r')<jupyter_output><empty_output><jupyter_text>Full Convolution ExampleIn this example I will try to mimick a forward pass in a CNN. Kernels are set manually to random weights to observe different results in each layer's activation maps.<jupyter_code># Hyper-parameters
padding = 1

# Images
img = img_ori # Grab all channels.

# Load the kernels for each layer. They all share the same 4 available kernels.
kernels = np.zeros((4, 3, 3, 3))
for n in range(kernels.shape[0]):
    kernels[n] = convolution.Convolution.KERNELS[n + 1]

# Build the dummy net.
net = dummy_network.DummyNetwork(
    layers=[
        conv_layer.ConvLayer(), 
        activations.Activations(), 
        pooling_layer.PoolingLayer(),
        conv_layer.ConvLayer(), 
        activations.Activations(), 
        pooling_layer.PoolingLayer(),
        conv_layer.ConvLayer(), 
        activations.Activations(), 
        pooling_layer.PoolingLayer()
    ],
    kernels=kernels
)

# Make a forward pass.
net.conv_layer_forward(img, padding, True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1504015575565831, 'fraction_numerical': 0.013628620102214651, 'mean_word_length': 5.284403669724771, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '85738', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1451, 'n_words': 312, 'path': 'notebooks/Proprietary/Computer_Vision/Convolutions.ipynb', 'repo_name': 'k-zen/SigmaProject', 'size': '4121'}