Пытался проверить нейронную сеть, скачал несколько изображений, загрузил структуру и весы, но выдаёт ошибку:
Код:
Using TensorFlow backend.
Traceback (most recent call last):
File "D:\Documents\Desktop\TFtraining\TFtraining\TFtraining.py", line 110, in
<module>
print(classes[prediction2[0]])[0]
TypeError: only integer scalar arrays can be converted to a scalar index
Вот код:
Код:
import PIL
import numpy as np
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers import Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.preprocessing import image
from keras.models import model_from_json
np.random.seed(42)
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
json_file = open("D:\Documents\Desktop\TFtraining\TFtraining\Weights and jsons\cifar10_model.json", "r")
load_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(load_model_json)
loaded_model.load_weights("D:\Documents\Desktop\TFtraining\TFtraining\Weights and jsons\cifar_model.h5" )
loaded_model.compile( loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'])
classes = ["Самолет", "Автомобиль", "Птица", "Кот", "Олень", "Собака", "Лягушка", "Лошадь", "Корабль", "Грузовик"]
img_car1_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\car.jpg"
img_car2_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\car1.jpg"
img_cat1_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\cat1.jpg"
img_cat2_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\cat2.jpg"
img_track1_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\track1.jpg"
img_track2_path = r"D:\Documents\Desktop\TFtraining\TFtraining\Images\track2.jpg"
img_car1 = image.load_img(img_car1_path, target_size = (32, 32))
img_car2 = image.load_img(img_car2_path, target_size = (32, 32))
img_cat1 = image.load_img(img_cat1_path, target_size = (32, 32))
img_cat2 = image.load_img(img_cat2_path, target_size = (32, 32))
img_track1 = image.load_img(img_track1_path, target_size = (32, 32))
img_track2 = image.load_img(img_track2_path, target_size = (32, 32))
img_car1_array = image.img_to_array(img_car1)
img_cat1_array = image.img_to_array(img_cat1)
img_car2_array = image.img_to_array(img_car2)
img_cat2_array = image.img_to_array(img_cat2)
img_track1_array = image.img_to_array(img_track1)
img_track2_array = image.img_to_array(img_track2)
img_car1_array /= 225
img_car2_array /= 225
img_cat1_array /= 225
img_cat2_array /= 225
img_track1_array /= 225
img_track2_array /= 225
img_car1_array = np.expand_dims(img_car1_array, axis = 0)
img_car2_array = np.expand_dims(img_car2_array, axis = 0)
img_cat1_array = np.expand_dims(img_cat1_array, axis = 0)
img_cat2_array = np.expand_dims(img_cat2_array, axis = 0)
img_track1_array = np.expand_dims(img_track1_array, axis = 0)
img_track2_array = np.expand_dims(img_track2_array, axis = 0)
prediction1 = loaded_model.predict(img_car1_array)
prediction2 = loaded_model.predict(img_car2_array)
prediction3 = loaded_model.predict(img_cat1_array)
prediction4 = loaded_model.predict(img_cat2_array)
prediction5 = loaded_model.predict(img_track1_array)
prediction6 = loaded_model.predict(img_track2_array)
print(classes[prediction1[0]])
print(classes[prediction2[0]])
print(classes[prediction3[0]])
print(classes[prediction4[0]])
print(classes[prediction5[0]])
print(classes[presiction6[0]])
Искал в интернете, и всё, что я нашел, это добавить [0] в конец, но это не помогло. Скажите, пожалуйста, что не так?