@ -16,23 +16,48 @@ import numpy as np
from op_test import OpTest
def get_output_shape ( attrs , in_shape ) :
def get_output_shape ( attrs , in_shape , img_real_size ) :
batchsize = in_shape [ 0 ]
img_height = in_shape [ 2 ]
img_width = in_shape [ 3 ]
paddings = np . array ( attrs [ ' paddings ' ] ) . astype ( " int32 " )
kernels = np . array ( attrs [ ' kernels ' ] ) . astype ( " int32 " )
strides = np . array ( attrs [ ' strides ' ] ) . astype ( " int32 " )
output_height = np . zeros ( ( 1 , batchsize ) ) . astype ( " int32 " )
output_width = np . zeros ( ( 1 , batchsize ) ) . astype ( " int32 " )
if len ( img_real_size ) :
out_stride = np . array ( attrs [ ' out_stride ' ] ) . astype ( " int32 " )
imgreal_h = 0
imgreal_w = 0
for index in range ( batchsize ) :
if img_real_size [ index , 0 ] % out_stride [ 0 ] == 0 :
imgreal_h = img_real_size [ index , 0 ] / out_stride [ 0 ]
else :
imgreal_h = img_real_size [ index , 0 ] / out_stride [ 0 ] + 1
if img_real_size [ index , 0 ] % out_stride [ 1 ] == 0 :
imgreal_w = img_real_size [ index , 1 ] / out_stride [ 1 ]
else :
imgreal_w = img_real_size [ index , 0 ] / out_stride [ 1 ] + 1
output_height [ 0 , index ] = \
1 + \
( imgreal_h + paddings [ 0 ] + paddings [ 2 ] - kernels [ 0 ] + strides [ 0 ] - 1 ) / \
strides [ 0 ]
paddings = attrs [ ' paddings ' ]
kernels = attrs [ ' kernels ' ]
strides = attrs [ ' strides ' ]
output_width [ 0 , index ] = \
1 + \
( imgreal_w + paddings [ 1 ] + paddings [ 3 ] - kernels [ 1 ] + strides [ 1 ] - 1 ) / \
strides [ 1 ]
else :
for index in range ( batchsize ) :
output_height [ 0 , index ] = \
1 + \
( img_height + paddings [ 0 ] + paddings [ 2 ] - kernels [ 0 ] + strides [ 0 ] - 1 ) / \
strides [ 0 ]
output_height = \
1 + \
( img_height + paddings [ 0 ] + paddings [ 2 ] - kernels [ 0 ] + strides [ 0 ] - 1 ) / \
strides [ 0 ]
output_width = \
1 + \
( img_width + paddings [ 1 ] + paddings [ 3 ] - kernels [ 1 ] + strides [ 1 ] - 1 ) / \
strides [ 1 ]
output_width [ 0 , index ] = \
1 + \
( img_width + paddings [ 1 ] + paddings [ 3 ] - kernels [ 1 ] + strides [ 1 ] - 1 ) / \
strides [ 1 ]
return output_height , output_width
@ -75,22 +100,25 @@ def im2col(attrs, im, col):
im_row_offset ] [ im_col_offset ]
def Im2Sequence ( inputs , attrs ) :
output_height , output_width = get_output_shape ( attrs , inputs . shape )
def Im2Sequence ( inputs , img_real_size , attrs ) :
output_height , output_width = get_output_shape ( attrs , inputs . shape ,
img_real_size )
img_channels = inputs . shape [ 1 ]
batch_size = inputs . shape [ 0 ]
out = np . zeros ( [
batch_size , output_height , output_width , img_channels ,
attrs [ ' kernels ' ] [ 0 ] , attrs [ ' kernels ' ] [ 1 ]
] ) . astype ( " float32 " )
for i in range ( len ( inputs ) ) :
im2col ( attrs , inputs [ i ] , out [ i ] )
out = out . reshape ( [
batch_size * output_height * output_width ,
img_channels * attrs [ ' kernels ' ] [ 0 ] * attrs [ ' kernels ' ] [ 1 ]
] )
out = [ ]
for index in range ( batch_size ) :
tmp = np . zeros ( [
output_height [ 0 , index ] , output_width [ 0 , index ] , img_channels ,
attrs [ ' kernels ' ] [ 0 ] , attrs [ ' kernels ' ] [ 1 ]
] ) . astype ( " float32 " )
out . append ( tmp )
for index in range ( len ( inputs ) ) :
im2col ( attrs , inputs [ index ] , out [ index ] )
out [ index ] = out [ index ] . reshape ( [
output_height [ 0 , index ] * output_width [ 0 , index ] ,
img_channels * attrs [ ' kernels ' ] [ 0 ] * attrs [ ' kernels ' ] [ 1 ]
] )
out = np . concatenate ( out , axis = 0 )
return out
@ -103,7 +131,7 @@ class TestBlockExpandOp(OpTest):
self . attrs = {
' kernels ' : [ 2 , 2 ] ,
' strides ' : [ 1 , 1 ] ,
' paddings ' : [ 1 , 1 , 1 , 1 ]
' paddings ' : [ 1 , 1 , 1 , 1 ] ,
}
def setUp ( self ) :
@ -113,7 +141,8 @@ class TestBlockExpandOp(OpTest):
self . batch_size , self . img_channels , self . img_height , self . img_width
] ) . astype ( " float32 " )
out = Im2Sequence ( x , self . attrs )
real_size = np . array ( [ ] ) . astype ( " float32 " )
out = Im2Sequence ( x , real_size , self . attrs )
self . inputs = { ' X ' : x }
self . outputs = { ' Out ' : out }
@ -133,20 +162,20 @@ class TestBlockExpandOpCase2(TestBlockExpandOp):
self . attrs = {
' kernels ' : [ 2 , 1 ] ,
' strides ' : [ 2 , 1 ] ,
' paddings ' : [ 2 , 1 , 2 , 1 ]
' paddings ' : [ 2 , 1 , 2 , 1 ] ,
}
class TestBlockExpandOpCase3 ( TestBlockExpandOp ) :
def config ( self ) :
self . batch_size = 3
self . batch_size = 2
self . img_channels = 1
self . img_height = 4
self . img_width = 5
self . attrs = {
' kernels ' : [ 2 , 1 ] ,
' strides ' : [ 2 , 1 ] ,
' paddings ' : [ 2 , 0 , 2 , 0 ]
' paddings ' : [ 2 , 0 , 2 , 0 ] ,
}
@ -159,9 +188,94 @@ class TestBlockExpandOpCase4(TestBlockExpandOp):
self . attrs = {
' kernels ' : [ 2 , 2 ] ,
' strides ' : [ 1 , 1 ] ,
' paddings ' : [ 0 , 0 , 0 , 0 ]
' paddings ' : [ 0 , 0 , 0 , 0 ] ,
}
class TestBlockExpandOpCase5 ( OpTest ) :
def config ( self ) :
self . batch_size = 1
self . img_channels = 3
self . img_height = 4
self . img_width = 5
self . attrs = {
' kernels ' : [ 2 , 1 ] ,
' strides ' : [ 2 , 1 ] ,
' paddings ' : [ 2 , 1 , 2 , 1 ] ,
' out_stride ' : [ 2 , 2 ] ,
}
def setUp ( self ) :
self . config ( )
self . op_type = " im2sequence "
x = np . random . uniform ( 0.1 , 1 , [
self . batch_size , self . img_channels , self . img_height , self . img_width
] ) . astype ( " float32 " )
real_size = np . array ( [ [ 8 , 10 ] , [ 5 , 8 ] ] ) . astype ( " float32 " )
out = np . array ( Im2Sequence ( x , real_size , self . attrs ) )
self . inputs = { ' X ' : x , ' Y ' : real_size } #l ??
self . outputs = { ' Out ' : out }
def test_check_output ( self ) :
self . check_output ( )
class TestBlockExpandOpCase6 ( OpTest ) :
def config ( self ) :
self . batch_size = 3
self . img_channels = 1
self . img_height = 4
self . img_width = 5
self . attrs = {
' kernels ' : [ 2 , 1 ] ,
' strides ' : [ 1 , 1 ] ,
' paddings ' : [ 0 , 0 , 0 , 0 ] ,
' out_stride ' : [ 1 , 1 ] ,
}
def setUp ( self ) :
self . config ( )
self . op_type = " im2sequence "
x = np . random . uniform ( 0.1 , 1 , [
self . batch_size , self . img_channels , self . img_height , self . img_width
] ) . astype ( " float32 " )
real_size = np . array ( [ [ 8 , 10 ] , [ 5 , 8 ] , [ 5 , 8 ] ] ) . astype ( " float32 " )
out = np . array ( Im2Sequence ( x , real_size , self . attrs ) )
self . inputs = { ' X ' : x , ' Y ' : real_size } #l ??
self . outputs = { ' Out ' : out }
def test_check_output ( self ) :
self . check_output ( )
class TestBlockExpandOpCase7 ( OpTest ) :
def config ( self ) :
self . batch_size = 2
self . img_channels = 2
self . img_height = 3
self . img_width = 3
self . attrs = {
' kernels ' : [ 2 , 2 ] ,
' strides ' : [ 1 , 1 ] ,
' paddings ' : [ 1 , 0 , 1 , 0 ] ,
' out_stride ' : [ 2 , 2 ] ,
}
def setUp ( self ) :
self . config ( )
self . op_type = " im2sequence "
x = np . random . uniform ( 0.1 , 1 , [
self . batch_size , self . img_channels , self . img_height , self . img_width
] ) . astype ( " float32 " )
real_size = np . array ( [ [ 6 , 6 ] , [ 4 , 4 ] ] ) . astype ( " float32 " )
out = np . array ( Im2Sequence ( x , real_size , self . attrs ) )
self . inputs = { ' X ' : x , ' Y ' : real_size }
self . outputs = { ' Out ' : out }
def test_check_output ( self ) :
self . check_output ( )
if __name__ == ' __main__ ' :
unittest . main ( )
#set shiftwidth=4 set expandtab set tabstop=4