Spaces:
Build error
Build error
First Commit
Browse filesFiles changed (30)
- .streamlit/config.tomlAdded+9
- Functions/__pycache__/image_augmentation_functions.cpython-311.pycAdded
- Functions/__pycache__/image_processing_functions.cpython-311.pycAdded
- Functions/__pycache__/model_training_functions.cpython-311.pycAdded
- Functions/image_augmentation_functions.pyAdded+1027
- Functions/image_processing_functions.pyAdded+430
- Functions/model_training_functions.pyAdded+1896
- README.md+12-14
- Welcome.pyAdded+8
- config.tomlAdded+9
- model_data/input_files/config.yamlAdded+6
- model_data/model_training_code_pipline/config.yamlAdded+6
- model_data/model_training_code_pipline/model_training.pyAdded+69
- model_data/models/yolov8l-seg.ptAdded+3
- model_data/models/yolov8l.ptAdded+3
- model_data/models/yolov8m-seg.ptAdded+3
- model_data/models/yolov8m.ptAdded+3
- model_data/models/yolov8n-seg.ptAdded+3
- model_data/models/yolov8n.ptAdded+3
- model_data/models/yolov8s-seg.ptAdded+3
- model_data/models/yolov8s.ptAdded+3
- model_data/models/yolov8x-seg.ptAdded+3
- model_data/models/yolov8x.ptAdded+3
- packages.txtAdded+1
- packages_db.xlsxAdded
- pages/2_Image_Processing.pyAdded+512
- pages/3_Image_Augmentation.pyAdded+612
- pages/4_Model_Training.pyAdded+364
- requirements.txtAdded
- utils.pyAdded+446
123456789[theme]
primaryColor = "#101276"
backgroundColor = "#FFFFFF"
textColor = "#006EC0"
secondaryBackgroundColor = "#D2E5F2"
font = "sans serif"
[server]
maxUploadSize = 100001234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027# Importing necessary libraries
import io
import os
import cv2
import utils
import random
import zipfile
import numpy as np
import pandas as pd
from PIL import Image
import streamlit as st
import albumentations as A
import matplotlib.pyplot as plt
# Function to fetch the names of augmentation techniques applicable to the selected option
st.cache_resource(show_spinner=False)
def get_applicable_techniques(df, option):
return df[df[option] == "Applicable"]["Name"]
# Function to generate unique colors for each given list of unique numbers
def generate_unique_colors(unique_numbers):
# Get a color map
cmap = plt.get_cmap("hsv")
# Generate unique colors using the colormap
colors = {
number: cmap(i / len(unique_numbers))[:3]
for i, number in enumerate(unique_numbers)
}
# Convert colors from RGB to BGR format and scale to 0-255
colors_bgr = {
k: (int(v[2] * 255), int(v[1] * 255), int(v[0] * 255))
for k, v in colors.items()
}
return colors_bgr
# Function to adjust zero values to a small positive number
def adjust_zero_value(value):
return value if value != 0 else 1e-9
# Function to parse a YOLO label file and convert it into Albumentations-compatible Bboxes format
def bboxes_label(label_file, class_dict):
bboxes_data = {"bboxes": [], "class_labels": []}
for line in label_file:
try:
# Extracting class_id and bounding box coordinates from each line
class_id, x_center, y_center, width, height = map(
float, line.decode().strip().split()
)
class_id += 1 # Shifting its starting value to 1, since 0 is reserved for the background
# Adjusting bounding box coordinates to avoid zero values
# Albumentations does not accept zero values, but they are acceptable in YOLO format
x_center, y_center, width, height = map(
adjust_zero_value, [x_center, y_center, width, height]
)
# Check if values are within the expected range and class_id exists in class_dict
if (
0 <= x_center <= 1
and 0 <= y_center <= 1
and 0 <= width <= 1
and 0 <= height <= 1
and class_id in class_dict.keys()
):
bboxes_data["bboxes"].append([x_center, y_center, width, height])
bboxes_data["class_labels"].append(class_id)
else:
return None # Return None if any value is out of range or class_id is invalid
except Exception as e:
# Return None if any exception is encountered
return None
# Return None if the file is empty or no valid data found
return bboxes_data if bboxes_data["bboxes"] else None
# Function to parse a YOLO label file and convert it into compatible Mask format
def masks_label(label_file, class_dict):
mask_data = {"masks": [], "class_labels": []}
for line in label_file:
try:
# Clean up the line and split into parts
parts = line.decode().strip().split()
class_id = (
int(parts[0]) + 1
) # Shifting its starting value to 1, since 0 is reserved for the background
points = [float(p) for p in parts[1:]]
# Check if class_id exists in class_dict and coordinates are within the expected range
if class_id in class_dict.keys() and all(0 <= p <= 1 for p in points):
# Group points into (x, y) tuples
polygon = [(points[i], points[i + 1]) for i in range(0, len(points), 2)]
# Append class label and polygon to the mask data
mask_data["class_labels"].append(class_id)
mask_data["masks"].append(polygon)
else:
return None # Return None if class_id is invalid or coordinates are out of range
except Exception as e:
# Return None if any exception is encountered
return None
# Return None if the file is empty or no valid data found
return mask_data if mask_data["masks"] else None
# Function to generate mask for albumentations format
def generate_mask(masks, class_ids, image_height, image_width):
# Create an empty mask of the same size as the image, filled with 0 for background
mask = np.full((image_height, image_width), 0, dtype=np.int32)
# Iterate over each polygon and its corresponding class_id
for polygon, class_id in zip(masks, class_ids):
# Scale the polygon points to the image size
scaled_polygon = [
(int(x * image_width), int(y * image_height)) for x, y in polygon
]
# Draw the polygon on the mask
cv2.fillPoly(mask, [np.array(scaled_polygon, dtype=np.int32)], color=class_id)
return mask
# Function to convert a single-channel mask back to YOLO format
def mask_to_yolo(mask):
yolo_data = {"masks": [], "class_labels": []}
unique_values = np.unique(mask)
for value in unique_values:
if value == 0: # Skip the background
continue
# Extract mask for individual object
single_object_mask = np.uint8(mask == value)
# Find contours
contours, _ = cv2.findContours(
single_object_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
)
for contour in contours:
# Normalize and flatten contour points
normalized_contour = [
(point[0][0] / mask.shape[1], point[0][1] / mask.shape[0])
for point in contour
]
yolo_data["masks"].append(normalized_contour)
yolo_data["class_labels"].append(value)
return yolo_data
# Function to create a user interface for adjusting bounding box parameters
def bbox_params():
with st.expander("Bounding Box Parameters"):
# Create two columns for the input widgets
col1, col2 = st.columns(2)
with col1:
min_area = st.number_input(
"Minimum Area",
min_value=0,
max_value=1000,
value=0,
step=1,
help="Minimum area of a bounding box. Boxes smaller than this will be removed.",
on_change=utils.reset_validation_trigger,
key=st.session_state["bbox1_key"],
)
min_visibility = st.number_input(
"Minimum Visibility",
min_value=0,
max_value=1000,
value=0,
step=1,
help="Minimum fraction of area for a bounding box to remain in the list.",
on_change=utils.reset_validation_trigger,
key=st.session_state["bbox2_key"],
)
with col2:
min_width = st.number_input(
"Minimum Width",
min_value=0,
max_value=1000,
value=0,
step=1,
help="Minimum width of a bounding box. Boxes narrower than this will be removed.",
on_change=utils.reset_validation_trigger,
key=st.session_state["bbox3_key"],
)
min_height = st.number_input(
"Minimum Height",
min_value=0,
max_value=1000,
value=0,
step=1,
help="Minimum height of a bounding box. Boxes shorter than this will be removed.",
on_change=utils.reset_validation_trigger,
key=st.session_state["bbox4_key"],
)
check_each_transform = st.checkbox(
"Check Each Transform",
help="If checked, bounding boxes will be checked after each dual transform.",
value=True,
on_change=utils.reset_validation_trigger,
key=st.session_state["bbox5_key"],
)
# Return the collected parameters as a dictionary
return {
"min_area": min_area,
"min_visibility": min_visibility,
"min_width": min_width,
"min_height": min_height,
"check_each_transform": check_each_transform,
}
# Function to check if the uploaded images and labels are valid
st.cache_resource(show_spinner=False)
def check_valid_labels(uploaded_files, selected_option, class_dict):
# Early exit if no files are uploaded
if len(uploaded_files) == 0:
st.warning("Please upload at least one image to apply augmentation.", icon="⚠️")
return False, {}, {}, None, None
# Initialize dictionaries to hold images and labels
image_files, label_files = {}, {}
# Extracting the name of the first file
first_file_name = os.path.splitext(uploaded_files[0].name)[0]
# Counters for images and labels
image_count, label_count = 0, 0
# Initialize a progress bar and progress text
progress_bar = st.progress(0)
progress_text = st.empty()
total_files = len(uploaded_files)
# Categorize and prepare uploaded files
for index, file in enumerate(uploaded_files):
file.seek(0) # Reset file pointer to ensure proper file reading
file_name_without_extension = os.path.splitext(file.name)[0]
# Distribute files into image or label categories based on their file type
if file.type in ["image/jpeg", "image/png"]:
image_files[file_name_without_extension] = file
image_count += 1
elif file.type == "text/plain":
file_content = file.readlines()
if selected_option == "Bboxes":
label_data = bboxes_label(file_content, class_dict)
elif selected_option == "Masks":
label_data = masks_label(file_content, class_dict)
# Check for valid label data
if label_data is None:
st.warning(
f"Invalid label format or data in file: {file.name}",
icon="⚠️",
)
return False, {}, {}, None, None
label_files[file_name_without_extension] = label_data
label_count += 1
# Update progress bar and display current progress
progress_percentage = (index + 1) / total_files
progress_bar.progress(progress_percentage)
progress_text.text(f"Validating file {index + 1} of {total_files}")
# Extract sets of unique file names for images and labels
unique_image_names = set(image_files.keys())
unique_label_names = set(label_files.keys())
# Remove progress bar and progress text after processing
progress_bar.empty()
progress_text.empty()
if (len(unique_image_names) != image_count) or (
len(unique_label_names) != label_count
):
# Warn the user about the presence of duplicate file names
st.warning(
"Duplicate file names detected. Please ensure each image and label has a unique name.",
icon="⚠️",
)
return False, {}, {}, None, None
# Perform validation checks
if (len(image_files) > 0) and (len(label_files) > 0):
# Check if the number of images and labels match and each pair has corresponding files
if (len(image_files) == len(label_files)) and (
unique_image_names == unique_label_names
):
st.info(
f"Validated: {len(image_files)} images and labels successfully matched.",
icon="✅",
)
return (
True,
image_files,
label_files,
image_files[first_file_name],
label_files[first_file_name],
)
elif len(image_files) != len(label_files):
# Warn if the count of images and labels does not match
st.warning(
"Count Mismatch: The number of uploaded images and labels does not match.",
icon="⚠️",
)
return False, {}, {}, None, None
else:
# Warn if there is a mismatch in file names between images and labels
st.warning(
"Mismatch detected: Some images do not have corresponding label files.",
icon="⚠️",
)
return False, {}, {}, None, None
elif len(image_files) > 0:
# Inform the user if only images are uploaded without labels
st.info(
f"Note: {len(image_files)} images uploaded without labels. Label type and class labels will be ignored in this case.",
icon="✅",
)
return True, image_files, {}, image_files[first_file_name], None
else:
# Warn if no images are uploaded
st.warning("Please upload an image to apply augmentation.", icon="⚠️")
return False, {}, {}, None, None
# Function to apply an augmentation technique to an image and return any errors along with the processed image
def apply_and_test_augmentation(
augmentation,
params,
image,
label,
label_type,
label_input_parameters,
allowed_image_types,
):
try:
# Check the data type and number of channels of the input image
input_image_type = image.dtype
num_channels = (
image.shape[2] if len(image.shape) == 3 else 1
) # Assuming 1 for single-channel images
# Validate if the input image type is among the allowed types
if not utils.is_image_type_allowed(
input_image_type, num_channels, allowed_image_types
):
# Format the allowed types for display in the warning message
allowed_types_formatted = ", ".join(map(str, allowed_image_types))
# Display a warning message specifying the acceptable image types
st.warning(
f"Error applying {augmentation}: Incompatible image type. The input image should be one of the following types: {allowed_types_formatted}",
icon="⚠️",
)
return True, None # Error occurred
# Set the seed for reproducibility using iteration number
random.seed(0)
if label is None:
# Apply augmentation technique for no label input
transform = A.Compose([utils.apply_albumentation(params, augmentation)])
processed_image = transform(image=image)["image"]
return False, processed_image
elif label_type == "Bboxes":
# Apply augmentation technique for Bboxes lable format
transform = A.Compose(
[utils.apply_albumentation(params, augmentation)],
bbox_params=A.BboxParams(
format="yolo",
label_fields=["class_labels"],
min_area=label_input_parameters["min_area"],
min_visibility=label_input_parameters["min_visibility"],
min_width=label_input_parameters["min_width"],
min_height=label_input_parameters["min_height"],
check_each_transform=label_input_parameters["check_each_transform"],
),
)
processed_image = transform(
image=image,
bboxes=label["bboxes"],
class_labels=label["class_labels"],
)["image"]
elif label_type == "Masks":
# Apply augmentation technique for Masks lable format
transform = A.Compose([utils.apply_albumentation(params, augmentation)])
processed_image = transform(
image=image,
mask=generate_mask(
label["masks"],
label["class_labels"],
image.shape[0],
image.shape[1],
),
)["image"]
return False, processed_image # No error
except Exception as e:
st.warning(f"Error applying {augmentation}: {e}", icon="⚠️")
return True, None # Error occurred
# Generates a DataFrame detailing augmentation technique parameters and descriptions
def create_augmentations_dataframe(augmentations_params, augmentation_params_db):
data = []
for aug_name, params in augmentations_params.items():
for param_name, param_value in params.items():
# Retrieve relevant augmentation information from the database
augmentation_info = augmentation_params_db[
augmentation_params_db["Name"] == aug_name
]
param_info = augmentation_info[
augmentation_info["Parameter Name"] == param_name
]
# Check if the parameter information exists in the database
if not param_info.empty:
# Get the description of the current parameter
param_description = param_info["Parameter Description"].iloc[0]
else:
param_description = "Description not available"
# Append augmentation name, parameter name, its value, and description to the data list
data.append([aug_name, param_name, param_value, param_description])
# Create the DataFrame from the accumulated data
augmentations_df = pd.DataFrame(
data, columns=["augmentation", "Parameter", "Value", "Description"]
)
return augmentations_df
# Function to Generate Python Code for Augmentation with Bounding Box Labels
def generate_python_code_bboxes(
augmentations_params,
label_input_parameters,
num_variations=1,
include_original=False,
):
# Start with necessary library imports
code_str = "# Importing necessary libraries\n"
code_str += "import os\nimport cv2\nimport shutil\nimport albumentations as A\n\n"
# Paths for input and output directories
code_str += "# Define the paths for input and output directories\n"
code_str += "input_directory = 'path/to/input'\n"
code_str += "output_directory = 'path/to/output'\n\n"
# Function to read YOLO format labels
code_str += "# Function to read YOLO format labels\n"
code_str += "def read_yolo_label(label_path):\n"
code_str += " bboxes = []\n"
code_str += " class_ids = []\n"
code_str += " with open(label_path, 'r') as file:\n"
code_str += " for line in file:\n"
code_str += " class_id, x_center, y_center, width, height = map(float, line.split())\n"
code_str += " bboxes.append([x_center, y_center, width, height])\n"
code_str += " class_ids.append(int(class_id))\n"
code_str += " return bboxes, class_ids\n\n"
# Function to create an augmentation pipeline
code_str += "# Function to create an augmentation pipeline using Albumentations\n"
code_str += "def process_image(image, bboxes, class_ids):\n"
code_str += " # Define the sequence of augmentation techniques\n"
code_str += " pipeline = A.Compose([\n"
for technique, params in augmentations_params.items():
code_str += f" A.{technique}({', '.join(f'{k}={v}' for k, v in params.items())}),\n"
code_str += " ], bbox_params=A.BboxParams(\n"
code_str += f" format='yolo',\n"
code_str += f" label_fields=['class_labels'],\n"
code_str += f" min_area={label_input_parameters['min_area']},\n"
code_str += f" min_visibility={label_input_parameters['min_visibility']},\n"
code_str += f" min_width={label_input_parameters['min_width']},\n"
code_str += f" min_height={label_input_parameters['min_height']},\n"
code_str += f" check_each_transform={label_input_parameters['check_each_transform']}\n"
code_str += " ))\n"
code_str += " # Apply the augmentation pipeline\n"
code_str += (
" return pipeline(image=image, bboxes=bboxes, class_labels=class_ids)\n\n"
)
# Function to process a batch of images
code_str += "# Function to process a batch of images\n"
code_str += "def process_batch(input_directory, output_directory):\n"
code_str += " for filename in os.listdir(input_directory):\n"
code_str += " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n"
code_str += " image_path = os.path.join(input_directory, filename)\n"
code_str += " label_path = os.path.splitext(image_path)[0] + '.txt'\n\n"
code_str += " # Read the image and label\n"
code_str += " image = cv2.imread(image_path)\n"
code_str += " bboxes, class_ids = read_yolo_label(label_path)\n\n"
# Include original image and label logic
code_str += " # Include original image and label\n"
if include_original:
code_str += " shutil.copy2(image_path, output_directory)\n"
code_str += " shutil.copy2(label_path, output_directory)\n\n"
# Generate variations for each image and process them
code_str += " # Generate variations for each image and process them\n"
code_str += f" for variation in range({num_variations}):\n"
code_str += (
" processed_data = process_image(image, bboxes, class_ids)\n"
)
code_str += " processed_image = processed_data['image']\n"
code_str += " processed_bboxes = processed_data['bboxes']\n"
code_str += (
" processed_class_ids = processed_data['class_labels']\n\n"
)
code_str += " # Save the processed image\n"
code_str += " output_filename = f'processed_{os.path.splitext(filename)[0]}_{variation}{os.path.splitext(filename)[1]}'\n"
code_str += " cv2.imwrite(os.path.join(output_directory, output_filename), processed_image)\n\n"
code_str += " with open(os.path.join(output_directory, os.path.splitext(output_filename)[0] + '.txt'), 'w') as label_file:\n"
code_str += " for bbox, class_id in zip(processed_bboxes, processed_class_ids):\n"
code_str += " label_line = ' '.join(map(str, [class_id] + list(bbox)))\n"
code_str += " label_file.write(label_line + '\\n')\n\n"
# Execute the batch processing function
code_str += (
"# Execute the batch processing function with the specified parameters\n"
)
code_str += f"process_batch(input_directory, output_directory)\n"
return code_str
def generate_python_code_masks(
augmentations_params,
label_input_parameters,
num_variations=1,
include_original=False,
):
# Start with necessary library imports
code_str = "# Importing necessary libraries\n"
code_str += "import os\nimport cv2\nimport shutil\nimport numpy as np\nimport albumentations as A\n\n"
# Paths for input and output directories
code_str += "# Define the paths for input and output directories\n"
code_str += "input_directory = 'path/to/input'\n"
code_str += "output_directory = 'path/to/output'\n\n"
# Function to read YOLO mask format and convert to mask
code_str += "# Function to read YOLO mask format and convert to mask\n"
code_str += "def read_yolo_label(label_path, image_shape):\n"
code_str += " mask = np.full(image_shape, -1, dtype=np.int32)\n"
code_str += " with open(label_path, 'r') as file:\n"
code_str += " for line in file:\n"
code_str += " parts = line.strip().split()\n"
code_str += " class_id = int(parts[0])\n"
code_str += (
" points = np.array([float(p) for p in parts[1:]]).reshape(-1, 2)\n"
)
code_str += " points = (points * [image_shape[1], image_shape[0]]).astype(np.int32)\n"
code_str += " cv2.fillPoly(mask, [points], class_id)\n"
code_str += " return mask\n\n"
# Function to convert mask to YOLO format
code_str += "# Function to convert mask to YOLO format\n"
code_str += "def mask_to_yolo(mask):\n"
code_str += " yolo_format = ''\n"
code_str += " for class_id in np.unique(mask):\n"
code_str += " if class_id == -1:\n"
code_str += " continue\n"
code_str += " contours, _ = cv2.findContours(\n"
code_str += " np.uint8(mask == class_id), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE\n"
code_str += " )\n"
code_str += " for contour in contours:\n"
code_str += " contour = contour.flatten().tolist()\n"
code_str += " normalized_contour = [\n"
code_str += " str(coord / mask.shape[i % 2]) for i, coord in enumerate(contour)\n"
code_str += " ]\n"
code_str += " yolo_format += f'{class_id} ' + ' '.join(normalized_contour) + '\\n'\n"
code_str += " return yolo_format\n\n"
# Function to create an augmentation pipeline
code_str += "# Function to create an augmentation pipeline using Albumentations\n"
code_str += "def process_image(image, mask):\n"
code_str += " # Define the sequence of augmentation techniques\n"
code_str += " pipeline = A.Compose([\n"
for technique, params in augmentations_params.items():
code_str += f" A.{technique}({', '.join(f'{k}={v}' for k, v in params.items())}),\n"
code_str += " ])\n"
code_str += " # Apply the augmentation pipeline\n"
code_str += " return pipeline(image=image, mask=mask)\n\n"
# Function to process a batch of images
code_str += "# Function to process a batch of images\n"
code_str += "def process_batch(input_directory, output_directory, include_original=False, num_variations=1):\n"
code_str += " for filename in os.listdir(input_directory):\n"
code_str += " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n"
code_str += " image_path = os.path.join(input_directory, filename)\n"
code_str += " label_path = os.path.splitext(image_path)[0] + '.txt'\n\n"
code_str += " # Read the image\n"
code_str += " image = cv2.imread(image_path)\n\n"
# Include original image and label logic
code_str += " # Include original image\n"
if include_original:
code_str += " shutil.copy2(image_path, output_directory)\n"
code_str += " shutil.copy2(label_path, output_directory)\n\n"
code_str += " # Check if label file exists and read mask\n"
code_str += " mask = None\n"
code_str += " if os.path.exists(label_path):\n"
code_str += (
" mask = read_yolo_label(label_path, image.shape[:2])\n\n"
)
# Generate variations for each image and process them
code_str += " # Generate variations for each image and process them\n"
code_str += f" for variation in range({num_variations}):\n"
code_str += " processed_image, processed_mask = image, mask\n"
code_str += " if mask is not None:\n"
code_str += " processed_data = process_image(image, mask)\n"
code_str += " processed_image, processed_mask = processed_data['image'], processed_data['mask']\n\n"
code_str += " # Save the processed image\n"
code_str += " output_filename = f'processed_{os.path.splitext(filename)[0]}_{variation}.jpg'\n"
code_str += " cv2.imwrite(os.path.join(output_directory, output_filename), processed_image)\n\n"
code_str += " # Save the processed label in YOLO format\n"
code_str += " if processed_mask is not None:\n"
code_str += (
" processed_label_str = mask_to_yolo(processed_mask)\n"
)
code_str += " with open(os.path.join(output_directory, os.path.splitext(output_filename)[0] + '.txt'), 'w') as label_file:\n"
code_str += " label_file.write(processed_label_str)\n\n"
# Execute the batch processing function with the specified parameters
code_str += (
"# Execute the batch processing function with the specified parameters\n"
)
code_str += "process_batch(input_directory, output_directory)\n"
return code_str
# Function to create an augmentation pipeline based on the selected techniques and their parameters
def create_augmentation_pipeline(
selected_augmentations, augmentation_params, label_type, label_input_parameters=None
):
pipeline = []
for aug_name in selected_augmentations:
# Append the function call with its parameters to the pipeline
pipeline.append(
utils.apply_albumentation(augmentation_params[aug_name], aug_name)
)
# Compose all the augmentations into one transformation
try:
# Set the seed for reproducibility using iteration number
random.seed(0)
if label_type is None:
# Apply augmentation technique for no label input
transform = A.Compose(pipeline)
return transform
elif label_type == "Bboxes":
# Apply augmentation technique for Bboxes lable format
transform = A.Compose(
pipeline,
bbox_params=A.BboxParams(
format="yolo",
label_fields=["class_labels"],
min_area=label_input_parameters["min_area"],
min_visibility=label_input_parameters["min_visibility"],
min_width=label_input_parameters["min_width"],
min_height=label_input_parameters["min_height"],
check_each_transform=label_input_parameters["check_each_transform"],
),
)
elif label_type == "Masks":
# Apply augmentation technique for Masks lable format
transform = A.Compose(pipeline)
return transform # No error
except Exception as e:
st.warning(f"Error applying augmentation")
return None # Error occurred
# Function to convert label data from dictionary format to YOLO format
def convert_labels_to_yolo_format(label_data, class_dict):
yolo_label_str = ""
# Convert bounding boxes to YOLO format
if "bboxes" in label_data:
for bbox, class_label in zip(label_data["bboxes"], label_data["class_labels"]):
class_id = class_label - 1 # Revert to the original value
x_center, y_center, width, height = bbox
yolo_label_str += f"{class_id} {x_center} {y_center} {width} {height}\n"
# Convert masks to YOLO format
if "masks" in label_data:
for mask, class_label in zip(label_data["masks"], label_data["class_labels"]):
class_id = class_label - 1 # Revert to the original value
# Flatten the mask array into a single line of coordinates
mask_flattened = [coord for point in mask for coord in point]
mask_str = " ".join(map(str, mask_flattened))
yolo_label_str += f"{class_id} {mask_str}\n"
return yolo_label_str
# Function to apply the augmentation pipeline to the image based on the label type
def apply_augmentation_pipeline(image, label_file, label_type, transform):
# Initialize an empty dictionary to store processed labels
processed_label = {}
# Apply the transformation based on the label type
if label_type is None:
processed_output = transform(image=image)
processed_label = None
elif label_type == "Bboxes":
processed_output = transform(
image=image,
bboxes=label_file["bboxes"],
class_labels=label_file["class_labels"],
)
processed_label = {
"bboxes": processed_output["bboxes"],
"class_labels": processed_output["class_labels"],
}
elif label_type == "Masks":
mask = generate_mask(
label_file["masks"],
label_file["class_labels"],
image.shape[0],
image.shape[1],
)
processed_output = transform(image=image, mask=mask)
mask_yolo = mask_to_yolo(processed_output["mask"])
processed_label = mask_yolo
# Extract the processed image
processed_image = processed_output["image"]
return processed_image, processed_label
# Function to process images and labels, apply augmentations, and create a zip file with the results
@st.cache_resource(show_spinner=False)
def process_images_and_labels(
image_files,
label_files,
selected_augmentations,
_augmentations_params,
label_type,
label_input_parameters,
num_variations,
include_original,
class_dict,
):
zip_buffer = io.BytesIO() # Create an in-memory buffer for the zip file
st.session_state[
"image_repository_augmentation"
] = {} # Initialize a repository to store processed image data
st.session_state[
"processed_image_mapping_augmentation"
] = {} # Map original images to their processed versions
st.session_state["unique_images_names"] = [] # List to store unique images names
# Create progress bar and text elements in Streamlit
progress_bar = st.progress(0)
progress_text = st.empty()
with zipfile.ZipFile(
zip_buffer, mode="a", compression=zipfile.ZIP_DEFLATED, allowZip64=True
) as zip_file:
# Determine the label type for augmentation, if label files are present
effective_label_type = None if len(label_files) == 0 else label_type
# Create an augmentation pipeline based on selected augmentations and parameters
transform = create_augmentation_pipeline(
selected_augmentations,
_augmentations_params,
effective_label_type,
label_input_parameters,
)
total_images = len(image_files) * num_variations
processed_count = 0 # Counter for processed images
# Iterate over each uploaded file
for image_name, image_file in image_files.items():
image_file.seek(0) # Reset file pointer to start
file_bytes = np.asarray(bytearray(image_file.read()), dtype=np.uint8)
original_image = cv2.cvtColor(
cv2.imdecode(file_bytes, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB
)
original_image_resized = utils.resize_image(original_image)
# Include original images and labels in the output if selected
if include_original:
original_img_buffer = io.BytesIO()
Image.fromarray(original_image).save(original_img_buffer, format="JPEG")
zip_file.writestr(image_file.name, original_img_buffer.getvalue())
# Convert and save original labels to YOLO format if they exist
label_file = label_files.get(image_name)
if label_file is not None:
yolo_label_str = convert_labels_to_yolo_format(
label_file, class_dict
)
zip_file.writestr(f"{image_name}.txt", yolo_label_str)
original_file_name = image_file.name
st.session_state["unique_images_names"].append(original_file_name)
st.session_state["processed_image_mapping_augmentation"][
original_file_name
] = []
st.session_state["image_repository_augmentation"][image_file.name] = {
"image": original_image_resized,
"label": label_files.get(image_name),
}
# Apply augmentations and generate variations
for i in range(num_variations):
random.seed(i)
(
processed_image,
processed_label,
) = apply_augmentation_pipeline(
original_image,
label_files.get(image_name),
effective_label_type,
transform,
)
img_buffer = io.BytesIO()
Image.fromarray(processed_image).save(img_buffer, format="JPEG")
processed_filename = f"processed_{image_name.split('.')[0]}_{i}.jpg"
zip_file.writestr(processed_filename, img_buffer.getvalue())
processed_image_resized = utils.resize_image(processed_image)
st.session_state["processed_image_mapping_augmentation"][
image_file.name
].append(processed_filename)
st.session_state["image_repository_augmentation"][
processed_filename
] = {
"image": processed_image_resized,
"label": processed_label,
}
# Convert and save processed labels to YOLO format if they exist
label_file = label_files.get(image_name)
if label_file is not None:
processed_label_str = convert_labels_to_yolo_format(
processed_label, class_dict
)
zip_file.writestr(
f"processed_{image_name.split('.')[0]}_{i}.txt",
processed_label_str,
)
processed_count += 1
# Update progress bar and text
progress_bar.progress(processed_count / total_images)
progress_text.text(
f"Processing image {processed_count} of {total_images}"
)
# Remove the progress bar and text after processing is complete
progress_bar.empty()
progress_text.empty()
zip_buffer.seek(0) # Reset buffer to start for download
st.session_state["zip_data_augmentation"] = zip_buffer.getvalue()
# Function to overlay labels on images
def overlay_labels(image, labels_to_plot, label_file, label_type, colors, class_dict):
# Ensure the image is in the correct format (RGB)
if len(image.shape) == 2 or image.shape[2] == 1:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
# Overlay Bounding Boxes
if label_type == "Bboxes":
for bbox, label in zip(label_file["bboxes"], label_file["class_labels"]):
if label in labels_to_plot:
# Convert bbox from yolo format to xmin, ymin, xmax, ymax
x_center, y_center, width, height = bbox
xmin = int((x_center - width / 2) * image.shape[1])
xmax = int((x_center + width / 2) * image.shape[1])
ymin = int((y_center - height / 2) * image.shape[0])
ymax = int((y_center + height / 2) * image.shape[0])
# Get color for the class
color = colors[label]
# Draw rectangle and label
image = cv2.rectangle(
image, (xmin, ymin), (xmax, ymax), color, thickness=2
)
# Put class label text
label_text = class_dict.get(label, "Unknown")
cv2.putText(
image,
label_text,
(xmin, ymin - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 0), # Black color for text
2,
)
# Overlay Mask
elif label_type == "Masks":
for polygon, label in zip(label_file["masks"], label_file["class_labels"]):
if label in labels_to_plot:
# Convert polygon points from yolo format to image coordinates
polygon_points = [
(int(x * image.shape[1]), int(y * image.shape[0]))
for x, y in polygon
]
# Get color for the class
color = colors[label]
# Create a temporary image to draw the polygon
temp_image = image.copy()
cv2.fillPoly(
temp_image, [np.array(polygon_points, dtype=np.int32)], color
)
# Blend the temporary image with the original image
cv2.addWeighted(temp_image, 0.5, image, 0.5, 0, image)
# Optional: Put class label text near the first point of the polygon
label_text = class_dict.get(label, "Unknown")
cv2.putText(
image,
label_text,
polygon_points[0],
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(0, 0, 0), # Black color for text
2,
)
return image
# Function to generate a downloadable file
def display_code_and_download_button(generated_code):
def generate_downloadable_file(code_str):
return code_str.encode("utf-8")
# Display the generated code in Streamlit with description and download button in columns
with st.expander("Plug and Play Code"):
col1, col2 = st.columns([7, 3])
with col1:
st.markdown(
"""
### Description of the Code Pipeline
"""
)
st.markdown(
"""
<div style='text-align: justify;'>
This code is a ready-to-use Python script for batch augmentation. It applies selected augmentation techniques to all images in a specified input directory and saves the processed images in an output directory.
**To use this script:**
- Ensure you have the necessary dependencies installed.
- Specify the input and output paths: Replace `'path/to/input'` with the path to your input images and `'path/to/output'` with the desired path for the processed images.
- The number of augmented variations per image, the inclusion of the original images in the output, and the augmentation techniques with their parameters will be automatically set based on your selections.
### Python Code
</div>
""",
unsafe_allow_html=True,
)
# Display python code
st.code(generated_code, language="python")
with col2:
# Create a button for downloading the Python file
st.download_button(
label="Download Python File",
data=generate_downloadable_file(generated_code),
file_name="augmentation_script.py",
mime="text/plain",
use_container_width=True,
)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430# Importing necessary libraries
import io
import os
import cv2
import utils
import random
import zipfile
import numpy as np
import pandas as pd
from PIL import Image
import streamlit as st
import albumentations as A
# Function to check if the uploaded images and labels are valid
st.cache_resource(show_spinner=False)
def check_valid_labels(uploaded_files):
# Early exit if no files are uploaded
if len(uploaded_files) == 0:
st.warning(
"Please upload at least one image to apply image processing.", icon="⚠️"
)
return False, {}, {}, None, None
# Initialize dictionaries to hold images and labels
image_files, label_files = {}, {}
# Extracting the name of the first file
first_file_name = os.path.splitext(uploaded_files[0].name)[0]
# Counters for images and labels
image_count, label_count = 0, 0
# Initialize a progress bar and progress text
progress_bar = st.progress(0)
progress_text = st.empty()
total_files = len(uploaded_files)
# Categorize and prepare uploaded files
for index, file in enumerate(uploaded_files):
file.seek(0) # Reset file pointer to ensure proper file reading
file_name_without_extension = os.path.splitext(file.name)[0]
# Distribute files into image or label categories based on their file type
if file.type in ["image/jpeg", "image/png"]:
image_files[file_name_without_extension] = file
image_count += 1
elif file.type == "text/plain":
label_files[file_name_without_extension] = file
label_count += 1
# Update progress bar and display current progress
progress_percentage = (index + 1) / total_files
progress_bar.progress(progress_percentage)
progress_text.text(f"Validating file {index + 1} of {total_files}")
# Extract sets of unique file names for images and labels
unique_image_names = set(image_files.keys())
unique_label_names = set(label_files.keys())
# Remove progress bar and progress text after processing
progress_bar.empty()
progress_text.empty()
if (len(unique_image_names) != image_count) or (
len(unique_label_names) != label_count
):
# Warn the user about the presence of duplicate file names
st.warning(
"Duplicate file names detected. Please ensure each image and label has a unique name.",
icon="⚠️",
)
return False, {}, {}, None, None
# Perform validation checks
if (len(image_files) > 0) and (len(label_files) > 0):
# Check if the number of images and labels match and each pair has corresponding files
if (len(image_files) == len(label_files)) and (
unique_image_names == unique_label_names
):
st.info(
f"Validated: {len(image_files)} images and labels successfully matched.",
icon="✅",
)
return (
True,
image_files,
label_files,
image_files[first_file_name],
label_files[first_file_name],
)
elif len(image_files) != len(label_files):
# Warn if the count of images and labels does not match
st.warning(
"Count Mismatch: The number of uploaded images and labels does not match.",
icon="⚠️",
)
return False, {}, {}, None, None
else:
# Warn if there is a mismatch in file names between images and labels
st.warning(
"Mismatch detected: Some images do not have corresponding label files.",
icon="⚠️",
)
return False, {}, {}, None, None
elif len(image_files) > 0:
# Inform the user if only images are uploaded without labels
st.info(
f"Note: {len(image_files)} images uploaded without labels. Label type and class labels will be ignored in this case.",
icon="✅",
)
return True, image_files, {}, image_files[first_file_name], None
else:
# Warn if no images are uploaded
st.warning("Please upload an image to apply image processing.", icon="⚠️")
return False, {}, {}, None, None
# Function to apply an image processing technique to an image and return any errors along with the processed image
def apply_and_test_image_processing(
image_processing, params, image, allowed_image_types
):
try:
# Check the data type and number of channels of the input image
input_image_type = image.dtype
num_channels = (
image.shape[2] if len(image.shape) == 3 else 1
) # Assuming 1 for single-channel images
# Validate if the input image type is among the allowed types
if not utils.is_image_type_allowed(
input_image_type, num_channels, allowed_image_types
):
# Format the allowed types for display in the warning message
allowed_types_formatted = ", ".join(map(str, allowed_image_types))
# Display a warning message specifying the acceptable image types
st.warning(
f"Error applying {image_processing}: Incompatible image type. The input image should be one of the following types: {allowed_types_formatted}",
icon="⚠️",
)
return True, None # Error occurred
# Set the seed for reproducibility using iteration number
random.seed(0)
# Apply image processing technique
transform = A.Compose([utils.apply_albumentation(params, image_processing)])
processed_image = transform(image=image)["image"]
return False, processed_image # No error
except Exception as e:
st.warning(f"Error applying {image_processing}: {e}", icon="⚠️")
return True, None # Error occurred
# Function to generates a DataFrame detailing image processing technique parameters and descriptions
def create_image_processings_dataframe(
image_processings_params, image_processing_params_db
):
data = []
for aug_name, params in image_processings_params.items():
for param_name, param_value in params.items():
# Retrieve relevant image_processing information from the database
image_processing_info = image_processing_params_db[
image_processing_params_db["Name"] == aug_name
]
param_info = image_processing_info[
image_processing_info["Parameter Name"] == param_name
]
# Check if the parameter information exists in the database
if not param_info.empty:
# Get the description of the current parameter
param_description = param_info["Parameter Description"].iloc[0]
else:
param_description = "Description not available"
# Append image_processing name, parameter name, its value, and description to the data list
data.append([aug_name, param_name, param_value, param_description])
# Create the DataFrame from the accumulated data
image_processings_df = pd.DataFrame(
data, columns=["image_processing", "Parameter", "Value", "Description"]
)
return image_processings_df
# Function to generate python code for images and labels
def generate_python_code_images_labels(
augmentations_params,
num_variations=1,
include_original=False,
):
# Start with necessary library imports
code_str = "# Importing necessary libraries\n"
code_str += "import os\nimport cv2\nimport shutil\nimport albumentations as A\n\n"
# Paths for input and output directories
code_str += "# Define the paths for input and output directories\n"
code_str += "input_directory = 'path/to/input'\n"
code_str += "output_directory = 'path/to/output'\n\n"
# Function to create an augmentation pipeline
code_str += "# Function to create an augmentation pipeline using Albumentations\n"
code_str += "def process_image(image):\n"
code_str += " # Define the sequence of augmentation techniques\n"
code_str += " pipeline = A.Compose([\n"
for technique, params in augmentations_params.items():
code_str += f" A.{technique}({', '.join(f'{k}={v}' for k, v in params.items())}),\n"
code_str += " ])\n"
code_str += " # Apply the augmentation pipeline\n"
code_str += " return pipeline(image=image)['image']\n\n"
# Function to process a batch of images
code_str += "# Function to process a batch of images\n"
code_str += "def process_batch(input_directory, output_directory):\n"
code_str += " for filename in os.listdir(input_directory):\n"
code_str += " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n"
code_str += " image_path = os.path.join(input_directory, filename)\n"
code_str += " label_path = os.path.splitext(image_path)[0] + '.txt'\n\n"
code_str += " # Read the image\n"
code_str += " image = cv2.imread(image_path)\n\n"
# Include original image and label logic
if include_original:
code_str += " # Include original image and label\n"
code_str += " shutil.copy2(image_path, output_directory)\n"
code_str += " shutil.copy2(label_path, output_directory)\n\n"
# Generate variations for each image and process them
code_str += " # Generate variations for each image and process them\n"
code_str += f" for variation in range({num_variations}):\n"
code_str += " processed_image = process_image(image)\n\n"
code_str += " # Save the processed image\n"
code_str += " output_filename = f'processed_{os.path.splitext(filename)[0]}_{variation}{os.path.splitext(filename)[1]}'\n"
code_str += " cv2.imwrite(os.path.join(output_directory, output_filename), processed_image)\n\n"
code_str += (
" # Save the original label file for the processed image\n"
)
code_str += " if os.path.exists(label_path):\n"
code_str += " shutil.copy2(label_path, os.path.join(output_directory, os.path.splitext(output_filename)[0] + '.txt'))\n\n"
# Execute the batch processing function
code_str += (
"# Execute the batch processing function with the specified parameters\n"
)
code_str += "process_batch(input_directory, output_directory)\n"
return code_str
# Function to create an image processing pipeline based on the selected techniques and their parameters
def create_image_processing_pipeline(
selected_image_processings, image_processing_params
):
pipeline = []
for aug_name in selected_image_processings:
# Append the function call with its parameters to the pipeline
pipeline.append(
utils.apply_albumentation(image_processing_params[aug_name], aug_name)
)
# Compose all the image processings into one transformation
return A.Compose(pipeline)
# Function to process images and labels, apply image processing techniques, and create a zip file with the results
@st.cache_resource(show_spinner=False)
def process_images_and_labels(
image_files,
label_files,
selected_image_processings,
_image_processings_params,
num_variations,
include_original,
):
zip_buffer = io.BytesIO() # Create an in-memory buffer for the zip file
st.session_state[
"image_repository_preprocessing"
] = {} # Initialize a repository to store processed image data
st.session_state[
"processed_image_mapping_procesing"
] = {} # Map original images to their processed versions
st.session_state["unique_images_names"] = [] # List to store unique images names
# Create progress bar and text elements in Streamlit
progress_bar = st.progress(0)
progress_text = st.empty()
with zipfile.ZipFile(
zip_buffer, mode="a", compression=zipfile.ZIP_DEFLATED, allowZip64=True
) as zip_file:
# Compose all the image processings into one transformation
transform = create_image_processing_pipeline(
selected_image_processings, _image_processings_params
)
total_images = len(image_files) * num_variations
processed_count = 0 # Counter for processed images
# Iterate over each uploaded file
for image_name, image_file in image_files.items():
image_file.seek(0) # Reset file pointer to start
file_bytes = np.asarray(bytearray(image_file.read()), dtype=np.uint8)
original_image = cv2.cvtColor(
cv2.imdecode(file_bytes, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB
)
original_image_resized = utils.resize_image(original_image)
# Include original images and labels in the output if selected
if include_original:
original_img_buffer = io.BytesIO()
Image.fromarray(original_image).save(original_img_buffer, format="JPEG")
zip_file.writestr(image_file.name, original_img_buffer.getvalue())
# Save corresponding label file to zip if it exists
label_file = label_files.get(image_name)
if label_file is not None:
label_file.seek(0) # Reset the file pointer
zip_file.writestr(f"{image_name}.txt", label_file.read())
original_file_name = image_file.name
st.session_state["unique_images_names"].append(original_file_name)
st.session_state["processed_image_mapping_procesing"][
original_file_name
] = []
st.session_state["image_repository_preprocessing"][image_file.name] = {
"image": original_image_resized,
"label": label_files.get(image_name),
}
# Apply image processing techniques and generate variations
for i in range(num_variations):
random.seed(i)
# Apply the image processing pipeline to the image
processed_image = transform(image=original_image)["image"]
img_buffer = io.BytesIO()
Image.fromarray(processed_image).save(img_buffer, format="JPEG")
processed_filename = f"processed_{image_name.split('.')[0]}_{i}.jpg"
zip_file.writestr(processed_filename, img_buffer.getvalue())
processed_image_resized = utils.resize_image(processed_image)
# Save corresponding label file to zip if it exists
label_file = label_files.get(image_name)
if label_file is not None:
label_file.seek(0) # Reset the file pointer
zip_file.writestr(
f"processed_{image_name}_{i}.txt", label_file.read()
)
st.session_state["processed_image_mapping_procesing"][
image_file.name
].append(processed_filename)
st.session_state["image_repository_preprocessing"][
processed_filename
] = {
"image": processed_image_resized,
"label": label_file,
}
processed_count += 1
# Update progress bar and text
progress_bar.progress(processed_count / total_images)
progress_text.text(
f"Processing image {processed_count} of {total_images}"
)
# Remove the progress bar and text after processing is complete
progress_bar.empty()
progress_text.empty()
zip_buffer.seek(0) # Reset buffer to start for download
st.session_state["zip_data_processing"] = zip_buffer.getvalue()
# Function to generate a downloadable file
def display_code_and_download_button(generated_code):
def generate_downloadable_file(code_str):
return code_str.encode("utf-8")
# Display the generated code in Streamlit with description and download button in columns
with st.expander("Plug and Play Code"):
col1, col2 = st.columns([7, 3])
with col1:
st.markdown(
"""
### Description of the Code Pipeline
"""
)
st.markdown(
"""
<div style='text-align: justify;'>
This code is a ready-to-use Python script for batch augmentation. It applies selected augmentation techniques to all images in a specified input directory and saves the processed images in an output directory.
**To use this script:**
- Ensure you have the necessary dependencies installed.
- Specify the input and output paths: Replace `'path/to/input'` with the path to your input images and `'path/to/output'` with the desired path for the processed images.
- The number of processed variations per image, the inclusion of the original images in the output, and the processing techniques with their parameters will be automatically set based on your selections.
### Python Code
</div>
""",
unsafe_allow_html=True,
)
# Display python code
st.code(generated_code, language="python")
with col2:
# Create a button for downloading the Python file
st.download_button(
label="Download Python File",
data=generate_downloadable_file(generated_code),
file_name="image_processing_script.py",
mime="text/plain",
use_container_width=True,
)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896# Importing necessary libraries
import io
import os
import utils
import random
import shutil
import zipfile
import numpy as np
import pandas as pd
import streamlit as st
from ultralytics import YOLO
import plotly.graph_objs as go
from onnx.defs import onnx_opset_version
from plotly.subplots import make_subplots
# Function to get the dataset directory path based on the specified path type
def get_path(path_type):
main_directory_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if path_type == "train":
return os.path.join(
main_directory_path,
"model_data",
"input_files",
"datasets",
"train",
)
elif path_type == "val":
return os.path.join(
main_directory_path,
"model_data",
"input_files",
"datasets",
"val",
)
elif path_type == "test":
return os.path.join(
main_directory_path,
"model_data",
"input_files",
"datasets",
"test",
)
elif path_type == "config":
return os.path.join(main_directory_path, "model_data", "input_files")
elif path_type == "models":
return os.path.join(main_directory_path, "model_data", "models")
elif path_type == "output":
return os.path.join(main_directory_path, "model_data", "output_files")
else:
raise ValueError(f"Invalid path_type: {path_type}")
# Function to check minimum images in training and validation set
def check_min_images(total_files, train_pct, val_pct, test_pct):
# Calculate raw counts based on percentages
train_count = int(total_files * train_pct / 100)
val_count = int(total_files * val_pct / 100)
test_count = int(total_files * test_pct / 100)
# Ensure that both train and validation have at least one file
if train_count < 1 or val_count < 1:
return False
return True
# Function to clear data a folders
def clear_data_folders():
base_path = "./model_data/input_files/datasets"
for folder in ["train", "test", "val"]:
for subfolder in ["images", "labels"]:
folder_path = os.path.join(base_path, folder, subfolder)
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
os.makedirs(folder_path, exist_ok=True)
# Function to pairs image and label files based on their filenames
def pair_files(files):
paired_files = {}
for file in files:
# Split the filename into name and extension
file_name, file_ext = os.path.splitext(file.name)
# Initialize a dict for each unique file name
if file_name not in paired_files:
paired_files[file_name] = {"image": None, "label": None}
# Assign the file to its corresponding type (image or label) based on extension
if file_ext.lower() in [".jpg", ".png"]:
paired_files[file_name]["image"] = file
elif file_ext.lower() == ".txt":
paired_files[file_name]["label"] = file
return paired_files
# Function to split the paired files into training, testing, and validation sets based on specified percentages and saves them in corresponding folders
def split_and_save_files(paired_files, train_pct, test_pct):
base_path = "./model_data/input_files/datasets"
all_keys = list(paired_files.keys())
random.shuffle(all_keys)
# Determine the size of each dataset split
total_files = len(all_keys)
train_size = int(total_files * train_pct / 100)
test_size = int(total_files * test_pct / 100)
# Split the file keys into training, testing, and validation sets
train_keys = all_keys[:train_size]
test_keys = all_keys[train_size : train_size + test_size]
val_keys = all_keys[train_size + test_size :]
# Iterate through each split and save the files to their respective directories
for folder_name, keys in zip(
["train", "test", "val"], [train_keys, test_keys, val_keys]
):
for key in keys:
image_file = paired_files[key]["image"]
label_file = paired_files[key]["label"]
# Save the image and label files if they exist
if image_file:
save_file_to_folder(
image_file, os.path.join(base_path, folder_name, "images")
)
if label_file:
save_file_to_folder(
label_file, os.path.join(base_path, folder_name, "labels")
)
# Function to save an individual file to a specified folder
def save_file_to_folder(file, folder_path):
os.makedirs(folder_path, exist_ok=True)
file_path = os.path.join(folder_path, file.name)
with open(file_path, "wb") as f:
f.write(file.getbuffer())
# Function to save uploaded files to a specific folder within the base path
def save_files_to_folder(uploaded_files, folder_name):
# Define the base path for saving the files
base_path = "./model_data/input_files/datasets"
# Iterate through each uploaded file
for file in uploaded_files:
if file:
# Determine the file type based on file extension
file_type = (
"images"
if os.path.splitext(file.name)[1].lower() in [".jpg", ".png"]
else "labels"
)
# Save the file to the appropriate subfolder (images or labels)
save_file_to_folder(file, os.path.join(base_path, folder_name, file_type))
# Function to validate each line in the label file for bounding box data
def check_bboxes_label(label_file, class_dict):
for line in label_file:
try:
# Decode the line, strip whitespace, split into parts, and convert each part to float
class_id, x_center, y_center, width, height = map(
float, line.decode().strip().split()
)
# Check if bounding box coordinates and class ID are valid
if not (
0 <= x_center <= 1
and 0 <= y_center <= 1
and 0 <= width <= 1
and 0 <= height <= 1
and class_id in class_dict.keys()
):
# Return False if any condition is not met (invalid data)
return False
except Exception as e:
# Return False in case of any exception (e.g., parsing error)
return False
# Return True if all lines in the label file pass the validation
return True
# Function to validate each line in the label file for mask data
def check_masks_label(label_file, class_dict):
for line in label_file:
try:
# Decode the line and split into parts: class ID and points
parts = line.decode().strip().split()
class_id = int(
parts[0]
) # Convert the first part to an integer for class ID
points = [
float(p) for p in parts[1:]
] # Convert the remaining parts to float for coordinates
# Check if class ID exists in the class dictionary and all points are within [0, 1]
if not (class_id in class_dict.keys() and all(0 <= p <= 1 for p in points)):
return False # Return False if validation fails
except Exception as e:
# Return False in case of any exception (e.g., parsing error)
return False
return True # Return True if all lines in the label file pass the validation
# Function to read label from YOLO format
def read_label(file, selected_option, class_dict):
# Read the content of the file
file_content = file.readlines()
# Check and validate bounding box labels if the selected option is 'Bboxes'
if selected_option == "Bboxes":
return check_bboxes_label(file_content, class_dict) # Validate bbox labels
# Check and validate mask labels if the selected option is 'Masks'
elif selected_option == "Masks":
return check_masks_label(file_content, class_dict) # Validate mask labels
# Return False if the selected option is neither 'Bboxes' nor 'Masks'
return False
# Function to check for duplicates
def check_file_duplicates(file_names):
unique_names = set(file_names)
return len(unique_names) == len(file_names)
# Function to validates the uploaded image and label files
def validate_files(image_names, label_names):
# Check for duplicate filenames in both images and labels
if not check_file_duplicates(image_names) or not check_file_duplicates(label_names):
# Show warning if duplicates are found
st.warning(
"Duplicate file names detected. Please ensure each image and label has a unique name.",
icon="⚠️",
)
return False # Return False indicating validation failed
# Check if the number of images matches the number of labels
if len(image_names) != len(label_names):
# Show warning if counts don't match
st.warning(
"Count Mismatch: The number of uploaded images and labels does not match.",
icon="⚠️",
)
return False # Return False indicating validation failed
# Display a success message if the above checks pass
st.info(
f"Validated: {len(image_names)} images and labels successfully matched.",
icon="✅",
)
return True # Return True indicating successful validation
# Function to check labels format
@st.cache_resource(show_spinner=False)
def check_valid_labels(uploaded_files, selected_option, class_dict):
# Check if no files were uploaded
if len(uploaded_files) == 0:
st.warning("Please upload images and labels.", icon="⚠️")
return False
# Initialize lists to store names of image and label files
image_names, label_names = [], []
# Initialize a progress bar and progress text
progress_bar = st.progress(0)
progress_text = st.empty()
total_files = len(uploaded_files)
# Iterate over each uploaded file
for index, file in enumerate(uploaded_files):
# Reset the file pointer to the beginning
file.seek(0)
# Check file type and categorize as image or label
if file.type in ["image/jpeg", "image/png"]:
# Add to image names list if file is an image
image_names.append(file.name)
elif file.type == "text/plain":
# Read and validate label file
if not read_label(file, selected_option, class_dict):
# Show warning if label format or data is invalid
st.warning(
f"Invalid label format or data in file: {file.name}", icon="⚠️"
)
return False
# Add to label names list if file is a valid label
label_names.append(file.name)
# Update progress bar and display current progress
progress_percentage = (index + 1) / total_files
progress_bar.progress(progress_percentage)
progress_text.text(f"Validating file {index + 1} of {total_files}")
# Remove progress bar and progress text after processing
progress_bar.empty()
progress_text.empty()
# Validate if all images have corresponding labels and vice versa
return validate_files(image_names, label_names)
# Function to get training, validation and export configurations
def get_training_validation_export_configuration(selected_training):
with st.expander("Training Configuration"):
# User Instruction for Default Values
st.markdown(
"""
<div style='text-align: justify;'>
<b>User Instructions:</b> If you are unsure about the specific values to use for training parameters, it is
recommended to stick with the default values provided. These defaults are carefully chosen to provide a good balance
between performance and resource utilization for most scenarios. You can always come back and tweak these settings
once you have more experience or specific requirements for your model training.
</div>
""",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
# Training Configuration
st.markdown("### Training Configuration")
# Model Selection
st.write("**Model Selection**")
selected_model = st.selectbox(
"Choose a YOLOv8 model variant", list(utils.models_info.keys())
)
model_spec = utils.models_info[selected_model]
spec_string = (
"<div style='text-align: justify;'>"
f"The selected model, <b>{selected_model}</b>, is benchmarked on an image size of 640x640 pixels. It has a Mean Average Precision (mAPval) of <b>{model_spec['mAPval']}</b>, "
f"operates with a speed of <b>{model_spec['speed_cpu']} ms</b> on CPU (ONNX) and <b>{model_spec['speed_gpu']} ms</b> on GPU (TensorRT). "
f"It consists of approximately <b>{model_spec['params']} million</b> parameters and requires about <b>{model_spec['flops']} billion</b> Floating Point Operations (FLOPs)."
"</div>"
)
st.markdown(spec_string, unsafe_allow_html=True)
# Spacer
st.markdown("---")
# Time Configuration
st.write("**Time Configuration**")
col1_time, col2_time = st.columns([1, 3])
with col1_time:
top_padding_time = st.container()
time_allow = st.checkbox("Enable Time", value=False)
if time_allow:
with top_padding_time:
utils.top_padding(2)
time = col2_time.number_input(
"Time (hours)", min_value=1, max_value=100, value=1, step=1
)
else:
time = None
st.markdown(
"<div style='text-align: justify;'>Set the training duration in hours. This option overrides the epochs setting. Useful for limiting training time in scenarios with constrained resources.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Epochs Configuration
st.write("**Epochs Configuration**")
epochs = st.number_input(
"Epochs", min_value=1, max_value=1000, value=50, step=10
)
st.markdown(
"<div style='text-align: justify;'>Define the number of epochs for the training process. An epoch represents a complete pass over the entire dataset. More epochs can improve accuracy but increase training time.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Patience Configuration
st.write("**Patience Configuration**")
col1_patience, col2_patience = st.columns([1, 3])
with col1_patience:
top_padding_patience = st.container()
patience_allow = st.checkbox("Enable Patience", value=False)
if patience_allow:
with top_padding_patience:
utils.top_padding(2)
patience = col2_patience.number_input(
"Patience (epochs)", min_value=5, max_value=50, value=5, step=1
)
else:
patience = None
st.markdown(
"<div style='text-align: justify;'>Configure the early stopping mechanism. Patience denotes the number of epochs to wait for improvement in performance before stopping the training, helping to avoid overfitting.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Batch Size Configuration
st.write("**Batch Size Configuration**")
batch = st.number_input(
"Batch Size", min_value=-1, max_value=128, value=-1, step=1
)
st.markdown(
"<div style='text-align: justify;'>Determine the number of images processed together in one pass (batch). A larger batch size can lead to faster training but requires more memory. Use -1 for automatic batch sizing.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Image Size Configuration
st.write("**Image Size Configuration**")
imgsz = st.number_input(
"Image Size (pixels)", min_value=64, max_value=4096, value=640, step=32
)
st.markdown(
"<div style='text-align: justify;'>Specify the size of the input images. Larger images can capture more details but require more computational resources. The size is typically a square dimension, like 640x640 pixels.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Cache Configuration
st.write("**Cache Configuration**")
cache = st.selectbox("Cache Option", ["False", "True/ram", "disk"])
st.markdown(
"<div style='text-align: justify;'>Choose a caching method for data loading to speed up training. 'True/ram' caches data in RAM, 'disk' caches on disk, and 'False' disables caching.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Optimizer Configuration
st.write("**Optimizer Configuration**")
optimizer = st.selectbox(
"Optimizer",
["SGD", "Adam", "Adamax", "AdamW", "NAdam", "RAdam", "RMSProp", "auto"],
index=7,
)
st.markdown(
"<div style='text-align: justify;'>Select the optimizer for training. The optimizer adjusts weights to minimize the loss function. Choices include SGD, Adam, and others, with 'auto' selecting automatically based on the model.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# AMP Configuration
st.write("**AMP Configuration**")
amp = st.checkbox("Enable AMP", value=True)
st.markdown(
"<div style='text-align: justify;'>Enable Automatic Mixed Precision (AMP) to accelerate training on compatible hardware. AMP uses lower precision to reduce memory usage and speed up computations.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Deterministic Mode Configuration
st.write("**Deterministic Mode Configuration**")
deterministic = st.checkbox("Enable Deterministic Mode", value=False)
st.markdown(
"<div style='text-align: justify;'>Activate deterministic mode to ensure reproducible results. This mode might slow down the training but is useful for experimentation and debugging.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Rectangular Training Configuration
st.write("**Rectangular Training Configuration**")
rect = st.checkbox("Enable Rectangular Training", value=False)
st.markdown(
"<div style='text-align: justify;'>Enable rectangular training to process batches with minimal padding by reshaping images. This can lead to performance improvements but may affect accuracy.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Cosine Learning Rate Scheduler Configuration
st.write("**Cosine Learning Rate Scheduler**")
cos_lr = st.checkbox("Use Cosine LR Scheduler", value=False)
st.markdown(
"<div style='text-align: justify;'>Use a cosine learning rate scheduler to adjust the learning rate following a cosine curve, potentially leading to better convergence during training.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Freeze Layer Configuration
st.write("**Freeze Layer Configuration**")
col1_freeze, col2_freeze = st.columns([1, 3])
with col1_freeze:
top_padding_freeze = st.container()
freeze_allow = st.checkbox("Enable Freeze Layers", value=False)
if freeze_allow:
with top_padding_freeze:
utils.top_padding(2)
freeze = col2_freeze.number_input(
"Freeze Layers",
min_value=1,
max_value=1000,
value=10,
placeholder="Enter number of layers",
)
else:
freeze = None
st.markdown(
"<div style='text-align: justify;'>Enable freezing the initial layers of the model during training. Specify the number of layers to freeze or a comma-separated list of specific layer indices. Useful for fine-tuning pre-trained models without modifying early layers.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Initial Learning Rate Configuration
st.write("**Initial Learning Rate (lr0)**")
lr0 = st.number_input(
"Initial Learning Rate (lr0)",
min_value=0.00001,
max_value=1.0,
value=0.01,
format="%.5f",
)
st.markdown(
"<div style='text-align: justify;'>Specify the initial learning rate (lr0) for the training process. The initial rate is crucial as it determines the starting step size for weight updates. A well-chosen initial rate helps in achieving a balance between fast convergence and overshooting the optimal solution.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Final Learning Rate Configuration
st.write("**Final Learning Rate (lrf)**")
lrf = st.number_input(
"Final Learning Rate (lrf)",
min_value=0.00001,
max_value=1.0,
value=0.01,
format="%.5f",
)
st.markdown(
"<div style='text-align: justify;'>Determine the final learning rate, which is a factor (lrf) of the initial learning rate (lr0). This parameter is used to adjust the learning rate over the course of training, gradually decreasing it to fine-tune model weights and stabilize training as it approaches the minimum of the loss function.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Momentum Configuration
st.write("**Momentum Configuration**")
momentum = st.number_input(
"Momentum", min_value=0.0, max_value=1.0, value=0.937, format="%.3f"
)
st.markdown(
"<div style='text-align: justify;'>Set the momentum value for the optimizer. Momentum helps in accelerating the optimizer in the relevant direction and dampens oscillations, facilitating faster convergence.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Weight Decay Configuration
st.write("**Weight Decay Configuration**")
weight_decay = st.number_input(
"Weight Decay", min_value=0.0, max_value=0.1, value=0.0005, format="%.5f"
)
st.markdown(
"<div style='text-align: justify;'>Specify the weight decay, a regularization technique that adds a small penalty to the loss function for larger weights. It helps in preventing overfitting by encouraging simpler models.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Warmup Epochs Configuration
st.write("**Warmup Epochs Configuration**")
warmup_epochs = st.number_input(
"Warmup Epochs", min_value=0.0, max_value=10.0, value=3.0, step=0.1
)
st.markdown(
"<div style='text-align: justify;'>Define the number of warmup epochs. During warmup, the learning rate gradually increases to its initial value, which helps in stabilizing the training process in its early stages.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Warmup Momentum Configuration
st.write("**Warmup Momentum Configuration**")
warmup_momentum = st.number_input(
"Warmup Momentum", min_value=0.0, max_value=1.0, value=0.8, format="%.1f"
)
st.markdown(
"<div style='text-align: justify;'>Configure the momentum during the warmup phase. A lower momentum at the start can help in stabilizing the optimization process before reaching the specified momentum for the remaining epochs.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Warmup Bias Learning Rate Configuration
st.write("**Warmup Bias Learning Rate Configuration**")
warmup_bias_lr = st.number_input(
"Warmup Bias Learning Rate",
min_value=0.0,
max_value=1.0,
value=0.1,
format="%.1f",
)
st.markdown(
"<div style='text-align: justify;'>Adjust the bias learning rate during the warmup period. This parameter can be tuned to manage the initial learning rate specifically for the bias parameters in the early training phase.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Box Loss Gain Configuration
st.write("**Box Loss Gain Configuration**")
box = st.number_input(
"Box Loss Gain", min_value=0.0, max_value=10.0, value=7.5, step=0.1
)
st.markdown(
"<div style='text-align: justify;'>Configure the gain factor for the box loss. This gain helps in adjusting the importance of the box size and location accuracy in the loss function, affecting how the model prioritizes bounding box precision.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Class Loss Gain Configuration
st.write("**Class Loss Gain Configuration**")
cls = st.number_input(
"Class Loss Gain", min_value=0.0, max_value=10.0, value=0.5, step=0.1
)
st.markdown(
"<div style='text-align: justify;'>Set the gain factor for the class loss. This parameter scales the contribution of class prediction accuracy in the total loss, influencing how the model prioritizes correct class identification.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# DFL Loss Gain Configuration
st.write("**DFL Loss Gain Configuration**")
dfl = st.number_input(
"DFL Loss Gain", min_value=0.0, max_value=10.0, value=1.5, step=0.1
)
st.markdown(
"<div style='text-align: justify;'>Determine the gain factor for the DFL loss. Adjusting this gain influences the model's focus on the Directional Focal Loss component, which is critical for precise object localization and classification.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Label Smoothing Configuration
st.write("**Label Smoothing Configuration**")
label_smoothing = st.number_input(
"Label Smoothing (fraction)",
min_value=0.0,
max_value=1.0,
value=0.0,
format="%.1f",
)
st.markdown(
"<div style='text-align: justify;'>Specify the label smoothing value, a technique that introduces softening to the target labels. It promotes model generalization and reduces the impact of noisy labels on the training process.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Nominal Batch Size Configuration
st.write("**Nominal Batch Size Configuration**")
nbs = st.number_input(
"Nominal Batch Size", min_value=1, max_value=128, value=64, step=1
)
st.markdown(
"<div style='text-align: justify;'>Set the nominal batch size, which is used for normalizing the loss. This size does not affect the actual batch size but is used to scale the loss to a standard reference batch size.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Overlap Mask Configuration
st.write("**Overlap Mask Configuration**")
overlap_mask = st.checkbox("Masks Overlap during Training", value=True)
st.markdown(
"<div style='text-align: justify;'>Choose whether to allow masks to overlap during instance segmentation training. Overlapping can lead to more precise segmentation but may increase complexity.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Mask Ratio Configuration
st.write("**Mask Ratio Configuration**")
mask_ratio = st.number_input(
"Mask Downsample Ratio", min_value=1, max_value=10, value=4, step=1
)
st.markdown(
"<div style='text-align: justify;'>Set the downsample ratio for masks in instance segmentation. A higher ratio reduces the mask resolution, which can speed up computations but might decrease segmentation accuracy.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Dropout Configuration
st.write("**Dropout Configuration**")
dropout = st.number_input(
"Dropout Regularization",
min_value=0.0,
max_value=1.0,
value=0.0,
format="%.1f",
)
st.markdown(
"<div style='text-align: justify;'>Configure the dropout rate, which randomly disables a proportion of neurons during training. This prevents the model from relying too much on certain features and promotes better generalization.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Validation/Test Configuration
st.write("**Validation/Test Configuration**")
val = st.checkbox("Validate/Test during Training", value=True)
st.markdown(
"<div style='text-align: justify;'>Decide whether to perform validation and testing during the training process. Regular validation helps monitor model performance and adjust training accordingly.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Save Plots Configuration
st.write("**Save Plots Configuration**")
plots = st.checkbox("Save Plots and Images during Training", value=True)
st.markdown(
"<div style='text-align: justify;'>Enable saving of plots and images during training. This feature provides visual insights into the training progress and helps in diagnosing model performance across epochs.</div>",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
with st.expander("Validation Configuration"):
# User Instruction for Default Values
st.markdown(
"""
<div style='text-align: justify;'>
<b>User Instructions:</b> If you are unsure about the specific values to use for validation parameters, it is
recommended to stick with the default values provided. These defaults are carefully chosen to provide a good balance
between performance and resource utilization for most scenarios. You can always come back and tweak these settings
once you have more experience or specific requirements for your model validation.
</div>
""",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
# Validation Configuration
st.markdown("### Validation Configuration")
# Object Confidence Threshold
st.write("**Object Confidence Threshold**")
conf = st.number_input(
"Confidence Threshold",
min_value=0.0,
max_value=1.0,
value=0.001,
format="%.3f",
)
st.markdown(
"<div style='text-align: justify;'>Set the confidence threshold for object detection. This threshold filters out detections with lower confidence, reducing false positives and focusing on more likely object detections.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Intersection Over Union (IoU) Threshold
st.write("**IoU Threshold for NMS**")
iou = st.number_input(
"IoU Threshold", min_value=0.0, max_value=1.0, value=0.6, format="%.1f"
)
st.markdown(
"<div style='text-align: justify;'>Define the IoU threshold for Non-Maximum Suppression. NMS is used to refine the bounding boxes by eliminating redundancies and retaining the most probable ones.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Maximum Number of Detections
st.write("**Maximum Number of Detections**")
max_det = st.number_input(
"Max Detections", min_value=1, max_value=1000, value=300, step=1
)
st.markdown(
"<div style='text-align: justify;'>Limit the maximum number of detections per image. This setting is crucial for controlling the computational load and focusing the model on the most confident and relevant detections.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Use Half Precision
st.write("**Use Half Precision (FP16)**")
half = st.checkbox("Enable Half Precision", value=True)
st.markdown(
"<div style='text-align: justify;'>Enable half precision (FP16) training for enhanced performance on compatible GPUs. It reduces memory requirements and accelerates computation, beneficial for larger models and datasets.</div>",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
with st.expander("Export Configuration"):
# User Instruction for Default Values
st.markdown(
"""
<div style='text-align: justify;'>
<b>User Instructions:</b> If you are unsure about the specific values to use for export parameters, it is
recommended to stick with the default values provided. These defaults are carefully chosen to provide a good balance
between performance and resource utilization for most scenarios. You can always come back and tweak these settings
once you have more experience or specific requirements for your model export.
</div>
""",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
# Validation Configuration
st.markdown("### Export Configuration")
# Select Export Format
st.write("**Export Format**")
export_format = st.selectbox(
"Select Export Format",
[
"Only PyTorch",
"TorchScript",
"ONNX",
"OpenVINO",
"TensorRT",
"CoreML",
"TF SavedModel",
"TF GraphDef",
"TF Lite",
"TF Edge TPU",
"TF.js",
"PaddlePaddle",
"ncnn",
],
)
# Dynamically generate description
if export_format == "Only PyTorch":
st.markdown(
"""
<div style='text-align: justify;'>
You have selected <b>PyTorch</b> as the export format.
This will export the model in the standard PyTorch <code>.pt</code> format.
There are no additional format-specific parameters to consider for this selection.
The exported model will be the same as selected during training.
</div>
""",
unsafe_allow_html=True,
)
else:
format_info = utils.export_formats[export_format]
# Handling additional arguments
if len(format_info["arguments"]) > 0:
additional_arguments = ", ".join(format_info["arguments"])
arguments_info = f"Consider the following arguments for the <b>{export_format}</b> format: {additional_arguments}."
else:
arguments_info = (
"No additional parameters need to be considered for this format."
)
st.markdown(
f"""
<div style='text-align: justify;'>
You have selected <b>{export_format}</b> as the export format. Along with the PyTorch model,
this selection will also export the model in the <b>{export_format}</b> format. The image size of
the exported model will be the same as selected during training. {arguments_info}
</div>
""",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Use Keras for TF SavedModel export
st.write("**Use Keras for TF SavedModel Export**")
keras = st.checkbox("Enable Keras", value=False)
st.markdown(
"<div style='text-align: justify;'>Enabling Keras optimizes the TensorFlow SavedModel export for compatibility with the Keras API, making it easier to work with in Keras-centric workflows.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Optimize for mobile (TorchScript)
st.write("**Optimize TorchScript for Mobile**")
optimize = st.checkbox("Enable Optimization", value=False)
st.markdown(
"<div style='text-align: justify;'>Optimizing for mobile reduces the model size and computational needs, enhancing performance on mobile devices with limited resources.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# FP16 quantization
st.write("**FP16 Quantization**")
half = st.checkbox("Enable FP16 Quantization", value=False)
st.markdown(
"<div style='text-align: justify;'>FP16 quantization reduces model size and speeds up inference, especially on GPUs with Tensor Cores, while maintaining model accuracy.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# INT8 quantization
st.write("**INT8 Quantization**")
int8 = st.checkbox("Enable INT8 Quantization", value=False)
st.markdown(
"<div style='text-align: justify;'>INT8 quantization further reduces model size and inference time, ideal for edge devices, at the cost of a slight decrease in accuracy.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Dynamic axes for ONNX/TensorRT
st.write("**Dynamic Axes for ONNX/TensorRT**")
dynamic = st.checkbox("Enable Dynamic Axes", value=False)
st.markdown(
"<div style='text-align: justify;'>Dynamic axes allow the ONNX/TensorRT models to handle variable input sizes, increasing the model's flexibility in deployment.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Simplify model for ONNX/TensorRT
st.write("**Simplify Model for ONNX/TensorRT**")
simplify = st.checkbox("Enable Model Simplification", value=False)
st.markdown(
"<div style='text-align: justify;'>Simplification optimizes the ONNX/TensorRT models by removing redundant operations, improving efficiency without impacting accuracy.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# ONNX Opset Version Configuration
st.write("**ONNX Opset Version Configuration**")
col1_opset, col2_opset = st.columns([1, 3])
with col1_opset:
top_padding_opset = st.container()
opset_allow = st.checkbox("Specify Opset Version", value=False)
if opset_allow:
with top_padding_opset:
utils.top_padding(2)
# Create a range of opset versions for the dropdown
opset_versions = list(range(1, onnx_opset_version() + 1))
with col2_opset:
opset = st.selectbox(
"Select Opset Version",
opset_versions,
index=len(opset_versions) - 1,
)
else:
opset = None
st.markdown(
"<div style='text-align: justify;'>Select the ONNX opset version for the export. "
"Specifying an opset version can ensure compatibility with specific ONNX versions. "
"The latest version is recommended to ensure the most up-to-date features and optimizations. "
"If unsure, leave the checkbox unchecked to use the default opset version.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# TensorRT workspace size
st.write("**TensorRT Workspace Size (GB)**")
workspace = st.number_input(
"Workspace Size", min_value=1, max_value=32, value=4, step=1
)
st.markdown(
"<div style='text-align: justify;'>Set the TensorRT workspace size in GB. A larger workspace can lead to more optimized models but requires more memory.</div>",
unsafe_allow_html=True,
)
# Spacer
st.markdown("---")
# Add NMS for CoreML
st.write("**Add NMS for CoreML**")
nms = st.checkbox("Enable NMS", value=False)
st.markdown(
"<div style='text-align: justify;'>Enabling NMS (Non-Maximum Suppression) for CoreML models helps in reducing overlapping bounding boxes and improves the clarity of object detection results.</div>",
unsafe_allow_html=True,
)
# Padding
utils.top_padding(2)
if selected_training == "Object Detection":
model_path = os.path.join(
get_path("models"), selected_model.lower() + ".pt"
)
task = "detect"
elif selected_training == "Instance Segmentation":
model_path = os.path.join(
get_path("models"), selected_model.lower() + "-seg.pt"
)
task = "segment"
export_settings = {
"format": None if export_format == "Only PyTorch" else export_format,
"keras": keras,
"optimize": optimize,
"half": half,
"int8": int8,
"dynamic": dynamic,
"simplify": simplify,
"opset": opset,
"workspace": workspace,
"nms": nms,
}
return {
"model_path": model_path,
"task": task,
"model": selected_model,
"time": time,
"epochs": epochs,
"patience": patience,
"batch": batch,
"imgsz": imgsz,
"cache": cache,
"optimizer": optimizer,
"amp": amp,
"deterministic": deterministic,
"rect": rect,
"cos_lr": cos_lr,
"freeze": freeze,
"lr0": lr0,
"lrf": lrf,
"momentum": momentum,
"weight_decay": weight_decay,
"warmup_epochs": warmup_epochs,
"warmup_momentum": warmup_momentum,
"warmup_bias_lr": warmup_bias_lr,
"box": box,
"cls": cls,
"dfl": dfl,
"label_smoothing": label_smoothing,
"nbs": nbs,
"overlap_mask": overlap_mask,
"mask_ratio": mask_ratio,
"dropout": dropout,
"val": val,
"plots": plots,
"conf": conf,
"iou": iou,
"max_det": max_det,
"half": half,
"export_settings": export_settings,
}
# Function to generate python code for model training
def generate_python_code_model_training(training_configuration):
# Copy the original configuration and update with additional parameters
training_configuration_code = training_configuration.copy()
training_configuration_code["data"] = r".\config.yaml" # Path to config file
training_configuration_code["save_dir"] = r".\output\train" # Output directory
training_configuration_code["pretrained"] = True # Use a pretrained model
training_configuration_code["save"] = True # Save the trained model
training_configuration_code["save_period"] = -1 # Save period configuration
training_configuration_code["augment"] = False # Augmentation setting
training_configuration_code["seed"] = 0 # Seed for reproducibility
training_configuration_code["verbose"] = True # Verbose output
training_configuration_code["single_cls"] = False # Single class setting
training_configuration_code["resume"] = False # Resume training setting
training_configuration_code["exist_ok"] = True # Overwrite existing files
training_configuration_code["project"] = r".\output" # Project directory
training_configuration_code["name"] = "train" # Project name
# Extract the model name from the model path
model_name = training_configuration_code["model_path"].split("\\")[-1]
# Start with necessary library imports and model initialization
code_str = "# Importing necessary libraries\n"
code_str += "from ultralytics import YOLO\n\n"
# Initialize the YOLO model
code_str += f"# Initialize the YOLO model '{model_name}'\n"
code_str += f"model = YOLO('{model_name}')\n"
# Add the model training code
code_str += "\n# Start the training process\n"
code_str += "model.train(\n"
for key, value in training_configuration_code.items():
if key not in [
"model_path",
"model",
"export_settings",
]: # Exclude specific keys
code_str += f" {key}={value},\n"
code_str = code_str.rstrip(",\n") + "\n)\n"
# Add model export code
code_str += "\n# Model export process\n"
code_str += "model.export(\n"
for key, value in training_configuration_code["export_settings"].items():
if key == "format" and value is None:
continue # Skip format if it's None
code_str += f" {key}={value},\n"
code_str = code_str.rstrip(",\n") + "\n)\n"
return code_str
# Function to overwrites a Python file with new code
def overwrite_python_file(code_str, file_path):
# Open the file in write mode, which automatically deletes old content
with open(file_path, "w") as file:
file.write(code_str)
# Function to generate a downloadable file
def display_code_and_download_button(generated_code):
# Display the generated code in Streamlit with description and download button in columns
with st.expander("Plug and Play Code"):
col1, col2 = st.columns([7, 3])
with col1:
st.markdown(
"""
### Description of the Code Pipeline
"""
)
st.markdown(
"""
<div style='text-align: justify;'>
This Python script is configured for training a YOLO model. It includes necessary configurations and parameters for a custom YOLO model training session.
**To use this script:**
- Ensure you have the necessary dependencies installed.
- Place your image and label files in the `'datasets/train'`, `'datasets/val'`, and `'datasets/test'` folders respectively.
- The `'config.yaml'` file and the training script are set up based on your provided configurations.
### Python Code
</div>
""",
unsafe_allow_html=True,
)
# Display python code
st.code(generated_code, language="python")
# Determine the main directory path
main_directory_path = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
# Overwrites a Python file with new code
overwrite_python_file(
generated_code,
os.path.join(
main_directory_path,
"model_data",
"model_training_code_pipline",
"model_training.py",
),
)
# Determine the main directory path
main_directory_path = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
# Prepare a ZIP file of the training output folder in memory for download
zip_bytes_io = zip_folder_to_bytesio(
os.path.join(
main_directory_path, "model_data", "model_training_code_pipline"
)
)
with col2:
# Create a button for downloading the training pipeline
st.download_button(
label="Download Training Pipeline",
data=zip_bytes_io,
file_name="model_training_code.zip",
mime="application/zip",
use_container_width=True,
)
# Function to generates a YOLO model training code snippet and displays it with a download button
def generate_and_display_yolo_training_code(class_labels, training_configuration):
# Determine the main directory path
main_directory_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Construct the path to the config file directory
config_file_path = os.path.join(
main_directory_path, "model_data", "model_training_code_pipline"
)
# Define the path to the dataset directory
dataset_directory_path = "./datasets"
# Create YOLO config file using provided class labels and dataset directory
create_yolo_config_file(config_file_path, class_labels, dataset_directory_path)
# Generate the Python code for YOLO model training
generated_code = generate_python_code_model_training(training_configuration)
# Display the generated code and a download button
display_code_and_download_button(generated_code)
# Function to create a yolo config file
def create_yolo_config_file(
config_file_path, class_labels, dataset_directory_path=None
):
if dataset_directory_path is None:
dataset_directory_path = os.path.join(config_file_path, "datasets")
# Number of classes
num_classes = len(class_labels)
# Create the configuration content
config_content = f"""path: {dataset_directory_path} # Path to the dataset directory
train: train # Path to the training set directory
val: val # Path to the validation set directory
test: test # Path to the testing set directory
nc: {num_classes} # Number of classes
names: {class_labels} # List of class names
"""
# Write the configuration to a file
with open(os.path.join(config_file_path, "config.yaml"), "w") as file:
file.write(config_content)
# Function to delete and recreate a folder
def delete_and_recreate_folder(folder_path):
try:
# Use shutil.rmtree to delete the folder and its contents
shutil.rmtree(folder_path)
# Recreate the folder at the same location
os.makedirs(folder_path)
except Exception as e:
print(f"Error deleting or recreating folder {folder_path}: {e}")
# Function to read csv and get values
def read_csv_and_get_values(csv_file_path):
# Read the CSV file into a pandas DataFrame
df = pd.read_csv(csv_file_path)
# Initialize an empty dictionary to store the results
result_dict = {}
# Iterate through the columns of the DataFrame
for column in df.columns:
# Remove leading and trailing spaces from the column name
clean_column_name = column.strip()
# Get the values in the column
column_values = df[column].astype(float)
# Add the cleaned column name and values to the result dictionary
result_dict[clean_column_name] = np.array(column_values)
return result_dict
# Global variables
plot_container = None
val_dataframe_container = None
progress_bar = None
progress_text = None
# Function to define a custom callback function for on_pretrain_routine_start
def on_pretrain_routine_start(trainer):
global progress_text, progress_bar
progress_bar = st.empty()
progress_text = st.empty()
progress_text.info(
"Loading selected model...",
icon="✅",
)
# Function to define a custom callback function for on_train_start
def on_train_start(trainer):
global progress_bar, progress_text
progress_bar = st.progress(0)
progress_text.info(
"Training Started...",
icon="✅",
)
# Function to display metrics plot
st.cache_resource(show_spinner=False)
def display_metrics_plot(output_data):
global plot_container
# Extract data for each metric
epoch_history = output_data.get("epoch")
# Extract loss histories
train_box_loss_history = output_data.get("train/box_loss")
train_cls_loss_history = output_data.get("train/cls_loss")
train_dfl_loss_history = output_data.get("train/dfl_loss")
train_seg_loss_history = output_data.get("train/seg_loss")
val_box_loss_history = output_data.get("val/box_loss")
val_cls_loss_history = output_data.get("val/cls_loss")
val_dfl_loss_history = output_data.get("val/dfl_loss")
val_seg_loss_history = output_data.get("val/seg_loss")
if train_seg_loss_history is None:
train_seg_loss_history = epoch_history * 0
val_seg_loss_history = epoch_history * 0
# Extract precision, recall, and mAP histories for B and M box/mask
precision_B_history = output_data.get("metrics/precision(B)")
recall_B_history = output_data.get("metrics/recall(B)")
mAP50_B_history = output_data.get("metrics/mAP50(B)")
mAP50_95_B_history = output_data.get("metrics/mAP50-95(B)")
precision_M_history = output_data.get("metrics/precision(M)")
recall_M_history = output_data.get("metrics/recall(M)")
mAP50_M_history = output_data.get("metrics/mAP50(M)")
mAP50_95_M_history = output_data.get("metrics/mAP50-95(M)")
# Check for 'None' data and adjust the number of rows in the grid
num_rows = 4
subplot_titles = [
"Precision B",
"Recall B",
"mAP50 B",
"mAP50-95 B",
"Precision R",
"Recall R",
"mAP50 R",
"mAP50-95 R",
"Train Box Loss",
"Train Class Loss",
"Train DFL Loss",
"Train Seg Loss",
"Val Box Loss",
"Val Class Loss",
"Val DFL Loss",
"Val Seg Loss",
]
if precision_M_history is None:
num_rows = 3
subplot_titles = subplot_titles[0:4] + subplot_titles[8:]
# Create a subplot grid
fig = make_subplots(
rows=num_rows,
cols=4,
subplot_titles=subplot_titles,
vertical_spacing=0.05,
)
# Initialize row number
row_number = 1
# Add precision, recall, mAP plots for B and R box/mask
fig.add_trace(
go.Scatter(
x=epoch_history, y=precision_B_history, mode="lines", name="Precision B"
),
row=row_number,
col=1,
)
fig.add_trace(
go.Scatter(x=epoch_history, y=recall_B_history, mode="lines", name="Recall B"),
row=row_number,
col=2,
)
fig.add_trace(
go.Scatter(x=epoch_history, y=mAP50_B_history, mode="lines", name="mAP50 B"),
row=row_number,
col=3,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=mAP50_95_B_history, mode="lines", name="mAP50-95 B"
),
row=row_number,
col=4,
)
if precision_M_history is not None:
# Increment row number
row_number += 1
fig.add_trace(
go.Scatter(
x=epoch_history, y=precision_M_history, mode="lines", name="Precision R"
),
row=row_number,
col=1,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=recall_M_history, mode="lines", name="Recall R"
),
row=row_number,
col=2,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=mAP50_M_history, mode="lines", name="mAP50 R"
),
row=row_number,
col=3,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=mAP50_95_M_history, mode="lines", name="mAP50-95 R"
),
row=row_number,
col=4,
)
# Increment row number
row_number += 1
# Add loss plots
fig.add_trace(
go.Scatter(
x=epoch_history,
y=train_box_loss_history,
mode="lines",
name="Train Box Loss",
),
row=row_number,
col=1,
)
fig.add_trace(
go.Scatter(
x=epoch_history,
y=train_cls_loss_history,
mode="lines",
name="Train Class Loss",
),
row=row_number,
col=2,
)
fig.add_trace(
go.Scatter(
x=epoch_history,
y=train_dfl_loss_history,
mode="lines",
name="Train DFL Loss",
),
row=row_number,
col=3,
)
fig.add_trace(
go.Scatter(
x=epoch_history,
y=train_seg_loss_history,
mode="lines",
name="Train Seg Loss",
),
row=row_number,
col=4,
)
# Increment row number
row_number += 1
fig.add_trace(
go.Scatter(
x=epoch_history, y=val_box_loss_history, mode="lines", name="Val Box Loss"
),
row=row_number,
col=1,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=val_cls_loss_history, mode="lines", name="Val Class Loss"
),
row=row_number,
col=2,
)
fig.add_trace(
go.Scatter(
x=epoch_history, y=val_dfl_loss_history, mode="lines", name="Val DFL Loss"
),
row=row_number,
col=3,
)
fig.add_trace(
go.Scatter(
x=epoch_history,
y=val_seg_loss_history,
mode="lines",
name="Val Seg Loss",
),
row=row_number,
col=4,
)
# Check if the plot container is already initialized
if plot_container is None:
plot_container = st.empty()
# Update layout
fig.update_layout(
height=1200,
width=1600,
title_text="Metrics",
legend=dict(orientation="h", yanchor="bottom", xanchor="left"),
)
# Display the updated plot in the same container
plot_container.plotly_chart(fig, use_container_width=True)
# Function to define a custom callback function for on_fit_epoch_end
def on_fit_epoch_end(trainer):
current_epoch = int(trainer.epoch)
total_epochs = int(trainer.epochs)
# Define the path to the output CSV
output_csv_path = os.path.join(get_path("output"), "train", "results.csv")
# Read the CSV data
st.session_state["plot_data"] = read_csv_and_get_values(output_csv_path)
# Call a function to update the plot using this data
display_metrics_plot(st.session_state["plot_data"])
# Update progress bar and text
progress_bar.progress((current_epoch + 1) / total_epochs)
progress_text.write(f"Epoch {(current_epoch + 1)}/{total_epochs}")
# Function to define a custom callback function for on_train_end
def on_train_end(trainer):
global progress_bar, progress_text
progress_bar.empty()
progress_text.info(
"Best and last model save completed successfully.",
icon="✅",
)
# Function to add various callbacks to the YOLO model for different stages of the training process
def callback_add(model):
# Add a callback to be triggered at the start of the pre-training routine
model.add_callback("on_pretrain_routine_start", on_pretrain_routine_start)
# Add a callback to be triggered at the start of the training
model.add_callback("on_train_start", on_train_start)
# Add a callback to be triggered at the end of each training epoch
model.add_callback("on_fit_epoch_end", on_fit_epoch_end)
# Add a callback to be triggered at the end of the training process
model.add_callback("on_train_end", on_train_end)
# Function to zip a folder and all its subfolders and return a BytesIO object
def zip_folder_to_bytesio(folder_path):
bytes_io = io.BytesIO()
with zipfile.ZipFile(bytes_io, "w", zipfile.ZIP_DEFLATED) as zipf:
folder_path_abs = os.path.abspath(folder_path)
for root, dirs, files in os.walk(folder_path):
# Calculate the relative path from the folder_path
folder_rel_path = os.path.relpath(root, folder_path_abs)
# If the directory is empty, add the directory itself
if not dirs and not files:
# ZIP format requires a trailing slash for empty directories
zip_dir_path = f"{folder_rel_path}/" if folder_rel_path != "." else ""
zipf.write(root, zip_dir_path)
for file in files:
file_path = os.path.join(root, file)
# Construct the path within the zip file
zip_file_path = (
os.path.join(folder_rel_path, file)
if folder_rel_path != "."
else file
)
zipf.write(file_path, zip_file_path)
bytes_io.seek(0) # Go to the start of the BytesIO buffer
return bytes_io
# Function to display Metrics Table
st.cache_resource(show_spinner=False)
def display_val_dataframe(val_dataframe):
global val_dataframe_container
# Check if the dataframe container is already initialized
if val_dataframe_container is None:
val_dataframe_container = st.container()
# Display the updated dataframe in the same container
with val_dataframe_container:
# Display the message to indicate that the metrics table is ready
st.markdown("**Metrics Table**", unsafe_allow_html=True)
# Display the DataFrame
st.dataframe(val_dataframe)
# Function to display the DataFrame
def val_dataframe(model):
# Placeholder for the initial message
message = st.empty()
message.markdown("**Generating Metrics Table...**", unsafe_allow_html=True)
# Extract the metrics from the model
metrics = model.val()
# Extract the class indices and names
class_index = metrics.ap_class_index
class_names = metrics.names
# Extract precision, recall, and mAP values for the box (B) metrics
precision_B_values = metrics.box.p
recall_B_values = metrics.box.r
mAP50_95_B_values = [metrics.box.maps[i] for i in class_index]
# Check if segmentation (mask) metrics exist
try:
metrics_mask = metrics.seg
except:
metrics_mask = False
if metrics_mask:
precision_M_values = metrics_mask.p
recall_M_values = metrics_mask.r
mAP50_95_M_values = [metrics_mask.maps[i] for i in class_index]
# Extract aggregated metrics from the results dictionary
results_dict = metrics.results_dict
# Initialize lists for overall precision, recall, and mAP for box (B)
precision_B = [results_dict.get("metrics/precision(B)")]
recall_B = [results_dict.get("metrics/recall(B)")]
mAP50_95_B = [results_dict.get("metrics/mAP50-95(B)")]
# Initialize lists for overall precision, recall, and mAP for mask (M) if available
precision_M = [results_dict.get("metrics/precision(M)")] if metrics_mask else None
recall_M = [results_dict.get("metrics/recall(M)")] if metrics_mask else None
mAP50_95_M = [results_dict.get("metrics/mAP50-95(M)")] if metrics_mask else None
# Create a list of class names starting with "All" for the overall metrics
name_list = ["All"] + [str(class_names[i]) for i in class_index]
# Extend the metrics lists with values for each class
precision_B.extend(precision_B_values)
recall_B.extend(recall_B_values)
mAP50_95_B.extend(mAP50_95_B_values)
# If mask metrics are available, extend their lists with values for each class
if metrics_mask:
precision_M.extend(precision_M_values)
recall_M.extend(recall_M_values)
mAP50_95_M.extend(mAP50_95_M_values)
# Create a DataFrame with the computed metrics
if metrics_mask:
st.session_state["val_dataframe"] = pd.DataFrame(
{
"Class Name": name_list,
"Precision (B)": precision_B,
"Recall (B)": recall_B,
"mAP50-95 (B)": mAP50_95_B,
"Precision (M)": precision_M,
"Recall (M)": recall_M,
"mAP50-95 (M)": mAP50_95_M,
}
)
else:
st.session_state["val_dataframe"] = pd.DataFrame(
{
"Class Name": name_list,
"Precision (B)": precision_B,
"Recall (B)": recall_B,
"mAP50-95 (B)": mAP50_95_B,
}
)
# Clear the initial message
message.empty()
# Update the message to indicate that the metrics table is ready and Display the DataFrame
display_val_dataframe(st.session_state["val_dataframe"])
# Function to train the YOLO model
def train_yolo_model(training_configuration):
# Clear and recreate the output folder to ensure a fresh start
delete_and_recreate_folder(get_path("output"))
# Initialize the YOLO model with the specified path from the training configuration
model = YOLO(training_configuration["model_path"])
# Add any callbacks or additional configuration to the model
callback_add(model)
# Train the model with the specified parameters
model.train(
task=training_configuration["task"],
data=os.path.join(get_path("config"), "config.yaml"),
epochs=training_configuration["epochs"],
time=training_configuration["time"],
patience=training_configuration["patience"],
batch=training_configuration["batch"],
imgsz=training_configuration["imgsz"],
save=True,
save_period=-1,
cache=training_configuration["cache"],
pretrained=True,
optimizer=training_configuration["optimizer"],
verbose=True,
seed=0,
deterministic=training_configuration["deterministic"],
single_cls=False,
rect=training_configuration["rect"],
cos_lr=training_configuration["cos_lr"],
resume=False,
amp=training_configuration["amp"],
fraction=1.0,
freeze=training_configuration["freeze"],
lr0=training_configuration["lr0"],
lrf=training_configuration["lrf"],
momentum=training_configuration["momentum"],
weight_decay=training_configuration["weight_decay"],
warmup_epochs=training_configuration["warmup_epochs"],
warmup_momentum=training_configuration["warmup_momentum"],
warmup_bias_lr=training_configuration["warmup_bias_lr"],
box=training_configuration["box"],
cls=training_configuration["cls"],
dfl=training_configuration["dfl"],
label_smoothing=training_configuration["label_smoothing"],
nbs=training_configuration["nbs"],
overlap_mask=training_configuration["overlap_mask"],
mask_ratio=training_configuration["mask_ratio"],
dropout=training_configuration["dropout"],
val=training_configuration["val"],
plots=training_configuration["plots"],
save_dir=os.path.join(get_path("output"), "train"),
project=get_path("output"),
name="train",
augment=False,
exist_ok=True,
)
return model
# Function to export the model with the given parameters
def export_model_with_parameters(model, export_params):
global progress_text
if export_params["format"] is not None:
# Informing the user that the export process has started
progress_text.info(
"Starting the export process with the specified settings.",
icon="✅",
)
# Perform the model export
model.export(
format=export_params["format"],
keras=export_params["keras"],
optimize=export_params["optimize"],
half=export_params["half"],
int8=export_params["int8"],
dynamic=export_params["dynamic"],
simplify=export_params["simplify"],
opset=export_params["opset"],
workspace=export_params["workspace"],
nms=export_params["nms"],
)
# Informing the user that the export process has completed successfully
progress_text.info(
"The model has been successfully saved using the specified export settings.",
icon="✅",
)
# Function to start the YOLO model training process
def start_yolo_training(selected_training, class_labels):
global plot_container, val_dataframe_container
# Retrieve the training configuration based on the user's selection
training_configuration = get_training_validation_export_configuration(
selected_training
)
# Generates a YOLO model training code snippet and displays it with a download button
generate_and_display_yolo_training_code(class_labels, training_configuration)
# Create two columns
col1, col2 = st.columns(2)
# When the "Start Training" button is clicked in the first column
if col1.button("Start Training", use_container_width=True):
plot_container = None
val_dataframe_container = None
with st.spinner("Training in Progress..."):
# Train the YOLO model using the provided configuration
trained_model = train_yolo_model(training_configuration)
# Export the model with the given parameters
export_model_with_parameters(
trained_model, training_configuration["export_settings"]
)
# Display the validation results in a DataFrame after training
val_dataframe(trained_model)
elif "plot_data" in st.session_state and "val_dataframe" in st.session_state:
plot_container = None
val_dataframe_container = None
# Display metrics plot and table if already exist
display_metrics_plot(st.session_state["plot_data"])
display_val_dataframe(st.session_state["val_dataframe"])
# Prepare a ZIP file of the training output folder in memory for download
zip_bytes_io = zip_folder_to_bytesio(os.path.join(get_path("output"), "train"))
# Provide a button in the second column to download the ZIP file
col2.download_button(
label="Download",
data=zip_bytes_io,
file_name="model_training_output.zip",
mime="application/zip",
use_container_width=True,
)1234567891011121314---title: CV Acceleratoremoji: 🐠colorFrom: yellowcolorTo: bluesdk: streamlitsdk_version: 1.41.1app_file: app.pypinned: falselicense: unlicenseshort_description: A no-code CV Accelerator---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112---title: CV Acceleratoremoji: 📷colorFrom: graycolorTo: indigosdk: streamlitsdk_version: 1.32.0app_file: Welcome.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
12345678# Set the page config
import streamlit as st
st.set_page_config(
page_title="Welcome",
page_icon=":open_file_folder:",
layout="wide",
)123456789[theme]
primaryColor = "#101276"
backgroundColor = "#FFFFFF"
textColor = "#006EC0"
secondaryBackgroundColor = "#D2E5F2"
font = "sans serif"
[server]
maxUploadSize = 10000123456path: C:\Users\SamkeetSangai\OneDrive - Blend 360\Work\CoEs\Computer Vision\cv_accelerator\model_data\input_files\datasets # Path to the dataset directory
train: train # Path to the training set directory
val: val # Path to the validation set directory
test: test # Path to the testing set directory
nc: 80 # Number of classes
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] # List of class names123456path: ./datasets # Path to the dataset directory
train: train # Path to the training set directory
val: val # Path to the validation set directory
test: test # Path to the testing set directory
nc: 80 # Number of classes
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] # List of class names123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869# Importing necessary libraries
from ultralytics import YOLO
# Initialize the YOLO model 'yolov8n.pt'
model = YOLO('yolov8n.pt')
# Start the training process
model.train(
task=detect,
time=None,
epochs=2,
patience=None,
batch=4,
imgsz=640,
cache=False,
optimizer=auto,
amp=True,
deterministic=False,
rect=False,
cos_lr=False,
freeze=None,
lr0=0.01,
lrf=0.01,
momentum=0.937,
weight_decay=0.0005,
warmup_epochs=3.0,
warmup_momentum=0.8,
warmup_bias_lr=0.1,
box=7.5,
cls=0.5,
dfl=1.5,
label_smoothing=0.0,
nbs=64,
overlap_mask=True,
mask_ratio=4,
dropout=0.0,
val=True,
plots=True,
conf=0.001,
iou=0.6,
max_det=300,
half=False,
data=.\config.yaml,
save_dir=.\output\train,
pretrained=True,
save=True,
save_period=-1,
augment=False,
seed=0,
verbose=True,
single_cls=False,
resume=False,
exist_ok=True,
project=.\output,
name=train
)
# Model export process
model.export(
keras=False,
optimize=False,
half=False,
int8=False,
dynamic=False,
simplify=False,
opset=None,
workspace=4,
nms=False
)123version https://git-lfs.github.com/spec/v1oid sha256:dfa891dab47031c12ac82cb1b883af4ab9efbc732118cc04604bfbf107f9dfa8size 92391859123version https://git-lfs.github.com/spec/v1oid sha256:18218ea4798da042d9862e6029ca9531adbd40ace19b6c9a75e2e28f1adf30ccsize 87769683123version https://git-lfs.github.com/spec/v1oid sha256:f9fc740ca0824e14b44681d491dc601efa664ec6ecea9a870acf876053826448size 54899779123version https://git-lfs.github.com/spec/v1oid sha256:6c25b0b63b1a433843f06d821a9ac1deb8d5805f74f0f38772c7308c5adc55a5size 52117635123version https://git-lfs.github.com/spec/v1oid sha256:d39e867b2c3a5dbc1aa764411544b475cb14727bf6af1ec46c238f8bb1351ab9size 7054355123version https://git-lfs.github.com/spec/v1oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95size 6534387123version https://git-lfs.github.com/spec/v1oid sha256:c035c6c5f9c48ee962518ef648854c2cc5e60fa404fac443e17d306fdda16543size 23897299123version https://git-lfs.github.com/spec/v1oid sha256:268e5bb54c640c96c3510224833bc2eeacab4135c6deb41502156e39986b562dsize 22573363123version https://git-lfs.github.com/spec/v1oid sha256:d63cbfa5764867c0066bedfa43cf2dcd90a412a1de44b2e238c43978a9d28ea6size 144076467123version https://git-lfs.github.com/spec/v1oid sha256:c4d5a3f000d771762f03fc8b57ebd0aae324aeaefdd6e68492a9c4470f2d1e8bsize 1368675391python3-opencv123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512# Set the page config
import streamlit as st
st.set_page_config(
page_title="Image Processing",
page_icon=":open_file_folder:",
layout="wide",
initial_sidebar_state="collapsed",
)
# Importing necessary libraries
import cv2
import utils
import numpy as np
import Functions.image_processing_functions as image_processing_functions
# Load image processing technique parameters and details from an Excel file
image_processing_params_df = utils.load_data_from_excel(
"packages_db.xlsx", "image_processing_parameters"
)
image_processing_details_df = utils.load_data_from_excel(
"packages_db.xlsx", "image_processing_details"
)
# Display the page title
st.title("Image Processing")
# # Clear the Streamlit session state on the first load of the page
# utils.clear_session_state_on_first_load("image_processing_clear")
# List of session state keys to initialize if they are not already present
session_state_keys = [
"file_uploader_key_processing",
"select_processing_technique_key_processing",
]
# Iterate through each session state key
for key in session_state_keys:
# Check if the key is not already in the session state
if key not in st.session_state:
# Initialize the key with a dictionary containing itself set to True
st.session_state[key] = {key: True}
# Initialize session state variables if not present
if "validation_triggered" not in st.session_state:
st.session_state["validation_triggered"] = False
if "uploaded_files_cache_processing" not in st.session_state:
st.session_state["uploaded_files_cache_processing"] = False
if "zip_data_processing" not in st.session_state:
st.session_state["zip_data_processing"] = ""
if "widget_states" not in st.session_state:
st.session_state["widget_states"] = {}
# Interface for uploading an images and labels
utils.display_file_uploader(
"uploaded_files",
"Choose images and labels...",
st.session_state["file_uploader_key_processing"],
st.session_state["uploaded_files_cache_processing"],
)
# Note to users
st.markdown(
"""
<div style='text-align: justify;'>
<b>Note to Users:</b>
<ul>
<li>The <i>first uploaded image</i> will be used for demonstration purposes and to validate parameters for image processing techniques.</li>
<li>Uploading <i>labels is optional</i>. If no labels are uploaded, the output will consist solely of processed images.</li>
<li>When moving to another page or if you wish to upload a new set of images and labels, don't forget to hit the <b>Reset</b> button. This helps in faster computation and frees up unused memory, ensuring smoother operation.</li>
</ul>
</div>
""",
unsafe_allow_html=True,
)
# List of session state variables to initialize
session_vars = [
"is_valid",
"image_files",
"label_files",
"first_image_file",
"first_label_file",
]
# Initialize each variable as None if it doesn't exist in the session state
for var in session_vars:
if var not in st.session_state:
st.session_state[var] = None
# Create two columns
col1, col2 = st.columns(2)
# Button to trigger validation
if (
col1.button("Validate Input", use_container_width=True)
or st.session_state["widget_states"].get("validate_input_button", False)
) and not st.session_state["validation_triggered"]:
st.session_state["validation_triggered"] = True
st.session_state["uploaded_files_cache_processing"] = True
(
st.session_state["is_valid"],
st.session_state["image_files"],
st.session_state["label_files"],
st.session_state["first_image_file"],
st.session_state["first_label_file"],
) = image_processing_functions.check_valid_labels(
st.session_state["uploaded_files"]
)
elif st.session_state["validation_triggered"]:
pass
else:
st.session_state["is_valid"] = False
st.warning(
"Please upload images and labels and click **Validate Input**.", icon="⚠️"
)
with col2:
# Check if the 'Reset' button is pressed
if st.button("Reset", use_container_width=True):
# Toggle the keys for file uploader and processing technique to reset their states
current_value = st.session_state["file_uploader_key_processing"][
"file_uploader_key_processing"
]
updated_value = not current_value # Invert the current value
# List of session state keys that need to be reset
session_state_keys = [
"file_uploader_key_processing",
"select_processing_technique_key_processing",
]
# Iterate through each session state key
for session_state_key in session_state_keys:
# Update each key in the session state with the toggled value
st.session_state[session_state_key] = {session_state_key: updated_value}
# Clear all other session state keys except for widget_state_keys
for key in list(st.session_state.keys()):
if key not in session_state_keys:
del st.session_state[key]
# Clear global variables except for protected and Streamlit module
global_vars = list(globals().keys())
vars_to_delete = [
var for var in global_vars if not var.startswith("_") and var != "st"
]
for var in vars_to_delete:
del globals()[var]
# Clear the Streamlit caches
st.cache_resource.clear()
st.cache_data.clear()
# Rerun the app to reflect the reset state
st.rerun()
# Interface to select image processing techniques
available_image_processings = image_processing_details_df["Name"]
# Mapping each image processing techniques to its corresponding image types
input_mapping_dict = utils.technique_image_input_mapping(
available_image_processings, image_processing_details_df
)
# Present the option to select image processing techniques only if the uploaded files are validated successfully
if st.session_state["is_valid"]:
selected_image_processings = st.multiselect(
"Select image processing technique(s)",
available_image_processings,
key=st.session_state["select_processing_technique_key_processing"],
)
# Read the first uploaded image into a NumPy array
st.session_state["first_image_file"].seek(0) # Reset file pointer to start
file_bytes_first_image = np.frombuffer(
st.session_state["first_image_file"].read(), dtype=np.uint8
)
uploaded_first_image = cv2.cvtColor(
cv2.imdecode(file_bytes_first_image, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB
)
# Resize the image
uploaded_first_image = cv2.resize(uploaded_first_image, (256, 256))
else:
# Reset selected techniques to empty if input validation fails
selected_image_processings = []
#######################################################################################################
# Build custom image processing pipeline
#######################################################################################################
# Store parameters for each selected image processing technique
image_processings_params = {}
# Initialize a flag to track if any error exists
error_in_parameters = False
# Loop through each selected image processing techniques to set up parameters
for image_processing in selected_image_processings:
with st.expander(f"{image_processing}"):
# Retrieve image processing details from the database
image_processing_info = image_processing_details_df[
image_processing_details_df["Name"] == image_processing
]
# Set up columns for displaying details and image placeholders
details_col, image_col = st.columns([7, 3])
with details_col:
# Display the description for the image processing technique
image_processing_description = (
image_processing_info["Description"].iloc[0]
if not image_processing_info.empty
else "No description available."
)
st.markdown(
f"<div style='text-align: justify;'><b>Description:</b> {image_processing_description}</div>",
unsafe_allow_html=True,
)
# Display the category for the image processing
image_processing_category = (
image_processing_info["Category"].iloc[0]
if not image_processing_info.empty
else "Unknown"
)
st.write("Category:", image_processing_category)
# Retrieve the source code link for the image processing
image_processing_source_code = (
image_processing_info["Source Code Link"].iloc[0]
if not image_processing_info.empty
else "www.google.com"
)
# Set up columns for displaying source code button and custom settings checkbox
source_code_col, custo_setting_col = st.columns(2)
source_code_col.link_button("Source Code", image_processing_source_code)
# Toggle for custom settings
custom_settings = custo_setting_col.checkbox(
f"Customize {image_processing}", key=f"toggle_{image_processing}"
)
with image_col:
# Create two columns
col1, col2 = st.columns(2)
original_image_placeholder = col1.container(height=200, border=False)
processed_image_placeholder = col2.container(height=200, border=False)
# Apply custom settings
if custom_settings:
# Retrieve parameters for the image processing
params_df = image_processing_params_df[
image_processing_params_df["Name"] == image_processing
]
# Process parameters for each image processing technique and store in a dictionary
image_processings_params[image_processing] = utils.process_image_parameters(
params_df, image_processing
)
else:
# Use default settings if customization is not selected
image_processings_params[image_processing] = utils.get_default_params(
image_processing
)
# Check for errors in the selected parameters by applying them to a sample image
(
error_flag,
processed_first_image,
) = image_processing_functions.apply_and_test_image_processing(
image_processing,
image_processings_params[image_processing],
uploaded_first_image,
input_mapping_dict[image_processing],
)
# If there is an error in the parameters, set the global error flag
if error_flag:
error_in_parameters = True
else:
# If no error, display the original and processed images side by side
# Display the original and processed images in their respective placeholders
with original_image_placeholder:
st.image(
uploaded_first_image,
caption="Original Image",
use_column_width=True,
clamp=True,
)
with processed_image_placeholder:
st.image(
processed_first_image,
caption="Processed Image",
use_column_width=True,
clamp=True,
)
# Update the base image with the previously processed image output
uploaded_first_image = processed_first_image
#######################################################################################################
# Display selected image processing technique parameters as DataFrame
#######################################################################################################
# Check if any image processings have been defined
if (image_processings_params.keys()) and (not error_in_parameters):
# Create a dropdown for selecting an image processing technique or 'All'
selected_image_processing = st.selectbox(
"Select image processing technique",
options=["All"] + list(image_processings_params.keys()),
)
else:
selected_image_processing = None
# Create the DataFrame from the accumulated data
image_processings_df = image_processing_functions.create_image_processings_dataframe(
image_processings_params, image_processing_params_df
)
image_processings_df["Value"] = image_processings_df["Value"].astype(
str
) # Ensure consistent data types and handle potential serialization issues
# Filter the DataFrame based on the selected image processing
if selected_image_processing != "All":
filtered_image_processings_df = image_processings_df[
image_processings_df["image_processing"] == selected_image_processing
]
else:
filtered_image_processings_df = image_processings_df
# Check if the filtered dataframe is not empty and the selected configurations are valid
if (not filtered_image_processings_df.empty) and (not error_in_parameters):
# Display the DataFrame in Streamlit and use the full width of the container
st.dataframe(filtered_image_processings_df, use_container_width=False)
# Display code and description
code_placeholder = st.empty()
#######################################################################################################
# Process images and download processed images
#######################################################################################################
# Proceed if inputs are valid, techniques selected, and no errors in configurations
if (
st.session_state["is_valid"]
and (len(selected_image_processings) > 0)
and not error_in_parameters
):
# Create two columns
col1, col2 = st.columns(2)
# Allow user to specify the number of variations to be generated
num_variations = col1.number_input(
"Set the number of variations to be generated",
min_value=1,
max_value=3,
step=1,
)
# Checkbox to include original images and labels in the output
with col2:
for top_padding in range(2): # Top padding
st.write("")
include_original = st.checkbox(
"Include original images and labels in output", value=False
)
# Display code and download once all inputs are available
with code_placeholder:
# Generate the code with the function
if len(st.session_state["label_files"]) == 0:
generated_code = utils.generate_python_code_images(
image_processings_params,
num_variations,
include_original,
)
else:
generated_code = (
image_processing_functions.generate_python_code_images_labels(
image_processings_params,
num_variations,
include_original,
)
)
# Display the generated Python code with a description and provide a download button in the Streamlit app
image_processing_functions.display_code_and_download_button(generated_code)
# Create two columns
col1, col2 = st.columns(2)
# Add a button for the user to confirm their selections and proceed with processing
if col1.button("Accept and Process", use_container_width=True):
# Call the function and store the results
image_processing_functions.process_images_and_labels(
st.session_state["image_files"],
st.session_state["label_files"],
selected_image_processings,
image_processings_params,
num_variations,
include_original,
)
# Download button
col2.download_button(
label="Download",
data=st.session_state["zip_data_processing"],
file_name="processed_images.zip",
mime="application/zip",
use_container_width=True,
disabled=False,
)
else:
if (len(selected_image_processings) == 0) and st.session_state["is_valid"]:
# Inform the user that no image processing techniques have been selected
st.warning("Please select at least one image processing technique.", icon="⚠️")
if error_in_parameters and st.session_state["is_valid"]:
# Inform the user that there are errors in parameters
st.warning(
"There are errors in the image processing parameters. Please review your selections.",
icon="⚠️",
)
#######################################################################################################
# Display original and processed images
#######################################################################################################
if (
"image_repository_preprocessing" in st.session_state
and "processed_image_mapping_procesing" in st.session_state
):
# Number of unique images
num_unique_images = len(st.session_state["unique_images_names"])
if num_unique_images > 1:
# Create a slider to select an image index from the processed image mapping
selected_image_index = st.slider(
"Select an Image",
min_value=1,
max_value=num_unique_images, # Set the maximum to the number of unique images
step=1,
)
else:
selected_image_index = 1
# Retrieve the name of the selected original image using the slider index
selected_original_image_name = st.session_state["unique_images_names"][
selected_image_index - 1
]
# Retrieve the names of all processed variants for the selected original image
processed_variant_names = st.session_state["processed_image_mapping_procesing"].get(
selected_original_image_name, []
)
# Combine the original image name with its processed variants
all_image_names = [selected_original_image_name] + processed_variant_names
# Number of images and columns
num_images = len(all_image_names)
num_columns = 4
# Display images in a grid of 4 columns and dynamic number of rows
for i in range(0, num_images, num_columns):
# Create a row of columns
cols = st.columns(num_columns)
for j in range(num_columns):
# Calculate the current image index
image_index = i + j
if image_index < num_images:
# Get the image name and data from the repository
image_name = all_image_names[image_index]
image_data = st.session_state["image_repository_preprocessing"][
image_name
]["image"]
# Display the image in the respective column with caption
with cols[j]:
st.image(
image_data,
clamp=True,
caption=image_name,
use_column_width=True,
)
# if st.button("Run"):
# utils.button_click(on_click=None)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612# Set the page config
import streamlit as st
st.set_page_config(
page_title="Image Augmentation",
page_icon=":open_file_folder:",
layout="wide",
initial_sidebar_state="collapsed",
)
# Importing necessary libraries
import cv2
import utils
import numpy as np
import Functions.image_augmentation_functions as augmentation_functions
# Load augmentation technique parameters and details from an Excel file
augmentation_params_df = utils.load_data_from_excel(
"packages_db.xlsx", "augmentation_parameters"
)
augmentation_details_df = utils.load_data_from_excel(
"packages_db.xlsx", "augmentation_details"
)
# Display the page title
st.title("Image Augmentation")
# # Clear the Streamlit session state on the first load of the page
# utils.clear_session_state_on_first_load("image_augmentation_clear")
# List of session state keys to initialize if they are not already present
session_state_keys = [
"file_uploader_key_augmentation",
"select_processing_technique_key_augmentation",
"selected_option_key_augmentation",
"class_labels_input_key_augmentation",
"bbox1_key",
"bbox2_key",
"bbox3_key",
"bbox4_key",
"bbox5_key",
]
# Iterate through each session state key
for key in session_state_keys:
# Check if the key is not already in the session state
if key not in st.session_state:
# Initialize the key with a dictionary containing itself set to True
st.session_state[key] = {key: True}
# Initialize session state variables if not present
if "validation_triggered" not in st.session_state:
st.session_state["validation_triggered"] = False
if "uploaded_files_cache_augmentation" not in st.session_state:
st.session_state["uploaded_files_cache_augmentation"] = False
if "zip_data_augmentation" not in st.session_state:
st.session_state["zip_data_augmentation"] = ""
# Interface for uploading an images and labels
utils.display_file_uploader(
"uploaded_files",
"Choose images and labels...",
st.session_state["file_uploader_key_augmentation"],
st.session_state["uploaded_files_cache_augmentation"],
)
# Dropdown for selecting label type
label_type = st.selectbox(
"Choose the label type for your augmentation process:",
["Masks", "Bboxes"],
index=1,
on_change=utils.reset_validation_trigger,
key=st.session_state["selected_option_key_augmentation"],
)
# Choosing parameters based on the label type selected by the user
if label_type == "Bboxes":
# If the selected label type is Bboxes, call the bbox_params function
label_input_parameters = augmentation_functions.bbox_params()
elif label_type == "Masks":
# If the selected label type is Masks
label_input_parameters = None
# Text area for user to input class labels
class_labels_input = st.text_area(
"Enter class labels, separated by commas:",
utils.sample_class_labels,
on_change=utils.reset_validation_trigger,
key=st.session_state["class_labels_input_key_augmentation"],
) # Example default values
class_labels_input = (
class_labels_input.strip()
) # Remove unecessary space form start and end
# Generating a dictionary mapping class IDs to their respective labels
try:
class_labels = [
label.strip() for label in class_labels_input.split(",") if label.strip()
]
class_dict = {
i + 1: label for i, label in enumerate(class_labels)
} # Shifting class labels (keys) by 1, since 0 is reserved for the background
# Invert the class_dict to map class names to class IDs
class_names_to_ids = {v: k for k, v in class_dict.items()}
colors = augmentation_functions.generate_unique_colors(class_dict.keys())
except Exception as e:
st.warning(
"Invalid format for class labels. Please enter labels separated by commas.",
icon="⚠️",
)
class_dict, class_names_to_ids = (
{},
{},
) # Keeping class_dict and class_names_to_ids as an empty
# Note to users
st.markdown(
"""
<div style='text-align: justify;'>
<b>Note to Users:</b>
<ul>
<li>The <i>first uploaded image</i> will be used for demonstration purposes and to validate parameters for augmentation techniques.</li>
<li>Uploading <i>labels is optional</i>. If no labels are uploaded, the output will consist solely of processed images.</li>
<li>When moving to another page or if you wish to upload a new set of images and labels, don't forget to hit the <b>Reset</b> button. This helps in faster computation and frees up unused memory, ensuring smoother operation.</li>
<li>Select the class labels, label type and label parameters before uploading large data for faster computation and more efficient processing.</li>
</ul>
</div>
""",
unsafe_allow_html=True,
)
# List of session state variables to initialize
session_vars = [
"is_valid",
"image_files",
"label_files",
"first_image_file",
"first_label_file",
]
# Initialize each variable as None if it doesn't exist in the session state
for var in session_vars:
if var not in st.session_state:
st.session_state[var] = None
# Create two columns
col1, col2 = st.columns(2)
# Button to trigger validation
if (
col1.button("Validate Input", use_container_width=True)
and not st.session_state["validation_triggered"]
):
st.session_state["validation_triggered"] = True
st.session_state["uploaded_files_cache_augmentation"] = True
(
st.session_state["is_valid"],
st.session_state["image_files"],
st.session_state["label_files"],
st.session_state["first_image_file"],
st.session_state["first_label_file"],
) = augmentation_functions.check_valid_labels(
st.session_state["uploaded_files"], label_type, class_dict
)
elif st.session_state["validation_triggered"]:
pass
else:
st.session_state["is_valid"] = False
st.warning(
"Please upload images and labels and click **Validate Input**.", icon="⚠️"
)
with col2:
# Check if the 'Reset' button is pressed
if st.button("Reset", use_container_width=True):
# Toggle the keys for file uploader and processing technique to reset their states
current_value = st.session_state["file_uploader_key_augmentation"][
"file_uploader_key_augmentation"
]
updated_value = not current_value # Invert the current value
# Iterate through each session state key
for session_state_key in session_state_keys:
# Update each key in the session state with the toggled value
st.session_state[session_state_key] = {session_state_key: updated_value}
# Clear all other session state keys except for widget_state_keys
for key in list(st.session_state.keys()):
if key not in session_state_keys:
del st.session_state[key]
# Clear global variables except for protected and Streamlit module
global_vars = list(globals().keys())
vars_to_delete = [
var for var in global_vars if not var.startswith("_") and var != "st"
]
for var in vars_to_delete:
del globals()[var]
# Clear the Streamlit caches
st.cache_resource.clear()
st.cache_data.clear()
# Rerun the app to reflect the reset state
st.rerun()
# Fetching the names of techniques applicable to the selected option
available_augmentations = augmentation_functions.get_applicable_techniques(
augmentation_details_df, label_type
)
# Mapping each image processing techniques to its corresponding image types
input_mapping_dict = utils.technique_image_input_mapping(
available_augmentations, augmentation_details_df
)
# Present the option to select augmentation techniques only if the uploaded files are validated successfully
if st.session_state["is_valid"]:
selected_augmentations = st.multiselect(
"Select augmentation technique(s)",
available_augmentations,
key=st.session_state["select_processing_technique_key_augmentation"],
)
# Read the first uploaded image into a NumPy array
st.session_state["first_image_file"].seek(0) # Reset file pointer to start
file_bytes_first_image = np.frombuffer(
st.session_state["first_image_file"].read(), dtype=np.uint8
)
uploaded_first_image = cv2.cvtColor(
cv2.imdecode(file_bytes_first_image, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB
)
# # Resize the image
# uploaded_first_image = cv2.resize(uploaded_first_image, (256, 256))
else:
# Reset selected techniques to empty if input validation fails
selected_augmentations = []
#######################################################################################################
# Build custom augmentation pipeline
#######################################################################################################
# Store parameters for each selected augmentation technique
augmentations_params = {}
# Initialize a flag to track if any error exists
error_in_parameters = False
# Loop through each selected augmentation techniques to set up parameters
for augmentation in selected_augmentations:
with st.expander(f"{augmentation}"):
# Retrieve augmentation details from the database
augmentation_info = augmentation_details_df[
augmentation_details_df["Name"] == augmentation
]
# Set up columns for displaying details and image placeholders
details_col, image_col = st.columns([7, 3])
with details_col:
# Display the description for the augmentation technique
augmentation_description = (
augmentation_info["Description"].iloc[0]
if not augmentation_info.empty
else "No description available."
)
st.markdown(
f"<div style='text-align: justify;'><b>Description:</b> {augmentation_description}</div>",
unsafe_allow_html=True,
)
# Display the category for the augmentation
augmentation_category = (
augmentation_info["Category"].iloc[0]
if not augmentation_info.empty
else "Unknown"
)
st.write("Category:", augmentation_category)
# Retrieve the source code link for the augmentation
augmentation_source_code = (
augmentation_info["Source Code Link"].iloc[0]
if not augmentation_info.empty
else "www.google.com"
)
# Set up columns for displaying source code button and custom settings checkbox
source_code_col, custo_setting_col = st.columns(2)
source_code_col.link_button("Source Code", augmentation_source_code)
# Toggle for custom settings
custom_settings = custo_setting_col.checkbox(
f"Customize {augmentation}", key=f"toggle_{augmentation}"
)
with image_col:
# Create two columns
col1, col2 = st.columns(2)
original_image_placeholder = col1.container(height=150, border=False)
processed_image_placeholder = col2.container(height=150, border=False)
# Apply custom settings
if custom_settings:
# Retrieve parameters for the augmentation
params_df = augmentation_params_df[
augmentation_params_df["Name"] == augmentation
]
# Process parameters for each augmentation technique and store in a dictionary
augmentations_params[augmentation] = utils.process_image_parameters(
params_df, augmentation
)
else:
# Use default settings if customization is not selected
augmentations_params[augmentation] = utils.get_default_params(augmentation)
# Check for errors in the selected parameters by applying them to a sample image
(
error_flag,
processed_first_image,
) = augmentation_functions.apply_and_test_augmentation(
augmentation,
augmentations_params[augmentation],
uploaded_first_image,
st.session_state["first_label_file"],
label_type,
label_input_parameters,
input_mapping_dict[augmentation],
)
# If there is an error in the parameters, set the global error flag
if error_flag:
error_in_parameters = True
else:
# If no error, display the original and processed images side by side
# Display the original and processed images in their respective placeholders
with original_image_placeholder:
st.image(
uploaded_first_image,
caption="Original Image",
use_column_width=True,
clamp=True,
)
with processed_image_placeholder:
st.image(
processed_first_image,
caption="Processed Image",
use_column_width=True,
clamp=True,
)
# Update the base image with the previously processed image output
uploaded_first_image = processed_first_image
#######################################################################################################
# Display selected augmentation technique parameters as DataFrame
#######################################################################################################
# Check if any augmentations have been defined
if (augmentations_params.keys()) and (not error_in_parameters):
# Create a dropdown for selecting an augmentation technique or 'All'
selected_augmentation = st.selectbox(
"Select augmentation technique",
options=["All"] + list(augmentations_params.keys()),
)
else:
selected_augmentation = None
# Create the DataFrame from the accumulated data
augmentations_df = augmentation_functions.create_augmentations_dataframe(
augmentations_params, augmentation_params_df
)
augmentations_df["Value"] = augmentations_df["Value"].astype(
str
) # Ensure consistent data types and handle potential serialization issues
# Filter the DataFrame based on the selected augmentation
if selected_augmentation != "All":
filtered_augmentations_df = augmentations_df[
augmentations_df["augmentation"] == selected_augmentation
]
else:
filtered_augmentations_df = augmentations_df
# Check if the filtered dataframe is not empty and the selected configurations are valid
if (not filtered_augmentations_df.empty) and (not error_in_parameters):
# Display the DataFrame in Streamlit and use the full width of the container
st.dataframe(filtered_augmentations_df, use_container_width=False)
# Display code and description
code_placeholder = st.empty()
#######################################################################################################
# Process images and download processed images
#######################################################################################################
# Proceed if inputs are valid, techniques selected, and no errors in configurations
if (
st.session_state["is_valid"]
and (len(selected_augmentations) > 0)
and not error_in_parameters
):
# Create two columns
col1, col2 = st.columns(2)
# Allow user to specify the number of variations to be generated
num_variations = col1.number_input(
"Set the number of variations to be generated",
min_value=1,
max_value=3,
step=1,
)
# Checkbox to include original images and labels in the output
with col2:
for top_padding in range(2): # Top padding
st.write("")
include_original = st.checkbox(
"Include original images and labels in output", value=False
)
# Display code and download once all inputs are available
with code_placeholder:
# Generate the code with the function
if len(st.session_state["label_files"]) == 0:
generated_code = utils.generate_python_code_images(
augmentations_params,
num_variations,
include_original,
)
elif label_type == "Bboxes": # Selected label type is Bboxes
generated_code = augmentation_functions.generate_python_code_bboxes(
augmentations_params,
label_input_parameters,
num_variations,
include_original,
)
elif label_type == "Masks": # Selected label type is Bboxes
generated_code = augmentation_functions.generate_python_code_masks(
augmentations_params,
label_input_parameters,
num_variations,
include_original,
)
# Display the generated Python code with a description and provide a download button in the Streamlit app
augmentation_functions.display_code_and_download_button(generated_code)
# Create two columns
col1, col2 = st.columns(2)
# Add a button for the user to confirm their selections and proceed with processing
if col1.button("Accept and Process", use_container_width=True):
# Call the function and store the results
augmentation_functions.process_images_and_labels(
st.session_state["image_files"],
st.session_state["label_files"],
selected_augmentations,
augmentations_params,
label_type,
label_input_parameters,
num_variations,
include_original,
class_dict,
)
# Download button
col2.download_button(
label="Download",
data=st.session_state["zip_data_augmentation"],
file_name="augmented_images.zip",
mime="application/zip",
use_container_width=True,
disabled=False,
)
else:
if (len(selected_augmentations) == 0) and st.session_state["is_valid"]:
# Inform the user that no augmentation techniques have been selected
st.warning("Please select at least one augmentation technique.", icon="⚠️")
if error_in_parameters and st.session_state["is_valid"]:
# Inform the user that there are errors in parameters
st.warning(
"There are errors in the augmentation parameters. Please review your selections.",
icon="⚠️",
)
#######################################################################################################
# Display original and processed images
#######################################################################################################
# Check if image_repository and processed_image_mapping exist in session_state
if (
"image_repository_augmentation" in st.session_state
and "processed_image_mapping_augmentation" in st.session_state
):
# Number of unique images
num_unique_images = len(st.session_state["unique_images_names"])
if num_unique_images > 1:
# Create a slider to select an image index from the processed image mapping
selected_image_index = st.slider(
"Select an Image",
min_value=1,
max_value=num_unique_images, # Set the maximum to the number of unique images
step=1,
)
else:
selected_image_index = 1
# Retrieve the name of the selected original image using the slider index
selected_original_image_name = st.session_state["unique_images_names"][
selected_image_index - 1
]
# Retrieve the names of all processed variants for the selected original image
processed_variant_names = st.session_state[
"processed_image_mapping_augmentation"
].get(selected_original_image_name, [])
# Combine the original image name with its processed variants
all_image_names = [selected_original_image_name] + processed_variant_names
if len(st.session_state["label_files"]) > 0:
# Options for displaying labels on the images
label_display_options = ["No Label", "All Labels", "Specific Labels"]
# Select box for the user to choose how labels should be displayed on the images
selected_label_display_option = st.selectbox(
"Choose how to display labels:",
label_display_options,
index=0, # Default option is 'No Label'
)
# If 'All Labels' option is selected, include all class IDs
if selected_label_display_option == "All Labels":
labels_to_plot = list(class_dict.keys())
# If 'Specific Labels' option is selected, allow user to select specific class IDs
elif selected_label_display_option == "Specific Labels":
selected_class_names = st.multiselect(
"Select specific labels to display",
list(class_names_to_ids.keys()),
class_dict[1],
)
labels_to_plot = [class_names_to_ids[name] for name in selected_class_names]
else:
selected_label_display_option = "No Label"
# Display images in a grid
num_images = len(all_image_names)
num_columns = 4
for i in range(0, num_images, num_columns):
cols = st.columns(num_columns)
for j in range(num_columns):
image_index = i + j
if image_index < num_images:
image_name = all_image_names[image_index]
image_data = st.session_state["image_repository_augmentation"][
image_name
]["image"]
label_file = st.session_state["image_repository_augmentation"][
image_name
]["label"]
# Overlay labels on the image based on the selected option
if selected_label_display_option in ["All Labels", "Specific Labels"]:
# Overlay labels if selected
modified_image = augmentation_functions.overlay_labels(
image=image_data.copy(),
labels_to_plot=labels_to_plot,
label_file=label_file,
label_type=label_type,
colors=colors,
class_dict=class_dict,
)
else:
# Use the original image without overlay if 'No Label' is selected
modified_image = image_data
# Display the image in the respective column with a caption
with cols[j]:
st.image(
modified_image,
clamp=True,
caption=image_name,
use_column_width=True,
)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364# Set the page config
import streamlit as st
st.set_page_config(
page_title="Model_Training",
page_icon=":open_file_folder:",
layout="wide",
initial_sidebar_state="collapsed",
)
# Importing necessary libraries
import utils
import streamlit as st
import Functions.model_training_functions as model_training_functions
# Display the page title
st.title("Model Training")
# # Clear the Streamlit session state on the first load of the page
# utils.clear_session_state_on_first_load("model_training_clear")
# List of session state keys to initialize if they are not already present
session_state_keys = [
"file_uploader_split_key_training",
"file_uploader_train_key_training",
"file_uploader_val_key_training",
"file_uploader_test_key_training",
"number_input_train_key",
"number_input_val_key",
"number_input_test_key",
"split_method_key",
"training_type_key",
"class_labels_input_key_training",
]
# Iterate through each session state key
for key in session_state_keys:
# Check if the key is not already in the session state
if key not in st.session_state:
# Initialize the key with a dictionary containing itself set to True
st.session_state[key] = {key: True}
# Initialize session state variables if not present
if "validation_triggered" not in st.session_state:
st.session_state["validation_triggered"] = False
if "uploaded_files_cache_processing" not in st.session_state:
st.session_state["uploaded_files_cache_processing"] = False
# Initialize session state variables if not present
if "is_valid" not in st.session_state:
st.session_state["is_valid"] = False
# Container for file uploaders
file_uploader_container = st.container()
# Dictionary for mapping the user-friendly terms to technical label types
label_type_mapping = {"Object Detection": "Bboxes", "Instance Segmentation": "Masks"}
# Create two columns for widgets
column_select_training, column_split_method = st.columns(2)
# Dropdown for selecting the training type
with column_select_training:
selected_training = st.selectbox(
"Select the training type:",
list(label_type_mapping.keys()),
index=0,
on_change=utils.reset_validation_trigger,
key=st.session_state["training_type_key"],
)
# Getting the corresponding label type
label_type = label_type_mapping[selected_training]
# Toggle for choosing the split method
with column_split_method:
split_method = st.radio(
"Select the dataset split method:",
["Percentage Split", "Direct Upload"],
horizontal=True,
on_change=utils.reset_validation_trigger,
key=st.session_state["split_method_key"],
)
# Text area for user to input class labels
class_labels_input = st.text_area(
"Enter class labels, separated by commas:",
utils.sample_class_labels,
on_change=utils.reset_validation_trigger,
key=st.session_state["class_labels_input_key_training"],
) # Example default values
class_labels_input = (
class_labels_input.strip()
) # Remove unecessary space form start and end
# Generating a dictionary mapping class IDs to their respective labels
try:
class_labels = [
label.strip() for label in class_labels_input.split(",") if label.strip()
]
class_dict = {i: label for i, label in enumerate(class_labels)}
# Invert the class_dict to map class names to class IDs
class_names_to_ids = {v: k for k, v in class_dict.items()}
except Exception as e:
st.warning(
"Invalid format for class labels. Please enter labels separated by commas.",
icon="⚠️",
)
class_dict, class_names_to_ids = (
{},
{},
) # Keeping class_dict and class_names_to_ids as an empty
# Note to users
st.markdown(
"""
<div style='text-align: justify;'>
<b>Note to Users:</b>
<ul>
<li>When moving to another page or if you wish to upload a new set of images and labels, don't forget to hit the <b>Reset</b> button. This helps in faster computation and frees up unused memory, ensuring smoother operation.</li>
<li>Select the training type, class labels, dataset split method and its parameters before uploading large data for faster computation and more efficient processing.</li>
</ul>
</div>
""",
unsafe_allow_html=True,
)
# Create two columns for input percentages
validate_button_col, reset_button_col = st.columns(2)
with reset_button_col:
# Check if the 'Reset' button is pressed
if st.button("Reset", use_container_width=True):
# Clear folders
model_training_functions.delete_and_recreate_folder(
model_training_functions.get_path("output")
)
model_training_functions.clear_data_folders()
# List of session state keys that need to be reset
session_state_keys = [
"file_uploader_split_key_training",
"file_uploader_train_key_training",
"file_uploader_val_key_training",
"file_uploader_test_key_training",
"number_input_train_key",
"number_input_val_key",
"number_input_test_key",
"split_method_key",
"training_type_key",
"class_labels_input_key_training",
]
# Iterate through each session state key
for session_state_key in session_state_keys:
# Toggle the keys to reset their states
current_value = st.session_state[session_state_key][session_state_key]
updated_value = not current_value # Invert the current value
# Update each key in the session state with the toggled value
st.session_state[session_state_key] = {session_state_key: updated_value}
# Clear all other session state keys except for widget_state_keys
for key in list(st.session_state.keys()):
if key not in session_state_keys:
del st.session_state[key]
# Clear global variables except for protected and Streamlit module
global_vars = list(globals().keys())
vars_to_delete = [
var for var in global_vars if not var.startswith("_") and var != "st"
]
for var in vars_to_delete:
del globals()[var]
# Clear the Streamlit caches
st.cache_resource.clear()
st.cache_data.clear()
# Rerun the app to reflect the reset state
st.rerun()
# Code for "Percentage Split" method
if split_method == "Percentage Split":
with file_uploader_container:
# User uploads images and labels
utils.display_file_uploader(
"uploaded_files",
"Choose images and labels...",
st.session_state["file_uploader_split_key_training"],
st.session_state["uploaded_files_cache_processing"],
)
# Create three columns for input percentages
col1, col2, col3 = st.columns(3)
# User specifies split percentages
train_pct = col1.number_input(
"Train Set Percentage",
0,
100,
70,
1,
on_change=utils.reset_validation_trigger,
key=st.session_state["number_input_train_key"],
)
test_pct = col2.number_input(
"Test Set Percentage",
0,
100,
15,
1,
on_change=utils.reset_validation_trigger,
key=st.session_state["number_input_val_key"],
)
val_pct = col3.number_input(
"Validation Set Percentage",
0,
100,
15,
1,
on_change=utils.reset_validation_trigger,
key=st.session_state["number_input_test_key"],
)
# Check if the total percentage equals 100%
pct_check = train_pct + test_pct + val_pct
# Validating the input percentages
pct_condition_check = (
pct_check == 100
and train_pct > 0
and val_pct > 0
and model_training_functions.check_min_images(
len(st.session_state["uploaded_files"]), train_pct, val_pct, test_pct
)
)
if not pct_condition_check:
file_uploader_container.warning(
"The percentages for train, test, and validation sets should add up to 100%, and train and validation set should not be empty.",
icon="⚠️",
)
# Button to trigger validation
if validate_button_col.button("Validate Input", use_container_width=True):
st.session_state["validation_triggered"] = True
st.session_state["is_valid"] = model_training_functions.check_valid_labels(
st.session_state["uploaded_files"], label_type, class_dict
)
if st.session_state["is_valid"]:
model_training_functions.create_yolo_config_file(
model_training_functions.get_path("config"),
class_labels,
)
model_training_functions.clear_data_folders()
paired_files = model_training_functions.pair_files(
st.session_state["uploaded_files"]
)
model_training_functions.split_and_save_files(
paired_files, train_pct, test_pct
)
# Process files if input is valid
if st.session_state["validation_triggered"] and (
pct_condition_check and st.session_state["is_valid"]
):
model_training_functions.start_yolo_training(selected_training, class_labels)
else:
# Display a warning message if the validation is not successful or conditions are not met
st.warning(
"Please upload valid input, select valid parameters, and click **Validate Input**.",
icon="⚠️",
)
# Code for "Direct Upload" method
elif split_method == "Direct Upload":
with file_uploader_container:
# Create three columns for uploading train, val, and test files
col1, col2, col3 = st.columns(3)
with col1:
utils.display_file_uploader(
"uploaded_train_files",
"Upload Training Images and Labels",
st.session_state["file_uploader_train_key_training"],
st.session_state["uploaded_files_cache_processing"],
)
with col2:
utils.display_file_uploader(
"uploaded_val_files",
"Upload Validation Images and Labels",
st.session_state["file_uploader_val_key_training"],
st.session_state["uploaded_files_cache_processing"],
)
with col3:
utils.display_file_uploader(
"uploaded_test_files",
"Upload Test Images and Labels",
st.session_state["file_uploader_test_key_training"],
st.session_state["uploaded_files_cache_processing"],
)
# Check for valid input
pct_condition_check = (
len(st.session_state["uploaded_train_files"]) > 0
and len(st.session_state["uploaded_val_files"]) > 0
)
if not pct_condition_check:
file_uploader_container.warning(
"The train and validation set should not be empty.",
icon="⚠️",
)
# Button to trigger validation
if validate_button_col.button("Validate Input", use_container_width=True):
st.session_state["validation_triggered"] = True
st.session_state["is_valid"] = model_training_functions.check_valid_labels(
st.session_state["uploaded_train_files"]
+ st.session_state["uploaded_val_files"]
+ st.session_state["uploaded_test_files"],
label_type,
class_dict,
)
if st.session_state["is_valid"]:
model_training_functions.create_yolo_config_file(
model_training_functions.get_path("config"),
class_labels,
)
model_training_functions.clear_data_folders()
model_training_functions.save_files_to_folder(
st.session_state["uploaded_train_files"], "train"
)
model_training_functions.save_files_to_folder(
st.session_state["uploaded_val_files"], "val"
)
# Only save test files if they are uploaded
if len(st.session_state["uploaded_test_files"]) > 0:
model_training_functions.save_files_to_folder(
st.session_state["uploaded_test_files"], "test"
)
# Process files if input is valid
if st.session_state["validation_triggered"] and (
pct_condition_check and st.session_state["is_valid"]
):
model_training_functions.start_yolo_training(selected_training, class_labels)
else:
# Display a warning message if the validation is not successful or conditions are not met
st.warning(
"Please upload valid input, select valid parameters, and click **Validate Input**.",
icon="⚠️",
)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446# Importing necessary libraries
import cv2
import utils
import inspect
import numpy as np
import pandas as pd
import streamlit as st
import albumentations as A
# Please retain all the imported libraries, even if they appear to be unused, as they are necessary for the input part of the code (dynamic_input function)
# Sample class labels
sample_class_labels = "person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop sign, parking meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra, giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard, sports ball, kite, baseball bat, baseball glove, skateboard, surfboard, tennis racket, bottle, wine glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli, carrot, hot dog, pizza, donut, cake, chair, couch, potted plant, bed, dining table, toilet, tv, laptop, mouse, remote, keyboard, cell phone, microwave, oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy bear, hair drier, toothbrush"
# Model info
models_info = {
"YOLOv8n": {
"size": 640,
"mAPval": 37.3,
"speed_cpu": 80.4,
"speed_gpu": 0.99,
"params": 3.2,
"flops": 8.7,
},
"YOLOv8s": {
"size": 640,
"mAPval": 44.9,
"speed_cpu": 128.4,
"speed_gpu": 1.20,
"params": 11.2,
"flops": 28.6,
},
"YOLOv8m": {
"size": 640,
"mAPval": 50.2,
"speed_cpu": 234.7,
"speed_gpu": 1.83,
"params": 25.9,
"flops": 78.9,
},
"YOLOv8l": {
"size": 640,
"mAPval": 52.9,
"speed_cpu": 375.2,
"speed_gpu": 2.39,
"params": 43.7,
"flops": 165.2,
},
"YOLOv8x": {
"size": 640,
"mAPval": 53.9,
"speed_cpu": 479.1,
"speed_gpu": 3.53,
"params": 68.2,
"flops": 257.8,
},
}
# Export formats info
export_formats = {
"TorchScript": {"format_argument": "torchscript", "arguments": ["optimize"]},
"ONNX": {
"format_argument": "onnx",
"arguments": ["half", "dynamic", "simplify", "opset"],
},
"OpenVINO": {"format_argument": "openvino", "arguments": ["half", "int8"]},
"TensorRT": {
"format_argument": "engine",
"arguments": ["half", "dynamic", "simplify", "workspace"],
},
"CoreML": {"format_argument": "coreml", "arguments": ["half", "int8", "nms"]},
"TF SavedModel": {"format_argument": "saved_model", "arguments": ["keras", "int8"]},
"TF GraphDef": {"format_argument": "pb", "arguments": []},
"TF Lite": {"format_argument": "tflite", "arguments": ["half", "int8"]},
"TF Edge TPU": {"format_argument": "edgetpu", "arguments": []},
"TF.js": {"format_argument": "tfjs", "arguments": ["half", "int8"]},
"PaddlePaddle": {"format_argument": "paddle", "arguments": []},
"ncnn": {"format_argument": "ncnn", "arguments": ["half"]},
}
# Function to add Top Padding
def top_padding(padding_aount=0):
for i in range(padding_aount):
st.write("")
# Function to load data from an Excel file
@st.cache_resource(show_spinner=False)
def load_data_from_excel(filename, sheet_name):
"""Load data from a specified sheet in an Excel file."""
return pd.read_excel(filename, sheet_name=sheet_name)
# Function to resets the validation trigger in the session state
def reset_validation_trigger():
st.session_state["validation_triggered"] = False
# Function to clear the Streamlit session state on the first load of the page
def clear_session_state_on_first_load(key):
# Check if the function has already been executed
if key not in st.session_state:
# Clear the session state
for k in list(st.session_state.keys()):
if k != key: # Preserve the key itself
del st.session_state[k]
# Mark this function as executed
st.session_state[key] = True
# Function to display a file uploader based on the cached state
def display_file_uploader(uploaded_files_type, label_value, key_value, is_cached):
if not is_cached:
st.session_state[uploaded_files_type] = st.file_uploader(
label=label_value,
type=["jpg", "png", "txt"],
accept_multiple_files=True,
on_change=utils.reset_validation_trigger,
key=key_value,
)
else:
st.file_uploader(
label_value,
type=["jpg", "png", "txt"],
accept_multiple_files=True,
disabled=True,
)
# Function to map each technique to its corresponding image types
@st.cache_resource(show_spinner=False)
def technique_image_input_mapping(available_techniques, details_df):
input_mapping_dict = {}
for technique in available_techniques:
# Filter the DataFrame for rows where the Name matches the technique
filtered_df = details_df[details_df["Name"] == technique]
# Extract the Image Types for these rows, split by comma, and strip spaces
image_types = (
filtered_df["Image Types"]
.apply(lambda x: [type.strip() for type in str(x).split(",")])
.tolist()
)
# Add to the dictionary
input_mapping_dict[technique] = image_types[0]
return input_mapping_dict
# Function to check if the image type match the allowed types
def is_image_type_allowed(input_image_type, num_channels, allowed_image_types):
for allowed_type in allowed_image_types:
# Handle general types like 'Any' or 'nan'
if allowed_type in ["Any", "nan", input_image_type]:
return True
# Handle specific multi-channel types (e.g., '3-channel uint8 images only')
if "channel" in allowed_type:
parts = allowed_type.split()
try:
allowed_channels = int(parts[0][0])
allowed_dtype = parts[1]
if (
num_channels == allowed_channels
and input_image_type == allowed_dtype
):
return True
except ValueError:
# Continue to the next allowed type
continue
# Add additional specific checks if needed
return False # If none of the conditions are met
# Function to creates a dynamic input widget based on the specified data type
def dynamic_input(
image_processing, data_type, label, input_key, input_data, default_val
):
if data_type == "bool":
# Create a checkbox for bool input
return st.checkbox(label=label, value=eval(default_val), key=input_key)
elif data_type == "int" or data_type == "float":
# Create a slider for integer / float input
return st.select_slider(
label=label,
options=eval(input_data),
value=eval(default_val),
key=input_key,
)
elif data_type == "tuple":
# Create a slider for tuple input
min_val, max_val = st.select_slider(
label=label,
options=eval(input_data),
value=(eval(default_val)[0], eval(default_val)[1]),
key=input_key,
)
return (min_val, max_val)
elif data_type == "single_select":
# Create a selectbox for single select input
return st.selectbox(
label=label,
options=eval(input_data),
index=eval(input_data).index(eval(default_val)),
)
elif data_type == "none":
# Return None without creating any input widget
return None
elif data_type == "code":
# Create a selectbox for code input
try:
# Evaluate the code entered by the user in the text input
input_code = eval(st.text_input(label=label, value=default_val))
except:
# If the user input is not valid Python code, catch the exception
st.warning(f"Invalid input for {label}. Using default value.", icon="⚠️")
# Return the default parameter value for this image processing technique
input_code = get_default_params(image_processing)[label]
# Show an example of the expected input format to the user
st.write(f"Sample Input for {label}:")
st.code(input_data, language="python", line_numbers=False)
return input_code
# Handle unsupported data types
else:
# Display an error message and return None for unsupported data types
st.error(f"Unsupported data type: {data_type}")
# Returns a dictionary of default parameters for a given Albumentations augmentation class
def get_default_params(augmentation_name):
# Retrieve the augmentation class from the Albumentations module
augmentation_class = getattr(A, augmentation_name, None)
# Inspect the constructor (__init__) of the augmentation class to get its parameters
params = inspect.signature(augmentation_class.__init__).parameters
# Create a dictionary of parameter names and their default values
default_params = {
name: param.default
for name, param in params.items()
if param.default is not inspect.Parameter.empty
}
return default_params
# Function to resize the image to a maximum dimension of 512 pixels while maintaining aspect ratio
def resize_image(image, max_size=512):
height, width = image.shape[:2]
# Calculate the ratio to maintain aspect ratio
ratio = min(max_size / height, max_size / width)
new_dimensions = (int(width * ratio), int(height * ratio))
resized_image = cv2.resize(image, new_dimensions, interpolation=cv2.INTER_AREA)
return resized_image
# Function to apply the specified transformation with optional custom parameters
def apply_albumentation(custom_params, albumentation_name):
# Get the Albumentations transformation class from the albumentation_name string
albumentation_class = getattr(A, albumentation_name, None)
# Check if the transformation class exists in the Albumentations library
if albumentation_class is not None:
# Apply the transformation with or without custom parameters
if custom_params is None:
return albumentation_class()
else:
return albumentation_class(**custom_params)
# Function to process image parameters from a DataFrame and create interactive UI
def process_image_parameters(params_df, image_processing):
# Dictionary to map UI input types to database values
type_mapping = {
"Boolean": "bool",
"Integer": "int",
"Float": "float",
"Tuple": "tuple",
"Single Select": "single_select",
"Code": "code",
"None": "none",
}
# Create a reverse mapping from database values to UI input types
reverse_type_mapping = {v: k for k, v in type_mapping.items()}
# Initialize a dictionary to store user-selected parameter values
image_processing_params = {}
if not params_df.empty:
for _, row in params_df.iterrows():
# Spacer
st.markdown("---")
param_name = row["Parameter Name"]
input_types = [
input_type.strip() for input_type in str(row["Input Type"]).split(",")
]
if len(input_types) > 1:
col1, col2 = st.columns([3, 7])
selected_type_ui = col1.selectbox(
"Choose input type",
[reverse_type_mapping[ip] for ip in input_types],
key=f"{image_processing}_{param_name}_selectbox",
)
# Map the selected UI input type to database value
selected_type = type_mapping[selected_type_ui]
selected_index = input_types.index(selected_type)
with col2:
user_input = utils.dynamic_input(
image_processing=image_processing,
data_type=selected_type,
label=param_name,
input_data=str(row[f"Input Values {selected_index+1}"]),
default_val=str(row[f"Default Value {selected_index+1}"]),
input_key=f"{image_processing}_{param_name}_{selected_type}",
)
else:
user_input = utils.dynamic_input(
image_processing=image_processing,
data_type=input_types[0],
label=param_name,
input_data=str(row[f"Input Values 1"]),
default_val=str(row[f"Default Value 1"]),
input_key=f"{image_processing}_{param_name}_{input_types[0]}",
)
st.markdown(
f"<div style='text-align: justify;'><b>Description:</b> {row['Parameter Description']}</div>",
unsafe_allow_html=True,
)
image_processing_params[param_name] = user_input
# Top-padding
top_padding(2)
return image_processing_params
# Function to generates a DataFrame detailing image processing technique parameters and descriptions
@st.cache_resource(show_spinner=False)
def create_image_processings_dataframe(
image_processings_params, image_processing_params_db
):
data = []
for aug_name, params in image_processings_params.items():
for param_name, param_value in params.items():
# Retrieve relevant image_processing information from the database
image_processing_info = image_processing_params_db[
image_processing_params_db["Name"] == aug_name
]
param_info = image_processing_info[
image_processing_info["Parameter Name"] == param_name
]
# Check if the parameter information exists in the database
if not param_info.empty:
# Get the description of the current parameter
param_description = param_info["Parameter Description"].iloc[0]
else:
param_description = "Description not available"
# Append image_processing name, parameter name, its value, and description to the data list
data.append([aug_name, param_name, param_value, param_description])
# Create the DataFrame from the accumulated data
image_processings_df = pd.DataFrame(
data, columns=["image_processing", "Parameter", "Value", "Description"]
)
return image_processings_df
def generate_python_code_images(
augmentations_params,
num_variations=1,
include_original=False,
):
# Start with necessary library imports
code_str = "# Importing necessary libraries\n"
code_str += "import os\nimport cv2\nimport shutil\nimport albumentations as A\n\n"
# Paths for input and output directories
code_str += "# Define the paths for input and output directories\n"
code_str += "input_directory = 'path/to/input'\n"
code_str += "output_directory = 'path/to/output'\n\n"
# Function to create an augmentation pipeline
code_str += "# Function to create an augmentation pipeline using Albumentations\n"
code_str += "def process_image(image):\n"
code_str += " # Define the sequence of augmentation techniques\n"
code_str += " pipeline = A.Compose([\n"
for technique, params in augmentations_params.items():
code_str += f" A.{technique}({', '.join(f'{k}={v}' for k, v in params.items())}),\n"
code_str += " ])\n"
code_str += " # Apply the augmentation pipeline\n"
code_str += " return pipeline(image=image)['image']\n\n"
# Function to process a batch of images
code_str += "# Function to process a batch of images\n"
code_str += "def process_batch(input_directory, output_directory, include_original=False, num_variations=1):\n"
code_str += " for filename in os.listdir(input_directory):\n"
code_str += " if filename.lower().endswith(('.png', '.jpg', '.jpeg')):\n"
code_str += " image_path = os.path.join(input_directory, filename)\n\n"
code_str += " # Read the image\n"
code_str += " image = cv2.imread(image_path)\n\n"
# Include original image
code_str += " # Include original image\n"
if include_original:
code_str += " shutil.copy2(image_path, output_directory)\n\n"
code_str += " # Generate variations for each image and process them\n"
code_str += f" for variation in range({num_variations}):\n"
code_str += " processed_image = process_image(image)\n\n"
code_str += " # Save the processed image\n"
code_str += " output_filename = f'processed_{os.path.splitext(filename)[0]}_{variation}{os.path.splitext(filename)[1]}'\n"
code_str += " cv2.imwrite(os.path.join(output_directory, output_filename), processed_image)\n\n"
# Execute the batch processing function
code_str += (
"# Execute the batch processing function with the specified parameters\n"
)
code_str += "process_batch(input_directory, output_directory)\n"
return code_str.streamlit/config.tomlAdded+9
Functions/__pycache__/image_augmentation_functions.cpython-311.pycAdded
Functions/__pycache__/image_processing_functions.cpython-311.pycAdded
Functions/__pycache__/model_training_functions.cpython-311.pycAdded
Functions/image_augmentation_functions.pyAdded+1027
Functions/image_processing_functions.pyAdded+430
Functions/model_training_functions.pyAdded+1896
README.md+12-14
Welcome.pyAdded+8
config.tomlAdded+9
model_data/input_files/config.yamlAdded+6
model_data/model_training_code_pipline/config.yamlAdded+6
model_data/model_training_code_pipline/model_training.pyAdded+69
model_data/models/yolov8l-seg.ptAdded+3
model_data/models/yolov8l.ptAdded+3
model_data/models/yolov8m-seg.ptAdded+3
model_data/models/yolov8m.ptAdded+3
model_data/models/yolov8n-seg.ptAdded+3
model_data/models/yolov8n.ptAdded+3
model_data/models/yolov8s-seg.ptAdded+3
model_data/models/yolov8s.ptAdded+3
model_data/models/yolov8x-seg.ptAdded+3
model_data/models/yolov8x.ptAdded+3
packages.txtAdded+1
packages_db.xlsxAdded
pages/2_Image_Processing.pyAdded+512
pages/3_Image_Augmentation.pyAdded+612
pages/4_Model_Training.pyAdded+364
requirements.txtAdded
utils.pyAdded+446