Skip to content

Commit

Permalink
Merge pull request #4 from Atashnezhad/Atashnezhad-patch-1
Browse files Browse the repository at this point in the history
Update bit_vision.py
  • Loading branch information
Atashnezhad committed Jun 24, 2023
2 parents 23ef848 + 153f1cd commit fc92ff0
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 113 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ __pycache__/
/dataset_augmented/
/dataset_train_test_val/
/deep_model/
/tests/augmented_dataset/
/tests/dataset/
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ from neural_network_model.bit_vision import BitVision
if __name__ == "__main__":
# download the images
obj = Preprocessing(dataset_address=Path(__file__).parent / "dataset")
obj.download_images(limit=25)
obj.download_images(limit=10)
print(obj.image_dict)
obj.augment_data(
number_of_images_tobe_gen=100,
number_of_images_tobe_gen=10,
augment_data_address=Path(__file__).parent / "augmented_dataset"
)
obj.train_test_split(
Expand All @@ -99,29 +99,33 @@ if __name__ == "__main__":
print(obj.data_details)
obj.plot_image_category()
obj.compile_model()

#
model_name = "model_epoch_{epoch:02d}_loss_{loss:.2f}_acc_{accuracy:.2f}_val_acc_{val_accuracy:.2f}_.h5"
obj.train_model(
epochs=8,
model_save_address=Path(__file__).parent / "deep_model",
model_name=model_name,
epochs=40,
model_name=model_name
)
obj.plot_history(fig_folder_address=Path(__file__).parent / "figures")

best_model = obj.return_best_model_name(directory="deep_model")

obj.predict(
fig_save_address=Path(__file__).parent / "figures",
model_path=Path(__file__).parent / "deep_model" / model_name,
model_path=Path(__file__).parent / "deep_model" / best_model,
test_folder_address=Path(__file__).parent / "dataset_train_test_val" / "test"
)

# find list of images in the Path(__file__).parent / "dataset_train_test_val" / "test" / "pdc_bit"
directory_path = Path(__file__).parent / "dataset_train_test_val" / "test" / "pdc_bit"
list_of_images = [str(x) for x in directory_path.glob("*.jpeg")]

obj.grad_cam_viz(
model_path=Path(__file__).parent / "deep_model" / model_name,
model_path=Path(__file__).parent / "deep_model" / best_model,
fig_to_save_address=Path(__file__).parent / "figures",
img_to_be_applied_path=Path(__file__).parent / "dataset_train_test_val" / "test" / "pdc_bit" / list_of_images[0],
output_gradcam_fig_name="gradcam.png"
)



Binary file modified figures/history.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/prediction_pdc_bit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/prediction_rollercone_bit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figures/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 24 additions & 24 deletions neural_network_model/bit_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def _calculate_number_from_dict(self, my_dict: dict) -> int:
return total

def train_model(
self, model_save_address: str = SETTING.MODEL_SETTING.MODEL_PATH, **kwargs
self, model_save_address: Path = SETTING.MODEL_SETTING.MODEL_PATH, **kwargs
) -> None:
"""
This function is used to train the model.
Expand Down Expand Up @@ -281,10 +281,10 @@ def train_model(
callbacks=[self._check_points(model_save_address, model_name)],
)

self.model.save(
model_save_address / model_name
or SETTING.MODEL_SETTING.MODEL_PATH / SETTING.MODEL_SETTING.MODEL_NAME
)
# self.model.save(
# model_save_address / model_name
# or SETTING.MODEL_SETTING.MODEL_PATH / SETTING.MODEL_SETTING.MODEL_NAME
# )
logger.info(f"Model saved to {SETTING.MODEL_SETTING.MODEL_PATH}")

def plot_history(self, *args, **kwargs):
Expand Down Expand Up @@ -351,11 +351,11 @@ def predict(self, *args, **kwargs):
if model_path is None:
logger.info(f"model_path from SETTING is was used - {model_path}")

test_folder_dir = kwargs.get(
"test_folder_dir", SETTING.DATA_ADDRESS_SETTING.TEST_DIR_ADDRESS
)
if test_folder_dir is None:
raise ValueError("test_folder_address is None")
# test_folder_dir = kwargs.get(
# "test_folder_dir", SETTING.DATA_ADDRESS_SETTING.TEST_DIR_ADDRESS
# )
# if test_folder_dir is None:
# raise ValueError("test_folder_address is None")

model = keras.models.load_model(model_path)
logger.info(f"Model loaded from {model_path}")
Expand All @@ -365,20 +365,20 @@ def predict(self, *args, **kwargs):
number_of_cols = SETTING.FIGURE_SETTING.NUM_COLS_IN_PRED_MODEL
number_of_rows = SETTING.FIGURE_SETTING.NUM_ROWS_IN_PRED_MODEL
number_of_test_to_pred = SETTING.MODEL_SETTING.NUMBER_OF_TEST_TO_PRED
if test_folder_dir:
train_test_val_dir = (
test_folder_dir
or SETTING.PREPROCESSING_SETTING.TRAIN_TEST_VAL_SPLIT_DIR_ADDRESS
)
else:
train_test_val_dir = (
self.train_test_val_dir
or SETTING.PREPROCESSING_SETTING.TRAIN_TEST_VAL_SPLIT_DIR_ADDRESS
)
# if test_folder_dir:
# train_test_val_dir = (
# test_folder_dir
# or SETTING.PREPROCESSING_SETTING.TRAIN_TEST_VAL_SPLIT_DIR_ADDRESS
# )
# else:
# train_test_val_dir = (
# self.train_test_val_dir
# or SETTING.PREPROCESSING_SETTING.TRAIN_TEST_VAL_SPLIT_DIR_ADDRESS
# )

# get the list of test images
test_images_list = os.listdir(
train_test_val_dir
self.train_test_val_dir
/ SETTING.PREPROCESSING_SETTING.TRAIN_TEST_SPLIT_DIR_NAMES[1]
/ category
)
Expand All @@ -387,7 +387,7 @@ def predict(self, *args, **kwargs):

for i, img in enumerate(test_images_list[0:number_of_test_to_pred]):
path_to_img = (
train_test_val_dir
self.train_test_val_dir
/ SETTING.PREPROCESSING_SETTING.TRAIN_TEST_SPLIT_DIR_NAMES[1]
/ category
/ str(img)
Expand Down Expand Up @@ -432,7 +432,7 @@ def predict(self, *args, **kwargs):

datagen = image.ImageDataGenerator(SETTING.DATA_GEN_SETTING.RESCALE)
DoubleCheck_generator = datagen.flow_from_directory(
directory=test_folder_dir / "test",
directory=self.train_test_val_dir / "test",
target_size=SETTING.FLOW_FROM_DIRECTORY_SETTING.TARGET_SIZE,
color_mode=SETTING.FLOW_FROM_DIRECTORY_SETTING.COLOR_MODE,
classes=None,
Expand Down Expand Up @@ -603,7 +603,7 @@ def _save_and_display_gradcam(

@staticmethod
def return_best_model_name(
directory: str = SETTING.MODEL_SETTING.MODEL_PATH,
directory: Path = SETTING.MODEL_SETTING.MODEL_PATH,
) -> str:
"""
Return the best model name
Expand Down
58 changes: 58 additions & 0 deletions neural_network_model/script_run_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from neural_network_model.bit_vision import BitVision
from neural_network_model.process_data import Preprocessing
from pathlib import Path


def main():

parent_dir = Path(__file__).resolve().parent.parent
# download the images
obj = Preprocessing(dataset_address=parent_dir / "dataset")
obj.download_images(limit=8)
print(obj.image_dict)
obj.augment_data(
number_of_images_tobe_gen=10,
augment_data_address=parent_dir / "dataset_augmented",
)
obj.train_test_split(
augmented_data_address=parent_dir / "dataset_augmented",
train_test_val_split_dir_address=parent_dir / "dataset_train_test_val"
)

obj = BitVision(train_test_val_dir=parent_dir / "dataset_train_test_val")
print(obj.categories)
print(obj.data_details)
obj.plot_image_category()
obj.compile_model()
#
model_name = "model_epoch_{epoch:02d}_loss_{loss:.2f}_acc_{accuracy:.2f}_val_acc_{val_accuracy:.2f}_.h5"
obj.train_model(
epochs=8,
model_save_address=parent_dir / "deep_model",
model_name=model_name
)
obj.plot_history(fig_folder_address=parent_dir / "figures")

best_model = obj.return_best_model_name(directory=parent_dir / "deep_model")

obj.predict(
fig_save_address=parent_dir / "figures",
model_path=parent_dir / "deep_model" / best_model,
test_folder_address=parent_dir / "dataset_train_test_val" / "test"
)

# find list of images in the parent_dir / "dataset_train_test_val" / "test" / "pdc_bit"
directory_path = parent_dir / "dataset_train_test_val" / "test" / "pdc_bit"
list_of_images = [str(x) for x in directory_path.glob("*.jpeg")]

obj.grad_cam_viz(
model_path=parent_dir / "deep_model" / best_model,
fig_to_save_address=parent_dir / "figures",
img_to_be_applied_path=parent_dir / "dataset_train_test_val" / "test" / "pdc_bit" / list_of_images[
0],
output_gradcam_fig_name="test.png"
)


if __name__ == "__main__":
main()
86 changes: 4 additions & 82 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import argparse
import os
from pathlib import Path
import sys
Expand All @@ -9,86 +8,9 @@
main_dir = current_dir.parent
# Add the main directory to the Python path
sys.path.append(str(main_dir))
# skip this test TODO: fix this test later
import pytest

from neural_network_model.bit_vision import BitVision
from neural_network_model.process_data import Preprocessing


@pytest.mark.skip(reason="no way of currently testing this")
def test_run():
# check if a dir name test_resources exists if not create one
if not os.path.exists(Path(__file__).parent / "test_resources"):
os.mkdir(Path(__file__).parent / "test_resources")

# download the images
obj = Preprocessing(
dataset_address=Path(__file__).parent / "test_resources" / "dataset"
)
obj.download_images(limit=10)
print(obj.image_dict)
obj.augment_data(
number_of_images_tobe_gen=10,
augment_data_address=Path(__file__).parent
/ "test_resources"
/ "augmented_dataset",
)
obj.train_test_split(
augmented_data_address=Path(__file__).parent
/ "test_resources"
/ "augmented_dataset",
train_test_val_split_dir_address=Path(__file__).parent
/ "test_resources"
/ "dataset_train_test_val",
)

obj = BitVision(
train_test_val_dir=Path(__file__).parent
/ "test_resources"
/ "dataset_train_test_val"
)
print(obj.categories)
print(obj.data_details)
obj.plot_image_category()
obj.compile_model()

model_name = "model_epoch_{epoch:02d}_loss_{loss:.2f}_acc_{accuracy:.2f}_val_acc_{val_accuracy:.2f}_.h5"
obj.train_model(
model_save_address=Path(__file__).parent / "test_resources" / "deep_model",
model_name=model_name,
epochs=10,
)
obj.plot_history(
fig_folder_address=Path(__file__).parent / "test_resources" / "figures"
)

obj.predict(
fig_save_address=Path(__file__).parent / "test_resources" / "figures",
model_path=Path(__file__).parent / "test_resources" / "deep_model" / model_name,
test_folder_address=Path(__file__).parent
/ "test_resources"
/ "dataset_train_test_val"
/ "test",
)

# find list of images in the Path(__file__).parent / "dataset_train_test_val" / "test" / "pdc_bit"
directory_path = (
Path(__file__).parent
/ "test_resources"
/ "dataset_train_test_val"
/ "test"
/ "pdc_bit"
)
list_of_images = [str(x) for x in directory_path.glob("*.jpeg")]
obj.grad_cam_viz(
model_path=Path(__file__).parent / "test_resources" / "deep_model" / model_name,
fig_to_save_address=Path(__file__).parent / "test_resources" / "figures",
img_to_be_applied_path=Path(__file__).parent
/ "test_resources"
/ "dataset_train_test_val"
/ "test"
/ "pdc_bit"
/ list_of_images[0],
output_gradcam_fig_name="gradcam.png",
)
def test_run_all():
script_path = Path(__file__).parent / ".." / "neural_network_model" / "script_run_all.py"
os.system(f'python {script_path}')
assert True

0 comments on commit fc92ff0

Please sign in to comment.