Ошибка:Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 7

nordslomin

Новичок
Пользователь
Май 8, 2020
2
0
1
20
Windows 10 Pro x64

Python 3.8

absl-py 0.9.0
astunparse 1.6.3
attrs 19.3.0
beautifulsoup4 4.9.0
bleach 3.1.5
cachetools 4.1.0
certifi 2020.4.5.1
chardet 3.0.4
click 7.1.2
colorama 0.4.3
cycler 0.10.0
decorator 4.4.2
defusedxml 0.6.0
entrypoints 0.3
gast 0.3.3
google-auth 1.14.2
google-auth-oauthlib 0.4.1
google-pasta 0.2.0
grpcio 1.28.1
gTTS 2.1.1
gTTS-token 1.1.3
h5py 2.10.0
idna 2.9
ipykernel 4.6.1
ipython 5.5.0
ipython-genutils 0.2.0
ipywidgets 7.5.1
Jinja2 2.11.2
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 6.1.3
jupyter-console 6.1.0
jupyter-core 4.6.3
Keras 2.3.1
Keras-Applications 1.0.8
Keras-Preprocessing 1.1.0
kiwisolver 1.2.0
Markdown 3.2.1
MarkupSafe 1.1.1
matplotlib 3.2.1
mistune 0.8.4
nbconvert 5.6.1
nbformat 5.0.6
notebook 5.2.2
numpy 1.18.4
oauthlib 3.1.0
opt-einsum 3.2.1
packaging 20.3
pandocfilters 1.4.2
pickleshare 0.7.5
Pillow 7.1.2
pip 20.1
prompt-toolkit 3.0.5
protobuf 3.11.3
pyasn1 0.4.8
pyasn1-modules 0.2.8
Pygments 2.6.1
pyparsing 2.4.7
pypiwin32 223
pyrsistent 0.16.0
python-dateutil 2.8.1
pytz 2020.1
pywin32 227
PyYAML 5.3.1
pyzmq 19.0.1
qtconsole 4.7.3
QtPy 1.9.0
requests 2.23.0
requests-oauthlib 1.3.0
rsa 4.0
scipy 1.4.1
setuptools 46.1.3
simplegeneric 0.8.1
six 1.12.0
soupsieve 2.0
tensorboard 2.2.1
tensorboard-plugin-wit 1.6.0.post3
tensorflow 2.2.0
tensorflow-estimator 2.2.0
termcolor 1.1.0
testpath 0.4.4
tornado 4.5.3
traitlets 4.3.3
urllib3 1.25.9
wcwidth 0.1.9
webencodings 0.5.1
Werkzeug 1.0.1
wheel 0.34.2
widgetsnbextension 3.5.1
wrapt 1.12.1

Хотел создать нейросеть, которая будет определять, какая цифра нарисована на картинке. Картинки все загрузил. Пробовал писать и на Google Colaboratory, и на PyCharm, везде выдаётся ошибка:Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 7744]
Делал нейросеть для проекта по этому видео:
. У него всё получилось, а у меня почему-то нет. Помогите пожалуйста, а то сдача проекта скоро.

Python:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import imageio

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

print(type(x_test))

print(x_test.shape)
print(y_test.shape)

x_train = x_train / 255
x_test = x_test / 255

plt.figure(figsize=(8, 8))
for i in range(16):
    plt.subplot(4, 4, i + 1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(x_train[i], cmap=plt.cm.binary)
    plt.colorbar()
    plt.xlabel(y_train[i])
plt.show()

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

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

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

print(model.evaluate(x_test, y_test))

def model_answer(model, filename, display=True):
  image = imageio.imread(filename)
  image = np.mean(image, 2, dtype=float)
  image = image / 255
  if display:
    plt.xticks([])
    plt.yticks([])
    plt.imshow(image, cmap=plt.cm.binary)
    plt.xlabel(filename)
    plt.show()
  image = np.expand_dims(image, 0)
  return np.argmax(model.predict(image))

for i in range(10):
  filename = f'{i}.png'
  print('Имя файла: ', filename, '\tОтвет сети: ', model_answer(model, filename, False))
print(model_answer(model, '2.png'))
 

Вложения

  • a1.PNG
    a1.PNG
    121,1 КБ · Просмотры: 5
  • b1.PNG
    b1.PNG
    182,1 КБ · Просмотры: 5
  • b2.PNG
    b2.PNG
    30,5 КБ · Просмотры: 5
  • c1.PNG
    c1.PNG
    207,5 КБ · Просмотры: 5
  • d1.PNG
    d1.PNG
    114,8 КБ · Просмотры: 5
  • e1.PNG
    e1.PNG
    116,4 КБ · Просмотры: 5
  • e2.PNG
    e2.PNG
    81,2 КБ · Просмотры: 5
  • e3.PNG
    e3.PNG
    104,7 КБ · Просмотры: 5

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
Судя по ошибке ваши тестовые картинки неправильного размера. Должны быть 28х28, а в программу загружаете 88х88.
 
  • Мне нравится
Реакции: nordslomin

nordslomin

Новичок
Пользователь
Май 8, 2020
2
0
1
20
Судя по ошибке ваши тестовые картинки неправильного размера. Должны быть 28х28, а в программу загружаете 88х88.
Помогло. Спасибо большое.
 

Форум IT Специалистов