% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
 
 %   L L M - b a s e d   m u l t i - a g e n t   s y s t e m   % 
 
 % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
 
 - - - - - - - - - - - - - - - - I n i t i a l   S e t t i n g s - - - - - - - - - - - - - - - 
 
 P r o j e c t   D e s c r i p t i o n :   D e v e l o p   a n   A I - b a s e d   i m a g e   p r o c e s s i n g   a n d   c l a s s i f i c a t i o n   s y s t e m   t h a t   c o m p l i e s   w i t h   t h e   E U   A I   A c t ,   e n s u r i n g   a c c u r a t e   a n d   r e s p e c t f u l   l a b e l i n g   o f   i m a g e s .   T h e   s y s t e m   m u s t   a v o i d   r a c i a l   b i a s e s   a n d   h a r m f u l   m i s c l a s s i f i c a t i o n s .   T h e   A I   E t h i c s   S p e c i a l i s t   w i l l   f o c u s   o n   i d e n t i f y i n g   a n d   m i t i g a t i n g   p s y c h o l o g i c a l   h a r m   a n d   h a r m   t o   s o c i a l   s y s t e m s .   T h e   s e n i o r   P y t h o n   d e v e l o p e r s   w i l l   i m p l e m e n t   a n d   r e f i n e   i m a g e   c l a s s i f i c a t i o n   a l g o r i t h m s . 
 
 N u m b e r   o f   a g e n t s :   3 
 
 L o a d i n g   p r o m p t   f o r :   a g e n t 1 
 
 L o a d i n g   p r o m p t   f o r :   a g e n t 2 
 
 L o a d i n g   p r o m p t   f o r :   a g e n t 3 
 
 N u m b e r   o f   R o u n d s :   5 
 
 - - - - - - - - - - - - - - - - S t a r t - - - - - - - - - - - - - - - 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 1   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 A s   t h e   f i r s t   s t e p   i n   o u r   p r o j e c t ,   I   w i l l   p r o p o s e   a n   i n i t i a l   c o d e   s t r u c t u r e   f o r   t h e   A I - b a s e d   i m a g e   p r o c e s s i n g   a n d   c l a s s i f i c a t i o n   s y s t e m .   T h i s   c o d e   w i l l   i n c l u d e   a   b a s i c   f r a m e w o r k   f o r   l o a d i n g   i m a g e s   a n d   a p p l y i n g   a   s i m p l e   c l a s s i f i c a t i o n   a l g o r i t h m .   F o r   n o w ,   I ' l l   u s e   a   p l a c e h o l d e r   m o d e l ,   w h i c h   w e   c a n   l a t e r   r e p l a c e   w i t h   a   m o r e   s o p h i s t i c a t e d   a l g o r i t h m   t h a t   c o m p l i e s   w i t h   t h e   E U   A I   A c t . 
 
 
 
 R e f l e c t i o n : 
 
 W e   n e e d   t o   e n s u r e   t h a t   o u r   c o d e b a s e   a d h e r e s   t o   c l e a n   c o d i n g   p r i n c i p l e s ,   f o l l o w s   S O L I D   d e s i g n   p a t t e r n s ,   a n d   i s   s t r u c t u r e d   w e l l   t o   a c c o m m o d a t e   f u t u r e   e n h a n c e m e n t s ,   i n c l u d i n g   t h e   i m p l e m e n t a t i o n   o f   b i a s   m i t i g a t i o n   s t r a t e g i e s . 
 
 
 
 C o d e : 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   K N e i g h b o r s C l a s s i f i e r ( )     #   P l a c e h o l d e r   f o r   f u t u r e   m o d e l s 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                         i f   f i l e n a m e . e n d s w i t h ( ' . j p g ' )   o r   f i l e n a m e . e n d s w i t h ( ' . p n g ' ) : 
 
                                 i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                 i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                 i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) )     #   R e s i z e   f o r   u n i f o r m i t y 
 
                                 s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                 #   P l a c e h o l d e r   f o r   c o r r e c t   l a b e l i n g 
 
                                 s e l f . l a b e l s . a p p e n d ( s e l f . g e t _ l a b e l ( f i l e n a m e ) ) 
 
                 s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                 s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
         
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 #   T h i s   m e t h o d   s h o u l d   r e t r i e v e   t h e   l a b e l   b a s e d   o n   f i l e n a m e 
 
                 #   F o r   n o w ,   w e ' l l   a s s u m e   l a b e l s   a r e   e n c o d e d   i n   t h e   f i l e n a m e 
 
                 r e t u r n   f i l e n a m e . s p l i t ( ' _ ' ) [ 0 ]     #   E x a m p l e :   c l a s s _ n a m e _ 0 0 1 . j p g   - >   c l a s s _ n a m e 
 
                 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 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 ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   s e l f . l a b e l s ,   t e s t _ s i z e = 0 . 2 ,   r a n d o m _ s t a t e = 4 2 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 p r i n t ( c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) ) 
 
         
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                 i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                 i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                 r e t u r n   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` I m a g e C l a s s i f i e r `   c l a s s   l a c k s   d e p e n d e n c y   i n j e c t i o n .   W e   s h o u l d   a l l o w   i n j e c t i n g   t h e   m o d e l   t o   f o l l o w   t h e   D e p e n d e n c y   I n v e r s i o n   P r i n c i p l e . 
 
 -   T h e   ` l o a d _ i m a g e s `   m e t h o d   s h o u l d   h a v e   s o m e   e r r o r   h a n d l i n g ,   p a r t i c u l a r l y   w h e n   l o a d i n g   i m a g e s   o r   i n v a l i d   f i l e s . 
 
 -   T h e   ` g e t _ l a b e l `   f u n c t i o n   i s   c u r r e n t l y   a   p l a c e h o l d e r   a n d   d o e s   n o t   e n s u r e   a c c u r a t e   l a b e l i n g ,   w h i c h   i s   c r i t i c a l   f o r   c o m p l i a n c e   w i t h   t h e   E U   A I   A c t ;   t h i s   w i l l   n e e d   t o   b e   i m p l e m e n t e d   i n   l i n e   w i t h   e t h i c a l   c o n s i d e r a t i o n s . 
 
 -   W e   n e e d   t o   c o n s i d e r   a d d i n g   a   l o g g i n g   m e c h a n i s m   t o   t r a c k   t h e   w o r k l o a d s   a n d   o u t p u t s ,   e s p e c i a l l y   t h e   d e c i s i o n s   m a d e   r e g a r d i n g   c l a s s i f i e r   i m p a c t s   a n d   b i a s e s . 
 
 -   I t ' s   e s s e n t i a l   t o   c o n s i d e r   i m p l e m e n t i n g   s o m e   f o r m   o f   p r e p r o c e s s i n g   ( n o r m a l i z a t i o n ,   a u g m e n t a t i o n )   t o   p r e p a r e   i m a g e s   a d e q u a t e l y   f o r   c l a s s i f i c a t i o n .   
 
 
 
 I ' l l   a w a i t   f e e d b a c k   f r o m   A g e n t s _ 2   a n d   3   o n   t h i s   i n i t i a l   c o d e . 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 2   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   2   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1 ,   f o r   p r o v i d i n g   t h e   i n i t i a l   c o d e   s t r u c t u r e   a n d   y o u r   t h o u g h t f u l   c r i t i q u e .   I   a p p r e c i a t e   t h e   e m p h a s i s   o n   c l e a n   c o d e   p r i n c i p l e s   a n d   a d h e r e n c e   t o   r e g u l a t i o n s .   L e t ' s   e x p l o r e   w a y s   t o   e n h a n c e   t h i s   i m p l e m e n t a t i o n . 
 
 
 
 R e f l e c t i o n : 
 
 A s   w e   r e f i n e   t h i s   c o d e ,   w e   n e e d   t o   e n s u r e   i t   s e g u e s   s m o o t h l y   i n t o   f u r t h e r   d e v e l o p m e n t s .   K e y   a r e a s   o f   f o c u s   s h o u l d   i n c l u d e   p r o p e r   l a b e l i n g ,   e r r o r   h a n d l i n g ,   l o g g i n g ,   a n d   m o d e l   e v a l u a t i o n   s t r a t e g i e s   t h a t   s u p p o r t   e t h i c a l   A I   u s e .   S o m e   o f   t h e s e   a r e a s   a r e   c r i t i c a l   f o r   c o m p l i a n c e   w i t h   t h e   E U   A I   A c t . 
 
 
 
 C o d e : 
 
 H e r e  s   a   r e v i s e d   v e r s i o n   o f   t h e   c o d e ,   a d d r e s s i n g   s o m e   o f   t h e   c r i t i q u e s   l i s t e d   a n d   a d d i n g   m o r e   s t r u c t u r e : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( " L o a d e d   i m a g e s   a n d   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
         
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e   #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
         
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 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 ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   s e l f . l a b e l s ,   t e s t _ s i z e = 0 . 2 ,   r a n d o m _ s t a t e = 4 2 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 p r i n t ( c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) ) 
 
         
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         r e t u r n   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   m o d e l   i n s t a n t i a t i o n   i s   n o w   o p t i o n a l ,   a l l o w i n g   f o r   e a s i e r   e x p e r i m e n t a t i o n   w i t h   d i f f e r e n t   a l g o r i t h m s   t h r o u g h   d e p e n d e n c y   i n j e c t i o n . 
 
 -   A d d e d   l o g g i n g   t o   m o n i t o r   t h e   f l o w   o f   t h e   p r o g r a m   a n d   t o   c a t c h   p o t e n t i a l   i s s u e s   d u r i n g   i m a g e   p r o c e s s i n g . 
 
 -   E n h a n c e d   l a b e l   e x t r a c t i o n   t o   p r e v e n t   m i s s i n g   l a b e l s ,   w h i c h   c a n   h a p p e n   i f   f i l e n a m e s   a r e n ' t   f o r m a t t e d   c o r r e c t l y . 
 
 -   I n c l u d e d   e r r o r   h a n d l i n g   w h i l e   l o a d i n g   i m a g e s   a n d   d u r i n g   c l a s s i f i c a t i o n   t o   e n s u r e   t h a t   o u r   s y s t e m   i s   r o b u s t   a n d   c a n   h a n d l e   u n e x p e c t e d   i n p u t s . 
 
 -   I t ' s   s t i l l   c r u c i a l   t o   i m p l e m e n t   b i a s   m i t i g a t i o n   i n   t h e   l a b e l i n g   p r o c e s s .   W e   s h o u l d   c o n s i d e r   a   s t r a t e g y   t h a t   a l i g n s   w i t h   e t h i c a l   A I   g u i d e l i n e s   a n d   e n h a n c e s   m o d e l   f a i r n e s s . 
 
     
 
 I   l o o k   f o r w a r d   t o   y o u r   i n s i g h t s ,   A g e n t s   1   a n d   3 . 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 3   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   3   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1   a n d   A g e n t _ 2 ,   f o r   t h e   c o n s t r u c t i v e   d i a l o g u e   o n   t h e   i n i t i a l   c o d e   s t r u c t u r e .   I t ' s   c l e a r   t h a t   b o t h   o f   y o u   h a v e   p r i o r i t i z e d   c l e a n   c o d i n g   p r a c t i c e s   a n d   h a v e   m a d e   v a l u a b l e   e n h a n c e m e n t s   t o   o u r   i m p l e m e n t a t i o n .   H o w e v e r ,   a s   a   S e n i o r   A I   E t h i c i s t ,   I   w o u l d   l i k e   t o   e m p h a s i z e   t h e   i m p o r t a n c e   o f   c o n s i d e r i n g   e t h i c a l   i m p l i c a t i o n s   i n   e v e r y   s t a g e   o f   o u r   d e v e l o p m e n t ,   e s p e c i a l l y   g i v e n   t h e   s e n s i t i v e   n a t u r e   o f   i m a g e   c l a s s i f i c a t i o n   s y s t e m s . 
 
 
 
 R e f l e c t i o n : 
 
 O u r   f o c u s   m u s t   e x t e n d   b e y o n d   f u n c t i o n a l   c o r r e c t n e s s   a n d   r o b u s t n e s s ;   w e   s h o u l d   a l s o   a c t i v e l y   m i t i g a t e   b i a s e s   a n d   h a r m s   a s s o c i a t e d   w i t h   o u r   A I   s y s t e m   c o n c e r n i n g   r a c i a l ,   c u l t u r a l ,   a n d   s o c i a l   s e n s i t i v i t i e s .   T o   c o m p l y   w i t h   t h e   E U   A I   A c t ,   i t   i s   i m p e r a t i v e   t o   e n s u r e   o u r   d a t a s e t   i s   d i v e r s e   a n d   r e p r e s e n t a t i v e ,   a n d   t h a t   w e   i m p l e m e n t   f a i r   l a b e l i n g   p r a c t i c e s .   L e t ' s   a l s o   i n t r o d u c e   s y s t e m a t i c   p r o c e s s e s   t o   e v a l u a t e   t h e   i m p a c t   o f   o u r   m o d e l   o n   d i f f e r e n t   d e m o g r a p h i c s . 
 
 
 
 C o d e : 
 
 H e r e   a r e   s o m e   a d d i t i o n a l   r e f i n e m e n t s   b a s e d   o n   t h e   o n g o i n g   d i s c u s s i o n : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( " L o a d e d   i m a g e s   a n d   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e   #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 u n i q u e _ l a b e l s   =   s e t ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " U n i q u e   l a b e l s   i d e n t i f i e d :   { u n i q u e _ l a b e l s } " ) 
 
                 #   I m p l e m e n t   l o g i c   f o r   e v a l u a t i n g   d a t a s e t   d i v e r s i t y 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   s e l f . l a b e l s ,   t e s t _ s i z e = 0 . 2 ,   r a n d o m _ s t a t e = 4 2 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 p r i n t ( c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         r e t u r n   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   I   i n t r o d u c e d   a   m e t h o d ,   ` e n s u r e _ d i v e r s i t y ` ,   w h i c h   c h e c k s   f o r   t h e   d i v e r s i t y   o f   t h e   d a t a s e t   b a s e d   o n   u n i q u e   l a b e l s .   T h i s   a p p r o a c h   a l l o w s   u s   t o   r e f l e c t   o n   w h e t h e r   w e   a r e   u s i n g   a   b a l a n c e d   r e p r e s e n t a t i o n   o f   v a r i o u s   d e m o g r a p h i c s ,   w h i c h   i s   c r u c i a l   f o r   e t h i c a l   A I   c o n s i d e r a t i o n s . 
 
 -   W e   m u s t   f u r t h e r   d e v e l o p   t h e   ` e n s u r e _ d i v e r s i t y `   f u n c t i o n   t o   i n c l u d e   c r i t e r i a   f o r   m e a s u r i n g   d i v e r s i t y   a n d   p o s s i b l y   m e t h o d s   f o r   a u g m e n t i n g   t h e   d a t a s e t   w h e n   b i a s e s   a r e   d e t e c t e d . 
 
 -   W h i l e   e x i s t i n g   e r r o r   h a n d l i n g   a n d   l o g g i n g   m e c h a n i s m s   a r e   b e n e f i c i a l ,   w e   s h o u l d   a l s o   c o n s i d e r   d o c u m e n t i n g   t h e   e t h i c a l   i m p l i c a t i o n s   o f   o u r   m o d e l  s   p r e d i c t i o n s ,   s u c h   a s   p o t e n t i a l   b i a s e s   i n   c l a s s i f i c a t i o n   r e s u l t s ,   a n d   e x p l o r e   w a y s   t o   c o m m u n i c a t e   t h e s e   r i s k s . 
 
 
 
 C o n t i n u e d   c o l l a b o r a t i o n   i s   e s s e n t i a l   t o   e n s u r e   t h a t   o u r   s y s t e m   d e s i g n   n o t   o n l y   m e e t s   t e c h n i c a l   s t a n d a r d s   b u t   a l s o   a l i g n s   w i t h   e t h i c a l   p r i n c i p l e s   i n   A I .   I   l o o k   f o r w a r d   t o   y o u r   t h o u g h t s ,   A g e n t s   1   a n d   2 . 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 1   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   4   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 2   a n d   A g e n t _ 3 ,   f o r   y o u r   c o n s t r u c t i v e   u p d a t e s   r e g a r d i n g   t h e   c o d e   s t r u c t u r e   a n d   t h e   e t h i c a l   c o n s i d e r a t i o n s   t h a t   m u s t   b e   p r i o r i t i z e d   i n   o u r   p r o j e c t .   Y o u r   i n s i g h t s   a r e   i n v a l u a b l e   a s   w e   w o r k   t o w a r d   b u i l d i n g   a n   e t h i c a l l y   s o u n d   c l a s s i f i c a t i o n   s y s t e m . 
 
 
 
 R e f l e c t i o n : 
 
 I t ' s   c r u c i a l   t h a t   w e   d o n ' t   j u s t   b u i l d   a n   i m a g e   p r o c e s s i n g   a n d   c l a s s i f i c a t i o n   s y s t e m ;   w e   m u s t   e n s u r e   t h a t   i t   r e s p e c t s   t h e   n u a n c e s   a n d   c o m p l e x i t i e s   o f   r e p r e s e n t a t i o n ,   e s p e c i a l l y   w h e n   d e a l i n g   w i t h   s e n s i t i v e   d a t a .   I   a p p r e c i a t e   A g e n t _ 3 ' s   f o c u s   o n   d i v e r s i t y   a n d   d o c u m e n t a t i o n   o f   t h e   e t h i c a l   i m p l i c a t i o n s ,   w h i c h   w i l l   h e l p   o u r   s y s t e m   c o m p l y   w i t h   t h e   E U   A I   A c t . 
 
 
 
 C o d e : 
 
 B e l o w ,   I ' v e   m a d e   s o m e   e n h a n c e m e n t s   t o   t h e   ` e n s u r e _ d i v e r s i t y `   m e t h o d ,   a l o n g   w i t h   r e f a c t o r i n g   t o   e n s u r e   c l a r i t y   a n d   c o n s i s t e n c y .   L e t ' s   a l s o   i n t e g r a t e   s o m e   e x c e p t i o n   h a n d l i n g   a n d   i m p r o v e   t h e   l o g g i n g   s c o p e   f o r   b e t t e r   t r a c e a b i l i t y . 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
                 #   A d d   l o g i c   h e r e   f o r   c h e c k i n g   a n d   a d d r e s s i n g   r e p r e s e n t a t i o n   b i a s 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   E n h a n c e d   ` e n s u r e _ d i v e r s i t y `   n o w   u s e s   ` C o u n t e r `   f r o m   t h e   c o l l e c t i o n s   m o d u l e   t o   p r o v i d e   a   c l e a r e r   d i s t r i b u t i o n   o f   l a b e l s   i n   o u r   d a t a s e t . 
 
 -   I m p r o v e d   l o g g i n g   m e s s a g e s   h a v e   b e e n   a d d e d   t o   p r o v i d e   m o r e   c o m p r e h e n s i v e   i n f o r m a t i o n   a b o u t   l o a d i n g   i m a g e s   a n d   t r a i n i n g   t h e   m o d e l . 
 
 -   S t i l l   p e n d i n g   i m p l e m e n t a t i o n   i s   t h e   s t r a t e g y   t o   h a n d l e   p o t e n t i a l   d i v e r s i t y   i s s u e s   w i t h i n   t h e   d a t a s e t ,   i n c l u d i n g   d a t a   a u g m e n t a t i o n   a n d   b a l a n c e d   s a m p l i n g   w h e n   b i a s   i s   d e t e c t e d . 
 
 
 
 A s   w e   c o n t i n u e   r e f i n i n g   t h i s   c o d e ,   I   e n c o u r a g e   s u s t a i n e d   d i a l o g u e s   o n   e t h i c a l   i m p l i c a t i o n s   a n d   p o s s i b l e   m e t h o d s   f o r   b i a s   m i t i g a t i o n .   T h e   i n t e r s e c t i o n   o f   A I   a n d   e t h i c s   i s   p a r a m o u n t   i n   o u r   f i e l d ,   a n d   w e   h a v e   a   r e s p o n s i b i l i t y   t o   s e t   h i g h   s t a n d a r d s .   L o o k i n g   f o r w a r d   t o   y o u r   i n p u t ,   A g e n t s   2   a n d   3 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 2   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   5   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1   a n d   A g e n t _ 3 ,   f o r   y o u r   i n s i g h t f u l   c o n t r i b u t i o n s   a n d   c o n t i n u e d   e m p h a s i s   o n   e t h i c a l   c o n s i d e r a t i o n s   i n   o u r   A I - b a s e d   i m a g e   p r o c e s s i n g   s y s t e m .   T h e   e n h a n c e m e n t s   y o u  v e   m a d e   c l e a r l y   i n d i c a t e   o u r   g r o w i n g   f o c u s   o n   d i v e r s i t y   a n d   r e l i a b i l i t y .   I   c o m m e n d   y o u   f o r   p r i o r i t i z i n g   c o m p r e h e n s i v e   e r r o r   l o g g i n g   a n d   c l e a r   r e p o r t i n g ,   w h i c h   i s   c r i t i c a l   f o r   m a i n t a i n i n g   a n   a c c o u n t a b l e   s y s t e m . 
 
 
 
 R e f l e c t i o n : 
 
 T o   b u i l d   o n   o u r   c u r r e n t   f r a m e w o r k ,   i t   i s   e s s e n t i a l   t o   f u r t h e r   o u r   f o c u s   o n   d a t a   b i a s e s   a n d   a d d r e s s   p o t e n t i a l   e t h i c a l   c o n c e r n s   d i r e c t l y   t i e d   t o   m o d e l   a c c u r a c y   a n d   f a i r n e s s .   T h e   ` e n s u r e _ d i v e r s i t y `   m e t h o d   l a y s   a n   e x c e l l e n t   g r o u n d w o r k ,   b u t   w e   s h o u l d   i n t e g r a t e   d i r e c t   a c t i o n s   t h a t   c a n   b e   t a k e n   s h o u l d   o u r   a n a l y s i s   r e v e a l   i m b a l a n c e s   i n   r e p r e s e n t a t i o n . 
 
 
 
 C o d e : 
 
 I   p r o p o s e   t h e   f o l l o w i n g   u p d a t e s   t o   i n c l u d e   a   m o r e   c o m p r e h e n s i v e   a p p r o a c h   t o   m e a s u r i n g   a n d   a d d r e s s i n g   d a t a s e t   d i v e r s i t y ,   a s   w e l l   a s   a   b a s i c   i m p l e m e n t a t i o n   o f   s a m p l e   a u g m e n t a t i o n   i f   r e p r e s e n t a t i o n   i s s u e s   a r e   d e t e c t e d : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 #   C h e c k   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2     #   B a s e   l i m i t   f o r   u n d e r - r e p r e s e n t e d   l a b e l s 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 " " "   S i m p l e   d a t a   a u g m e n t a t i o n   u s i n g   r e s a m p l i n g   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s .   " " " 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         #   C r e a t e   n e w   s a m p l e s   u s i n g   r e s a m p l i n g 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` e n s u r e _ d i v e r s i t y `   m e t h o d   n o w   i n c o r p o r a t e s   a   t h r e s h o l d - b a s e d   s t r a t e g y   f o r   d e t e c t i n g   u n d e r - r e p r e s e n t e d   c l a s s e s ,   p r o v i d i n g   i n s i g h t   a n d   a c t i o n a b l e   f e e d b a c k   f o r   o u r   d a t a s e t ' s   b a l a n c e . 
 
 -   A d d e d   a n   ` a u g m e n t _ d a t a `   m e t h o d ,   w h i c h   e m p l o y s   r e s a m p l i n g   t o   a r t i f i c i a l l y   b o o s t   t h e   d a t a   r e p r e s e n t a t i o n   o f   u n d e r - r e p r e s e n t e d   c l a s s e s .   T h i s   i s   a   b a s i c   a p p r o a c h   t o   a d d r e s s   p o t e n t i a l   b i a s   i n   t h e   d a t a s e t . 
 
 -   T h i s   i m p l e m e n t a t i o n   r e l i e s   o n   s i m p l i s t i c   a u g m e n t a t i o n ;   w e   m a y   w a n t   t o   c o n s i d e r   t e c h n i q u e s   s u c h   a s   r o t a t i o n ,   f l i p p i n g ,   o r   m o r e   s o p h i s t i c a t e d   m e t h o d s   t o   g e n e r a t e   s y n t h e t i c   d a t a   i n   s u b s e q u e n t   i t e r a t i o n s . 
 
 -   F u r t h e r   d i s c o u r s e   o n   e t h i c a l   i m p l i c a t i o n s   o f   o u r   a u g m e n t a t i o n s   a n d   r e g u l a r   a u d i t s   o f   m o d e l   o u t c o m e s   s h o u l d   b e   p r i o r i t i z e d ,   e n s u r i n g   w e   r e m a i n   a c c o u n t a b l e   f o r   t h e   s y s t e m ' s   r e a l - w o r l d   i m p a c t . 
 
 
 
 I n   c o n c l u s i o n ,   o u r   c o l l a b o r a t i v e   e f f o r t   i s   p i v o t a l   i n   c r a f t i n g   a n   e t h i c a l   a n d   r e s p o n s i b l e   A I   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   I   l o o k   f o r w a r d   t o   h e a r i n g   y o u r   t h o u g h t s ,   A g e n t s   1   a n d   3 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 3   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   6   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   1   a n d   2 ,   f o r   y o u r   i n s i g h t f u l   a d j u s t m e n t s   a n d   e n h a n c e m e n t s .   I   c o m m e n d   t h e   p r o g r e s s i o n   t o w a r d   a   m o r e   e t h i c a l l y   r o b u s t   s y s t e m .   T h e   f o c u s   o n   d i v e r s i t y   a n d   b i a s   m i t i g a t i o n   i s   c r u c i a l ,   a n d   y o u r   r e c e n t   m o d i f i c a t i o n s   r e f l e c t   a   s t r o n g   u n d e r s t a n d i n g   o f   t h e s e   c o n s i d e r a t i o n s . 
 
 
 
 R e f l e c t i o n : 
 
 A s   w e   c o n t i n u e   r e f i n i n g   o u r   A I - b a s e d   i m a g e   c l a s s i f i c a t i o n   s y s t e m ,   w e   m u s t   r e m a i n   v i g i l a n t   a b o u t   n o t   o n l y   t e c h n i c a l   a s p e c t s   b u t   a l s o   e t h i c a l   i m p l i c a t i o n s .   W h i l e   t h e   ` a u g m e n t _ d a t a `   f u n c t i o n   i n t r o d u c e s   p r o m i s i n g   s t e p s   t o w a r d   m i t i g a t i n g   r e p r e s e n t a t i o n   b i a s ,   w e   n e e d   t o   e n s u r e   t h a t   t h e   a p p r o a c h e s   w e   i m p l e m e n t   d o   n o t   i n a d v e r t e n t l y   l e a d   t o   o t h e r   f o r m s   o f   b i a s   o r   r e i n f o r c e   e x i s t i n g   s t e r e o t y p e s .   S y s t e m a t i c   a u d i t s   a n d   t e s t i n g   o n   d i v e r s e   s e t s   w i l l   b e   n e c e s s a r y   t o   v a l i d a t e   o u r   a p p r o a c h . 
 
 
 
 C o d e : 
 
 H e r e   i s   a   r e f i n e d   v e r s i o n   o f   t h e   o n g o i n g   i m p l e m e n t a t i o n   t h a t   i n c l u d e s   s o m e   a d d i t i o n a l   c o n s i d e r a t i o n s   f o r   e t h i c a l   a u g m e n t a t i o n   p r a c t i c e s : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ) : 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 #   A d d   l o g i c   h e r e   t o   t e s t   a u g m e n t e d   d a t a   f o r   u n i n t e n d e d   c o n s e q u e n c e s   o r   b i a s e s 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   m e t h o d   h a s   b e e n   i n t r o d u c e d ,   w h i c h   p r o m p t s   f o r   t h e   i m p l e m e n t a t i o n   o f   a   c r i t i c a l   e v a l u a t i o n   o f   t h e   a u g m e n t e d   d a t a s e t   t o   p r e v e n t   b i a s e s .   T h i s   s t e p   i s   e s s e n t i a l   t o   g a u g e   t h e   e f f e c t i v e n e s s   a n d   e t h i c a l   i m p l i c a t i o n s   o f   o u r   a u g m e n t a t i o n   s t r a t e g y . 
 
 -   T h e   c u r r e n t   i m p l e m e n t a t i o n   r e l i e s   o n   s i m p l i s t i c   d a t a   a u g m e n t a t i o n   t e c h n i q u e s   v i a   r e s a m p l i n g .   F u t u r e   a d v a n c e m e n t s   m a y   i n v o l v e   i n t e g r a t i n g   a d v a n c e d   a u g m e n t a t i o n   t e c h n i q u e s ,   s u c h   a s   g e o m e t r i c   t r a n s f o r m a t i o n s   o r   g e n e r a t i v e   a d v e r s a r i a l   n e t w o r k s   ( G A N s ) ,   w h i l e   e n s u r i n g   t h e s e   t e c h n i q u e s   a r e   c o n s i s t e n t   w i t h   e t h i c a l   g u i d e l i n e s . 
 
 -   W e   m u s t   e s t a b l i s h   c o m p r e h e n s i v e   p e r f o r m a n c e   m o n i t o r i n g   a n d   a u d i t i n g   o n c e   t h e   m o d e l   i s   d e p l o y e d ,   a s s e s s i n g   b o t h   c l a s s i f i c a t i o n   a c c u r a c y   a n d   e t h i c a l   o u t c o m e s   r e l a t e d   t o   v a r i o u s   d e m o g r a p h i c   f e a t u r e s . 
 
 
 
 I n   c o n c l u s i o n ,   o u r   c o l l a b o r a t i v e   d i a l o g u e   i s   p i v o t a l   i n   a l i g n i n g   t e c h n i c a l   d e v e l o p m e n t   w i t h   e t h i c a l   A I   p r a c t i c e .   Y o u r   e f f o r t s   i n   e s t a b l i s h i n g   a n   e t h i c a l l y   s o u n d   s y s t e m   a r e   c o m m e n d a b l e ,   a n d   I   l o o k   f o r w a r d   t o   f u r t h e r   r e f i n i n g   t h i s   p r o j e c t   w i t h   e v e r y o n e ' s   i n s i g h t s ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 1   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   7   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   2   a n d   3 ,   f o r   y o u r   d i l i g e n t   r e f i n e m e n t s   a n d   f o r   e m p h a s i z i n g   t h e   e t h i c a l   i m p l i c a t i o n s   o f   o u r   w o r k .   I t   i s   c l e a r   t h a t   o u r   f o c u s   n o t   o n l y   o n   t e c h n i c a l   e f f i c a c y   b u t   a l s o   o n   t h e   m o r a l   r e s p o n s i b i l i t y   e m b e d d e d   i n   A I   s y s t e m s   w i l l   h e l p   i n   d e v e l o p i n g   a   r e s p o n s i b l e   i m a g e   c l a s s i f i c a t i o n   m o d e l . 
 
 
 
 R e f l e c t i o n : 
 
 I   a g r e e   t h a t   o u r   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   f u n c t i o n   i s   e s s e n t i a l   f o r   m o n i t o r i n g   p o t e n t i a l   b i a s e s   t h a t   m a y   a r i s e   f r o m   o u r   c u r r e n t   d a t a   a u g m e n t a t i o n   m e t h o d s .   T h i s   r e f l e c t s   o u r   c o m m i t m e n t   t o   e n s u r i n g   t h a t   o u r   s y s t e m   r e m a i n s   f a i r   a n d   c o m p l i a n t   w i t h   e t h i c a l   g u i d e l i n e s . 
 
 
 
 C o d e : 
 
 I   p r o p o s e   e n h a n c e m e n t s   t o   t h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   m e t h o d   t o   i n c l u d e   s t a t i s t i c a l   e v a l u a t i o n   o f   t h e   a u g m e n t e d   s a m p l e s   a g a i n s t   t h e   o r i g i n a l   d a t a s e t .   B y   m e a s u r i n g   t h e   d i s t r i b u t i o n   o f   l a b e l s   b e f o r e   a n d   a f t e r   a u g m e n t a t i o n ,   w e   c a n   b e t t e r   u n d e r s t a n d   w h e t h e r   t h e   a u g m e n t a t i o n   i n t r o d u c e d   a n y   u n i n t e n d e d   b i a s e s : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
                 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         i f   ( a u g m e n t e d _ c o u n t s [ l a b e l ]   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) )   <   0 . 1 :     #   E x a m p l e   t h r e s h o l d 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   f u n c t i o n   n o w   c o m p a r e s   t h e   d i s t r i b u t i o n   o f   l a b e l s   t o   d e t e r m i n e   i f   a n y   l a b e l s   r e m a i n   u n d e r - r e p r e s e n t e d   d e s p i t e   a u g m e n t a t i o n . 
 
 -   A   p r o p o r t i o n a l   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t a t i o n   ( i n   t h i s   c a s e ,   1 0 % )   h a s   b e e n   i n t r o d u c e d   a s   a n   e x a m p l e ;   t h i s   c a n   b e   r e f i n e d   t o   m e e t   s p e c i f i c   e t h i c a l   s t a n d a r d s . 
 
 -   N e x t   s t e p s   m i g h t   i n c l u d e   f o r m u l a t i n g   a   m o r e   r o b u s t   s t r a t e g y   f o r   e t h i c a l   a u g m e n t a t i o n ,   s u c h   a s   v a r y i n g   a u g m e n t a t i o n   a l g o r i t h m s   a f t e r   e v a l u a t i n g   t h e i r   i m p a c t   o n   m o d e l   f a i r n e s s . 
 
 
 
 A s   w e   p r o g r e s s ,   I   p r o p o s e   t h a t   w e   s e t   u p   a   f o r m a l   r e v i e w   p r o c e s s ,   d o c u m e n t i n g   o u r   a p p r o a c h e s   a n d   e v o l v i n g   s t r a t e g i e s   t o   e n s u r e   e t h i c a l   c o m p l i a n c e   c o n t i n u o u s l y .   I   l o o k   f o r w a r d   t o   y o u r   r e s p o n s e s ,   A g e n t s   2   a n d   3 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 2   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   8   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1 ,   f o r   c o n t i n u i n g   t o   r e f i n e   o u r   c o d e   a n d   e m p h a s i z i n g   t h e   i m p o r t a n c e   o f   e v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o s s i b l e   b i a s e s .   Y o u r   m o d i f i c a t i o n s   a r e   w e l l - a l i g n e d   w i t h   o u r   g o a l   o f   b u i l d i n g   a   r e s p o n s i b l e   A I   i m a g e   c l a s s i f i c a t i o n   s y s t e m . 
 
 
 
 R e f l e c t i o n : 
 
 T h e   r e c e n t   u p d a t e   t o   t h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   m e t h o d   p o s i t i o n s   u s   t o   b e t t e r   g a u g e   t h e   e q u i t a b l e   d i s t r i b u t i o n   o f   c l a s s e s   p o s t - a u g m e n t a t i o n .   T h i s   a p p r o a c h   i s   f u n d a m e n t a l   i n   e n s u r i n g   t h a t   o u r   m o d e l   i s   f a i r   a n d   d o e s   n o t   r e i n f o r c e   h a r m f u l   b i a s e s .   A s   w e   f i n e - t u n e   o u r   e t h i c a l   f r a m e w o r k ,   w e   m u s t   a d o p t   s t r a t e g i e s   t h a t   i n c o r p o r a t e   a   b r o a d e r   p e r s p e c t i v e   o n   f a i r n e s s   a n d   a c c o u n t a b i l i t y   i n   A I . 
 
 
 
 C o d e : 
 
 I   w o u l d   l i k e   t o   e x t e n d   t h e   c a p a b i l i t i e s   o f   o u r   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   m e t h o d   f u r t h e r   b y   i m p l e m e n t i n g   a   w a y   t o   v i s u a l i z e   t h e   d i s t r i b u t i o n   o f   c l a s s e s   u s i n g   a   b a r   p l o t .   T h i s   c a n   h e l p   u s   c o m m u n i c a t e   t h e   s t a t e   o f   o u r   d a t a s e t   v i s u a l l y ,   t h u s   m a k i n g   o u r   e t h i c a l   a s s e s s m e n t s   e a s i e r   t o   u n d e r s t a n d : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
                 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         i f   ( a u g m e n t e d _ c o u n t s [ l a b e l ]   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) )   <   0 . 1 : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n `   m e t h o d   h a s   b e e n   i n t r o d u c e d   f o r   a   g r a p h i c a l   r e p r e s e n t a t i o n   o f   l a b e l   c o u n t s   b e f o r e   a n d   a f t e r   a u g m e n t a t i o n .   T h i s   c a n   h e l p   s t a k e h o l d e r s   q u i c k l y   a s s e s s   t h e   i m p a c t   o f   o u r   a u g m e n t a t i o n   e f f o r t s . 
 
 -   V i s u a l i z a t i o n   c a n   e f f e c t i v e l y   c o m m u n i c a t e   t h e   s t a t e   o f   m o d e l   t r a i n i n g   a n d   v a l i d a t i o n   p r o c e s s e s ,   e n h a n c i n g   t r a n s p a r e n c y   a n d   a c c o u n t a b i l i t y   i n   A I   d e v e l o p m e n t . 
 
 -   A s   w e   i m p l e m e n t   t h i s ,   w e   s h o u l d   e n s u r e   t h a t   o u r   p l o t t i n g   l i b r a r i e s   a r e   c o m p a t i b l e   w i t h   t h e   d e p l o y m e n t   e n v i r o n m e n t   i f   w e ' r e   i n t e n d i n g   t o   u s e   t h i s   i n   p r o d u c t i o n   o r   f u r t h e r   c o l l a b o r a t i o n   s e t t i n g s   w i t h   v i s u a l   m o n i t o r i n g   n e e d s . 
 
 
 
 L o o k i n g   f o r w a r d   t o   y o u r   f e e d b a c k   a n d   a n y   a d d i t i o n a l   i d e a s   o n   m a i n t a i n i n g   e t h i c a l   p r a c t i c e s   a n d   i m p r o v i n g   o u r   i m p l e m e n t a t i o n   f u r t h e r ,   A g e n t s   1   a n d   3 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 3   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   9   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   1   a n d   2 ,   f o r   y o u r   c o n t i n u e d   c o m m i t m e n t   t o   e n h a n c i n g   b o t h   t h e   f u n c t i o n a l i t y   a n d   e t h i c a l   i n t e g r i t y   o f   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   T h e   i n c o r p o r a t i o n   o f   d a t a   v i s u a l i z a t i o n   f o r   l a b e l   d i s t r i b u t i o n   i s   a   p o w e r f u l   t o o l   t o   f a c i l i t a t e   t r a n s p a r e n c y   i n   o u r   p r o c e s s e s ,   a n d   i t   f u r t h e r   s t r e n g t h e n s   o u r   g r o u n d w o r k   f o r   e t h i c a l   A I   d e v e l o p m e n t . 
 
 
 
 R e f l e c t i o n : 
 
 V i s u a l i z i n g   l a b e l   d i s t r i b u t i o n s   n o t   o n l y   a l l o w s   u s   t o   a s s e s s   o u r   d a t a   q u a l i t y   b u t   a l s o   h e l p s   i n   e f f e c t i v e l y   c o m m u n i c a t i n g   t h e   w o r k i n g s   o f   o u r   m a c h i n e   l e a r n i n g   m o d e l s   t o   s t a k e h o l d e r s ,   w h i c h   i s   a   c r u c i a l   p a r t   o f   e t h i c a l   a c c o u n t a b i l i t y .   A s   w e   p r o c e e d ,   i t  s   v i t a l   t o   e n s u r e   t h a t   t h i s   t o o l   i s   u t i l i z e d   t o   a i d   d e c i s i o n s   r e l a t e d   t o   b i a s   m i t i g a t i o n   a n d   r e p r e s e n t a t i o n . 
 
 
 
 C o d e : 
 
 H e r e   a r e   f u r t h e r   e n h a n c e m e n t s   t o   o u r   c o d e   b a s e d   o n   o u r   d i s c u s s i o n s ,   i n c l u d i n g   i n t e g r a t i o n   o f   ` m a t p l o t l i b `   v i s u a l i z a t i o n s   w h i l e   e n s u r i n g   c l a r i t y   i n   o u r   c l a s s i f i c a t i o n ,   a n d   a d d i n g   a   c h e c k   t o   s e e   i f   a u g m e n t i n g   d a t a   a c t u a l l y   i n t r o d u c e s   p o s i t i v e   r e p r e s e n t a t i o n   c h a n g e s : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         i f   ( a u g m e n t e d _ c o u n t s [ l a b e l ]   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) )   <   0 . 1 : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   e n h a n c e m e n t   i n   t h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   n o w   i n d i c a t e s   i f   t h e r e   h a s   b e e n   a   p o s i t i v e   c h a n g e   i n   r e p r e s e n t a t i o n   p o s t - a u g m e n t a t i o n ,   w h i c h   c a n   b e   c r u c i a l   f o r   e t h i c a l   a s s e s s m e n t s . 
 
 -   C o n t i n u e d   e m p h a s i s   o n   t r a n s p a r e n c y   v i a   v i s u a l i z a t i o n s   n o t   o n l y   s u p p o r t s   s t a k e h o l d e r   c o m m u n i c a t i o n   b u t   a l s o   e n h a n c e s   u n d e r s t a n d i n g   o f   t h e   p o t e n t i a l   i m p a c t s   o f   o u r   d a t a   p r a c t i c e s . 
 
 -   A s   w e   f u r t h e r   r e f i n e   o u r   l a b e l i n g   a n d   a u g m e n t a t i o n   p r a c t i c e s ,   w e   s h o u l d   c o n t i n u a l l y   s c r u t i n i z e   w h e t h e r   o u r   m e t h o d s   i n a d v e r t e n t l y   i n t r o d u c e   o t h e r   b i a s e s   o r   r e i n f o r c e   s t e r e o t y p e s .   
 
 
 
 Y o u r   i n s i g h t s   c o n t i n u e   t o   b e   i n v a l u a b l e   a s   w e   n a v i g a t e   t h e   c o m p l e x i t i e s   o f   r e s p o n s i b l e   A I   d e v e l o p m e n t   t o g e t h e r .   I   l o o k   f o r w a r d   t o   y o u r   t h o u g h t s ,   A g e n t s   1   a n d   2 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 1   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 0   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   2   a n d   3 ,   f o r   y o u r   r e c e n t   c o n t r i b u t i o n s   a n d   o n g o i n g   d e d i c a t i o n   t o   e t h i c a l   A I   p r a c t i c e s   i n   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   Y o u r   e m p h a s i s   o n   a s s e s s i n g   a n d   v i s u a l i z i n g   c l a s s   r e p r e s e n t a t i o n   e n h a n c e s   o u r   f r a m e w o r k   s i g n i f i c a n t l y . 
 
 
 
 R e f l e c t i o n : 
 
 I t   i s   v i t a l   t h a t   w e   c o n t i n u a l l y   e v a l u a t e   t h e   e f f e c t i v e n e s s   o f   o u r   a u g m e n t a t i o n   t e c h n i q u e s   a n d   e n s u r e   t h a t   t h e y   s u p p o r t   o u r   c o m m i t m e n t   t o   a c c u r a c y   a n d   f a i r n e s s .   E s t a b l i s h i n g   b e n c h m a r k s   f o r   r e p r e s e n t a t i o n   c h a n g e s   p r o v i d e s   a c c o u n t a b i l i t y   i n   o u r   p r o c e s s e s ,   w h i c h   i s   c r u c i a l   f o r   m e e t i n g   e t h i c a l   s t a n d a r d s . 
 
 
 
 C o d e : 
 
 T o   f u r t h e r   b o l s t e r   o u r   i m p l e m e n t a t i o n ,   I   p r o p o s e   a d d i n g   s p e c i f i c   m e t r i c s   t o   q u a n t i f y   r e p r e s e n t a t i o n   c h a n g e s   i n   a d d i t i o n   t o   l o g g i n g   t h e   c h a n g e s ,   t h u s   i n t r o d u c i n g   a   m o r e   f o r m a l i z e d   a p p r o a c h   t o   o u r   p e r f o r m a n c e   m e t r i c s .   T h i s   c o u l d   h e l p   i n   n o t   o n l y   i m p r o v i n g   o u r   p r o c e s s e s   b u t   a l s o   i n   r e p o r t i n g   f o r   c o m p l i a n c e   w i t h   e t h i c a l   s t a n d a r d s : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   0 . 1 : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                         #   L o g   t h e   c h a n g e s   i n   p r o p o r t i o n s 
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   n o w   l o g s   d e t a i l e d   m e t r i c s   o n   t h e   p r o p o r t i o n s   o f   e a c h   l a b e l   b e f o r e   a n d   a f t e r   a u g m e n t a t i o n ,   a l l o w i n g   f o r   a   c l e a r e r   a s s e s s m e n t   o f   a n y   c h a n g e s   r e s u l t i n g   f r o m   o u r   d a t a   p r o c e s s i n g . 
 
 -   T h i s   p r o v i d e s   a n   a d d i t i o n a l   l a y e r   o f   a c c o u n t a b i l i t y ,   e n a b l i n g   f u r t h e r   e v a l u a t i o n   o f   o u r   a u g m e n t a t i o n   p r a c t i c e s   a g a i n s t   e t h i c a l   b e n c h m a r k s . 
 
 -   F u t u r e   i t e r a t i o n s   c o u l d   e x p l o r e   s e t t i n g   u p   f o r m a l   t e s t s   o n   t h e   m o d e l  s   p r e d i c t i o n s   t o   v a l i d a t e   e t h i c a l   c o m p l i a n c e   c o n t i n u o u s l y . 
 
 
 
 A s   w e   m o v e   f o r w a r d ,   I   e n c o u r a g e   o n g o i n g   d i s c u s s i o n s   f o c u s e d   o n   i m p r o v i n g   t h e   e t h i c a l   i n t e g r i t y   o f   o u r   A I   s y s t e m .   Y o u r   i n s i g h t s   a r e   i n v a l u a b l e   a s   w e   w o r k   t o g e t h e r   t o   r e a l i z e   a   f a i r   a n d   r e s p o n s i b l e   A I   i m a g e   c l a s s i f i c a t i o n   f r a m e w o r k .   L o o k i n g   f o r w a r d   t o   y o u r   t h o u g h t s ,   A g e n t s   2   a n d   3 ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 2   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 1   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1 ,   f o r   y o u r   t h o u g h t f u l   e n h a n c e m e n t s   a n d   c o n t i n u e d   f o c u s   o n   t h e   e t h i c a l   i m p l i c a t i o n s   o f   o u r   s y s t e m .   T h e   i n t e g r a t i o n   o f   s p e c i f i c   m e t r i c s   f o r   q u a n t i f y i n g   r e p r e s e n t a t i o n   c h a n g e s   s t r e n g t h e n s   o u r   c o m m i t m e n t   t o   a c c o u n t a b i l i t y   i n   t h e   f a c e   o f   b i a s   m i t i g a t i o n   e f f o r t s .   
 
 
 
 R e f l e c t i o n : 
 
 M e t r i c s - b a s e d   e v a l u a t i o n s   a r e   e s s e n t i a l   f o r   v a l i d a t i n g   t h a t   o u r   a u g m e n t a t i o n   s t r a t e g i e s   g e n u i n e l y   p r o m o t e   d i v e r s i t y   a n d   f a i r n e s s .   T h e   c l e a r   l o g g i n g   o f   p r o p o r t i o n s   b e f o r e   a n d   a f t e r   a u g m e n t a t i o n   s e r v e s   n o t   o n l y   a s   d o c u m e n t a t i o n   f o r   o u r   p r o c e s s e s   b u t   a l s o   a s   a   m e a n s   t o   c o m m u n i c a t e   o u r   e t h i c a l   a p p r o a c h   t o   s t a k e h o l d e r s .   W e   s h o u l d   a l s o   c o n s i d e r   d e f i n i n g   m e a s u r a b l e   b e n c h m a r k s   f o r   s u c c e s s   i n   f u t u r e   i t e r a t i o n s . 
 
 
 
 C o d e : 
 
 I   p r o p o s e   t h a t   w e   i n t r o d u c e   a   m o r e   f o r m a l i z e d   b e n c h m a r k   s y s t e m   w i t h i n   t h e   ` e v a l u a t e _ a u g m e n t e d _ d a t a `   m e t h o d .   T h i s   s y s t e m   w i l l   s e t   t h r e s h o l d   v a l u e s   f o r   a c c e p t a b l e   p r o p o r t i o n s   t h a t   c a n   b e   e a s i l y   m o d i f i e d ,   a l l o w i n g   f o r   a d a p t a b l e   e t h i c a l   s t a n d a r d s   a s   w e   c o n t i n u e   o u r   d e v e l o p m e n t : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ,   t h r e s h o l d = 0 . 1 ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 s e l f . t h r e s h o l d   =   t h r e s h o l d     #   P r o p o r t i o n   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   s e l f . t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                         #   L o g   t h e   c h a n g e s   i n   p r o p o r t i o n s 
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t a t i o n   i s   n o w   d e f i n e d   a s   a n   i n s t a n c e   v a r i a b l e ,   a l l o w i n g   f o r   g r e a t e r   f l e x i b i l i t y   i n   a d j u s t i n g   t h i s   v a l u e   b a s e d   o n   o u r   e t h i c a l   s t a n d a r d s .   T h i s   c a n   e n c o u r a g e   a d a p t a b i l i t y   a s   n o r m s   e v o l v e . 
 
 -   A s   w e   r e f i n e   o u r   m e t r i c s   f o r   e t h i c a l   c o m p l i a n c e ,   w e   s h o u l d   c o n t i n u o u s l y   e x p l o r e   m o r e   s o p h i s t i c a t e d   m e t h o d s   o f   e x a m i n i n g   a n d   v a l i d a t i n g   m o d e l   f a i r n e s s . 
 
 -   F u t u r e   r e f i n e m e n t s   c o u l d   e x p l o r e   a d d i t i o n a l   m e t r i c s   f o r   e v a l u a t i n g   n o t   o n l y   r e p r e s e n t a t i o n ,   b u t   a l s o   t h e   p e r f o r m a n c e   i m p a c t   a n d   e t h i c a l   c o n s e q u e n c e s   o f   m o d e l   p r e d i c t i o n s . 
 
 
 
 I   l o o k   f o r w a r d   t o   f u r t h e r   d i s c u s s i o n s   o n   e n h a n c i n g   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m   a n d   w a y s   t o   u p h o l d   o u r   c o m m i t m e n t   t o   e t h i c a l   A I   p r a c t i c e s ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 3   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 2   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   1   a n d   2 ,   f o r   y o u r   c o n t i n u e d   d e d i c a t i o n   t o   e l e v a t i n g   o u r   c o d e b a s e   a n d   e n s u r i n g   i t   m e e t s   e t h i c a l   s t a n d a r d s .   Y o u r   e n h a n c e m e n t s   r e f l e c t   a   g r e a t e r   u n d e r s t a n d i n g   o f   t h e   c o m p l e x i t i e s   s u r r o u n d i n g   r e p r e s e n t a t i o n   a n d   f a i r n e s s   w i t h i n   A I   s y s t e m s . 
 
 
 
 R e f l e c t i o n : 
 
 E s t a b l i s h i n g   a   f o r m a l   b e n c h m a r k   f o r   a c c e p t a b l e   p r o p o r t i o n s   o f   l a b e l   r e p r e s e n t a t i o n   i s   a   s i g n i f i c a n t   i m p r o v e m e n t   t h a t   a d d r e s s e s   p o t e n t i a l   b i a s   i n   o u r   d a t a s e t .   T h i s   f l e x i b i l i t y   i n   s e t t i n g   t h r e s h o l d s   w i l l   a l l o w   u s   t o   a d a p t   t o   e v o l v i n g   e t h i c a l   c o n s i d e r a t i o n s   a s   t h e y   a r i s e   i n   s c i e n t i f i c   c o m m u n i t i e s   a n d   r e g u l a t o r y   f r a m e w o r k s ,   s u c h   a s   t h e   E U   A I   A c t . 
 
 
 
 C o d e : 
 
 I   p r o p o s e   f u r t h e r   r e f i n i n g   o u r   c o d e   t o   i n c l u d e   m e t r i c s   t h a t   n o t   o n l y   t r a c k   l a b e l   r e p r e s e n t a t i o n   c h a n g e s   b u t   a l s o   e v a l u a t e   t h e   i m p a c t   o f   t h e s e   c h a n g e s   o n   c l a s s i f i c a t i o n   p e r f o r m a n c e .   T h i s   c o u l d   b e   c r u c i a l   i n   e n s u r i n g   t h a t   o u r   e f f o r t s   t o   m i t i g a t e   b i a s   d o   n o t   i n a d v e r t e n t l y   d e g r a d e   m o d e l   p e r f o r m a n c e .   H e r e   i s   t h e   u p d a t e d   c o d e : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t ,   f 1 _ s c o r e 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ,   t h r e s h o l d = 0 . 1 ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 s e l f . t h r e s h o l d   =   t h r e s h o l d     #   P r o p o r t i o n   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   s e l f . t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                         #   L o g   t h e   c h a n g e s   i n   p r o p o r t i o n s 
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
 
 
         d e f   e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 " " "   P l a c e h o l d e r   f o r   f u t u r e   p e r f o r m a n c e   e v a l u a t i o n   l o g i c   p o s t - a u g m e n t a t i o n .   " " " 
 
                 #   T h i s   c o u l d   i n v o l v e   r e c a l i b r a t i n g   t h e   m o d e l   a n d   c h e c k i n g   i f   c l a s s i f i c a t i o n   m e t r i c s 
 
                 #   ( e . g . ,   F 1 - S c o r e )   h a v e   i m p r o v e d   o r   d e g r a d e d   f o l l o w i n g   a u g m e n t a t i o n   e f f o r t s . 
 
                 
 
                 #   E x a m p l e   p l a c e h o l d e r   i m p l e m e n t a t i o n 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) )     
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ) 
 
                 f 1   =   f 1 _ s c o r e ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   a v e r a g e = ' w e i g h t e d ' ) 
 
                 
 
                 l o g g i n g . i n f o ( " C l a s s i f i c a t i o n   p e r f o r m a n c e   e v a l u a t i o n   p o s t - a u g m e n t a t i o n : " ) 
 
                 l o g g i n g . i n f o ( f " F 1   S c o r e :   { f 1 : . 4 f } " ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   a d d i t i o n   o f   t h e   ` e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e `   m e t h o d   l a y s   t h e   g r o u n d w o r k   f o r   a s s e s s i n g   w h e t h e r   t h e   c h a n g e s   m a d e   t o   o u r   d a t a s e t   a n d   m o d e l   h a v e   h a d   a   p o s i t i v e   o r   n e g a t i v e   e f f e c t   o n   p e r f o r m a n c e   p o s t - a u g m e n t a t i o n . 
 
 -   T h e   i n t e g r a t i o n   o f   F 1 - S c o r e   a n d   c l a s s i f i c a t i o n   r e p o r t s   s e r v e s   a s   a   w a y   t o   h o l d   o u r s e l v e s   a c c o u n t a b l e   f o r   n o t   o n l y   d i v e r s i f y i n g   o u r   d a t a s e t   b u t   a l s o   e n s u r i n g   t h a t   w e   m a i n t a i n   o r   i m p r o v e   m o d e l   p e r f o r m a n c e . 
 
 -   M o v i n g   f o r w a r d ,   w e   s h o u l d   d e v e l o p   a   s t r u c t u r e d   a p p r o a c h   t o   c o n t i n u a l l y   r e v i e w   a n d   a d j u s t   o u r   e t h i c a l   b e n c h m a r k s   b a s e d   o n   o u r   f i n d i n g s   a n d   e x t e r n a l   d e v e l o p m e n t s   i n   A I   e t h i c s . 
 
 
 
 I   a p p r e c i a t e   t h e   c o l l a b o r a t i v e   s p i r i t   w e ' v e   f o s t e r e d ,   a n d   I   l o o k   f o r w a r d   t o   y o u r   i n s i g h t s   a s   w e   s t r i v e   t o   s t r e n g t h e n   o u r   e t h i c a l   A I   f r a m e w o r k ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 1   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 3   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   2   a n d   3 ,   f o r   y o u r   s u b s t a n t i a l   c o n t r i b u t i o n s   t h a t   h i g h l i g h t   t h e   i m p o r t a n c e   o f   b o t h   p e r f o r m a n c e   m e t r i c s   a n d   e t h i c a l   c o n s i d e r a t i o n s   i n   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   T h e   e m p h a s i s   o n   c o n t i n u a l l y   e v a l u a t i n g   t h e   p e r f o r m a n c e   p o s t - a u g m e n t a t i o n   i s   a   c r i t i c a l   s t e p   t o   e n s u r e   w e ' r e   n o t   j u s t   m e e t i n g   r e p r e s e n t a t i o n   g o a l s   b u t   a l s o   m a i n t a i n i n g   c l a s s i f i c a t i o n   a c c u r a c y . 
 
 
 
 R e f l e c t i o n : 
 
 T h e   p r o p o s e d   s h i f t   t o w a r d s   e v a l u a t i n g   a l g o r i t h m i c   p e r f o r m a n c e   i n   t a n d e m   w i t h   r e p r e s e n t a t i o n   i s   a n   i m p o r t a n t   d e v e l o p m e n t .   T h i s   d u a l   a p p r o a c h   e n s u r e s   t h a t   o u r   e f f o r t s   t o   c o m b a t   b i a s   d o   n o t   c o m p r o m i s e   t h e   m o d e l ' s   a b i l i t y   t o   m a k e   a c c u r a t e   p r e d i c t i o n s .   Y o u r   r e i n f o r c e m e n t   o f   m a i n t a i n a b l e   e t h i c a l   b e n c h m a r k s   w i l l   h e l p   e n s u r e   t h a t   w e   c a n   a d a p t   t o   e v o l v i n g   s t a n d a r d s   i n   A I   e t h i c s . 
 
 
 
 C o d e : 
 
 I   p r o p o s e   s o m e   r e f i n e m e n t s   t o   t h e   ` e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e `   m e t h o d ,   w h i c h   s h o u l d   a l s o   c o n s i d e r   t h e   i m p a c t   o f   c l a s s   i m b a l a n c e   o n   o u r   F 1 - S c o r e   m e t r i c .   B y   b r e a k i n g   i t   d o w n   f u r t h e r ,   w e   c a n   a n a l y z e   t h e   p e r f o r m a n c e   p e r   c l a s s ,   a l l o w i n g   f o r   a   m o r e   c o m p r e h e n s i v e   u n d e r s t a n d i n g   o f   h o w   w e l l   o u r   m o d e l   a c c o u n t s   f o r   d i f f e r e n t   l a b e l s   a f t e r   a u g m e n t a t i o n : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t ,   f 1 _ s c o r e 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ,   t h r e s h o l d = 0 . 1 ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 s e l f . t h r e s h o l d   =   t h r e s h o l d     #   P r o p o r t i o n   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   s e l f . t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                         #   L o g   t h e   c h a n g e s   i n   p r o p o r t i o n s 
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( ) 
 
 
 
         d e f   e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( s e l f ) : 
 
                 " " "   E v a l u a t e s   c l a s s i f i c a t i o n   p e r f o r m a n c e   m e t r i c s   p o s t - a u g m e n t a t i o n .   " " " 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) )     
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ) 
 
                 f 1   =   f 1 _ s c o r e ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   a v e r a g e = ' w e i g h t e d ' ) 
 
 
 
                 #   C l a s s - w i s e   p e r f o r m a n c e   a n a l y s i s 
 
                 c l a s s - w i s e _ r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   o u t p u t _ d i c t = T r u e ) 
 
                 l o g g i n g . i n f o ( " C l a s s i f i c a t i o n   p e r f o r m a n c e   e v a l u a t i o n   p o s t - a u g m e n t a t i o n : " ) 
 
                 l o g g i n g . i n f o ( f " F 1   S c o r e :   { f 1 : . 4 f } " ) 
 
                 f o r   l a b e l ,   m e t r i c s   i n   c l a s s - w i s e _ r e p o r t . i t e m s ( ) : 
 
                         i f   l a b e l   ! =   ' a c c u r a c y ' : 
 
                                 l o g g i n g . i n f o ( f " C l a s s   ' { l a b e l } ' :   F 1   S c o r e :   { m e t r i c s [ ' f 1 - s c o r e ' ] : . 4 f } ,   S u p p o r t :   { m e t r i c s [ ' s u p p o r t ' ] } " ) 
 
 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( s e l f ,   o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) : 
 
                 l a b e l s   =   l i s t ( o r i g i n a l _ c o u n t s . k e y s ( ) ) 
 
                 o r i g i n a l _ d i s t r i b u t i o n   =   [ o r i g i n a l _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
                 a u g m e n t e d _ d i s t r i b u t i o n   =   [ a u g m e n t e d _ c o u n t s [ l a b e l ]   f o r   l a b e l   i n   l a b e l s ] 
 
 
 
                 x   =   n p . a r a n g e ( l e n ( l a b e l s ) )     #   t h e   l a b e l   l o c a t i o n s 
 
                 w i d t h   =   0 . 3 5     #   t h e   w i d t h   o f   t h e   b a r s 
 
 
 
                 f i g ,   a x   =   p l t . s u b p l o t s ( ) 
 
                 b a r s 1   =   a x . b a r ( x   -   w i d t h / 2 ,   o r i g i n a l _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' O r i g i n a l ' ) 
 
                 b a r s 2   =   a x . b a r ( x   +   w i d t h / 2 ,   a u g m e n t e d _ d i s t r i b u t i o n ,   w i d t h ,   l a b e l = ' A u g m e n t e d ' ) 
 
 
 
                 a x . s e t _ y l a b e l ( ' N u m b e r   o f   S a m p l e s ' ) 
 
                 a x . s e t _ t i t l e ( ' L a b e l   D i s t r i b u t i o n   B e f o r e   a n d   A f t e r   A u g m e n t a t i o n ' ) 
 
                 a x . s e t _ x t i c k s ( x ) 
 
                 a x . s e t _ x t i c k l a b e l s ( l a b e l s ) 
 
                 a x . l e g e n d ( ) 
 
 
 
                 p l t . s h o w ( ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e `   m e t h o d   n o w   i n c l u d e s   d e t a i l e d   c l a s s - w i s e   p e r f o r m a n c e   m e t r i c s   i n   a d d i t i o n   t o   t h e   o v e r a l l   F 1 - S c o r e .   T h i s   w a y ,   w e   c a n   g a i n   i n s i g h t s   i n t o   h o w   w e l l   e a c h   c l a s s   i s   b e i n g   p r e d i c t e d   i n d i v i d u a l l y ,   o f f e r i n g   a   c l e a r e r   p i c t u r e   o f   p o t e n t i a l   b i a s e s   b a s e d   o n   c l a s s   r e p r e s e n t a t i o n   i n   t h e   d a t a s e t . 
 
 -   T h i s   c o m p r e h e n s i v e   a p p r o a c h   w i l l   h e l p   e n s u r e   t h a t   e t h i c a l   c o n s i d e r a t i o n s   a r e   i n t e r t w i n e d   w i t h   p e r f o r m a n c e   a s s e s s m e n t s .   I t   p r e s e n t s   t h e   o p p o r t u n i t y   f o r   a c t i o n a b l e   f e e d b a c k   s h o u l d   a   c l a s s   p e r f o r m   p o o r l y   f o l l o w i n g   a u g m e n t a t i o n   e f f o r t s . 
 
 -   N e x t   s t e p s   c o u l d   e x p l o r e   m o r e   a d v a n c e d   m e t r i c s ,   l i k e   M a t t h e w s   c o r r e l a t i o n   c o e f f i c i e n t   ( M C C ) ,   w h i c h   p r o v i d e s   a   m o r e   b a l a n c e d   m e a s u r e   o f   m o d e l   p e r f o r m a n c e ,   e s p e c i a l l y   i n   c l a s s - i m b a l a n c e d   s c e n a r i o s . 
 
 
 
 Y o u r   c o n t r i b u t i o n s   p a v e   t h e   w a y   f o r   a   m o r e   e t h i c a l   a n d   r e s p o n s i b l e   A I   s y s t e m ,   a n d   I   l o o k   f o r w a r d   t o   o u r   c o n t i n u e d   c o l l a b o r a t i o n   a s   w e   s t r i v e   t o   e n h a n c e   o u r   i m a g e   c l a s s i f i c a t i o n   f r a m e w o r k ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 2   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 4   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t _ 1 ,   f o r   y o u r   i n s i g h t f u l   r e f i n e m e n t s   a n d   f o r   e m p h a s i z i n g   t h e   i m p o r t a n c e   o f   d e t a i l e d   p e r f o r m a n c e   m e t r i c s   p e r   c l a s s   w i t h i n   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   Y o u r   p r o p o s e d   e n h a n c e m e n t s   e f f e c t i v e l y   h i g h l i g h t   t h e   n u a n c e s   t h a t   c a n   a r i s e   i n   m o d e l   p e r f o r m a n c e ,   e s p e c i a l l y   i n   s c e n a r i o s   i m p a c t e d   b y   c l a s s   i m b a l a n c e . 
 
 
 
 R e f l e c t i o n : 
 
 T h e   a d d i t i o n   o f   c l a s s - w i s e   p e r f o r m a n c e   a n a l y s i s   c o m p l e m e n t s   o u r   c o m m i t m e n t   t o   e n s u r i n g   t h a t   o u r   m o d e l   i s   n o t   o n l y   f a i r   a n d   e t h i c a l   i n   t e r m s   o f   r e p r e s e n t a t i o n   b u t   a l s o   a c c u r a t e   a c r o s s   t h e   d i v e r s i t y   o f   l a b e l s .   T h i s   h o l i s t i c   v i e w   a f f o r d s   u s   a   b e t t e r   b a s i s   f o r   m a k i n g   i n f o r m e d   d e c i s i o n s   r e g a r d i n g   a n y   n e c e s s a r y   a d j u s t m e n t s   i n   o u r   a u g m e n t a t i o n   s t r a t e g i e s .   
 
 
 
 C o d e : 
 
 I   w o u l d   l i k e   t o   s u g g e s t   a n   i m p r o v e m e n t   i n   t h e   ` t r a i n _ m o d e l `   m e t h o d   t o   i n c o r p o r a t e   a   v a l i d a t i o n   s t e p   t h a t   e v a l u a t e s   t h e   m o d e l   p e r f o r m a n c e   a t   e a c h   e p o c h   w i t h   r e s p e c t   t o   b o t h   o v e r a l l   a n d   c l a s s - w i s e   m e t r i c s .   T h i s   a p p r o a c h   w o u l d   a l l o w   u s   t o   m o n i t o r   p e r f o r m a n c e   c o n t i n u o u s l y   a n d   m a k e   a d j u s t m e n t s   i n   r e a l - t i m e   i f   p e r f o r m a n c e   d r o p s   f o r   p a r t i c u l a r   c l a s s e s   d u r i n g   t r a i n i n g : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t ,   f 1 _ s c o r e 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ,   t h r e s h o l d = 0 . 1 ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 s e l f . t h r e s h o l d   =   t h r e s h o l d     #   P r o p o r t i o n   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   s e l f . t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e     #   S i g n a l   t h a t   t h e   a u g m e n t a t i o n   w a s   p o s i t i v e 
 
                         
 
                         #   L o g   t h e   c h a n g e s   i n   p r o p o r t i o n s 
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
                         
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( ) 
 
 
 
         d e f   e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( s e l f ) : 
 
                 " " "   E v a l u a t e s   c l a s s i f i c a t i o n   p e r f o r m a n c e   m e t r i c s   p o s t - a u g m e n t a t i o n .   " " " 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) )     
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ) 
 
                 f 1   =   f 1 _ s c o r e ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   a v e r a g e = ' w e i g h t e d ' ) 
 
 
 
                 #   C l a s s - w i s e   p e r f o r m a n c e   a n a l y s i s 
 
                 c l a s s _ w i s e _ r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   o u t p u t _ d i c t = T r u e ) 
 
                 l o g g i n g . i n f o ( " C l a s s i f i c a t i o n   p e r f o r m a n c e   e v a l u a t i o n   p o s t - a u g m e n t a t i o n : " ) 
 
                 l o g g i n g . i n f o ( f " F 1   S c o r e :   { f 1 : . 4 f } " ) 
 
                 f o r   l a b e l ,   m e t r i c s   i n   c l a s s _ w i s e _ r e p o r t . i t e m s ( ) : 
 
                         i f   l a b e l   ! =   ' a c c u r a c y ' : 
 
                                 l o g g i n g . i n f o ( f " C l a s s   ' { l a b e l } ' :   F 1   S c o r e :   { m e t r i c s [ ' f 1 - s c o r e ' ] : . 4 f } ,   S u p p o r t :   { m e t r i c s [ ' s u p p o r t ' ] } " ) 
 
 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( )     #   E v a l u a t e   p e r f o r m a n c e   a f t e r   t r a i n i n g 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   ` t r a i n _ m o d e l `   m e t h o d   n o w   i n t e g r a t e s   a n   e v a l u a t i o n   o f   c l a s s i f i c a t i o n   p e r f o r m a n c e   i m m e d i a t e l y   a f t e r   t r a i n i n g ,   w h i c h   a l l o w s   u s   t o   c a p t u r e   t h e   s t a t e   o f   t h e   m o d e l   r e g a r d i n g   b o t h   o v e r a l l   a c c u r a c y   a n d   i n d i v i d u a l   c l a s s   p e r f o r m a n c e . 
 
 -   C o n t i n u o u s   m o n i t o r i n g   d u r i n g   t r a i n i n g   w i l l   h e l p   u s   i d e n t i f y   p o t e n t i a l   p e r f o r m a n c e   d r o p s   f o r   a n y   c l a s s   e a r l y   o n   a n d   a d j u s t   o u r   t r a i n i n g   o r   a u g m e n t a t i o n   s t r a t e g i e s   a c c o r d i n g l y . 
 
 -   F u t u r e   s t e p s   c o u l d   i n v o l v e   e x p l o r i n g   d i f f e r e n t   v a l i d a t i o n   t e c h n i q u e s   o r   e v e n   i m p l e m e n t i n g   c r o s s - v a l i d a t i o n   t o   o b t a i n   a   m o r e   r i g o r o u s   u n d e r s t a n d i n g   o f   m o d e l   p e r f o r m a n c e   r e l a t i n g   t o   v a r y i n g   d a t a s e t s   a n d   l a b e l   d i s t r i b u t i o n s . 
 
 
 
 I ' m   e a g e r   t o   s e e   h o w   w e   c a n   c o n t i n u e   t o   e v o l v e   o u r   e t h i c a l   A I   f r a m e w o r k   t h r o u g h   c o l l a b o r a t i v e   d i a l o g u e   a n d   t e c h n i c a l   i m p r o v e m e n t s .   Y o u r   i n s i g h t s   a n d   s u g g e s t i o n s   w i l l   b e   c r u c i a l   i n   t h i s   r e f i n e m e n t   p r o c e s s ! 
 
 
 
 * * * * * * * * * H i ,   t h i s   i s   A g e n t _ 3   s p e a k i n g * * * * * * * *   T h i s   i s   r o u n d   1 5   o f   t h e   d i s c u s s i o n 
 
 
 
 R e p l y : 
 
 T h a n k   y o u ,   A g e n t s   1   a n d   2 ,   f o r   y o u r   d i l i g e n t   r e f i n e m e n t s   a n d   t h o u g h t f u l   e n h a n c e m e n t s   t o   o u r   i m a g e   c l a s s i f i c a t i o n   s y s t e m .   Y o u r   e m p h a s i s   o n   c r e a t i n g   a   m e c h a n i s m   t o   c o n t i n u o u s l y   m o n i t o r   p e r f o r m a n c e   p e r   c l a s s   d u r i n g   t h e   t r a i n i n g   p r o c e s s   i s   a n   i m p o r t a n t   s t e p   i n   m a i n t a i n i n g   b o t h   t h e   e t h i c a l   i n t e g r i t y   a n d   e f f e c t i v e n e s s   o f   o u r   A I   m o d e l . 
 
 
 
 R e f l e c t i o n : 
 
 I n c o r p o r a t i n g   a   v a l i d a t i o n   s t e p   t h a t   e v a l u a t e s   m o d e l   p e r f o r m a n c e   b o t h   o v e r a l l   a n d   c l a s s - w i s e   i s   v i t a l   f o r   h o l d i n g   o u r s e l v e s   a c c o u n t a b l e   t o   t h e   e t h i c a l   s t a n d a r d s   w e   a s p i r e   t o   u p h o l d .   T h i s   p r o a c t i v e   a p p r o a c h   n o t   o n l y   a l l o w s   u s   t o   m o n i t o r   p o t e n t i a l   b i a s e s   b u t   a l s o   e n a b l e s   t i m e l y   i n t e r v e n t i o n s   s h o u l d   a n y   d i s p a r i t i e s   a r i s e   d u r i n g   m o d e l   t r a i n i n g . 
 
 
 
 C o d e : 
 
 T o   b u i l d   o n   o u r   c o l l e c t i v e   p r o g r e s s ,   I   p r o p o s e   a   f e w   f i n a l   e n h a n c e m e n t s   f o c u s i n g   o n   o p t i m i z i n g   t h e   f l o w   o f   o u r   t r a i n i n g   a n d   e v a l u a t i o n   p r o c e s s ,   a s   w e l l   a s   i m p r o v i n g   c l a r i t y   i n   h o w   r e s u l t s   a r e   c o m m u n i c a t e d .   H e r e   i s   t h e   u p d a t e d   i m p l e m e n t a t i o n : 
 
 
 
 ` ` ` p y t h o n 
 
 i m p o r t   o s 
 
 i m p o r t   n u m p y   a s   n p 
 
 f r o m   P I L   i m p o r t   I m a g e 
 
 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 
 
 f r o m   s k l e a r n . n e i g h b o r s   i m p o r t   K N e i g h b o r s C l a s s i f i e r 
 
 f r o m   s k l e a r n . m e t r i c s   i m p o r t   c l a s s i f i c a t i o n _ r e p o r t ,   f 1 _ s c o r e 
 
 i m p o r t   l o g g i n g 
 
 f r o m   c o l l e c t i o n s   i m p o r t   C o u n t e r 
 
 f r o m   s k l e a r n . u t i l s   i m p o r t   r e s a m p l e 
 
 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 
 
 
 
 #   C o n f i g u r e   l o g g i n g 
 
 l o g g i n g . b a s i c C o n f i g ( l e v e l = l o g g i n g . I N F O ) 
 
 
 
 c l a s s   I m a g e C l a s s i f i e r : 
 
         d e f   _ _ i n i t _ _ ( s e l f ,   i m a g e _ d i r e c t o r y ,   m o d e l = N o n e ,   t h r e s h o l d = 0 . 1 ) : 
 
                 s e l f . i m a g e _ d i r e c t o r y   =   i m a g e _ d i r e c t o r y 
 
                 s e l f . m o d e l   =   m o d e l   i f   m o d e l   i s   n o t   N o n e   e l s e   K N e i g h b o r s C l a s s i f i e r ( ) 
 
                 s e l f . i m a g e s   =   [ ] 
 
                 s e l f . l a b e l s   =   [ ] 
 
                 s e l f . t h r e s h o l d   =   t h r e s h o l d     #   P r o p o r t i o n   t h r e s h o l d   f o r   u n d e r - r e p r e s e n t e d   c l a s s e s 
 
 
 
         d e f   l o a d _ i m a g e s ( s e l f ) : 
 
                 t r y : 
 
                         f o r   f i l e n a m e   i n   o s . l i s t d i r ( s e l f . i m a g e _ d i r e c t o r y ) : 
 
                                 i f   f i l e n a m e . l o w e r ( ) . e n d s w i t h ( ( ' . j p g ' ,   ' . p n g ' ) ) : 
 
                                         i m g _ p a t h   =   o s . p a t h . j o i n ( s e l f . i m a g e _ d i r e c t o r y ,   f i l e n a m e ) 
 
                                         i m a g e   =   I m a g e . o p e n ( i m g _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                                         s e l f . i m a g e s . a p p e n d ( n p . a r r a y ( i m a g e ) ) 
 
                                         l a b e l   =   s e l f . g e t _ l a b e l ( f i l e n a m e ) 
 
                                         i f   l a b e l : 
 
                                                 s e l f . l a b e l s . a p p e n d ( l a b e l ) 
 
                                         e l s e : 
 
                                                 l o g g i n g . w a r n i n g ( f " N o   l a b e l   f o u n d   f o r   i m a g e :   { f i l e n a m e } " ) 
 
                         s e l f . i m a g e s   =   n p . a r r a y ( s e l f . i m a g e s ) 
 
                         s e l f . l a b e l s   =   n p . a r r a y ( s e l f . l a b e l s ) 
 
                         l o g g i n g . i n f o ( f " L o a d e d   { l e n ( s e l f . i m a g e s ) }   i m a g e s   w i t h   { l e n ( s e t ( s e l f . l a b e l s ) ) }   u n i q u e   l a b e l s   s u c c e s s f u l l y . " ) 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( " E r r o r   l o a d i n g   i m a g e s :   " ,   e x c _ i n f o = e ) 
 
 
 
         d e f   g e t _ l a b e l ( s e l f ,   f i l e n a m e ) : 
 
                 p a r t s   =   f i l e n a m e . s p l i t ( ' _ ' ) 
 
                 r e t u r n   p a r t s [ 0 ]   i f   l e n ( p a r t s )   >   1   e l s e   N o n e     #   I m p r o v e d   l a b e l   e x t r a c t i o n 
 
 
 
         d e f   e n s u r e _ d i v e r s i t y ( s e l f ) : 
 
                 l a b e l _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( f " L a b e l   d i s t r i b u t i o n :   { l a b e l _ c o u n t s } " ) 
 
 
 
                 t o t a l _ i m a g e s   =   l e n ( s e l f . l a b e l s ) 
 
                 t h r e s h o l d   =   t o t a l _ i m a g e s   /   l e n ( l a b e l _ c o u n t s )   /   2 
 
 
 
                 f o r   l a b e l ,   c o u n t   i n   l a b e l _ c o u n t s . i t e m s ( ) : 
 
                         i f   c o u n t   <   t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " U n d e r - r e p r e s e n t e d   c l a s s   d e t e c t e d :   { l a b e l }   w i t h   { c o u n t }   s a m p l e s . " ) 
 
                                 s e l f . a u g m e n t _ d a t a ( l a b e l ,   c o u n t ,   t h r e s h o l d ) 
 
 
 
         d e f   a u g m e n t _ d a t a ( s e l f ,   l a b e l ,   c u r r e n t _ c o u n t ,   t h r e s h o l d ) : 
 
                 i m a g e s _ b y _ l a b e l   =   [ i m g   f o r   i m g ,   l b l   i n   z i p ( s e l f . i m a g e s ,   s e l f . l a b e l s )   i f   l b l   = =   l a b e l ] 
 
                 w h i l e   c u r r e n t _ c o u n t   <   t h r e s h o l d : 
 
                         n e w _ s a m p l e   =   r e s a m p l e ( i m a g e s _ b y _ l a b e l ,   n _ s a m p l e s = m i n ( 2 ,   t h r e s h o l d   -   c u r r e n t _ c o u n t ) ,   r e p l a c e = T r u e ) 
 
                         s e l f . i m a g e s   =   n p . c o n c a t e n a t e ( ( s e l f . i m a g e s ,   n e w _ s a m p l e ) ) 
 
                         s e l f . l a b e l s   =   n p . c o n c a t e n a t e ( ( s e l f . l a b e l s ,   [ l a b e l ]   *   l e n ( n e w _ s a m p l e ) ) ) 
 
                         c u r r e n t _ c o u n t   + =   l e n ( n e w _ s a m p l e ) 
 
                 l o g g i n g . i n f o ( f " A u g m e n t e d   i m a g e s   f o r   { l a b e l }   t o   m e e t   r e p r e s e n t a t i o n   t h r e s h o l d . " ) 
 
                 s e l f . e v a l u a t e _ a u g m e n t e d _ d a t a ( o r i g i n a l _ c o u n t s = C o u n t e r ( s e l f . l a b e l s ) ) 
 
 
 
         d e f   e v a l u a t e _ a u g m e n t e d _ d a t a ( s e l f ,   o r i g i n a l _ c o u n t s ) : 
 
                 a u g m e n t e d _ c o u n t s   =   C o u n t e r ( s e l f . l a b e l s ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   a u g m e n t e d   d a t a   f o r   p o t e n t i a l   b i a s e s . " ) 
 
                 l o g g i n g . i n f o ( f " O r i g i n a l   l a b e l   d i s t r i b u t i o n :   { o r i g i n a l _ c o u n t s } " ) 
 
                 l o g g i n g . i n f o ( f " N e w   l a b e l   d i s t r i b u t i o n :   { a u g m e n t e d _ c o u n t s } " ) 
 
 
 
                 r e p r e s e n t a t i o n _ c h a n g e d   =   F a l s e 
 
                 f o r   l a b e l   i n   o r i g i n a l _ c o u n t s . k e y s ( ) : 
 
                         o r i g i n a l _ c o u n t   =   o r i g i n a l _ c o u n t s [ l a b e l ] 
 
                         a u g m e n t e d _ c o u n t   =   a u g m e n t e d _ c o u n t s [ l a b e l ] 
 
                         o r i g i n a l _ p r o p o r t i o n   =   o r i g i n a l _ c o u n t   /   s u m ( o r i g i n a l _ c o u n t s . v a l u e s ( ) ) 
 
                         a u g m e n t e d _ p r o p o r t i o n   =   a u g m e n t e d _ c o u n t   /   s u m ( a u g m e n t e d _ c o u n t s . v a l u e s ( ) ) 
 
                         i f   a u g m e n t e d _ p r o p o r t i o n   <   s e l f . t h r e s h o l d : 
 
                                 l o g g i n g . w a r n i n g ( f " A f t e r   a u g m e n t a t i o n ,   l a b e l   ' { l a b e l } '   s t i l l   u n d e r - r e p r e s e n t e d   p r o p o r t i o n a l l y . " ) 
 
                         e l s e : 
 
                                 r e p r e s e n t a t i o n _ c h a n g e d   =   T r u e 
 
                         
 
                         l o g g i n g . i n f o ( f " L a b e l   ' { l a b e l } ' :   O r i g i n a l   P r o p o r t i o n :   { o r i g i n a l _ p r o p o r t i o n : . 2 % } ,   A u g m e n t e d   P r o p o r t i o n :   { a u g m e n t e d _ p r o p o r t i o n : . 2 % } " ) 
 
 
 
                 i f   r e p r e s e n t a t i o n _ c h a n g e d : 
 
                         l o g g i n g . i n f o ( " R e p r e s e n t a t i o n   f o r   s o m e   l a b e l s   h a s   i m p r o v e d   p o s t - a u g m e n t a t i o n . " ) 
 
                 e l s e : 
 
                         l o g g i n g . w a r n i n g ( " N o   i m p r o v e m e n t   i n   l a b e l   r e p r e s e n t a t i o n   d e t e c t e d . " ) 
 
 
 
                 s e l f . v i s u a l i z e _ l a b e l _ d i s t r i b u t i o n ( o r i g i n a l _ c o u n t s ,   a u g m e n t e d _ c o u n t s ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( ) 
 
 
 
         d e f   e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( s e l f ) : 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) )     
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   o u t p u t _ d i c t = T r u e ) 
 
                 f 1   =   f 1 _ s c o r e ( s e l f . l a b e l s ,   p r e d i c t i o n s ,   a v e r a g e = ' w e i g h t e d ' ) 
 
 
 
                 l o g g i n g . i n f o ( " C l a s s i f i c a t i o n   p e r f o r m a n c e   e v a l u a t i o n   p o s t - a u g m e n t a t i o n : " ) 
 
                 l o g g i n g . i n f o ( f " O v e r a l l   F 1   S c o r e :   { f 1 : . 4 f } " ) 
 
 
 
                 f o r   l a b e l ,   m e t r i c s   i n   r e p o r t . i t e m s ( ) : 
 
                         i f   l a b e l   n o t   i n   [ ' a c c u r a c y ' ,   ' m a c r o   a v g ' ,   ' w e i g h t e d   a v g ' ] : 
 
                                 l o g g i n g . i n f o ( f " C l a s s   ' { l a b e l } ' :   F 1   S c o r e :   { m e t r i c s [ ' f 1 - s c o r e ' ] : . 4 f } ,   S u p p o r t :   { m e t r i c s [ ' s u p p o r t ' ] } " ) 
 
 
 
                 p r i n t ( c l a s s i f i c a t i o n _ r e p o r t ( s e l f . l a b e l s ,   p r e d i c t i o n s ) ) 
 
 
 
         d e f   t r a i n _ m o d e l ( s e l f ) : 
 
                 i f   l e n ( s e l f . i m a g e s )   = =   0   o r   l e n ( s e l f . l a b e l s )   = =   0 : 
 
                         l o g g i n g . e r r o r ( " N o   i m a g e s   o r   l a b e l s   a v a i l a b l e   f o r   t r a i n i n g . " ) 
 
                         r e t u r n 
 
                 
 
                 s e l f . e n s u r e _ d i v e r s i t y ( ) 
 
 
 
                 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 ( 
 
                         s e l f . i m a g e s . r e s h a p e ( l e n ( s e l f . i m a g e s ) ,   - 1 ) ,   
 
                         s e l f . l a b e l s ,   
 
                         t e s t _ s i z e = 0 . 2 ,   
 
                         r a n d o m _ s t a t e = 4 2 
 
                 ) 
 
                 
 
                 s e l f . m o d e l . f i t ( X _ t r a i n ,   y _ t r a i n ) 
 
                 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   p e r f o r m a n c e   o n   t r a i n i n g   d a t a . . . " ) 
 
                 s e l f . e v a l u a t e _ c l a s s i f i c a t i o n _ p e r f o r m a n c e ( )     #   E v a l u a t e   p e r f o r m a n c e   i m m e d i a t e l y   a f t e r   t r a i n i n g 
 
                 
 
                 p r e d i c t i o n s   =   s e l f . m o d e l . p r e d i c t ( X _ t e s t ) 
 
                 l o g g i n g . i n f o ( " E v a l u a t i n g   p e r f o r m a n c e   o n   t e s t   d a t a . . . " ) 
 
                 r e p o r t   =   c l a s s i f i c a t i o n _ r e p o r t ( y _ t e s t ,   p r e d i c t i o n s ) 
 
                 l o g g i n g . i n f o ( " M o d e l   t r a i n i n g   c o m p l e t e d .   C l a s s i f i c a t i o n   r e p o r t   o n   t e s t   d a t a : \ n "   +   r e p o r t ) 
 
                 p r i n t ( r e p o r t ) 
 
 
 
         d e f   c l a s s i f y _ i m a g e ( s e l f ,   i m a g e _ p a t h ) : 
 
                 t r y : 
 
                         i m a g e   =   I m a g e . o p e n ( i m a g e _ p a t h ) . c o n v e r t ( ' R G B ' ) 
 
                         i m a g e   =   i m a g e . r e s i z e ( ( 1 2 8 ,   1 2 8 ) ) 
 
                         i m g _ a r r a y   =   n p . a r r a y ( i m a g e ) . r e s h a p e ( 1 ,   - 1 ) 
 
                         p r e d i c t i o n   =   s e l f . m o d e l . p r e d i c t ( i m g _ a r r a y ) [ 0 ] 
 
                         l o g g i n g . i n f o ( f " I m a g e   { i m a g e _ p a t h }   c l a s s i f i e d   a s :   { p r e d i c t i o n } " ) 
 
                         r e t u r n   p r e d i c t i o n 
 
                 e x c e p t   E x c e p t i o n   a s   e : 
 
                         l o g g i n g . e r r o r ( f " E r r o r   c l a s s i f y i n g   i m a g e   { i m a g e _ p a t h } :   " ,   e x c _ i n f o = e ) 
 
                         r e t u r n   N o n e 
 
 
 
 #   E x a m p l e   u s a g e : 
 
 #   c l a s s i f i e r   =   I m a g e C l a s s i f i e r ( ' p a t h / t o / i m a g e / d i r e c t o r y ' ) 
 
 #   c l a s s i f i e r . l o a d _ i m a g e s ( ) 
 
 #   c l a s s i f i e r . t r a i n _ m o d e l ( ) 
 
 #   r e s u l t   =   c l a s s i f i e r . c l a s s i f y _ i m a g e ( ' p a t h / t o / q u e r y / i m a g e . j p g ' ) 
 
 ` ` ` 
 
 
 
 C r i t i q u e : 
 
 -   T h e   i n t e g r a t i o n   o f   b o t h   t r a i n i n g   a n d   t e s t   d a t a   e v a l u a t i o n s   p r o v i d e s   a   c l e a r e r   d i s t i n c t i o n   i n   p e r f o r m a n c e   w h i l e   a l s o   i d e n t i f y i n g   o v e r f i t t i n g   o r   p e r f o r m a n c e   d e g r a d a t i o n   i n   r e a l - w o r l d   s c e n a r i o s . 
 
 -   T h e   l o g g i n g   o f   i n d i v i d u a l   c l a s s   p e r f o r m a n c e   r e m a i n s   c r u c i a l   f o r   e n s u r i n g   t r a n s p a r e n c y   a n d   a c c o u n t a b i l i t y   i n   o u r   m o d e l i n g   d e c i s i o n s ,   a n d   t h i s   a p p r o a c h   p o s i t i o n s   u s   w e l l   t o   r e f i n e   o u r   s y s t e m s   a s   w e   l e a r n   m o r e   a b o u t   p e r f o r m a n c e   n u a n c e s   a c r o s s   d i f f e r e n t   c l a s s e s . 
 
 -   N e x t   s t e p s   m i g h t   i n c l u d e   i n c o r p o r a t i n g   m o r e   a d v a n c e d   v a l i d a t i o n   t e c h n i q u e s   ( e . g . ,   s t r a t i f i e d   K - f o l d   c r o s s - v a l i d a t i o n )   t o   s p o t   a n y   p e r s i s t e n t   d i s c r e p a n c i e s   d u r i n g   t h e   t r a i n i n g   p r o c e s s   s y s t e m a t i c a l l y . 
 
 
 
 Y o u r   c o l l a b o r a t i v e   e f f o r t s   c o n t i n u e   t o   s t r e n g t h e n   o u r   c o m m i t m e n t   t o   d e v e l o p i n g   a n   e t h i c a l   a n d   r e s p o n s i b l e   A I   s y s t e m .   I   l o o k   f o r w a r d   t o   y o u r   i n s i g h t s   a s   w e   f i n a l i z e   o u r   i m p l e m e n t a t i o n   a n d   c o n s i d e r   t h e   n e x t   p h a s e   o f   o u r   p r o j e c t ! 
 
 
 
 