from keras.models import load_model #下面报错 没有相关的提示信息?????? model = SqueezeNet() #保存model部分 # save as JSON 保存模型的结构,而不包含其权重或配置信息 model_json = model.to_json() # with open("model.json", "w") as json_file: # json_file.write(model_json) # del_json = model.to_json() # json_file.write(model_json) #只保存权重 model.save_weights("model.h5") #保存model和权重 model.save('my_model.h5') # creates a HDF5 file 'my_model.h5' del model # deletes the existing model model = load_model( 'my_model.h5') # returns a compiled model identical to the previous one #img = image.load_img(img_path, target_size=(224, 224)) # 加载图像,归一化大小 img = image.load_img('images/cat.jpeg', target_size=(227, 227)) x = image.img_to_array(img) # 序列化 x = np.expand_dims(x, axis=0) # 展开 x = preprocess_input(x) # 预处理到0~1 preds = model.predict(x) # 预测结果,1000维的向量 print(
class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') train_history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples / batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size) model.save_weights('navigation_cnn_squeezenet_EE_2F_4class_v2.h5') #view gradient descent import matplotlib.pyplot as plt def show_train_history(train_history, train, validation): plt.plot(train_history.history[train]) plt.plot(train_history.history[validation]) plt.title('Train History') plt.ylabel(train) plt.xlabel('Epoch') plt.legend(['train', 'validation'], loc='upper left') plt.show()
target_size=(227, 227), # all images will be resized to 150x150 batch_size=batch_size, class_mode='binary' ) # since we use binary_crossentropy loss, we need binary labels # this is a similar generator, for validation data validation_generator = test_datagen.flow_from_directory(path + 'valid', target_size=(227, 227), batch_size=batch_size, class_mode='binary') steps = train_generator.samples // batch_size if steps == 0: steps = 1 val_steps = validation_generator.samples // batch_size if val_steps == 0: val_steps = 1 model.fit_generator(train_generator, steps_per_epoch=steps, epochs=1, validation_data=validation_generator, validation_steps=val_steps) model.save_weights( 'squeezecatsdogs.h5' ) # always save your weights after training or during training