def train_digits(PERCENT=1, EPOCHS=1): digits = read_data.read_file(fdata='digitdata/trainingimages', flabel='digitdata/traininglabels', WIDTH=28, HEIGHT=28, type='digits') num_data = len(digits[0]) #amount of training data neurons = [] hits = 0 for x in range(10): #create 10 neuron classes neurons.append(neuron(size=28 * 28)) for epochs in range(EPOCHS): digits = read_data.read_file(fdata='digitdata/trainingimages', flabel='digitdata/traininglabels', WIDTH=28, HEIGHT=28, type='digits') for k in range(int(num_data * PERCENT)): x = rand.randint(0, len(digits[0]) - 1) #get x as random features = get_features.features_from_image( digits[0][x]) #get vector of features scores = [] for y in neurons: #get score for each class scores.append(y.score(features)) winnerIndex = scores.index(max(scores)) if winnerIndex != digits[1][x]: neurons[winnerIndex].weights -= features neurons[digits[1][x]].weights += features digits[0].pop(x) digits[1].pop(x) return neurons
def train_faces(PERCENT=1, EPOCHS=1): faces = read_data.read_file(fdata='facedata/facedatatrain', flabel='facedata/facedatatrainlabels', WIDTH=60, HEIGHT=70, type='faces') num_data = len(faces[0]) #amount of training neurons = [] hits = 0 neurons.append(neuron(size=60 * 70)) for epochs in range(EPOCHS): faces = read_data.read_file(fdata='facedata/facedatatrain', flabel='facedata/facedatatrainlabels', WIDTH=60, HEIGHT=70, type='faces') for k in range(int(num_data * PERCENT)): x = rand.randint(0, len(faces[0]) - 1) #get x as random features = get_features.features_from_image( faces[0][x]) #get vector of features if neurons[0].score(features) < 0 and faces[1][x] == 1: neurons[0].weights += features elif neurons[0].score(features) >= 0 and faces[1][x] == 0: neurons[0].weights -= features faces[0].pop(x) faces[1].pop(x) return neurons
def train_digits(PERCENTAGE=1): digits = read_data.read_file(fdata='digitdata/trainingimages', flabel='digitdata/traininglabels', WIDTH=28, HEIGHT=28, type='digits') num_data = len(digits[0]) #amount of training data label_obj = [] features = get_features.features_from_image(digits[0][0]) for x in range(10): #create 10 label objects for each class lbl = label() lbl.v0 = np.ones(len(features)) lbl.v1 = np.ones(len(features)) lbl.v2 = np.ones(len(features)) label_obj.append(lbl) ''' get frequency of feature values for each feature in training set ''' for k in range(int(num_data * PERCENTAGE)): # for each training data number x = rand.randint(0, len(digits[0]) - 1) #get x as random index features = get_features.features_from_image( digits[0][x]) #get vector of features label_obj[digits[1][x]].frequency += 1 for y in range(len(features)): if features[y] == 0: label_obj[digits[1][x]].v0[y] += 1 elif features[y] == 1: label_obj[digits[1][x]].v1[y] += 1 elif features[y] == 2: label_obj[digits[1][x]].v2[y] += 1 digits[0].pop(x) digits[1].pop(x) return label_obj, num_data
def perceptron_deploy(digit=1, PERCENTAGE=1, digits=True): if digits: data = read_data.read_file(fdata='digitdata/testimages', flabel='digitdata/testlabels', WIDTH=28, HEIGHT=28, type='digits') else: data = read_data.read_file(fdata='facedata/facedatatest', flabel='facedata/facedatatestlabels', WIDTH=60, HEIGHT=70, type='faces') num_data = len(data[1]) #amount of training data neurons = [] if digits: neurons = perceptron.train_digits(PERCENT=PERCENTAGE, EPOCHS=1) else: neurons = perceptron.train_faces(PERCENT=PERCENTAGE, EPOCHS=1) print("Trained Model!") hits = 0 num = 0 if digits: while True: num = rand.randint(0, len(data[0]) - 1) if digit == data[1][num]: print("Found Digit to Guess!") break else: num = rand.randint(0, len(data[0]) - 1) features = get_features.features_from_image( data[0][num]) #get vector of features scores = [] for y in neurons: scores.append(y.score(features)) if digits: winnerIndex = scores.index(max(scores)) print("Predicted the digit: %s" % winnerIndex) else: if scores[0] < 0: print("Not Face!") else: print("Is Face!") plt.imshow(data[0][num]) plt.show()
def infrence_model(PERCENTAGE=1): SMOOTHER = 1 label_obj, num_data = train_digits(PERCENTAGE=PERCENTAGE) digits = read_data.read_file(fdata='digitdata/testimages', flabel='digitdata/testlabels', WIDTH=28, HEIGHT=28, type='digits') predictions = [] #outputs from bayes classifier for x in range(len(digits[0])): features = get_features.features_from_image( digits[0][x]) #get array of features maxls = [] cur_guess = None for y in range( 10): #get prob of each label and choose highest as answer p_y = math.log( (label_obj[y].frequency) / int(num_data * PERCENTAGE)) likelihood = 0 for feats in range(len(features)): if features[feats] == 0: likelihood += math.log( (label_obj[y].v0[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v0[feats]) * SMOOTHER) elif features[feats] == 1: likelihood += math.log( (label_obj[y].v1[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v1[feats]) * SMOOTHER) elif features[feats] == 2: likelihood += math.log( (label_obj[y].v2[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v2[feats]) * SMOOTHER) likelihood = likelihood + p_y maxls.append(likelihood) predictions.append(maxls.index(max(maxls))) hits = 0 for x in range(len(digits[1])): if predictions[x] == digits[1][x]: hits += 1 accuracy = hits / len(digits[1]) return accuracy
def bayes_digit(digit=1, PERCENTAGE=1): SMOOTHER = 1 label_obj, num_data = naive_bayes_digits.train_digits(PERCENTAGE=1) print("Trained Model!") digits = read_data.read_file(fdata='digitdata/testimages', flabel='digitdata/testlabels', WIDTH=28, HEIGHT=28, type='digits') num = 0 while True: num = rand.randint(0, len(digits[0]) - 1) if digit == digits[1][num]: break print("Found Digit to Guess!") features = get_features.features_from_image( digits[0][num]) #get array of features maxls = [] for y in range(10): #get prob of each label and choose highest as answer p_y = math.log((label_obj[y].frequency) / int(num_data) * PERCENTAGE) likelihood = 0 for feats in range(len(features)): if features[feats] == 0: likelihood += math.log( (label_obj[y].v0[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v0[feats]) * SMOOTHER) elif features[feats] == 1: likelihood += math.log( (label_obj[y].v1[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v1[feats]) * SMOOTHER) elif features[feats] == 2: likelihood += math.log( (label_obj[y].v2[feats] + SMOOTHER) / (label_obj[y].frequency + label_obj[y].v2[feats]) * SMOOTHER) likelihood = likelihood + p_y maxls.append(likelihood) prediction = maxls.index(max(maxls)) print("Predicted the digit: %s" % prediction) plt.imshow(digits[0][num]) plt.show()
def deploy_model(PERCENT, EPOCHS, digits=True): #set digits to false to train faces if digits: data = read_data.read_file(fdata='digitdata/testimages', flabel='digitdata/testlabels', WIDTH=28, HEIGHT=28, type='digits') else: data = read_data.read_file(fdata='facedata/facedatatest', flabel='facedata/facedatatestlabels', WIDTH=60, HEIGHT=70, type='faces') num_data = len(data[1]) #amount of training data neurons = [] if digits: neurons = train_digits(PERCENT=PERCENT, EPOCHS=EPOCHS) else: neurons = train_faces(PERCENT=PERCENT, EPOCHS=EPOCHS) hits = 0 for x in range(num_data): features = get_features.features_from_image( data[0][x]) #get vector of features scores = [] for y in neurons: scores.append(y.score(features)) if digits: winnerIndex = scores.index(max(scores)) if winnerIndex == data[1][x]: hits += 1 else: if scores[0] < 0 and data[1][x] == 0: hits += 1 elif scores[0] >= 0 and data[1][x] == 1: hits += 1 return hits / num_data