def extract_patches_fn(image: tf.Tensor, patch_shape: list, offsets) -> tf.Tensor:
"""
:param image: tf.Tensor
:param patch_shape: [h, w]
:param offsets: tuple between 0 and 1
:return: patches [batch_patches, h, w, c]
"""
with tf.name_scope('patch_extraction'):
h, w = patch_shape
c = image.get_shape()[-1]
offset_h = tf.cast(tf.round(offsets[0] * h // 2), dtype=tf.int32)
offset_w = tf.cast(tf.round(offsets[1] * w // 2), dtype=tf.int32)
offset_img = image[offset_h:, offset_w:, :]
offset_img = offset_img[None, :, :, :]
patches = tf.extract_image_patches(offset_img, ksizes=[1, h, w, 1], strides=[1, h // 2, w // 2, 1],
rates=[1, 1, 1, 1], padding='VALID')
patches_shape = tf.shape(patches)
return tf.reshape(patches, [tf.reduce_prod(patches_shape[0:3]), h, w, int(c)]) # returns [batch_patches, h, w, c]
python类extract_image_patches()的实例源码
def extract_image_patches(x, ksizes, ssizes, padding='same',
data_format='channels_last'):
'''
Extract the patches from an image
# Parameters
x : The input image
ksizes : 2-d tuple with the kernel size
ssizes : 2-d tuple with the strides size
padding : 'same' or 'valid'
data_format : 'channels_last' or 'channels_first'
# Returns
The (k_w,k_h) patches extracted
TF ==> (batch_size,w,h,k_w,k_h,c)
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
kernel = [1, ksizes[0], ksizes[1], 1]
strides = [1, ssizes[0], ssizes[1], 1]
padding = _preprocess_padding(padding)
if data_format == 'channels_first':
x = KTF.permute_dimensions(x, (0, 2, 3, 1))
bs_i, w_i, h_i, ch_i = KTF.int_shape(x)
patches = tf.extract_image_patches(x, kernel, strides, [1, 1, 1, 1],
padding)
# Reshaping to fit Theano
bs, w, h, ch = KTF.int_shape(patches)
patches = tf.reshape(tf.transpose(tf.reshape(patches, [-1, w, h, tf.floordiv(ch, ch_i), ch_i]), [0, 1, 2, 4, 3]),
[-1, w, h, ch_i, ksizes[0], ksizes[1]])
if data_format == 'channels_last':
patches = KTF.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
return patches
def forward(self):
inp = self.inp.out
s = self.lay.stride
self.out = tf.extract_image_patches(
inp, [1,s,s,1], [1,s,s,1], [1,1,1,1], 'VALID')
def extract_image_patches(X, ksizes, ssizes, border_mode="same", dim_ordering="tf"):
'''
Extract the patches from an image
Parameters
----------
X : The input image
ksizes : 2-d tuple with the kernel size
ssizes : 2-d tuple with the strides size
border_mode : 'same' or 'valid'
dim_ordering : 'tf' or 'th'
Returns
-------
The (k_w,k_h) patches extracted
TF ==> (batch_size,w,h,k_w,k_h,c)
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
kernel = [1, ksizes[0], ksizes[1], 1]
strides = [1, ssizes[0], ssizes[1], 1]
padding = _preprocess_border_mode(border_mode)
if dim_ordering == "th":
X = KTF.permute_dimensions(X, (0, 2, 3, 1))
bs_i, w_i, h_i, ch_i = KTF.int_shape(X)
patches = tf.extract_image_patches(X, kernel, strides, [1, 1, 1, 1], padding)
# Reshaping to fit Theano
bs, w, h, ch = KTF.int_shape(patches)
patches = tf.reshape(tf.transpose(tf.reshape(patches, [bs, w, h, -1, ch_i]), [0, 1, 2, 4, 3]),
[bs, w, h, ch_i, ksizes[0], ksizes[1]])
if dim_ordering == "tf":
patches = KTF.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
return patches
def _forward(self, vs):
if self.local: # expand input patches and split by filters
input_local_expanded = tf.extract_image_patches(self.inpt,
pad_shape(self.ksize),
self.strides,
[1, 1, 1, 1],
padding=self.padding)
values = []
for filt in tf.split(axis=3, num_or_size_splits=self.n_filters, value=self.filters):
channel_i = tf.reduce_sum(tf.multiply(filt, input_local_expanded), 3,
keep_dims=True)
values.append(channel_i)
self.output = tf.concat(axis=3, values=values)
else: # split by images in batch and map to regular conv2d function
inpt = tf.expand_dims(self.inpt, 1)
filt_shape = [-1, self.ksize[0], self.ksize[1], self.n_cin, self.n_filters]
filt = tf.reshape(self.filters, filt_shape)
elems = (inpt, filt)
result = tf.map_fn(lambda x: tf.nn.conv2d(x[0], x[1],
self.strides,
self.padding), elems,
dtype=tf.float32, infer_shape=False)
result = tf.squeeze(result, [1])
result.set_shape(self.inpt.get_shape()[:-1].concatenate([self.n_filters]))
self.output = result
def _reorg(net, stride):
return tf.extract_image_patches(
net, [1, stride, stride, 1], [1, stride, stride, 1], [1, 1, 1, 1], 'VALID')
def _reorg(net, stride):
return tf.extract_image_patches(
net, [1, stride, stride, 1], [1, stride, stride, 1], [1, 1, 1, 1], 'VALID')
def test_replace_extract_image_patches(self):
with self.test_session() as sess:
net = tf.random_uniform([2, 26, 26, 128])
net1 = tf.extract_image_patches(
net, [1, 2, 2, 1], [1, 2, 2, 1], [1, 1, 1, 1], 'VALID')
print(net.get_shape().as_list())
from tensorflow.contrib import slim
net2 = slim.conv2d(net, 512, [1, 1], 2)
print(net.get_shape().as_list())
def forward(self):
inp = self.inp.out
s = self.lay.stride
self.out = tf.extract_image_patches(
inp, [1,s,s,1], [1,s,s,1], [1,1,1,1], 'VALID')
def extract_patches(self):
image_patches = tf.extract_image_patches(self.input_tensor, ksizes=[1, self.kernel_size,self.kernel_size, 1], strides=[1, self.stride_size,self.stride_size, 1], rates=[1, 1, 1, 1], padding=self.pad)
return tf.reshape(image_patches, [self.in_N, self.Hout,self.Wout, self.kernel_size,self.kernel_size, self.in_depth])
def extract_patches(self):
image_patches = tf.extract_image_patches(self.input_tensor, ksizes=[1, self.pool_size,self.pool_size, 1], strides=[1, self.stride_size,self.stride_size, 1], rates=[1, 1, 1, 1], padding=self.pad)
return tf.reshape(image_patches, [self.in_N, self.Hout,self.Wout, self.pool_size,self.pool_size, self.in_depth])
def extract_patches(self):
image_patches = tf.extract_image_patches(self.input_tensor, ksizes=[1, self.pool_size,self.pool_size, 1], strides=[1, self.stride_size,self.stride_size, 1], rates=[1, 1, 1, 1], padding=self.pad)
return tf.reshape(image_patches, [self.in_N, self.Hout,self.Wout, self.pool_size,self.pool_size, self.in_depth])
def forward(self):
inp = self.inp.out
s = self.lay.stride
self.out = tf.extract_image_patches(
inp, [1,s,s,1], [1,s,s,1], [1,1,1,1], 'VALID')
def pca_ii(ori_image):
# Resize particular size
ori_image = tf.stack(ori_image) # if dim 3 -> 4
ori_image = tf.expand_dims(ori_image, [0])
ori_image = tf.cast(ori_image, tf.float32)
ori_image += 1.0 # Prevent zero divide
# Extract uniform k sample
ksizes = [1, 1, 1, 1] # 1x1 pixel
strides = [1, 10, 10, 1] # 4
rates = [1, 1, 1, 1]
sample_pixel = tf.extract_image_patches(ori_image, ksizes, strides, rates, padding='VALID')
sample_pixel = tf.squeeze(sample_pixel, [0])
num_sample = sample_pixel.get_shape()[0].value * sample_pixel.get_shape()[1].value
sample_pixel = tf.reshape(sample_pixel, [num_sample, 1, 1, 3])
pixel_R = sample_pixel[:,0,0,0]
pixel_G = sample_pixel[:,0,0,1]
pixel_B = sample_pixel[:,0,0,2]
geoM = tf.pow(pixel_R * pixel_G * pixel_B, 1.0/3.0)
ch_r = tf.reshape(tf.log(pixel_R / geoM), [1, num_sample])
ch_b = tf.reshape(tf.log(pixel_B / geoM), [1, num_sample])
X = tf.concat([ch_r, ch_b], 0)
[U, S, V] = tf.svd(X)
vec = S[:,0]
alpha = tf.abs(tf.atan(-vec[0] / vec[1]))
tmp = tf.squeeze(ori_image, [0])
pixel_R = tmp[:,:,0]
pixel_G = tmp[:,:,1]
pixel_B = tmp[:,:,2]
geoM = tf.pow(pixel_R * pixel_G * pixel_B, 1.0/3.0)
num_pixel = ori_image.get_shape()[1].value * ori_image.get_shape()[2].value
ch_r = tf.log(pixel_R / geoM)
ch_b = tf.log(pixel_B / geoM)
ii_image = ch_r * tf.cos(alpha) + ch_b * tf.sin(alpha)
return ii_image, alpha