# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 O C R _ C N N _ T r a i n i n g . p y 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 
 
 i m p o r t   n u m p y   a s   n p 
 
 i m p o r t   c v 2 
 
 i m p o r t   o s 
 
 f r o m   s k l e a r n . m o d e l _ s e l e c t i o n   i m p o r t   t r a i n _ t e s t _ s p l i t 
 
 i m p o r t   m a t p l o t l i b . p y p l o t   a s   p l t 
 
 f r o m   k e r a s . p r e p r o c e s s i n g . i m a g e   i m p o r t   I m a g e D a t a G e n e r a t o r 
 
 f r o m   k e r a s . u t i l s . n p _ u t i l s   i m p o r t   t o _ c a t e g o r i c a l 
 
 f r o m   k e r a s . m o d e l s   i m p o r t   S e q u e n t i a l 
 
 f r o m   k e r a s . l a y e r s   i m p o r t   D e n s e 
 
 f r o m   k e r a s . o p t i m i z e r s   i m p o r t   A d a m 
 
 f o r m   k e r a s . l a y e r s   i m p o r t   D r o p o u t ,   F l a t t e n 
 
 f r o m   k e r a s . l a y e r s . c o n v o l u t i o n a l   i m p o r t   C o n v 2 D , M a x P o o l i n g 2 D 
 
 
 
 i m p o r t   p i c k l e 
 
 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 p a t h   =   ' m y D a t a ' 
 
 p a t h L a b e l s   =   ' l a b e l s . c s v ' 
 
 t e s t R a t i o   =   0 . 2 
 
 v a R a t i o   =   0 . 2 
 
 i m a g e D i m e n s i o n s   =   ( 3 2 , 3 2 , 3 ) 
 
 
 
 b a t c h s i z e V a l u e   =   5 0 
 
 e p o c h s i z e V a l u e   =   1 0 
 
 s t e p P e r E p o c h   =   2 0 0 0 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 m y L i s t   =   o s . l i s t d i r ( p a t h ) 
 
 i m a g e s   =   [ ] 
 
 c l a s s N o   =   [ ] 
 
 p r i n t ( m y L i s t ) 
 
 p r i n t ( l e n ( m y L i s t ) ) 
 
 n o O f C l a s s e s   =   l e n ( m y L i s t ) 
 
 f o r   x   i n   r a n g e ( 0 , n o O f C l a s s e s ) : 
 
 	 m y P i c L i s t   =   o s . l i s t d i r ( p a t h + " / " + s t r ( x ) ) 
 
 	 f o r   y   i n   m y P i c L i s t : 
 
 	 	 c u r I m 
 
 g   =   c v 2 . i m r e a d ( p a t h + " / " + s t r ( x ) + " / " + y ) 
 
 	 	 c u r I m g   =   c v 2 . r e s i z e ( c u r I m g , ( i m a g e D i m e n s i o n s [ 0 ] , i m a g e D i m e n s i o n s [ 1 ] ) ) 
 
 	 	 i m a g e s . a p p e n d ( c u r I m g ) 
 
 	 	 c l a s s N o . a p p e n d ( x ) 
 
 	 p r i n t ( x , e n d = "   " ) 
 
 p r i n t ( l e n ( i m a g e s ) )   # T o n g   s o   I m a g e   t r o n g   m y D a t a 
 
 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 #   x u   l y   d u   l i e u   i m a g e s   t h a n h   k i e u   n u m p y 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 i m a g e s   =   n p . a r r a y ( i m a g e s ) 
 
 c l a s s N o   =   n p . a r r a y ( c l a s s N o ) 
 
 p r i n t ( i m a g e s . s h a p e )   # - - - - - - - - - - > o u t p u t   ( 1 0 1 6 0 , 3 2 , 3 2 , 3 ) 
 
 p r i n t ( c l a s s N o . s h a p e ) # - - - - - - - - - - > o u t p u t   ( 1 0 1 6 0 , ) 
 
 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 #                                   S P L I T   D A T A                                           # 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 X _ t r a i n , X _ t e s t ,   y _ t r a i n , y _ t e s t   =   t r a i n _ t e s t _ s p l i t ( i m a g e s , c l a s s N o , t e s t _ s i z e = t e s t R a t i o ) 
 
 X _ t r a i n , X _ v a l i d a t i o n , y _ t r a i n , y _ v a l i d a t i o n   =   t r a i n _ t e s t _ s p l i t ( X _ t r a i n , y _ t r a i n , t e s t _ s i z e = v a R a t i o ) 
 
 
 
 
 
 p r i n t ( X _ t r a i n . s h a p e )   # - - - - - - - - - > ( 8 1 2 8 , 3 2 , 3 2 , 3 ) 
 
 p r i n t ( X _ t e s t . s h a p e )     # - - - - - - - - - > ( 2 0 3 2 , 3 2 , 3 2 , 3 ) 
 
 p r i n t ( X _ v a l i d a t i o n . s h a p e ) 
 
 p r i n t ( n p . w h e r e ( y _ t r a i n = = 0 ) ) 
 
 n u m O f S a m p l e s   =   [ ] 
 
 f o r   x   i n   r a n g e ( 0 , n o O f C l a s s e s ) : 
 
 	 p r i n t ( l e n ( n p . w h e r e ( y _ t r a i n = = x ) [ 0 ] ) ) 
 
 	 n u m O f S a m p l e s . a p p e n d ( l e n ( n p . w h e r e ( y _ t r a i n = = x ) [ 0 ] ) ) 
 
 p r i n t ( n u m O f S a m p l e s ) 
 
 p l t . f i g u r e ( ( f i g s i z e = ( 1 0 , 5 ) ) 
 
 p l t . b a r ( r a n g e ( 0 , n o O f C l a s s e s ) , n u m O f S a m p l e s ) 
 
 p l t . t i t l e ( " N o   o f   I m a g e s   f o r   e a c h   C l a s s " ) 
 
 p l t . x l a b e l ( " C l a s s   I D " ) 
 
 p l t . y l a b e l ( " N u m b e r   o f   I m a g e s " ) 
 
 p l t . s h o w ( ) 
 
 
 
 d e f   p r e P r o c e s s i n g ( i m g ) : 
 
 	 i m g   =   c v 2 . c v t C o l o r ( i m g , c v 2 . C O L O R _ B G R 2 G R A Y ) 
 
 	 i m g   =   c v 2 . e q u a l i z e H i s t ( i m g ) 
 
 	 i m g   =   i m g / 2 5 5 
 
 	 r e t u r n   i m g 
 
 X _ t r a i n   =   n p . a r r a y ( l i s t ( m a p ( p r e P r o c e s s i n g ,   X _ t r a i n ) ) 
 
 X _ t e s t   =   n p . a r r a y ( l i s t ( m a p ( p r e P r o c e s s i n g ,   X _ t e s t ) ) 
 
 X _ v a l i d a t i o n   =   n p . a r r a y ( l i s t ( m a p ( p r e P r o c e s s i n g ,   X _ v a l i d a t i o n ) ) 
 
 # i m g   =   X _ t r a i n [ 3 0 ] 
 
 # i m g   =   c v 2 . r e s i z e ( i m g , ( 3 0 0 , 3 0 0 ) ) 
 
 # c v 2 . i m s h o w ( " P r o c e s s e d " , i m g ) 
 
 # c v 2 . w a i K e y ( 0 ) 
 
 
 
 X _ t r a i n   =   X _ t r a i n . r e s h a p e ( X _ t r a i n . s h a p e [ 0 ] , X _ t r a i n . s h a p e [ 1 ] , X _ t r a i n . s h a p e [ 2 ] , 1 ) 
 
 X _ t e s t   =   X _ t e s t . r e s h a p e ( X _ t e s t . s h a p e [ 0 ] , X _ t e s t . s h a p e [ 1 ] , X _ t e s t . s h a p e [ 2 ] , 1 ) 
 
 X _ v a l i d a t i o n   =   X _ v a l i d a t i o n . r e s h a p e ( X _ v a l i d a t i o n . s h a p e [ 0 ] , X _ v a l i d a t i o n . s h a p e [ 1 ] , X _ v a l i d a t i o n . s h a p e [ 2 ] , 1 ) 
 
 
 
 
 
 d a t a G e n   =   I m a g e D a t a G e n e r a t o r ( w i d t h _ s h i f t _ r a n g e = 0 . 1 , 
 
 	 	 	           h e i g h t _ s h i f t _ r a n g e = 0 . 1 , 
 
                                                           z o o m _ r a n g e = 0 . 2 , 
 
                                                           s h e a r _ r a n g e = 0 . 1 , 
 
                                                           r o t a t i o n _ r a n g e = 1 0 ) 
 
 
 
 d a t a G e n . f i t ( X _ t r a i n ) 
 
 y _ t r a i n   =   t o _ c a t e g o r i c a l ( y _ t r a i n , n o O f C l a s s e s ) 
 
 y _ t e s t   =   t o _ c a t e g o r i c a l ( y _ t e s t , n o O f C l a s s e s ) 
 
 y _ v a l i d a t i o n   =   t o _ c a t e g o r i c a l ( y _ v a l i d a t i o n , n o O f C l a s s e s ) 
 
 
 
 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 #                           C r e a t e   M o d e l                                                           # 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 d e f   m y M o d e l ( ) : 
 
 	 n o O f F i l t e r s   =   6 0 
 
 	 s i z e O f F i l t e r s 1   = ( 5 , 5 ) 
 
 	 a i z e O f F i l t e r s 2   = ( 3 , 3 ) 
 
 	 s i z e O f P o o l   =   ( 2 , 2 ) 
 
 	 n o O f N o d e   =   5 0 0 
 
 	 m o d e l   =   S e q u e n t i a l ( ) 
 
 	 m o d e l . a d d ( ( C o n v 2 D ( n o O f F i l t e r s , s i z e O f F i l t e r s 1 , i n p u t _ s h a p e = ( i m a g e D i m e n s i o n s [ 0 ] , 
 
                                                                                                                                     i m a g e D i m e n s i o n s [ 1 ] , 1 ) , a c t i v a t i o n = ' R e l u ' 
 
                                                                                                                                     ) ) ) 
 
 
 
 	 m o d e l . a d d ( ( C o n v 2 D ( n o O f F i l t e r s , s i z e O f F i l t e r s 1 , a c t i v a t i o n = ' r e l u ' ) ) ) 
 
 	 m o d e l . a d d ( M a x P o o l i n g 2 D ( p o o l s i z e = s i z e O f P o o l ) ) 
 
 	 m o d e l . a d d ( ( C o n v 2 D ( n o O f F i l t e r s / / 2 , s i z e O f F i l t e r s 2 , a c t i v a t i o n = ' r e l u ' ) ) ) 
 
 	 m o d e l . a d d ( ( C o n v 2 D ( n o O f F i l t e r s / / 2 , s i z e O f F i l t e r s 2 , a c t i v a t i o n = ' r e l u ' ) ) ) 
 
 	 m o d e l . a d d ( M a x P o o l i n g 2 D ( p o o l s i z e = s i z e O f P o o l ) ) 
 
 	 m o d e l . a d d ( D r o p O u t ( 0 . 5 ) ) 
 
 	 
 
 	 m o d e l . a d d ( F l a t t e n ( ) ) 
 
 	 m o d e l . a d d ( D e n s e ( n o O f N o d e , a c t i v a t i o n = ' r e l u ' ) ) 
 
 	 m o d e l . a d d ( D r o p O u t ( 0 . 5 ) ) 
 
 
 
 	 m o d e l . a d d ( D e n s e ( n o O f C l a s s e d , a c t i v a t i o n = ' s o f t m a x ' ) ) 
 
 	 m o d e l . c o m p i l e ( A d a m ( l r = 0 . 0 0 1 ) , l o s s = ' c a t e g o r i c a l _ c r o s s e n t r o p y ' , m e t r i c s = [ ' a c c u r a c y ' ] ) 
 
 	 
 
 	 r e t u r n   m o d e l 
 
 m o d e l   =   m y M o d e l ( ) 
 
 p r i n t ( m o d e l . s u m m a r y ( ) ) 
 
 h i s t o r y   =   m o d e l . f i t _ g e n e r a t o r ( d a t a G e n . f l o w ( X _ t r a i n , y _ t r a i n , b a t c h _ s i z e = b a t c h s i z e V a l u e ) , 
 
                                                                                                               s t e p s _ p e r _ e p o c h = s t e p P e r E p o c h ,   
 
                                                                                                               e p o c h s = e p o c h s i z e V a l u e , 
 
                                                                                                               v a l i d a t i o n _ d a t a = ( X _ v a l i d a t i o n , y _ v a l i d a t i o n ) , 
 
                                                                                                               s h u f f l e   = 1 ) 
 
 
 
 p l t . f i g u r e ( 1 ) 
 
 p l t . p l o t ( h i s t o r y . h i s t o r y [ ' l o s s ' ] ) 
 
 p l t . p l o t ( h i s t o r y . h i s t o r y [ ' v a l _ l o s s ' ] ) 
 
 p l t . l e g e n d ( [ ' t r a i n i n g ' , ' v a l i d a t i o n ' ] ) 
 
 p l t . t i t l e ( ' L o s s ' ) 
 
 p l t . x l a b e l ( ' e p o c h ' ) 
 
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
 
 p l t . f i g u r e ( 2 ) 
 
 p l t . p l o t ( h i s t o r y . h i s t o r y [ ' a c c u r a c y ' ] ) 
 
 p l t . p l o t ( h i s t o r y . h i s t o r y [ ' v a l _ a c c u r a c y ' ] ) 
 
 p l t . l e g e n d ( [ ' t r a i n i n g ' , ' v a l i d a t i o n ' ] ) 
 
 p l t . t i t l e ( ' A c c u r a c y ' ) 
 
 p l t . x l a b e l ( ' e p o c h ' ) 
 
 p l t . s h o w ( ) 
 
 
 
 s c o r e   =   m o d e l . e v a l u a t e ( X _ t e s t , y _ t e s t , v e r b o s e = 0 ) 
 
 p r i n t ( ' T e s t   S c o r e   =   ' , s c o r e [ 0 ] ) 
 
 p r i n t ( ' T e s t   A c c u r a c y   =   ' ,   s c o r e [ 1 ] ) 
 
 
 
 p i c k l e _ o u t   =   o p e n ( " m o d e l _ t r a i n e d . p " , " w b " ) 
 
 p i c k l e . d u m p ( m o d e l . p i c k l e _ o u t ) 
 
 p i c k l e _ o u t . c l o s e ( ) 
 
 
 
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - T H E   E N D   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 	 	 
 
 
 
 
 
 
 
 
 
 