def seg_predict_image_task1(image_path, use_tta=False): '''只针对一张图片''' num_folds = 4 # use_tta = False # 如果为false,表示不采用取平均数方式 y_pred = np.zeros(shape=(1, 224, 224)) resize_image_np, W, H = get_resize_image_np(image_path, 224) if use_tta: # 采用取平均数的模式 for k_fold in range(num_folds + 1): model_name = 'task1_vgg16' run_name = 'task1_vgg16_k%d_v0' % k_fold model = backbone('vgg16').segmentation_model(load_from=run_name) # model.load_weights("/home/zhangfan/workData/LinuxCode/pythonProject/ISIC2018/model_data/task1_vgg16_k0_v0/task1_vgg16_k0_v0.hdf5") y_pred += inv_sigmoid( model.predict_on_batch(resize_image_np))[:, :, :, 0] y_pred /= (num_folds + 1) else: # 只是用最后一组数据的权重 run_name = 'task1_vgg16_k%d_v0' % num_folds model = backbone('vgg16').segmentation_model(load_from=run_name) y_pred += inv_sigmoid(model.predict_on_batch(resize_image_np))[:, :, :, 0] y_pred = sigmoid(y_pred) current_pred = y_pred[0] * 255 current_pred[current_pred > 128] = 255 current_pred[current_pred <= 128] = 0 # 将resized_pred传给Nodejs print({"image_np": current_pred, "width": W, "height": H})
def cls_predict_images_task4(self,image_paths): results = [] resize_images_np, images_size = get_resize_images_np(image_paths) images_num = len(images_size) y_pred = np.zeros(shape=(images_num, 2)) with self.graph.as_default(): y_pred += self.task4_model.predict_on_batch(resize_images_np) y_prob = sigmoid(y_pred) for index, image_path in enumerate(image_paths): task4_result = predict_thrift.task4_result() task4_result.name = image_path task4_result.result = y_prob[index] results.append(task4_result) return results
def seg_predict_images_task1(image_paths, use_tta=False): '''输入图片地址,预测一组图片''' num_folds = 4 use_tta = False image_num = len(image_paths) # 得到图片的数量 if use_tta: pass else: y_pred = np.zeros(shape=(1, 224, 224)) run_name = 'task1_vgg16_k%d_v0' % num_folds model = backbone('vgg16').segmentation_model(load_from=run_name) for image_path in image_paths: resize_image_np, W, H = get_resize_image_np(image_path, 224) y_pred += inv_sigmoid( model.predict_on_batch(resize_image_np))[:, :, :, 0] y_pred = sigmoid(y_pred) current_pred = y_pred[0] * 255 current_pred[current_pred > 128] = 255 current_pred[current_pred <= 128] = 0 print({"image_np": current_pred, "width": W, "height": H})
def seg_predict_images_task1(self,image_paths): results = [] resize_images_np,images_size = get_resize_images_np(image_paths) images_num = len(images_size) y_pred = np.zeros(shape=(images_num,224,224)) with self.graph.as_default(): y_pred += inv_sigmoid(self.task1_model.predict_on_batch(resize_images_np))[:, :, :, 0] y_pred = sigmoid(y_pred) y_pred = task1_post_process(y_prediction=y_pred,threshold=0.5,gauss_sigma=2.) for index,image_path in enumerate(image_paths): image_dir,image_name,image_ext = get_fileDirectoryPath_fileName_fileExt(image_path) current_pred = y_pred[index] current_pred = current_pred * 255 resized_pred = transform.resize(current_pred, output_shape=images_size[index], preserve_range=True, mode='reflect') resized_pred[resized_pred > 128] = 255 resized_pred[resized_pred <= 128] = 0 img = Image.fromarray(resized_pred.astype(np.uint8)) image_path = os.path.join(task1_result_dir,image_name+"_result"+image_ext) img.save(image_path) results.append(image_path) return results
print('Starting prediction for set %s with TTA set to %r with num_folds %d' % (pred_set, use_tta, num_folds)) for k_fold in range(num_folds): print('Processing fold ', k_fold) model_name = 'task%d_%s' % (task_idx, backbone_name) run_name = 'task%d_%s_k%d_v%s' % (task_idx, backbone_name, k_fold, version) model = backbone(backbone_name).segmentation_model(load_from=run_name) if use_tta: y_pred += inv_sigmoid(task1_tta_predict(model=model, img_arr=images))[:, :, :, 0] else: y_pred += inv_sigmoid(model.predict(images))[:, :, :, 0] print('Done predicting -- now doing post-processing') y_pred = y_pred / num_folds y_pred = sigmoid(y_pred) y_pred = task1_post_process(y_prediction=y_pred, threshold=0.5, gauss_sigma=2.) output_dir = submission_dir + '/task1_' + pred_set mkdir_if_not_exist([output_dir]) for i_image, i_name in enumerate(image_names): current_pred = y_pred[i_image] current_pred = current_pred * 255 resized_pred = sk_resize(current_pred, output_shape=image_sizes[i_image], preserve_range=True, mode='reflect',