1

I'm trying to learn Machine Learning, so I'm following a tutorial on how to train a network to recognize digits. I'm at the stage where I've trained the network and I'm trying out some of my own images. However, somehow there is a problem with recognizing the images. I'm not sure what I've done wrong. I've uploaded the images in the same directory folder. You can find the code and errors below.

Errors

img = cv.imread(f'{x}.png')[:,:,0]

TypeError: 'NoneType' object is not subscriptable

I'm also getting this error although it doesn't seem to be affecting anything. Should I be worried about it?

2021-08-04 17:53:03.097193: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-08-04 17:53:03.099366: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):

Code

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units = 10, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs=3)

accuracy, loss = model.evaluate(x_test, y_test)
print(accuracy)
print(loss)

model.save('digits.model')

for x in range(1,6):
    img = cv.imread(f'{x}.png')[:,:,0]
    img= np.array([img])
    plt.imshow(img[0], cmap=plt.cm.binary)
    plt.show()
Zephyr
  • 11,891
  • 53
  • 45
  • 80
Iris S.
  • 11
  • 2
  • This looks like that the filename is invalid in some way, since you're just assuming imread works instead of making sure it found the file. Maybe wrong path, or the filesystem is case sensitive and your filename is wrong. – Donnie Aug 04 '21 at 16:10
  • @Donnie Thanks. I indeed failed the check the filenames. Such an obvious detail now that I look back. Thanks for your help. Do you think I should be worried about that other error? It hasn't seemed to affect anything negatively as of yet. – Iris S. Aug 04 '21 at 18:09
  • The second error is a warning about tensorflow not finding the correct cuda installation (see https://stackoverflow.com/questions/59823283/could-not-load-dynamic-library-cudart64-101-dll-on-tensorflow-cpu-only-install ) if this installation is not complete GPU acceleration will not function poperly. If you don't have a GPU (or need the acceleration) it will fall back to computing on the CPU. – amo-ej1 Aug 04 '21 at 20:14

1 Answers1

0

I think it will solve your problem :

How to solve the 'NoneType' object is not subscriptable is opencv

Sahil
  • 3
  • 5