1
0

Working with pretty damn good accuracy

This commit is contained in:
Silver-T 2018-05-24 01:35:40 +10:00
parent a0675ce03f
commit ca9cdeee13
4 changed files with 521 additions and 1229 deletions

View File

@ -7,6 +7,8 @@ from matplotlib import pyplot as plt
import math
import cv2
from skimage import color, exposure, transform
import skimage as sk
import random
'''
Defines some basic preprocessing for the images
@ -17,8 +19,29 @@ def preprocess(img):
hsv[:, :, 2] = exposure.equalize_hist(hsv[:, :, 2])
img = color.hsv2rgb(hsv)
img = img/255 # Scaling images down to values of 0-255
img = np.rollaxis(img, -1) # rolls colour axis to 0
#img = img/255 # Scaling images down to values of 0-255
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
@ -30,27 +53,40 @@ def gen_data(w_path, n_w_path):
imgs_raw = [] # Images
imgs_lbl = [] # Image labels
i = 0
w = 0
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 = 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
print('Completed: {0}/{1} Waldo images'.format(i+1, total_w))
i += 1
for j in range(0, 10):
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:
pic = cv2.imread(os.path.join(n_w_path, image_name))
pic = preprocess(pic)
pic = np.rollaxis(pic, -1) # rolls colour axis to 0
imgs_raw.append(pic)
imgs_lbl.append(0)
print('Completed: {0}/{1} non-Waldo images'.format(i+1, total_nw))
i += 1
print('Completed: {0}/{1} non-Waldo images'.format(nw+1, total_nw))
if nw > 50*w:
print("Non_Waldo files restricted")
break
else:
nw += 1
## Randomise and split data into training and test sets
# Code was modified from code written by: Kyle O'Brien (medium.com/@kylepob61392)

View File

@ -8,8 +8,12 @@ test_y = np.load("Waldo_test_lbl.npy")
test_y = to_categorical(test_y)
f = open("test_output.txt", 'w')
z = 0
for i in range(0, len(test_y)):
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()

File diff suppressed because it is too large Load Diff

View File

@ -117,7 +117,7 @@ model = FCN()
# ensemble_iclf = ImageClassifier(ensemble.RandomForestClassifier)
## 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
# (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