# 2、初始化和启动线程 tf.global_variables_initializer().run() tf.local_variables_initializer().run() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) model.restore(model_restore_name) #3、测试图片 index = 0 for i in range(test_data_number // batch_size): pics, pics_masks = sess.run([images, labels]) # 取出一个batchsize的图片 pics = pics / 255 # 3、计算耗时 since = time.time() pre = model.predict(pics) seconds = time.time() - since pre_list = np.split(pre[0], batch_size, axis=0) pres = np.squeeze(pre_list, axis=0) for img, label, predict in zip(pics, pics_masks, pres): img = img * 255 label = label * 255 predict = predict * 255 label = cv2.cvtColor(label, cv2.COLOR_GRAY2BGR) predict = cv2.cvtColor(predict, cv2.COLOR_GRAY2BGR) hstack = np.hstack((img, label, predict))
loss=bce_dice_loss, metrics=[bce_dice_loss, recall_score, precision_score, rocauc_score]) ckpt = ModelCheckpoint('.model.hdf5', save_best_only=True, monitor='val_rocauc_score', mode='max') model.fit_generator(train_generator, steps_per_epoch=len(xtr) / 6, epochs=50, validation_data=val_generator, validation_steps=len(xval) / batch_size, callbacks=[ckpt]) model.load_weights('.model.hdf5') preds_test = model.predict(X_test, verbose=1) preds_test_upsampled = [] for i in range(len(preds_test)): preds_test_upsampled.append( cv2.resize(preds_test[i], (sizes_test[i][1], sizes_test[i][0]))) test_ids = next(os.walk(test_path))[1] new_test_ids = [] rles = [] for n, id_ in enumerate(test_ids): rle = list(prob_to_rles(preds_test_upsampled[n])) rles.extend(rle) new_test_ids.extend([id_] * len(rle)) sub = pd.DataFrame() sub['ImageId'] = new_test_ids