import numpy as np import sys from Convolution1 import Convolution2D import os os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID" # see issue #152 os.environ["CUDA_VISIBLE_DEVICES"] = "" def Dilate_conv2D(input_array,kernel,kernel_b=None,dilate_rate=(1,1),padding='same'): img_h, img_w, n_channels = input_array.shape feature_num, k_h, k_w, k_n_channels = kernel.shape d_h,d_w = dilate_rate if n_channels != k_n_channels: print('n_channels_error') sys.exit() if len(input_array.shape) != len(kernel.shape) - 1: print ('length_error') sys.exit() pad_h = d_h-1 pad_w = d_w-1 if padding == 'same': new_kh = (pad_h)*(k_h+1)+k_h new_kw = (pad_w)*(k_w+1)+k_w y_range = range(pad_h,new_kh,d_h) x_range = range(pad_w,new_kw,d_w) new_kernel = np.zeros(shape=(feature_num,new_kh,new_kw,k_n_channels),dtype='float32') for j in y_range: for i in x_range: new_kernel[:,j,i,:] = kernel[:,j//d_h,i//d_w,:] return Convolution2D(input_array,kenel=new_kernel,kernel_b=None,stride=(1,1),padding=padding) else: new_kh = pad_h*(k_h-1)+k_h new_kw = pad_w*(k_w-1)+k_w y_range=range(0,new_kh,d_h) x_range=range(0,new_kw,d_w) new_kernel = np.zeros(shape=(feature_num,new_kh,new_kw,k_n_channels),dtype='float32') for j in y_range: for i in x_range: new_kernel[:,j,i,:] = kernel[:,j//d_h,i//d_w,:] return Convolution2D(input_array,kenel=new_kernel,kernel_b=None,stride=(1,1),padding=padding) if __name__ == '__main__': input_data = np.random.randint(0,10,(12,12,1)) kernel = np.random.randint(0,10,(2,3,3,1)) d_h,d_w = 3,3 result = Dilate_conv2D(input_data,kernel,kernel_b=None,dilate_rate=(d_h,d_w),padding='same') print(result) import tensorflow as tf data = np.asarray(input_data,dtype='float32') kernel = np.asarray(kernel,dtype='float32') data = np.expand_dims(data,axis=0) kernel = np.transpose(kernel,(1,2,3,0)) const_input = tf.constant(data, tf.float32) const_weights = tf.constant(kernel, tf.float32) y2 = tf.nn.atrous_conv2d(data, kernel,rate=3, padding="SAME") sess = tf.Session() print(sess.run(y2).squeeze(axis=0)) print(result.shape) print(sess.run(y2).shape)
Dilate_conv2D
最新推荐文章于 2022-11-22 17:05:23 发布