def main(): path_pos = '../../data/opinion-lexicon-English/positive-words.txt' path_neg = '../../data/opinion-lexicon-English/negative-words.txt' path_to_testset = '../../data/restaurants_gold.tsv' #load in test set data prompt = "Please enter filepath to CoNLL formatted SemEval 2014 restaurant test set: " input_path_to_testset = input(prompt) if input_path_to_testset == '': pass else: path_to_testset = input_path_to_testset df_test, df_gold_lists = test_set(path_to_testset) prompt = "Please enter filepath to positive seed lexicon: " input_path_pos = input(prompt) if input_path_pos == '': pass else: path_pos = input_path_pos prompt = "Please enter filepath to negative seed lexicon: " input_path_neg = input(prompt) if input_path_neg == '': pass else: path_neg = input_path_neg # populate seed opinion set and create aspect set O, F = initialise_lexicon(path_pos, path_neg) # run DP algorithm features_out_dict = algorithm(df_gold_lists, F, O) # convert phrase terms and prepare for prediction system_predictions = post_process(features_out_dict, df_test) # predict predict(df_test, system_predictions)
def detect(self, images, scales, paddings): with torch.no_grad(): detections = self.net(images) detections = post_process(detections, True, self.conf_thresh, self.nms_thres) for detection, scale, padding in zip(detections, scales, paddings): detection[..., :4] = untransform_bboxes(detection[..., :4], scale, padding) cxcywh_to_xywh(detection) return detections
def ensemble(args): #class_params = {0: (0.5, 25000), 1: (0.7, 15000), 2: (0.4, 25000), 3: (0.6, 10000)} models = create_models(args) class_params = find_class_params(args, models) #exit(0) test_loader = get_test_loader(args.encoder_types.split(',')[0], args.batch_size) probs, _ = predict_loader(models, test_loader) encoded_pixels, encoded_pixels_no_minsize = [], [] image_id = 0 for img_out in tqdm(probs): #runner_out = runner.predict_batch({"features": test_batch[0].cuda()})['logits'] #for i, batch in enumerate(runner_out): for probability in img_out: #probability = probability.cpu().detach().numpy() if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) predict, num_predict = post_process(probability, class_params[image_id % 4][0], class_params[image_id % 4][1]) if num_predict == 0: encoded_pixels.append('') else: r = mask2rle(predict) encoded_pixels.append(r) predict2, num_predict2 = post_process(probability, class_params[image_id % 4][0], 0) if num_predict2 == 0: encoded_pixels_no_minsize.append('') else: r2 = mask2rle(predict2) encoded_pixels_no_minsize.append(r2) image_id += 1 sub = pd.read_csv(os.path.join(settings.DATA_DIR, 'sample_submission.csv')) sub['EncodedPixels'] = encoded_pixels sub.to_csv(args.out, columns=['Image_Label', 'EncodedPixels'], index=False) sub['EncodedPixels'] = encoded_pixels_no_minsize sub.to_csv(args.out+'_no_minsize', columns=['Image_Label', 'EncodedPixels'], index=False)
def segmIm(im, r, c=4, dim=True): """ Image Segmentation with mean-shift algorithm """ points = reshape_im(im, dim) labels, peaks = meanshift(data=points, r=r, c=c) segmented_img = post_process(labels, peaks, im, dim) plotclusters3D(points, labels, peaks) return segmented_img, len(peaks)
def find_class_params(args, models): val_loader = get_train_val_loaders(args.encoder_types.split(',')[0], batch_size=args.batch_size)['valid'] probs, masks = predict_loader(models, val_loader) print(probs.shape, masks.shape) valid_masks = [] probabilities = np.zeros((2220, 350, 525)) for i, (img_probs, img_masks) in enumerate(zip(probs, masks)): for m in img_masks: if m.shape != (350, 525): m = cv2.resize(m, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) valid_masks.append(m) for j, probability in enumerate(img_probs): if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) probabilities[i * 4 + j, :, :] = probability print(len(valid_masks), len(probabilities), valid_masks[0].shape, probabilities[0].shape) class_params = {} for class_id in range(4): print(class_id) attempts = [] for t in range(30, 90, 5): t /= 100 #for ms in [0, 100, 1200, 5000, 10000]: for ms in [5000, 10000, 15000, 20000, 22500, 25000]: masks = [] for i in range(class_id, len(probabilities), 4): probability = probabilities[i] predict, num_predict = post_process(probability, t, ms) masks.append(predict) d = [] for i, j in zip(masks, valid_masks[class_id::4]): if (i.sum() == 0) & (j.sum() == 0): d.append(1) else: d.append(dice(i, j)) attempts.append((t, ms, np.mean(d))) attempts_df = pd.DataFrame(attempts, columns=['threshold', 'size', 'dice']) attempts_df = attempts_df.sort_values('dice', ascending=False) print(attempts_df.head()) best_threshold = attempts_df['threshold'].values[0] best_size = attempts_df['size'].values[0] class_params[class_id] = (best_threshold, best_size) print(class_params) return class_params
def predict_blend( loaders=None, runner=None, class_params: dict = None, path: str = "", sub_name: str = "", ): """ Args: loaders: runner: class_params: path: sub_name: Returns: """ encoded_pixels = [] image_id = 0 for _, test_batch in tqdm(enumerate(loaders["test"])): runner_out = runner.predict_batch({"features": test_batch[0].cuda()})["logits"] for _, batch in enumerate(runner_out): for probability in batch: probability = probability.cpu().detach().numpy() if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) prediction, num_predict = post_process( (probability), class_params[str(image_id % 4)][0], class_params[str(image_id % 4)][1], ) if num_predict == 0: encoded_pixels.append("") else: r = mask2rle(prediction) encoded_pixels.append(r) image_id += 1 sub = pd.read_csv(f"{path}/sample_submission.csv") sub["EncodedPixels"] = encoded_pixels sub.to_csv( f"submissions/submission_{sub_name}.csv", columns=["Image_Label", "EncodedPixels"], index=False, )
def forward(self, y, ret_edge=False): yp, params = utils.pre_process(y, self.tkargs[1], self.eval()) z = ST(self.A[0](yp), self.tau[0]) for i in range(1, self.iters): if ((i - 1) % self.edge_freq) == 0: edge = self.topK(z) r = self.B[i](z, edge) - yp z = ST(z - self.A[i](r, edge), self.tau[i]) edge = self.topK(z) xphat = self.D(z, edge) xhat = utils.post_process(xphat, params) if ret_edge: return xhat, edge return xhat
def predict(loaders=None, runner=None, class_params: dict = None, path: str = '', sub_name: str = ''): """ Args: loaders: runner: class_params: path: sub_name: Returns: """ encoded_pixels = [] image_id = 0 for _, test_batch in enumerate(loaders['test']): runner_out = runner.predict_batch({"features": test_batch[0].cuda()})['logits'] for _, batch in enumerate(runner_out): for probability in batch: probability = probability.cpu().detach().numpy() if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) prediction, num_predict = post_process( sigmoid(probability), class_params[image_id % 4][0], class_params[image_id % 4][1]) if num_predict == 0: encoded_pixels.append('') else: r = mask2rle(prediction) encoded_pixels.append(r) image_id += 1 sub = pd.read_csv(f'{path}/sample_submission.csv') sub['EncodedPixels'] = encoded_pixels sub.to_csv(f'submissions/submission_{sub_name}.csv', columns=['Image_Label', 'EncodedPixels'], index=False)
def main(): print("AAAAAHHHHH") # Read class names and generate different colors for different classes classes, colors = u.read_classes_and_generate_colors(classes_file) # Read pre-trained model and config file net = cv2.dnn.readNetFromDarknet(model_configuration, model_weights) net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV) net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) # Define a window to show the cam stream on it window_title = "YOLOv3" cv2.namedWindow(window_title, cv2.WINDOW_NORMAL) # --- Processing real time video ---S # Webcam input cap = cv2.VideoCapture(0) while cv2.waitKey(1) < 0: # get frame from the video hasFrame, frame = cap.read() blob = cv2.dnn.blobFromImage(frame, 1.0 / 255.0, (416, 416), [0, 0, 0], True, crop=False) # Sets the input to the network net.setInput(blob) # Runs the forward pass to get output of the output layers outs = net.forward(u.get_output_layers(net)) # Remove the bounding boxes with low confidence frame = u.post_process(frame, outs, classes, colors) # Put efficiency information. The function getPerfProfile returns the # overall time for inference(t) and the timings for each of the layers(in layersTimes) t, _ = net.getPerfProfile() label = 'Inference time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency()) cv2.putText(frame, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255)) cv2.imshow(window_title, frame)
def predict(): visualize = request.args.get('visualize', 'none') resize = request.args.get('resize', 'original') score_threshold = float( request.args.get('score_threshold', str(config.score_threshold))) nms_iou_threshold = float( request.args.get('nms_iou_threshold', str(config.nms_iou_threshold))) img = utils.extract_as_jpeg(request) x = utils.img2tensor(img, resize=True) x_original = utils.img2tensor(img, resize=False) scale = (x_original[0].size()[1] / x[0].size()[1], x_original[0].size()[2] / x[0].size()[2]) y = model(x) ret = dict() if len(y[0]['boxes']) > 0: ret = utils.post_process(y[0]['boxes'], y[0]['scores'], score_threshold, nms_iou_threshold) if resize == 'original': ret['boxes'] = utils.rescale_box(ret['boxes'], scale) else: ret['boxes'] = [[]] ret['scores'] = [] if visualize == 'none': return jsonify(ret) img_to_show = x_original[0] if resize == 'original' else x if visualize == 'bbox': fig = utils.show_detection(img_to_show, ret['boxes'], pred_score=ret['scores']) output = io.BytesIO() FigureCanvas(fig).print_png(output) return Response(output.getvalue(), mimetype='image/png') elif visualize == 'blur': return blur({'boxes': ret['boxes'], 'image': img})
def dataset_accuracy(net, csv_path, save=False, postprocess=False, show=False): file = open(csv_path, 'r') lines = csv.reader(file) mean_acc = [] orgs_acc = [] for line in lines: seg_path = os.path.join(label_path, "label%04d.nii" % int(line[0])) ct_path = os.path.join(image_path, "image%04d.nii" % int(line[0])) pred_seg = sample_predict(net, ct_path) # (9, D, 256, 256) if postprocess: pred_seg = post_process(pred_seg) pred_seg = np.expand_dims(pred_seg, axis=0) # (1, 9, D, 256, 256) target = sitk.ReadImage(seg_path) target_array = sitk.GetArrayFromImage(target) target_array = np.expand_dims(target_array, axis=0) # (1, D, 256, 256) accs, acc = accuracy(pred_seg, target_array) if save: save_seg(pred_seg, line, accs) if show: print("---------------- image%04d.nii" % int(line[0])) print(' '.join( ["%s:%s" % (i, str(j)) for i, j in zip(organs_name, accs)])) orgs_acc.append(accs) mean_acc.append(acc) orgs_acc = np.array(orgs_acc) org_mean = [ np.mean( np.array(list(set(orgs_acc[:, i]).difference(['None'])), dtype=np.float16)) for i in range(len(organs_name)) ] return org_mean, np.mean(mean_acc)
def forward(self, x, ret_edge=False): if ret_edge: edge_list = [] x, params = utils.pre_process(x, self.tkargs[1], self.eval()) z = torch.cat([self.PPCONV[i](self.INCONV[i](x)) for i in range(3)], dim=1) hiz = self.HPF(z) for i in range(self.iters): print(f"i = {i}") z0 = (1 - self.alpha[i]) * z + self.beta[i] * hiz z = self.LPF[i](z0, ret_edge=ret_edge) if ret_edge: z, edge = z edge_list.append(edge) z = z0 + z z = (1 - self.alpha[-1]) * z + self.beta[-1] * hiz edge = self.topK(z) z = self.GCout(z, edge) x = utils.post_process(x + z, params) if ret_edge: edge_list.append(edge) return x, edge_list return x
def predict(args): #model = create_model(args.encoder_type, ckp=args.ckp).cuda() #model = nn.DataParallel(model) #runner = SupervisedRunner(model=model) class_params, runner = find_class_params(args) #runner = create_runner(args) test_loader = get_test_loader(args.encoder_type, args.batch_size) loaders = {"test": test_loader} encoded_pixels = [] image_id = 0 for i, test_batch in enumerate(tqdm(loaders['test'])): runner_out = runner.predict_batch({"features": test_batch[0].cuda()})['logits'] for i, batch in enumerate(runner_out): for probability in batch: probability = probability.cpu().detach().numpy() if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) predict, num_predict = post_process( sigmoid(probability), class_params[image_id % 4][0], class_params[image_id % 4][1]) if num_predict == 0: encoded_pixels.append('') else: r = mask2rle(predict) encoded_pixels.append(r) image_id += 1 sub = pd.read_csv(os.path.join(settings.DATA_DIR, 'sample_submission.csv')) sub['EncodedPixels'] = encoded_pixels sub.to_csv(args.out, columns=['Image_Label', 'EncodedPixels'], index=False)
shape = (1400, 2100, 3) test_dataset = ImageDataset(utils.TEST_IMAGES, os.listdir(utils.TEST_IMAGES), None, transforms, shape, True) batch_size = 1 data_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=4) encodes = [["Image_Label", "EncodedPixels"]] for i, data in enumerate(data_loader): image, path = data image = image.to(device) out = model(image.view(-1, 3, 350, 525)) out = out.cpu().detach().numpy() print(str(i) + "/" + str(len(os.listdir(utils.TEST_IMAGES)))) plt.imshow(utils.conv_image(image[0])) plt.show() for mask, cat in zip(out[0], utils.CLASSES): current_name = path[0] + "_" + cat mask, n_masks = utils.post_process(mask, 0.65, 10000) if n_masks != 0: encodes.append([current_name, utils.mask2rle(mask)]) else: encodes.append([current_name, ""]) with open("submission.csv", 'w', newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) for row in encodes: wr.writerow(row)
'Path of feature file(.npy format), ignore it if no features are provided') parser.add_argument('--turn', type=int, default=30, help='Iteration turn(default 30)') parser.add_argument('--b', type=int, default=8, help='Maximum band of LSH') parser.add_argument('--seed', type=int, default=42, help='RNG seed') parser.add_argument('--debug', action='store_true', default=False, help='Debug flag') args = parser.parse_args() if __name__ == '__main__': adj = ssp.load_npz(args.adj) N = adj.shape[0] adj[adj.nonzero()] = 1 adj = adj.maximum(adj.T).tocoo() row = adj.row.astype(np.uint64) col = adj.col.astype(np.uint64) graph = DPGS.from_row_col(N, row, col) model = DPGS.DPGS(args.dataset, graph, args.b, args.seed, args.debug) model.run(args.turn) nodes_dict = model.getNodesDict() nodes_dict = dict( (i, sn) for (i, sn) in enumerate(nodes_dict) if len(sn) > 0) post_process(adj, nodes_dict, args.dataset)
batch_pred_masks = (batch_pred_masks1 + batch_pred_masks2 + batch_pred_masks3) / 3 # Predict out put shape is (320X480X4) # 4 = 4 classes, Fish, Flower, Gravel Surger. for j, idx in enumerate(batch_idx): # Batch prediction result set pred_masks = batch_pred_masks[j, ] for k in range(pred_masks.shape[-1]): pred_mask = pred_masks[..., k].astype('float32') if pred_mask.shape != (350, 525): pred_mask = cv2.resize(pred_mask, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) pred_mask, num_predict = post_process(pred_mask, threshold, min_size[k], (350, 525)) if num_predict == 0: encoded_pixels.append('') else: r = mask2rle(pred_mask) encoded_pixels.append(r) # # Submission sub_df['EncodedPixels'] = encoded_pixels sub_df.to_csv('submission.csv', columns=['Image_Label', 'EncodedPixels'], index=False)
normal_vec_front_d, normal_vec_front_ir, normal_vec_top_d, normal_vec_top_ir, test_loader_front_d, test_loader_front_ir, test_loader_top_d, test_loader_top_ir, score_folder, args.use_cuda) gt = get_fusion_label(os.path.join(args.root_path, 'LABEL.csv')) hashmap = { 'top_d': 'Top(D)', 'top_ir': 'Top(IR)', 'fusion_top': 'Top(DIR)', 'front_d': 'Front(D)', 'front_ir': 'Front(IR)', 'fusion_front': 'Front(DIR)', 'fusion_d': 'Fusion(D)', 'fusion_ir': 'Fusion(IR)', 'fusion_all': 'Fusion(DIR)' } for mode, mode_name in hashmap.items(): score = get_score(score_folder, mode) best_acc, best_threshold, AUC = evaluate(score, gt, False) print( f'Mode: {mode_name}: Best Acc: {round(best_acc, 2)} | Threshold: {round(best_threshold, 2)} | AUC: {round(AUC, 4)}' ) score = post_process(score, args.window_size) best_acc, best_threshold, AUC = evaluate(score, gt, False) print( f'View: {mode_name}(post-processed): Best Acc: {round(best_acc, 2)} | Threshold: {round(best_threshold, 2)} | AUC: {round(AUC, 4)} \n' )
def test_post_process(boxes, class_ids, indexes): boxes, class_ids = post_process(boxes, class_ids, indexes) assert isinstance(boxes, list) assert isinstance(class_ids, list)
def find_class_params(args): runner = SupervisedRunner() model = create_model(args.encoder_type) valid_loader = get_train_val_loaders(args.encoder_type, batch_size=args.batch_size)['valid'] encoded_pixels = [] loaders = {"infer": valid_loader} runner.infer( model=model, loaders=loaders, callbacks=[CheckpointCallback(resume=args.ckp), InferCallback()], ) print(runner.callbacks) valid_masks = [] probabilities = np.zeros((2220, 350, 525)) for i, (batch, output) in enumerate( tqdm( zip(valid_loader.dataset, runner.callbacks[0].predictions["logits"]))): image, mask = batch for m in mask: if m.shape != (350, 525): m = cv2.resize(m, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) valid_masks.append(m) for j, probability in enumerate(output): if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) probabilities[i * 4 + j, :, :] = probability class_params = {} for class_id in range(4): print(class_id) attempts = [] for t in range(0, 100, 5): t /= 100 #for ms in [0, 100, 1200, 5000, 10000]: for ms in [5000, 10000, 15000, 20000, 22500, 25000, 30000]: masks = [] for i in range(class_id, len(probabilities), 4): probability = probabilities[i] predict, num_predict = post_process( sigmoid(probability), t, ms) masks.append(predict) d = [] for i, j in zip(masks, valid_masks[class_id::4]): if (i.sum() == 0) & (j.sum() == 0): d.append(1) else: d.append(dice(i, j)) attempts.append((t, ms, np.mean(d))) attempts_df = pd.DataFrame(attempts, columns=['threshold', 'size', 'dice']) attempts_df = attempts_df.sort_values('dice', ascending=False) print(attempts_df.head()) best_threshold = attempts_df['threshold'].values[0] best_size = attempts_df['size'].values[0] class_params[class_id] = (best_threshold, best_size) print(class_params) return class_params, runner
import numpy as np import sys from keras.models import load_model import utils import models MEAN = 3.58171208604 TEST_DATA = sys.argv[1] ANS_PATH = sys.argv[2] MODEL = './model.h5' x_test = utils.load_data(TEST_DATA, file_type='test') model = load_model(MODEL, custom_objects={'rmse': models.rmse}) y_pred = model.predict(np.hsplit(x_test, 2)) print('--y_pred:\n', y_pred) # y_pred = utils.post_process(y_pred, bias = MEAN) y_pred = utils.post_process(y_pred, round=False, bias=MEAN) print('--ans:\n', y_pred) utils.save_dir(data=y_pred, path=ANS_PATH)
def main(args): stitchedImage = None input_images = args.input_images # Loop over input array of images while len(input_images) > 1: # Use last two sets of images for the first estimation left_image_path = input_images[-2] right_image_path = input_images[-1] # Initialize left and right image! left_image = cv2.imread(left_image_path) right_image = cv2.imread(right_image_path) # Create keypoint detector object by passing all relevant arguments that it needs. patch_size argument only for custom # descriptors since SIFT automatically chooses 16x16 neighbourhood. descriptor_method decides which method to use for the # descriptor generation harris_kd = KeypointDetector(block_size=args.harris_neighbourhood_size, descriptor_method=args.descriptor, \ keypoint_threshold=args.harris_keypoint_threshold, patch_size=args.patch_size) # Compute keypoints and descriptors for both the left and right images keypoint1, descriptor1 = harris_kd.detect_compute_descriptor( left_image) keypoint2, descriptor2 = harris_kd.detect_compute_descriptor( right_image) # Create matcher object. The matcher uses either normalized correlation or euclidean distance to generate matching keypoints. # The method is passed in while creating the object as matching_method matcher = Matcher(matching_method=args.matching_method) # Get matches for each descriptor in the left image. So the best matching descriptor is returned for each descriptor matches = matcher.match(descriptor1, descriptor2) # Sort the matches we have based on the distance between descriptors matches.sort(key=lambda x: x.distance) # Select top n matches as passed in the arguments matches = matches[:args.n_matches] # Create arrays to hold the coordinates of matches for both images set1 = np.zeros((len(matches), 2), dtype=np.float32) set2 = np.zeros((len(matches), 2), dtype=np.float32) # Fill in these arrays with coordinates from the top n matches. queryIdx refers to indices of keypoints in first image and trainIdx to the second image. # The coordinates of these keypoints are extracted from DMatch objects created during the matching process. for i, match in enumerate(matches): set1[i, :] = keypoint1[match.queryIdx].pt set2[i, :] = keypoint2[match.trainIdx].pt # RANSAC Affine transformation estimation. The two sets of keypoint coordinates are passed to the RANSAC function along with relevant arguments. # best_model is a dictionary with all metadata about the RANSAC process such as inlier count, inlier_indices, residuals and the best estimated affine transformation H best_model = RANSAC(set1, set2, init_points=args.RANSAC_init_points, N=args.RANSAC_iterations, inlier_threshold=args.RANSAC_inlier_threshold) # Collect all the inlier matches from the RANSAC estimation inlier_matches = [ match for idx, match in enumerate(matches) if idx in best_model["inlier_indices"] ] # Get the sensitivity score here for the keypoints and transformed keypoints for all the inliers. sensitivity = compute_euclidean_distance( set1[best_model["inlier_indices"]], set2[best_model["inlier_indices"]], best_model["H"]) logging.info("Sensitivity score: {}".format(sensitivity)) # Affine warp to create the final panorama if stitchedImage is None: stitchedImage = right_image stitchedImage = cv2.warpAffine( stitchedImage, best_model["H"][:-1, :], (left_image.shape[1] + stitchedImage.shape[1], left_image.shape[0])) stitchedImage[0:left_image.shape[0], 0:left_image.shape[1]] = left_image # Remove transformed image from the list. input_images.pop() # Postprocessing to remove black blocks in the image stitchedImage = post_process(stitchedImage) #Collect the arguments for experiment logging params = vars(args) params["Sensitivity"] = sensitivity # Keys of the best model are added to params to be saved params["n_inliers"] = best_model["n_inliers"] params["inlier_ratio"] = best_model["inlier_ratio"] params["average_residuals"] = best_model["average_residuals"] # # Log dict for drawing GUI/ Uploading to dashboard if not args.no_gui or args.wandb: log_dict = {} log_dict["left_image"] = left_image log_dict["right_image"] = right_image log_dict["keypoint1"] = keypoint1 log_dict["keypoint2"] = keypoint2 log_dict["matches"] = matches log_dict["inlier_matches"] = inlier_matches log_dict["sensitivity"] = sensitivity log_dict["stitchedImage"] = stitchedImage # # Draw only if GUI is enabled! if not args.no_gui: gui_display(log_dict)
Model = model().to(device) utils.path_checker(save_path) Model.load_state_dict(torch.load(Model_path)) for name in file_list: if name.split('.')[-1]!='nii': break test_set = Dataset(path=test_path+name, transform=transform) test_loader = DataLoader(test_set, batch_size=test_batch_size, shuffle=False) output = [] for index, img in enumerate(test_loader): if index==len(test_loader)-1: sys.stdout.write("\r[{}] [Batch {}/{}]\n".format(name, index+1, len(test_loader))) else: sys.stdout.write("\r[{}] [Batch {}/{}]".format(name, index+1, len(test_loader))) sys.stdout.flush() Model.eval() img = img.to(device) with torch.no_grad(): output.append(Model(img)) output[index] = torch.ge(output[index], 0.5).type(dtype=torch.float32) #二值化 output[index] = utils.post_process(output[index])#后处理,结果为uint16二值numpy数组 sys.stdout.write("\r[{}] [Saving] \n".format(name)) sys.stdout.flush() result = np.concatenate(output, axis=0).squeeze() utils.save(case_path=test_path, save_path=save_path, case_name=name, save_name=name, img=result)
# print('content_img size: ', content_img.size()) # utils.show_pic(style_img, 'style image') # utils.show_pic(content_img, 'content image') # ------------------------- # Eval() means the parameters of cnn are frozen. cnn = models.vgg19(pretrained=True).features.to(config.device0).eval() cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(config.device0) cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(config.device0) # Two different initialization ways input_img = torch.randn(1, 3, height_c, width_c).to(config.device0) # input_img = content_img.clone() # print('input_img size: ', input_img.size()) output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std, content_img, style_img, input_img, style_mask_tensor, content_mask_tensor, L) print('Style transfer completed') utils.save_pic(output, 'deep_style_tranfer') print() #-------------------------- print('Postprocessing......') utils.post_process(output, content_image_path) print('Done!')
predictions = np.zeros((len(files), 320, 480, 4)) for p in range(len(files)): pred = np.load(files[p]) for k in range(pred.shape[-1]): predictions[p, :, :, k] = cv2.threshold(pred[:, :, k], threshold, 1, cv2.THRESH_BINARY)[1] pred_masks = np.sum(predictions, axis=0) for k in range(pred_masks.shape[-1]): pred_mask = pred_masks[..., k].astype('float32') if pred_mask.shape != (350, 525): pred_mask = cv2.resize(pred_mask, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) pred_mask, num_predict = post_process(pred_mask, len(files) / 2.0, min_size[k], (350, 525)) if num_predict == 0: encoded_pixels.append('') else: r = mask2rle(pred_mask) encoded_pixels.append(r) # # Submission sub_df['EncodedPixels'] = encoded_pixels sub_df.to_csv('efnb4_unet_5fold_submission.csv', columns=['Image_Label', 'EncodedPixels'], index=False)
2: (0.7, 10000), 3: (0.6, 10000) } encoded_pixels = [] image_id = 0 for i, test_batch in enumerate(tqdm.tqdm(test_loader)): test_batch = {"features": test_batch[0].to(DEVICE)} output = model(test_batch["features"]) for i, batch in enumerate(output): for probability in batch: probability = probability.cpu().detach().numpy() if probability.shape != (350, 525): probability = cv2.resize(probability, dsize=(525, 350), interpolation=cv2.INTER_LINEAR) predict, num_predict = utils.post_process( sigmoid(probability), class_params[image_id % 4][0], class_params[image_id % 4][1]) if num_predict == 0: encoded_pixels.append('') else: r = utils.mask2rle(predict) encoded_pixels.append(r) image_id += 1 sub['EncodedPixels'] = encoded_pixels sub.to_csv('submission.csv', columns=['Image_Label', 'EncodedPixels'], index=False)
Model_path = '../7.pth' os.environ['CUDA_VISIBLE_DEVICES'] = '0' transform = transforms.Compose([ transforms.ToTensor() ]) save_path = path+'log/'+title+'_test/' device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") ##### test_set = Dataset(path=test_path, transform=transform, mode='test') test_loader = DataLoader(test_set, batch_size=test_batch_size, shuffle=False) Model = model(BN_enable=True, resnet_pretrain=False).to(device) utils.path_checker(save_path) Model.load_state_dict(torch.load(Model_path)) Model.eval() for index, (img,name) in enumerate(test_loader): Model.eval() img = img.to(device) with torch.no_grad(): output = Model(img) output = torch.ge(output, 0.5).type(dtype=torch.float32) #二值化 output = utils.post_process(output)#后处理 for i in range(test_batch_size): vutils.save_image(output[i,:,:,:], save_path+name[i].split('/')[1], padding=0) sys.stdout.write("\r[test] [Epoch {}/{}] [Batch {}/{}]".format(7, 10, index+1, len(test_loader))) sys.stdout.flush()
def detect(self, input_stream: Union[int, str]) -> None: """ Detect function responsible for working with camera\n given an input stream which can be 0 if webcamera is used\n or string path for video file\n Arguments: ---------------------------------- input_stream: Union[int, str] - int if webcamera is used or path to video file Returns: ---------------------------------- None - function visualize preprocessed output frame on window. """ camera = cv2.VideoCapture(input_stream) font = cv2.FONT_HERSHEY_PLAIN scalefactor: Union[int, float] = self.config.scalefactor img_size: tuple = tuple(self.config.size) mean: List[int] = self.config.mean swapRB: bool = self.config.swapRB score_threshold: float = self.config.score_threshold nms_threshold: float = self.config.nms_threshold timestamp_init_frames: int = self.config.timestamp_init_frames conf_threshold: float = self.config.conf_threshold write_perf_info: bool = self.config.write_perf_information skip_frames: int = self.config.skip_frames max_distance: int = self.config.max_distance is_on_rails_distance: int = self.config.is_on_rails_distance line: np.ndarray = np.array(self.config.line) if not camera.isOpened(): logging.error( "Detection suspended, camera is not opened, check your configuration" ) return frame_counter = 0 while True: _, frame = camera.read() if frame_counter % skip_frames != 0: frame_counter += 1 continue before = time.time() frame_shape: Tuple[int] = frame.shape blob = cv2.dnn.blobFromImage(image=frame, scalefactor=scalefactor, size=img_size, mean=mean, swapRB=swapRB) if self.config.use_opencv: self.model.setInput(blob) outputs = self.model.forward(self.output_layers) outputs = np.concatenate(outputs, axis=0) else: outputs = self.session.run(["classes", "boxes"], {"images": blob}) outputs = np.concatenate([outputs[1], outputs[0]], axis=1) matches = outputs[np.where( np.max(outputs[:, 4:], axis=1) > conf_threshold)] boxes, scores, class_ids = get_box_dimensions( matches, frame_shape[0], frame_shape[1], self.config.use_opencv) indexes = cv2.dnn.NMSBoxes(boxes, scores, score_threshold, nms_threshold) boxes, class_ids = post_process(boxes, class_ids, indexes) if timestamp_init_frames: if boxes is not None: self.tracker.feed_init_frame( multi_tracker=self.config.multi_tracker, frame=frame, bbox=boxes) self.detect_objects_near_line(boxes, class_ids, line, max_distance, is_on_rails_distance) for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(self.classes[class_ids[i]]) color: np.ndarray = self.colors[i] cv2.rectangle(frame, (x, y), (x + w, y + h), color) cv2.putText(frame, label, (x, y - 5), font, 2, color) cv2.imshow("Frame", frame) frame_counter += 1 if write_perf_info: after = time.time() logging.info(f"FPS: {1/(after-before)}") if cv2.waitKey(1) == ord("q"): break cv2.destroyAllWindows() camera.release()
def run_epoch(_model, data_loader, tag, optimizer=None, mean_loss=None, var_loss=None): assert tag in ["train", "val", "test"], "tag should be train, val or test" _loss, _loss_mean, _loss_var = 0, 0, 0 mean_pred, var_pred, mean_gold, var_gold = [], [], [], [] mean_gold_batch, var_gold_batch = None, None if tag == 'train': _model.train() else: _model.eval() data_size = len(data_loader.dataset) widgets = [tag, Percentage(), ' ', Bar('-'), ' ', Timer(), ' ', ETA()] _bar = ProgressBar(widgets=widgets, max_value=data_size) t = 0 for i, batch_data in enumerate(data_loader): if tag == "train" or tag == "val": _image_idx, _mean_idx, _var_idx = 0, 1, 2 image_batch = batch_data[_image_idx].to(DEVICE) mean_gold_batch = batch_data[_mean_idx].unsqueeze(-1) var_gold_batch = batch_data[_var_idx].unsqueeze(-1) mean_gold.append(mean_gold_batch) var_gold.append(var_gold_batch) mean_gold_batch = mean_gold_batch.to(DEVICE) var_gold_batch = var_gold_batch.to(DEVICE) else: image_batch = batch_data.to(DEVICE) t += image_batch.shape[0] _bar.update(t) if tag == "train": optimizer.zero_grad() mean_pred_batch, var_pred_batch = _model(image_batch) mean_pred.append(mean_pred_batch.cpu().detach()) var_pred.append(var_pred_batch.cpu().detach()) if DISCRETE_CLS and tag == "train": mean_gold_batch = mean_gold_batch.reshape(-1) var_gold_batch = var_gold_batch.reshape(-1) if tag == "train": batch_mean_loss = mean_loss(mean_pred_batch, mean_gold_batch) batch_var_loss = var_loss(var_pred_batch, var_gold_batch) batch_loss = 0.6 * batch_mean_loss + 0.4 * batch_var_loss batch_loss.backward() optimizer.step() _loss += batch_loss.item() _loss_mean += batch_mean_loss.item() _loss_var += batch_var_loss.item() print() mean_pred = torch.cat(mean_pred, dim=0) var_pred = torch.cat(var_pred, dim=0) if tag == "train" or tag == "val": mean_gold = torch.cat(mean_gold, dim=0) var_gold = torch.cat(var_gold, dim=0) if DISCRETE_REG: mean_pred, var_pred = post_process(mean_pred, var_pred) if DISCRETE_CLS: mean_pred = un_discrete_label(mean_pred, "mean") var_pred = un_discrete_label(var_pred, "var") mean_gold = un_discrete_label(mean_gold, "mean", True) var_gold = un_discrete_label(var_gold, "var", True) if tag == "train" or tag == "val": _rmse_mean = rmse(mean_pred, mean_gold) _rmse_var = rmse(var_pred, var_gold) if tag == "train": return _rmse_mean, _rmse_var, _loss, _loss_mean, _loss_var else: return _rmse_mean, _rmse_var else: return mean_pred, var_pred
def inference(config): # setup data_loader instances inference_loader = torch.utils.data.DataLoader(THUMOSInferenceDataset(config), batch_size=config.batch_size, shuffle=False, num_workers=8, pin_memory=True, drop_last=False, collate_fn=inference_collate_fn) # build model architecture and load checkpoint model = SSAD(config).to(device) checkpoint = torch.load(config.checkpoint_path + "/model_best.pth.tar") model.load_state_dict(checkpoint['state_dict']) model = model.to(device) model.eval() ''' ['xmin', 'xmax', 'conf', 'score_0', 'score_1', 'score_2', 'score_3', 'score_4', 'score_5', 'score_6', 'score_7', 'score_8', 'score_9', 'score_10', 'score_11', 'score_12', 'score_13', 'score_14', 'score_15', 'score_16', 'score_17', 'score_18', 'score_19', 'score_20'] ''' results = [] results_name = [] with torch.no_grad(): for n_iter, (batch_data, batch_video_names, batch_window_start) in enumerate(inference_loader): batch_data = batch_data.to(device) output_x, output_w, output_scores, output_labels = model(batch_data, device) output_labels = F.softmax(output_labels, dim=1) output_x = output_x.cpu().detach().numpy() output_w = output_w.cpu().detach().numpy() output_scores = output_scores.cpu().detach().numpy() output_labels = output_labels.cpu().detach().numpy() output_min = output_x - output_w / 2 output_max = output_x + output_w / 2 for ii in range(len(batch_video_names)): video_name = batch_video_names[ii] window_start = batch_window_start[ii] a_min = output_min[ii, :] a_max = output_max[ii, :] a_scores = output_scores[ii, :] a_labels = output_labels[ii, :, :] for jj in range(output_min.shape[-1]): corrected_min = max(a_min[jj] * config.window_size * config.unit_size, 0.) + window_start corrected_max = min(a_max[jj] * config.window_size * config.unit_size, config.window_size * config.unit_size) + window_start results_name.append([video_name]) results.append([corrected_min, corrected_max, a_scores[jj]] + a_labels[:, jj].tolist()) results_name = np.stack(results_name) results = np.stack(results) df = pd.DataFrame(results, columns=config.outdf_columns) df['video_name'] = results_name result_file = './results.txt' if os.path.isfile(result_file): os.remove(result_file) df = df[df.score_0 < config.filter_neg_threshold] df = df[df.conf > config.filter_conf_threshold] video_name_list = list(set(df.video_name.values[:])) for video_name in video_name_list: tmpdf = df[df.video_name == video_name] tmpdf = post_process(tmpdf, config) temporal_nms(config, tmpdf, result_file, video_name)
] my_metric = lambda x, y: MyModels.loss2acc(x, y, True) my_metric.__name__ = 'loss2acc' model.compile(optimizer=keras.optimizers.Adam(lr=1 - 3), loss=['mse'], metrics=[my_metric]) dbg = True queue.put({'model_path': config.model_path}) if 'gray' in config.type: ind = 1 else: ind = 3 for x, y in utils.gen_from_dir(config, True): break y_pred = model.predict(x) utils.my_imshow(x[0][..., :ind], block=False) utils.my_imshow(y[0][..., :ind], block=False) y_pred[0][..., :ind] = utils.post_process(x[0][..., :ind], y_to=y_pred[0][..., :1], config=config) utils.my_imshow(y_pred[0][..., :ind], block=False, name='pred_train') print utils.my_mse(y_pred[0][..., :ind], x[0][..., :ind]) break cnt = 0 tmp = imread('data/val_corr/cara1_04.png', mode='RGB') tmp = tmp.mean(axis=-1) / 255. for x, y in utils.gen_from_dir(config, False): x_1 = x.copy() y_pred = model.predict(x) # utils.my_imshow(x[0][..., :ind], block=False) # utils.my_imshow(y[0][..., :ind], block=False) y_pred[0][..., :ind] = utils.post_process(x[0][..., :ind], y_to=y_pred[0][..., :1], config=config) utils.my_imshow(y_pred[0][..., :ind] * 255., block=False, name='pred_val') print utils.my_mse(y_pred[0][..., :ind], x[0][..., :ind])