Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import numpy
- from keras.preprocessing import image
- from keras.models import model_from_json
- from matplotlib import pyplot as plt
- from keras.backend import set_image_data_format
- # Загрузка и компиляции модели
- with open("mnist_conv.json", "r") as json_file:
- loaded_model_json = json_file.read()
- loaded_model = model_from_json(loaded_model_json)
- loaded_model.load_weights("mnist_conv.h5")
- loaded_model.compile(loss="categorical_crossentropy", optimizer="adam",
- metrics=["accuracy"])
- # Какова размерность входных данных для загруженной нейронной сети?
- first_layer_config = loaded_model.get_config()['layers'][0]['config']
- batch_input_shape = first_layer_config['batch_input_shape']
- data_format = first_layer_config['data_format']
- img_shape = (batch_input_shape[2:] if
- data_format == 'channels_first' else
- batch_input_shape[1:-1])
- # Загружаем свою картинку
- img_path = '7py.png'
- img = image.load_img(img_path, target_size=img_shape, color_mode='grayscale')
- plt.imshow(img, cmap='gray')
- plt.show()
- # Преобразуем картинку в массив и нормализуем
- set_image_data_format(data_format)
- x = image.img_to_array(img)
- x = 255 - x
- x /= 255
- x = numpy.expand_dims(x, axis=0)
- prediction = loaded_model.predict(x)
- print(prediction)
- prediction = numpy.argmax(prediction, axis=1)
- print(prediction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement