input.jpeg
import numpy from skimage import io import skimage.data import matplotlib.pyplot as plt import numpy import sys def conv(img, conv_filter): if len(img.shape) > 2 or len(conv_filter.shape) > 3: if img.shape[-1] != conv_filter.shape[-1]: print("Error: Number of channels in both image and filter must match.") sys.exit() if conv_filter.shape[1] != conv_filter.shape[2]: print('Error: Filter must be a square matrix.') sys.exit() if conv_filter.shape[1]%2==0: print('Error: Filter must have an odd size.') sys.exit() feature_maps = numpy.zeros((img.shape[0]-conv_filter.shape[1]+1, img.shape[1]-conv_filter.shape[1]+1, conv_filter.shape[0])) for filter_num in range(conv_filter.shape[0]): print("Filter ", filter_num + 1) curr_filter = conv_filter[filter_num, :] if len(curr_filter.shape) > 2: conv_map = conv_(img[:, :, 0], curr_filter[:, :, 0]) for ch_num in range(1, curr_filter.shape[-1]): conv_map = conv_map + conv_(img[:, :, ch_num],curr_filter[:, :, ch_num]) else: conv_map = conv_(img, curr_filter) feature_maps[:, :, filter_num] = conv_map return feature_maps def conv_(img, conv_filter): filter_size = conv_filter.shape[0] result = numpy.zeros((img.shape)) for r in numpy.uint16(numpy.arange(filter_size/2, img.shape[0]-filter_size/2-2)): for c in numpy.uint16(numpy.arange(filter_size/2, img.shape[1]-filter_size/2-2)): curr_region = img[r:r+filter_size, c:c+filter_size] curr_result = curr_region * conv_filter conv_sum = numpy.sum(curr_result) result[r, c] = conv_sum final_result = result[numpy.uint16(filter_size/2):result.shape[0]-numpy.uint16(filter_size/2), numpy.uint16(filter_size/2):result.shape[1]-numpy.uint16(filter_size/2)] return final_result def relu(feature_map): relu_out = numpy.zeros(feature_map.shape) for map_num in range(feature_map.shape[-1]): for r in numpy.arange(0, feature_map.shape[0]): for c in numpy.arange(0, feature_map.shape[1]): relu_out[r, c, map_num] = numpy.max(feature_map[r, c, map_num], 0) return relu_out def pooling(feature_map, size, stride): pool_out = numpy.zeros((numpy.uint16((feature_map.shape[0] - size + 1) / stride), numpy.uint16((feature_map.shape[1] - size+1) / stride), feature_map.shape[-1])) for map_num in range(feature_map.shape[-1]): r2 = 0 for r in numpy.arange(0, feature_map.shape[0]-size-1, stride): c2 = 0 for c in numpy.arange(0, feature_map.shape[1]-size-1, stride): pool_out[r2, c2, map_num] = numpy.max(feature_map[r:r+size, c:c+size]) c2 = c2 + 1 r2 = r2 + 1 return pool_out img0 = io.imread('input.jpeg') img = skimage.color.rgb2gray(img0) # First conv layer l1_filter = numpy.zeros((2, 3, 3)) l1_filter[0, :, :] = numpy.array([[[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]]) l1_filter[1, :, :] = numpy.array([[[1, 1, 1], [0, 0, 0], [-1, -1, -1]]]) print("**Working with conv layer 1**") l1_feature_map = conv(img, l1_filter) print("**ReLU**") l1_feature_map_relu = relu(l1_feature_map) print("**Pooling**") l1_feature_map_relu_pool = pooling(l1_feature_map_relu, 2, 2) print("**End of conv layer 1**n") # Second conv layer l2_filter = numpy.random.rand(3, 5, 5, l1_feature_map_relu_pool.shape[-1]) print("**Working with conv layer 2**") l2_feature_map = conv(l1_feature_map_relu_pool, l2_filter) print("**ReLU**") l2_feature_map_relu = relu(l2_feature_map) print("**Pooling**") l2_feature_map_relu_pool = pooling(l2_feature_map_relu, 2, 2) print("**End of conv layer 2**n") # Third conv layer l3_filter = numpy.random.rand(1, 7, 7, l2_feature_map_relu_pool.shape[-1]) print("**Working with conv layer 3**") l3_feature_map = conv(l2_feature_map_relu_pool, l3_filter) print("**ReLU**") l3_feature_map_relu = relu(l3_feature_map) print("**Pooling**") l3_feature_map_relu_pool = pooling(l3_feature_map_relu, 2, 2) print("**End of conv layer 3**n") plt.figure(1) plt.subplot(4,2,1) plt.imshow(img0) plt.subplot(4,2,2) plt.imshow(img,cmap='gray') plt.subplot(4,2,3) plt.imshow(l1_feature_map[:,:,0],cmap='gray') plt.subplot(4,2,4) plt.imshow(l1_feature_map[:,:,1],cmap='gray') plt.subplot(4,2,5) plt.imshow(l1_feature_map_relu[:,:,0],cmap='gray') plt.subplot(4,2,6) plt.imshow(l1_feature_map_relu[:,:,1],cmap='gray') plt.subplot(4,2,7) plt.imshow(l1_feature_map_relu_pool[:,:,0],cmap='gray') plt.subplot(4,2,8) plt.imshow(l1_feature_map_relu_pool[:,:,1],cmap='gray') plt.figure(2) plt.subplot(3,2,1) plt.imshow(l2_feature_map[:,:,0],cmap='gray') plt.subplot(3,2,2) plt.imshow(l2_feature_map[:,:,1],cmap='gray') plt.subplot(3,2,3) plt.imshow(l2_feature_map_relu[:,:,0],cmap='gray') plt.subplot(3,2,4) plt.imshow(l2_feature_map_relu[:,:,1],cmap='gray') plt.subplot(3,2,5) plt.imshow(l2_feature_map_relu_pool[:,:,0],cmap='gray') plt.subplot(3,2,6) plt.imshow(l2_feature_map_relu_pool[:,:,1],cmap='gray') plt.figure(3) plt.subplot(3,1,1) plt.imshow(l3_feature_map,cmap='gray') plt.subplot(3,1,2) plt.imshow(l3_feature_map_relu,cmap='gray') plt.subplot(3,1,3) plt.imshow(l3_feature_map_relu_pool,cmap='gray') plt.show()
First conv layer
Second conv layer
Third conv layer
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)