Skip to content

Commit

Permalink
refactoring 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianGroeger96 committed May 8, 2019
1 parent 91b10ce commit f01a71a
Show file tree
Hide file tree
Showing 22 changed files with 35 additions and 34 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from data_extractor.extractor import Extractor
from DataExtractor.extractor import Extractor


def main():
Expand Down
10 changes: 5 additions & 5 deletions data_extractor/extractor.py → DataExtractor/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from natsort import natsorted
from tqdm import tqdm
from isolator.isolator import Isolator
from trainer.models.model_gnet_light import ModelGNetLight
from Isolator.isolator import Isolator
from Trainer.Models.model_gnet_light import ModelGNetLight
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.preprocessing.image import array_to_img, img_to_array, load_img

Expand Down Expand Up @@ -131,7 +131,7 @@ def create_random_images(self, category, count):
category_dir = os.path.join(self.current_working_dir, constants.OUTPUT_DATA_DIR, category)

for index in tqdm(range(count)):
if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image_rand = np.random.randint(0, 255, size=(constants.IMG_SIZE, constants.IMG_SIZE), dtype=np.uint8)
else:
image_rand = np.random.randint(0, 255,
Expand All @@ -157,7 +157,7 @@ def create_training_data(self):
img_array = cv2.imread(os.path.join(category_dir, img))
if img_array is not None:
# convert image to grayscale if parameter is set in constants file
if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY)
# resize to normalize data size
new_array = cv2.resize(img_array, (constants.IMG_SIZE, constants.IMG_SIZE))
Expand Down Expand Up @@ -210,7 +210,7 @@ def categorize_with_trained_model(self):
image_path = os.path.join(extracted_data_dir, img)
img_array = cv2.imread(image_path)
if img_array is not None:
if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
resized_image_array = cv2.resize(image_array, (constants.IMG_SIZE, constants.IMG_SIZE))
reshaped_image = resized_image_array.reshape(-1, constants.IMG_SIZE, constants.IMG_SIZE,
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions isolator/isolator.py → Isolator/isolator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cv2
import constants
import numpy as np
from isolator.isolator_constants import IsolatorConstants
from isolator.isolator_constants_320_240 import IsolatorConstants320240
from isolator.isolator_constants_640_480 import IsolatorConstants640480
from Isolator.isolator_constants import IsolatorConstants
from Isolator.isolator_constants_320_240 import IsolatorConstants320240
from Isolator.isolator_constants_640_480 import IsolatorConstants640480


class Isolator:
Expand All @@ -16,7 +16,7 @@ def get_regions_of_interest(self, image):
# 0: roi, 1: type
regions_of_interest = []

if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

if self.CONSTANTS is None:
Expand All @@ -40,7 +40,7 @@ def get_contours_and_rois(self, image):
# 0: roi, 1: type
contours_signal_type = []

if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

if self.CLASS_CONSTANTS is not None:
Expand All @@ -64,7 +64,7 @@ def get_contours_and_rois(self, image):
return contours_signal_type

def __set_constants(self, image):
if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image_height, image_width = image.shape
else:
image_height, image_width, _ = image.shape
Expand All @@ -84,7 +84,7 @@ def __crop(self, image):
width_start = self.CONSTANTS.CROP_WIDTH_START
width_end = self.CONSTANTS.CROP_WIDTH_END

if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
image_info = image[info_start:info_end, width_start:width_end]
image_stop = image[stop_start:stop_end, width_start:width_end]
else:
Expand All @@ -102,7 +102,7 @@ def __detect_edges(self, channel):
return sobel

def __preprocess(self, image):
if constants.USE_GRAYSCALE:
if constants.USE_GRAY_SCALE:
# create gradient image from gray scale image
# for better performance (only 1 channel)
image = self.__detect_edges(image)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from isolator.isolator_constants import IsolatorConstants
from Isolator.isolator_constants import IsolatorConstants


class IsolatorConstants320240(IsolatorConstants):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from isolator.isolator_constants import IsolatorConstants
from Isolator.isolator_constants import IsolatorConstants


class IsolatorConstants640480(IsolatorConstants):
Expand Down
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ and a category for 'not numbers'.
### Includes

- DataExtractor (to extract data for the dataset)
- Isolator (to find the numbers inside an image)
- Trainer (to train the model)
- Tester (to test the model on custom images)
- Utils (test, simulate the isolator on images)

## Install

Expand Down
File renamed without changes.
12 changes: 7 additions & 5 deletions tester/test_model.py → Tester/test_model.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import numpy as np
from tester import Tester
from Tester.tester import Tester
from Trainer.Models.model_gnet_light import ModelGNetLight
from Trainer.Models.model_gnet_deep import ModelGNetDeep


def main():
tester = Tester()
tester = Tester(ModelGNetDeep('GNet'))

# test the model with a given image
# tester.test_model_with_image('frame_overlaying_cnt_2.jpg')
# Tester.test_model_with_image('frame_overlaying_cnt_2.jpg')

# test the model with a random color array
# image_random_color = np.random.randint(0, 255, size=(28, 28, 3), dtype=np.uint8)
# tester.test_model_with_array(image_random_color)
# Tester.test_model_with_array(image_random_color)

# test the model with a random gray scale array
# image_random_gray = np.random.randint(0, 255, size=(28, 28), dtype=np.uint8)
# tester.test_model_with_array(image_random_gray)
# Tester.test_model_with_array(image_random_gray)

# test the model with a folder of images
tester.test_model_with_folder('continuous')
Expand Down
6 changes: 2 additions & 4 deletions tester/tester.py → Tester/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import shutil
import numpy as np
from tqdm import tqdm
from isolator.isolator import Isolator
from trainer.models.model_gnet_light import ModelGNetLight
from Isolator.isolator import Isolator


class Tester:

def __init__(self):
def __init__(self, model_obj):
model_path = "{}.h5".format(constants.MODEL_DIR)
model_obj = ModelGNetLight('GNet')
model_obj.create_model(weights_path=model_path)
self.model = model_obj.model
self.model.summary()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion trainer/models/model.py → Trainer/Models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.framework.graph_util import convert_variables_to_constants
from tensorflow.python.keras.callbacks import TensorBoard
from trainer.utils.tensorboard_filter_visualisation import TensorBoardFilterVisualisation
from Trainer.Utils.tensorboard_filter_visualisation import TensorBoardFilterVisualisation

# only show tensorflow errors
tf.logging.set_verbosity(tf.logging.ERROR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import constants
from trainer.models.model import Model
from Trainer.Models.model import Model
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import constants
from trainer.models.model import Model
from Trainer.Models.model import Model
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import constants
from trainer.models.model import Model
from Trainer.Models.model import Model
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.python.keras.layers import Conv2D
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions trainer/train_model.py → Trainer/train_model.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import time
import constants
from trainer.models.model_gnet_light import ModelGNetLight
from trainer.models.model import Model
from Trainer.Models.model_gnet_light import ModelGNetLight
from Trainer.Models.model_gnet_deep import ModelGNetDeep
from Trainer.Models.model import Model
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Activation, Flatten
from tensorflow.python.keras.layers import Conv2D, MaxPooling2D


def main():
model = ModelGNetLight('new-without-duplicates-aug-batch-16-epoch-10-test')
model = ModelGNetDeep('new-without-duplicates-aug-batch-16-epoch-10-test')
model.create_model()
model.train_model()
model.save_model()
Expand Down
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
MODEL_DIR = "../number_detection_model"

IMG_SIZE = 28
USE_GRAYSCALE = True
USE_GRAY_SCALE = True
DIMENSION = 1

SIGNAL_TYPES = ["info", "stop"]
Expand Down

0 comments on commit f01a71a

Please sign in to comment.