Skip to content

Commit

Permalink
Merge pull request #395 from Programmer-RD-AI/deepsource-fix-2581d75f
Browse files Browse the repository at this point in the history
Put docstring into a single line
  • Loading branch information
Programmer-RD-AI authored Aug 23, 2022
2 parents 186ba00 + 3693032 commit 7a8d27a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 201 deletions.
68 changes: 17 additions & 51 deletions .Other/V2/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@


class Model:
"""
This class helps anyone to train a detectron2 model for this project easily so anyone can train this model.
"""
"""This class helps anyone to train a detectron2 model for this project easily so anyone can train this model."""

def __init__(
self,
Expand Down Expand Up @@ -165,9 +163,7 @@ def __init__(

@staticmethod
def remove_files_in_output() -> None:
"""
- remove_files_in_output - remove all of the file in ./output/
"""
"""- remove_files_in_output - remove all of the file in ./output/"""
files_to_remove = os.listdir(
"./output/") # Get the files in the directory
try:
Expand Down Expand Up @@ -318,9 +314,7 @@ def __train(self, ) -> DefaultTrainer:
return trainer

def create_predictor(self) -> DefaultPredictor:
"""
- create_predictor - create the predictor to predict images
"""
"""- create_predictor - create the predictor to predict images"""
torch.cuda.empty_cache()
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = (
self.SCORE_THRESH_TEST) # Setting SCORE_THRESH_TEST
Expand Down Expand Up @@ -352,9 +346,7 @@ def create_coco_eval(self,

@staticmethod
def metrics_file_to_dict() -> list:
"""
- metrics_file_to_dict - in ./output/metrics.json it logs the metrics of the model
"""
"""- metrics_file_to_dict - in ./output/metrics.json it logs the metrics of the model"""
new_logs = []
try:
logs = open("./output/metrics.json", "r").read().split("\n")
Expand All @@ -371,9 +363,7 @@ def metrics_file_to_dict() -> list:
return new_logs

def predict_test_images(self, predictor: DefaultPredictor) -> list:
"""
- predict_test_images - predict test images
"""
"""- predict_test_images - predict test images"""
imgs = []
torch.cuda.empty_cache()
for img in tqdm(
Expand All @@ -393,9 +383,7 @@ def predict_test_images(self, predictor: DefaultPredictor) -> list:
return imgs

def create_target_and_preds(self, predictor: DefaultPredictor) -> tuple:
"""
- create_target_and_preds - create the target and predictions
"""
"""- create_target_and_preds - create the target and predictions"""
info = self.data.iloc[self.create_target_and_preds_iter]
img = cv2.imread("./Img/" + info["Path"])
height, width = cv2.imread("./Img/" + info["Path"]).shape[:2]
Expand Down Expand Up @@ -425,9 +413,7 @@ def create_target_and_preds(self, predictor: DefaultPredictor) -> tuple:

@staticmethod
def create_rmse(preds: torch.tensor, target: torch.tensor) -> float:
"""
- create_rmse - Create Root-mean-square deviation
"""
"""- create_rmse - Create Root-mean-square deviation"""
lowest_rmse = 0
r_mean_squared_error = MeanSquaredError(squared=False)
preds_new = (preds["instances"].__dict__["_fields"]
Expand Down Expand Up @@ -470,9 +456,7 @@ def create_iou(preds: torch.tensor, targets: torch.tensor) -> float:

@staticmethod
def create_mse(preds: torch.tensor, target: torch.tensor) -> float:
"""
- create_mse - Create Mean-square deviation
"""
"""- create_mse - Create Mean-square deviation"""
lowest_mse = 0
mean_squared_error = MeanSquaredError(squared=True)
preds_new = (preds["instances"].__dict__["_fields"]
Expand All @@ -485,9 +469,7 @@ def create_mse(preds: torch.tensor, target: torch.tensor) -> float:

@staticmethod
def create_x_y_w_h(xmin: int, ymin: int, xmax: int, ymax: int) -> list:
"""
- create_x_y_w_h - Conver xmin,ymin, xmax, ymax to x,y,w,h
"""
"""- create_x_y_w_h - Conver xmin,ymin, xmax, ymax to x,y,w,h"""
x = xmin
y = ymin
w = xmax - xmin
Expand All @@ -496,18 +478,14 @@ def create_x_y_w_h(xmin: int, ymin: int, xmax: int, ymax: int) -> list:

@staticmethod
def crop_img(x: int, y: int, w: int, h: int, img: np.array) -> np.array:
"""
- crop_img - cropping the image using x,y,w,h
"""
"""- crop_img - cropping the image using x,y,w,h"""
crop = img[y:y + h, x:x + w]
cv2.imwrite("./test.png", crop)
return crop

def create_ssim(self, preds: torch.tensor, target: torch.tensor,
height: int, width: int) -> float:
"""
- create_ssim - create SSIM # TODO it is not done yet
"""
"""- create_ssim - create SSIM # TODO it is not done yet"""
lowest_ssim = 0
ssim = SSIM()
preds_new = (preds["instances"].__dict__["_fields"]
Expand All @@ -529,9 +507,7 @@ def create_ssim(self, preds: torch.tensor, target: torch.tensor,

@staticmethod
def create_psnr(preds: torch.tensor, target: torch.tensor) -> float:
"""
- create_psnr - Peak signal-to-noise ratio (how similar is a image)
"""
"""- create_psnr - Peak signal-to-noise ratio (how similar is a image)"""
lowest_psnr = 0
psnr = PSNR()
preds_new = (preds["instances"].__dict__["_fields"]
Expand All @@ -544,9 +520,7 @@ def create_psnr(preds: torch.tensor, target: torch.tensor) -> float:

@staticmethod
def create_mae(preds: torch.tensor, target: torch.tensor) -> float:
"""
- create_mae - Mean absolute error
"""
"""- create_mae - Mean absolute error"""
lowest_mae = 0
mae = MeanAbsoluteError()
preds_new = (preds["instances"].__dict__["_fields"]
Expand Down Expand Up @@ -619,9 +593,7 @@ def evaluation(self, predictor):
}

def train(self) -> dict:
"""
- train - trains the model
"""
"""- train - trains the model"""
torch.cuda.empty_cache()
wandb.init(
project=PROJECT_NAME,
Expand Down Expand Up @@ -711,9 +683,7 @@ def __init__(self, ) -> None:

@staticmethod
def tune(params: dict) -> dict:
"""
Tune all of the parameters
"""
"""Tune all of the parameters"""
final_metrics = []
model = Model()
params = ParameterGrid(params)
Expand Down Expand Up @@ -742,9 +712,7 @@ def tune(params: dict) -> dict:

@staticmethod
def ray_tune_func(config):
"""
https://docs.ray.io/en/latest/tune/index.html
"""
"""https://docs.ray.io/en/latest/tune/index.html"""
base_lr = config["BASE_LR"]
ims_per_batch = (config["IMS_PER_BATCH"], )
batch_size_per_image = config["BATCH_SIZE_PER_IMAGE"]
Expand All @@ -761,9 +729,7 @@ def ray_tune_func(config):
tune.report(average_precisions=ap)

def ray_tune(self):
"""
https://docs.ray.io/en/latest/tune/user-guide.html
"""
"""https://docs.ray.io/en/latest/tune/user-guide.html"""
analysis = tune.run(self.ray_tune_func,
config=params,
resources_per_trial={
Expand Down
36 changes: 9 additions & 27 deletions .Other/V2/Model/models/detectron2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class Model:
"""
This class helps anyone to train a detectron2 model for this project easily so anyone can train this model.
"""
"""This class helps anyone to train a detectron2 model for this project easily so anyone can train this model."""

def __init__(
self,
Expand Down Expand Up @@ -106,9 +104,7 @@ def __init__(

@staticmethod
def remove_files_in_output() -> None:
"""
- remove_files_in_output - remove all of the file in ./output/
"""
"""- remove_files_in_output - remove all of the file in ./output/"""
files_to_remove = os.listdir(
"./output/") # Get the files in the directory
try:
Expand Down Expand Up @@ -259,9 +255,7 @@ def __train(self, ) -> DefaultTrainer:
return trainer

def create_predictor(self) -> DefaultPredictor:
"""
- create_predictor - create the predictor to predict images
"""
"""- create_predictor - create the predictor to predict images"""
torch.cuda.empty_cache()
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = (
self.SCORE_THRESH_TEST) # Setting SCORE_THRESH_TEST
Expand Down Expand Up @@ -293,9 +287,7 @@ def create_coco_eval(self,

@staticmethod
def metrics_file_to_dict() -> list:
"""
- metrics_file_to_dict - in ./output/metrics.json it logs the metrics of the model
"""
"""- metrics_file_to_dict - in ./output/metrics.json it logs the metrics of the model"""
new_logs = []
try:
logs = open("./output/metrics.json", "r").read().split("\n")
Expand All @@ -312,9 +304,7 @@ def metrics_file_to_dict() -> list:
return new_logs

def predict_test_images(self, predictor: DefaultPredictor) -> list:
"""
- predict_test_images - predict test images
"""
"""- predict_test_images - predict test images"""
imgs = []
torch.cuda.empty_cache()
for img in tqdm(
Expand All @@ -334,9 +324,7 @@ def predict_test_images(self, predictor: DefaultPredictor) -> list:
return imgs

def create_target_and_preds(self, predictor: DefaultPredictor) -> tuple:
"""
- create_target_and_preds - create the target and predictions
"""
"""- create_target_and_preds - create the target and predictions"""
info = self.data.iloc[self.create_target_and_preds_iter]
img = cv2.imread("./Img/" + info["Path"])
height, width = cv2.imread("./Img/" + info["Path"]).shape[:2]
Expand Down Expand Up @@ -365,9 +353,7 @@ def create_target_and_preds(self, predictor: DefaultPredictor) -> tuple:

@staticmethod
def create_x_y_w_h(xmin: int, ymin: int, xmax: int, ymax: int) -> list:
"""
- create_x_y_w_h - Conver xmin,ymin, xmax, ymax to x,y,w,h
"""
"""- create_x_y_w_h - Conver xmin,ymin, xmax, ymax to x,y,w,h"""
x = xmin
y = ymin
w = xmax - xmin
Expand All @@ -376,9 +362,7 @@ def create_x_y_w_h(xmin: int, ymin: int, xmax: int, ymax: int) -> list:

@staticmethod
def crop_img(x: int, y: int, w: int, h: int, img: np.array) -> np.array:
"""
- crop_img - cropping the image using x,y,w,h
"""
"""- crop_img - cropping the image using x,y,w,h"""
crop = img[y:y + h, x:x + w]
cv2.imwrite("./test.png", crop)
return crop
Expand Down Expand Up @@ -421,9 +405,7 @@ def evaluation(self, predictor):
}

def train(self) -> dict:
"""
- train - trains the model
"""
"""- train - trains the model"""
torch.cuda.empty_cache()
wandb.init(
project=PROJECT_NAME,
Expand Down
12 changes: 3 additions & 9 deletions .Other/V2/Model/param_tunning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ def __init__(self, ) -> None:

@staticmethod
def tune(params: dict) -> dict:
"""
Tune all of the parameters
"""
"""Tune all of the parameters"""
final_metrics = []
model = Model()
params = ParameterGrid(params)
Expand Down Expand Up @@ -39,9 +37,7 @@ def tune(params: dict) -> dict:

@staticmethod
def ray_tune_func(config):
"""
https://docs.ray.io/en/latest/tune/index.html
"""
"""https://docs.ray.io/en/latest/tune/index.html"""
base_lr = config["BASE_LR"]
ims_per_batch = (config["IMS_PER_BATCH"], )
batch_size_per_image = config["BATCH_SIZE_PER_IMAGE"]
Expand All @@ -58,9 +54,7 @@ def ray_tune_func(config):
tune.report(average_precisions=ap)

def ray_tune(self):
"""
https://docs.ray.io/en/latest/tune/user-guide.html
"""
"""https://docs.ray.io/en/latest/tune/user-guide.html"""
analysis = tune.run(self.ray_tune_func,
config=params,
resources_per_trial={
Expand Down
28 changes: 7 additions & 21 deletions .history/ML/Model/modelling/detectron2_20220429065528.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@


class Detectron2:
"""
This class helps anyone to train a detectron2 model for this project easily so anyone can train this model.
"""
"""This class helps anyone to train a detectron2 model for this project easily so anyone can train this model."""

def __init__(
self,
Expand Down Expand Up @@ -138,9 +136,7 @@ def __init__(

@staticmethod
def remove_files_in_output() -> None:
"""
- remove_files_in_output - remove all of the file in ./output/
"""
"""- remove_files_in_output - remove all of the file in ./output/"""
files_to_remove = os.listdir(
"/media/indika/Sync/Programmer-RD-AI/Programming/Projects/Python/Rest-Api/Car-Object-Detection-REST-API/Find-Card/ML/Model/output/"
) # Get the files in the directory
Expand Down Expand Up @@ -313,9 +309,7 @@ def __train(self, ) -> DefaultTrainer:
return trainer

def create_predictor(self) -> DefaultPredictor:
"""
- create_predictor - create the predictor to predict images
"""
"""- create_predictor - create the predictor to predict images"""
torch.cuda.empty_cache()
self.cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = (
self.SCORE_THRESH_TEST) # Setting SCORE_THRESH_TEST
Expand Down Expand Up @@ -375,9 +369,7 @@ def create_coco_eval_detectron2(self,

@staticmethod
def metrics_file_to_dict_detectron2() -> list:
"""
- metrics_file_to_dict_detectron2 - in ./output/metrics.json it logs the metrics of the model
"""
"""- metrics_file_to_dict_detectron2 - in ./output/metrics.json it logs the metrics of the model"""
new_logs = []
try:
logs = open("./Model/output/metrics.json", "r").read().split("\n")
Expand All @@ -395,9 +387,7 @@ def metrics_file_to_dict_detectron2() -> list:

def predict_test_images_detectron2(self,
predictor: DefaultPredictor) -> list:
"""
- predict_test_images_detectron2 - predict test images
"""
"""- predict_test_images_detectron2 - predict test images"""
imgs = []
torch.cuda.empty_cache()
for img in tqdm(
Expand Down Expand Up @@ -431,9 +421,7 @@ def predict_test_images_detectron2(self,

def create_target_and_preds_detectron2(
self, predictor: DefaultPredictor) -> tuple:
"""
- create_target_and_preds_detectron2 - create the target and predictions
"""
"""- create_target_and_preds_detectron2 - create the target and predictions"""
info = self.data.iloc[self.create_target_and_preds_iter]
img = cv2.imread(
"/media/indika/Sync/Programmer-RD-AI/Programming/Projects/Python/Rest-Api/Car-Object-Detection-REST-API/Find-Card/ML/Model/dataset/Img/"
Expand Down Expand Up @@ -467,9 +455,7 @@ def create_target_and_preds_detectron2(
width)

def train(self, PROJECT_NAME, param) -> dict:
"""
- train - trains the model
"""
"""- train - trains the model"""
torch.cuda.empty_cache()
wandb.init(
project=PROJECT_NAME,
Expand Down
Loading

0 comments on commit 7a8d27a

Please sign in to comment.