-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvolution_relu_edge_detection.py
More file actions
45 lines (38 loc) · 1.35 KB
/
Copy pathconvolution_relu_edge_detection.py
File metadata and controls
45 lines (38 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import tensorflow as tf
import os
import matplotlib.pyplot as plt
image_files = [
'images.jpg',
'aic-home (1).jpg',
'jpegls-home (1).jpg',
'Un_super_paysage.jpg',
'what-is-jpeg-format (1).jpg'
]
kernel = tf.constant([
[-1., -1., -1.],
[-1., 8., -1.],
[-1., -1., -1.]
], dtype=tf.float32)
kernel = tf.reshape(kernel, [3, 3, 1, 1]) # [height, width, in_channels, out_channels]
for image_path in image_files:
if os.path.exists(image_path):
# Read, decode, grayscale, resize
image = tf.io.read_file(image_path)
image = tf.io.decode_jpeg(image, channels=1)
image = tf.image.resize(image, [300, 300])
# Convert to float32
image = tf.image.convert_image_dtype(image, tf.float32)
# Add batch dimension
image = tf.expand_dims(image, axis=0)
print(f"Image shape ready for CNN ({image_path}):", image.shape)
# Apply convolution (edge detection)
conv = tf.nn.conv2d(image, filters=kernel, strides=1, padding='SAME')
relu = tf.nn.relu(conv)
# Display convolution result
plt.figure(figsize=(5,5))
plt.imshow(tf.squeeze(relu), cmap='gray')
plt.axis('off')
plt.title(f'After ReLU Activation: {image_path}')
plt.show()
else:
print(f"File not found: {image_path}")