<jupyter_start><jupyter_text>**meaning that in the first batch of train_generator, 64 images of dimensions 224 x 224 x 3 were created, along with 64 labels of 5 one-hot encoding formatting.**<jupyter_code># checking assigned encoding values - notice the alphabetical order
print(train_generator.class_indices)<jupyter_output>{'daisy': 0, 'dandelion': 1, 'roses': 2, 'sunflowers': 3, 'tulips': 4}<jupyter_text>**Save these labels for future usage to deploy the model in the Flutter application**<jupyter_code>labels = '\n'.join(sorted(train_generator.class_indices.keys()))

with open('labels.txt', 'w') as f:
  f.write(labels)
labels<jupyter_output><empty_output><jupyter_text>Creating a base model and freezing all the layers except the last one<jupyter_code>IMG_SHAPE = (IMAGE_SIZE, IMAGE_SIZE, 3)

base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                              include_top=False, 
                                              weights='imagenet')<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5
9412608/9406464 [==============================] - 0s 0us/step<jupyter_text>**Now, we freeze the base model so that the weights in the MobileNetV2 model remain unaffected by future training, as follows:**<jupyter_code>base_model.trainable = False<jupyter_output><empty_output><jupyter_text>**Next we will create an extended CNN, and extend the base model to add another layer after the base model layers, like this:**<jupyter_code>model = tf.keras.Sequential([
      base_model,
  tf.keras.layers.Conv2D(32, 3, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.GlobalAveragePooling2D(),
  tf.keras.layers.Dense(5, activation='softmax')
])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
mobilenetv2_1.00_224 (Functi (None, 7, 7, 1280)        2257984   
_________________________________________________________________
conv2d (Conv2D)              (None, 5, 5, 32)          368672    
_________________________________________________________________
dropout (Dropout)            (None, 5, 5, 32)          0         
_________________________________________________________________
global_average_pooling2d (Gl (None, 32)                0         
_________________________________________________________________
dense (Dense)                (None, 5)                 165       
=================================================================
Total params: 2,626,821
Trainable params: 368,837
Non-trainable params: 2,257,984
________________________________________[...]<jupyter_text>**Compiling the model to perform training on it**<jupyter_code>model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss='categorical_crossentropy',
              metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>We set the loss as categorical cross-entropy and the model evaluation metric as the accuracy of prediction. Softmax has been found to perform best with categorical cross-entropy as the loss function, hence the choice.  **Train and save the model**<jupyter_code>epochs=10
model.fit(train_generator,
         epochs=epochs,
         validation_data=val_generator)<jupyter_output>Epoch 1/10
46/46 [==============================] - 147s 3s/step - loss: 1.3411 - accuracy: 0.5763 - val_loss: 0.5538 - val_accuracy: 0.8372
Epoch 2/10
46/46 [==============================] - 143s 3s/step - loss: 0.3582 - accuracy: 0.8705 - val_loss: 0.5127 - val_accuracy: 0.8290
Epoch 3/10
46/46 [==============================] - 141s 3s/step - loss: 0.1837 - accuracy: 0.9391 - val_loss: 0.4477 - val_accuracy: 0.8605
Epoch 4/10
46/46 [==============================] - 141s 3s/step - loss: 0.1184 - accuracy: 0.9615 - val_loss: 0.4718 - val_accuracy: 0.8632
Epoch 5/10
46/46 [==============================] - 139s 3s/step - loss: 0.0621 - accuracy: 0.9822 - val_loss: 0.4984 - val_accuracy: 0.8605
Epoch 6/10
46/46 [==============================] - 140s 3s/step - loss: 0.0402 - accuracy: 0.9929 - val_loss: 0.5336 - val_accuracy: 0.8714
Epoch 7/10
46/46 [==============================] - 141s 3s/step - loss: 0.0352 - accuracy: 0.9920 - val_loss: 0.5504 - val_accuracy: 0.8618
Epoch 8/10
46[...]<jupyter_text>The model is trained for 10 epochs, which means every sample is thrown at the neural network at least 10 times. Notice the use of train_generator and val_generator in this function. The training takes quite some time on this, even with 12GB+ of RAM and TPU acceleration available (which would be an overkill on any personal, mid-end device). You'll be able to observe the training logs below the cell that runs the preceding code.   **We can then save the model, after which we can proceed to convert the saved model file,**<jupyter_code>saved_model_dir = ''
tf.saved_model.save(model, saved_model_dir)<jupyter_output>/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:2292: UserWarning: `Model.state_updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
  warnings.warn('`Model.state_updates` will be removed in a future version. '
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:1377: UserWarning: `layer.updates` will be removed in a future version. This property should not be used in TensorFlow 2.0, as `updates` are applied automatically.
  warnings.warn('`layer.updates` will be removed in a future version. '<jupyter_text>Convert and download the model file to TensorFlow Lite. We can now convert the saved model file using the following code. This saves the model as a model.tflite<jupyter_code>converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

with open('model.tflite', 'wb') as f:
  f.write(tflite_model)<jupyter_output><empty_output><jupyter_text>We now need to download this file, for embedding it into the Flutter application that we build. We can do so using the following code:<jupyter_code>from google.colab import files
files.download('model.tflite')
files.download('labels.txt')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14783281733746131, 'fraction_numerical': 0.05619195046439628, 'mean_word_length': 4.598786828422877, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '439101', 'license': 'MIT', 'n_tokens_mistral': 2185, 'n_tokens_neox': 1900, 'n_words': 556, 'path': 'Plant-Species-Recognition/Plant_Species_Recofnition.ipynb', 'repo_name': 'vishal2develop/Deep-Learning', 'size': '6460'}