{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import torch\nimport subprocess\nimport argparse\nimport threading\nimport time","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-03T12:28:03.994158Z","iopub.execute_input":"2023-05-03T12:28:03.994676Z","iopub.status.idle":"2023-05-03T12:28:08.009016Z","shell.execute_reply.started":"2023-05-03T12:28:03.994637Z","shell.execute_reply":"2023-05-03T12:28:08.008201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INFERENCE_TIME_CAP = 120 # 120 sec","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:08.010907Z","iopub.execute_input":"2023-05-03T12:28:08.011759Z","iopub.status.idle":"2023-05-03T12:28:08.018237Z","shell.execute_reply.started":"2023-05-03T12:28:08.011726Z","shell.execute_reply":"2023-05-03T12:28:08.017249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"args_train = {\n    'root_dir': '/kaggle/input/budgeted-model-inference-iccv-2023-rcv-workshop/umnist_iccv_1024/umnist_iccv_1024',\n    'epochs': 100,\n    'train_batch_size': 6,\n    'script_location': '/kaggle/usr/lib/starter_inference_script/starter_inference_script.py',\n    'time': 8.8*60*60\n       }\nargs_test = {\n    'root_dir': '/kaggle/input/budgeted-model-inference-iccv-2023-rcv-workshop/umnist_iccv_1024/umnist_iccv_1024',\n    'epochs': 0,\n    'train_batch_size': 6,\n    'model_load_dir': './models',\n    'script_location': '/kaggle/usr/lib/starter_inference_script/starter_inference_script.py',\n    'time': INFERENCE_TIME_CAP\n       }","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:08.026557Z","iopub.execute_input":"2023-05-03T12:28:08.027404Z","iopub.status.idle":"2023-05-03T12:28:08.035159Z","shell.execute_reply.started":"2023-05-03T12:28:08.027371Z","shell.execute_reply":"2023-05-03T12:28:08.034093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def main_function(args):\n    root_dir = args['root_dir']\n    epochs = args['epochs']\n    script_location = args['script_location']\n    train_batch_size = args['train_batch_size']\n    model_load_dir = None\n    if 'model_load_dir' in args.keys():\n        model_load_dir = args['model_load_dir']\n    # Call caller.py with optional arguments using subprocess.Popen\n    if model_load_dir is None:\n        caller_process = subprocess.Popen(\n            [\"python\",script_location, '--root_dir', root_dir,'--train_batch_size', str(train_batch_size),  \"--epochs\", str(epochs)])\n    else:\n        caller_process = subprocess.Popen(\n            [\"python\",script_location, '--root_dir', root_dir, '--train_batch_size', str(train_batch_size),'--model_load_dir',model_load_dir, \"--epochs\", str(epochs)])\n    # Start a timer to terminate the subprocess after a certain time \n    exit_timer = threading.Timer(\n        args['time'], terminate_subprocess, args=[caller_process])\n    exit_timer.start()\n\n    # Wait for the subprocess to complete\n    caller_process.wait()","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:08.037739Z","iopub.execute_input":"2023-05-03T12:28:08.038339Z","iopub.status.idle":"2023-05-03T12:28:08.055038Z","shell.execute_reply.started":"2023-05-03T12:28:08.038304Z","shell.execute_reply":"2023-05-03T12:28:08.052787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def terminate_subprocess(process):\n    print(\"Terminating subprocess...\")\n    process.terminate()","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:08.057214Z","iopub.execute_input":"2023-05-03T12:28:08.058301Z","iopub.status.idle":"2023-05-03T12:28:08.066636Z","shell.execute_reply.started":"2023-05-03T12:28:08.058244Z","shell.execute_reply":"2023-05-03T12:28:08.064257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_function(args_train)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:08.068695Z","iopub.execute_input":"2023-05-03T12:28:08.069590Z","iopub.status.idle":"2023-05-03T12:28:35.677954Z","shell.execute_reply.started":"2023-05-03T12:28:08.069552Z","shell.execute_reply":"2023-05-03T12:28:35.675340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_function(args_test)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T12:28:35.681448Z","iopub.status.idle":"2023-05-03T12:28:35.684423Z","shell.execute_reply.started":"2023-05-03T12:28:35.684172Z","shell.execute_reply":"2023-05-03T12:28:35.684194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}