Skip to content

Commit aa1592d

Browse files
Fixed PyYAML issue and added ^C common error
1 parent 1b1514a commit aa1592d

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

Train_TFLite2_Object_Detction_Model.ipynb

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
"outputs": [],
159159
"source": [
160160
"# Install the Object Detection API\n",
161+
"# Need to do a temporary fix with PyYAML because Colab isn't able to install PyYAML v5.4.1\n",
162+
"!pip install pyyaml==5.3\n",
161163
"!pip install /content/models/research/\n",
162164
"\n",
163165
"# Need to downgrade to TF v2.8.0 due to Colab compatibility bug with TF v2.10 (as of 10/03/22)\n",
@@ -170,7 +172,7 @@
170172
"id": "6V7TrfUos-9E"
171173
},
172174
"source": [
173-
"You may get warnings or errors related to package dependencies in the previous code block, but you can ignore them for now. \n",
175+
"You may get warnings or errors related to package dependencies in the previous code block, but you can ignore them for now.\n",
174176
"\n",
175177
"Let's test our installation by running `model_builder_tf2_test.py` to make sure everything is working as expected. Run the following code block and confirm that it finishes without errors. If you get errors, try Googling them or checking the FAQ at the end of this Colab."
176178
]
@@ -351,7 +353,7 @@
351353
},
352354
"source": [
353355
"## 3.3 Create Labelmap and TFRecords\n",
354-
"Finally, we need to create a labelmap for the detector and convert the images into a data file format called TFRecords, which are used by TensorFlow for training. We'll use Python scripts to automatically convert the data into TFRecord format. Before running them, we need to define a labelmap for our classes. \n",
356+
"Finally, we need to create a labelmap for the detector and convert the images into a data file format called TFRecords, which are used by TensorFlow for training. We'll use Python scripts to automatically convert the data into TFRecord format. Before running them, we need to define a labelmap for our classes.\n",
355357
"\n",
356358
"The code section below will create a \"labelmap.txt\" file that contains a list of classes. Replace the `class1`, `class2`, `class3` text with your own classes (for example, `penny`, `nickel`, `dime`, `quarter`), adding a new line for each class. Then, click play to execute the code."
357359
]
@@ -609,11 +611,11 @@
609611
"with open(pipeline_fname) as f:\n",
610612
" s = f.read()\n",
611613
"with open('pipeline_file.config', 'w') as f:\n",
612-
" \n",
614+
"\n",
613615
" # Set fine_tune_checkpoint path\n",
614616
" s = re.sub('fine_tune_checkpoint: \".*?\"',\n",
615617
" 'fine_tune_checkpoint: \"{}\"'.format(fine_tune_checkpoint), s)\n",
616-
" \n",
618+
"\n",
617619
" # Set tfrecord files for train and test datasets\n",
618620
" s = re.sub(\n",
619621
" '(input_path: \".*?)(PATH_TO_BE_CONFIGURED/train)(.*?\")', 'input_path: \"{}\"'.format(train_record_fname), s)\n",
@@ -631,23 +633,23 @@
631633
" # Set training steps, num_steps\n",
632634
" s = re.sub('num_steps: [0-9]+',\n",
633635
" 'num_steps: {}'.format(num_steps), s)\n",
634-
" \n",
636+
"\n",
635637
" # Set number of classes num_classes\n",
636638
" s = re.sub('num_classes: [0-9]+',\n",
637639
" 'num_classes: {}'.format(num_classes), s)\n",
638640
"\n",
639641
" # Change fine-tune checkpoint type from \"classification\" to \"detection\"\n",
640642
" s = re.sub(\n",
641643
" 'fine_tune_checkpoint_type: \"classification\"', 'fine_tune_checkpoint_type: \"{}\"'.format('detection'), s)\n",
642-
" \n",
644+
"\n",
643645
" # If using ssd-mobilenet-v2, reduce learning rate (because it's too high in the default config file)\n",
644646
" if chosen_model == 'ssd-mobilenet-v2':\n",
645647
" s = re.sub('learning_rate_base: .8',\n",
646648
" 'learning_rate_base: .08', s)\n",
647-
" \n",
649+
"\n",
648650
" s = re.sub('warmup_learning_rate: 0.13333',\n",
649651
" 'warmup_learning_rate: .026666', s)\n",
650-
" \n",
652+
"\n",
651653
" # If using efficientdet-d0, use fixed_shape_resizer instead of keep_aspect_ratio_resizer (because it isn't supported by TFLite)\n",
652654
" if chosen_model == 'efficientdet-d0':\n",
653655
" s = re.sub('keep_aspect_ratio_resizer', 'fixed_shape_resizer', s)\n",
@@ -921,7 +923,7 @@
921923
" # Load image and resize to expected shape [1xHxWx3]\n",
922924
" image = cv2.imread(image_path)\n",
923925
" image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
924-
" imH, imW, _ = image.shape \n",
926+
" imH, imW, _ = image.shape\n",
925927
" image_resized = cv2.resize(image_rgb, (width, height))\n",
926928
" input_data = np.expand_dims(image_resized, axis=0)\n",
927929
"\n",
@@ -950,7 +952,7 @@
950952
" xmin = int(max(1,(boxes[i][1] * imW)))\n",
951953
" ymax = int(min(imH,(boxes[i][2] * imH)))\n",
952954
" xmax = int(min(imW,(boxes[i][3] * imW)))\n",
953-
" \n",
955+
"\n",
954956
" cv2.rectangle(image, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)\n",
955957
"\n",
956958
" # Draw label\n",
@@ -963,19 +965,19 @@
963965
"\n",
964966
" detections.append([object_name, scores[i], xmin, ymin, xmax, ymax])\n",
965967
"\n",
966-
" \n",
968+
"\n",
967969
" # All the results have been drawn on the image, now display the image\n",
968970
" if txt_only == False: # \"text_only\" controls whether we want to display the image results or just save them in .txt files\n",
969971
" image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n",
970972
" plt.figure(figsize=(12,16))\n",
971973
" plt.imshow(image)\n",
972974
" plt.show()\n",
973-
" \n",
975+
"\n",
974976
" # Save detection results in .txt files (for calculating mAP)\n",
975977
" elif txt_only == True:\n",
976978
"\n",
977979
" # Get filenames and paths\n",
978-
" image_fn = os.path.basename(image_path) \n",
980+
" image_fn = os.path.basename(image_path)\n",
979981
" base_fn, ext = os.path.splitext(image_fn)\n",
980982
" txt_result_fn = base_fn +'.txt'\n",
981983
" txt_savepath = os.path.join(savepath, txt_result_fn)\n",
@@ -1021,7 +1023,7 @@
10211023
"cell_type": "markdown",
10221024
"source": [
10231025
"### 7.2 Calculate mAP\n",
1024-
"Now we have a visual sense of how our model performs on test images, but how can we quantitatively measure its accuracy? \n",
1026+
"Now we have a visual sense of how our model performs on test images, but how can we quantitatively measure its accuracy?\n",
10251027
"\n",
10261028
"One popular methord for measuring object detection model accuracy is \"mean average precision\" (mAP). Basically, the higher the mAP score, the better your model is at detecting objects in images. To learn more about mAP, read through this [article from Roboflow](https://blog.roboflow.com/mean-average-precision/).\n",
10271029
"\n",
@@ -1037,9 +1039,9 @@
10371039
"%%bash\n",
10381040
"git clone https://github.com/Cartucho/mAP /content/mAP\n",
10391041
"cd /content/mAP\n",
1040-
"rm input/detection-results/* \n",
1041-
"rm input/ground-truth/* \n",
1042-
"rm input/images-optional/* \n",
1042+
"rm input/detection-results/*\n",
1043+
"rm input/ground-truth/*\n",
1044+
"rm input/images-optional/*\n",
10431045
"wget https://raw.githubusercontent.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/master/util_scripts/calculate_map_cartucho.py"
10441046
],
10451047
"metadata": {
@@ -1237,7 +1239,7 @@
12371239
"id": "GSJ2wgGCixy2"
12381240
},
12391241
"source": [
1240-
"## 8.2. Deploy model \n",
1242+
"## 8.2. Deploy model\n",
12411243
"TensorFlow Lite models can run on a wide variety of hardware, including PCs, embedded systems, and phones. This section provides instructions showing how to deploy your TFLite model on various devices.\n",
12421244
"\n",
12431245
"### 8.2.1. Deploy on Raspberry Pi\n",
@@ -1263,7 +1265,7 @@
12631265
"A window will appear showing a live feed from your webcam with boxes drawn around detected objects in each frame.\n",
12641266
"\n",
12651267
"### 8.2.2. Deploy on Windows, Linux, or macOS\n",
1266-
"Follow the instructions linked below to quickly set up your Windows, Linux, or macOS computer to run TFLite models. It only takes a few minutes! Running a model on your PC is good for quickly testing your model with a webcam. However, keep in mind that the TFLite Runtime is optimized for lower-power processors, and it won't utilize the full capability of your PC's processor. \n",
1268+
"Follow the instructions linked below to quickly set up your Windows, Linux, or macOS computer to run TFLite models. It only takes a few minutes! Running a model on your PC is good for quickly testing your model with a webcam. However, keep in mind that the TFLite Runtime is optimized for lower-power processors, and it won't utilize the full capability of your PC's processor.\n",
12671269
"\n",
12681270
"Here are links to the deployment guides for Windows, Linux, and macOS:\n",
12691271
"* [How to Run TensorFlow Lite Models on Windows](https://github.com/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/deploy_guides/Windows_TFLite_Guide.md)\n",
@@ -1557,7 +1559,7 @@
15571559
"! curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -\n",
15581560
"! echo \"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list\n",
15591561
"! sudo apt-get update\n",
1560-
"! sudo apt-get install edgetpu-compiler\t"
1562+
"! sudo apt-get install edgetpu-compiler"
15611563
]
15621564
},
15631565
{
@@ -1641,7 +1643,13 @@
16411643
"id": "sEbd9cO7I_o3"
16421644
},
16431645
"source": [
1644-
"Here are solutions to common errors that can occur while stepping through this notebook."
1646+
"Here are solutions to common errors that can occur while stepping through this notebook.\n",
1647+
"\n",
1648+
"**1. Training suddenly stops with ^C output**\n",
1649+
"\n",
1650+
"If your training randomly stops without any error messages except a `^C`, that means the virtual machine has run out of memory. To resolve the issue, try reducing the `batch_size` variable in Step 4 to a lower value like `batch_size = 4`. The value must be a power of 2. (e.g. 2, 4, 8 ...)\n",
1651+
"\n",
1652+
"Source: https://stackoverflow.com/questions/75901898/why-my-model-training-automatically-stopped-during-training"
16451653
]
16461654
}
16471655
],
@@ -1662,7 +1670,7 @@
16621670
"WoptFnAhCSrR",
16631671
"5VI_Gh5dCd7w"
16641672
],
1665-
"authorship_tag": "ABX9TyOihM81eF7ffleAR5jS8BVt",
1673+
"authorship_tag": "ABX9TyPPnvPyzD3u2VVWwYcBGqy2",
16661674
"include_colab_link": true
16671675
},
16681676
"gpuClass": "standard",

0 commit comments

Comments
 (0)