#-*-coding:utf-8-*-
import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
import os
import sys
import cv2
import numpy as np
import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
import nnabla.solvers as S
import nnabla.initializer as I
from nnabla.utils.data_iterator import data_iterator_simple
import os
import numpy as np
NPZ = "data/img2train_data_test.npz"
param_file = "nnabla_parameters.h5"
データセットの読み込みはこんな感じで実装
#Read .npz File
def load_data(npz, input="img", teacher="img_test"):
print("loading dataset for training")
#loading data from NPZ file
with np.load(npz) as data:
tmp_train = data[input]
tmp_train_label = data[teacher]
print(tmp_train.shape)
print(tmp_train_label.shape)
return tmp_train, tmp_train_label
#Loading Data
def data_iterator_my_dateset(train, teach, batch_size=8, shuffle=False, rng=None):
def load_func(index):
"""Loading an image and its label"""
img = train[index]
label = teach[index]
return img, label
return data_iterator_simple(load_func, train.shape[0], batch_size, shuffle, rng, with_file_cache=False)
#Define network
def network(x):
with nn.parameter_scope("cnn"):
with nn.parameter_scope("conv0"):
h = F.relu(PF.convolution(x, 6, (2,2)))
with nn.parameter_scope("deconv0"):
h = F.relu(PF.deconvolution(h, 3, (2,2)))
with nn.parameter_scope("conv1"):
h = F.relu(PF.convolution(x, 6, (4,8), stride=(2,4)))
with nn.parameter_scope("conv2"):
h = F.relu(PF.convolution(h, 16, (9,9), stride=(3,3)))
with nn.parameter_scope("deconv2"):
h = F.relu(PF.deconvolution(h, 16, (9,9), stride=(3,3)))
with nn.parameter_scope("deconv1"):
h = PF.deconvolution(h, 6, (4,8), stride=(2,4))
return h
#Define Training Function
def training(xt,tt,data,loss,steps):
solver = S.Adam()
solver.set_parameters(nn.get_parameters()) # Set parameter variables to be updatd.
for i in range(steps):
xt.d, tt.d = data.next()
solver.zero_grad() # Initialize gradients of all parameters to zero.
loss.forward()
loss.backward()
solver.update()
if i % 100 == 0: # Print for each 10 iterations
print(str(i) +":" + str(np.mean(loss.d)))
try:
#Loading Dataset
train, teach = load_data(NPZ,"img","img_test")
data = data_iterator_my_dateset(train, teach, batch_size=30, shuffle=True)
#Set Params to network
nn.clear_parameters()
img, label = data.next()
x = nn.Variable(img.shape)
y = network(x)
t = nn.Variable(label.shape)
loss = loss_func(y, t)
#Search if Params file or not
if os.path.exists(param_file) == True:
yn = input("parameter is exist! use this? y/n : ")
if yn == "Y" or yn == "y":
nn.load_parameters(param_file)
#Training
training(x,t,data,loss,500)
except:
import traceback
traceback.print_exc()
finally:
#Save Params
nn.save_parameters(param_file)
input(">>")
#-*-coding:utf-8-*-
import matplotlib.font_manager as fm
fonts = fm.findSystemFonts()
for f in fonts:
font = fm.FontProperties(fname=f)
print(font.get_name())
input(">")
今回、ArduinoとRaspberry Piの通信部分はUSB経由のUARTにしたので、そこの配線は不要でした。(というよりArduino Pro MicroのUARTピンがうまく使いこなせなかったOTL。)
モータードライバは、i2Cでモータの電流が制御できるDRV8830を二つ使用しました。秋月で取り扱っているのもよかったですね。モータドライバを二つ乗せたのは、今後別のことにも転用できるようにするためです。