Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-028
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ramanayaka D.H.
2023-028
Commits
0a1efdf1
Commit
0a1efdf1
authored
May 25, 2023
by
Marasinghe G.M.H.T.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Here this is the code which I used to train my dataset to MobilenetV2 pretrained ML model
parent
df4e099e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
206 additions
and
0 deletions
+206
-0
Backup_Mobilenet.ipynb
Backup_Mobilenet.ipynb
+206
-0
No files found.
Backup_Mobilenet.ipynb
0 → 100644
View file @
0a1efdf1
{
"cells": [
{
"cell_type": "code",
"execution_count": 6,
"id": "0f8a8071",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found 1857 images belonging to 2 classes.\n",
"Found 463 images belonging to 2 classes.\n",
"Epoch 1/10\n",
"59/59 [==============================] - 51s 778ms/step - loss: 0.2540 - accuracy: 0.9251 - val_loss: 0.1802 - val_accuracy: 1.0000\n",
"Epoch 2/10\n",
"59/59 [==============================] - 44s 739ms/step - loss: 0.0450 - accuracy: 1.0000 - val_loss: 0.1289 - val_accuracy: 1.0000\n",
"Epoch 3/10\n",
"59/59 [==============================] - 44s 743ms/step - loss: 0.0229 - accuracy: 1.0000 - val_loss: 0.0968 - val_accuracy: 1.0000\n",
"Epoch 4/10\n",
"59/59 [==============================] - 44s 739ms/step - loss: 0.0145 - accuracy: 1.0000 - val_loss: 0.0778 - val_accuracy: 1.0000\n",
"Epoch 5/10\n",
"59/59 [==============================] - 44s 739ms/step - loss: 0.0103 - accuracy: 1.0000 - val_loss: 0.0658 - val_accuracy: 1.0000\n",
"Epoch 6/10\n",
"59/59 [==============================] - 44s 746ms/step - loss: 0.0077 - accuracy: 1.0000 - val_loss: 0.0571 - val_accuracy: 1.0000\n",
"Epoch 7/10\n",
"59/59 [==============================] - 44s 742ms/step - loss: 0.0061 - accuracy: 1.0000 - val_loss: 0.0476 - val_accuracy: 1.0000\n",
"Epoch 8/10\n",
"59/59 [==============================] - 44s 737ms/step - loss: 0.0050 - accuracy: 1.0000 - val_loss: 0.0420 - val_accuracy: 1.0000\n",
"Epoch 9/10\n",
"59/59 [==============================] - 44s 742ms/step - loss: 0.0041 - accuracy: 1.0000 - val_loss: 0.0386 - val_accuracy: 1.0000\n",
"Epoch 10/10\n",
"59/59 [==============================] - 44s 742ms/step - loss: 0.0035 - accuracy: 1.0000 - val_loss: 0.0379 - val_accuracy: 1.0000\n",
"15/15 [==============================] - 9s 567ms/step - loss: 0.0379 - accuracy: 1.0000\n",
"Validation accuracy: 100.00%\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"import tensorflow_hub as hub\n",
"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
"from PIL import Image\n",
"\n",
"# Define the paths to your dataset folders\n",
"dataset_path = r'C:\\Users\\A C E R\\Desktop\\ELBOW ANGLE'\n",
"correct_position_path = r'C:\\Users\\A C E R\\Desktop\\ELBOW ANGLE\\Corect Position'\n",
"wrong_position_path = r'C:\\Users\\A C E R\\Desktop\\ELBOW ANGLE\\Wrong Posision'\n",
"\n",
"# Parameters for training\n",
"batch_size = 32\n",
"image_size = (224, 224)\n",
"num_epochs = 10\n",
"learning_rate = 0.001\n",
"\n",
"# Create data generators for training and validation\n",
"datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n",
"\n",
"train_generator = datagen.flow_from_directory(\n",
" dataset_path,\n",
" target_size=image_size,\n",
" batch_size=batch_size,\n",
" class_mode='binary',\n",
" subset='training'\n",
")\n",
"\n",
"val_generator = datagen.flow_from_directory(\n",
" dataset_path,\n",
" target_size=image_size,\n",
" batch_size=batch_size,\n",
" class_mode='binary',\n",
" subset='validation'\n",
")\n",
"\n",
"# Load the MobileNet model from TensorFlow Hub\n",
"model_url = 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'\n",
"base_model = hub.KerasLayer(model_url, input_shape=image_size + (3,))\n",
"\n",
"# Freeze the base model\n",
"base_model.trainable = False\n",
"\n",
"# Build the model\n",
"model = tf.keras.Sequential([\n",
" base_model,\n",
" tf.keras.layers.Dense(1, activation='sigmoid')\n",
"])\n",
"\n",
"# Compile the model\n",
"model.compile(\n",
" optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),\n",
" loss=tf.keras.losses.BinaryCrossentropy(),\n",
" metrics=['accuracy']\n",
")\n",
"\n",
"# Train the model\n",
"history = model.fit(\n",
" train_generator,\n",
" validation_data=val_generator,\n",
" epochs=num_epochs\n",
")\n",
"\n",
"# Evaluate the model on the validation set\n",
"_, accuracy = model.evaluate(val_generator)\n",
"print(f'Validation accuracy: {accuracy*100:.2f}%')\n",
"\n",
"# Load a test image and predict the class label\n",
"#test_image_path = 'C:\\\\Users\\\\A C E R\\\\Desktop\\\\image_2.jpeg' # Provide the path to your test image\n",
"#test_image = Image.open(test_image_path)\n",
"#test_image = test_image.resize(image_size) # Resize the image to match the input size of the model\n",
"#test_image = tf.keras.preprocessing.image.img_to_array(test_image)\n",
"#test_image = test_image / 255.0 # Normalize the image\n",
"\n",
"# Expand dimensions to create a batch of size 1\n",
"#test_image = tf.expand_dims(test_image, axis=0)\n",
"\n",
"# Predict the class probability\n",
"#prediction = model.predict(test_image)[0][0]\n",
"\n",
"# Determine the predicted class label based on the threshold of 0.5\n",
"#predicted_label = 'Correct Position' if prediction >= 0.5 else 'Incorrect Position'\n",
"\n",
"# Print the predicted class label and probability\n",
"#print(f'Test image prediction: {predicted_label}')\n",
"#print(f'Prediction probability: {prediction}')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8037638f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1/1 [==============================] - 1s 530ms/step\n",
"Test image prediction: Correct Position\n",
"Prediction probability: 0.9298062324523926\n"
]
}
],
"source": [
"# Load a test image and predict the class label\n",
"test_image_path = 'C:\\\\Users\\\\A C E R\\\\Desktop\\\\image_2.jpeg' # Provide the path to your test image\n",
"test_image = Image.open(test_image_path)\n",
"test_image = test_image.resize(image_size) # Resize the image to match the input size of the model\n",
"test_image = tf.keras.preprocessing.image.img_to_array(test_image)\n",
"test_image = test_image / 255.0 # Normalize the image\n",
"\n",
"# Expand dimensions to create a batch of size 1\n",
"test_image = tf.expand_dims(test_image, axis=0)\n",
"\n",
"# Predict the class probability\n",
"prediction = model.predict(test_image)[0][0]\n",
"\n",
"# Determine the predicted class label based on the threshold of 0.5\n",
"predicted_label = 'Correct Position' if prediction >= 0.5 else 'Incorrect Position'\n",
"\n",
"# Print the predicted class label and probability\n",
"print(f'Test image prediction: {predicted_label}')\n",
"print(f'Prediction probability: {prediction}')\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ec84268",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd28c2e7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment