示例#1
0
def load_dataset(train_count=500, test_count=100):
    width = 320
    height = 180
    if os.path.isfile(abs_dir + "/image_data.pkl"):
        x_train = pickle_load(abs_dir + "/image_data.pkl")
    else:
        print("image_data.pkl not found")
        x_train = np.array([load_img('1', train_count), load_img('2', train_count), load_img('3', train_count), load_img('4', train_count), load_img('5', train_count)])
        x_train = x_train.reshape(5*train_count, 3, height, width)
        pickle_dump(x_train, abs_dir + "/image_data.pkl")

    t_train = np.array([load_label(1, train_count), load_label(2, train_count), load_label(3, train_count), load_label(4, train_count), load_label(5, train_count)])
    t_train = t_train.reshape(5*train_count, 5)

    if os.path.isfile(abs_dir + "/test_image_data.pkl"):
        x_test = pickle_load(abs_dir + "/test_image_data.pkl")
    else:
        print("test_image_data.pkl not found")
        x_test = np.array([load_img('1test', test_count), load_img('2test', test_count), load_img('3test', test_count), load_img('4test', test_count), load_img('5test', test_count)])
        x_test = x_test.reshape(5*test_count, 3, height, width)
        pickle_dump(x_test, abs_dir + "/test_image_data.pkl")

    t_test = np.array([load_label(1, test_count), load_label(2, test_count), load_label(3, test_count), load_label(4, test_count), load_label(5, test_count)])
    t_test = t_test.reshape(5*test_count, 5)
    x_train, t_train = shuffle_dataset(x_train, t_train)
    return (x_train, t_train), (x_test, t_test)
示例#2
0
def load_mnist(train_count=60000, test_count=10000, image_data_format="channels_first"):
    K.set_image_data_format(image_data_format)
    (x_train, t_train), (x_test, t_test) = mnist.load_data()
    x_train = np.array([x_train])
    x_test = np.array([x_test])
    x_train = x_train.transpose(1, 0, 2, 3)
    x_test = x_test.transpose(1, 0, 2, 3)
    (x_train, t_train) = shuffle_dataset(x_train, t_train)
    return (x_train[:train_count], t_train[:train_count]), (x_test[:test_count], t_test[:test_count])
    def train(self):
        # 书上没有利用已经有的这个shuffle函数,我觉得可以用上。用于打乱训练集
        self.train_x, self.train_label = shuffle_dataset(
            self.train_x, self.train_label)
        for i in range(self.max_iter):  # 训练max_iter次
            self.train_step()

        # 训练结束后计算测试集的准确率
        test_acc = self.network.accuracy(self.test_x, self.test_label)

        # 如果是verbose模式,输出测试结果
        if self.verbose:
            print("=============== Final Test Accuracy ===============")
            print("test acc: " + str(test_acc))
def main():
    # 获取MNIST数据集,为了加速测试,只使用训练集前500个样本
    (train_x, train_label), _ = load_mnist()
    train_x = train_x[: 500]
    train_label = train_label[: 500]

    # 从训练集中划分一部分作为验证集
    validation_rate = 0.2
    validation_num = int(train_x.shape[0] * validation_rate)
    # 先打乱训练集再划分
    train_x, train_label = shuffle_dataset(train_x, train_label)
    val_x = train_x[: validation_num]
    val_label = train_label[: validation_num]
    train_x = train_x[validation_num:]
    train_label = train_label[validation_num:]

    # 迭代100次来寻找最优超参数
    optimization_trial = 100
    val_result = {}
    train_result = {}

    for _ in range(optimization_trial):
        # 在指定的搜索范围随机对L2正则化强度和学习率进行采样
        weight_decay = 10 ** np.random.uniform(-8, -4)
        lr = 10 ** np.random.uniform(-6, -2)

        # 利用本次迭代采样得到的两个超参数进行训练,得到验证集和训练集上的准确率
        val_acc_list, train_acc_list = train(train_x, train_label, val_x,
                                             val_label, lr, weight_decay)
        print("val acc: " + str(val_acc_list[-1]) + " | lr: " + str(lr) +
              " | weight decay: " + str(weight_decay))
        # 把本次迭代的结果保存起来,记录所用的超参数以及测试结果
        key = "lr: " + str(lr) + ", weight decay: " + str(weight_decay)
        val_result[key] = val_acc_list
        train_result[key] = train_acc_list

    print("\n========== Hyper-Parameter Optimization Result ===========")
    draw(val_result, train_result)
示例#5
0
from dataset.mnist import load_mnist
from common.multi_layer_net import MultiLayerNet
from common.util import shuffle_dataset
from common.trainer import Trainer

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# using small size dataset (1/500) for fast training
# you can change this larger (e.g. 1000, 2000) if your computer is slow
x_train = x_train[:500]
t_train = t_train[:500]

# splitting validation data
validation_rate = 0.20
validation_num = int(x_train.shape[0] * validation_rate)
x_train, t_train = shuffle_dataset(x_train, t_train)
x_val = x_train[:validation_num]
t_val = t_train[:validation_num]
x_train = x_train[validation_num:]
t_train = t_train[validation_num:]


def __train(lr, weight_decay, epocs=50):
    network = MultiLayerNet(input_size=784,
                            hidden_size_list=[100, 100, 100, 100, 100, 100],
                            output_size=10,
                            weight_decay_lambda=weight_decay)
    trainer = Trainer(network,
                      x_train,
                      t_train,
                      x_val,
示例#6
0
import matplotlib.pyplot as plt
from common.multi_layer_net import MultiLayerNet
from dataset.mnist import load_mnist
from common.util import shuffle_dataset

# load data
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# data reduction
x_train = x_train[:500]
t_train = t_train[:500]

# create validation data
validation_rate = 0.20
validation_num = int(x_train.shape[0] * validation_rate)
x_train, t_test = shuffle_dataset(x_train, t_train)
x_val = x_train[:validation_num]
t_val = t_train[:validation_num]
x_train = x_train[validation_num:]
t_train = t_train[validation_num:]

# training
def __train(lr, weight_decay, epocs=50):
	network = MultiLayerNet(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100],
							output_size = 10, weight_decay_lambda=weight_decay)
	trainer = Trainer(network, x_train, t_train, x_val, t_val,
					  epochs=epocs, mini_batch_size=100,
					  optimizer='sgd', optimizer_param={'lr': lr}, verbose=False)
	trainer.train()

	return trainer.test_acc_list, trainer.train_acc_list
示例#7
0
def loadData():
    # 1.훈련 데이터 만들기
    datasetx = np.zeros((120000, 1))  #입력 데이터 24000*5개
    datasety = np.zeros((24000, 7))  #출력 데이터 24000개
    temp = np.array((120000, 1))  #입력 데이터가 저장될 임시 공간

    # 2.훈련 출력 데이터 만들기

    data_zero = np.ones((2000, 5))
    data_one = np.ones((2000, 5))
    data_two = np.ones((2000, 5))
    data_two2 = np.ones((2000, 5))
    data_three = np.zeros((2000, 5))
    data_three2 = np.zeros((2000, 5))
    data_four = np.zeros((2000, 5))
    data_five = np.zeros((2000, 5))
    #data_six = np.zeros((2000,5))

    for i in range(0, 2000):
        data_one[i][1] = 0
        data_two[i][1] = 0
        data_two[i][2] = 0
        data_two2[i][0] = 0
        data_two2[i][1] = 0
        data_three[i][0] = 1
        data_three[i][4] = 1
        data_three2[i][3] = 1
        data_three2[i][4] = 1
        data_four[i][0] = 1

    data_zero = np.reshape(data_zero, (10000, 1))
    data_one = np.reshape(data_one, (10000, 1))
    data_two = np.reshape(data_two, (10000, 1))
    data_two2 = np.reshape(data_two2, (10000, 1))
    data_three = np.reshape(data_three, (10000, 1))
    data_three2 = np.reshape(data_three2, (10000, 1))
    data_four = np.reshape(data_four, (10000, 1))
    data_five = np.reshape(data_five, (10000, 1))

    temp = np.concatenate([
        data_zero, data_one, data_two, data_two2, data_three, data_three2,
        data_four, data_five
    ])

    for i in range(0, 80000):
        if temp[i] == 1:
            datasetx[i] = randint(801, 930)
        else:
            datasetx[i] = randint(700, 800)

    for i in range(80000, 120000):
        datasetx[i] = randint(700, 930)

    datasetx = np.reshape(datasetx, (24000, 5))

    for i in range(0, 16000):
        if i < 2000:
            datasety[i][0] = 1
        elif i < 4000:
            datasety[i][1] = 1
        elif i < 8000:
            datasety[i][2] = 1
        elif i < 12000:
            datasety[i][3] = 1
        elif i < 14000:
            datasety[i][4] = 1
        elif i < 16000:
            datasety[i][5] = 1

    for j in range(16000, 24000):
        k = 0
        if (datasetx[j][k] >
                800) and (datasetx[j][k + 1] >
                          800) and datasetx[j][k + 2] > 800 and datasetx[j][
                              k + 3] > 800 and datasetx[j][k + 4] > 800:
            datasety[j][0] = 1
        elif (datasetx[j][k] > 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] > 800 and datasetx[j][k + 3] > 800 and \
                datasetx[j][k + 4] > 800:
            datasety[j][1] = 1
        elif (datasetx[j][k] > 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] <= 800 and datasetx[j][k + 3] > 800 and \
                datasetx[j][k + 4] > 800:
            datasety[j][2] = 1
        elif (datasetx[j][k] <= 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] > 800 and datasetx[j][k + 3] > 800 and \
                datasetx[j][k + 4] > 800:
            datasety[j][2] = 1
        elif (datasetx[j][k] > 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] <= 800 and datasetx[j][k + 3] <= 800 and \
                datasetx[j][k + 4] > 800:
            datasety[j][3] = 1
        elif (datasetx[j][k] <= 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] <= 800 and datasetx[j][k + 3] > 800 and \
                datasetx[j][k + 4] > 800:
            datasety[j][3] = 1
        elif (datasetx[j][k] > 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] <= 800 and datasetx[j][k + 3] <= 800 and \
                datasetx[j][k + 4] <= 800:
            datasety[j][4] = 1
        elif (datasetx[j][k] <= 800) and (datasetx[j][k + 1] <= 800) and datasetx[j][k + 2] <= 800 and datasetx[j][k + 3] <= 800 and \
                datasetx[j][k + 4] <= 800:
            datasety[j][5] = 1
        else:
            datasety[j][6] = 1

    validation_rate = 0.20
    validation_num = int(datasetx.shape[0] * validation_rate)
    datasetx, datasety = shuffle_dataset(datasetx, datasety)
    #x_val = datasetx[:validation_num]
    #t_val = datasety[:validation_num]
    #datasetx = datasetx[validation_num:]
    #datasety = datasety[validation_num:]

    # 2.검증 데이터 만들기
    testsetx = []
    testsety = []

    #temp1 = []

    filename = "dataset.xlsx"  # 파일명
    book = openpyxl.load_workbook(filename)  # 엑셀파일 book 변수에 저장
    sheet = book.worksheets[0]

    for row in sheet.rows:  # 전체 행에 대하여 반복실행
        testsetx.append(row[0].value)  # 1열 데이터)

    number = int(len(testsetx) / 5)
    testsetx = np.reshape(testsetx, (number, 5))
    testsety = np.zeros((number, 7))

    for j in range(0, number):
        k = 0
        if (testsetx[j][k] >
                800) and (testsetx[j][k + 1] >
                          800) and testsetx[j][k + 2] > 800 and testsetx[j][
                              k + 3] > 800 and testsetx[j][k + 4] > 800:
            testsety[j][0] = 1
        elif (testsetx[j][k] > 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] > 800 and testsetx[j][k + 3] > 800 and \
                testsetx[j][k + 4] > 800:
            testsety[j][1] = 1
        elif (testsetx[j][k] > 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] <= 800 and testsetx[j][k + 3] > 800 and \
                testsetx[j][k + 4] > 800:
            testsety[j][2] = 1
        elif (testsetx[j][k] <= 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] > 800 and testsetx[j][k + 3] > 800 and \
                testsetx[j][k + 4] > 800:
            testsety[j][2] = 1
        elif (testsetx[j][k] > 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] <= 800 and testsetx[j][k + 3] <= 800 and \
                testsetx[j][k + 4] > 800:
            testsety[j][3] = 1
        elif (testsetx[j][k] <= 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] <= 800 and testsetx[j][k + 3] > 800 and \
                testsetx[j][k + 4] > 800:
            testsety[j][3] = 1
        elif (testsetx[j][k] > 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] <= 800 and testsetx[j][k + 3] <= 800 and \
                testsetx[j][k + 4] <= 800:
            testsety[j][4] = 1
        elif (testsetx[j][k] <= 800) and (testsetx[j][k + 1] <= 800) and testsetx[j][k + 2] <= 800 and testsetx[j][k + 3] <= 800 and \
                testsetx[j][k + 4] <= 800:
            testsety[j][5] = 1
        else:
            testsety[j][6] = 1

    testsetx, testsety = shuffle_dataset(testsetx, testsety)

    datasetx = datasetx.astype(dtype=np.int64)
    datasety = datasety.astype(dtype=np.int64)
    testsetx = testsetx.astype(dtype=np.int64)
    testsety = testsety.astype(dtype=np.int64)

    #print(testsetx[0])
    return ((datasetx, datasety), (testsetx, testsety))
示例#8
0
def load_cifar(train_count=50000, test_count=10000, image_data_format="channels_first"):
    K.set_image_data_format(image_data_format)
    (x_train, t_train), (x_test, t_test) = cifar10.load_data()
    (x_train, t_train) = shuffle_dataset(x_train, t_train)
    return (x_train[:train_count], t_train[:train_count]), (x_test[:test_count], t_test[:test_count])
示例#9
0
def cnn_constructor():
    """
    Referenced by https://github.com/oreilly-japan/deep-learning-from-scratch
    common modules referenced there too.
    """

    global network, classes, imsize

    (x_train, t_train), (x_test,
                         t_test), classes = dataset(image_dir="images",
                                                    test_percentage=10,
                                                    validation_percentage=10,
                                                    imsize=imsize)

    x_train = chenneling(x_train)
    x_test = chenneling(x_test)

    train_num = x_train.shape[0]
    test_num = x_test.shape[0]

    x_train, t_train = shuffle_dataset(x_train, t_train)
    x_test, t_test = shuffle_dataset(x_test, t_test)

    net_param = "cnn_params" + str(imsize) + ".pkl"
    if not os.path.exists("params/"):
        os.makedirs("params/")

    # make convolution eural network
    # x_train.shape[1:] returns channel, height, width
    network = ConvNet(input_dim=(x_train.shape[1:]),
                      conv_param={
                          'filter_num': 20,
                          'filter_size': 3,
                          'pad': 0,
                          'stride': 1
                      },
                      hidden_size=32,
                      output_size=classes,
                      weight_init_std=0.001)

    trainer = Trainer(network,
                      x_train,
                      t_train,
                      x_test,
                      t_test,
                      epochs=1,
                      mini_batch_size=FLAGS.batch_size,
                      optimizer='Adam',
                      optimizer_param={'lr': 0.001},
                      evaluate_sample_num_per_epoch=train_num)

    params_loaded = False
    if not os.path.exists("params/"):
        os.makedirs("params/")
    if (os.path.exists("params/" + net_param)):
        network.load_params("params/" + net_param)
        params_loaded = True
        print("\n* Loaded Network Parameters!  -  " + net_param)
    if ((FLAGS.train_epochs > 0) or (params_loaded == False)):
        if (FLAGS.train_epochs <= 0):
            FLAGS.train_epochs = 10
        # Training
        for ep in range(FLAGS.train_epochs):
            trainer.train()
            # Save parameters
            network.save_params("params/" + net_param)

            # plot graphs
            # Grpah 1: Accuracy
            markers = {'train': 'o', 'test': 's', 'loss': 'd'}
            x1 = np.arange(len(trainer.train_acc_list))
            plt.clf()
            plt.plot(x1,
                     trainer.train_acc_list,
                     marker='o',
                     label='train',
                     markevery=1)
            plt.plot(x1,
                     trainer.test_acc_list,
                     marker='s',
                     label='test',
                     markevery=1)
            plt.xlabel("epochs")
            plt.ylabel("accuracy")
            plt.ylim(0, 1.1)
            plt.legend(loc='lower right')
            plt.title("Accuracy")
            now = datetime.now()
            filename = "params/" + now.strftime(
                '%Y%m%d_%H%M%S%f') + "_" + "ep" + ".png"
            plt.savefig(filename)
            #plt.show()

            # Graph 2: Loss
            x2 = np.arange(len(trainer.train_loss_list))
            plt.clf()
            plt.plot(x2,
                     trainer.train_loss_list,
                     marker='o',
                     label='loss',
                     markevery=1)
            plt.xlabel("iter")
            plt.ylabel("loss")
            plt.legend(loc='lower right')
            plt.title("Cross entropy loss")
            now = datetime.now()
            filename = "params/" + now.strftime(
                '%Y%m%d_%H%M%S%f') + "_" + "ep" + ".png"
            plt.savefig(filename)
            #plt.show()
        print("\n* Saved Network Parameters!  -  " + net_param)
import matplotlib.pyplot as plt
from dataset.mnist import load_mnist
from common.multi_layer_net import MultiLayerNet
from common.util import shuffle_dataset
from common.trainer import Trainer

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# 高速化のため訓練データの削減
x_train = x_train[:500]
t_train = t_train[:500]

# 検証データの分離
validation_rate = 0.20
validation_num = x_train.shape[0] * validation_rate
x_train, t_train = shuffle_dataset(x_train, t_train)
x_val = x_train[:validation_num]
t_val = t_train[:validation_num]
x_train = x_train[validation_num:]
t_train = t_train[validation_num:]


def __train(lr, weight_decay, epocs=50):
    network = MultiLayerNet(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100],
                            output_size=10, weight_decay_lambda=weight_decay)
    trainer = Trainer(network, x_train, t_train, x_val, t_val,
                      epochs=epocs, mini_batch_size=100,
                      optimizer='sgd', optimizer_param={'lr': lr}, verbose=False)
    trainer.train()

    return trainer.test_acc_list, trainer.train_acc_list
示例#11
0
import matplotlib.pyplot as plt
from dataset.mnist import load_mnist
from common.multi_layer_net import MultiLayerNet
from common.util import shuffle_dataset
from common.trainer import Trainer

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# 결과를 빠르게 얻기 위해 훈련 데이터를 줄임
x_train = x_train[:500]
t_train = t_train[:500]

# 20%를 검증 데이터로 분할
validation_rate = 0.20  # train 데이터의 20%를 검증 데이터로 따로 빼둔다
validation_num = int(x_train.shape[0] * validation_rate)
x_train, t_train = shuffle_dataset(x_train, t_train)  # 트레인과 정답지는 같이 섞어야한다
x_val = x_train[:validation_num]
t_val = t_train[:validation_num]
x_train = x_train[validation_num:]
t_train = t_train[validation_num:]


def __train(lr, weight_decay, epocs=50):
    network = MultiLayerNet(input_size=784,
                            hidden_size_list=[100, 100, 100, 100, 100, 100],
                            output_size=10,
                            weight_decay_lambda=weight_decay)
    trainer = Trainer(network,
                      x_train,
                      t_train,
                      x_val,
示例#12
0
from common.trainer import Trainer

"""
MNIST 데이터셋으로 검증셋을 이용하여 하이퍼파라미터의 최적화 구현
"""

(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True)

# 결과를 빠르게 얻기 위해 훈련 데이터를 줄임
x_train = x_train[:500]
t_train = t_train[:500]

#20%로 검증 데이터 분할
validation_rate = 0.2
validation_num = int(x_train.shape[0] * validation_rate)
x_train, t_train = shuffle_dataset(x_train, t_train)  #데이터셋을 무작위로 섞어준다.

x_val = x_train[:validation_num]
t_val = t_train[:validation_num]

x_train = x_train[validation_num:] #슬라이싱으로 검증 데이터셋 이후부터 훈련 데이터셋으로 사용
t_train = t_train[validation_num:]

def __train(lr, weight_decay, epocs = 50):
    #네트워크 생성
    network = MultiLayerNet(input_size=784, hidden_size_list=[100, 100, 100, 100, 100, 100],
                            output_size= 10, weight_decay_lambda=weight_decay)

    #훈련 초기화
    trainer = Trainer(network,x_train,t_train,x_test,t_test,epocs,mini_batch_size=100,
                      optimizer='adam', optimizer_param={'lr':lr},verbose=False)