def colAC(idx, div, A, X): i1, i2 = idx // div, idx % div want = A[0, i1, :, i2:i2 + 1].T have = X[0, i1, :, i2:i2 + 1].T wantAC = TC.conv2d(want, want[:, ::-1], border_mode='full') haveAC = TC.conv2d(have, have[:, ::-1], border_mode='full') return ((wantAC - haveAC)**2).sum()
def test_fail(self): # Test that conv2d fails for dimensions other than 2 or 3. with pytest.raises(Exception): conv.conv2d(T.dtensor4(), T.dtensor3()) with pytest.raises(Exception): conv.conv2d(T.dtensor3(), T.dvector())
def test_bug_josh_reported(self): # Test refers to a bug reported by Josh, when due to a bad merge these # few lines of code failed. See # http://groups.google.com/group/theano-dev/browse_thread/thread/8856e7ca5035eecb m1 = theano.tensor.matrix() m2 = theano.tensor.matrix() conv.conv2d(m1, m2)
def __init__(self, input, depth, length, width, in_l, in_w, first_layer=False): self.W = theano.shared(np.random.uniform(low=-1./np.sqrt(width*length*depth), high=1./np.sqrt(width*length*depth), size=(depth,length,width)).astype(dtype=theano.config.floatX)) if first_layer: self.output = conv.conv2d(input[0], self.W[0], image_shape=(in_l, in_w), filter_shape=(length, width)) else: self.output = conv.conv2d(input[0], self.W[0], image_shape=(in_l, in_w), filter_shape=(length, width), border_mode='full') for i in range(1, depth): if first_layer: self.output = self.output + conv.conv2d(input[i], self.W[i], image_shape=(in_l, in_w), filter_shape=(length, width)) else: self.output = self.output + conv.conv2d(input[i], self.W[i], image_shape=(in_l, in_w), filter_shape=(length, width), border_mode='full')
def test_fail(self): """ Test that conv2d fails for dimensions other than 2 or 3. """ try: conv.conv2d(T.dtensor4(), T.dtensor3()) self.fail() except: pass try: conv.conv2d(T.dtensor3(), T.dvector()) self.fail() except: pass
def __init__(self, input, n_in,n_out,filter_size,activation=T.tanh): delta = 0.01 self.input = input self.n_in = n_in self.n_out = n_out n_filters = n_out self.name = 'C1D' + str(n_filters) + 'x'+str(filter_size) self.W = theano.shared( (np.random.uniform(-1,1,(n_filters, filter_size,n_in)) * delta).astype(T.config.floatX)) self.b = theano.shared( (np.zeros(n_out,))) self.params = [self.W, self.b] pad = self.W.shape[1]/2 zeros = T.zeros((input.shape[0] + 2*pad, input.shape[1])) xPad = T.set_subtensor(zeros[pad:input.shape[0]+pad], input) out = conv.conv2d( input=xPad, filters=self.W) out = out[:,:,0].dimshuffle(1,0) out = activation(self.b + out) self.output = out self.updates = None
def course_grain(excitation_grid, cg_factor): """ excitation_grid should be list of 2d arrays in time order where each 2d array is the animation state of the system at time t. The excitation_grid of a system can be obtained using b = animator.Visual('file_name'), selecting your desired animation range and then exporting excitation_grid = b.animation_data. cg_factor is the unitless factor corresponding to the number of small original cells along each side of the new course grained cell. e.g. If a 200x200 array is processed with cg_factor = 5, the new course grained array will be shape 40x40 where each new cell corresponds to the net excitations from 5x5 sections of the original array.""" exc = np.array(excitation_grid).astype( 'float') #Asserts data type of imported excitation_grid filt = np.ones((cg_factor, cg_factor), dtype='float' ) #Square matrix of ones in shape of course_grained cells. norm = cg_factor**2 #Number of original cells in each course grained cell a = T.dtensor3( 'a' ) #Theano requires us to specify data types. dtensor3 is a 3d tensor of float64's b = T.dmatrix('b') #Matrix of float64's z = conv2d(a, b, subsample=( cg_factor, cg_factor)) / norm #This specifies the function to process. # Convolution with subsample step length results in course grained matrices f = function( [a, b], z ) #Theano function definition where inputs ([a,b]) and outputs (z) are specified return f(exc, filt) #Returns function with excitation_grid and filter as output
def filt_app(y): filt = T.alloc(1., filt_len, 1) / filt_len #filter height = filt_len, filter width = 1 temp = conv2d(y, filt, border_mode='full') temp = temp[:, filt_len / 2:, :] temp = temp[:, :rec_length, :] return temp
def __dealWithOneDoc(self, DocSentenceCount0, oneDocSentenceCount1, docs, oneDocSentenceWordCount, docW, docB, sentenceW, sentenceB): # t = T.and_((shareRandge < oneDocSentenceCount1 + 1), (shareRandge >= DocSentenceCount0)).nonzero() oneDocSentenceWordCount = oneDocSentenceWordCount[ DocSentenceCount0:oneDocSentenceCount1 + 1] sentenceResults, _ = theano.scan( fn=self.__dealWithSentence, non_sequences=[docs, sentenceW, sentenceB], sequences=[dict(input=oneDocSentenceWordCount, taps=[-1, -0])], strict=True) # p = printing.Print('docPool') # docPool = p(docPool) # p = printing.Print('sentenceResults') # sentenceResults = p(sentenceResults) # p = printing.Print('doc_out') # doc_out = p(doc_out) doc_out = conv.conv2d(input=sentenceResults, filters=docW) docPool = downsample.max_pool_2d(doc_out, (self.__MAXDIM, 1), mode="average_exc_pad", ignore_border=False) docOutput = T.tanh(docPool + docB.dimshuffle([0, 'x', 'x'])) doc_embedding = docOutput.flatten(1) return doc_embedding
def __dealWithOneDoc(self, DocSentenceCount0, oneDocSentenceCount1, \ docs, corpusPos, oneDocSentenceWordCount, docW, docB, sentenceW, sentenceB, posW, posB): # t = T.and_((shareRandge < oneDocSentenceCount1 + 1), (shareRandge >= DocSentenceCount0)).nonzero() oneDocSentenceWordCount = oneDocSentenceWordCount[DocSentenceCount0:oneDocSentenceCount1 + 1] sentenceResults0, _ = theano.scan(fn=self.__dealWithSentence, non_sequences=[docs, sentenceW, sentenceB], sequences=[dict(input=oneDocSentenceWordCount, taps=[-1, -0])], strict=True) sentenceResults1, _ = theano.scan(fn=self.__dealWithSentence, non_sequences=[corpusPos, posW, posB], sequences=[dict(input=oneDocSentenceWordCount, taps=[-1, -0])], strict=True) sentenceResults = T.concatenate([sentenceResults0, sentenceResults1], axis=1) # p = printing.Print('docPool') # docPool = p(docPool) # p = printing.Print('sentenceResults') # sentenceResults = p(sentenceResults) # p = printing.Print('doc_out') # doc_out = p(doc_out) doc_out = conv.conv2d(input=sentenceResults, filters=docW) docPool = downsample.max_pool_2d(doc_out, (self.__MAXDIM, 1), mode=self.__pooling_mode, ignore_border=False) docOutput = T.tanh(docPool + docB.dimshuffle([0, 'x', 'x'])) doc_embedding = docOutput.flatten(1) return doc_embedding
def filter_spike_train(n, S, taus): """ Helper function to filter the spike train """ filt = T.shape_padright(filt_fn(taus[n]), n_ones=1) filtered_S = conv2d(T.shape_padright(S[:, n], n_ones=1), filt, border_mode='full') return filtered_S[0, :, 0]
def connect(self, *layers): assert len(layers) == 1 self.intputShape = layers[0].get_outputShape() self.set_inputTensor( layers[0].get_outputTensor() ) filter = np.ones( (1, 1) ) outputTensor = self.coef * conv2d( self.get_inputTensor(), filters=filter, subsample=self.subSampleShape ) + self.bias self.set_outputTensor( outputTensor )
def filter_spike_train(n,S,taus): """ Helper function to filter the spike train """ filt = T.shape_padright(filt_fn(taus[n]), n_ones=1) filtered_S = conv2d(T.shape_padright(S[:,n], n_ones=1), filt, border_mode='full') return filtered_S[0,:,0]
def __dealWithSentence(self, wc0, wc1, docs, W, B): sentence = docs[wc0:wc1] sentence_out = conv.conv2d(input=sentence, filters=W) sentence_pool = downsample.max_pool_2d(sentence_out, (self.__MAXDIM, 1), mode=self.__pooling_mode, ignore_border=False) sentence_output = T.tanh(sentence_pool + B.dimshuffle([0, 'x', 'x'])) sentence_embedding = sentence_output.flatten(1) return sentence_embedding
def __dealWithSentence(self, sentenceWordCount0, sentenceWordCount1, docs, sentenceW, sentenceB): # t = T.and_((shareRandge < sentenceWordCount1), (shareRandge >= sentenceWordCount0)).nonzero() sentence = docs[sentenceWordCount0:sentenceWordCount1] sentence_out = conv.conv2d(input=sentence, filters=sentenceW) sentence_pool = downsample.max_pool_2d(sentence_out, (self.__MAXDIM, 1), mode=self.__pooling_mode, ignore_border=False) sentence_output = T.tanh(sentence_pool + sentenceB.dimshuffle([0, 'x', 'x'])) sentence_embedding = sentence_output.flatten(1) return sentence_embedding
def __dealWithSentence(self, sentenceWordCount0, sentenceWordCount1, docs, sentenceW, sentenceB): # t = T.and_((shareRandge < sentenceWordCount1), (shareRandge >= sentenceWordCount0)).nonzero() sentence = docs[sentenceWordCount0:sentenceWordCount1] sentence_out = conv.conv2d(input=sentence, filters=sentenceW) sentence_pool = downsample.max_pool_2d(sentence_out, (self.__MAXDIM, 1), mode="average_exc_pad", ignore_border=False) sentence_output = T.tanh(sentence_pool + sentenceB.dimshuffle([0, 'x', 'x'])) sentence_embedding = sentence_output.flatten(1) return sentence_embedding
def connect(self, *layers): assert len(layers) == 1 self.intputShape = layers[0].get_outputShape() self.set_inputTensor(layers[0].get_outputTensor()) filter = np.ones((1, 1)) outputTensor = self.coef * conv2d( self.get_inputTensor(), filters=filter, subsample=self.subSampleShape) + self.bias self.set_outputTensor(outputTensor)
def __init__(self, input, depth, length, width, in_l, in_w, first_layer=False): self.W = theano.shared( np.random.uniform(low=-1. / np.sqrt(width * length * depth), high=1. / np.sqrt(width * length * depth), size=(depth, length, width)).astype(dtype=theano.config.floatX)) if first_layer: self.output = conv.conv2d(input[0], self.W[0], image_shape=(in_l, in_w), filter_shape=(length, width)) else: self.output = conv.conv2d(input[0], self.W[0], image_shape=(in_l, in_w), filter_shape=(length, width), border_mode='full') for i in range(1, depth): if first_layer: self.output = self.output + conv.conv2d( input[i], self.W[i], image_shape=(in_l, in_w), filter_shape=(length, width)) else: self.output = self.output + conv.conv2d( input[i], self.W[i], image_shape=(in_l, in_w), filter_shape=(length, width), border_mode='full')
def conv_offset(inp,filt,amplitude=1.): # offset = tt.cast(offset,'int64') amplitude = tt.cast(amplitude,'float64') amplitude = tt.clip(amplitude,1e-12,1e9) # Limit to prevent NANs zero = tt.zeros_like(inp) a0rp = tt.concatenate((inp,zero,),0) * amplitude # a0rp = tt.set_subtensor(a0,0.) * amplitude a0rp3d = tt.alloc(0.,1,a0rp.shape[0],1 ) a0rp = tt.set_subtensor(a0rp3d[0,:,0],a0rp) filt3d = tt.alloc(0.,1,filt.shape[0],1 ) filt = tt.set_subtensor(filt3d[0,:,0],filt) return tt_conv.conv2d(a0rp,filt,None,None,border_mode='full').flatten()
def connect(self, *layers): self.set_preLayers(layers) self.init_filters() self.outputShape = conv2D_shape( layers[0].get_outputShape(), self.get_filterShape() ) self.init_bias() outputTensor = self.get_bias() inputTensors = [] #one filter for every pre-layer for i, filter in zip( xrange( self.get_numOfFilters() ), self.get_filters() ): outputTensor = outputTensor + conv2d( layers[i].get_outputTensor(), filters=filter ) inputTensors.append( layers[i].get_outputTensor() ) self.set_outputTensor( outputTensor ) self.set_inputTensors( inputTensors )
def test_conv(X, music_shape, filter_shape, subsample): """ X: Shared variable """ W = T.tensor3('W') output = conv2d(X, W, image_shape=music_shape, filter_shape=filter_shape, subsample=subsample) f = theano.function([W], output) ' now test: ' try: W = np.random.rand(*filter_shape).astype(np.float32) f(W) return 1 except Exception, err: print 'ERROR: %s\n' % str(err) return 0
def main(): # Get alpha, psi and put in shared variable # psi is M x K x D (axis-1: x, y and z components of AEB) # alpha is N x D (axis-0:time/frame, axis-1: various AEB) #alpha_np,psi_np = fio.toyExample_medium_3d_multicomp() alpha_np = [[0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0]] psi_np = [[[1, 2, 3], [4, 5, 6]], [[3, 2, 1], [6, 5, 4]]] # alpha = th.shared(value=alpha_np,name='alpha',borrow=True) # psi = th.shared(value=psi_np,name='psi',borrow=True) alpha = T.ftensor3('alpha') psi = T.fmatrix('psi') # Build a convolution expression convexpr = conv2d(psi_, alpha_, border_mode='full') consig, upd = th.scan(fn=convexpr, sequences=[psi, alpha]) lossf = function([psi, alpha], outputs=convexpr) print lossf(psi_np, alpha_np)
def connect(self, *layers): self.set_preLayers(layers) self.init_filters() self.outputShape = conv2D_shape(layers[0].get_outputShape(), self.get_filterShape()) self.init_bias() outputTensor = self.get_bias() inputTensors = [] #one filter for every pre-layer for i, filter in zip(xrange(self.get_numOfFilters()), self.get_filters()): outputTensor = outputTensor + conv2d(layers[i].get_outputTensor(), filters=filter) inputTensors.append(layers[i].get_outputTensor()) self.set_outputTensor(outputTensor) self.set_inputTensors(inputTensors)
def get_output(self, inputs): """ Get outputs of encoder layer. Return all of the hidden status. """ (self.sentence, self.mask) = inputs conv_out = conv.conv2d(input=self.sentence, \ filters=self.tparams[self._p(self.prefix, 'W')], \ filter_shape=self.filter_shape, \ image_shape=self.image_shape) conv_out = conv_out * self.mask.dimshuffle(0, 'x', 1, 'x') # downsample each feature map individually, using maxpooling pooled_out = downsample.pool_2d(input=conv_out, ds=self.pool_size, \ ignore_border=True, mode='max') output = tensor.tanh(pooled_out + self.tparams[self._p(self.prefix, 'b')]\ .dimshuffle('x', 0, 'x', 'x')) return output[:, :, 0, 0], conv_out
def cnn_layer(tparams, state_below, options, prefix='cnn'): klength = options['cnn_kernel_length'] for i in range(options['cnn_layer_num']): if i == 0: cnn_out = sconv.conv2d( state_below, tparams[_p(prefix, 'W1')].dimshuffle([0, 1, 'x'])) cnn_out = cnn_out + tparams[_p(prefix, 'b')][i].dimshuffle( ['x', 0, 'x', 'x']) cnn_out = tensor.nnet.relu(cnn_out) #cnn_out = nsigmoid(cnn_out) if options['use_pooling'] == True: cnn_out = spool.pool_2d(cnn_out, (3, 1), ignore_border=False, mode=options['pool_mode']).astype( config.floatX) if i != 0: cnn_out = nconv.conv2d( cnn_out, tparams[_p(prefix, 'W2')].dimshuffle([0, 1, 2, 'x'])) cnn_out = cnn_out + tparams[_p(prefix, 'b')][i].dimshuffle( ['x', 0, 'x', 'x']) cnn_out = tensor.nnet.relu(cnn_out) #cnn_out = nsigmoid(cnn_out) return cnn_out.astype(config.floatX)
def test_mean_filter(imgname, filter_shape): im = plt.imread(imgname) average_filter_shape = filter_shape # tuple average_filter = np.ones(average_filter_shape) / np.sum( average_filter_shape) # normalize the filter # theano requires two array to have same data type average_filter = average_filter.astype(float) im = im.astype(float) # this function does not have requirement on image size image_shape = im.shape # zero padding is used. the step of slide is 1 by default conv_out = conv.conv2d(input=im, filters=average_filter, filter_shape=average_filter_shape, image_shape=image_shape) # need to evaluate it # theano's calculation is symbolic. This evaluation step is needed to compile and return the value return (im, conv_out.eval())
def sym_conv2d(input, filters): return conv.conv2d(input, filters)
images.shape = (images.shape[0], 1, images.shape[1], images.shape[2]) Wshape = W.shape if (len(W.shape) < 4): W.shape = (W.shape[0], 1, W.shape[1], W.shape[2]) tempI = shared(images) tempW = shared(W) c_out = T.nnet.conv2d(tempI, tempW) c_fun = theano.function([], c_out) R = c_fun() W.shape = Wshape images.shape = Ishape return R A = T.dtensor3('W') B = T.dtensor3('conv_op_input') co = conv.conv2d(A, B) conv_fun_T3 = theano.function([A, B], co) def rot(A): return np.rot90(A, 2) def average_pool(A, pool_dim): B = np.ones((1, pool_dim, pool_dim)).astype(floatX) R = np.zeros((A.shape[0], A.shape[1], A.shape[2]/pool_dim, A.shape[3]/pool_dim)).astype(floatX) for i in range(A.shape[0]): temp = convolveTH4(A[i], B)[:, 0, 0::pool_dim, 0::pool_dim] R[i] = temp / (pool_dim * pool_dim) return R
Wshape = W.shape if (len(W.shape) < 4): W.shape = (W.shape[0], 1, W.shape[1], W.shape[2]) tempI = shared(images) tempW = shared(W) c_out = T.nnet.conv2d(tempI, tempW) c_fun = theano.function([], c_out) R = c_fun() W.shape = Wshape images.shape = Ishape return R A = T.dtensor3('W') B = T.dtensor3('conv_op_input') co = conv.conv2d(A, B) conv_fun_T3 = theano.function([A, B], co) def rot(A): return np.rot90(A, 2) def average_pool(A, pool_dim): B = np.ones((1, pool_dim, pool_dim)).astype(floatX) R = np.zeros((A.shape[0], A.shape[1], A.shape[2] / pool_dim, A.shape[3] / pool_dim)).astype(floatX) for i in range(A.shape[0]): temp = convolveTH4(A[i], B)[:, 0, 0::pool_dim, 0::pool_dim] R[i] = temp / (pool_dim * pool_dim) return R
(img.size[1], img.size[0])) blue_pixels = np.asarray(blue_values, dtype="int32").reshape( (img.size[1], img.size[0])) green_pixels = np.asarray(green_values, dtype="int32").reshape( (img.size[1], img.size[0])) #img_array = np.asarray(img, dtype = "int32") tensor1 = T.shared(np.matrix("0 0 0; 0 1 0; 0 0 0")) tensor2 = T.shared(np.matrix("0 0 0; 0 -1 0; 0 0 0")) tensor = T.shared(np.matrix("0 0 0; 0 5 0; 0 0 0")) tensor3 = T.shared(np.matrix("1 0 -1; 0 0 0; -1 0 1")) t1 = np.asarray( (tensor.eval(), tensor1.eval(), tensor2.eval(), tensor3.eval())) #print(t1.shape) stride = (1, 1) conv1 = conv2d(input=red_pixels, filters=t1, subsample=stride) conv2 = conv2d(input=green_pixels, filters=t1, subsample=stride) conv3 = conv2d(input=blue_pixels, filters=t1, subsample=stride) red_feature_map_layer1 = conv1.eval() blue_feature_map_layer1 = conv2.eval() green_feature_map_layer1 = conv3.eval() red_feature_map_layer1 = conv1.eval().reshape( red_feature_map_layer1.shape[0], red_feature_map_layer1.shape[2], red_feature_map_layer1.shape[1]) #feature map 1 from layer 1 blue_feature_map_layer1 = conv2.eval().reshape( blue_feature_map_layer1.shape[0], blue_feature_map_layer1.shape[2], blue_feature_map_layer1.shape[1]) #feature map 2 from layer 1 green_feature_map_layer1 = conv3.eval().reshape( green_feature_map_layer1.shape[0], green_feature_map_layer1.shape[2], green_feature_map_layer1.shape[1]) #feature map 3 from layer 1
def convolve_one_image(self,input4D, one_image, image_shape, Pstruct, filter_shape, image_index, channel_index): """ Convolve one image with separabl filters. :type input: theano.tensor.dtensor3 :param input: symbolic image tensor, of shape image_shape :type image_shape: tuple or list of length 3 :param image_shape: ( nbr channels, image height, image width) :type filter_shape: tuple or list of length 4 :param filter_shape: (number of filters,nbrmaps, filter height,filter width) :type poolsize: tuple or list of length 2 :param poolsize: the downsampling (pooling) factor (#rows,#cols) """ ## We look at the composition for the first channel in the beginning rank = Pstruct[0]['U1'].shape[1] fwidth = filter_shape[2] fheight = filter_shape[3] # Construct horizontal filters #TODO save the filters in the correct shape horizontal_filter_shape = (rank, 1, fwidth) horizontal_filters = np.ndarray(horizontal_filter_shape) horizontal_filters[:, 0, :] = np.transpose(Pstruct[channel_index]['U1']); # Output is 1 x rank x W x H horizontal_conv_out = conv.conv2d(input=one_image, filters = horizontal_filters, filter_shape = horizontal_filter_shape, image_shape = image_shape) # Construct vertical filters vertical_filter_shape = (rank, fheight, 1) vertical_filters = np.ndarray(vertical_filter_shape) vertical_filters[:,:, 0] = np.transpose(Pstruct[channel_index]['U2']); initial_n_rows = image_shape[1] final_n_rows = initial_n_rows- fwidth + 1 final_n_cols = image_shape[2] - fheight + 1 conv_out = theano.shared(np.zeros((rank, final_n_rows, final_n_cols))) for r in range(rank): # temp is 1x1x imageW x imageH A = conv.conv2d(input = horizontal_conv_out[:,r,:,:], filters = vertical_filters[r,:,:], filter_shape = (1, fheight, 1), image_shape = (1, initial_n_rows, final_n_cols)) conv_out = T.set_subtensor(conv_out[r,:,:], A[0,:,:]) nbr_filters = Pstruct[0]['U3'].shape[0] # Final number of rows and columns ## numberof images, number of filters, image width, image height alphas = Pstruct[channel_index]['U3'] for f in range(nbr_filters): temp = theano.shared(np.zeros((final_n_rows, final_n_cols))) for r in range(rank): temp = temp + conv_out[r, :,:]* alphas[f, r] * Pstruct[channel_index]['lmbda'][r]; input4D =T.set_subtensor(input4D[image_index,f,:,:], temp) return input4D
# normalize eeg = normalize(eeg, axis=1) acc = normalize(acc, axis=1) # set dims for convolution eeg = eeg.dimshuffle(0, 2, 1, 'x') acc = acc.dimshuffle(0, 2, 1, 'x') # apply gaussian filter on eeg if eeg_gaussian_filter_width > 0: l = eeg_gaussian_filter_width / 2 kernel = numpy.exp(-(numpy.arange(-l, l)**2) / (2 * eeg_gaussian_filter_sigma**2)) kernel = kernel / numpy.sqrt(2 * 3.1415) / eeg_gaussian_filter_sigma kernel = kernel.astype('float32') eeg1 = conv2d(eeg[:, 0, :, :], kernel[:, None], border_mode='full')[:, None, :, :] d1 = (eeg1.shape[2] - eeg.shape[2]) / 2 eeg = eeg1[:, :, d1:d1 + eeg.shape[2]:eeg_gaussian_filter_step, :] # first convolutions only on eeg eeg_channels = 1 for i, cp in enumerate(conv_eeg): bconv = Convolutional(filter_size=(cp['filter_size'], 1), num_filters=cp['num_filters'], num_channels=eeg_channels, border_mode='full', tied_biases=True, name="conv_eeg_%d" % i) bmaxpool = MaxPooling(pooling_size=(cp['pool_size'], 1), name='maxpool_eeg_%d' % i) # convolve
def filt_app(y): filt = T.alloc(1., filt_len, 1)/filt_len #filter height = filt_len, filter width = 1 temp = conv2d(y, filt, border_mode='full') temp = temp[:, filt_len/2:, :] temp = temp[:, :rec_length, :] return temp
def __init__(self, input, response, image_shape=None, gkern=5, gsigma=1.5, c1=6.5025, c2=58.5225): if isinstance(input, Layer): self.input = input.output if image_shape == None: image_shape = input.output_shape else: self.input = input assert image_shape == response.resp_shape kernel = np.array([[ np.exp((-x * x - y * y) * 0.5 / gsigma / gsigma) / (gsigma * np.sqrt(2 * np.pi)) for x in range(-gkern, gkern + 1) ] for y in range(-gkern, gkern + 1)], 'f') KERNEL = theano.shared(kernel, name='SSIM_KERNEL_%s_%s' % (gkern, gsigma)) iflat = self.input.reshape( (image_shape[0] * image_shape[1], image_shape[2], image_shape[3])) oflat = response.resp.reshape( (image_shape[0] * image_shape[1], image_shape[2], image_shape[3])) iflatsqr = iflat * iflat oflatsqr = oflat * oflat crossflat = iflat * oflat iwindow = sconv.conv2d( iflat, KERNEL, image_shape=image_shape, filter_shape=(gkern * 2 + 1, gkern * 2 + 1), border_mode='full')[:, gkern:-gkern, gkern:-gkern].reshape(image_shape) isqrwin = sconv.conv2d( iflatsqr, KERNEL, image_shape=image_shape, filter_shape=(gkern * 2 + 1, gkern * 2 + 1), border_mode='full')[:, gkern:-gkern, gkern:-gkern].reshape(image_shape) owindow = sconv.conv2d( oflat, KERNEL, image_shape=image_shape, filter_shape=(gkern * 2 + 1, gkern * 2 + 1), border_mode='full')[:, gkern:-gkern, gkern:-gkern].reshape(image_shape) osqrwin = sconv.conv2d( oflatsqr, KERNEL, image_shape=image_shape, filter_shape=(gkern * 2 + 1, gkern * 2 + 1), border_mode='full')[:, gkern:-gkern, gkern:-gkern].reshape(image_shape) crosswin = sconv.conv2d( crossflat, KERNEL, image_shape=image_shape, filter_shape=(gkern * 2 + 1, gkern * 2 + 1), border_mode='full')[:, gkern:-gkern, gkern:-gkern].reshape(image_shape) vari = isqrwin - iwindow * iwindow varo = osqrwin - owindow * owindow cross = crosswin - iwindow * owindow SSIMblk = (2 * iwindow * owindow + c1) * (2 * cross + c2) / ( iwindow * iwindow + owindow * owindow + c1) / (vari + varo + c2) SSIM = SSIMblk.mean() self.loss = 1 - SSIM self.output = response.resp self.output_shape = response.resp_shape
filePath = r'E:\VirtualDesktop\nnet\minist\double_mnist.pkl.gz' picPath = r'E:\VirtualDesktop\nnet\minist\kernels.pkl' xData = readMnist(filePath) xData10 = xData[1][0] yData = (xData10[3], xData10[2], xData10[1], xData10[44], xData10[4], xData10[8], xData10[11], xData10[0], xData10[61], xData10[62]) from theano.tensor.signal.conv import conv2d import theano from theano import tensor as T xT = T.matrix() filter = np.ones((1, 1)) outputTensor = conv2d(input=xT, filters=filter, subsample=(2, 2)) fun = theano.function(inputs=[xT], outputs=outputTensor) tmp = [] tmpImg = None tmp2 = [] for img in yData: tmpImg = fun(img) tmpImg = norm_img(tmpImg)[2:-2] tmp.append(trim_img(tmpImg)) for img in tmp: tmp2.append(img.flat[:]) yyData = tuple(tmp2)
def make_theano_m2_convolve(mattype_inp, mattype_ker): dtype = np.dtype(rtype).name input = mattype_inp('input', dtype) kernel = mattype_ker('kernel', dtype) return function([input, kernel], conv2d(input, kernel))
def convolve_special_image(channel_index, input4D, input_images, fU1, fU2, fU3, flmda, ib, iw, ih): """ Convolve one image with separabl filters. :type input: theano.tensor.dtensor3 :param input: symbolic image tensor, of shape image_shape :type image_shape: tuple or list of length 3 :param image_shape: ( nbr channels, image height, image width) :type filter_shape: tuple or list of length 4 :param filter_shape: (number of filters,nbrmaps, filter height,filter width) :type poolsize: tuple or list of length 2 :param poolsize: the downsampling (pooling) factor (#rows,#cols) """ U1 = fU1[channel_index,:,:] U2 = fU2[channel_index,:,:] U3 = fU3[channel_index,:,:] lmda = flmda[channel_index,:] one_chanel_images = input_images[:,channel_index,:,:]; ## We look at the composition for the first channel in the beginning image_shape = (ib, iw, ih) filter_shape = (U3.shape[0], input_images[1],U2.shape[0],U1.shape[0]) rank = U1.shape[1] fwidth = filter_shape[2] fheight = filter_shape[3] # Construct horizontal filters #TODO save the filters in the correct shape horizontal_filter_shape = (rank, 1, fwidth) horizontal_filters = np.ndarray(shape=horizontal_filter_shape) horizontal_filters[:, 0, :] = np.transpose(U1); # Output is batch size x rank x W x H horizontal_conv_out = conv.conv2d(input=one_chanel_images, filters = horizontal_filters, filter_shape = horizontal_filter_shape, image_shape = image_shape) # Construct vertical filters vertical_filter_shape = (rank, fheight, 1) vertical_filters = np.ndarray(vertical_filter_shape) vertical_filters[:,:, 0] = np.transpose(U2); initial_n_rows = image_shape[1] final_n_rows = initial_n_rows- fwidth + 1 final_n_cols = image_shape[2] - fheight + 1 batch_size = image_shape[0] conv_out = theano.shared(np.zeros((batch_size, rank, final_n_rows, final_n_cols))) for r in range(rank): # output is batch_size x 1 x imageW x imageH A = conv.conv2d(input = horizontal_conv_out[:,r,:,:].reshape((batch_size, initial_n_rows, final_n_cols)), filters = vertical_filters[r,:,:], filter_shape = (1, fheight, 1), image_shape = (batch_size, initial_n_rows, final_n_cols)) conv_out = T.set_subtensor(conv_out[:,r,:,:], A[:,:,:]) nbr_filters = U3.shape[0] # Final number of rows and columns ## numberof images, number of filters, image width, image height alphas = U3 for f in range(nbr_filters): temp = theano.shared(np.zeros((batch_size, final_n_rows, final_n_cols))) for r in range(rank): temp = temp + conv_out[:,r, :,:]* alphas[f, r] * lmda[r]; input4D =T.set_subtensor(input4D[:,f,:,:], temp) return input4D
def build(self): """ Builds and returns the Generative model. Also sets self.model """ p_delay = get_delay_distribution() nonzero_days = self.observed.total.gt(0) len_observed = len(self.observed) convolution_ready_gt = self._get_convolution_ready_gt(len_observed) x = np.arange(len_observed)[:, None] coords = { "date": self.observed.index.values, "nonzero_date": self.observed.index.values[self.observed.total.gt(0)], } with pm.Model(coords=coords) as self.model: # Let log_r_t walk randomly with a fixed prior of ~0.035. Think # of this number as how quickly r_t can react. log_r_t = pm.GaussianRandomWalk("log_r_t", sigma=0.035, dims=["date"]) r_t = pm.Deterministic("r_t", pm.math.exp(log_r_t), dims=["date"]) # For a given seed population and R_t curve, we calculate the # implied infection curve by simulating an outbreak. While this may # look daunting, it's simply a way to recreate the outbreak # simulation math inside the model: # https://staff.math.su.se/hoehle/blog/2020/04/15/effectiveR0.html seed = pm.Exponential("seed", 1 / 0.02) y0 = tt.zeros(len_observed) y0 = tt.set_subtensor(y0[0], seed) outputs, _ = theano.scan( fn=lambda t, gt, y, r_t: tt.set_subtensor( y[t], tt.sum(r_t * y * gt)), sequences=[tt.arange(1, len_observed), convolution_ready_gt], outputs_info=y0, non_sequences=r_t, n_steps=len_observed - 1, ) infections = pm.Deterministic("infections", outputs[-1], dims=["date"]) # Convolve infections to confirmed positive reports based on a known # p_delay distribution. See patients.py for details on how we calculate # this distribution. test_adjusted_positive = pm.Deterministic( "test_adjusted_positive", conv2d( tt.reshape(infections, (1, len_observed)), tt.reshape(p_delay, (1, len(p_delay))), border_mode="full", )[0, :len_observed], dims=["date"]) # Picking an exposure with a prior that exposure never goes below # 0.1 * max_tests. The 0.1 only affects early values of Rt when # testing was minimal or when data errors cause underreporting # of tests. tests = pm.Data("tests", self.observed.total.values, dims=["date"]) exposure = pm.Deterministic("exposure", pm.math.clip( tests, self.observed.total.max() * 0.1, 1e9), dims=["date"]) # Test-volume adjust reported cases based on an assumed exposure # Note: this is similar to the exposure parameter in a Poisson # regression. positive = pm.Deterministic("positive", exposure * test_adjusted_positive, dims=["date"]) # Save data as part of trace so we can access in inference_data observed_positive = pm.Data("observed_positive", self.observed.positive.values, dims=["date"]) nonzero_observed_positive = pm.Data( "nonzero_observed_positive", self.observed.positive[nonzero_days.values].values, dims=["nonzero_date"]) positive_nonzero = pm.NegativeBinomial( "nonzero_positive", mu=positive[nonzero_days.values], alpha=pm.Gamma("alpha", mu=6, sigma=1), observed=nonzero_observed_positive, dims=["nonzero_date"]) return self.model
filePath = r'E:\VirtualDesktop\nnet\minist\double_mnist.pkl.gz' picPath = r'E:\VirtualDesktop\nnet\minist\kernels.pkl' xData = readMnist( filePath ) xData10 = xData[1][0] yData = (xData10[3], xData10[2], xData10[1], xData10[44], xData10[4], xData10[8], xData10[11], xData10[0], xData10[61], xData10[62]) from theano.tensor.signal.conv import conv2d import theano from theano import tensor as T xT = T.matrix() filter = np.ones( (1, 1) ) outputTensor = conv2d(input=xT, filters=filter, subsample=(2,2) ) fun = theano.function(inputs=[xT], outputs=outputTensor) tmp = [] tmpImg = None tmp2=[] for img in yData: tmpImg = fun(img) tmpImg = norm_img(tmpImg)[2:-2] tmp.append(trim_img(tmpImg)) for img in tmp: tmp2.append(img.flat[:]) yyData = tuple(tmp2)
def setup(self, bottom, top): from caffe_helper.theano_util import init_theano init_theano() import theano as tn import theano.tensor as T from theano.tensor.signal.conv import conv2d assert len(bottom) >= 2 assert len(bottom) <= 3 assert len(top) == 1 # parameter self.K_ = [0.01, 0.03] self.L_ = 1.0 param = eval(self.param_str_) self.hsize_ = param.get('hsize', 11) self.sigma_ = param.get('sigma', 1.5) assert self.hsize_ % 2 == 1 hsize = self.hsize_ sigma = self.sigma_ C1 = (self.K_[0] * self.L_)**2 C2 = (self.K_[1] * self.L_)**2 # Creating gaussian filter x = np.exp(-0.5 * ((np.arange(hsize) - int(hsize / 2))**2) / (sigma**2)) filt = x.reshape(-1, 1) * x.reshape(1, -1) filt /= filt.sum() # Build a Theano function which computes SSIM and its gradients wrt two # images simg1_in = T.ftensor3() simg2_in = T.ftensor3() if len(bottom) > 2: smask = T.ftensor3() sk = T.sum(simg1_in * simg2_in * smask) \ / T.sum(simg1_in * simg1_in * smask) simg1 = sk * simg1_in * smask simg2 = simg2_in * smask else: sk = T.sum(simg1_in * simg2_in) \ / T.sum(simg1_in * simg1_in) simg1 = sk * simg1_in simg2 = simg2_in sfilt = tn.shared(filt.astype(np.float32)) smu1 = conv2d(simg1, sfilt) smu2 = conv2d(simg2, sfilt) smu1_sq = smu1 * smu1 smu2_sq = smu2 * smu2 smu1_mu2 = smu1 * smu2 ssig1_sq = conv2d(simg1 * simg1, sfilt) - smu1_sq ssig2_sq = conv2d(simg2 * simg2, sfilt) - smu2_sq ssig12 = conv2d(simg1 * simg2, sfilt) - smu1_mu2 sssim = (((2 * smu1_mu2 + C1) * (2 * ssig12 + C2)) / ((smu1_sq + smu2_sq + C1) * (ssig1_sq + ssig2_sq + C2))).mean() sdssim = (1 - sssim) / 2 gimg1, gimg2 = tn.grad(sdssim, [simg1_in, simg2_in]) if len(bottom) > 2: self.fdssim_with_grad = tn.function([simg1_in, simg2_in, smask], [sdssim, gimg1, gimg2]) else: self.fdssim_with_grad = tn.function([simg1_in, simg2_in], [sdssim, gimg1, gimg2])
def setup(self, bottom, top): from caffe_helper.theano_util import init_theano init_theano() import theano as tn import theano.tensor as T from theano.tensor.signal.conv import conv2d assert len(bottom) >= 2 assert len(bottom) <= 3 assert len(top) == 1 # parameter self.K_ = [0.01, 0.03] self.L_ = 1.0 param = eval(self.param_str_) self.hsize_ = param.get('hsize', 11) self.sigma_ = param.get('sigma', 1.5) assert self.hsize_ % 2 == 1 hsize = self.hsize_ sigma = self.sigma_ C1 = (self.K_[0] * self.L_) ** 2 C2 = (self.K_[1] * self.L_) ** 2 # Creating gaussian filter x = np.exp(-0.5 * ((np.arange(hsize) - int(hsize / 2)) ** 2) / (sigma ** 2)) filt = x.reshape(-1, 1) * x.reshape(1, -1) filt /= filt.sum() # Build a Theano function which computes SSIM and its gradients wrt two # images simg1_in = T.ftensor3() simg2_in = T.ftensor3() if len(bottom) > 2: smask = T.ftensor3() sk = T.sum(simg1_in * simg2_in * smask) \ / T.sum(simg1_in * simg1_in * smask) simg1 = sk * simg1_in * smask simg2 = simg2_in * smask else: sk = T.sum(simg1_in * simg2_in) \ / T.sum(simg1_in * simg1_in) simg1 = sk * simg1_in simg2 = simg2_in sfilt = tn.shared(filt.astype(np.float32)) smu1 = conv2d(simg1, sfilt) smu2 = conv2d(simg2, sfilt) smu1_sq = smu1 * smu1 smu2_sq = smu2 * smu2 smu1_mu2 = smu1 * smu2 ssig1_sq = conv2d(simg1 * simg1, sfilt) - smu1_sq ssig2_sq = conv2d(simg2 * simg2, sfilt) - smu2_sq ssig12 = conv2d(simg1 * simg2, sfilt) - smu1_mu2 sssim = ( ((2 * smu1_mu2 + C1) * (2 * ssig12 + C2)) / ((smu1_sq + smu2_sq + C1) * (ssig1_sq + ssig2_sq + C2)) ).mean() sdssim = (1 - sssim) / 2 gimg1, gimg2 = tn.grad(sdssim, [simg1_in, simg2_in]) if len(bottom) > 2: self.fdssim_with_grad = tn.function( [simg1_in, simg2_in, smask], [sdssim, gimg1, gimg2]) else: self.fdssim_with_grad = tn.function( [simg1_in, simg2_in], [sdssim, gimg1, gimg2])
def __init__(self, inpt, img_sz, num_maps=1, translation=0, zoom=1, magnitude=0, sigma=1, pflip=0, angle=0, rand_gen=None, invert_image=False, nearest=False): self.inpt = inpt self.img_sz = img_sz self.translation = translation self.zoom = zoom self.magnitude = magnitude self.sigma = sigma self.invert = invert_image self.nearest = nearest self.out_sz = img_sz self.num_maps = num_maps self.n_out = self.num_maps * self.out_sz**2 self.params = [] self.representation = ('Elastic Maps:{:d} Size:{:2d} Translation:{:} ' 'Zoom:{} Mag:{:2d} Sig:{:2d} Noise:{} ' 'Angle:{} Invert:{} ' 'Interpolation: {}'.format( self.num_maps, img_sz, translation, zoom, magnitude, sigma, pflip, angle, invert_image, 'Nearest' if nearest else 'Linear')) if invert_image: inpt = 1 - inpt assert zoom > 0 if not (magnitude or translation or pflip or angle) and zoom == 1: self.output = inpt self.debugout = [self.output, tt.as_tensor_variable((0, 0))] return srs = tt.shared_randomstreams.RandomStreams( rand_gen.randint(1e6) if rand_gen else None) h = w = img_sz # Humble as-is beginning target = tt.as_tensor_variable(np.indices((h, w))) # Translate if translation: transln = translation * srs.uniform((2, 1, 1), -1) target += transln # Apply elastic transform if magnitude: # Build a gaussian filter var = sigma**2 filt = np.array([[ np.exp(-.5 * (i * i + j * j) / var) for i in range(-sigma, sigma + 1) ] for j in range(-sigma, sigma + 1)], dtype=float_x) filt /= 2 * np.pi * var # Elastic elast = magnitude * srs.normal((2, h, w)) elast = sigconv.conv2d(elast, filt, (2, h, w), filt.shape, 'full') elast = elast[:, sigma:h + sigma, sigma:w + sigma] target += elast # Center at 'about' half way if zoom - 1 or angle: origin = srs.uniform((2, 1, 1), .25, .75) * \ np.array((h, w)).reshape((2, 1, 1)) target -= origin # Zoom if zoom - 1: zoomer = tt.exp(np.log(zoom) * srs.uniform((2, 1, 1), -1)) target *= zoomer # Rotate if angle: theta = angle * np.pi / 180 * srs.uniform(low=-1) c, s = tt.cos(theta), tt.sin(theta) rotate = tt.stack(c, -s, s, c).reshape((2, 2)) target = tt.tensordot(rotate, target, axes=((0, 0))) # Uncenter target += origin # Clip the mapping to valid range and linearly interpolate transy = tt.clip(target[0], 0, h - 1 - .001) transx = tt.clip(target[1], 0, w - 1 - .001) if nearest: vert = tt.iround(transy) horz = tt.iround(transx) output = inpt[:, :, vert, horz] else: topp = tt.cast(transy, 'int32') left = tt.cast(transx, 'int32') fraction_y = tt.cast(transy - topp, float_x) fraction_x = tt.cast(transx - left, float_x) output = inpt[:, :, topp, left] * (1 - fraction_y) * (1 - fraction_x) + \ inpt[:, :, topp, left + 1] * (1 - fraction_y) * fraction_x + \ inpt[:, :, topp + 1, left] * fraction_y * (1 - fraction_x) + \ inpt[:, :, topp + 1, left + 1] * fraction_y * fraction_x # Now add some noise if pflip: mask = srs.binomial(n=1, p=pflip, size=inpt.shape, dtype=float_x) output = (1 - output) * mask + output * (1 - mask) self.output = output self.debugout = [ self.output, target - np.indices((h, w)), ] if translation: self.debugout.append(transln) if zoom - 1 or angle: self.debugout.append(origin) if angle: self.debugout.append(theta * 180 / np.pi) if zoom - 1: self.debugout.append(zoomer)
def __init__(self, corpus, sentenceWordCount, rng, wordEmbeddingDim, sentenceLayerNodesNum=2, sentenceLayerNodesSize=(2, 2), docLayerNodesNum=2, docLayerNodesSize=(2, 3), datatype=theano.config.floatX, pooling_mode="average_exc_pad"): self.__wordEmbeddingDim = wordEmbeddingDim self.__sentenceLayerNodesNum = sentenceLayerNodesNum self.__sentenceLayerNodesSize = sentenceLayerNodesSize self.__docLayerNodesNum = docLayerNodesNum self.__docLayerNodesSize = docLayerNodesSize self.__WBound = 0.2 self.__MAXDIM = 10000 self.__datatype = datatype self.sentenceW = None self.sentenceB = None self.docW = None self.docB = None self.__pooling_mode = pooling_mode # For DomEmbeddingNN optimizer. # self.shareRandge = T.arange(maxRandge) # Get sentence layer W self.sentenceW = theano.shared( numpy.asarray( rng.uniform(low=-self.__WBound, high=self.__WBound, size=(self.__sentenceLayerNodesNum, self.__sentenceLayerNodesSize[0], self.__sentenceLayerNodesSize[1])), dtype=datatype ), borrow=True ) # Get sentence layer b sentenceB0 = numpy.zeros((sentenceLayerNodesNum,), dtype=datatype) self.sentenceB = theano.shared(value=sentenceB0, borrow=True) # Get doc layer W self.docW = theano.shared( numpy.asarray( rng.uniform(low=-self.__WBound, high=self.__WBound, size=(self.__docLayerNodesNum, self.__docLayerNodesSize[0], self.__docLayerNodesSize[1])), dtype=datatype ), borrow=True ) # Get doc layer b docB0 = numpy.zeros((docLayerNodesNum,), dtype=datatype) self.docB = theano.shared(value=docB0, borrow=True) self.sentenceResults, _ = theano.scan(fn=self.__dealWithSentence, non_sequences=[corpus, self.sentenceW, self.sentenceB], sequences=[dict(input=sentenceWordCount, taps=[-1, -0])], strict=True) # p = printing.Print('docPool') # docPool = p(docPool) # p = printing.Print('sentenceResults') # sentenceResults = p(sentenceResults) # p = printing.Print('doc_out') # doc_out = p(doc_out) doc_out = conv.conv2d(input=self.sentenceResults, filters=self.docW) docPool = downsample.max_pool_2d(doc_out, (self.__MAXDIM, 1), mode=self.__pooling_mode, ignore_border=False) docOutput = T.tanh(docPool + self.docB.dimshuffle([0, 'x', 'x'])) self.output = docOutput.flatten(1) self.params = [self.sentenceW, self.sentenceB, self.docW, self.docB] self.outputDimension = self.__docLayerNodesNum * \ (self.__sentenceLayerNodesNum * (self.__wordEmbeddingDim - self.__sentenceLayerNodesSize[1] + 1) - self.__docLayerNodesSize[1] + 1)
sys.path.append('./pdnn') from models.dnn import DNN from io_func.model_io import _file2nnet from io_func import smart_open import cPickle import theano from theano.tensor.signal import conv import theano.tensor as T numpy_rng = numpy.random.RandomState(101) # CODE BELOW NEEDED TO MAKE CONVOLUTIONS WITH THEANO input = T.dmatrix('input') filter = T.dmatrix('filter') conv_out = conv.conv2d(input, filter) convolution_function = theano.function([input, filter], conv_out) ################################################# """ Simple example pokerbot, written in python. This is an example of a bare bones pokerbot. It only sets up the socket necessary to connect with the engine and then always returns the same action. It is meant as an example of how a pokerbot should communicate with the engine. """ def load_flop_network(nnet_param = 'neural_network/flop_network_params', nnet_cfg = 'neural_network/flop_network_cfg'): cfg = cPickle.load(smart_open(nnet_cfg,'r')) cfg.init_activation() model = DNN(numpy_rng=numpy_rng, cfg = cfg)
def __init__(self, inpt, img_sz, num_maps = 1, translation=0, zoom=1, magnitude=0, sigma=1, pflip=0, angle=0, rand_gen=None, invert_image=False, nearest=False): self.inpt = inpt self.img_sz = img_sz self.translation = translation self.zoom = zoom self.magnitude = magnitude self.sigma = sigma self.invert = invert_image self.nearest = nearest self.out_sz = img_sz self.num_maps = num_maps self.n_out = self.num_maps * self.out_sz ** 2 self.params = [] self.representation = ('Elastic Maps:{:d} Size:{:2d} Translation:{:} ' 'Zoom:{} Mag:{:d} Sig:{:d} Noise:{} ' 'Angle:{} Invert:{} ' 'Interpolation:{}'.format( self.num_maps, img_sz, translation, zoom, magnitude, sigma, pflip, angle, invert_image, 'Nearest' if nearest else 'Linear')) if invert_image: inpt = 1 - inpt assert zoom > 0 if not (magnitude or translation or pflip or angle) and zoom == 1: self.output = inpt self.debugout = [self.output, tt.as_tensor_variable((0, 0))] return srs = tt.shared_randomstreams.RandomStreams(rand_gen.randint(1e6) if rand_gen else None) h = w = img_sz # Humble as-is beginning target = tt.as_tensor_variable(np.indices((h, w))) # Translate if translation: transln = translation * srs.uniform((2, 1, 1), -1) target += transln # Apply elastic transform if magnitude: # Build a gaussian filter var = sigma ** 2 filt = np.array([[np.exp(-.5 * (i * i + j * j) / var) for i in range(-sigma, sigma + 1)] for j in range(-sigma, sigma + 1)], dtype=float_x) filt /= 2 * np.pi * var # Elastic elast = magnitude * srs.normal((2, h, w)) elast = sigconv.conv2d(elast, filt, (2, h, w), filt.shape, 'full') elast = elast[:, sigma:h + sigma, sigma:w + sigma] target += elast # Center at 'about' half way if zoom-1 or angle: origin = srs.uniform((2, 1, 1), .25, .75) * \ np.array((h, w)).reshape((2, 1, 1)) target -= origin # Zoom if zoom-1: zoomer = tt.exp(np.log(zoom) * srs.uniform((2, 1, 1), -1)) target *= zoomer # Rotate if angle: theta = angle * np.pi / 180 * srs.uniform(low=-1) c, s = tt.cos(theta), tt.sin(theta) rotate = tt.stack(c, -s, s, c).reshape((2,2)) target = tt.tensordot(rotate, target, axes=((0, 0))) # Uncenter target += origin # Clip the mapping to valid range and linearly interpolate transy = tt.clip(target[0], 0, h - 1 - .001) transx = tt.clip(target[1], 0, w - 1 - .001) if nearest: vert = tt.iround(transy) horz = tt.iround(transx) output = inpt[:, :, vert, horz] else: topp = tt.cast(transy, 'int32') left = tt.cast(transx, 'int32') fraction_y = tt.cast(transy - topp, float_x) fraction_x = tt.cast(transx - left, float_x) output = inpt[:, :, topp, left] * (1 - fraction_y) * (1 - fraction_x) + \ inpt[:, :, topp, left + 1] * (1 - fraction_y) * fraction_x + \ inpt[:, :, topp + 1, left] * fraction_y * (1 - fraction_x) + \ inpt[:, :, topp + 1, left + 1] * fraction_y * fraction_x # Now add some noise if pflip: mask = srs.binomial(n=1, p=pflip, size=inpt.shape, dtype=float_x) output = (1 - output) * mask + output * (1 - mask) self.output = output self.debugout = [self.output, target - np.indices((h, w)),] if translation: self.debugout.append(transln) if zoom-1 or angle: self.debugout.append(origin) if angle: self.debugout.append(theta*180/np.pi) if zoom-1: self.debugout.append(zoomer)