Advertisement
Stan_

mnist_conv_using

May 12th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import numpy
  4. from keras.preprocessing import image
  5. from keras.models import model_from_json
  6. from matplotlib import pyplot as plt
  7. from keras.backend import set_image_data_format
  8.  
  9. # Загрузка и компиляции модели
  10. with open("mnist_conv.json", "r") as json_file:
  11.     loaded_model_json = json_file.read()
  12. loaded_model = model_from_json(loaded_model_json)
  13. loaded_model.load_weights("mnist_conv.h5")
  14. loaded_model.compile(loss="categorical_crossentropy", optimizer="adam",
  15.                      metrics=["accuracy"])
  16.  
  17. # Какова размерность входных данных для загруженной нейронной сети?
  18. first_layer_config = loaded_model.get_config()['layers'][0]['config']
  19. batch_input_shape = first_layer_config['batch_input_shape']
  20. data_format = first_layer_config['data_format']
  21. img_shape = (batch_input_shape[2:] if
  22.              data_format == 'channels_first' else
  23.              batch_input_shape[1:-1])
  24.  
  25. # Загружаем свою картинку
  26. img_path = '7py.png'
  27. img = image.load_img(img_path, target_size=img_shape, color_mode='grayscale')
  28. plt.imshow(img, cmap='gray')
  29. plt.show()
  30.  
  31. # Преобразуем картинку в массив и нормализуем
  32. set_image_data_format(data_format)
  33. x = image.img_to_array(img)
  34. x = 255 - x
  35. x /= 255
  36. x = numpy.expand_dims(x, axis=0)
  37.  
  38. prediction = loaded_model.predict(x)
  39. print(prediction)
  40. prediction = numpy.argmax(prediction, axis=1)
  41. print(prediction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement