import gradio as gr import sys shared_theme = gr.themes.Base() sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/Interface_Dependencies') sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/Interface') sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface/yolov7-main') sys.path.append('Engineering-Clinic-Emerging-AI-Design-Interface') sys.path.append('Interface_Dependencies') sys.path.append('Interface') from run_methods_train import run_all_files, interface_login #from train import interface_login, train def build_train_interface(): with gr.Blocks(theme=shared_theme) as demo: gr.Markdown( """ # Training Interface for YOLOv7 (Work in progress...) Train your own YOLOv7 model! """) with gr.Row() as file_settings: # Allows choice for uploading training files or using default official files [official files included in github] file_type = gr.Radio(label="File Type",info="Choose 'Default' if you are using default values uploaded by server, Choose 'Upload' if you are uploading your own files from your computer", choices=['Default','Upload'],value='Default',interactive=True,visible=True) # Allows choice of source and device type source_type = gr.Radio(label="Source Type",info="'Computer' is set by default", choices=['Computer'],value='Computer',interactive=True,visible=True) dev_type = gr.Radio(label="Device Type",info="Choose 'CPU' if you have no gpu, Choose 'Cuda' if you have a cuda enabled gpu", choices=['CPU','Cuda'],value='CPU',interactive=True,visible=True) #loggers with gr.Accordion("Logger Options") as login_accordion: use_logger = gr.Checkbox(label="Use Logger",info="Check this box if you want to use a logger",visible=True,interactive=True,value=True) logger_type = gr.Radio(choices=['WANDB', 'ClearML', 'Tensorboard'],value='WANDB',interactive=True,visible=True, label="Logger",info="Choose which logger to use") key = gr.Textbox(label="WANDB Key", placeholder="Input your WANDB project key here...", interactive=True) placeholder = gr.Textbox(label="NULL",placeholder="Not implemented yet, skips loggers process", interactive=False) response = gr.Textbox(label="NULL",placeholder="Skips loggers process", interactive=False) login_but = gr.Button(value="Login") clear_but = gr.ClearButton(value="Clear key", components=[key], interactive=True,visible=True) #login_but.click(fn=interface_login,inputs=[logger_type, key],outputs=[]) #Input parameters with gr.Row("Inputs") as inputs: is_finetune = gr.Checkbox(label="Finetune",info="Check this box if you want to finetune a model", interactive=True) custom_pretrained_file = gr.File(label='Weights File',file_count='single', type='file', visible=True,interactive=True) pretrained_official = gr.File(label='Official Weight File',type='file',file_count='single',file_types=["pt"],value="weights/yolov7.pt", visible=True,interactive=True) custom_dataset_file = gr.File(label="Custom Dataset",file_count='single',type='file', file_types=['.zip'],visible=True,interactive=True) dataset_official = gr.File(label="Official dataset", type='file',file_count='single',file_types=["zip"], value="coco128.zip", visible=True,interactive=True) custom_data_file = gr.File(label="Custom Data file",file_count='single',type='file', file_types=['.yaml'],visible=True,interactive=True) datafile_official = gr.File(label="Official data file", type='file',file_count='single',file_types=["yaml"], value="yolov7-main/data/coco128.yaml", visible=True,interactive=True) custom_hyp_file = gr.File(label="Custom hyp file",file_count='single',type='file', file_types=['.yaml'],visible=True,interactive=True) hypfile_official = gr.File(label="Official hyp file", type='file',file_count='single',file_types=["yaml"], value="yolov7-main/data/hyp.scratch.p6.yaml", visible=True,interactive=True) #training settings with gr.Accordion("""Model settings""") as model_settings: inf_size = gr.Number(label='Inference Size (pixels)',value=640,precision=0, interactive=True) pgt_lr = gr.Number(label='Learning rate for plausibility gradient',value=0.02, interactive=True) pgt_lr_decay = gr.Number(label='decay learning rate for plausibility gradient',value=0.54, interactive=True) agnostic_nms = gr.Checkbox(label='Agnostic NMS',value=True, interactive=True) pgt_lr_decay_step = gr.Number(label='Step for decay learning rate',value=1.0, interactive=True) epochs = gr.Number(label='# of training loops',value=5, interactive=True) conf_thres = gr.Number(label='object confidence threshold',value=0.45, interactive=True) batch_size = gr.Number(label='total batch size for GPU',value=10, interactive=True) #device type settings with gr.Accordion("Device Options") as device_settings: device = gr.Radio(choices=['0', '1', '2','3'],value='Cuda',interactive=True,visible=True, label="Cuda",info="Choose cuda device number") default_device = gr.Radio(value='CPU',interactive=True,visible=True, label="CPU",info="defaults to CPU if no cuda device is present") #run timer with gr.Row("Timer") as outputs: formatted_time = gr.Textbox(label = 'Time to Run in Seconds:', value = "") #output file with gr.Row("Outputs") as outputs: output_weight_file = gr.File(type='file',label="Output weight file", interactive=False,visible=True) clear_comp_list_1 = [output_weight_file, formatted_time, is_finetune, custom_pretrained_file, custom_dataset_file, custom_data_file, custom_hyp_file] clear_comp_list_2 = [output_weight_file, formatted_time, is_finetune, custom_pretrained_file, pretrained_official, custom_dataset_file, dataset_official, custom_data_file, datafile_official, custom_hyp_file, hypfile_official] # Row for start & clear buttons with gr.Row() as buttons: start_but = gr.Button(label="Start") clear_but_1= gr.ClearButton(value='Clear All', components=clear_comp_list_1, interactive=True,visible=True) clear_but_2= gr.ClearButton(value='Clear All', components=clear_comp_list_2, interactive=True,visible=True) def change_Loggers_type(logger): """ Changes the visible components of the gradio interface Args: CPU (bool): If default defice is CPU CUDA (bool): If default device is cuda Returns: Dictionary: Each component of the interface that needs to be updated. """ if logger == "WANDB": return {key: gr.Textbox(visible=True), placeholder: gr.Textbox(visible=False), response: gr.Textbox(visible=False) } elif logger == "ClearML": return {key: gr.Textbox(visible=False), placeholder: gr.Textbox(visible=True), response: gr.Textbox(visible=False) } elif logger == "Tensorboard": return {key: gr.Textbox(visible=False), placeholder: gr.Textbox(visible=True), response: gr.Textbox(visible=False) } def change_device_type(dev): """ Changes the visible components of the gradio interface Args: CPU (bool): If default defice is CPU CUDA (bool): If default device is cuda Returns: Dictionary: Each component of the interface that needs to be updated. """ if dev == "CPU": return {default_device: gr.Checkbox(visible=True), device: gr.Slider(visible=False)} elif dev == "Cuda": return {default_device: gr.Checkbox(visible=False), device: gr.Slider(visible=True)} def change_file_type(file, source): """ Changes the visible components of the gradio interface Args: file (str): Type of the file (uploaded or default) source (str): If the file is uploaded from computer Returns: Dictionary: Each component of the interface that needs to be updated. """ if file == "Default": return { output_weight_file: gr.File(visible=True), formatted_time: gr.Textbox(visible=True), is_finetune: gr.File(visible=True), custom_pretrained_file: gr.File(label='Weights File',file_count='single', type='file',file_types=["pt"], visible=False, interactive=True), pretrained_official: gr.Dropdown(visible=True), custom_dataset_file: gr.File(label="Custom Dataset",file_count='single',type='file', file_types=['.zip'], visible=False,interactive=True), dataset_official: gr.Dropdown(visible=True), custom_data_file: gr.File(label="Custom Data file",file_count='single',type='file', file_types=['.yaml'], visible=False, interactive=True), datafile_official: gr.Dropdown(visible=True), custom_hyp_file: gr.File(label="Custom hyp file",file_count='single',type='file', file_types=['.yaml'], visible=False, interactive=True), hypfile_official: gr.Dropdown(visible=True), formatted_time: gr.Textbox(visible=True), clear_but_1: gr.ClearButton(visible=True), clear_but_2: gr.ClearButton(visible=False) } elif file == "Upload": if source == "Computer": return { output_weight_file: gr.File(visible=True), formatted_time: gr.Textbox(visible=True), is_finetune: gr.File(visible=True), custom_pretrained_file: gr.File(label='Weights File',file_count='single', type='file',file_types=["pt"], visible=True, interactive=True), pretrained_official: gr.Dropdown(visible=False), custom_dataset_file: gr.File(label="Custom Dataset",file_count='single',type='file', file_types=['.zip'], visible=True,interactive=True), dataset_official: gr.Dropdown(visible=False), custom_data_file: gr.File(label="Custom Data file",file_count='single',type='file', file_types=['.yaml'], visible=True,interactive=True), datafile_official: gr.Dropdown(visible=False), custom_hyp_file: gr.File(label="Custom hyp file",file_count='single',type='file', file_types=['.yaml'], visible=True,interactive=True), hypfile_official: gr.Dropdown(visible=False), formatted_time: gr.Textbox(visible=True), clear_but_1: gr.ClearButton(visible=False), clear_but_2: gr.ClearButton(visible=True) } change_comp_list = [output_weight_file, formatted_time, is_finetune, custom_pretrained_file, pretrained_official, custom_dataset_file, dataset_official, clear_but_1, clear_but_2, custom_data_file, datafile_official, custom_hyp_file, hypfile_official, device, default_device, key, placeholder, response] # List of gradio components that are input into the run_all method (when start button is clicked) run_inputs = [pgt_lr, pgt_lr_decay, pgt_lr_decay_step, conf_thres, epochs, conf_thres, batch_size, default_device, device, inf_size, agnostic_nms, custom_pretrained_file] # List of gradio components that are output from the run_all method (when start button is clicked) run_outputs = [output_weight_file, formatted_time] # When these settings are changed, the change_file_type method is called file_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type], outputs=change_comp_list) source_type.input(change_file_type, show_progress=True, inputs=[file_type, source_type], outputs=change_comp_list) dev_type.input(change_device_type, show_progress=True, inputs=[dev_type], outputs=change_comp_list) logger_type.input(change_Loggers_type, show_progress=True, inputs=[logger_type], outputs=change_comp_list) # When start button is clicked, the run_all method is called start_but.click(run_all_files, inputs=run_inputs, outputs=run_outputs) login_but.click(fn=interface_login,inputs=[logger_type, key],outputs=[]) # When the demo is first started, run the change_file_type method to ensure default settings demo.load(change_device_type, show_progress=True, inputs=[dev_type], outputs=change_comp_list) demo.load(change_Loggers_type, show_progress=True, inputs=[logger_type], outputs=change_comp_list) demo.load(change_file_type, show_progress=True, inputs=[file_type, source_type], outputs=change_comp_list) return demo if __name__== "__main__" : demo = build_train_interface()