Working with pretty damn good accuracy
This commit is contained in:
parent
a0675ce03f
commit
ca9cdeee13
@ -7,6 +7,8 @@ from matplotlib import pyplot as plt
|
|||||||
import math
|
import math
|
||||||
import cv2
|
import cv2
|
||||||
from skimage import color, exposure, transform
|
from skimage import color, exposure, transform
|
||||||
|
import skimage as sk
|
||||||
|
import random
|
||||||
|
|
||||||
'''
|
'''
|
||||||
Defines some basic preprocessing for the images
|
Defines some basic preprocessing for the images
|
||||||
@ -17,8 +19,29 @@ def preprocess(img):
|
|||||||
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
|
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
|
||||||
img = color.hsv2rgb(hsv)
|
img = color.hsv2rgb(hsv)
|
||||||
|
|
||||||
img = img/255 # Scaling images down to values of 0-255
|
#img = img/255 # Scaling images down to values of 0-255
|
||||||
img = np.rollaxis(img, -1) # rolls colour axis to 0
|
|
||||||
|
return img
|
||||||
|
|
||||||
|
'''
|
||||||
|
Augments an image (horizontal reflection, hue, contrast, saturation adjustment) to expand on the dataset
|
||||||
|
Code was adapted from Medium.com/@thimblot (Thomas Himblot)
|
||||||
|
'''
|
||||||
|
def augment(img):
|
||||||
|
# Randomly rotate the image
|
||||||
|
random_degree = random.uniform(-25, 25) # Random degree of rotation between 25% on the left and 25% on the right
|
||||||
|
img = sk.transform.rotate(img, random_degree)
|
||||||
|
|
||||||
|
# Add random noise to the image
|
||||||
|
img = sk.util.random_noise(img)
|
||||||
|
|
||||||
|
# Randomly (25% chance) flips the image horizontally (by inverting the image dimensions)
|
||||||
|
if random.randint(0, 4) == 1:
|
||||||
|
img = img[:, ::-1]
|
||||||
|
|
||||||
|
# Randomly (25% chance) inverts the intensity range of the image
|
||||||
|
if random.randint(0, 4) == 1:
|
||||||
|
img = sk.util.invert(img)
|
||||||
|
|
||||||
return img
|
return img
|
||||||
|
|
||||||
@ -30,27 +53,40 @@ def gen_data(w_path, n_w_path):
|
|||||||
imgs_raw = [] # Images
|
imgs_raw = [] # Images
|
||||||
imgs_lbl = [] # Image labels
|
imgs_lbl = [] # Image labels
|
||||||
|
|
||||||
i = 0
|
w = 0
|
||||||
for image_name in waldo_file_list:
|
for image_name in waldo_file_list:
|
||||||
pic = cv2.imread(os.path.join(w_path, image_name)) # NOTE: cv2.imread() returns a numpy array in BGR not RGB
|
pic = cv2.imread(os.path.join(w_path, image_name)) # NOTE: cv2.imread() returns a numpy array in BGR not RGB
|
||||||
pic = preprocess(pic)
|
pic = preprocess(pic)
|
||||||
|
|
||||||
imgs_raw.append(pic)
|
pic_roll = np.rollaxis(pic, -1) # rolls colour axis to 0
|
||||||
|
imgs_raw.append(pic_roll)
|
||||||
imgs_lbl.append(1) # Value of 1 as Waldo is present in the image
|
imgs_lbl.append(1) # Value of 1 as Waldo is present in the image
|
||||||
|
|
||||||
print('Completed: {0}/{1} Waldo images'.format(i+1, total_w))
|
for j in range(0, 10):
|
||||||
i += 1
|
pic = augment(pic)
|
||||||
|
pic_roll = np.rollaxis(pic, -1) # rolls colour axis to 0
|
||||||
|
imgs_raw.append(pic_roll)
|
||||||
|
imgs_lbl.append(1)
|
||||||
|
|
||||||
i = 0
|
|
||||||
|
print('Completed: {0}/{1} Waldo images'.format(w+1, total_w))
|
||||||
|
w += 1
|
||||||
|
|
||||||
|
nw = 0
|
||||||
for image_name in not_waldo_file_list:
|
for image_name in not_waldo_file_list:
|
||||||
pic = cv2.imread(os.path.join(n_w_path, image_name))
|
pic = cv2.imread(os.path.join(n_w_path, image_name))
|
||||||
pic = preprocess(pic)
|
pic = preprocess(pic)
|
||||||
|
|
||||||
|
pic = np.rollaxis(pic, -1) # rolls colour axis to 0
|
||||||
imgs_raw.append(pic)
|
imgs_raw.append(pic)
|
||||||
imgs_lbl.append(0)
|
imgs_lbl.append(0)
|
||||||
|
|
||||||
print('Completed: {0}/{1} non-Waldo images'.format(i+1, total_nw))
|
print('Completed: {0}/{1} non-Waldo images'.format(nw+1, total_nw))
|
||||||
i += 1
|
if nw > 50*w:
|
||||||
|
print("Non_Waldo files restricted")
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
nw += 1
|
||||||
|
|
||||||
## Randomise and split data into training and test sets
|
## Randomise and split data into training and test sets
|
||||||
# Code was modified from code written by: Kyle O'Brien (medium.com/@kylepob61392)
|
# Code was modified from code written by: Kyle O'Brien (medium.com/@kylepob61392)
|
||||||
|
@ -8,8 +8,12 @@ test_y = np.load("Waldo_test_lbl.npy")
|
|||||||
test_y = to_categorical(test_y)
|
test_y = to_categorical(test_y)
|
||||||
|
|
||||||
f = open("test_output.txt", 'w')
|
f = open("test_output.txt", 'w')
|
||||||
|
z = 0
|
||||||
for i in range(0, len(test_y)):
|
for i in range(0, len(test_y)):
|
||||||
print(pred_y[i], test_y[i], file=f)
|
print(pred_y[i], test_y[i], file=f)
|
||||||
|
# Calculates correct predictions
|
||||||
|
if pred_y[i][0] == test_y[i][0]:
|
||||||
|
z+=1
|
||||||
|
|
||||||
|
print("Accuracy: {}%".format(z/len(test_y)*100))
|
||||||
f.close()
|
f.close()
|
File diff suppressed because it is too large
Load Diff
@ -117,7 +117,7 @@ model = FCN()
|
|||||||
# ensemble_iclf = ImageClassifier(ensemble.RandomForestClassifier)
|
# ensemble_iclf = ImageClassifier(ensemble.RandomForestClassifier)
|
||||||
|
|
||||||
## Define training parameters
|
## Define training parameters
|
||||||
epochs = 20 # an epoch is one forward pass and back propogation of all training data
|
epochs = 10 # an epoch is one forward pass and back propogation of all training data
|
||||||
batch_size = 150 # batch size - number of training example used in one forward/backward pass
|
batch_size = 150 # batch size - number of training example used in one forward/backward pass
|
||||||
# (higher batch size uses more memory, smaller batch size takes more time)
|
# (higher batch size uses more memory, smaller batch size takes more time)
|
||||||
#lrate = 0.01 # Learning rate of the model - controls magnitude of weight changes in training the NN
|
#lrate = 0.01 # Learning rate of the model - controls magnitude of weight changes in training the NN
|
||||||
|
Reference in New Issue
Block a user