y3 = predict2both(y) print('%s accu:' % i) # 只取最高置信度的准确率 s1 = [(labels_test[i] == y1[i]).min() for i in range(len(y1))] print(sum(s1) / len(s1)) # 只取置信度大于0.5的准确率 s2 = [(labels_test[i] == y2[i]).min() for i in range(len(y1))] print(sum(s2) / len(s2)) # 结合前两个 s3 = [(labels_test[i] == y3[i]).min() for i in range(len(y1))] print(sum(s3) / len(s3)) print('%s f1:' % i) # 只取最高置信度的准确率 s4 = f1_avg(y_pred=y1, y_true=labels_test) print(s4) # 只取置信度大于0.5的准确率 s5 = f1_avg(y_pred=y2, y_true=labels_test) print(s5) # 结合前两个 s6 = f1_avg(y_pred=y3, y_true=labels_test) print(s6) score_list1.append( [i, sum(s1) / len(s1), sum(s2) / len(s2), sum(s3) / len(s3)]) score_list2.append([i, s4, s5, s6]) print(pd.DataFrame(score_list1)) print(pd.DataFrame(score_list2))
optimizer = Adam(lr=0.001) model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) ##################################################################################################################### n_start = 1 n_end = 51 score_list = [] for i in range(n_start, n_end): model.fit(x=data_train_x, y=data_train_y, batch_size=batch_size, epochs=1, verbose=2) # model.save('./models/CNN_simple_3_filters_%d_bs_%d_epochs_%d.h5' % (filters, batch_size, i)) y1 = model.predict(data_valid_x) y1_tag = p2tag(y1) valid_tag = p2tag(data_valid_y) accu = accu_score(y1_tag, valid_tag) f1 = f1_avg(y1_tag, valid_tag) score_list.append([i, accu, f1]) metrics = pd.DataFrame(score_list, columns=['batch_size', 'accu', 'f1']) print(metrics) metrics.to_csv('./metrics/CNN_simple_3_256_512_1024_filters_%d_bs_%d.csv' % (filters, batch_size), index=False) y2 = model.predict(test_x) y2_tag = p2tag(y2) result = pd.DataFrame({'ImageId': range(1, 1 + len(y2_tag)), 'Label': y2_tag}, columns=['ImageId', 'Label']) result.to_csv('./result/result_CNN_simple_3_256_512_1024_filters_%d_bs_%d_epochs_%d.csv' % (filters, batch_size, i), index=False) print('end', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))