File size: 16,037 Bytes
c6baa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fa72db
c6baa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fa72db
c6baa40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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()