def test():
    
    network = SimpleConvNet(input_dim=(3,128,128), 
                        conv_param = {'filter_num': [32,32], 'filter_size': [3,3], 'pad': [1,1], 'stride': [1,1]},
                        hidden_size=100, output_size=12, weight_init_std=0.01)

    network.load_params("params.pkl")

    data_transform = transforms.Compose([
        transforms.RandomResizedCrop(128),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])

    path = join('dataset\plant-seedlings-classification', 'test')

    sample_submission = pd.read_csv('sample_submission.csv')
    submission = sample_submission.copy()
    for i, f in enumerate(sample_submission['file']):
        print(join(path, f))
        image = Image.open(join(path, f))

        #convert image
        if image.mode != "RGB":
            image = image.convert("RGB")
        image = data_transform(image)
        image = np.expand_dims(image, axis=0)
        
        output = network.predict(image)
        output = np.argmax(output, axis=1)
        
        submission['species'][i] = one_hot_key[output[0]]

    submission.to_csv('submission.csv', index=False)
Example #2
0
def main():
    (x_train, t_train), (x_test, t_test) = load_mnist(flatten=False)

    # 処理に時間のかかる場合はデータを削除
    #x_train, t_train = x_train[:5000], t_train[:5000]
    #x_test, t_test = x_test[:1000], t_test[:1000]

    max_epochs = 20

    network = SimpleConvNet(input_dim=(1, 28, 28),
                            conv_param={
                                'filter_num': 30,
                                'filter_size': 5,
                                'pad': 0,
                                'stride': 1
                            },
                            hidden_size=100,
                            output_size=10,
                            weight_init_std=0.01)

    trainer = Trainer(network,
                      x_train,
                      t_train,
                      x_test,
                      t_test,
                      epochs=max_epochs,
                      mini_batch_size=100,
                      optimizer='Adam',
                      optimizer_param={'lr': 0.001},
                      evaluate_sample_num_per_epoch=1000)
    trainer.train()

    # パラメータの保存
    network.save_params("params.pkl")
    print("Saved Network Parameters!")

    # グラフの描画
    markers = {'train': 'o', 'test': 's'}
    x = np.arange(max_epochs)
    plt.plot(x, trainer.train_acc_list, marker='o', label='train', markevery=2)
    plt.plot(x, trainer.train_acc_list, marker='s', label='test', markevery=2)
    plt.xlabel = ("epochs")
    plt.ylabel = ("accuracy")
    plt.ylim(0, 1.0)
    plt.legend(loc='lower right')
    plt.show()
Example #3
0
@author: ghqls
"""

import numpy as np
import matplotlib.pyplot as plt
from simple_convnet import SimpleConvNet


def filter_show(filters, nx=8, margin=3, scale=10):
    FN, C, FH, FW = filters.shape
    ny = int(np.ceil(FN / nx))

    fig = plt.figure()
    fig.subplots_adjust(left=0,
                        right=1,
                        bottom=0,
                        top=1,
                        hspace=0.05,
                        wspace=0.05)

    for i in range(FN):
        ax = fig.add_subplot(ny, nx, i + 1, xticks=[], yticks=[])
        ax.imshow(filters[i, 0], cmap=plt.cm.gray_r, interpolation='nearest')
    plt.show()


network = SimpleConvNet()
filter_show(network.params['W1'])

network.load_params('params.pkl')
filter_show(network.params['W1'])
Example #4
0
 @FileName: gradient_check
 @Desc:  
 @Author: yuzhongchun
 @Date: 2019-01-21 22:40
 @Last Modified by: yuzhongchun
 @Last Modified time: 2019-01-21 22:40
"""

import numpy as np
from simple_convnet import SimpleConvNet

network = SimpleConvNet(input_dim=(1, 10, 10),
                        conv_param={
                            'filter_num': 10,
                            'filter_size': 3,
                            'pad': 0,
                            'stride': 1
                        },
                        hidden_size=10,
                        output_size=10,
                        weight_init_std=0.01)

X = np.random.rand(100).reshape((1, 1, 10, 10))
T = np.array([1]).reshape((1, 1))

grad_num = network.numerical_gradient(X, T)
grad = network.gradient(X, T)

for key, val in grad_num.items():
    print(key, np.abs(grad_num[key] - grad[key]).mean())
# 処理に時間のかかる場合はデータを削減
#train_mask = np.random.choice(x_train.shape[0], 5000)
#x_train = x_train[train_mask]
#t_train = t_train[train_mask]
#test_mask = np.random.choice(x_test.shape[0], 1000)
x_test = x_test[:1000]
t_test = t_test[:1000]

t_test = cp.array(t_test, np.float32)

network = SimpleConvNet(input_dim=(3, 32, 32),
                        conv_param={
                            'filter_num': (32, 32, 64),
                            'filter_size': 3,
                            'pad': 1,
                            'stride': 1
                        },
                        hidden_size=512,
                        output_size=10,
                        weight_init_std=0.01)

# パラメータの復帰
network.load_params("params.pkl")
print("Loaded Network Parameters!")

start = time.time()
test_acc = network.accuracy(x_test, t_test)
elapsed_time = time.time() - start
print("=== " + "test acc:" + str(test_acc) + " ===")
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")
#讀取數據
x_data, t_data = load_plant_seedlings()
#減少數據量及運行時間
#x_train, t_train = x_train[:5000], t_train[:5000]
#x_test, t_test = x_test[:1000], t_test[:1000]

x_train, x_test, t_train, t_test = train_test_split(x_data, t_data, test_size=0.2)

print(x_data.shape)
print(x_train.shape)
print(x_test.shape)

max_epochs = 40

network = SimpleConvNet(input_dim=(3,128,128), 
                        conv_param = {'filter_num': [32,32], 'filter_size': [3,3], 'pad': [1,1], 'stride': [1,1]},
                        hidden_size=100, output_size=12, weight_init_std=0.01)
                        
trainer = Trainer(network, x_train, t_train, x_test, t_test,
                  epochs=max_epochs, mini_batch_size=100,
                  optimizer='Adam', optimizer_param={'lr': 0.001},
                  evaluate_sample_num_per_epoch=200)
trainer.train()

#保存參數
network.save_params("params.pkl")
print("Saved Network Parameters!")

#繪圖
markers = {'train': 'o', 'validation': 's'}
x = np.arange(len(trainer.train_acc_list))
Example #7
0
import numpy as np
from simple_convnet import SimpleConvNet
from layers import *

# Convolution Layer Test

input_size=4
a = np.array(range(input_size**2)).reshape(1,1,input_size,input_size)

print("Input:",a)
print(a.shape)


network = SimpleConvNet(input_dim=(1,input_size,input_size),
                        conv_param = {'filter_num': 2, 'filter_size': 2, 'pad': 0, 'stride': 1},
                        hidden_size=10, output_size=3, weight_init_std=0.01)
print (network.params['W1'])
print (network.params['W1'].shape)

c_net = Convolution(network.params['W1'],network.params['b1'],1,0)
out_a = c_net.forward(a)

print("Result:",out_a)
print(out_a.shape)
Example #8
0
for size in range(22, 23, 2):
    tmp_acc_list.clear()
    print("size ---> ", size)
    print("Now calculating...... ")
    for j in range(1):
        input_data_dim = (3, size, size)
        (x_train, t_train), (x_test,
                             t_test) = load_data_list(size, load_num,
                                                      train_data_num,
                                                      test_data_num)
        network = SimpleConvNet(PRE_DATA=True,
                                input_dim=input_data_dim,
                                conv_param={
                                    "filter_num": 30,
                                    "filter_size": 5,
                                    "pad": 0,
                                    "stride": 1
                                },
                                hidden_size=100,
                                output_size=2,
                                weight_init_std=0.01)

        trainer = Trainer(network,
                          x_train,
                          t_train,
                          x_test,
                          t_test,
                          epochs=max_epochs,
                          mini_batch_size=100,
                          optimizer="Adam",
                          optimizer_param={"lr": 0.001},
Example #9
0
def main():
    """
    Main Function.
    :return:
    """
    machine = "local"
    model_type = "simple"
    optimizer_type = "adam"
    if len(sys.argv) > 1:
        machine = sys.argv[1]
    if len(sys.argv) > 2:
        model_type = sys.argv[2]
    if len(sys.argv) > 3:
        optimizer_type = sys.argv[3]

    if machine == 'local':
        print("Machine:", machine)
        gpu_mem = 1800
    elif machine == 'cade':
        print("Machine:", machine)
        gpu_mem = 3800
    define_gpu(minimum_memory_mb=gpu_mem)

    train_data = BreastPathQDataSet(split="train")
    val_data = BreastPathQDataSet(split="val")
    test_data = BreastPathQDataSet(split="test")

    # for epoch in epochs:
    #     for batch_size in batch_size:
    #         for lr in learning_rates:
    #             optimizer = torch.optim.Adam(model.parameters(), lr=lr)
    #             if len(sys.argv) > 3:
    #                 if sys.argv[3] == 'sgd':
    #                     optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=0.9, nesterov=True)
    #             print("Max Epochs:", epoch, "Learning Rate:", lr, "Batch Size:", batch_size)
    #             trained_model = model.train(epoch, batch_size, criterion, optimizer)

    losses_figure = plt.figure()
    losses_figure_ax = losses_figure.add_subplot(111)

    scores_figure = plt.figure()
    scores_figure_ax = scores_figure.add_subplot(111)

    epochs = 20
    learning_rates = [
        random.uniform(0.01, 0.0001),
        random.uniform(0, 0.0001),
        random.uniform(0, 0.0001)
    ]

    criterion = torch.nn.BCEWithLogitsLoss()
    # criterion = torch.nn.MSELoss()
    utils = Utils(train_data, val_data, test_data)

    batch_size = [8, 16]
    if model_type == 'resnet':
        batch_size = [12, 24]
    if model_type == 'improved':
        epochs = 20

    for batch in batch_size:
        for lr in learning_rates:
            if model_type == 'simple':
                print("Model:", model_type)
                model = SimpleConvNet()
            elif model_type == 'resnet':
                print("Model:", model_type)
                resnet = models.resnet18(pretrained=True)
                resnet.fc = torch.nn.Linear(in_features=512, out_features=1)
                model = resnet
            elif model_type == 'improved':
                print("Model:", model_type)
                model = ImprovedConvNet()

            if optimizer_type == 'adam':
                print("Optimizer:", optimizer_type)
                optimizer = torch.optim.Adam(model.parameters(), lr=lr)
            elif optimizer_type == 'sgd':
                print("Optimizer:", optimizer_type)
                optimizer = torch.optim.SGD(model.parameters(),
                                            lr=lr,
                                            momentum=0.9,
                                            nesterov=True)
            print()

            print("Learning Rate:", lr)
            trained_model, losses, scores = utils.train(
                model, epochs, batch, criterion, optimizer)
            lr_string = '%.2E' % Decimal(str(lr))
            label = "LR: " + lr_string + ", Batch: " + str(batch)
            losses_figure_ax.plot(range(0, len(losses)), losses, label=label)
            scores_figure_ax.plot(range(0, len(scores)), scores, label=label)

            test_score = utils.evaluate(train_data)
            print("Test Score:", test_score)

    losses_figure_ax.set_title("Losses vs. Epochs (" + model_type + "+" +
                               optimizer_type + ")")
    losses_figure_ax.set_xlabel("Epochs")
    losses_figure_ax.set_ylabel("Losses")
    losses_figure_ax.legend()
    losses_figure.savefig("./graphs/" + model_type + "_" + optimizer_type +
                          "_Losses_" + str(epochs) + "e.png")

    scores_figure_ax.set_title("Scores vs. Epochs")
    scores_figure_ax.set_xlabel("Epochs")
    scores_figure_ax.set_ylabel("Scores")
    scores_figure_ax.legend()
    scores_figure.savefig("./graphs/" + model_type + "_" + optimizer_type +
                          "_Scores_" + str(epochs) + "e.png")
Example #10
0
# coding: utf-8
# ch07ディレクトリに置く
import sys, os, time, subprocess
sys.path.append(os.pardir)
import numpy as np
from common.layers import *
from simple_convnet import SimpleConvNet
from PIL import Image

# 畳み込みネットワークの読み込みと、学習済パラメータの読み込み
network = SimpleConvNet()
network.load_params("params.pkl")

devnull = open('os.devnull', 'w')
ipaddr = subprocess.check_output(["hostname", "-I"]).decode("utf-8").strip()
commd = "http://"+ipaddr+":9000/?action=snapshot"

while True:
    time.sleep(3)

    # カメラの画像を取り込む
    subprocess.run(["wget", "-O", "photo.jpg", commd], stdout=devnull, stderr=subprocess.STDOUT)

    # 画像の前処理(を28x28に整形、白黒対応、二値化)
    img = Image.open("photo.jpg").convert('L')
    img28 = np.array(img.resize((28,28)))

    img.save('img.jpg')
    pil_img_gray = Image.fromarray(np.uint8(img28)).save('img28.jpg')

    thresh = np.median(img28)
Example #11
0

def filter_show(filters, nx=8, margin=3, scale=10):
    """
    c.f. https://gist.github.com/aidiary/07d530d5e08011832b12#file-draw_weight-py
    """
    FN, C, FH, FW = filters.shape
    ny = int(np.ceil(FN / nx))

    fig = plt.figure()
    fig.subplots_adjust(left=0,
                        right=1,
                        bottom=0,
                        top=1,
                        hspace=0.05,
                        wspace=0.05)

    for i in range(FN):
        ax = fig.add_subplot(ny, nx, i + 1, xticks=[], yticks=[])
        ax.imshow(filters[i, 0], cmap=plt.cm.gray_r, interpolation='nearest')
    plt.show()


network = SimpleConvNet()
# ランダム初期化後の重み
##filter_show(network.params['W1'])

# 学習後の重み
network.load_params("deep_convnet_params9.pkl")
filter_show(network.params['W1'])
# coding: utf-8
import numpy as np
from simple_convnet import SimpleConvNet

network = SimpleConvNet(input_dim=(1,10, 10), 
                        conv_param = {'filter_num':10, 'filter_size':3, 'pad':0, 'stride':1},
                        hidden_size=10, output_size=10, weight_init_std=0.01)

X = np.random.rand(100).reshape((1, 1, 10, 10))
T = np.array([1]).reshape((1,1))

grad_num = network.numerical_gradient(X, T)
grad = network.gradient(X, T)

for key, val in grad_num.items():
    print(key, np.abs(grad_num[key] - grad[key]).mean())
Example #13
0
# データの読み込み
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=False)

# 処理に時間のかかる場合はデータを削減
x_train, t_train = x_train[:5000], t_train[:5000]
x_test, t_test = x_test[:1000], t_test[:1000]

# ハイパーパラメータの設定
max_epochs = 20
batch_size = 50

model = SimpleConvNet(input_dim=(1, 28, 28),
                      conv_param={
                          'filter_num': 30,
                          'filter_size': 5,
                          'pad': 0,
                          'stride': 1
                      },
                      hidden_size=100,
                      output_size=10,
                      weight_init_std=0.01)
optimizer = AdaGrad(lr=0.001)
trainer = Trainer(model, optimizer)
trainer.fit(x_train,
            t_train,
            x_test,
            t_test,
            max_epochs,
            batch_size,
            eval_interval=10)

# グラフの描画

def filter_show(filters, nx=8):
    """
    c.f. https://gist.github.com/aidiary/07d530d5e08011832b12#file-draw_weight-py
    """
    FN, C, FH, FW = filters.shape
    ny = int(np.ceil(FN / nx))

    fig = plt.figure()
    fig.subplots_adjust(left=0,
                        right=1,
                        bottom=0,
                        top=1,
                        hspace=0.05,
                        wspace=0.05)

    for i in range(FN):
        ax = fig.add_subplot(ny, nx, i + 1, xticks=[], yticks=[])
        ax.imshow(filters[i, 0], cmap=plt.cm.gray_r, interpolation='nearest')
    plt.show()


network = SimpleConvNet()
# Weights after random initialization
filter_show(network.params['W1'])

# Weight after training
network.load_params("params.pkl")
filter_show(network.params['W1'])
Example #15
0
#  coding: utf-8

import numpy as np
import matplotlib.pyplot as plt
from simple_convnet import SimpleConvNet
from trainer import Trainer
from mnist import load_mnist

(x_train, t_train), (x_test, t_test)=load_mnist(flatten=False)

x_train, t_train = x_train[:5000], t_train[:5000]
x_test, t_test = x_test[:1000], t_test[:1000]
max_epochs = 20

network = SimpleConvNet(input_dim=(1,28,28),
                        conv_param={"filter_num": 30,"filter_size": 5,"pad": 0,"stride": 1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)

trainer = Trainer(network, x_train, t_train, x_test, t_test,
                epochs=max_epochs, mini_batch_size=100,
                optimizer="Adam", optimizer_param={"lr":0.001},
                evaluate_sample_num_per_epoch=1000)
trainer.train()

#パラメータの保存
network.save_params("params.pkl")
print("Saved Network Parameters!")

#グラフの描画
markers = {"train":"o", "test":"s"}
x=np.arange(max_epochs)
    """
    c.f. https://gist.github.com/aidiary/07d530d5e08011832b12#file-draw_weight-py
    """
    FN, C, FH, FW = filters.shape
    ny = int(np.ceil(show_num / nx))

    fig = plt.figure()
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)

    for i in range(show_num):
        ax = fig.add_subplot(4, 4, i+1, xticks=[], yticks=[])
        ax.imshow(filters[i, 0], cmap=plt.cm.gray_r, interpolation='nearest')


network = SimpleConvNet(input_dim=(1,28,28), 
                        conv_param = {'filter_num':30, 'filter_size':5, 'pad':0, 'stride':1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)

# 学習後の重み
network.load_params("params.pkl")

filter_show(network.params['W1'], 16)

img = imread('../dataset/lena_gray.png')
img = img.reshape(1, 1, *img.shape)

fig = plt.figure()

w_idx = 1

for i in range(16):
from simple_convnet import SimpleConvNet
from common.functions import *

# データの読み込み
(x_train, t_train), (x_test, t_test) = load_cifar10(normalize=False, flatten=False)

#x_train = x_train[:1000]
#t_train = t_train[:1000]

x_train = x_train * 2.0 - 255
x_test = x_test * 2.0 - 255

batch_size=100

network = SimpleConvNet(input_dim=(3,32,32),
                        conv_param = {'filter_num': (32, 32, 64), 'filter_size': 3, 'pad': 1, 'stride': 1},
                        hidden_size=512, output_size=10, weight_init_std=0.01)

# パラメータの復帰
network.load_params("params.pkl")
print("Loaded Network Parameters!")

tt_array=np.empty((0,10),np.float32)

for i in range(int(x_train.shape[0] / batch_size)):
    tx = x_train[i*batch_size:(i+1)*batch_size]
    tt = network.predict(tx, train_flg=False)
    tt = softmax(tt).get()
    tt_array=np.concatenate((tt_array,tt),axis=0)        

#tt_array=tt_array.reshape(-1,10)
Example #18
0
x_test = x_test * 2.0 - 255

if os.path.exists("ttarray.pkl"):
    with open("ttarray.pkl", 'rb') as f:
        t_train = pickle.load(f)
        print("Loaded Teacher array!")

# 処理に時間のかかる場合はデータを削減 
#train_mask = np.random.choice(x_train.shape[0], 3000)
#x_train = x_train[train_mask]
#t_train = t_train[train_mask]

max_epochs = 25

network = SimpleConvNet(input_dim=(3,32,32),
                        conv_param = {'filter_num': (32, 32, 64), 'filter_size': 3, 'pad': 1, 'stride': 1},
                        hidden_size=512, output_size=10, weight_init_std=0.01)
                        
trainer = Trainer(network, x_train, t_train, x_test, t_test,
                  epochs=max_epochs, mini_batch_size=100,
                  optimizer='Adam', optimizer_param={'lr': 0.001},
                  evaluate_sample_num_per_epoch=1000, early_stopping=10)
start = time.time()
trainer.train()
elapsed_time = time.time() - start
print ("elapsed_time:{0}".format(elapsed_time) + "[sec]")

# パラメータの保存
network.save_params("params.pkl")
print("Saved Network Parameters!")
Example #19
0
                        right=1,
                        bottom=0,
                        top=1,
                        hspace=0.05,
                        wspace=0.05)

    for i in range(show_num):
        ax = fig.add_subplot(4, 4, i + 1, xticks=[], yticks=[])
        ax.imshow(filters[i, 0], cmap=plt.cm.gray_r, interpolation='nearest')


network = SimpleConvNet(input_dim=(1, 28, 28),
                        conv_param={
                            'filter_num': 30,
                            'filter_size': 5,
                            'pad': 0,
                            'stride': 1
                        },
                        hidden_size=100,
                        output_size=10,
                        weight_init_std=0.01)

# 学习后的权重
network.load_params("params.pkl")

filter_show(network.params['W1'], 16)

img = imread('../dataset/lena_gray.png')
img = img.reshape(1, 1, *img.shape)

fig = plt.figure()
import matplotlib.pyplot as plt
from dataset.mnist import load_mnist
from simple_convnet import SimpleConvNet
from common.trainer import Trainer

# データの読み込み
(x_train, t_train), (x_test, t_test) = load_mnist(flatten=False)

# 処理に時間のかかる場合はデータを削減 
#x_train, t_train = x_train[:5000], t_train[:5000]
#x_test, t_test = x_test[:1000], t_test[:1000]

max_epochs = 20

network = SimpleConvNet(input_dim=(1,28,28), 
                        conv_param = {'filter_num': 30, 'filter_size': 5, 'pad': 0, 'stride': 1},
                        hidden_size=100, output_size=10, weight_init_std=0.01)
                        
trainer = Trainer(network, x_train, t_train, x_test, t_test,
                  epochs=max_epochs, mini_batch_size=100,
                  optimizer='Adam', optimizer_param={'lr': 0.001},
                  evaluate_sample_num_per_epoch=1000)
trainer.train()

# パラメータの保存
network.save_params("params.pkl")
print("Saved Network Parameters!")

# グラフの描画
markers = {'train': 'o', 'test': 's'}
x = np.arange(max_epochs)