Anuj-Panthri commited on
Commit
fd0734b
·
1 Parent(s): 25184de

added set_seed function

Browse files
kaggle/train-image-colorization.ipynb CHANGED
@@ -1 +1 @@
1
- {"cells":[{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:35.423893Z","iopub.status.busy":"2024-05-07T05:57:35.422571Z","iopub.status.idle":"2024-05-07T05:57:37.744423Z","shell.execute_reply":"2024-05-07T05:57:37.743082Z","shell.execute_reply.started":"2024-05-07T05:57:35.423838Z"},"trusted":true},"outputs":[],"source":["!git clone https://github.com/AnujPanthri/Image-Colorization.git\n","%cd Image-Colorization\n","!git checkout train"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:38.983147Z","iopub.status.busy":"2024-05-07T05:57:38.982736Z","iopub.status.idle":"2024-05-07T05:57:38.989575Z","shell.execute_reply":"2024-05-07T05:57:38.987925Z","shell.execute_reply.started":"2024-05-07T05:57:38.983115Z"},"trusted":true},"outputs":[],"source":["# %cd ..\n","# !rm -r Image-Colorization"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:39.454828Z","iopub.status.busy":"2024-05-07T05:57:39.454444Z","iopub.status.idle":"2024-05-07T05:58:13.154377Z","shell.execute_reply":"2024-05-07T05:58:13.152964Z","shell.execute_reply.started":"2024-05-07T05:57:39.4548Z"},"trusted":true},"outputs":[],"source":["!pip install -r requirements.txt --quiet\n","!pip install -e ."]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:13.15798Z","iopub.status.busy":"2024-05-07T05:58:13.157513Z","iopub.status.idle":"2024-05-07T05:58:14.401676Z","shell.execute_reply":"2024-05-07T05:58:14.400055Z","shell.execute_reply.started":"2024-05-07T05:58:13.157935Z"},"trusted":true},"outputs":[],"source":["from kaggle_secrets import UserSecretsClient\n","user_secrets = UserSecretsClient()\n","COMET_API_KEY = user_secrets.get_secret(\"comet_api_key\")\n","\n","# !export COMET_API_KEY={COMET_API_KEY}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:14.412388Z","iopub.status.busy":"2024-05-07T05:58:14.412023Z","iopub.status.idle":"2024-05-07T05:58:14.422002Z","shell.execute_reply":"2024-05-07T05:58:14.420946Z","shell.execute_reply.started":"2024-05-07T05:58:14.41236Z"},"trusted":true},"outputs":[],"source":["import os\n","\n","os.environ[\"COMET_API_KEY\"]=COMET_API_KEY\n","config_file = \"configs/experiment1.yaml\""]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:14.423858Z","iopub.status.busy":"2024-05-07T05:58:14.423359Z","iopub.status.idle":"2024-05-07T05:58:20.695681Z","shell.execute_reply":"2024-05-07T05:58:20.694312Z","shell.execute_reply.started":"2024-05-07T05:58:14.423828Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/prepare_dataset.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:20.697948Z","iopub.status.busy":"2024-05-07T05:58:20.697592Z","iopub.status.idle":"2024-05-07T05:58:20.703728Z","shell.execute_reply":"2024-05-07T05:58:20.702527Z","shell.execute_reply.started":"2024-05-07T05:58:20.697915Z"},"trusted":true},"outputs":[],"source":["# !cat src/utils/data_utils.py"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:24.072758Z","iopub.status.busy":"2024-05-07T05:58:24.072343Z","iopub.status.idle":"2024-05-07T05:58:27.518713Z","shell.execute_reply":"2024-05-07T05:58:27.517194Z","shell.execute_reply.started":"2024-05-07T05:58:24.072725Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/visualize_dataset.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:41.523718Z","iopub.status.busy":"2024-05-07T05:58:41.523307Z","iopub.status.idle":"2024-05-07T06:03:03.088039Z","shell.execute_reply":"2024-05-07T06:03:03.086519Z","shell.execute_reply.started":"2024-05-07T05:58:41.523683Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/train.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T06:03:34.595185Z","iopub.status.busy":"2024-05-07T06:03:34.594725Z","iopub.status.idle":"2024-05-07T06:03:46.842693Z","shell.execute_reply":"2024-05-07T06:03:46.841521Z","shell.execute_reply.started":"2024-05-07T06:03:34.595147Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/visualize_results.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["%cd ..\n","\n","! cp -r Image-Colorization/artifacts .\n","! rm -r artifacts/model/\n","! rm -r Image-Colorization\n","! ls"]}],"metadata":{"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30698,"isGpuEnabled":false,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.12.1"}},"nbformat":4,"nbformat_minor":4}
 
1
+ {"cells":[{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:35.423893Z","iopub.status.busy":"2024-05-07T05:57:35.422571Z","iopub.status.idle":"2024-05-07T05:57:37.744423Z","shell.execute_reply":"2024-05-07T05:57:37.743082Z","shell.execute_reply.started":"2024-05-07T05:57:35.423838Z"},"trusted":true},"outputs":[],"source":["!git clone https://github.com/AnujPanthri/Image-Colorization.git\n","%cd Image-Colorization\n","!git checkout train"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:38.983147Z","iopub.status.busy":"2024-05-07T05:57:38.982736Z","iopub.status.idle":"2024-05-07T05:57:38.989575Z","shell.execute_reply":"2024-05-07T05:57:38.987925Z","shell.execute_reply.started":"2024-05-07T05:57:38.983115Z"},"trusted":true},"outputs":[],"source":["# %cd ..\n","# !rm -r Image-Colorization"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:57:39.454828Z","iopub.status.busy":"2024-05-07T05:57:39.454444Z","iopub.status.idle":"2024-05-07T05:58:13.154377Z","shell.execute_reply":"2024-05-07T05:58:13.152964Z","shell.execute_reply.started":"2024-05-07T05:57:39.4548Z"},"trusted":true},"outputs":[],"source":["!pip install -r requirements.txt --quiet\n","!pip install -e ."]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:13.15798Z","iopub.status.busy":"2024-05-07T05:58:13.157513Z","iopub.status.idle":"2024-05-07T05:58:14.401676Z","shell.execute_reply":"2024-05-07T05:58:14.400055Z","shell.execute_reply.started":"2024-05-07T05:58:13.157935Z"},"trusted":true},"outputs":[],"source":["from kaggle_secrets import UserSecretsClient\n","user_secrets = UserSecretsClient()\n","COMET_API_KEY = user_secrets.get_secret(\"comet_api_key\")\n","\n","# !export COMET_API_KEY={COMET_API_KEY}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:14.412388Z","iopub.status.busy":"2024-05-07T05:58:14.412023Z","iopub.status.idle":"2024-05-07T05:58:14.422002Z","shell.execute_reply":"2024-05-07T05:58:14.420946Z","shell.execute_reply.started":"2024-05-07T05:58:14.41236Z"},"trusted":true},"outputs":[],"source":["import os\n","\n","os.environ[\"COMET_API_KEY\"]=COMET_API_KEY\n","config_file = \"configs/experiment1.yaml\""]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:14.423858Z","iopub.status.busy":"2024-05-07T05:58:14.423359Z","iopub.status.idle":"2024-05-07T05:58:20.695681Z","shell.execute_reply":"2024-05-07T05:58:20.694312Z","shell.execute_reply.started":"2024-05-07T05:58:14.423828Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/prepare_dataset.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:20.697948Z","iopub.status.busy":"2024-05-07T05:58:20.697592Z","iopub.status.idle":"2024-05-07T05:58:20.703728Z","shell.execute_reply":"2024-05-07T05:58:20.702527Z","shell.execute_reply.started":"2024-05-07T05:58:20.697915Z"},"trusted":true},"outputs":[],"source":["# !cat src/utils/data_utils.py"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:24.072758Z","iopub.status.busy":"2024-05-07T05:58:24.072343Z","iopub.status.idle":"2024-05-07T05:58:27.518713Z","shell.execute_reply":"2024-05-07T05:58:27.517194Z","shell.execute_reply.started":"2024-05-07T05:58:24.072725Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/visualize_dataset.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T05:58:41.523718Z","iopub.status.busy":"2024-05-07T05:58:41.523307Z","iopub.status.idle":"2024-05-07T06:03:03.088039Z","shell.execute_reply":"2024-05-07T06:03:03.086519Z","shell.execute_reply.started":"2024-05-07T05:58:41.523683Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/train.py {config_file} --log"]},{"cell_type":"code","execution_count":null,"metadata":{"execution":{"iopub.execute_input":"2024-05-07T06:03:34.595185Z","iopub.status.busy":"2024-05-07T06:03:34.594725Z","iopub.status.idle":"2024-05-07T06:03:46.842693Z","shell.execute_reply":"2024-05-07T06:03:46.841521Z","shell.execute_reply.started":"2024-05-07T06:03:34.595147Z"},"trusted":true},"outputs":[],"source":["!python3 src/scripts/visualize_results.py {config_file}"]},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["%cd ..\n","\n","! cp -r Image-Colorization/artifacts .\n","! rm -r artifacts/model/\n","! rm -r Image-Colorization\n","! ls"]}],"metadata":{"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30698,"isGpuEnabled":false,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.12.1"}},"nbformat":4,"nbformat_minor":4}
src/scripts/train.py CHANGED
@@ -1,7 +1,7 @@
1
  import os,shutil
2
  import argparse
3
  from comet_ml import Experiment
4
- from src.utils.config_loader import Config,constants
5
  from src.utils import config_loader
6
  from src.utils.data_utils import print_title
7
  from src.utils.script_utils import validate_config
@@ -17,8 +17,10 @@ def train(args):
17
  # validate config
18
  validate_config(config)
19
 
20
- # set config globally
21
  config_loader.config = config
 
 
22
 
23
  # now load the model
24
  Model = importlib.import_module(f"src.{config.task}.model.models.{config.model}").Model
 
1
  import os,shutil
2
  import argparse
3
  from comet_ml import Experiment
4
+ from src.utils.config_loader import Config,constants,set_seed
5
  from src.utils import config_loader
6
  from src.utils.data_utils import print_title
7
  from src.utils.script_utils import validate_config
 
17
  # validate config
18
  validate_config(config)
19
 
20
+ # set config globally & set seed
21
  config_loader.config = config
22
+ set_seed(config.seed)
23
+
24
 
25
  # now load the model
26
  Model = importlib.import_module(f"src.{config.task}.model.models.{config.model}").Model
src/scripts/visualize_dataset.py CHANGED
@@ -1,8 +1,9 @@
1
  import argparse
2
- from src.utils.config_loader import Config
3
  from src.utils import config_loader
4
  from src.utils.script_utils import validate_config
5
  import importlib
 
6
 
7
 
8
  def visualize_dataset(args):
@@ -12,8 +13,9 @@ def visualize_dataset(args):
12
  # validate config
13
  validate_config(config)
14
 
15
- # set config globally
16
  config_loader.config = config
 
17
 
18
  # now visualize the dataset
19
  visualize_fn = importlib.import_module(f"src.{config.task}.data.visualize_dataset").visualize
 
1
  import argparse
2
+ from src.utils.config_loader import Config,set_seed
3
  from src.utils import config_loader
4
  from src.utils.script_utils import validate_config
5
  import importlib
6
+ import random
7
 
8
 
9
  def visualize_dataset(args):
 
13
  # validate config
14
  validate_config(config)
15
 
16
+ # set config globally and set seed
17
  config_loader.config = config
18
+ set_seed(config.seed)
19
 
20
  # now visualize the dataset
21
  visualize_fn = importlib.import_module(f"src.{config.task}.data.visualize_dataset").visualize
src/scripts/visualize_results.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import argparse
3
- from src.utils.config_loader import Config,constants
4
  from src.utils import config_loader
5
  from src.utils.script_utils import validate_config
6
  import importlib
@@ -13,8 +13,9 @@ def visualize_results(args):
13
  # validate config
14
  validate_config(config)
15
 
16
- # set config globally
17
  config_loader.config = config
 
18
 
19
  # now load model and visualize the results
20
  model_dir = constants.ARTIFACT_MODEL_DIR
 
1
  import os
2
  import argparse
3
+ from src.utils.config_loader import Config,constants,set_seed
4
  from src.utils import config_loader
5
  from src.utils.script_utils import validate_config
6
  import importlib
 
13
  # validate config
14
  validate_config(config)
15
 
16
+ # set config globally & set seed
17
  config_loader.config = config
18
+ set_seed(config.seed)
19
 
20
  # now load model and visualize the results
21
  model_dir = constants.ARTIFACT_MODEL_DIR
src/utils/config_loader.py CHANGED
@@ -1,5 +1,9 @@
1
  import yaml
2
  from pathlib import Path
 
 
 
 
3
 
4
  class Config:
5
  def __init__(self,config_file_path:str):
@@ -20,4 +24,18 @@ constants.config_dict['RAW_DATASET_DIR'] = Path(constants.config_dict['RAW_DATAS
20
  constants.config_dict['INTERIM_DATASET_DIR'] = Path(constants.config_dict['INTERIM_DATASET_DIR'])
21
  constants.config_dict['PROCESSED_DATASET_DIR'] = Path(constants.config_dict['PROCESSED_DATASET_DIR'])
22
 
23
- config = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import yaml
2
  from pathlib import Path
3
+ import random
4
+ import tensorflow as tf
5
+ import numpy as np
6
+ import os
7
 
8
  class Config:
9
  def __init__(self,config_file_path:str):
 
24
  constants.config_dict['INTERIM_DATASET_DIR'] = Path(constants.config_dict['INTERIM_DATASET_DIR'])
25
  constants.config_dict['PROCESSED_DATASET_DIR'] = Path(constants.config_dict['PROCESSED_DATASET_DIR'])
26
 
27
+ config = None
28
+
29
+
30
+ def set_seed(seed: int = 42) -> None:
31
+ random.seed(seed)
32
+ np.random.seed(seed)
33
+ tf.random.set_seed(seed)
34
+ tf.experimental.numpy.random.seed(seed)
35
+
36
+ # When running on the CuDNN backend, two further options must be set
37
+ os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
38
+ os.environ['TF_DETERMINISTIC_OPS'] = '1'
39
+ # Set a fixed value for the hash seed
40
+ os.environ["PYTHONHASHSEED"] = str(seed)
41
+ print(f"Random seed set as {seed}")