def font_load(idx):
    font_data = PickleHelper.load_pickle(
        path="/mnt/SharedData/Development/Personal_Dev/Machine-Learning/Data/Font data/",
        name = fonts_pkl[idx])

    train_xdata = np.array(font_data[0])
    #train_xdata = np.reshape(font_data[0], (-1, 28, 28))
    train_ydata = np.array([i for i in range(0, len(train_xdata))])

    return train_xdata, train_ydata
Beispiel #2
0
            nabla_w[-l] = np.dot(delta, activations[-l - 1].transpose())

        return (nabla_b, nabla_w)

    def cost_derivative(self, output_activations, y):
        return (output_activations - y)

    def evaluate(self, test_data):
        test_results = [(np.argmax(self.feedforward(x)), y)
                        for (x, y) in test_data]
        return sum(int(x == y) for (x, y) in test_results)


if __name__ == "__main__":

    train_set, valid_set, test_set = PickleHelper.load_pickle(path="./",
                                                              name="mnist.pkl")

    def vectorized_result(j):
        e = np.zeros((10, 1))
        e[j] = 1.0
        return e

    train_x = [np.reshape(x, (784, 1)) for x in train_set[0]]
    train_y = [vectorized_result(y) for y in train_set[1]]

    test_x = [np.reshape(x, (784, 1)) for x in test_set[0]]

    #print(np.shape(train_x), np.shape(train_y))
    #print(np.max(train_x[0]))

    import matplotlib.pyplot as plt
Beispiel #3
0
from activation import Sigmoid
from cost import SSE
from update import Update
import numpy as np
import os, sys
from pickle_helper import PickleHelper
from tqdm import tqdm
from random import shuffle
"""
global_path = os.path.abspath("../data/")

if global_path not in sys.path:
    sys.path.append(global_path)
"""

train_set, valid_set, test_set = PickleHelper.load_pickle(path="../Data/",
                                                          name="mnist.pkl")


def vectorized_result(j):
    e = np.zeros(10)
    e[j] = 1.0
    return e


def data_processing(dataset):

    x = np.array([np.reshape(x, 784) for x in dataset[0]])
    y = np.array([vectorized_result(y) for y in dataset[1]])

    return x, y
    def wrangling_augmentation(self):
        features = PickleHelper.load_pickle(
            path="../../Data/Face/Face-Obj-Augment/",
            name="faces-obj-32x32-features-norm.pkl")
        labels = PickleHelper.load_pickle(
            path="../../Data/Face/Face-Obj-Augment/",
            name="faces-obj-32x32-labels.pkl")
        #labels = PickleHelper.load_pickle(path = "../../Data/Face/", name = "faces-obj-32x32-labels-landmark.pkl")

        print("Data shape: {0}, Landmark shape: {1}".format(
            features.shape, labels.shape))

        margin = list(range(-12, 13))

        for idx in tqdm(range(len(features))):

            # IMAGE ZOOM AND ZOOM OUT
            if np.random.randint(2) == 1:
                scale = np.random.randint(16, 48)
                resized_img = ImgFunctions.resize_img(features[idx],
                                                      size=(scale, scale))
                #print(resized_img.shape, " | ", scale)

                padding = int((32 - scale) / 2)
                if padding >= 0:
                    resized_img = cv2.copyMakeBorder(resized_img,
                                                     int((32 - scale) / 2),
                                                     int((32 - scale) / 2),
                                                     int((32 - scale) / 2),
                                                     int((32 - scale) / 2),
                                                     cv2.BORDER_CONSTANT,
                                                     value=[0, 0, 0])

                    features[idx] = ImgFunctions.resize_img(resized_img)

                else:
                    resized_img = resized_img[int((scale - 32) /
                                                  2):int((scale - 32) / 2) +
                                              32,
                                              int((scale - 32) /
                                                  2):int((scale - 32) / 2) +
                                              32]
                    features[idx] = resized_img
                    '''
                # IMAGE TRANSLATE BY MARGIN 8
                if labels[idx][0] == 0: # if label is for face
                    
                    margin_rnd_idx = np.random.randint(len(margin))
                    features[idx] = ImgFunctions.translation(features[idx], margin[margin_rnd_idx], margin[margin_rnd_idx])
                    
                    labels[idx][1] = labels[idx][1]+margin[margin_rnd_idx]
                    labels[idx][2] = labels[idx][2]+margin[margin_rnd_idx]
                    '''
                    '''
                    import matplotlib.pyplot as plt
                    plt.imshow(features[idx])
                    print(labels[idx])
                    plt.show()
                    '''

        PickleHelper.save_to_pickle(
            save_dir, "augment_faces-obj-32x32-features-norm.pkl", features)
        PickleHelper.save_to_pickle(
            save_dir, "augment_faces-obj-32x32-labels-landmark.pkl", labels)
    def wrangling1(self):
        ''' # # face image to pickle
        features, labels = self.img2numpy_array(self.dir_path)
        
        PickleHelper.save_to_pickle(save_dir, "faces-32x32-features.pkl", features)
        PickleHelper.save_to_pickle(save_dir, "faces-32x32-labels.pkl", labels)    
        '''

        # Load pickle data
        feature1 = PickleHelper.load_pickle(path="../../Data/Face/",
                                            name="faces-32x32-features.pkl")
        feature2 = PickleHelper.load_pickle(
            path="../../Data/Objects/cifar-100-python/", name="train")[b'data']

        # Data wragline for concatenating face and cifar dataset
        # # Normalization for face images

        new_feature = []
        label_landmark = [
        ]  # for the center of face. [class (face:0, none:1), x, y]
        for f in tqdm(feature1):
            #f_gray = self.bgr2gray(f)
            f_norm = ImgFunctions.scailing(f, new_min=0, new_max=1)
            #print("feature1 < min: {0} | max: {1} >".format(np.min(f_norm), np.max(f_norm)))
            new_feature.append(f_norm)
            label_landmark.append([0, 16., 16.])
        feature1 = new_feature

        # # Change channel of image to the last dimension
        feature2 = np.reshape(feature2, (-1, 3, 32, 32))
        feature2 = np.moveaxis(feature2, 1, 3)

        # # Normalization for object images
        new_feature = []
        for f in tqdm(feature2):
            #f_gray = self.bgr2gray(f)
            f_norm = ImgFunctions.scailing(f, new_min=0, new_max=1)
            #print("feature1 < min: {0} | max: {1} >".format(np.min(f_norm), np.max(f_norm)))
            new_feature.append(ImgFunctions.scailing(f_norm))
            label_landmark.append([1, None, None])

        feature2 = new_feature

        features = np.vstack([feature1, feature2])

        # For simple classification [1 (face), 2 (none)]
        label1 = np.ones(len(feature1), dtype=np.float32)
        label2 = np.ones(len(feature2), dtype=np.float32) * 2
        labels = np.hstack([label1, label2])

        # # shuffle the dataset
        shuffle_idx = np.arange(len(features))
        np.random.shuffle(shuffle_idx)
        features = features[shuffle_idx]
        labels = labels[shuffle_idx]

        label_landmark = np.array(label_landmark)
        label_landmark = label_landmark[shuffle_idx]

        print("Data shape: {0}, Landmark shape: {1}".format(
            features.shape, labels.shape))
        #print("Data shape: {0}, Landmark shape: {1}".format(features.shape, label_landmark.shape))
        labels = self.one_hot_encoding(labels)

        #print(labels[:10])

        PickleHelper.save_to_pickle(save_dir,
                                    "faces-obj-32x32-features-norm.pkl",
                                    features)
        PickleHelper.save_to_pickle(save_dir, "faces-obj-32x32-labels.pkl",
                                    labels)
Beispiel #6
0
from pickle_helper import PickleHelper
from network import Network
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm

train_set, valid_set, test_set = PickleHelper.load_pickle(path="../Data/",
                                                          name="mnist.pkl")
W, B = PickleHelper.load_pickle(path="./", name="minist_one_layer.pkl")

train_x = [np.reshape(x, (784, 1)) for x in train_set[0]]
train_x = np.array(train_x)
test_x = [np.reshape(x, (784, 1)) for x in test_set[0]]
test_x = np.array(test_x)

ann = Network([784, 30, 10])
ann.weights = W
ann.biases = B
print np.argmax(ann.feedforward(train_x[10]))

fig, ax = plt.subplots(1, 5, figsize=(10, 2))
#plt.show()
'''
indice = np.random.choice(len(test_x), size=5)
print indice
test_x = np.array(test_x)
test_x[indice]
'''

for iter in tqdm(range(100)):
Beispiel #7
0
 def save_network(self, name):
     PickleHelper.save_to_pickle(path="./",
                                 name=name,
                                 data=[self.weights, self.biases])
            #img2 = cv2.rectangle(hd_img,(tx,ty),(tx+kernel_size[0],ty+kernel_size[1]),(0,255,0),5)
            #plt.imshow(ImgFunctions.bgr2rgb(img2))
            #plt.show()

            patch_img = ImgFunctions.resize_img(hd_img[ty:ty + kernel_size[1],
                                                       tx:tx + kernel_size[0]])
            patch_img = ImgFunctions.scailing(patch_img)
            return_list.append(patch_img)

            ty += strides[0]

        tx += strides[1]
        ty = 0

    print("Return shape:", np.shape(return_list))
    #plt.imshow(ImgFunctions.bgr2rgb(return_list[-1]))
    #plt.show()
    return np.array(return_list)


PickleHelper.save_to_pickle("../../Data/Face/", "star_wars_360x360.pkl",
                            cut_down_to_windows(img, (250, 250), (10, 10)))
#PickleHelper.save_to_pickle("../../Data/Face/", "mission_impossible_360x360.pkl", cut_down_to_windows(img, (250, 250), (10, 10)))
#PickleHelper.save_to_pickle("../../Data/Face/", "Gang_360x360.pkl", cut_down_to_windows(img, (250, 250), (10, 10)))
#PickleHelper.save_to_pickle("../../Data/Face/", "Ermin-Gang_360x360.pkl", cut_down_to_windows(img, (250, 250), (10, 10)))
#PickleHelper.save_to_pickle("../../Data/Face/", "Tsunematsu-Itamochi_360x360.pkl", cut_down_to_windows(img, (250, 250), (10, 10)))

#https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/
#https://www.coursera.org/learn/convolutional-neural-networks/lecture/fF3O0/yolo-algorithm