for test_batch_images, test_batch_labels in test_ds: test_batch_labels = test_batch_labels.numpy() test_batch_pres = model.predict(test_batch_images) test_batch_labels_max = np.argmax(test_batch_labels, axis=1) test_batch_pres_max = np.argmax(test_batch_pres, axis=1)
时间: 2023-09-11 10:10:23 浏览: 146
这段代码看起来是用于对测试数据集进行预测和评估的部分。首先,它使用一个循环来迭代测试数据集(test_ds)中的批量图像和标签。然后,使用model.predict()方法对测试批量图像进行预测。接下来,使用np.argmax()函数找到每个批量标签和预测结果中的最大值索引,以便进行后续的评估和比较。
相关问题
def test_mobilenet(): # todo 加载数据, 224*224的大小 模型一次训练16张图片 train_ds, test_ds, class_names = data_load(r"C:\Users\wjx\Desktop\项目\data\flower_photos_split\train", r"C:\Users\wjx\Desktop\项目\data\flower_photos_split\test", 224, 224, 16) # todo 加载模型 model = tf.keras.models.load_model("models/mobilenet_fv.h5") # model.summary() # 测试,evaluate的输出结果是验证集的损失值和准确率 loss, accuracy = model.evaluate(test_ds) # 输出结果 print('Mobilenet test accuracy :', accuracy) test_real_labels = [] test_pre_labels = [] for test_batch_images, test_batch_labels in test_ds: test_batch_labels = test_batch_labels.numpy() test_batch_pres = model.predict(test_batch_images) # print(test_batch_pres) test_batch_labels_max = np.argmax(test_batch_labels, axis=1) test_batch_pres_max = np.argmax(test_batch_pres, axis=1) # print(test_batch_labels_max) # print(test_batch_pres_max) # 将推理对应的标签取出 for i in test_batch_labels_max: test_real_labels.append(i) for i in test_batch_pres_max: test_pre_labels.append(i) # break # print(test_real_labels) # print(test_pre_labels) class_names_length = len(class_names) heat_maps = np.zeros((class_names_length, class_names_length)) for test_real_label, test_pre_label in zip(test_real_labels, test_pre_labels): heat_maps[test_real_label][test_pre_label] = heat_maps[test_real_label][test_pre_label] + 1 print(heat_maps) heat_maps_sum = np.sum(heat_maps, axis=1).reshape(-1, 1) # print(heat_maps_sum) print() heat_maps_float = heat_maps / heat_maps_sum print(heat_maps_float) # title, x_labels, y_labels, harvest show_heatmaps(title="heatmap", x_labels=class_names, y_labels=class_names, harvest=heat_maps_float, save_name="images/heatmap_mobilenet.png")
这段代码是用来测试 Mobilenet 模型在花卉数据集上的表现的。首先,使用 `data_load` 函数加载数据集,然后使用 `tf.keras.models.load_model` 函数加载预训练好的 Mobilenet 模型。接着,使用 `model.evaluate` 函数对测试集进行评估,得到测试集的损失值和准确率。然后,依次对测试集中的每一批数据进行预测,将真实标签和预测标签分别存储在两个列表中。最后,使用这两个列表生成混淆矩阵,并将混淆矩阵可视化为热力图。
import idx2numpy import numpy as np from functions import * from two_layer_network import * #导入训练集和训练集对应的标签并将其初始化 X_train,T_train=idx2numpy.convert_from_file('emnist/emnist-letters-train-images-idx3-ubyte'),idx2numpy.convert_from_file('emnist/emnist-letters-train-labels-idx1-ubyte') X_train,T_train=X_train.copy(),T_train.copy() X_train=X_train.reshape((X_train.shape[0],-1)) T_train=T_train-1 T_train=np.eye(26)[T_train] #导入测试集和测试集对应的标签标签并将其初始化 X_test,T_test=idx2numpy.convert_from_file('emnist/emnist-letters-test-images-idx3-ubyte'),idx2numpy.convert_from_file('emnist/emnist-letters-test-labels-idx1-ubyte') X_test,T_test=X_test.copy(),T_test.copy() X_test=X_test.reshape((X_test.shape[0],-1)) T_test=T_test-1 T_test=np.eye(26)[T_test] network=TwoLayerNet(input_size=784,hidden_size=45,output_size=26) train_size=X_train.shape[0] batch_size=100 iters_num=100000 learning_rate=0.01 train_loss_list=[] train_acc_list=[] test_acc_list=[] iter_per_epoch=max(train_size/batch_size,1) for i in range(iters_num): batch_mask=np.random.choice(train_size,batch_size) X_batch=X_train[batch_mask] T_batch=T_train[batch_mask] #从数据集里抽取batch_size笔数据 #grad=network.numerical_gradient(X_batch,T_batch)(跑不出来,卡在None None) grad=network.gradient(X_batch,T_batch) #计算梯度 for key in ('W1','b1','W2','b2') : network.params[key]-=learning_rate*grad[key] #误差反向传播法调整参数 loss=network.loss(X_batch,T_batch) train_loss_list.append(loss) #记录学习过程 if i % iter_per_epoch==0: train_acc=network.accuracy(X_train,T_train) test_acc=network.accuracy(X_test,T_test) train_acc_list.append(train_acc) test_acc_list.append(test_acc) print(train_acc,test_acc) #调整学习率 if i > 0 and i % 1000 == 0 and i<3000: learning_rate *= 0.1 print("learning rate reduced to " + str(learning_rate)) print(network.params) print(train_acc_list[-1],test_acc_list[-1])如果我想存储参数,该怎么做
如果想要存储参数,可以使用Python内置的pickle模块。具体步骤如下:
1. 在代码中加入以下代码,将训练后的参数保存到文件中:
```
import pickle
with open("params.pkl", "wb") as f:
pickle.dump(network.params, f)
```
其中,"params.pkl"是你想要存储参数的文件名,network.params是你的神经网络的参数。
2. 在需要使用训练后的参数进行预测时,可以通过以下代码从文件中读取参数:
```
import pickle
with open("params.pkl", "rb") as f:
params = pickle.load(f)
network.params = params
```
其中,params是从文件中读取到的参数,将其赋给network.params即可。
阅读全文