import tensorflow as tf
import tensorboard as tb
from math import *
import time
import serial
import numpy as np
import cv2 as cv
cam = cv.VideoCapture(0)
DataRaw = []
LabelsRaw = []
cv.namedWindow("in",cv.WINDOW_GUI_EXPANDED)
Input = tf.placeholder(tf.float32,(None,120,160,3),"Input")
y = tf.placeholder(tf.int32,(None),"Labels")
with tf.name_scope("Siec"):
hidden1 = tf.layers.conv2d(Input,10,5,strides=(2,2))
hidden2 = tf.layers.conv2d(hidden1,5,3,strides=(2,2))
convert = tf.reshape(hidden2,[-1,28*23*5])
hidden3 = tf.layers.dense(convert,100,activation=tf.nn.elu,name="h2")
hidden4 = tf.layers.dense(hidden3,50,activation=tf.nn.elu,name="h3")
output = tf.layers.dense(hidden4,4,name="out",activation=tf.nn.relu)
with tf.name_scope("Uczenie"):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y,logits=output)
loss = tf.reduce_mean(xentropy)
optimizer = tf.train.GradientDescentOptimizer(0.01)
op = optimizer.minimize(loss)
with tf.name_scope("Ocena"):
correct = tf.nn.in_top_k(output,y,1)
acc = tf.reduce_mean(tf.cast(correct,tf.float32))
init = tf.global_variables_initializer()
fw = tf.summary.FileWriter("Hello",tf.get_default_graph())
lasttime = 0
Learned = False
Key = False
with tf.Session() as sess:
fw.close()
init.run()
while(cam.isOpened()):
ret, frame = cam.read()
if ret == True:
out = cv.pyrDown(frame)
out = cv.pyrDown(out)
cv.imshow('in',out)
if cv.waitKey(1) == 49:
DataRaw.append(out)
LabelsRaw.append(1)
print('A')
Learned = False
Key = True
if cv.waitKey(1) == 50:
DataRaw.append(out)
LabelsRaw.append(2)
print('B')
Learned = False
Key = True
if cv.waitKey(1) == 51:
DataRaw.append(out)
LabelsRaw.append(3)
print('C')
Learned = False
Key = True
if cv.waitKey(1) == 52:
DataRaw.append(out)
LabelsRaw.append(4)
print('D')
Learned = False
Key = True
if len(DataRaw) != 0:
if cv.waitKey(1) == -1:
if Key == True:
lasttime = time.clock()
Key = False
if time.clock() - lasttime > 2:
if not Key and not Learned:
for epoch in range(50):
X = np.array(DataRaw)
Y = np.array(LabelsRaw)
sess.run(op, feed_dict={Input:X, y:Y})
if epoch%10 == 0:
Calc_Acc = acc.eval(feed_dict={Input:X, y:Y})
print("Acc: ", Calc_Acc)
Learned = True
Czy mógłby mi ktoś powiedzieć czemu przy sess.run wywala mi InvalidArgumentError().
Chciałbym sie jeszcze zapytać czy popełniłem jakieś większe błędy nielicząc niepodzielenia zbioru uczącego na na minigrupy.