def save_images(self, webpage, visuals, image_path, aspect_ratio=1.0): image_dir = webpage.get_image_dir() short_path = ntpath.basename(image_path[0]) name = os.path.splitext(short_path)[0] webpage.add_header(name) ims = [] txts = [] links = [] for label, im in visuals.items(): image_name = '%s_%s.png' % (name, label) save_path = os.path.join(image_dir, image_name) h, w, _ = im.shape if aspect_ratio > 1.0: im = cv2.imresize(src=im, dsize=(h, int(w * aspect_ratio)), interpolation=cv2.INTER_CUBIC) if aspect_ratio < 1.0: im = cv2.imresize(src=im, dsize=(int(h / aspect_ratio), w), interpolation=cv2.INTER_CUBIC) #util.save_image(im, save_path) print("save in......", save_path) cv2.imwrite(save_path, im) ims.append(image_name) txts.append(label) links.append(image_name) webpage.add_images(ims, txts, links, width=self.win_size)
def loadDeeptofTestData(args): depth_ref_images = [] mpi_abs_images = [] with h5py.File('DeepToF_validation_1.7k_256x256.h5', 'r') as f: depth_ref = list(f['depth_ref']) mpi_abs = list(f['mpi_abs']) for i in range(len(depth_ref)): depth_ref_images.append(cv2.imresize(np.reshape(depth_ref[0], (256, 256)), (424, 512))) mpi_abs_images.append(cv2.imresize(np.reshape(mpi_abs[0], (256, 256)), (424, 512))) return (depth_ref_images, mpi_abs_images)
def predict(filename): img = cv2.imread(filename) img = cv2.imresize(img, (24, 24)) model = load_model(r'C:\Users\Wojtek\Documents\Projects\DeepLearningPlayground\Resnet\model') img = img/255. prediction = model.predict(img) return prediction
def __getitem__(self, index): hr_img = (self.hr_dataset[index]) if (lr_dataset == 'None'): lr_img = self.hr_dataset[index] else: lr_img = (self.lr_dataset[index]) # data augmentation [hr_img, lr_img] = augment([hr_img, lr_img], True, True) # SFM if (sfm > 0 and random.random() > 0.5): lr_img, mask = random_drop(lr_img, mode=0) if (lr_dataset != 'None'): lr_img = cv2.imresize(lr_img, (0, 0), fx=1 / scale, fy=1 / scale) # KMSR: kernel blur kernel_index = min(random.randint(0, 1999), len(self.kernel) - 1) kernel = self.kernel[kernel_index] lr_img[0, :, :] = signal.convolve2d(lr_img[0, :, :], kernel[0, :, :], 'same') lr_img[1, :, :] = signal.convolve2d(lr_img[1, :, :], kernel[0, :, :], 'same') lr_img[2, :, :] = signal.convolve2d(lr_img[2, :, :], kernel[0, :, :], 'same') return hr_img.astype('float'), lr_img.astype('float')
def recover_cls_masks(masks, rois, ih, iw, interp='bilinear'): """Decode 14x14 masks into final masks Arguments - masks : (N, C, 14, 14) float32, ranging [0,1] - rois : (N, 4) [xyxy] float32 - ih : image height - iw : image width - interp: bilinear or nearest Returns - recovered_masks : (N, ih, iw) uint8, range [0, 255] """ assert rois.shape[0] == masks.shape[0], '%s rois vs %d masks' % ( rois.shape[0], masks.shape[0]) num_rois = rois.shape[0] num_classes = masks.shape[1] recovered_masks = np.zeros((num_rois, num_classes, ih, iw), dtype=np.uint8) # (num_rois, ih, iw) rois = clip_np_boxes(rois, (ih, iw)) for i in np.arange(num_rois): # read mask of (C, 14, 14) float32 mask = masks[i, :, :, :] # range [0, 255] float32 mask *= 255. # resize h, w = int(rois[i, 3] - rois[i, 1] + 1), int(rois[i, 2] - rois[i, 0] + 1) x, y = int(rois[i, 0]), int(rois[i, 1]) for c in range(num_classes): m = mask[c] # (14, 14) m = imresize(m, (h, w), interp=interp) # (roi_h, roi_w) uint8 recovered_masks[i, c, y:y + h, x:x + w] = m return recovered_masks
def preprocess(img, dtype=np.float32): # img_gray = np.mean(img, axis=2) img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img_down = imresize(img_gray, (84, 84), interpolation=cv2.INTER_AREA) img_norm = img_down / 255. img_norm = np.asarray(img_norm, dtype=dtype) return img_norm
def show_heatmaps(cfg, img, scmap, pose, cmap="jet"): interp = "bilinear" all_joints = cfg.all_joints all_joints_names = cfg.all_joints_names subplot_width = 3 subplot_height = math.ceil((len(all_joints) + 1) / subplot_width) f, axarr = plt.subplots(subplot_height, subplot_width) for pidx, part in enumerate(all_joints): plot_j = (pidx + 1) // subplot_width plot_i = (pidx + 1) % subplot_width scmap_part = np.sum(scmap[:, :, part], axis=2) scmap_part = imresize(scmap_part, 8.0, interp='bicubic') scmap_part = np.lib.pad(scmap_part, ((4, 0), (4, 0)), 'minimum') curr_plot = axarr[plot_j, plot_i] curr_plot.set_title(all_joints_names[pidx]) curr_plot.axis('off') curr_plot.imshow(img, interpolation=interp) curr_plot.imshow(scmap_part, alpha=.5, cmap=cmap, interpolation=interp) curr_plot = axarr[0, 0] curr_plot.set_title('Pose') curr_plot.axis('off') curr_plot.imshow(visualize_joints(img, pose)) plt.show()
def load_data(path): """ load data into shared variables """ v, p, skeleton_feature, l = load_gzip(path) v = v[:, :, :2] # print v.shape[0] res_shape[0] = v.shape[0] v_new = empty(res_shape, dtype="uint8") for i in range(v.shape[0]): #batch if p[i] < 10: p[i] = 100 ofs = p[i] * ratio mid = v.shape[-1] / 2. sli = None if ofs < mid: start = int(round(mid - ofs)) end = int(round(mid + ofs)) sli = slice(start, end) for j in range(v.shape[2]): #maps for k in range(v.shape[3]): #frames #body img = v[i, 0, j, k] img = cut_img(img, 5) img = imresize(img, (h, h)) # if j==0: img = 255-misc.imfilter(img,"contour") v_new[i, 0, j, k] = img #hand img = v[i, 1, j, k] img = img[sli, sli] img = imresize(img, (h, h)) v_new[i, 1, j, k] = img vid, lbl = v_new, l # shuffle data ind = permutation(l.shape[0]) # ind = ind[:1000] vid = vid[:, :, :, :4, :, :] vid, skeleton_feature, lbl = vid[ind].astype("float32"), skeleton_feature[ ind].astype("float32"), lbl[ind].astype("float32") # set value # x_.set_value(vid, borrow=True) # y_.set_value(lbl, borrow=True) return vid, lbl, skeleton_feature
def load_image(): img = os.listdir("images")[0] image = np.array(cv2.imread("predict/" + img)) image = cv2.imresize(image, (64, 64)) image = np.array([image]) image = pre_process(image) return image
def save_img(img, filename): img = deprocess_img(img) img = img.numpy() img *= 255.0 img = img.clip(0, 255) img = np.transpose(img, (1, 2, 0)) img = cv2.imresize(img, (250, 200, 3)) img = img.astype(np.uint8) imsave(filename, img) print("Image saved as {}".format(filename))
def lr_images(images_real, downscale): images = [] for img in range(len(images_real)): images.append( imresize(images_real[img], (images_real[img].shape[0] // downscale, images_real[img].shape[1] // downscale))) # images.append(imresize(images_real[img], [images_real[img].shape[0]//downscale,images_real[img].shape[1]//downscale], interp='bicubic', mode=None)) images_lr = array(images) return images_lr
def _add_gt_image(self): # add back mean image = self._image_gt_summaries['image'] + cfg.PIXEL_MEANS # changes by me image = imresize( image[0], tuple(self._im_info[0][1::-1] / self._im_info[0][2])) # assume we only have 1 image # image = imresize(image[0], self._im_info[0][:2] / self._im_info[0][2]) # assume we only have 1 image # BGR to RGB (opencv uses BGR) self._gt_image = image[np.newaxis, :, :, ::-1].copy(order='C')
def save_lip_images(image_path, samples, out_dir): img_A = imread(image_path).astype(np.float) rows = img_A.shape[0] cols = img_A.shape[1] image = samples[0] img_split = image_path.split('/') img_id = img_split[-1][:-4] with open('{}/{}.txt'.format(out_dir, img_id), 'w') as f: for p in range(image.shape[2]): channel_ = image[:, :, p] if channel_.shape[0] != rows or channel_.shape[1] != cols: print('sizes do not match...') channel_ = cv2.imresize(channel_, [rows, cols], interp=cv2.INTER_NEAREST) r_, c_ = np.unravel_index(channel_.argmax(), channel_.shape) f.write('%d %d ' % (int(c_), int(r_)))
def getSegOverlay(im,box_curr,seg_curr,alpha=0.5): to_crop=[0-box_curr[0],0-box_curr[1],box_curr[2]-im.shape[0],box_curr[3]-im.shape[1]]; to_crop=[max(val,0) for val in to_crop]; box_start=[max(0,box_curr[0]),max(0,box_curr[1])]; seg_crop=seg_curr[to_crop[0]:seg_curr.shape[0]-to_crop[2],to_crop[1]:seg_curr.shape[1]-to_crop[3]]; heatmap=visualize.getHeatMap(seg_crop); if len(im.shape)<3: im=np.dstack((im,im,im)); heatmap_big=128*np.ones((im.shape)) # print heatmap_big.shape,im.shape,heatmap.shape,box_start box_end=[min(heatmap_big.shape[0],box_start[0]+heatmap.shape[0]),min(heatmap_big.shape[1],box_start[1]+heatmap.shape[1])] heatmap_big[box_start[0]:box_end[0],box_start[1]:box_end[1],:]=heatmap[:box_end[0]-box_start[0],:box_end[1]-box_start[1],:] if heatmap_big.shape!=im.shape: # print 'resizing',heatmap_big.shape,im.shape, heatmap_big=cv2.imresize(heatmap_big,(im.shape[1],im.shape[0])); # print heatmap_big.shape img_fuse=visualize.fuseAndSave(im,heatmap_big,alpha); return img_fuse;
def generate(values, nb_classes, batch_size, input_size, image_dir, anno_dir): while 1: random.shuffle(values) images, labels = update_inputs(batch_size=batch_size, input_size=input_size, num_classes=nb_classes) for i, d in enumerate(values): img = imresize(imread(os.path.join(image_dir, d['image']), 1), input_size[::-1]) y = imread(os.path.join(anno_dir, d['anno']), 0) y = (y > 0) * 1 # findgrass specific labeling h, w = input_size y = zoom(y, (1.*h/y.shape[0], 1.*w/y.shape[1]), order=1, prefilter=False) y = (np.arange(nb_classes) == y[:,:,None]).astype('float32') assert y.shape[2] == nb_classes images[i % batch_size] = img labels[i % batch_size] = y if (i + 1) % batch_size == 0: yield images, labels images, labels = update_inputs(batch_size=batch_size, input_size=input_size, num_classes=nb_classes)
def run(self, img, target_width, target_height): if target_width > self.MAX_WIDTH or target_height > self.MAX_HEIGHT: print( f'target height and width must be less than {self.MAX_HEIGHT} and {self.MAX_WIDTH} respectively' ) return None img_h, img_w, _ = img.shape scale_h = target_height / img_h scale_w = target_width / img_w scale = max(scale_h, scale_w) img_scaled = imresize(img, (0, 0), fx=scale, fy=scale) rows_tobe_carved = img_scaled.shape[0] - target_height cols_tobe_carved = img_scaled.shape[1] - target_width out = self._crop_r(img_scaled, rows_tobe_carved) out = self._crop_c(out, cols_tobe_carved) return out
def recover_masks(masks, rois, ih, iw, interp='bilinear'): """Decode 14x14 masks into final masks Params - masks : of shape (N, 14, 14) float32, ranging [0, 1] - rois : of shape (N, 4) [x1, y1, x2, y2] float32. Note there is no batch_ids in rois! - ih : image height - iw : image width - interp: bilinear or nearest Returns - recovered_masks : of shape (N, ih, iw) uint8, range [0, 255] """ assert rois.shape[0] == masks.shape[0], '%s rois vs %d masks' % ( rois.shape[0], masks.shape[0]) num_rois = rois.shape[0] recovered_masks = np.zeros((num_rois, ih, iw), dtype=np.uint8) # (num_rois, ih, iw) rois = clip_np_boxes(rois, (ih, iw)) for i in np.arange(num_rois): # read mask of (14, 14) float32 mask = masks[i, :, :] # range [0, 255] float32 mask *= 255. # resize will convert it to uint8 [0, 255] h, w = int(rois[i, 3] - rois[i, 1] + 1), int(rois[i, 2] - rois[i, 0] + 1) x, y = int(rois[i, 0]), int(rois[i, 1]) # changes by me inter = cv2.INTER_LINEAR if (interp == 'bilinear') else cv2.INTER_NEAREST mask = imresize(mask, (w, h), interpolation=inter) # (roi_h, roi_w) uint8 # mask = imresize(mask, (h, w), interp=interp) # (roi_h, roi_w) uint8 # paint recovered_masks[i, y:y + h, x:x + w] = mask return recovered_masks
def get_roi(img, scale, ori_roi): ''' here img is [h,w,3] ''' shape = [int(s * scale) for s in img.shape] roi = np.ceil(ori_roi * scale) temp = imresize(img, (shape[1], shape[0])) #temp=temp_im[roi[1]:roi[1]+roi[3],roi[0]:roi[0]+roi[2],:] a = np.ones(shape=(INPUT_SIZE, INPUT_SIZE, 3)) * 128 offset_h = max(int(roi[1] + roi[3] / 2 - INPUT_SIZE / 2.0), 0) offset_w = max(int(roi[0] + roi[2] / 2 - INPUT_SIZE / 2.0), 0) h = min(INPUT_SIZE / 2.0, roi[1] + roi[3] / 2) + min( INPUT_SIZE / 2.0, shape[0] - (roi[1] + roi[3] / 2)) w = min(INPUT_SIZE / 2.0, roi[0] + roi[2] / 2) + min( INPUT_SIZE / 2.0, shape[1] - (roi[0] + roi[2] / 2)) a_offest_h = max(int(INPUT_SIZE / 2.0 - (roi[1] + roi[3] / 2)), 0) a_offest_w = max(int(INPUT_SIZE / 2.0 - (roi[0] + roi[2] / 2)), 0) a[a_offest_h:a_offest_h + h, a_offest_w:a_offest_w + w, :] = temp[offset_h:offset_h + h, offset_w:offset_w + w, :] return a, (-a_offest_h + offset_h, -a_offest_w + offset_w, h, w)
def _sample_rois(all_rois, all_scores, gt_boxes, gt_masks, fg_rois_per_image, rois_per_image, num_classes): """Generate a random sample of RoIs comprising foreground and background examples. Return: - labels: (Nkp, ) - rois : (Nkp, 5), [0 x1 y1 x2 y2] - roi_scores : (Nkp, ) - bbox_targets: (Nkp, 4k) - bbox_inside_weights: (Nkp, 4k) """ # overlaps: (rois x gt_boxes) all_rois_data = all_rois.data gt_boxes_data = gt_boxes.data overlaps = bbox_overlaps(all_rois_data[:, 1:5], gt_boxes_data[:, :4]) max_overlaps, gt_assignment = overlaps.max(1) # cuda tensor labels = gt_boxes[gt_assignment, [4]] # cuda Variable # Select foreground RoIs as those with >= FG_THRESH overlap fg_inds = (max_overlaps >= cfg.TRAIN.FG_THRESH).nonzero().view(-1) # Guard against the case when an image has fewer than fg_rois_per_image # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI) bg_inds = ((max_overlaps < cfg.TRAIN.BG_THRESH_HI) + (max_overlaps >= cfg.TRAIN.BG_THRESH_LO) == 2).nonzero().view(-1) # Small modification to the original version where we ensure a fixed number of regions are sampled if fg_inds.numel() > 0 and bg_inds.numel() > 0: fg_rois_per_image = min(fg_rois_per_image, fg_inds.numel()) fg_inds = fg_inds[torch.from_numpy(npr.choice(np.arange(0, fg_inds.numel()), size=int(fg_rois_per_image), replace=False)).long().cuda()] bg_rois_per_image = rois_per_image - fg_rois_per_image to_replace = bg_inds.numel() < bg_rois_per_image bg_inds = bg_inds[torch.from_numpy(npr.choice(np.arange(0, bg_inds.numel()), size=int(bg_rois_per_image), replace=to_replace)).long().cuda()] elif fg_inds.numel() > 0: to_replace = fg_inds.numel() < rois_per_image fg_inds = fg_inds[torch.from_numpy(npr.choice(np.arange(0, fg_inds.numel()), size=int(rois_per_image), replace=to_replace)).long().cuda()] fg_rois_per_image = rois_per_image elif fg_inds.numel() == 0: # we always make fg_inds.numel() > 0 zeros = Variable(all_rois.data.new(gt_boxes.size(0), 1)) all_rois = torch.cat( (all_rois, torch.cat((zeros, gt_boxes[:, :-1]), 1)) , 0) # not sure if it a wise appending, but anyway i am not using it all_scores = torch.cat((all_scores, zeros), 0) return _sample_rois(all_rois, all_scores, gt_boxes, gt_masks, fg_rois_per_image, rois_per_image, num_classes) # elif bg_inds.numel() > 0: # to_replace = bg_inds.numel() < rois_per_image # bg_inds = bg_inds[torch.from_numpy(npr.choice(np.arange(0, bg_inds.numel()), size=int(rois_per_image), replace=to_replace)).long().cuda()] # fg_rois_per_image = 0 else: import pdb pdb.set_trace() # The indices that we're selecting (both fg and bg) keep_inds = torch.cat([fg_inds, bg_inds], 0) # Select sampled values from various arrays: labels = labels[keep_inds].contiguous() # Clamp labels for the background RoIs to 0 labels[int(fg_rois_per_image):] = 0 rois = all_rois[keep_inds].contiguous() roi_scores = all_scores[keep_inds].contiguous() bbox_target_data = _compute_targets( rois[:, 1:5].data, gt_boxes[gt_assignment[keep_inds]][:, :4].data, labels.data) bbox_targets, bbox_inside_weights = \ _get_bbox_regression_labels(bbox_target_data, num_classes) # Get masks, float (num_boxes, 14, 14) # corresponding to the selected boxes mask_targets = torch.FloatTensor(fg_inds.numel(), cfg.MASK_SIZE, cfg.MASK_SIZE).cuda() mix = 0 for i in fg_inds.cpu().numpy().tolist(): roi = all_rois_data[i] # tensor [xyxyc] cropped = gt_masks[gt_assignment[i], int(roi[2]):int(roi[4])+1, int(roi[1]):int(roi[3])+1] # uint8 {0,1} # changes by me cropped = imresize(cropped, (cfg.MASK_SIZE, cfg.MASK_SIZE), interpolation=cv2.INTER_NEAREST) # still uint8 {0,1} # cropped = imresize(cropped, (cfg.MASK_SIZE, cfg.MASK_SIZE), interp='nearest') # still uint8 {0,1} cropped = cropped.astype(np.float32) # float32, range [0,1] mask_targets[mix,:,:] = torch.from_numpy(cropped).cuda() mix += 1 assert mask_targets.max() <= 1.0001 return labels, rois, roi_scores, bbox_targets, bbox_inside_weights, mask_targets
def load_and_resize(image_filename): img = cv2.imread(image_filename) img = cv2.imresize(img, (size, size))
batchstream) engine = trt.lite.Engine( framework="c1", deployfile="./models/pretrained/cmupose/pose_deploy_linevec.prototxt", modelfile="./models/pretrained/cmupose/pose_iter_440000.caffemodel", max_batch_size=10, max_workspace_size=(1 << 20), input_nodes={"image": (3, args.input_height, args.input_width)}, output_nodes=["net_output"], #preprocessors={"image":sub_mean_chw}, # postprocessors={"score":color_map}, data_type=trt.infer.DataType.INT8, calibrator=int8_calibrator, logger_severity=trt.infer.LogSeverity.INFO) image = cv2.imread(args.imgpath) image = cv2.imresize(image, (args.input_width, args.input_height)) concat_stage7 = engine.infer(image)[0] heatMat, pafMat = concat_stage7[:19, :, :], concat_stage7[19:, :, :] humans = PoseEstimator.estimate(heatMat, pafMat) process_img = CocoPose.display_image(image, heatMat, pafMat, as_numpy=True) image = cv2.imread(args.imgpath) image_h, image_w = image.shape[:2] image = TfPoseEstimator.draw_humans(image, humans) scale = 480.0 / image_h newh, neww = 480, int(scale * image_w + 0.5) image = cv2.resize(image, (neww, newh), interpolation=cv2.INTER_AREA) convas = np.zeros([480, 640 + neww, 3], dtype=np.uint8) convas[:, :640] = process_img convas[:, 640:] = image cv2.imwrite("cmupose_int8.jpg", convas)
def main(): args = parser.parse_args() if args.gt_type == 'KITTI': from kitti_eval.depth_evaluation_utils import test_framework_KITTI as test_framework elif args.gt_type == 'stillbox': from stillbox_eval.depth_evaluation_utils import test_framework_stillbox as test_framework elif args.gt_type == 'pfm': from pfm_eval.depth_evaluation_utils import test_framework_stillbox as test_framework disp_net = getattr(models, args.dispnet)().cuda() weights = torch.load(args.pretrained_dispnet) disp_net.load_state_dict(weights['state_dict']) disp_net.eval() if args.pretrained_posenet is None: print( 'no PoseNet specified, scale_factor will be determined by median ratio, which is kiiinda cheating\ (but consistent with original paper)') seq_length = 0 else: weights = torch.load(args.pretrained_posenet) seq_length = int(weights['state_dict']['conv1.0.weight'].size(1) / 3) pose_net = getattr(models, args.posenet)(nb_ref_imgs=seq_length - 1, output_exp=False).cuda() pose_net.load_state_dict(weights['state_dict'], strict=False) dataset_dir = Path(args.dataset_dir) if args.dataset_list is not None: with open(args.dataset_list, 'r') as f: test_files = list(f.read().splitlines()) else: test_files = [ file.relpathto(dataset_dir) for file in sum([ dataset_dir.files('*.{}'.format(ext)) for ext in args.img_exts ], []) ] framework = test_framework(dataset_dir, test_files, seq_length, args.min_depth, args.max_depth) print('{} files to test'.format(len(test_files))) errors = np.zeros((2, 7, len(test_files)), np.float32) if args.output_dir is not None: output_dir = Path(args.output_dir) viz_dir = output_dir / 'viz' print("Saving output to", viz_dir) output_dir.makedirs_p() viz_dir.makedirs_p() for j, sample in enumerate(tqdm(framework)): tgt_img = sample['tgt'] ref_imgs = sample['ref'] h, w, _ = tgt_img.shape if (not args.no_resize) and (h != args.img_height or w != args.img_width): tgt_img = imresize( tgt_img, (args.img_height, args.img_width)).astype(np.float32) ref_imgs = [ imresize(img, (args.img_height, args.img_width)).astype(np.float32) for img in ref_imgs ] tgt_img = np.transpose(tgt_img, (2, 0, 1)) ref_imgs = [np.transpose(img, (2, 0, 1)) for img in ref_imgs] tgt_img = torch.from_numpy(tgt_img).unsqueeze(0) tgt_img = ((tgt_img / 255 - 0.5) / 0.5).cuda() tgt_img_var = Variable(tgt_img, volatile=True) ref_imgs_var = [] for i, img in enumerate(ref_imgs): img = torch.from_numpy(img).unsqueeze(0) img = ((img / 255 - 0.5) / 0.5).cuda() ref_imgs_var.append(Variable(img, volatile=True)) pred_disp = disp_net(tgt_img_var) if args.spatial_normalize: pred_disp = spatial_normalize(pred_disp) pred_disp = pred_disp.data.cpu().numpy()[0, 0] if args.output_dir is not None: if j == 0: predictions = np.zeros((len(test_files), *pred_disp.shape)) predictions[j] = 1 / pred_disp depth_viz = tensor2array(torch.FloatTensor(pred_disp), max_value=None, colormap='magma') depth_viz = np.transpose(depth_viz, (1, 2, 0)) depth_viz_im = Image.fromarray((255 * depth_viz).astype('uint8')) depth_viz_im.save(viz_dir / str(j).zfill(4) + 'depth.png') mean_errors = errors.mean(2) error_names = ['abs_rel', 'sq_rel', 'rms', 'log_rms', 'a1', 'a2', 'a3'] if args.pretrained_posenet: print("Results with scale factor determined by PoseNet : ") print("{:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}".format( *error_names)) print( "{:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}" .format(*mean_errors[0])) print( "Results with scale factor determined by GT/prediction ratio (like the original paper) : " ) print("{:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}, {:>10}".format( *error_names)) print( "{:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}, {:10.4f}". format(*mean_errors[1])) if args.output_dir is not None: np.save(output_dir / 'predictions.npy', predictions)
import numpy as np import keras.models from keras.models import model_from_json # from scipy.misc import imread, imresize,imshow import cv2 import matplotlib.pyplot as plt json_file = open('model.json','r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) #load woeights into new model loaded_model.load_weights("model.h5") print("Loaded Model from disk") #compile and evaluate loaded model loaded_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy']) #loss,accuracy = model.evaluate(X_test,y_test) #print('loss:', loss) #print('accuracy:', accuracy) x = cv2.imread('output.png') x = np.invert(x) x = cv2.imresize(x,(28,28)) plt.imshow(x) x = x.reshape(1,28,28,1) out = loaded_model.predict(x) print(out) print(np.argmax(out,axis=1))
if __name__ == '__main__': origimg = plt.imread('1.jpg') if len(origimg.shape) == 3: img = origimg.mean(axis=-1) else: img = origimg keyPoints,discriptors = SIFT(img) origimg2 = plt.imread('2.jpg') if len(origimg.shape) == 3: img2 = origimg2.mean(axis=-1) else: img2 = origimg2 ScaleRatio = img.shape[0]*1.0/img2.shape[0] img2 = imresize(img2,(img.shape[0],int(round(ScaleRatio*img2.shape[1])))) keyPoints2, discriptors2 = SIFT(img2,True) knn = KNeighborsClassifier(n_neighbors=1) knn.fit(discriptors,[0]*len(discriptors)) match = knn.kneighbors(discriptors2,n_neighbors=1,return_distance=True) keyPoints = np.array(keyPoints)[:,:2] keyPoints2 = np.array(keyPoints2)[:,:2] keyPoints2[:, 1] = img.shape[1] + keyPoints2[:, 1] origimg2 = imresize(origimg2,img2.shape) result = np.hstack((origimg,origimg2)) keyPoints = keyPoints[match[1][:,0]]
def test(args): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model_file_name = os.path.split(args.model_path)[1] model_name = model_file_name[:model_file_name.find("_")] # Setup image print("Read Input Image from : {}".format(args.img_path)) img = cv2.imread(args.img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) data_loader = get_loader(args.dataset) loader = data_loader(root=None, is_transform=True, img_norm=args.img_norm, test_mode=True) n_classes = loader.n_classes resized_img = cv2.resize(img, (loader.img_size[1], loader.img_size[0]), interpolation=cv2.INTER_CUBIC) orig_size = img.shape[:-1] if model_name in ["pspnet", "icnet", "icnetBN"]: # uint8 with RGB mode, resize width and height which are odd numbers img = cv2.resize( img, (orig_size[1] // 2 * 2 + 1, orig_size[0] // 2 * 2 + 1)) else: img = cv2.resize(img, (loader.img_size[1], loader.img_size[0])) img = img[:, :, ::-1] img = img.astype(np.float64) img -= loader.mean if args.img_norm: img = img.astype(float) / 255.0 # NHWC -> NCHW img = img.transpose(2, 0, 1) img = np.expand_dims(img, 0) img = torch.from_numpy(img).float() # Setup Model model_dict = {"arch": model_name} model = get_model(model_dict, n_classes, version=args.dataset) state = convert_state_dict( torch.load(args.model_path, map_location='cpu')["model_state"]) model.load_state_dict(state) model.eval() model.to(device) images = img.to(device) outputs = model(images) if args.dcrf: unary = outputs.data.cpu().numpy() unary = np.squeeze(unary, 0) unary = -np.log(unary) unary = unary.transpose(2, 1, 0) w, h, c = unary.shape unary = unary.transpose(2, 0, 1).reshape(loader.n_classes, -1) unary = np.ascontiguousarray(unary) resized_img = np.ascontiguousarray(resized_img) d = dcrf.DenseCRF2D(w, h, loader.n_classes) d.setUnaryEnergy(unary) d.addPairwiseBilateral(sxy=5, srgb=3, rgbim=resized_img, compat=1) q = d.inference(50) mask = np.argmax(q, axis=0).reshape(w, h).transpose(1, 0) decoded_crf = loader.decode_segmap(np.array(mask, dtype=np.uint8)) dcrf_path = args.out_path[:-4] + "_drf.png" cv2.imsave(dcrf_path, decoded_crf) print("Dense CRF Processed Mask Saved at: {}".format(dcrf_path)) pred = np.squeeze(outputs.data.max(1)[1].cpu().numpy(), axis=0) if model_name in ["pspnet", "icnet", "icnetBN"]: pred = pred.astype(np.float32) # float32 with F mode, resize back to orig_size pred = cv2.imresize(pred, orig_size, "nearest", mode="F") decoded = loader.decode_segmap(pred) print("Classes found: ", np.unique(pred)) #cv2.imwrite(args.out_path, decoded) print("Segmentation Mask Saved at: {}".format(args.out_path)) plt.imshow(decoded) return (decoded * 255).astype(int)
def read_img(path, size=None, dtype=np.float32): img = imread(path) if size is not None: img = imresize(img, size) return img.astype(dtype)
def preprocess_img(img, input_shape): img = imresize(img, input_shape) img = img - DATA_MEAN img = img[:, :, ::-1] img.astype('float32') return img
def multi_scale_infer(img_path, rect): starting = rect[3] * 1.2 * 0.8 ending = rect[3] * 1.2 * 3.0 ms = np.arange(np.log2(INPUT_SIZE / ending), np.log2(INPUT_SIZE / starting), 1.0 / 4.0) multi_scale = 2**ms ####get sym and module ctx = mx.cpu() sym = get_sym() mod = mx.mod.Module(sym, data_names=( 'image', 'center_map', ), label_names=(), context=ctx) mod.bind(data_shapes=[('image', (1, 3, 368, 368)), ('center_map', (1, 1, 368, 368))], label_shapes=[], for_training=False) arg_params, aux_params = load_params('cpm_infer/mpii.params') mod.init_params(initializer=None, arg_params=arg_params, aux_params=aux_params, allow_missing=False, force_init=True) img = Image.open(img_path) img = np.array(img, dtype=np.float32) center_map = get_center_map((0, 0, 368, 368), 21) output = [] offset = [] stamp = datetime.now().strftime('%H_%M_%S') print stamp for scale in multi_scale: im, off = get_roi(img, scale, rect) image = preprocess_image(im) mod.forward(mx.io.DataBatch( [mx.nd.array(image), mx.nd.array(center_map)], []), is_train=False) out = [np.squeeze(ot.asnumpy()) for ot in mod.get_outputs()] output.append(out) offset.append(off) stamp = datetime.now().strftime('%H_%M_%S') print stamp final = np.zeros((img.shape[0], img.shape[1], 15, NSTAGE)) for k, scale in enumerate(multi_scale): op = output[k] os = offset[k] tmp = np.zeros((img.shape[0] * scale, img.shape[1] * scale, 15)) tmp_offset_h = max(os[0], 0) tmp_offset_w = max(os[1], 0) h = os[2] w = os[3] op_offset_h = max(-os[0], 0) op_offset_w = max(-os[1], 0) for i in range(NSTAGE): opi = op[i] opi = np.swapaxes(opi, 0, 1) opi = np.swapaxes(opi, 1, 2) tm = imresize(opi, (368, 368)) tmp[tmp_offset_h:tmp_offset_h + h, tmp_offset_w:tmp_offset_w + w, :] = tm[op_offset_h:op_offset_h + h, op_offset_w:op_offset_w + w, :] final[:, :, :, i] += imresize(tmp, (img.shape[1], img.shape[0])) final /= multi_scale.shape[0] heat_plot(img.astype(np.int8), final[:, :, :, 5], 15)
def _imresize(image_array, size): return cv2.imresize(image_array, size)
def get_minibatch(roidb, num_classes): """Given a roidb, construct a minibatch sampled from it.""" num_images = len(roidb) # Sample random scales to use for each image in this batch random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES), size=num_images) assert(cfg.TRAIN.BATCH_SIZE % num_images == 0), \ 'num_images ({}) must divide BATCH_SIZE ({})'. \ format(num_images, cfg.TRAIN.BATCH_SIZE) # Get the input image blob, formatted for caffe im_blob, im_scales = _get_image_blob(roidb, random_scale_inds) blobs = {'data': im_blob} assert len(im_scales) == 1, "Single batch only" assert len(roidb) == 1, "Single batch only" # gt boxes: ndarray float32 (n, 5), [x1, y1, x2, y2, cls] if cfg.TRAIN.USE_ALL_GT: # Include all ground truth boxes gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0] else: # For the COCO ground truth boxes, exclude the ones that are ''iscrowd'' gt_inds = np.where( roidb[0]['gt_classes'] != 0 & np.all(roidb[0]['gt_overlaps'].toarray() > -1.0, axis=1))[0] gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32) gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0] gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds] blobs['gt_boxes'] = gt_boxes # gt masks: (n, scaled_height, scaled_width) as we only have one image # ndarray uint8, {0, 1} flipped = roidb[0]['flipped'] segms = [roidb[0]['segms'][k] for k in gt_inds.tolist()] assert len(segms) == gt_boxes.shape[0], '%s segms vs %s gt_boxes' % ( len(segms), gt_boxes.shape[0]) ori_h, ori_w = roidb[0]['height'], roidb[0]['width'] gt_masks = np.empty((len(segms), im_blob.shape[1], im_blob.shape[2]), dtype=np.uint8) for i, segm in enumerate(segms): m = segmToMask(segm, ori_h, ori_w) # (ih, iw) uint8 {0,1} assert m.dtype == 'uint8' # changes by me m = imresize( m, dsize=(im_blob.shape[2], im_blob.shape[1]), interpolation=cv2.INTER_NEAREST) # (blob_h, blob_w) uint8 {0,1} # m = imresize(m, size=(im_blob.shape[1], im_blob.shape[2]), interp='nearest') # (blob_h, blob_w) uint8 {0,1} if flipped: m = np.flip(m, 1) # (blob_h, blob_w) uint8 {0,1} gt_masks[i] = m blobs['gt_masks'] = gt_masks # im_info blobs['im_info'] = np.array( [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]], dtype=np.float32) return blobs