### Test Installation Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Run the model builder test script to verify the installation. ```bash python object_detection/builders/model_builder_test.py ``` -------------------------------- ### Install dependencies via apt-get Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md System-level installation of required libraries on Ubuntu 16.04. ```bash sudo apt-get install protobuf-compiler python-pil python-lxml python-tk pip install --user Cython pip install --user contextlib2 pip install --user jupyter pip install --user matplotlib ``` -------------------------------- ### Manual Protobuf installation on MacOS Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Commands to install the protobuf compiler on macOS using Homebrew or direct download. ```bash brew install protobuf ``` ```bash PROTOC_ZIP=protoc-3.3.0-osx-x86_64.zip curl -OL https://github.com/google/protobuf/releases/download/v3.3.0/$PROTOC_ZIP sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc rm -f $PROTOC_ZIP ``` ```bash # From tensorflow/models/research/ protoc object_detection/protos/*.proto --python_out=. ``` -------------------------------- ### Manual Protobuf installation on Linux Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Download and use a specific version of the protobuf compiler if the system version is incompatible. ```bash # From tensorflow/models/research/ wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip unzip protobuf.zip ``` ```bash # From tensorflow/models/research/ ./bin/protoc object_detection/protos/*.proto --python_out=. ``` -------------------------------- ### Install dependencies via pip Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Alternative method to install required libraries using pip. ```bash pip install --user Cython pip install --user contextlib2 pip install --user pillow pip install --user lxml pip install --user jupyter pip install --user matplotlib ``` -------------------------------- ### Run Tensorboard Locally Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md Start Tensorboard on your local machine to visualize training and evaluation progress from Google Cloud ML. Ensure `YOUR_CLOUD_BUCKET` is replaced with your actual GCS bucket name. It may take a few minutes for results to appear. ```bash tensorboard --logdir=gs://${YOUR_CLOUD_BUCKET} ``` -------------------------------- ### Install Tensorflow via pip Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Commands to install either the CPU or GPU version of Tensorflow. ```bash # For CPU pip install tensorflow # For GPU pip install tensorflow-gpu ``` -------------------------------- ### Install COCO API Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Commands to clone, build, and move the COCO API tools to the research directory. ```bash git clone https://github.com/cocodataset/cocoapi.git cd cocoapi/PythonAPI make cp -r pycocotools /models/research/ ``` -------------------------------- ### Authenticate and Run Tensorboard Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Run this command to authenticate your local machine for GCS access and then start Tensorboard to monitor training progress. Access Tensorboard via localhost:6006. ```bash gcloud auth application-default login ``` ```bash tensorboard --logdir=gs://${YOUR_GCS_BUCKET}/model_dir ``` -------------------------------- ### TFRecord Input Configuration Example Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/configuring_jobs.md Example of an input configuration for the TensorFlow Object Detection API using TFRecord files. Specify the path to the training data and the label map. ```protobuf tf_record_input_reader { input_path: "/usr/home/username/data/train.record" } label_map_path: "/usr/home/username/data/label_map.pbtxt" ``` -------------------------------- ### Install APK via ADB Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_mobile_tensorflowlite.md Command to install the generated APK onto a connected Android device. ```shell adb install bazel-bin/tensorflow/lite/examples/android/tflite_demo.apk ``` -------------------------------- ### Run the Docker container Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/dockerfiles/android/README.md Starts the container in interactive mode with privileged access and port mapping for TensorBoard. ```bash docker run --rm -it --privileged -p 6006:6006 detect-tf ``` -------------------------------- ### EczemaNet Training Pipeline Setup Source: https://context7.com/tanaka-group/eczemanet/llms.txt Sets up the training pipeline for EczemaNet, including loading metadata, splitting data into train/test sets, loading and preprocessing images, and preparing ordinal labels. The label ordinariiser converts severity scores into a binary encoding suitable for the model. ```python from lib.EczemaNet_helper import train_model, load_images, label_ordinariser, weight_ratio import numpy as np import pandas as pd # Configuration BRANCHES_LIST = ['sassad_cra', 'sassad_dry', 'tiss_ery', 'tiss_exc', 'sassad_exu', 'sassad_lic', 'tiss_oed'] # Load metadata containing image paths and labels meta_data = pd.read_csv("data/meta_data.csv") # Expected columns: filepath, refno, visno, sassad_cra, sassad_dry, tiss_ery, etc. # Split into train/test meta_train = meta_data[meta_data['refno'].isin(train_refnos)] meta_test = meta_data[meta_data['refno'].isin(test_refnos)] # Load and preprocess images x_train = load_images(meta_train['filepath'], image_size=224) x_test = load_images(meta_test['filepath'], image_size=224) # Prepare ordinal labels (convert 0,1,2,3 -> binary encoding) y_train = {} y_test = {} for branch in BRANCHES_LIST: y_train[branch + "_output"] = np.array(label_ordinariser(meta_train[branch])) y_test[branch + "_output"] = np.array(label_ordinariser(meta_test[branch])) # Example: severity 2 -> [1, 1, 0], severity 3 -> [1, 1, 1] ``` -------------------------------- ### Create TFRecord Conversion Script Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/using_your_own_dataset.md Template for converting custom data into TensorFlow Example protos. Requires populating image metadata and bounding box coordinates. ```python import tensorflow as tf from object_detection.utils import dataset_util flags = tf.app.flags flags.DEFINE_string('output_path', '', 'Path to output TFRecord') FLAGS = flags.FLAGS def create_tf_example(example): # TODO(user): Populate the following variables from your example. height = None # Image height width = None # Image width filename = None # Filename of the image. Empty if image is not from file encoded_image_data = None # Encoded image bytes image_format = None # b'jpeg' or b'png' xmins = [] # List of normalized left x coordinates in bounding box (1 per box) xmaxs = [] # List of normalized right x coordinates in bounding box # (1 per box) ymins = [] # List of normalized top y coordinates in bounding box (1 per box) ymaxs = [] # List of normalized bottom y coordinates in bounding box # (1 per box) classes_text = [] # List of string class name of bounding box (1 per box) classes = [] # List of integer class id of bounding box (1 per box) tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example def main(_): writer = tf.python_io.TFRecordWriter(FLAGS.output_path) # TODO(user): Write code to read in your dataset to examples variable for example in examples: tf_example = create_tf_example(example) writer.write(tf_example.SerializeToString()) writer.close() if __name__ == '__main__': tf.app.run() ``` -------------------------------- ### Create TFRecord Example for an Image Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/using_your_own_dataset.md This Python function generates a tf.Example proto for an image, suitable for TFRecord files. It requires the encoded image data and defines image dimensions, filename, and bounding box information including normalized coordinates, class text, and labels. Ensure you have the `dataset_util` module available for feature creation. ```python def create_cat_tf_example(encoded_cat_image_data): """Creates a tf.Example proto from sample cat image. Args: encoded_cat_image_data: The jpg encoded data of the cat image. Returns: example: The created tf.Example. """ height = 1032.0 width = 1200.0 filename = 'example_cat.jpg' image_format = b'jpg' xmins = [322.0 / 1200.0] xmaxs = [1062.0 / 1200.0] ymins = [174.0 / 1032.0] ymaxs = [761.0 / 1032.0] classes_text = ['Cat'] classes = [1] tf_example = tf.train.Example(features=tf.train.Features(feature={ 'image/height': dataset_util.int64_feature(height), 'image/width': dataset_util.int64_feature(width), 'image/filename': dataset_util.bytes_feature(filename), 'image/source_id': dataset_util.bytes_feature(filename), 'image/encoded': dataset_util.bytes_feature(encoded_image_data), 'image/format': dataset_util.bytes_feature(image_format), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), 'image/object/class/label': dataset_util.int64_list_feature(classes), })) return tf_example ``` -------------------------------- ### Sharded Output File Pattern Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/using_your_own_dataset.md Example of the file naming convention produced by the sharding process. ```bash /path/to/train_dataset.record-00000-00010 /path/to/train_dataset.record-00001-00010 ... /path/to/train_dataset.record-00009-00010 ``` -------------------------------- ### TPU Friendly Loss Configuration Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Substitute `hard_example_miner` with `weighted_sigmoid_focal` loss for classification to ensure TPU compatibility, as non-max suppression used in hard example mining is unsupported on TPUs. ```protobuf loss { classification_loss { weighted_sigmoid_focal { alpha: 0.25 gamma: 2.0 } } localization_loss { weighted_smooth_l1 { } } classification_weight: 1.0 localization_weight: 1.0 } ``` -------------------------------- ### Submit Evaluation Job on CMLE Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md Run this command to start an evaluation job. This is necessary when training with TPU, as TPU training does not interleave evaluation. Ensure `MODEL_DIR` and `PIPELINE_CONFIG_PATH` are set to valid GCS paths. `checkpoint_dir` should point to where training checkpoints are saved. ```bash gcloud ml-engine jobs submit training object_detection_eval_`date +%m_%d_%Y_%H_%M_%S` \ --runtime-version 1.12 \ --job-dir=gs://${MODEL_DIR} \ --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz,/tmp/pycocotools/pycocotools-2.0.tar.gz \ --module-name object_detection.model_main \ --region us-central1 \ --scale-tier BASIC_GPU \ -- \ --model_dir=gs://${MODEL_DIR} \ --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} \ --checkpoint_dir=gs://${MODEL_DIR} ``` -------------------------------- ### Launch Tensorboard Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_locally.md Command to monitor training and evaluation progress by pointing to the model directory. ```bash tensorboard --logdir=${MODEL_DIR} ``` -------------------------------- ### Set up and run VRD evaluation script Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/challenge_evaluation.md This command sets up the necessary environment variables and executes the Open Images Visual Relationships Detection (VRD) challenge evaluation script. Ensure all input paths are correctly configured. ```bash INPUT_ANNOTATIONS_BOXES=/path/to/challenge-2018-train-vrd.csv INPUT_ANNOTATIONS_LABELS=/path/to/challenge-2018-train-vrd-labels.csv INPUT_PREDICTIONS=/path/to/predictions.csv INPUT_CLASS_LABELMAP=/path/to/oid_object_detection_challenge_500_label_map.pbtxt INPUT_RELATIONSHIP_LABELMAP=/path/to/relationships_labelmap.pbtxt OUTPUT_METRICS=/path/to/output/metrics/file echo "item { name: '/m/02gy9n' id: 602 display_name: 'Transparent' } item { name: '/m/05z87' id: 603 display_name: 'Plastic' } item { name: '/m/0dnr7' id: 604 display_name: '(made of)Textile' } item { name: '/m/04lbp' id: 605 display_name: '(made of)Leather' } item { name: '/m/083vt' id: 606 display_name: 'Wooden'} ">>${INPUT_CLASS_LABELMAP} echo "item { name: 'at' id: 1 display_name: 'at' } item { name: 'on' id: 2 display_name: 'on (top of)' } item { name: 'holds' id: 3 display_name: 'holds' } item { name: 'plays' id: 4 display_name: 'plays' } item { name: 'interacts_with' id: 5 display_name: 'interacts with' } item { name: 'wears' id: 6 display_name: 'wears' } item { name: 'is' id: 7 display_name: 'is' } item { name: 'inside_of' id: 8 display_name: 'inside of' } item { name: 'under' id: 9 display_name: 'under' } item { name: 'hits' id: 10 display_name: 'hits' } "> ${INPUT_RELATIONSHIP_LABELMAP} python object_detection/metrics/oid_vrd_challenge_evaluation.py \ --input_annotations_boxes=${INPUT_ANNOTATIONS_BOXES} \ --input_annotations_labels=${INPUT_ANNOTATIONS_LABELS} \ --input_predictions=${INPUT_PREDICTIONS} \ --input_class_labelmap=${INPUT_CLASS_LABELMAP} \ --input_relationship_labelmap=${INPUT_RELATIONSHIP_LABELMAP} \ --output_metrics=${OUTPUT_METRICS} ``` -------------------------------- ### Execute Training Job Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_locally.md Command to initiate the training process using the model_main.py script. ```bash # From the tensorflow/models/research/ directory PIPELINE_CONFIG_PATH={path to pipeline config file} MODEL_DIR={path to model directory} NUM_TRAIN_STEPS=50000 SAMPLE_1_OF_N_EVAL_EXAMPLES=1 python object_detection/model_main.py \ --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ --model_dir=${MODEL_DIR} \ --num_train_steps=${NUM_TRAIN_STEPS} \ --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ --alsologtostderr ``` -------------------------------- ### Create Evaluation Configuration Files Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/oid_inference_and_evaluation.md Generates the input and evaluation configuration files required for computing detection metrics. ```bash # From tensorflow/models/research/oid SPLIT=validation # or test NUM_SHARDS=1 # Set to NUM_GPUS if using the parallel evaluation script above mkdir -p ${SPLIT}_eval_metrics echo " label_map_path: '../object_detection/data/oid_bbox_trainable_label_map.pbtxt' tf_record_input_reader: { input_path: '${SPLIT}_detections.tfrecord@${NUM_SHARDS}' } " > ${SPLIT}_eval_metrics/${SPLIT}_input_config.pbtxt echo " metrics_set: 'oid_V2_detection_metrics' " > ${SPLIT}_eval_metrics/${SPLIT}_eval_config.pbtxt ``` -------------------------------- ### Configure Model Paths and Branches Source: https://github.com/tanaka-group/eczemanet/blob/master/notebooks/Model Training.ipynb Sets up constants for the model name, data path, output directory, and the list of branches to be processed. ```python ECZEMANET_MODEL="eczemanet_models.EczemaNet_VGG16" PATH_TO_DATASET = "../data" OUTPUT_PATH = "../output/EczemaNet_VGG16/" BRANCHES_LIST = ['sassad_cra','sassad_dry','tiss_ery','tiss_exc','sassad_exu','sassad_lic','tiss_oed'] ``` -------------------------------- ### Environment Initialization and GPU Verification Source: https://context7.com/tanaka-group/eczemanet/llms.txt Commands to create the environment and verify that the GPU is accessible by Keras. ```bash # Create and activate environment conda env create -f environment.yml conda activate EczemaNet # Verify GPU availability python -c "from keras import backend as K; print(len(K.tensorflow_backend._get_available_gpus()))" ``` -------------------------------- ### Export TensorFlow Lite SSD Graph Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_mobile_tensorflowlite.md Export a frozen graph compatible with TensorFlow Lite from an SSD model checkpoint. Ensure you have the necessary Python libraries installed. ```shell object_detection/export_tflite_ssd_graph.py \ --pipeline_config_path=$CONFIG_FILE \ --trained_checkpoint_prefix=$CHECKPOINT_PATH \ --output_directory=$OUTPUT_DIR \ --add_postprocessing_op=true ``` -------------------------------- ### Initialize Environment and Imports Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Imports necessary libraries and verifies the TensorFlow version requirement. ```python import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import zipfile from distutils.version import StrictVersion from collections import defaultdict from io import StringIO from matplotlib import pyplot as plt from PIL import Image # This is needed since the notebook is stored in the object_detection folder. sys.path.append("..") from object_detection.utils import ops as utils_ops if StrictVersion(tf.__version__) < StrictVersion('1.12.0'): raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.') ``` -------------------------------- ### Configure Manual Step Learning Rate Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Define a manual step learning rate schedule with warmup. This allows for custom learning rate adjustments at specific training steps. ```protobuf learning_rate: { manual_step_learning_rate { warmup: true initial_learning_rate: .01333 schedule { step: 2000 learning_rate: 0.04 } schedule { step: 15000 learning_rate: 0.004 } } } ``` -------------------------------- ### Configure and Upload Pipeline Config Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Updates the configuration file placeholders with the GCS bucket path and uploads the file to the cloud. ```bash # From tensorflow/models/research/ # Edit the faster_rcnn_resnet101_pets.config template. Please note that there # are multiple places where PATH_TO_BE_CONFIGURED needs to be set. sed -i "s|PATH_TO_BE_CONFIGURED|"gs://${YOUR_GCS_BUCKET}"/data|g" \ object_detection/samples/configs/faster_rcnn_resnet101_pets.config # Copy edited template to cloud. gsutil cp object_detection/samples/configs/faster_rcnn_resnet101_pets.config \ gs://${YOUR_GCS_BUCKET}/data/faster_rcnn_resnet101_pets.config ``` -------------------------------- ### Import Object Detection Utilities Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Imports specific utility modules for label mapping and visualization. ```python from utils import label_map_util from utils import visualization_utils as vis_util ``` -------------------------------- ### Build the Docker image Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/dockerfiles/android/README.md Builds the Docker container from the current directory using the specified tag. ```bash docker build --tag detect-tf . ``` -------------------------------- ### Submit TPU Training Job on CMLE Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md Use this command to launch a training job with a TPU-compatible pipeline configuration. Ensure `MODEL_DIR` and `PIPELINE_CONFIG_PATH` are set to valid GCS paths. Requires specifying `tpu_zone` and `scale-tier` as `BASIC_TPU`. ```bash gcloud ml-engine jobs submit training `whoami`_object_detection_`date +%m_%d_%Y_%H_%M_%S` \ --job-dir=gs://${MODEL_DIR} \ --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz,/tmp/pycocotools/pycocotools-2.0.tar.gz \ --module-name object_detection.model_tpu_main \ --runtime-version 1.12 \ --scale-tier BASIC_TPU \ --region us-central1 \ -- \ --tpu_zone us-central1 \ --model_dir=gs://${MODEL_DIR} \ --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} ``` -------------------------------- ### Define Model Configuration Variables Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Sets paths and names for the model and label map files. ```python # What model to download. MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17' MODEL_FILE = MODEL_NAME + '.tar.gz' DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/' # Path to frozen detection graph. This is the actual model that is used for the object detection. PATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb' # List of the strings that is used to add correct label for each box. PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt') ``` -------------------------------- ### Configure Cosine Decay Learning Rate Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Set up a cosine decay learning rate schedule with warmup. The base learning rate should be scaled linearly with the batch size. ```protobuf learning_rate: { cosine_decay_learning_rate { learning_rate_base: .04 total_steps: 25000 warmup_learning_rate: .013333 warmup_steps: 2000 } } ``` -------------------------------- ### Define Test Image Paths Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Sets up a list of paths for images to be processed. ```python # For the sake of simplicity we will use only 2 images: # image1.jpg # image2.jpg # If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS. PATH_TO_TEST_IMAGES_DIR = 'test_images' TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ] ``` -------------------------------- ### Initialize and Run ROI Detector Source: https://context7.com/tanaka-group/eczemanet/llms.txt Initializes the ROI detector and runs inference on a single image to detect eczema lesions. Ensure the image path is correct and the ROI class is properly imported. ```python from lib.EczemaNet_helper import ROI from PIL import Image import numpy as np # Initialize ROI detector (loads frozen graph and label map) roi_detector = ROI() # Load and process an image image_path = "patient_photo.jpg" pil_image = Image.open(image_path) np_image = roi_detector.load_image_into_numpy_array(pil_image) # Run detection detection_result = roi_detector.run_inference_for_single_image(np_image) ``` -------------------------------- ### Create TFRecords for Open Images Dataset Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/oid_inference_and_evaluation.md Packages the Open Images dataset into TFRecords of TFExamples, which is the format required by the Object Detection API. This script requires the input CSV annotations, image directory, and a label map. The output is a set of TFRecord files. ```bash # From tensorflow/models/research/oid SPLIT=validation # Set SPLIT to "test" to create TFRecords for the test split mkdir ${SPLIT}_tfrecords PYTHONPATH=$PYTHONPATH:$(readlink -f ..) \ python -m object_detection.dataset_tools.create_oid_tf_record \ --input_box_annotations_csv 2017_07/$SPLIT/annotations-human-bbox.csv \ --input_images_directory raw_images_${SPLIT} \ --input_label_map ../object_detection/data/oid_bbox_trainable_label_map.pbtxt \ --output_tf_record_path_prefix ${SPLIT}_tfrecords/$SPLIT.tfrecord \ --num_shards=100 ``` -------------------------------- ### Sample Train Config with Optimizer and Data Augmentation Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/configuring_jobs.md A sample train_config for the TensorFlow Object Detection API, including batch size, optimizer settings with a manual step learning rate schedule, fine-tune checkpoint path, and data augmentation options like random horizontal flip. ```protobuf batch_size: 1 optimizer { momentum_optimizer: { learning_rate: { manual_step_learning_rate { initial_learning_rate: 0.0002 schedule { step: 0 learning_rate: .0002 } schedule { step: 900000 learning_rate: .00002 } schedule { step: 1200000 learning_rate: .000002 } } } momentum_optimizer_value: 0.9 } use_moving_average: false } fine_tune_checkpoint: "/usr/home/username/tmp/model.ckpt-#####" from_detection_checkpoint: true load_all_detection_checkpoint_vars: true gradient_clipping_by_norm: 10.0 data_augmentation_options { random_horizontal_flip { } } ``` -------------------------------- ### Build Android Demo App Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_mobile_tensorflowlite.md Bazel command to compile the TensorFlow Lite Android demo application. ```shell bazel build -c opt --config=android_arm{,64} --cxxopt='--std=c++11' "//tensorflow/lite/examples/android:tflite_demo" ``` -------------------------------- ### Image Resizer with Aspect Ratio Padding Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Alternatively, use `keep_aspect_ratio_resizer` with `pad_to_max_dimension: true` to pad images to a fixed spatial shape, ensuring static tensor shapes for TPU training. ```protobuf image_resizer { keep_aspect_ratio_resizer { min_dimension: 640 max_dimension: 640 pad_to_max_dimension: true } } ``` -------------------------------- ### Submit Training Job to Cloud ML Engine Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md Command to submit a training job to Google Cloud ML Engine. Ensure all package paths and directory variables are correctly set. ```bash # From tensorflow/models/research/ gcloud ml-engine jobs submit training object_detection_`date +%m_%d_%Y_%H_%M_%S` \ --runtime-version 1.12 \ --job-dir=gs://${MODEL_DIR} \ --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz,/tmp/pycocotools/pycocotools-2.0.tar.gz \ --module-name object_detection.model_main \ --region us-central1 \ --config ${PATH_TO_LOCAL_YAML_FILE} \ -- \ --model_dir=gs://${MODEL_DIR} \ --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} ``` -------------------------------- ### Skeleton Training Pipeline Configuration Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/configuring_jobs.md This is a skeleton configuration file for the TensorFlow Object Detection API training pipeline. Users should fill in the specific configurations for model, training, and input readers. ```protobuf model { (... Add model config here...) } train_config : { (... Add train_config here...) } train_input_reader: { (... Add train_input configuration here...) } eval_config: { } eval_input_reader: { (... Add eval_input configuration here...) } ``` -------------------------------- ### Define Directory Structure Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_locally.md Recommended file and folder organization for training and evaluation data. ```text +data -label_map file -train TFRecord file -eval TFRecord file +models + model -pipeline config file +train +eval ``` -------------------------------- ### Set Training Steps for TPU Training Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Specify the total number of training steps. For TPUs with large batch sizes, typically only tens of thousands of steps are needed. ```protobuf train_config { num_steps: 25000 } ``` -------------------------------- ### Compile Protobufs Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Compile the Protobuf definitions required for model and training configuration. ```bash # From tensorflow/models/research/ protoc object_detection/protos/*.proto --python_out=. ``` -------------------------------- ### Visualize and save model architecture Source: https://github.com/tanaka-group/eczemanet/blob/master/notebooks/Model Training.ipynb Generates a PNG image of the model structure and saves it to the specified output path. ```python print("===========================================") plot_model(model, to_file=os.path.join(OUTPUT_PATH,"model.png")) ``` -------------------------------- ### Cloud ML Engine Training Configuration Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md A sample YAML configuration file for a multiworker training job with GPUs on Google Cloud ML Engine. Adjust worker counts and types based on your needs. ```yaml trainingInput: runtimeVersion: "1.12" scaleTier: CUSTOM masterType: standard_gpu workerCount: 9 workerType: standard_gpu parameterServerCount: 3 parameterServerType: standard ``` -------------------------------- ### Set PYTHONPATH Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/installation.md Add the research and slim directories to the Python path for local execution. ```bash # From tensorflow/models/research/ export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim ``` -------------------------------- ### Create Pycocotools Package Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_on_cloud.md Use this script to create the necessary Python packages for Tensorflow Object Detection API on Cloud ML. Ensure you are in the 'tensorflow/models/research/' directory. ```bash bash object_detection/dataset_tools/create_pycocotools_package.sh /tmp/pycocotools python setup.py sdist (cd slim && python setup.py sdist) ``` -------------------------------- ### Generate PASCAL VOC TFRecord files Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/preparing_inputs.md Downloads and converts the PASCAL VOC 2012 dataset into training and validation TFRecord files. ```bash # From tensorflow/models/research/ wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar tar -xvf VOCtrainval_11-May-2012.tar python object_detection/dataset_tools/create_pascal_tf_record.py \ --label_map_path=object_detection/data/pascal_label_map.pbtxt \ --data_dir=VOCdevkit --year=VOC2012 --set=train \ --output_path=pascal_train.record python object_detection/dataset_tools/create_pascal_tf_record.py \ --label_map_path=object_detection/data/pascal_label_map.pbtxt \ --data_dir=VOCdevkit --year=VOC2012 --set=val \ --output_path=pascal_val.record ``` -------------------------------- ### Import Libraries for EczemaNet Model Training Source: https://github.com/tanaka-group/eczemanet/blob/master/notebooks/Model Training.ipynb Imports all necessary Python libraries for data manipulation, model building, and evaluation. Ensure TensorFlow backend is configured. ```python import sys sys.path.append("..\n") sys.path.append("../lib") import os import pandas as pd import numpy as np import pickle from tqdm import tqdm import itertools from datetime import datetime import matplotlib.pyplot as plt import scikitplot as skplt import scipy from scipy import ndimage from sklearn.metrics import accuracy_score, f1_score, log_loss from sklearn.calibration import calibration_curve from sklearn.metrics import (brier_score_loss, precision_score, recall_score) from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import confusion_matrix from sklearn.utils.multiclass import unique_labels from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score from sklearn.model_selection import KFold from sklearn.utils import class_weight from math import sqrt import keras from keras.models import model_from_json from keras.preprocessing import image from keras.utils import plot_model from keras.callbacks import EarlyStopping, TensorBoard from keras import optimizers # Confirm Keras sees the GPU from keras import backend as K assert len(K.tensorflow_backend._get_available_gpus()) > 0 from EczemaNet_helper import * ``` -------------------------------- ### Run Jupyter Notebook for Object Detection Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_notebook.md Execute this command from the `tensorflow/models/research/object_detection` directory to launch the Jupyter notebook for interactive inference. ```bash # From tensorflow/models/research/object_detection jupyter notebook ``` -------------------------------- ### Convert Dataset to TFRecord Format Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Runs the conversion script to transform the raw dataset into TFRecord files required by the Object Detection API. ```bash # From tensorflow/models/research/ python object_detection/dataset_tools/create_pet_tf_record.py \ --label_map_path=object_detection/data/pet_label_map.pbtxt \ --data_dir=`pwd` \ --output_dir=`pwd` ``` -------------------------------- ### Download Open Images Annotations Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/oid_inference_and_evaluation.md Downloads the human bounding box annotations for the Open Images dataset. Ensure you are in the 'tensorflow/models/research/oid' directory. ```bash # From tensorflow/models/research mkdir oid cd oid wget https://storage.googleapis.com/openimages/2017_07/annotations_human_bbox_2017_07.tar.gz tar -xvf annotations_human_bbox_2017_07.tar.gz ``` -------------------------------- ### Generate Oxford-IIIT Pet TFRecord files Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/preparing_inputs.md Downloads and converts the Oxford-IIIT Pet dataset into 10-sharded TFRecord files. ```bash # From tensorflow/models/research/ wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz tar -xvf annotations.tar.gz tar -xvf images.tar.gz python object_detection/dataset_tools/create_pet_tf_record.py \ --label_map_path=object_detection/data/pet_label_map.pbtxt \ --data_dir=`pwd` \ --output_dir=`pwd` ``` -------------------------------- ### Configure ImageDataGenerator Source: https://context7.com/tanaka-group/eczemanet/llms.txt Sets up real-time image augmentation and fits the generator to training data for model training. ```python from lib.image_preprocessing import ImageDataGenerator # Configure augmentation for skin images datagen = ImageDataGenerator( rotation_range=20, # Random rotation +/- 20 degrees width_shift_range=0.2, # Horizontal shift height_shift_range=0.2, # Vertical shift horizontal_flip=True, # Mirror horizontally vertical_flip=True, # Mirror vertically fill_mode='nearest', # Fill strategy for shifted regions brightness_range=(0.8, 1.2), # Brightness variation zoom_range=0.1 # Random zoom ) # Fit generator to training data (computes statistics) datagen.fit(x_train) # Train with augmented data model.fit_generator( datagen.flow(x_train, y_train, batch_size=32), validation_data=(x_test, y_test), epochs=50, steps_per_epoch=len(x_train) // 32 ) ``` -------------------------------- ### Configure Matplotlib for Notebooks Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Enables inline image display for Jupyter notebooks. ```python # This is needed to display the images. %matplotlib inline ``` -------------------------------- ### Set Batch Size for TPU Training Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/tpu_compatibility.md Configure the batch size for object detection training on TPU. Use the largest batch size that fits on the cloud TPU. ```protobuf train_config { batch_size: 1024 } ``` -------------------------------- ### Download and Upload Pre-trained Model Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Downloads the COCO-pretrained Faster R-CNN model, extracts it, and uploads the checkpoint files to a GCS bucket. ```bash wget http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz gsutil cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* gs://${YOUR_GCS_BUCKET}/data/ ``` -------------------------------- ### Evaluate Object Detection Metrics Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/challenge_evaluation.md Runs the evaluation script using expanded ground-truth CSV files and model prediction CSVs. ```bash INPUT_PREDICTIONS=/path/to/detection_predictions.csv OUTPUT_METRICS=/path/to/output/metrics/file python models/research/object_detection/metrics/oid_od_challenge_evaluation.py \ --input_annotations_boxes=${BOUNDING_BOXES}_expanded.csv \ --input_annotations_labels=${IMAGE_LABELS}_expanded.csv \ --input_class_labelmap=object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt \ --input_predictions=${INPUT_PREDICTIONS} \ --output_metrics=${OUTPUT_METRICS} ``` -------------------------------- ### PNG Instance Segmentation Masks Format Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/instance_segmentation.md Use this format to supply instance segmentation masks as serialized PNG images. Each mask should be a single-channel image with pixel values of 0 (background) or 1 (object mask). ```shell image/object/mask = ["\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\...", ...] ``` -------------------------------- ### Download and Unpack Detection Model Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/oid_inference_and_evaluation.md Downloads and extracts the Faster R-CNN Inception ResNet V2 model from the TensorFlow model zoo. ```bash # From tensorflow/models/research/oid wget http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_oid_14_10_2017.tar.gz tar -zxvf faster_rcnn_inception_resnet_v2_atrous_oid_14_10_2017.tar.gz ``` -------------------------------- ### Run Offline Evaluation Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/challenge_evaluation.md Generates configuration files and executes the offline evaluation script to produce final metrics. ```bash INPUT_TFRECORDS_WITH_DETECTIONS=/path/to/tf_records_with_detections OUTPUT_CONFIG_DIR=/path/to/configs echo " label_map_path: 'object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt' tf_record_input_reader: { input_path: '${INPUT_TFRECORDS_WITH_DETECTIONS}' } " > ${OUTPUT_CONFIG_DIR}/input_config.pbtxt echo " metrics_set: 'oid_challenge_detection_metrics' " > ${OUTPUT_CONFIG_DIR}/eval_config.pbtxt OUTPUT_METRICS_DIR=/path/to/metrics_csv python object_detection/metrics/offline_eval_map_corloc.py \ --eval_dir=${OUTPUT_METRICS_DIR} \ --eval_config_path=${OUTPUT_CONFIG_DIR}/eval_config.pbtxt \ --input_config_path=${OUTPUT_CONFIG_DIR}/input_config.pbtxt ``` -------------------------------- ### Download and Extract Oxford-IIIT Pets Dataset Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Downloads the image and annotation tarballs and extracts them into the research directory. ```bash # From tensorflow/models/research/ wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz tar -xvf images.tar.gz tar -xvf annotations.tar.gz ``` -------------------------------- ### Upload Data to Google Cloud Storage Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/running_pets.md Copies the generated TFRecord files and label map to the specified GCS bucket. ```bash # From tensorflow/models/research/ gsutil cp pet_faces_train.record-* gs://${YOUR_GCS_BUCKET}/data/ gsutil cp pet_faces_val.record-* gs://${YOUR_GCS_BUCKET}/data/ gsutil cp object_detection/data/pet_label_map.pbtxt gs://${YOUR_GCS_BUCKET}/data/pet_label_map.pbtxt ``` -------------------------------- ### Create Category Index Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/object_detection_tutorial.ipynb Generates a dictionary mapping label indices to category names. ```python category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True) ``` -------------------------------- ### Expand Hierarchical Labels Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/challenge_evaluation.md Expands bounding box and image-level label annotations to include hierarchical relationships required for evaluation. ```bash HIERARCHY_FILE=/path/to/bbox_labels_500_hierarchy.json BOUNDING_BOXES=/path/to/challenge-2018-train-annotations-bbox IMAGE_LABELS=/path/to/challenge-2018-train-annotations-human-imagelabels python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ --json_hierarchy_file=${HIERARCHY_FILE} \ --input_annotations=${BOUNDING_BOXES}.csv \ --output_annotations=${BOUNDING_BOXES}_expanded.csv \ --annotation_type=1 python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ --json_hierarchy_file=${HIERARCHY_FILE} \ --input_annotations=${IMAGE_LABELS}.csv \ --output_annotations=${IMAGE_LABELS}_expanded.csv \ --annotation_type=2 ``` -------------------------------- ### Configure Sharded Input Reader Source: https://github.com/tanaka-group/eczemanet/blob/master/lib/object_detection/g3doc/using_your_own_dataset.md Configuration snippet for reading sharded TFRecord files using a glob pattern. ```bash tf_record_input_reader { input_path: "/path/to/train_dataset.record-?????-of-00010" } ```