diff --git a/workshop/places365_train.ipynb b/workshop/places365_train.ipynb index 4a4e8d0..b447046 100644 --- a/workshop/places365_train.ipynb +++ b/workshop/places365_train.ipynb @@ -11,14 +11,15 @@ }, { "cell_type": "code", - "source": [ - "!gdown 1y-LdQ_4dbOip6sBgZ-Ub1FI6Hh5kl3h1" - ], + "execution_count": null, "metadata": { "id": "x9DwxHaAj3V4" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "# !gdown 1Hkk2HNvnh2cZqIcOGuxpxUkDSDh-QW86\n", + "!gdown 1y-LdQ_4dbOip6sBgZ-Ub1FI6Hh5kl3h1" + ] }, { "cell_type": "code", @@ -103,14 +104,14 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "YlN_UCYM4oH9" + }, "source": [ "The following code loads images and builds a preprocessing pipeline within Datasets (Tensorflow-specific structures providing input data).\n", "\n", "Documentation: https://www.tensorflow.org/guide/data" - ], - "metadata": { - "id": "YlN_UCYM4oH9" - } + ] }, { "cell_type": "code", @@ -169,16 +170,16 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "ws_I8t8f5QeB" + }, "source": [ "This code builds a model that will be trained in the following cells. The field `base_model` is initialized with a pre-defined architecture loaded from Keras.\n", "\n", "You can find the list of possible architectures at https://keras.io/api/applications/\n", "\n", "On top of `base_model`, we add a fully-connected layer with 3 neurons, and a softmax activation function. This settings allows scaling the output in a way that it can be interpreted as a probability distribution where all of the probabilities sum up to 1.\n" - ], - "metadata": { - "id": "ws_I8t8f5QeB" - } + ] }, { "cell_type": "code", @@ -191,12 +192,13 @@ "class Places365Model(tf.keras.Model):\n", " def __init__(self):\n", " super().__init__()\n", - " self.base_model = tf.keras.applications.MobileNet(\n", + " self.base_model = tf.keras.applications.MobileNetV3Small(\n", " input_shape=(224, 224, 3),\n", " include_top=False,\n", " weights=None,\n", " classes=3,\n", - " pooling=\"avg\"\n", + " pooling=\"avg\",\n", + " minimalistic=True\n", " )\n", " self.fc = tf.keras.layers.Dense(3, activation='softmax')\n", "\n", @@ -206,7 +208,15 @@ "\n", "\n", "model = Places365Model()\n", - "model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics='accuracy')" + "\n", + "# updating momentum of the BatchNorm layers\n", + "for layer in model.base_model.layers:\n", + " if isinstance(layer, tf.keras.layers.BatchNormalization):\n", + " layer.momentum = 0.5\n", + "\n", + "optimizer = tf.keras.optimizers.Adam(learning_rate=0.005)\n", + "\n", + "model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics='accuracy')" ] }, { @@ -228,7 +238,7 @@ "source": [ "history = model.fit(\n", " train_ds,\n", - " epochs=10)" + " epochs=32)" ] }, { @@ -242,12 +252,12 @@ }, { "cell_type": "markdown", - "source": [ - "Let's see the results of our training!" - ], "metadata": { "id": "Lhdx7LkU_vxx" - } + }, + "source": [ + "Let's see the results of our training!" + ] }, { "cell_type": "code", @@ -272,16 +282,16 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "KNtWihJPAfes" + }, "source": [ "Confusion matrix is a tool that helps us to interpret the results and draw conclusions about the characteristics of the model.\n", "\n", "It is a table presenting the number of correct predictions, and showing us how many mistakes it made, specifically focusing on the confusions between classes.\n", "\n", "A row represents the true class, and column represents a predicted one." - ], - "metadata": { - "id": "KNtWihJPAfes" - } + ] }, { "cell_type": "code", @@ -291,8 +301,10 @@ }, "outputs": [], "source": [ - "results = model.predict(val_ds)\n", - "ds = list(val_ds.unbatch().as_numpy_iterator())\n", + "ds_for_prediction = train_ds\n", + "\n", + "results = model.predict(ds_for_prediction)\n", + "ds = list(ds_for_prediction.unbatch().as_numpy_iterator())\n", "\n", "y_true = [class_names[entry[1]] for entry in ds]\n", "y_pred = [class_names[np.argmax(result)] for result in results]\n", @@ -317,8 +329,11 @@ }, { "cell_type": "markdown", + "metadata": { + "id": "VjeQNCVsBScm" + }, "source": [ - "Let's see the classifiation report!\n", + "Let's see the classification report!\n", "\n", "We can observe the following metrics:\n", "- **precision** - this a frequentist probability that the predicted class is true. It is calculated with the equation TP / (TP + FP).\n", @@ -339,10 +354,7 @@ "The following article explains all the above quite well:\n", "\n", "https://medium.com/analytics-vidhya/confusion-matrix-accuracy-precision-recall-f1-score-ade299cf63cd" - ], - "metadata": { - "id": "VjeQNCVsBScm" - } + ] }, { "cell_type": "code", @@ -366,12 +378,12 @@ }, { "cell_type": "markdown", - "source": [ - "Let's plot the misclassified images and see how our model confused their classes!" - ], "metadata": { "id": "iU16Qm19Goai" - } + }, + "source": [ + "Let's plot the misclassified images and see how our model confused their classes!" + ] }, { "cell_type": "code", @@ -403,9 +415,9 @@ "metadata": { "accelerator": "GPU", "colab": { - "name": "places365 - train.ipynb", - "provenance": [], - "collapsed_sections": [] + "collapsed_sections": [], + "name": "places365_train.ipynb", + "provenance": [] }, "kernelspec": { "display_name": "Python 3",