def _linspace(start, stop, num): # Theano linspace. Behaves similar to np.linspace start = T.cast(start, theano.config.floatX) stop = T.cast(stop, theano.config.floatX) num = T.cast(num, theano.config.floatX) step = (stop-start)/(num-1) return T.arange(num, dtype=theano.config.floatX)*step+start
def get_output_for(self, input, **kwargs): num_feature_maps = input.shape[self.axis] num_pools = num_feature_maps // self.pool_size pool_shape = () arange_shuffle_pattern = () for k in range(self.axis): pool_shape += (input.shape[k],) arange_shuffle_pattern += ('x',) pool_shape += (num_pools, self.pool_size) arange_shuffle_pattern += ('x', 0) for k in range(self.axis + 1, input.ndim): pool_shape += (input.shape[k],) arange_shuffle_pattern += ('x',) input_reshaped = input.reshape(pool_shape) max_indices = T.argmax(input_reshaped, axis=self.axis + 1, keepdims=True) arange = T.arange(self.pool_size).dimshuffle(*arange_shuffle_pattern) mask = T.eq(max_indices, arange).reshape(input.shape) return input * mask
def _interpolate(im, x, y, out_height, out_width): # *_f are floats num_batch, height, width, channels = im.shape height_f = T.cast(height, theano.config.floatX) width_f = T.cast(width, theano.config.floatX) # clip coordinates to [-1, 1] x = T.clip(x, -1, 1) y = T.clip(y, -1, 1) # scale coordinates from [-1, 1] to [0, width/height - 1] x = (x + 1) / 2 * (width_f - 1) y = (y + 1) / 2 * (height_f - 1) # obtain indices of the 2x2 pixel neighborhood surrounding the coordinates; # we need those in floatX for interpolation and in int64 for indexing. for # indexing, we need to take care they do not extend past the image. x0_f = T.floor(x) y0_f = T.floor(y) x1_f = x0_f + 1 y1_f = y0_f + 1 x0 = T.cast(x0_f, 'int64') y0 = T.cast(y0_f, 'int64') x1 = T.cast(T.minimum(x1_f, width_f - 1), 'int64') y1 = T.cast(T.minimum(y1_f, height_f - 1), 'int64') # The input is [num_batch, height, width, channels]. We do the lookup in # the flattened input, i.e [num_batch*height*width, channels]. We need # to offset all indices to match the flat version dim2 = width dim1 = width*height base = T.repeat( T.arange(num_batch, dtype='int64')*dim1, out_height*out_width) base_y0 = base + y0*dim2 base_y1 = base + y1*dim2 idx_a = base_y0 + x0 idx_b = base_y1 + x0 idx_c = base_y0 + x1 idx_d = base_y1 + x1 # use indices to lookup pixels for all samples im_flat = im.reshape((-1, channels)) Ia = im_flat[idx_a] Ib = im_flat[idx_b] Ic = im_flat[idx_c] Id = im_flat[idx_d] # calculate interpolated values wa = ((x1_f-x) * (y1_f-y)).dimshuffle(0, 'x') wb = ((x1_f-x) * (y-y0_f)).dimshuffle(0, 'x') wc = ((x-x0_f) * (y1_f-y)).dimshuffle(0, 'x') wd = ((x-x0_f) * (y-y0_f)).dimshuffle(0, 'x') output = T.sum([wa*Ia, wb*Ib, wc*Ic, wd*Id], axis=0) return output