Skip to content

Commit eaf049c

Browse files
JiabinYangphlrain
authored andcommitted
test=develop, refine ocr attention model (#17763)
* test=develop, refine ocr attention model * test=develop, refine code, remove cpu only part test=develop, refine code, remove cpu only part
1 parent 5e4f99d commit eaf049c

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

python/paddle/fluid/tests/unittests/test_imperative_ocr_attention_model.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def __init__(self,
9090
3,
9191
padding=1,
9292
param_attr=conv_param_0,
93-
bias_attr=None,
93+
bias_attr=False,
9494
act=None,
9595
use_cudnn=use_cudnn)
9696
self.bn_0_layer = BatchNorm(
@@ -102,7 +102,7 @@ def __init__(self,
102102
filter_size=3,
103103
padding=1,
104104
param_attr=conv_param_1,
105-
bias_attr=None,
105+
bias_attr=False,
106106
act=None,
107107
use_cudnn=use_cudnn)
108108
self.bn_1_layer = BatchNorm(
@@ -301,7 +301,11 @@ def __init__(self, scope_name, decoder_size):
301301
decoder_size,
302302
act=None,
303303
bias_attr=False)
304-
self.fc_2 = FC(self.full_name(), 1, act=None, bias_attr=False)
304+
self.fc_2 = FC(self.full_name(),
305+
1,
306+
num_flatten_dims=2,
307+
act=None,
308+
bias_attr=False)
305309

306310
def _build_once(self, encoder_vec, encoder_proj, decoder_state):
307311
pass
@@ -317,19 +321,17 @@ def forward(self, encoder_vec, encoder_proj, decoder_state):
317321
decoder_state_expand)
318322
concated = fluid.layers.tanh(x=concated)
319323
attention_weight = self.fc_2(concated)
324+
320325
weights_reshape = fluid.layers.reshape(
321-
x=attention_weight, shape=[-1], inplace=False)
326+
x=attention_weight,
327+
shape=[attention_weight.shape[0], attention_weight.shape[1]],
328+
inplace=False)
329+
330+
weights_reshape = fluid.layers.softmax(weights_reshape)
322331
scaled = fluid.layers.elementwise_mul(
323332
x=encoder_vec, y=weights_reshape, axis=0)
324-
scaled = fluid.layers.transpose(scaled, [0, 2, 1])
325-
scaled = fluid.layers.reshape(
326-
scaled, [-1, scaled.shape[1], scaled.shape[2], 1], inplace=False)
327-
context = fluid.layers.pool2d(
328-
input=scaled,
329-
pool_size=[scaled.shape[2], scaled.shape[3]],
330-
pool_type='avg')
331-
context = fluid.layers.reshape(
332-
context, [-1, context.shape[1]], inplace=False)
333+
context = fluid.layers.reduce_sum(scaled, dim=1)
334+
333335
return context
334336

335337

@@ -381,7 +383,7 @@ def forward(self, target_embedding, encoder_vec, encoder_proj,
381383
out = self.out_layer(h)
382384
res.append(out)
383385

384-
res1 = fluid.layers.concat(res, axis=0)
386+
res1 = fluid.layers.concat(res, axis=1)
385387

386388
return res1
387389

@@ -427,7 +429,11 @@ class TestDygraphOCRAttention(unittest.TestCase):
427429
def test_while_op(self):
428430
seed = 90
429431
epoch_num = 2
430-
batch_num = 20
432+
if core.is_compiled_with_cuda():
433+
batch_num = 20
434+
else:
435+
print("in CPU")
436+
batch_num = 2
431437
np.random.seed = seed
432438
image_np = np.random.randn(Config.batch_size, Config.DATA_SHAPE[0],
433439
Config.DATA_SHAPE[1],
@@ -441,7 +447,6 @@ def test_while_op(self):
441447
i * Config.max_length,
442448
dtype='int64').reshape([1, Config.max_length])))
443449

444-
print(label_in_np.shape)
445450
label_out_np = np.arange(
446451
0, Config.max_length,
447452
dtype='int64').reshape([1, Config.max_length])
@@ -450,7 +455,6 @@ def test_while_op(self):
450455
(i - 1) * Config.max_length,
451456
i * Config.max_length,
452457
dtype='int64').reshape([1, Config.max_length])))
453-
print(label_out_np.shape)
454458
#if Config.use_gpu:
455459
# place = fluid.CUDAPlace(0)
456460
#else:
@@ -484,6 +488,8 @@ def test_while_op(self):
484488
dy_prediction = ocr_attention(img, label_in)
485489
label_out = fluid.layers.reshape(
486490
label_out, [-1, 1], inplace=False)
491+
dy_prediction = fluid.layers.reshape(
492+
dy_prediction, [label_out.shape[0], -1], inplace=False)
487493
loss = fluid.layers.cross_entropy(
488494
input=dy_prediction, label=label_out)
489495
avg_loss = fluid.layers.reduce_sum(loss)
@@ -536,6 +542,9 @@ def test_while_op(self):
536542

537543
static_prediction = ocr_attention(images, static_label_in)
538544

545+
static_prediction = fluid.layers.reshape(
546+
static_prediction, shape=[-1, Config.num_classes + 2])
547+
539548
cost = fluid.layers.cross_entropy(
540549
input=static_prediction, label=static_label_out)
541550
static_avg_loss = fluid.layers.reduce_sum(cost)
@@ -558,8 +567,6 @@ def test_while_op(self):
558567
static_param_init_value[static_param_name_list[i]] = out[i]
559568

560569
fetch_list = [static_avg_loss.name]
561-
# print(static_test.name)
562-
# fetch_list = [static_avg_loss.name, static_test.name]
563570
fetch_list.extend(static_param_name_list)
564571
fetch_list.extend(static_grad_name_list)
565572
for epoch in range(epoch_num):

0 commit comments

Comments
 (0)