@@ -90,7 +90,7 @@ def __init__(self,
90
90
3 ,
91
91
padding = 1 ,
92
92
param_attr = conv_param_0 ,
93
- bias_attr = None ,
93
+ bias_attr = False ,
94
94
act = None ,
95
95
use_cudnn = use_cudnn )
96
96
self .bn_0_layer = BatchNorm (
@@ -102,7 +102,7 @@ def __init__(self,
102
102
filter_size = 3 ,
103
103
padding = 1 ,
104
104
param_attr = conv_param_1 ,
105
- bias_attr = None ,
105
+ bias_attr = False ,
106
106
act = None ,
107
107
use_cudnn = use_cudnn )
108
108
self .bn_1_layer = BatchNorm (
@@ -301,7 +301,11 @@ def __init__(self, scope_name, decoder_size):
301
301
decoder_size ,
302
302
act = None ,
303
303
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 )
305
309
306
310
def _build_once (self , encoder_vec , encoder_proj , decoder_state ):
307
311
pass
@@ -317,19 +321,17 @@ def forward(self, encoder_vec, encoder_proj, decoder_state):
317
321
decoder_state_expand )
318
322
concated = fluid .layers .tanh (x = concated )
319
323
attention_weight = self .fc_2 (concated )
324
+
320
325
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 )
322
331
scaled = fluid .layers .elementwise_mul (
323
332
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
+
333
335
return context
334
336
335
337
@@ -381,7 +383,7 @@ def forward(self, target_embedding, encoder_vec, encoder_proj,
381
383
out = self .out_layer (h )
382
384
res .append (out )
383
385
384
- res1 = fluid .layers .concat (res , axis = 0 )
386
+ res1 = fluid .layers .concat (res , axis = 1 )
385
387
386
388
return res1
387
389
@@ -427,7 +429,11 @@ class TestDygraphOCRAttention(unittest.TestCase):
427
429
def test_while_op (self ):
428
430
seed = 90
429
431
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
431
437
np .random .seed = seed
432
438
image_np = np .random .randn (Config .batch_size , Config .DATA_SHAPE [0 ],
433
439
Config .DATA_SHAPE [1 ],
@@ -441,7 +447,6 @@ def test_while_op(self):
441
447
i * Config .max_length ,
442
448
dtype = 'int64' ).reshape ([1 , Config .max_length ])))
443
449
444
- print (label_in_np .shape )
445
450
label_out_np = np .arange (
446
451
0 , Config .max_length ,
447
452
dtype = 'int64' ).reshape ([1 , Config .max_length ])
@@ -450,7 +455,6 @@ def test_while_op(self):
450
455
(i - 1 ) * Config .max_length ,
451
456
i * Config .max_length ,
452
457
dtype = 'int64' ).reshape ([1 , Config .max_length ])))
453
- print (label_out_np .shape )
454
458
#if Config.use_gpu:
455
459
# place = fluid.CUDAPlace(0)
456
460
#else:
@@ -484,6 +488,8 @@ def test_while_op(self):
484
488
dy_prediction = ocr_attention (img , label_in )
485
489
label_out = fluid .layers .reshape (
486
490
label_out , [- 1 , 1 ], inplace = False )
491
+ dy_prediction = fluid .layers .reshape (
492
+ dy_prediction , [label_out .shape [0 ], - 1 ], inplace = False )
487
493
loss = fluid .layers .cross_entropy (
488
494
input = dy_prediction , label = label_out )
489
495
avg_loss = fluid .layers .reduce_sum (loss )
@@ -536,6 +542,9 @@ def test_while_op(self):
536
542
537
543
static_prediction = ocr_attention (images , static_label_in )
538
544
545
+ static_prediction = fluid .layers .reshape (
546
+ static_prediction , shape = [- 1 , Config .num_classes + 2 ])
547
+
539
548
cost = fluid .layers .cross_entropy (
540
549
input = static_prediction , label = static_label_out )
541
550
static_avg_loss = fluid .layers .reduce_sum (cost )
@@ -558,8 +567,6 @@ def test_while_op(self):
558
567
static_param_init_value [static_param_name_list [i ]] = out [i ]
559
568
560
569
fetch_list = [static_avg_loss .name ]
561
- # print(static_test.name)
562
- # fetch_list = [static_avg_loss.name, static_test.name]
563
570
fetch_list .extend (static_param_name_list )
564
571
fetch_list .extend (static_grad_name_list )
565
572
for epoch in range (epoch_num ):
0 commit comments