python Tensorflow 实现图像的卷积处理

1.convolution.py

import numpy as np
from sklearn.datasets import  load_sample_images
import tensorflow as tf
import matplotlib.pyplot as plt

dataset = np.array(load_sample_images().images, dtype=np.float32)
batch_size, height, width, channels = dataset.shape

print(batch_size, height, width, channels)

print(type(dataset))

filters_test = tf.placeholder(tf.float32, shape=(15, 15, channels, 2))



X = tf.placeholder(tf.float32, shape=(None, height, width, channels))

dataset = dataset/255

convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 2, 2, 1], padding='SAME')

with tf.Session() as sess:
    out = {}
    filters = np.zeros(shape=(15, 15, channels, 2))
    for i in range(2):
        if i == 0:
            filters[7, :, :, 1] = 1
        elif i == 1:
            filters[:, 7, :, 1] = 1
        output = sess.run(convolution, feed_dict={X: dataset, filters_test: filters})

        print(output)
        out['output'+str(i)] = output

print(output)




plt.imshow(dataset[0])
plt.show()

for i in out:
    # print(out[i])
    # max_value = max(out[i][0].reshape(-1, 1))
    # print(max_value)
    # out[i] = out[i]/max_value

    plt.title(i)
    plt.imshow(255*out[i][0, :, :, 1], cmap='bone')
    plt.show()

猜你喜欢

转载自www.cnblogs.com/CK85/p/10282721.html