write this with all sections length being same " Implementation 5.1 Setup and…
- User
write this with all sections length being same " Implementation 5.1 Setup and Installation 5.1.1 Installing Required Libraries The code begins by importing necessary libraries. TensorFlow (tensorflow) is used for building and training deep learning models. Matplotlib (matplotlib) is employed for visualizing training metrics such as accuracy and loss. Pandas (pandas) and Seaborn (seaborn) are imported for data manipulation and enhanced visualizations, respectively. Scikit-learn (sklearn) is utilized for model evaluation metrics like confusion matrices and classification reports. 5.2 Data Loading and Preprocessing Resizing and Interpolation: Images from the ’train’ and ’valid’ directories are resized to 128x128 pixels using bilinear interpolation to maintain aspect ratio and ensure uniformity in input size for the neural network. Color Mode and Batch Processing: Loaded in RGB color mode, images are processed in batches of 32 during training, optimizing computational efficiency and enabling simultaneous weight updates based on multiple samples. 1 t r a i n i n g s e t = t f . k e ra s . u t i l s . i m a g e d a t a s e t f r o m d i r e c t o r y ( 2 ’ t r a i n ’ , 3 l a b e l s=” i n f e r r e d ” , 4 la b el mo d e=” c a t e g o r i c a l ” , 5 colo r mode=” rgb ” , 6 b a t c h s i z e =32, 7 i m a g e s i z e =(128 , 128 ) , 8 s h u f f l e=True , 9 i n t e r p o l a t i o n=” b i l i n e a r ” 13 10 ) 11 12 v a l i d a t i o n s e t = t f . k e ra s . u t i l s . i m a g e d a t a s e t f r o m d i r e c t o r y ( 13 ’ v a l i d ’ , 14 l a b e l s=” i n f e r r e d ” , 15 la b el mo d e=” c a t e g o r i c a l ” , 16 colo r mode=” rgb ” , 17 b a t c h s i z e =32, 18 i m a g e s i z e =(128 , 128 ) , 19 s h u f f l e=True , 20 i n t e r p o l a t i o n=” b i l i n e a r ” 21 ) 5.3 Model Definition Here we are defining the model to extract spatial features from images, followed by max pooling layers (MaxPool2D) to reduce dimensionality and extract dominant features. Dropout layers (Dropout) are incorporated to prevent overfitting by randomly deactivating neurons during training. The extracted features are flattened using Flatten and passed through dense layers (Dense) for final classification. The output layer employs a softmax activation function (softmax) suitable for multi-class classification tasks. 22 cnn = t f . k e ra s . models . S e q u e n ti al ( [ 23 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =32, k e r n e l s i z e =3, padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ , i n p u t s ha p e =[128 , 128 , 3 ] ) , 24 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =32, k e r n e l s i z e =3, a c t i v a t i o n=’ r e l u ’ ) , 25 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) , 26 Department of Computer Science and Engineering 14 27 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =128, k e r n e l s i z e =3, padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) , 28 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =128, k e r n e l s i z e =3, a c t i v a t i o n=’ r e l u ’ ) , 29 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) , 30 31 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =256, k e r n e l s i z e =3, padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) , 32 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =256, k e r n e l s i z e =3, a c t i v a t i o n=’ r e l u ’ ) , 33 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) , 34 35 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =512, k e r n e l s i z e =3, padding=’ same ’ , a c t i v a t i o n=’ r e l u ’ ) , 36 t f . k e ra s . l a y e r s . Conv2D ( f i l t e r s =512, k e r n e l s i z e =3, a c t i v a t i o n=’ r e l u ’ ) , 37 t f . k e ra s . l a y e r s . MaxPool2D ( p o o l s i z e =2, s t r i d e s =2) , 38 39 t f . k e ra s . l a y e r s . Dropout ( 0 . 2 5 ) , 40 t f . k e ra s . l a y e r s . Fla t t e n ( ) , 41 t f . k e ra s . l a y e r s . Dense ( u ni t s =1500, a c t i v a t i o n=’ r e l u ’ ) , 42 t f . k e ra s . l a y e r s . Dropout ( 0 . 4 ) , 43 t f . k e ra s . l a y e r s . Dense ( u ni t s=n um cla s se s , a c t i v a t i o n=’ softmax ’ ) 44 ] ) Department of Computer Science and Engineering 15 5.4 Model Training and Evaluation Here we are training and Evaluating our model through epochs 46 cnn . compile ( o p timi z e r=t f . k e ra s . o p timi z e r s .Adam( l e a r n i n g r a t e =0.0001) , l o s s=’ c a t e g o r i c a l c r o s s e n t r o p y ’ , 47 m e t ri c s =[ ’ accu ra c y ’ ] ) 48 cnn . summary ( ) 49 t r a i n i n g h i s t o r y = cnn . f i t ( x=t r a i n i n g s e t , v a l i d a t i o n d a t a= v a l i d a t i o n s e t , epoch s =10) 50 t r a i n l o s s , t r a i n a c c = cnn . e v al u a t e ( t r a i n i n g s e t ) 51 pr int ( ’ T rai ni ng ac cu rac y : ’ , t r a i n a c c ) 52 v a l l o s s , v a l a c c = cnn . e v al u a t e ( v a l i d a t i o n s e t ) 53 pr int ( ’ V ali d a ti o n accu rac y : ’ , v a l a c c ) Figure 5.1: Model Training Department of Computer Science and Engineering 16 Figure 5.2: Model Training 5.5 Visualization of Training History 54 import m a t pl o tli b . p y plo t a s p l t 55 epoch s = range ( 1 , 11 ) 56 p l t . pl o t ( epochs , t r a i n i n g h i s t o r y . h i s t o r y [ ’ accu ra c y ’ ] , c o l o r=’ red ’ , l a b e l=’ T rai ni ng Accuracy ’ ) 57 p l t . pl o t ( epochs , t r a i n i n g h i s t o r y . h i s t o r y [ ’ v al a c c u r a c y ’ ] , c o l o r=’ bl u e ’ , l a b e l=’ V ali d a ti o n Accuracy ’ ) 58 p l t . x l a b e l ( ’No . of Epochs ’ ) 59 p l t . t i t l e ( ’ V i s u a l i z a t i o n of Accuracy R e s ul t ’ ) 60 p l t . l eg e n d ( ) 61 p l t . show ( ) Figure 5.3: Visualisation of Accuracy Result Department of Computer Science and Engineering 17 5.6 Model Evaluation Metrics 62 t e s t s e t = t f . k e ra s . u t i l s . i m a g e d a t a s e t f r o m d i r e c t o r y ( 63 ’ v a l i d ’ , 64 l a b e l s=” i n f e r r e d ” , 65 la b el mo d e=” c a t e g o r i c a l ” , 66 colo r mode=” rgb ” , 67 b a t c h s i z e =1, 68 i m a g e s i z e =(128 , 128 ) , 69 s h u f f l e=Fal se , 70 i n t e r p o l a t i o n=” b i l i n e a r ” 71 ) 72 73 # P r e d i c t c l a s s e s f o r t e s t s e t 74 y p red = cnn . p r e d i c t ( t e s t s e t ) 75 p r e d i c t e d c a t e g o r i e s = t f . argmax ( y pred , a x i s =1) . numpy ( ) 76 t r u e c a t e g o r i e s = t f . conca t ( [ y for x , y in t e s t s e t ] , a x i s =0) 77 Y t rue = t f . argmax ( t r u e c a t e g o r i e s , a x i s =1) . numpy ( ) 78 79 # Compute c o n f u s i o n m a t r ix and c l a s s i f i c a t i o n r e p o r t 80 cla s s nam e s = v a l i d a t i o n s e t . cl a s s nam e s 81 cm = c o nf u si o n m a t ri x ( Y true , p r e d i c t e d c a t e g o r i e s ) 82 pr int ( c l a s s i f i c a t i o n r e p o r t ( Y true , p r e d i c t e d c a t e g o r i e s , ta rg e t nam e s=cl a s s nam e s ) ) 83 84 # Pl o t c o n f u s i o n m a t r ix 85 p l t . f i g u r e ( f i g s i z e =(10 , 8 ) ) 86 s n s . heatmap (cm, annot=True , annot kws={” s i z e ” : 10} , fmt=”d” , cmap=” Blue s ” , x t i c k l a b e l s=cla s s nam e s , 87 y t i c k l a b e l s=cla s s n am e s ) 88 p l t . x l a b e l ( ’ P r e di c t e d Cla s s ’ , f o n t s i z e =12) Department of Computer Science and Engineering 18 89 p l t . y l a b e l ( ’ Ac tual Cla s s ’ , f o n t s i z e =12) 90 p l t . t i t l e ( ’ Plan t Di s ea s e P r e di c ti o n Confu sion Matrix ’ , f o n t s i z e =15) 91 p l t . show ( ) 92 cla s s nam e s = v a l i d a t i o n s e t . cl a s s nam e s 93 94 # Load t h e t r a i n e d model 95 cnn = t f . k e ra s . models . load model ( ’ t r a i n e d p l a n t d i s e a s e m o d e l . k e ra s ’ ) 96 97 # Path t o t h e image t o be t e s t e d 98 image pa th = r ”C: \ U se r s \ g o u ri \OneDrive\Documents\Desktop pla n t d i s e a s e \ c a u l i . jpg ” 99 100 # Read and p r e p r o c e s s t h e image 101 image = t f . k e ra s . p r e p r o c e s s i n g . image . load img ( image path , t a r g e t s i z e =(128 , 128 ) ) 102 i n p u t a r r = t f . k e ra s . p r e p r o c e s s i n g . image . im g t o a r r a y ( image ) 103 i n p u t a r r = np . a r ra y ( [ i n p u t a r r ] ) # Conver t s i n g l e image t o a b a t c h 104 105 # Make p r e d i c t i o n s 106 p r e d i c t i o n s = cnn . p r e d i c t ( i n p u t a r r ) 107 108 # P r i n t p r e d i c t i o n s 109 pr int ( p r e d i c t i o n s ) 110 111 # Get t h e p r e d i c t e d c l a s s i n d e x 112 r e s u l t i n d e x = np . argmax ( p r e d i c t i o n s ) # Return i n d e x o f max elemen t 113 pr int ( r e s u l t i n d e x ) Department of Computer Science and Engineering 19 114 115 # Get t h e c l a s s name o f t h e p r e d i c t e d c l a s s 116 m o d el p r e di c ti o n = cla s s nam e s [ r e s u l t i n d e x ] 117 118 # D i s pl a y t h e image w i t h t h e p r e d i c t i o n 119 p l t . imshow ( image ) 120 p l t . t i t l e ( f ” Di s ea s e Name: { m o d el p r e di c ti o n }” ) 121 p l t . x t i c k s ( [ ] ) 122 p l t . y t i c k s ( [ ] ) 123 p l t . show ( ) 124 125 # Make p r e d i c t i o n s on t h e v a l i d a t i o n s e t 126 y p red = cnn . p r e d i c t ( v a l i d a t i o n s e t ) 127 p r e d i c t e d c l a s s e s = np . argmax ( y pred , a x i s =1) 128 129 # Get t r u e c l a s s e s 130 t r u e c l a s s e s = np . co n ca t e na t e ( [ y for x , y in v a l i d a t i o n s e t ] , a x i s =0) 131 t r u e c l a s s e s = np . argmax ( t r u e c l a s s e s , a x i s =1) 132 133 # Genera te c l a s s i f i c a t i o n r e p o r t 134 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 ( t r u e c l a s s e s , p r e d i c t e d c l a s s e s , ta rg e t nam e s=cla s s nam e s , o u t p u t di c t =True ) 135 136 # E x t r a c t p r e c i s i o n and r e c a l l v a l u e s 137 p r e c i s i o n v a l u e s = [ r e p o r t [ cla s s nam e ] [ ’ p r e c i s i o n ’ ] for cla s s nam e in cl a s s nam e s ] 138 r e c a l l v a l u e s = [ r e p o r t [ cla s s nam e ] [ ’ r e c a l l ’ ] for cla s s nam e in cl a s s nam e s ] 139 140 # Pl o t p r e c i s i o n h i s t og r am f o r each c l a s s Department of Computer Science and Engineering 20 141 p l t . f i g u r e ( f i g s i z e =(10 , 6 ) ) 142 p l t . bar ( cla s s nam e s , p r e c i s i o n v a l u e s , c o l o r=’ s k y bl u e ’ , alpha =0.7 , l a b e l=’ P r e c i s i o n ’ ) 143 p l t . x l a b e l ( ’ Cla s s ’ ) 144 p l t . y l a b e l ( ’ P r e c i s i o n ’ ) 145 p l t . t i t l e ( ’ P r e c i s i o n f o r Each Cla s s ’ ) 146 p l t . x t i c k s ( r o t a t i o n =45) 147 p l t . ylim ( [ 0 , 1 ] ) 148 p l t . t i g h t l a y o u t ( ) 149 p l t . l eg e n d ( ) 150 p l t . show ( ) 151 152 # Pl o t r e c a l l h i s t og r am f o r each c l a s s 153 p l t . f i g u r e ( f i g s i z e =(10 , 6 ) ) 154 p l t . bar ( cla s s nam e s , r e c a l l v a l u e s , c o l o r=’ l i g h t c o r a l ’ , alpha =0.7 , l a b e l=’ R e c all ’ ) 155 p l t . x l a b e l ( ’ Cla s s ’ ) 156 p l t . y l a b e l ( ’ R e c all ’ ) 157 p l t . t i t l e ( ’ R e c all f o r Each Cla s s ’ ) 158 p l t . x t i c k s ( r o t a t i o n =45) 159 p l t . ylim ( [ 0 , 1 ] ) 160 p l t . t i g h t l a y o u t ( ) 161 p l t . l eg e n d ( ) 162 p l t . show ( ) 163 164 # P r i n t t h e p r e c i s i o n and r e c a l l v a l u e s f o r r e f e r e n c e 165 for cla s s name , p r e c i s i o n , r e c a l l in z ip ( cla s s nam e s , p r e c i s i o n v a l u e s , r e c a l l v a l u e s ) : 166 pr int ( f ’ Cla s s : { cla s s nam e } , P r e c i s i o n : { p r e c i s i o n : . 2 f } , R e c all : { r e c a l l : . 2 f } ’ )"
- Assistant
- User
- Assistant