Я пытаюсь реализовать собственный детектор для LeNet5 в наборе данных MNIST. Для обучения я использовал ноутбук I python от RaghavPrabhu: https://github.com/RaghavPrabhu/mnist_tf/blob/master/MNIST_TF_v1.ipynb
import ...
path = r'C:/somefolders/Projekt/MNIST/mnist_images/'
maxImages = 100
imagesize = 32
channels = 1
i=0
rightP = 0
wrongP = 0
imgs = np.ndarray(shape=(maxImages, imagesize, imagesize, channels), dtype=np.float32)
#imgs = imgs.reshape((-1,imagesize,imagesize,channels)).astype(np.float32)
#imgs = []
labelRes = []
# create placeholders
x = tf.placeholder(tf.float32, shape=[None,32,32,1])
y_ = tf.placeholder(tf.int32, (None))
# load CNN
logits = ln.LeNet_5(x)
# load tesimages
for fname in os.listdir(path):
print('Open Image : ', path + fname)
i = i + 1
img = plt.imread(path + fname)
# pad and normalize image
img = np.pad(img, [(2, ),(2, )], mode='constant', constant_values = 255)
img = np.array(img, dtype="float32") / 255
img = array(img).reshape(1,imagesize,imagesize,channels)
#plt.imshow(img, cmap='Greys_r') # to show actual image
#print('Image has a shape of : ', img.shape, ' Datatype : ', img.dtype, ' Dimensions : ', img.ndim)
# obtain labels from filename e.g. "0_[7].jpg"
m = re.search(r"(?<=\[)(.*)(?=\])", fname)
labelRes.append((int)(m.group(1)))
np.append(img, imgs)
if i >= maxImages:
break
print (labelRes)
path = os.getcwd()
path = path.replace(os.sep, '/')
saver = tf.train.Saver()
with tf.Session() as sess:
# Restore variables from disk.
saver.restore(sess, path + "/tmp/lenet.ckpt")
start = time.time()
Z = logits.eval(feed_dict={x: imgs})
y_pred = np.argmax(Z, axis=1)
print(len(y_pred))
i=0
for label in y_pred:
if labelRes[i] == label:
rightP = rightP + 1
elif labelRes[i] != label:
wrongP = wrongP + 1
print('Label : ', labelRes[i], 'Prediction: ', label)
acc = rightP / (rightP+wrongP)
i = i + 1
end = time.time()
duration = end - start
print('accuracy = ', acc, ' accurate predictions: ', rightP, ' wrong predictions: ', wrongP,
' Inference time : ', duration, 'Time/Image : ', duration/maxImages)
Но после самого простого теста с тем же набором данных MNIST я понял, что мои прогнозы очень неправильно. Выходные данные (я напечатал только первые четыре прогноза, первые два верны, но после этого все прогнозы неверны):
Label : 7 Prediction: 7
Label : 5 Prediction: 5
Label : 1 Prediction: 0
Label : 1 Prediction: 0
...
accuracy = 0.12 accurate predictions: 12 wrong predictions: 88 Inference time : 0.06399726867675781 Time/Image : 0.0006399726867675781
Я думаю, что моя ошибка заключается в том, что входные изображения не правильные форма, но я старался изо всех сил, чтобы сформировать их, как данные тренировки, может быть, кто-то видит ошибку, это мне очень поможет. Если вам нужна дополнительная информация, не стесняйтесь спрашивать меня.
Спасибо за ваше время.