def __init__(self, rng, input, input_shape, filter_shape, pool_shape=(2, 2)):
"""
???????????????????????
:param input: ?????
:param input_shape: ????????(batch_size, image_channel, image_weight, image_height)
:param filter_shape: ???????(filter_count, filter_channel, filter_weight, filter_height)
:param pool_shape: ??????
:return:
"""
#
assert input_shape[1] == filter_shape[1]
self.input = input
self.input_shape = input_shape
self.filter_shape = filter_shape
self.pool_shape = pool_shape
# ?????????
n_in = numpy.prod(input_shape[1:])
n_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // numpy.prod(pool_shape))
weight_max = numpy.sqrt(6. / (n_in + n_out))
self.w = theano.shared(
numpy.asarray(
rng.uniform(low=-weight_max, high=weight_max, size=filter_shape),
dtype=theano.config.floatX
),
borrow=True
)
self.b = theano.shared(numpy.zeros((filter_shape[0],), dtype=theano.config.floatX), borrow=True)
self.params = [self.w, self.b]
# calculate the output
self.conv_out = conv2d(
input=self.input,
filters=self.w,
filter_shape=self.filter_shape,
image_shape=self.input_shape
)
self.pool_out = pool_2d(
input=self.conv_out,
ds=pool_shape,
ignore_border=True
)
self.output = T.tanh(self.pool_out + self.b.dimshuffle('x', 0, 'x', 'x'))
python类conv2d()的实例源码
2d_convolutional_net.py 文件源码
项目:GT-Deep-Learning-for-Sign-Language-Recognition
作者: payamsiyari
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def model(X, w, w2, w3, w35, w4, p_drop_conv, p_drop_hidden):
l1a = rectify(conv2d(X, w, border_mode='full'))
#print "l1a",l1a.type
#print "l1a",l1a.shape.eval()
l1 = max_pool_2d(l1a, (2, 2))
#print "l1",l1.get_value().shape
#l1 = dropout(l1, p_drop_conv)
l2a = rectify(conv2d(l1, w2))
#print "l2a",l2a.get_value().shape
l2 = max_pool_2d(l2a, (2, 2))
#print "l2",l2.get_value().shape
#l2 = dropout(l2, p_drop_conv)
l3 = rectify(conv2d(l2, w3))
#print "l3",l3.get_value().shape
#l3 = max_pool_2d(l3a, (1, 1))
#l3 = dropout(l3, p_drop_conv)
l35a = rectify(conv2d(l3, w35))
#print "l35a",l35a.get_value().shape
l35b = max_pool_2d(l35a, (2, 2))
#print "l35b",l35b.get_value().shape
l35 = T.flatten(l35b, outdim=2)
#print "l35",l35.get_value().shape
#l35 = dropout(l35, p_drop_conv)
l4 = rectify(T.dot(l35, w4))
#print "l4",l4.get_value().shape
#l4 = dropout(l4, p_drop_hidden)
pyx = softmax(T.dot(l4, w_o))
return l1, l2, l3, l35, l4, pyx
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2),
stride=(1, 1)):
"""
Allocate a LeNetConvPoolLayer with shared variable internal parameters.
"""
assert image_shape[1] == filter_shape[1]
self.input = input
fan_in = np.prod(filter_shape[1:])
fan_out = (filter_shape[0] * np.prod(filter_shape[2:]) /
np.prod(poolsize))
W_bound = np.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(
np.asarray(
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
dtype=theano.config.floatX
),
borrow=True
)
b_values = np.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
conv_out = conv.conv2d(
input=input,
filters=self.W,
filter_shape=filter_shape,
image_shape=image_shape,
subsample=stride
)
pooled_out = downsample.max_pool_2d(
input=conv_out,
ds=poolsize,
ignore_border=True
)
self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
assert image_shape[1] == filter_shape[1]
self.input = input
# there are "num input feature maps * filter height * filter width"
# inputs to each hidden unit
fan_in = numpy.prod(filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# "num output feature maps * filter height * filter width" /
# pooling size
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
numpy.prod(poolsize))
# initialize weights with random weights
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(numpy.asarray(
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
dtype=theano.config.floatX),
borrow=True)
# the bias is a 1D tensor -- one bias per output feature map
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
# convolve input feature maps with filters
conv_out = conv.conv2d(input=input, filters=self.W,
filter_shape=filter_shape, image_shape=image_shape)
# downsample each feature map individually, using maxpooling
pooled_out = pool.pool_2d(input=conv_out, ds=poolsize, ignore_border=True)
self.output = T.maximum(0.0, pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
# store parameters of this layer
self.params = [self.W, self.b]
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
assert image_shape[1] == filter_shape[1]
self.input = input
# there are "num input feature maps * filter height * filter width"
# inputs to each hidden unit
fan_in = numpy.prod(filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# "num output feature maps * filter height * filter width" /
# pooling size
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
numpy.prod(poolsize))
# initialize weights with random weights
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(numpy.asarray(
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
dtype=theano.config.floatX),
borrow=True)
# the bias is a 1D tensor -- one bias per output feature map
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
# convolve input feature maps with filters
conv_out = conv.conv2d(input=input, filters=self.W,
filter_shape=filter_shape, image_shape=image_shape)
# downsample each feature map individually, using maxpooling
pooled_out = pool.pool_2d(input=conv_out, ds=poolsize, ignore_border=True)
self.output = T.maximum(0.0, pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
# store parameters of this layer
self.params = [self.W, self.b]
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)):
assert image_shape[1] == filter_shape[1]
self.input = input
# there are "num input feature maps * filter height * filter width"
# inputs to each hidden unit
fan_in = numpy.prod(filter_shape[1:])
# each unit in the lower layer receives a gradient from:
# "num output feature maps * filter height * filter width" /
# pooling size
fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
numpy.prod(poolsize))
# initialize weights with random weights
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
self.W = theano.shared(numpy.asarray(
rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
dtype=theano.config.floatX),
borrow=True)
# the bias is a 1D tensor -- one bias per output feature map
b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
self.b = theano.shared(value=b_values, borrow=True)
# convolve input feature maps with filters
conv_out = conv.conv2d(input=input, filters=self.W,
filter_shape=filter_shape, image_shape=image_shape)
# downsample each feature map individually, using maxpooling
pooled_out = downsample.max_pool_2d(input=conv_out,
ds=poolsize, ignore_border=True)
self.output = T.maximum(0.0, pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
# store parameters of this layer
self.params = [self.W, self.b]
def convolutionalLayer(inpu, feature_map, batch, length, window, dim, prefix, params, names):
down = window / 2
up = window - down - 1
zodown = T.zeros((batch, 1, down, dim), dtype=theano.config.floatX)
zoup = T.zeros((batch, 1, up, dim), dtype=theano.config.floatX)
inps = T.cast(T.concatenate([zoup, inpu, zodown], axis=2), dtype=theano.config.floatX)
fan_in = window * dim
fan_out = feature_map * window * dim / length #(length - window + 1)
filter_shape = (feature_map, 1, window, dim)
image_shape = (batch, 1, length + down + up, dim)
#if non_linear=="none" or non_linear=="relu":
# conv_W = theano.shared(0.2 * numpy.random.uniform(low=-1.0,high=1.0,\
# size=filter_shape).astype(theano.config.floatX))
#else:
# W_bound = numpy.sqrt(6. / (fan_in + fan_out))
# conv_W = theano.shared(numpy.random.uniform(low=-W_bound,high=W_bound,\
# size=filter_shape).astype(theano.config.floatX))
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
conv_W = theano.shared(numpy.random.uniform(low=-W_bound,high=W_bound,\
size=filter_shape).astype(theano.config.floatX))
conv_b = theano.shared(numpy.zeros(filter_shape[0], dtype=theano.config.floatX))
# bundle
params += [ conv_W, conv_b ]
names += [ prefix + '_convL_W_' + str(window), prefix + '_convL_b_' + str(window) ]
conv_out = conv.conv2d(input=inps, filters=conv_W, filter_shape=filter_shape, image_shape=image_shape)
conv_out = T.tanh(conv_out + conv_b.dimshuffle('x', 0, 'x', 'x'))
return conv_out.dimshuffle(0,2,1,3).flatten(3)
def LeNetConvPoolLayer(inps, feature_map, batch, length, window, dim, prefix, params, names):
fan_in = window * dim
fan_out = feature_map * window * dim / (length - window + 1)
filter_shape = (feature_map, 1, window, dim)
image_shape = (batch, 1, length, dim)
pool_size = (length - window + 1, 1)
#if non_linear=="none" or non_linear=="relu":
# conv_W = theano.shared(0.2 * numpy.random.uniform(low=-1.0,high=1.0,\
# size=filter_shape).astype(theano.config.floatX))
#else:
# W_bound = numpy.sqrt(6. / (fan_in + fan_out))
# conv_W = theano.shared(numpy.random.uniform(low=-W_bound,high=W_bound,\
# size=filter_shape).astype(theano.config.floatX))
W_bound = numpy.sqrt(6. / (fan_in + fan_out))
conv_W = theano.shared(numpy.random.uniform(low=-W_bound,high=W_bound,\
size=filter_shape).astype(theano.config.floatX))
conv_b = theano.shared(numpy.zeros(filter_shape[0], dtype=theano.config.floatX))
# bundle
params += [ conv_W, conv_b ]
names += [ prefix + '_conv_W_' + str(window), prefix + '_conv_b_' + str(window) ]
conv_out = conv.conv2d(input=inps, filters=conv_W, filter_shape=filter_shape, image_shape=image_shape)
conv_out_act = T.tanh(conv_out + conv_b.dimshuffle('x', 0, 'x', 'x'))
conv_output = downsample.max_pool_2d(input=conv_out_act, ds=pool_size, ignore_border=True)
return conv_output.flatten(2)
def convolve1d_4D_conv2d(input, W, mode='full'):
conv_out, _ = theano.scan(fn=lambda i: conv2d(input[:,:,:,i:i+1], W[:,:,:,i:i+1], border_mode=mode),
outputs_info=None,
sequences=[T.arange(0, W.shape[3])])
conv_out = conv_out.flatten(ndim=4).dimshuffle(1,2,3,0)
return conv_out
def convolve1d_4D_conv2d_image(input, W, mode='full'):
return conv2d(input, W, border_mode='valid')
def output_func(self, input):
return conv.conv2d(input, self.W, border_mode='valid',
filter_shape=self.filter_shape,
image_shape=self.input_shape)
# def Conv2dMaxPool(rng, filter_shape, activation):
# conv = Conv2dLayer(rng, filter_shape)
# nonlinearity = NonLinearityLayer(activation=activation)
# pooling = MaxPoolLayer()
# layer = FeedForwardNet(layers=[])
# return layer
def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
self.inpt = inpt.reshape(self.image_shape)
conv_out = conv.conv2d(
input=self.inpt, filters=self.w, filter_shape=self.filter_shape,
image_shape=self.image_shape)
pooled_out = downsample.max_pool_2d(
input=conv_out, ds=self.poolsize, ignore_border=True)
self.output = self.activation_fn(
pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))
self.output_dropout = self.output # no dropout in the convolutional layers
def encoder(tparams, layer0_input, filter_shape, pool_size,
prefix='cnn_encoder'):
""" filter_shape: (number of filters, num input feature maps, filter height,
filter width)
image_shape: (batch_size, num input feature maps, image height, image width)
"""
conv_out = conv.conv2d(input=layer0_input, filters=tparams[_p(prefix,'W')],
filter_shape=filter_shape)
conv_out_tanh = tensor.tanh(conv_out + tparams[_p(prefix,'b')].dimshuffle('x', 0, 'x', 'x'))
output = pool.pool_2d(input=conv_out_tanh, ds=pool_size, ignore_border=True)
return output.flatten(2)
def convolve1d_4D_conv2d(input, W, mode='full'):
conv_out, _ = theano.scan(fn=lambda i: conv2d(input[:,:,:,i:i+1], W[:,:,:,i:i+1], border_mode=mode),
outputs_info=None,
sequences=[T.arange(0, W.shape[3])])
conv_out = conv_out.flatten(ndim=4).dimshuffle(1,2,3,0)
return conv_out
def convolve1d_4D_conv2d_image(input, W, mode='full'):
return conv2d(input, W, border_mode='valid')
def output_func(self, input):
return conv.conv2d(input, self.W, border_mode='valid',
filter_shape=self.filter_shape,
image_shape=self.input_shape)
# def Conv2dMaxPool(rng, filter_shape, activation):
# conv = Conv2dLayer(rng, filter_shape)
# nonlinearity = NonLinearityLayer(activation=activation)
# pooling = MaxPoolLayer()
# layer = FeedForwardNet(layers=[])
# return layer
def __call__(self, input):
s, f = self.input_shape, self.filter_shape
hzeros = T.basic.zeros((s[0], s[1], (f[2]-1)//2, s[3]), dtype=theano.config.floatX)
vzeros = T.basic.zeros((s[0], s[1], s[2] + (f[2]-1), (f[3]-1)//2), dtype=theano.config.floatX)
input = T.concatenate([hzeros, input, hzeros], axis=2)
input = T.concatenate([vzeros, input, vzeros], axis=3)
input = conv.conv2d(
input=input,
filters=self.W,
border_mode='valid')
return input + self.b.dimshuffle('x', 0, 'x', 'x')
def inv(self, output):
output = output - self.b.dimshuffle('x', 0, 'x', 'x')
s, f = self.output_shape, self.filter_shape
hzeros = T.basic.zeros((s[0], s[1], (f[2]-1)//2, s[3]), dtype=theano.config.floatX)
vzeros = T.basic.zeros((s[0], s[1], s[2] + (f[2]-1), (f[3]-1)//2), dtype=theano.config.floatX)
output = T.concatenate([hzeros, output, hzeros], axis=2)
output = T.concatenate([vzeros, output, vzeros], axis=3)
output = conv.conv2d(
input=output.dimshuffle(0,1,2,3),
filters=self.W.dimshuffle(1,0,2,3)[:,:,::-1,::-1],
border_mode='valid')
return output
def __call__(self, input):
s, f = self.input_shape, self.filter_shape
zeros = T.basic.zeros((s[0], s[1], (f[2]-1)//2), dtype=theano.config.floatX)
input = T.concatenate([zeros, input, zeros], axis=2)
input = conv.conv2d(
input=input.dimshuffle(0,1,2,'x'),
filters=self.W.dimshuffle(0,1,2,'x'),
border_mode='valid')[:,:,:,0]
return input + self.b.dimshuffle('x', 0, 'x')
def inv(self, output):
output = output - self.b.dimshuffle('x', 0, 'x')
s, f = self.output_shape, self.filter_shape
zeros = T.basic.zeros((s[0], s[1], (f[2]-1)//2), dtype=theano.config.floatX)
output = T.concatenate([zeros, output, zeros], axis=2)
output = conv.conv2d(
input=output.dimshuffle(0,1,2,'x'),
filters=self.W.dimshuffle(1,0,2,'x')[:,:,::-1],
border_mode='valid')[:,:,:,0]
return output