def get_shape_from_img(xml_path): img_path = wmlu.change_suffix(xml_path, "jpg") if not os.path.exists(img_path): print(f"Error find img {img_path} faild.") return [0, 0, 3] else: return list(wmli.imread(img_path).shape)
def test_min_area_rect1(self): with self.test_session() as sess: target_points = [[[279.49993896484375, 223.49996948242188], [286.99993896484375, 78.99998474121094], [-0.0, 0.0]], [[279.5, 223.50003051757812], [292.7421875, 89.09545135498047], [-45.0, 0.0]], [[259.5, 247.5], [423.0, 47.0], [-0.0, 0.0]], [[259.75, 263.25006103515625], [432.042236328125, 60.81117630004883], [-45.0, 0.0]], [[263.5, 259.4999694824219], [46.99999237060547, 422.99993896484375], [-0.0, 0.0]], [[263.75, 251.75], [60.81117630004883, 432.042236328125], [-45.0, 0.0]]] img_files = [] for i in range(6): img_files.append(f"./imgs/img{i}.jpg") img = [] for f in img_files: img.append(wmli.nprgb_to_gray(wmli.imread(f)).astype(np.uint8)) imgs = np.stack(img, axis=0) res = wop.min_area_rect(imgs, res_points=False) sess = tf.Session() sess.run(tf.global_variables_initializer()) res = sess.run(res) self.assertAllClose(res, target_points, atol=1e-3)
def trans_one_file(xml_file,save_dir,labels,img_suffix): img_file = wmlu.change_suffix(xml_file,img_suffix) if not osp.exists(img_file): print(f"Find {img_file} faild.") return shape, bboxes, labels_names, difficult, truncated,probs = read_voc_xml(xml_file,absolute_coord=True) _bboxes = [] _labels_name = [] remove_nr = 0 remove_labels = [] bboxes2remove = [] for i,l in enumerate(labels_names): if l in labels: remove_nr += 1 bboxes2remove.append(bboxes[i]) remove_labels.append(l) continue _bboxes.append(bboxes[i]) _labels_name.append(l) if remove_nr==0: wmlu.try_link(img_file, save_dir) shutil.copy(xml_file,save_dir) else: print(f"{wmlu.base_name(xml_file)} remove {remove_nr} labels, labels is {remove_labels}") img_save_path = osp.join(save_dir,osp.basename(img_file)) xml_save_path = osp.join(save_dir,osp.basename(xml_file)) img = wmli.imread(img_file) img = wmli.remove_boxes_of_img(img,np.array(bboxes2remove).astype(np.int32)) wmli.imwrite(img_save_path,img) write_voc_xml(xml_save_path,img_save_path,shape,_bboxes,_labels_name,is_relative_coordinate=False)
def test_min_area_rect(self): with self.test_session() as sess: target_points = [[[135.99996948242188, 262.9999694824219], [135.99996948242188, 183.99996948242188], [422.9999084472656, 183.99996948242188], [422.9999084472656, 262.9999694824219]], [[207.5, 358.5000305175781], [144.5, 295.5000305175781], [351.5, 88.50003051757812], [414.5, 151.50003051757812]], [[48.0, 271.0], [48.0, 224.0], [471.0, 224.0], [471.0, 271.0]], [[128.5, 437.50006103515625], [85.5, 394.50006103515625], [391.0, 89.00006103515625], [434.0, 132.00006103515625]], [[240.0, 470.99993896484375], [240.0, 48.0], [287.0, 48.0], [287.0, 470.99993896484375]], [[395.0, 426.0], [89.5, 120.5], [132.5, 77.5], [438.0, 383.0]]] img_files = [] for i in range(6): img_files.append(f"./imgs/img{i}.jpg") img = [] for f in img_files: img.append(wmli.nprgb_to_gray(wmli.imread(f)).astype(np.uint8)) imgs = np.stack(img, axis=0) res = wop.min_area_rect(imgs, res_points=True) res1 = wop.min_area_rect(imgs, res_points=False) sess.run(tf.global_variables_initializer()) res, res1 = sess.run([res, res1]) self.assertAllClose(res, target_points, atol=1e-3) print("X:", res1)
def next_batch(self, start_idx, batch_size, vol_dim=64, process="train", shuffle_view=True): tmp_ids = [] if process == "train": tmp_ids = self.trainIds[start_idx:start_idx+batch_size] elif process == "val": tmp_ids = self.valIds[start_idx:start_idx+batch_size] elif process == "test": tmp_ids = self.testIds[start_idx:start_idx+batch_size] res_imgs = [] # [batch_size, (im_height, im_width, 3)] res_vols = [] # [batch, (vol_depth, vol_height, vol_width, 3)] for ins_id in tmp_ids: # Load image view_path = os.path.join(self.base_path, self.cat_id, ins_id, "views/*.png") im_paths = glob.glob(view_path) if shuffle_view: shuffle(im_paths) im_path = im_paths[0] im = img_utils.imread(im_path) res_imgs.append(im) # Load volume vol_path = os.path.join(self.base_path, self.cat_id, ins_id, "models/model_normalized_{}.h5".format(vol_dim)) f = h5py.File(vol_path) vol = f['data'][:] res_vols.append(vol) return np.array(res_imgs), np.array(res_vols)
def main(_): args = default_argument_parser().parse_args() test_dir = args.test_data_dir save_dir = args.save_data_dir wmlu.create_empty_dir(save_dir, remove_if_exists=True) files = glob.glob(os.path.join(test_dir, "*.jpg")) m = predm.PredictModel() m.restoreVariables() m.remove_batch() def id_to_text(id): return m.trainer.category_index[id] for file in files: img = wmli.imread(file) img = np.expand_dims(img, axis=0) m.predictImages(img) save_path = os.path.join(save_dir, os.path.basename(file)) xml_path = wmlu.change_suffix(save_path, "xml") shutil.copy(file, save_path) labels = [id_to_text(id) for id in m.res_data[RD_LABELS]] pvt.writeVOCXml(xml_path, m.res_data[RD_BOXES], labels) if RD_FULL_SIZE_MASKS in m.res_data: annotations = lmt.trans_odresult_to_annotations_list(m.res_data) json_path = wmlu.change_suffix(save_path, "json") lmt.save_labelme_datav1(json_path, file, img, annotations, label_to_text=id_to_text) img_save_path = wmlu.change_suffix(xml_path, "jpg") wmli.imwrite(img_save_path, m.res_data[RD_RESULT_IMAGE])
def _process_image(self,xml_file,img_file): if not os.path.exists(img_file): return None,None,None,None,None,None,None if self.image_preprocess is not None: img = wmli.imread(img_file) img = self.image_preprocess(img) image_data = wmli.encode_img(img) else: image_data = tf.gfile.FastGFile(img_file, 'rb').read() shape, _bboxes, _labels_text, _difficult, _truncated,_ = odu.read_voc_xml(xml_file, adjust=None) _labels = self.labels_text_to_labels(_labels_text) bboxes = [] labels_text = [] difficult = [] truncated = [] labels = [] for data in zip(_bboxes,_labels,_labels_text,_difficult,_truncated): if self.category_id_filter(data[1]): bboxes.append(data[0]) labels.append(data[1]) labels_text.append(data[2]) difficult.append(data[3]) truncated.append(data[4]) if len(labels) == 0: #print(f"Ignore {name}.") return None,None,None,None,None,None,None return image_data, shape, bboxes, labels, labels_text, difficult, truncated
def next_batch(self, start_idx, batch_size, shuffle_view=True): """ Get the next training batch. """ tmp_names = self.train_names[start_idx:start_idx+batch_size] res_imgs = [] # [batch_size, (im_height, im_width, 3)] res_vols = [] # [batch, (vol_depth, vol_height, vol_width, 3)] for name in tmp_names: cloth = name[0] mesh = name[1] # Load image view_path = os.path.join(self.base_path, cloth, mesh, "views/*.png") im_paths = glob.glob(view_path) if shuffle_view: shuffle(im_paths) im_path = im_paths[0] im = img_utils.imread(im_path) res_imgs.append(im) # Load volume vol_path = os.path.join(self.base_path, cloth, mesh, "{}.h5".format(mesh)) f = h5py.File(vol_path) vol = f['data'][:] res_vols.append(vol) return np.array(res_imgs), np.array(res_vols)
def trans_data(data_dir,save_dir): global name_to_id_dict wmlu.show_dict(name_to_id_dict) wmlu.create_empty_dir(save_dir,remove_if_exists=False) def name_to_id(x): return name_to_id_dict[x] ignored_labels = ["construction--barrier--ambiguous","construction--barrier--separator"] data = LabelMeData(label_text2id=name_to_id, shuffle=False) data.read_data(data_dir) for i,x in enumerate(data.get_items()): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x if len(category_ids) == 0: print(f"Skip {full_path}") continue new_mask = odm.dense_mask_to_sparse_mask(binary_mask,category_ids,default_label=255) #r_base_name = wmlu.base_name(full_path) r_base_name = f"IMG_{i+1:05d}" base_name = r_base_name+".png" save_path = os.path.join(save_dir,base_name) if resize_size is not None: new_mask = wmli.resize_img(new_mask,resize_size,keep_aspect_ratio=True,interpolation=cv2.INTER_NEAREST) img = wmli.imread(full_path) img = wmli.resize_img(img,resize_size,keep_aspect_ratio=True) img_save_path = os.path.join(save_dir,r_base_name+".jpg") wmli.imwrite(img_save_path,img) new_mask = new_mask.astype(np.uint8) if os.path.exists(save_path): print(f"WARNING: File {save_path} exists.") cv2.imwrite(save_path,new_mask) sys.stdout.write(f"\r{i}")
def next_flow_batch(self, start_idx, batch_size, vol_dim=64, process='train', shuffle_view=True): tmp_ids = [] if process == "train": tmp_ids = self.trainIds[start_idx:start_idx + batch_size] elif process == "val": tmp_ids = self.valIds[start_idx:start_idx + batch_size] res_imgs = [] # [batch_size, (im_height, im_width, 3)] res_flow_vols = [] # [batch, (vol_depth, vol_height, vol_width, 2)] res_clr_vols = [] # [batch, (vol_depth, vol_height, vol_width, 3)] for ins_id in tmp_ids: ###################### # Load image ###################### view_path = os.path.join(self.base_path, self.cat_id, ins_id, "views/*.png") im_paths = glob.glob(view_path) if shuffle_view: shuffle(im_paths) im_path = im_paths[0] im = img_utils.imread(im_path) res_imgs.append(im) ##################### # Load flow volume ##################### im_name = os.path.basename(im_path)[:-4] flow_path = os.path.join( self.base_path, self.cat_id, ins_id, "models/{}_{}_coor.h5".format(vol_dim, im_name)) f = h5py.File(flow_path) flow_vol = f['data'][:] res_flow_vols.append(flow_vol) #################### # Load clor volume #################### vol_path = os.path.join( self.base_path, self.cat_id, ins_id, "models/model_normalized_{}.h5".format(vol_dim)) f = h5py.File(vol_path) clr_vol = f['data'][:] # (vol_dim, vol_dim, vol_dim, 3) res_clr_vols.append(clr_vol) return np.array(res_imgs), np.array(res_flow_vols), np.array( res_clr_vols)
def create_tf_example(image, annotations): global src_file_index image_height = image['img_height'] image_width = image['img_width'] img_path = image['img_path'] if RECORD_IMG_SIZE is None: with tf.gfile.GFile(img_path, 'rb') as fid: encoded_jpg = fid.read() else: img = wmli.imread(img_path) img = wmli.resize_img(img, RECORD_IMG_SIZE, keep_aspect_ratio=True) encoded_jpg = wmli.encode_img(img) xmin = [] xmax = [] ymin = [] ymax = [] is_crowd = [] category_ids = [] for l, box in annotations: xmin.append(box[1]) xmax.append(box[3]) ymin.append(box[0]) ymax.append(box[2]) is_crowd.append(False) category_ids.append(l) if len(xmin) == 0: return None feature_dict = { 'image/height': dataset_util.int64_feature(image_height), 'image/width': dataset_util.int64_feature(image_width), 'image/filename': dataset_util.bytes_feature(img_path.encode('utf8')), 'image/encoded': dataset_util.bytes_feature(encoded_jpg), 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), 'image/object/class/label': dataset_util.int64_list_feature(category_ids), 'image/object/is_crowd': dataset_util.int64_list_feature(is_crowd), } example = tf.train.Example(features=tf.train.Features( feature=feature_dict)) return example
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.MODEL.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg,is_remove_batch=True) model.restoreVariables() data_path = args.test_data_dir if os.path.isdir(data_path): files = wmlu.recurse_get_filepath_in_dir(data_path,suffix=".jpg") else: files = [data_path] save_path = args.save_data_dir wmlu.create_empty_dir(save_path,remove_if_exists=True) for file in files: img = wmli.imread(file) imgs = np.expand_dims(img,axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img,res[RD_LABELS],res[RD_PROBABILITY],res[RD_BOXES], text_fn=text_fn, show_text=True) name = wmlu.base_name(file) img_save_path = os.path.join(save_path,name+".png") wmli.imwrite(img_save_path,r_img)
def show_anchor_box(img_file,boxes,size=None): nr = boxes.shape[0] classes = [] scores = [] for i in range(nr): classes.append(0) scores.append(1) img = wmli.imread(img_file) if size is not None: img = wmli.resize_img(img,(size[1],size[0])) odv.bboxes_draw_on_img(img, classes, scores, boxes) plt.figure(figsize=(10,10)) plt.imshow(img) plt.show() return img
def next_test_batch(self, start_idx, batch_size): tmp_ids = self.testIds[start_idx:start_idx+batch_size] res_imgs = [] # [view_size, (im_height, im_width, 3)] res_img_names = [] # [] for ins_id in tmp_ids: # Load image view_path = os.path.join(self.base_path, self.cat_id, ins_id, "views/*.png") im_paths = glob.glob(view_path) for im_path in im_paths: im = img_utils.imread(im_path) res_imgs.append(im) res_img_names.append(os.path.basename(im_path)) return np.array(res_imgs), res_img_names
def view_data(image_file, json_file, label_text_to_id=None, color_fn=None, alpha=0.4): image, annotation_list = read_labelme_data(json_file, label_text_to_id) image_data = wmli.imread(image_file) do_scale = False if image_data.shape[0] > 2048 or image_data.shape[1] > 2048: scale = min(2048.0 / image_data.shape[0], 2048.0 / image_data.shape[1]) size = (int(image_data.shape[1] * scale), int(image_data.shape[0] * scale)) image_data = cv.resize(image_data, size) do_scale = True else: size = (image_data.shape[1], image_data.shape[0]) for ann in annotation_list: if color_fn is not None: color = list(color_fn(ann["category_id"])) else: color = [ random.random() * 255, random.random() * 255, random.random() * 255 ] color = np.array([[color]], dtype=np.float) mask = ann["segmentation"] if do_scale: mask = cv.resize(mask, size) mask = np.expand_dims(mask, axis=-1) image_data = ( image_data * (np.array([[[1]]], dtype=np.float32) - mask * alpha)).astype( np.uint8) + (mask * color * alpha).astype(np.uint8) labels, bboxes = get_labels_and_bboxes(image, annotation_list) image_data = odv.bboxes_draw_on_imgv2(image_data, classes=labels, bboxes=bboxes, thickness=2) plt.figure(figsize=(10, 10)) plt.imshow(image_data) plt.show()
def next_test_batch(self, start_idx, batch_size): tmp_names = self.test_names[start_idx:start_idx+batch_size] res_imgs = [] # [view_size, (im_height, im_width, 3)] res_img_names = [] # [[cloth, mesh, view], ...] for name in tmp_names: cloth = name[0] mesh = name[1] view_path = os.path.join(self.base_path, cloth, mesh, "views/*.png") im_paths = glob.glob(view_path) for im_path in im_paths: im = img_utils.imread(im_path) res_imgs.append(im) res_img_names.append([cloth, mesh, os.path.basename(im_path)]) return np.array(res_imgs), res_img_names
def next_flow_batch(self, start_idx, batch_size, vol_dim=64, shuffle_view=True): """ Get the next training batch with flow data. """ tmp_names = self.train_names[start_idx:start_idx+batch_size] res_imgs = [] # [batch_size, (im_height, im_width, 3)] res_flow_vols = [] # [batch, (vol_depth, vol_height, vol_width, 2)] res_clr_vols = [] # [batch, (vol_depth, vol_height, vol_width, 3)] for name in tmp_names: cloth = name[0] mesh = name[1] # Load image view_path = os.path.join(self.base_path, cloth, mesh, "views/*.png") im_paths = glob.glob(view_path) if shuffle_view: shuffle(im_paths) im_path = im_paths[0] im = img_utils.imread(im_path) res_imgs.append(im) ##################### # Load flow volume ##################### im_name = os.path.basename(im_path)[:-4] flow_path = os.path.join(self.base_path, cloth, mesh, "flow/{}_{}_coor.h5".format(vol_dim, im_name)) f = h5py.File(flow_path) flow_vol = f['data'][:] res_flow_vols.append(flow_vol) #################### # Load clor volume #################### vol_path = os.path.join(self.base_path, cloth, mesh, "{}.h5".format(mesh)) f = h5py.File(vol_path) vol = f['data'][:] res_clr_vols.append(vol) return np.array(res_imgs), np.array(res_flow_vols), np.array(res_clr_vols)
def main(_): is_training = False args = default_argument_parser().parse_args() cfg = setup(args) data_loader = DataLoader(cfg=cfg, is_training=is_training) data_args = DATASETS_REGISTRY[cfg.DATASETS.TEST] data, num_classes = data_loader.load_data(*data_args, batch_size=1, is_training=False) cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes cfg.MODEL.SSD.NUM_CLASSES = num_classes cfg.MODEL.RETINANET.NUM_CLASSES = num_classes cfg.MODEL.CENTERNET.NUM_CLASSES = num_classes cfg.MODEL.YOLACT.NUM_CLASSES = num_classes cfg.MODEL.FCOS.NUM_CLASSES = num_classes cfg.DATASETS.NUM_CLASSES = num_classes cfg.freeze() config.set_global_cfg(cfg) model = PredictModel(cfg=cfg, is_remove_batch=True) model.restoreVariables() save_path = args.save_data_dir wmlu.create_empty_dir(save_path, remove_if_exists=True) metrics = COCOEvaluation(num_classes=90) items = eval_dataset() for data in items: full_path, shape, gt_labels, category_names, gt_boxes, binary_masks, area, is_crowd, num_annotations_skipped = data img = wmli.imread(full_path) imgs = np.expand_dims(img, axis=0) res = model.predictImages(imgs) if RD_MASKS in res: r_img = odv.draw_bboxes_and_mask(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], res[RD_MASKS], show_text=True) else: r_img = odv.bboxes_draw_on_imgv2(img, res[RD_LABELS], res[RD_PROBABILITY], res[RD_BOXES], text_fn=text_fn, show_text=True) kwargs = {} kwargs['gtboxes'] = gt_boxes kwargs['gtlabels'] = gt_labels kwargs['boxes'] = res[RD_BOXES] kwargs['labels'] = res[RD_LABELS] kwargs['probability'] = res[RD_PROBABILITY] kwargs['img_size'] = shape metrics(**kwargs) if model.step % 100 == 0: metrics.show() name = wmlu.base_name(full_path) img_save_path = os.path.join(save_path, name + ".png") wmli.imwrite(img_save_path, r_img) metrics.show()
def _evaluate_denoise(sr_model : BaseSuperResolutionModel, validation_dir, scale_pred=False): print("Validating %s model" % sr_model.model_name) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) validation_path_set5 = validation_dir + "set5/" validation_path_set14 = validation_dir + "set14/" validation_dirs = [validation_path_set5, validation_path_set14] for val_dir in validation_dirs: image_fns = [name for name in os.listdir(val_dir)] nb_images = len(image_fns) print("Validating %d images from path %s" % (nb_images, val_dir)) total_psnr = 0.0 for impath in os.listdir(val_dir): t1 = time.time() # Input image y = img_utils.imread(val_dir + impath, mode='RGB') width, height, _ = y.shape if ((width // sr_model.scale_factor) % 4 != 0) or ((height // sr_model.scale_factor) % 4 != 0) \ or (width % 2 != 0) or (height % 2 != 0): width = ((width // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor height = ((height // sr_model.scale_factor) // 4) * 4 * sr_model.scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (sr_model.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') y = np.expand_dims(y, axis=0) x_temp = y.copy() if sr_model.type_scale_type == "tanh": x_temp = (x_temp - 127.5) / 127.5 y = (y - 127.5) / 127.5 else: x_temp /= 255. y /= 255. img = img_utils.imresize(x_temp[0], (width // sr_model.scale_factor, height // sr_model.scale_factor), interp='bicubic', mode='RGB') if not sr_model.type_true_upscaling: img = img_utils.imresize(img, (width, height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) sr_model.model = sr_model.create_model(height, width, load_weights=True) if sr_model.evaluation_func is None: if sr_model.uses_learning_phase: sr_model.evaluation_func = K.function([sr_model.model.layers[0].input, K.learning_phase()], [sr_model.model.layers[-1].output]) else: sr_model.evaluation_func = K.function([sr_model.model.layers[0].input], [sr_model.model.layers[-1].output]) if sr_model.uses_learning_phase: y_pred = sr_model.evaluation_func([x, 0])[0][0] else: y_pred = sr_model.evaluation_func([x])[0][0] if scale_pred: if sr_model.type_scale_type == "tanh": y_pred = (y_pred + 1) * 127.5 else: y_pred *= 255. if sr_model.type_scale_type == 'tanh': y = (y + 1) / 2 psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_%s_generated.png" % (sr_model.model_name, os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f \n" % (total_psnr / nb_images))
parser.add_argument('--log-l1-loss', dest='log_l1_loss', action='store_true') args = parser.parse_args() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: model = DCGAN(sess, image_size=args.img_size, checkpoint_dir=args.checkpoint_dir, lam=args.lam) # Need to have a loaded model tf.global_variables_initializer().run() assert(model.load(model.checkpoint_dir)) # Construct mask image_shape = imread(args.imgs[0]).shape mask_type = args.mask_type if mask_type == 'random': fraction_masked = 0.2 mask = np.ones(image_shape) mask[np.random.random(image_shape[:2]) < fraction_masked] = 0.0 elif mask_type == 'center': center_scale = args.center_scale assert(center_scale <= 0.5) mask = np.ones(image_shape) l = int(image_shape[0] * center_scale) u = int(image_shape[0] * (1.0 - center_scale)) mask[l:u, l:u, :] = 0.0 elif mask_type == 'left': mask = np.ones(image_shape) c = image_size // 2
l, r = pair if res[l, 0] < res[r, 0] or res[l, 2] < 0.1 or res[r, 2] < 0.1: is_good = False break head_bbox = self.get_head_pos(mpii_kps) if is_good and head_bbox is not None and self.kps_in_bbox( coco_kps[self.coco_idxs], head_bbox): res[self.coco_idxs] = coco_kps[self.coco_idxs] return res if __name__ == "__main__": file_path = '/home/wj/ai/mldata1/crowd_pose/CrowdPose/crowdpose_train.json' images_dir = '/home/wj/ai/mldata1/crowd_pose/images' save_dir = '/home/wj/ai/mldata1/crowd_pose/tmp/vis' wmlu.create_empty_dir(save_dir, remove_if_exists=False) datas = read_crowd_pose(file_path) do_vis = True for data in datas: image_name, kps, bbox = data image = osp.join(images_dir, image_name) img = wmli.imread(image) img = odv.draw_keypoints(img, kps, no_line=True) t_bboxes = np.array([bbox]) t_bboxes = odb.npchangexyorder(t_bboxes) img = odv.draw_bboxes(img, bboxes=t_bboxes, is_relative_coordinate=False) save_path = osp.join(save_dir, image_name) wmli.imwrite(save_path, img)
def evaluate(self, validation_dir, scale_factor, target_size=256, small_train_images=False): """ Evaluates the model on the Set5 Validation images """ if self.model == None: self.create_model(load_weights=True, small_train_images=small_train_images) if self.evaluation_func is None: self.evaluation_func = K.function([self.model.layers[0].input], [self.model.layers[-1].output]) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) image_fns = [name for name in os.listdir(validation_dir)] nb_images = len(image_fns) print("Validating %d images" % (nb_images)) total_psnr = 0.0 for impath in os.listdir(validation_dir): t1 = time.time() # Input image y = img_utils.imread(validation_dir + impath, mode='RGB') width, height, _ = y.shape if self.model_name in self.denoise_models: # Denoise models require precise width and height, divisible by 4 if ((width // scale_factor) % 4 != 0) or ( (height // scale_factor) % 4 != 0): width = ((width // scale_factor) // 4) * 4 * scale_factor height = ((height // scale_factor) // 4) * 4 * scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (self.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_width = width if not small_train_images else width // scale_factor x_height = height if not small_train_images else height // scale_factor x_temp = y.copy() img = img_utils.gaussian_filter(x_temp[0], sigma=0.01) img = img_utils.imresize( img, (x_width // scale_factor, x_height // scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (x_width, x_height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print( "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_generated.png" % ( os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f" % (total_psnr / nb_images))
def evaluate(self, validation_dir, scale_factor, target_size=256, small_train_images=False): """ Evaluates the model on the Set5 Validation images """ if self.model == None: self.create_model(load_weights=True, small_train_images=small_train_images) if self.evaluation_func is None: self.evaluation_func = K.function([self.model.layers[0].input], [self.model.layers[-1].output]) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) image_fns = [name for name in os.listdir(validation_dir)] nb_images = len(image_fns) print("Validating %d images" % (nb_images)) total_psnr = 0.0 for impath in os.listdir(validation_dir): t1 = time.time() # Input image y = img_utils.imread(validation_dir + impath, mode='RGB') width, height, _ = y.shape if self.model_name in self.denoise_models: # Denoise models require precise width and height, divisible by 4 if ((width // scale_factor) % 4 != 0) or ((height // scale_factor) % 4 != 0): width = ((width // scale_factor) // 4) * 4 * scale_factor height = ((height // scale_factor) // 4) * 4 * scale_factor print("Model %s require the image size to be divisible by 4. New image size = (%d, %d)" % \ (self.model_name, width, height)) y = img_utils.imresize(y, (width, height), interp='bicubic') y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_width = width if not small_train_images else width // scale_factor x_height = height if not small_train_images else height // scale_factor x_temp = y.copy() img = img_utils.gaussian_filter(x_temp[0], sigma=0.01) img = img_utils.imresize(img, (x_width // scale_factor, x_height // scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (x_width, x_height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print("Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_generated.png" % (os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f" % (total_psnr / nb_images))
def file_to_snpe_raw(img_path, raw_filepath,input_size=None): img = wmli.imread(img_path) img = img.astype(np.float32) array_to_snpe_raw(img,raw_filepath,input_size)
help="path to test data dir") parser.add_argument("--save_dir", default="/2_data/wj/mldata/coco/coco_results", type=str, help="path to save data dir") return parser if __name__ == "__main__": args = default_argument_parser().parse_args() save_path = args.save_dir dataset = PascalVOCData() # data_path0 = get_data_dir2("annotationed_data/verify_p03_1020_1_proc_m") data_path = args.test_dir if not dataset.read_data(data_path): exit(0) wmlu.create_empty_dir(save_path, remove_if_exists=True, yes_to_all=True) for data in dataset.get_items(): full_path, shape, category_ids, category_names, boxes, binary_masks, area, is_crowd, num_annotations_skipped = data print(f"Process {full_path}") img = wmli.imread(full_path) base_name = wmlu.base_name(full_path) + "_a.jpg" save_file_path = os.path.join(save_path, base_name) img = odv.draw_bboxes(img, classes=category_names, bboxes=boxes, show_text=True, text_color=(255., 0., 0.)) wmli.imsave(save_file_path, img)
def view_data(name, save_dir, nr=20): print(f"View {name}") raw_name = name names = name.split("--") if names[0] == "void" or "ambiguous" in raw_name: return if "road" not in raw_name: return for x in names: save_dir = os.path.join(save_dir, x) wmlu.create_empty_dir(save_dir, remove_if_exists=False) allowed_names = [raw_name] NAME2ID = {} ID2NAME = {} def name_to_id(x): global lid if x in NAME2ID: return NAME2ID[x] else: NAME2ID[x] = lid ID2NAME[lid] = x lid += 1 return NAME2ID[x] data = MapillaryVistasData(label_text2id=name_to_id, shuffle=False, ignored_labels=None, label_map=None, allowed_labels_fn=allowed_names) data.read_data(wmlu.home_dir("ai/mldata/mapillary_vistas")) i = 0 for x in data.get_items(): full_path, img_info, category_ids, category_names, boxes, binary_mask, area, is_crowd, num_annotations_skipped = x img = wmli.imread(full_path) def text_fn(classes, scores): return f"{ID2NAME[classes]}" if len(category_ids) == 0: continue wmlu.show_dict(NAME2ID) odv.draw_bboxes_and_maskv2(img=img, classes=category_ids, scores=None, bboxes=boxes, masks=binary_mask, color_fn=None, text_fn=text_fn, thickness=4, show_text=True, fontScale=0.8) base_name = os.path.basename(full_path) save_path = os.path.join(save_dir, base_name) wmli.imwrite(save_path, img) i += 1 if i >= nr: break
def evaluate(self, validation_dir, small_train_images=False): print("Validating %s model" % self.model_name) predict_path = "val_predict/" if not os.path.exists(predict_path): os.makedirs(predict_path) validation_path_set5 = validation_dir + "set5/" validation_path_set14 = validation_dir + "set14/" validation_dirs = [validation_path_set5, validation_path_set14] for val_dir in validation_dirs: image_fns = [name for name in os.listdir(val_dir)] nb_images = len(image_fns) print("Validating %d images from path %s" % (nb_images, val_dir)) total_psnr = 0.0 for impath in os.listdir(val_dir): t1 = time.time() # Input image y = img_utils.imread(val_dir + impath, mode='RGB') width, height, _ = y.shape y = y.astype('float32') / 255. y = np.expand_dims(y, axis=0) x_temp = y.copy() img = img_utils.imresize( x_temp[0], (width // self.scale_factor, height // self.scale_factor), interp='bicubic') if not small_train_images: img = img_utils.imresize(img, (width, height), interp='bicubic') x = np.expand_dims(img, axis=0) if K.image_dim_ordering() == "th": x = x.transpose((0, 3, 1, 2)) y = y.transpose((0, 3, 1, 2)) self.model = self.create_model(height, width, load_weights=True) self.evaluation_func = K.function( [self.model.layers[0].input], [self.model.layers[-1].output]) y_pred = self.evaluation_func([x])[0][0] psnr_val = psnr(y[0], np.clip(y_pred, 0, 255) / 255) total_psnr += psnr_val t2 = time.time() print( "Validated image : %s, Time required : %0.2f, PSNR value : %0.4f" % (impath, t2 - t1, psnr_val)) generated_path = predict_path + "%s_%s_generated.png" % ( self.model_name, os.path.splitext(impath)[0]) if K.image_dim_ordering() == "th": y_pred = y_pred.transpose((1, 2, 0)) y_pred = np.clip(y_pred, 0, 255).astype('uint8') img_utils.imsave(generated_path, y_pred) print("Average PRNS value of validation images = %00.4f \n" % (total_psnr / nb_images))