-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from aditya9211/blur_clear_v2
Blur clear v2
- Loading branch information
Showing
6 changed files
with
1,012 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
This file contains constants required in the training | ||
and testing stage of data processing also for training | ||
Neural Networks Model | ||
""" | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
# Stores current exceuting path | ||
HOME_FOLDER_PATH = os.getcwd() | ||
|
||
# if not passes default model path | ||
MODEL_PATH = HOME_FOLDER_PATH + '/model/result.pkl' | ||
DATA_PATH = HOME_FOLDER_PATH + '/data' | ||
|
||
# Path where splitted data get stored | ||
TRAIN_DATA_PATH = DATA_PATH + '/train_images.npy' | ||
TRAIN_LABEL_PATH = DATA_PATH + '/train_labels.npy' | ||
TEST_DATA_PATH = DATA_PATH + '/test_images.npy' | ||
TEST_LABEL_PATH = DATA_PATH + '/test_labels.npy' | ||
|
||
# Path for saving plot of cost vs iterations | ||
PLOT_PATH = DATA_PATH + '/loss_decay.png' | ||
|
||
|
||
# Median Filter size | ||
RADIUS = 3 | ||
|
||
# width and height of resized image | ||
WIDTH = 100 | ||
HEIGHT = 100 | ||
|
||
# Splitting ratio for training & testing | ||
SPLIT_RATIO = 0.2 | ||
|
||
# Seed to get same results on re-run | ||
SEED = 10 | ||
|
||
# Size of data to be fed at each epochs | ||
BATCH_SIZE = 10 | ||
|
||
# Hidden Layer neurons size | ||
NEURONS_SIZE = 300 | ||
|
||
# Iteration in NN Model | ||
MAX_ITER = 50 | ||
|
||
# Logging steps to show summary | ||
LOGGING_STEPS = min(1, MAX_ITER) | ||
|
||
# Learning rate in NN Model | ||
ALPHA = 0.001 | ||
|
||
# Regularization Term used in cost | ||
LAMBDA = 0.0007 | ||
|
||
# Default Activation Function | ||
ACT = 'sig' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Script for predicting label as | ||
good or bad given the image | ||
It preprocess the image same as it | ||
was pre-processed in training | ||
""" | ||
import argparse | ||
from sklearn.externals import joblib | ||
import numpy as np | ||
import scipy.misc as ms | ||
import scipy.ndimage as nd | ||
from utils import (resize, validate, path_validation) | ||
from config import (MODEL_PATH, WIDTH, HEIGHT, | ||
RADIUS) | ||
|
||
def predict_preprocess(IMAGE_PATH): | ||
""" | ||
Preprocess the image given path of the image/images | ||
to apply median filter and resizing the image | ||
same as done in training the network | ||
@ Parameters: | ||
------------- | ||
IMAGE_PATH: str | ||
Path of the images | ||
@ Returns: | ||
---------- | ||
img: np.array | ||
filtered and pre-processed combined | ||
images arrays | ||
""" | ||
# Reading images in grayscale mode | ||
img = ms.imread(IMAGE_PATH, mode='L') | ||
# APllying median filter to remove noise | ||
img = nd.median_filter(img, RADIUS) | ||
# To make it 2D | ||
img = img[np.newaxis, :] | ||
# Resizing the images to that of train | ||
img = resize(img, width=WIDTH, height=HEIGHT) | ||
# Addition of bias term | ||
img = np.insert(img, 0, 1, axis=1) | ||
|
||
return img | ||
|
||
|
||
def main(): | ||
""" | ||
Parse the argument and check validaton | ||
of passed image and trained model path | ||
Predict the label of images passed after | ||
pre-process the images same as done in | ||
training part | ||
""" | ||
# Construct the argument parser and parse the arguments | ||
ap = argparse.ArgumentParser() | ||
ap.add_argument("-img", "--image_path", required=True, | ||
help="path to image") | ||
args = vars(ap.parse_args()) | ||
|
||
IMAGE_PATH = args["image_path"] | ||
|
||
# Path Validation of image and Model | ||
if not path_validation(IMAGE_PATH, read_access=True): | ||
exit(0) | ||
if not path_validation(MODEL_PATH, read_access=True): | ||
exit(0) | ||
|
||
# Preprocessed the images | ||
img = predict_preprocess(IMAGE_PATH) | ||
|
||
# Load the trained NN model | ||
params = joblib.load(MODEL_PATH) | ||
|
||
# Find the label predicted by the model | ||
predicted_label = validate(params, img) | ||
|
||
for label in predicted_label: | ||
if label: | ||
print("Good Image\n") | ||
else: | ||
print("Bad Image\n") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
|
Oops, something went wrong.