def save_yaml(fpath, inputs): with open(fpath, 'w') as f: yaml = ry.YAML() yaml.default_flow_style = None yaml.width = float('inf') yaml.boolean_representation = ['False', 'True'] yaml.indent(mapping=2, sequence=4, offset=2) yaml.dump(inputs, f)
def write_yaml(instance, foutput): # Write yaml with updated values yaml = ry.YAML() yaml.default_flow_style = None yaml.width = float("inf") yaml.indent(mapping=4, sequence=6, offset=3) with open(foutput, 'w') as f: yaml.dump(instance, f)
def write_yaml(instance, foutput): # Write yaml with updated values yaml = ry.YAML() yaml.default_flow_style = None yaml.width = float("inf") yaml.indent(mapping=4, sequence=6, offset=3) yaml.allow_unicode = False with open(foutput, "w", encoding="utf-8") as f: yaml.dump(instance, f)
def load_yaml(yml_file): # Load YAML data from file using the "safe" loading option. try: yaml_ = yaml.YAML(typ="safe") with open(yml_file, "rt", encoding="utf-8") as stream: return yaml_.load(stream) except yaml.constructor.ConstructorError: with open(yml_file, "rt", encoding="utf-8") as stream: # Ensure that the loader remains backward-compatible with legacy # ruamel.yaml versions (prior to 0.17.0). return yaml.safe_load(stream)
def loaddeps(self): with open(self.sln.fileName + '.deps.yaml') as f: # Read "raw" yaml config file content = f.read() # Render using jinja2 jenv = jinja2.Environment().from_string(content) rendered = jenv.render() with open(self.sln.fileName + '.rendered.yaml', 'w') as f: f.write(rendered) # Load jinja2-rendered config file yaml = ruamel_yaml.YAML() self.deps = yaml.load(rendered)
def load_yaml(fname_input, package=0): # Import a .yaml file if package == 0: with open(fname_input) as f: data = yaml.safe_load(f) return data elif package == 1: with open(fname_input, 'r') as myfile: text_input = myfile.read() myfile.close() ryaml = ry.YAML() return dict(ryaml.load(text_input))
def save_yaml(outdir, fname, data_out): if not os.path.isdir(outdir) and outdir!='': os.makedirs(outdir) fname = os.path.join(outdir, fname) data_out = remove_numpy(data_out) f = open(fname, "w") yaml=ry.YAML() yaml.default_flow_style = None yaml.width = float("inf") yaml.indent(mapping=4, sequence=6, offset=3) yaml.dump(data_out, f)
def write(self): with open(self.sln.fileName + '.generated.deps.yaml', 'w') as f: yaml = ruamel_yaml.YAML() yaml.emitter.MAX_SIMPLE_KEY_LENGTH = 9999 # Fix truncating long keys... yaml.dump(self.deps, f) w = sln.SolutionWriter(self.sln) w.write(self.sln.fileName) for u, p in self.sln.projects.items(): if p.loaded: w = vcxproj.writer(p.loaded) w.write(p.fileName) self.writecopydeps()
def read_config(self): """ Reads configuration yaml file Raises ------ FileNotFoundError error thrown if pose configuration file does nott exist """ cfg_path = os.path.normpath(self.path + "/pose_cfg.yaml") if not os.path.isfile(cfg_path): raise FileNotFoundError( f"The pose configuration file for the exported model at {cfg_path} was not found. Please check the path to the exported model directory" ) ruamel_file = ruamel_yaml.YAML() self.cfg = ruamel_file.load(open(cfg_path, "r"))
def load_yaml(fname_input, package=0): ''' Import a .yaml file - ripped from WEIS Parameters: ----------- fname_input: str yaml file to load package: bool 0 = yaml, 1 = ruamel ''' if package == 0: with open(fname_input) as f: data = yaml.safe_load(f) return data elif package == 1: with open(fname_input, 'r') as myfile: text_input = myfile.read() myfile.close() ryaml = ry.YAML() return dict(ryaml.load(text_input))
def save_yaml(outdir, fname, data_out): ''' Save yaml file - ripped from WEIS Parameters: ----------- outdir: str directory to save yaml fname: str filename for yaml data_out: dict data to dump to yaml ''' fname = os.path.join(outdir, fname) if not os.path.isdir(outdir): os.makedirs(outdir) f = open(fname, "w") yaml = ry.YAML() yaml.default_flow_style = None yaml.width = float("inf") yaml.indent(mapping=4, sequence=6, offset=3) yaml.dump(data_out, f)
def benchmark( model_path, video_path, tf_config=None, resize=None, pixels=None, n_frames=1000, print_rate=False, display=False, pcutoff=0.0, display_radius=3, cmap="bmy", save_poses=False, save_video=False, output=None, ) -> typing.Tuple[np.ndarray, tuple, bool, dict]: """ Analyze DeepLabCut-live exported model on a video: Calculate inference time, display keypoints, or get poses/create a labeled video Parameters ---------- model_path : str path to exported DeepLabCut model video_path : str path to video file tf_config : :class:`tensorflow.ConfigProto` tensorflow session configuration resize : int, optional resize factor. Can only use one of resize or pixels. If both are provided, will use pixels. by default None pixels : int, optional downsize image to this number of pixels, maintaining aspect ratio. Can only use one of resize or pixels. If both are provided, will use pixels. by default None n_frames : int, optional number of frames to run inference on, by default 1000 print_rate : bool, optional flat to print inference rate frame by frame, by default False display : bool, optional flag to display keypoints on images. Useful for checking the accuracy of exported models. pcutoff : float, optional likelihood threshold to display keypoints display_radius : int, optional size (radius in pixels) of keypoint to display cmap : str, optional a string indicating the :package:`colorcet` colormap, `options here <https://colorcet.holoviz.org/>`, by default "bmy" save_poses : bool, optional flag to save poses to an hdf5 file. If True, operates similar to :function:`DeepLabCut.benchmark_videos`, by default False save_video : bool, optional flag to save a labeled video. If True, operates similar to :function:`DeepLabCut.create_labeled_video`, by default False output : str, optional path to directory to save pose and/or video file. If not specified, will use the directory of video_path, by default None Returns ------- :class:`numpy.ndarray` vector of inference times tuple (image width, image height) bool tensorflow inference flag dict metadata for video Example ------- Return a vector of inference times for 10000 frames: dlclive.benchmark('/my/exported/model', 'my_video.avi', n_frames=10000) Return a vector of inference times, resizing images to half the width and height for inference dlclive.benchmark('/my/exported/model', 'my_video.avi', n_frames=10000, resize=0.5) Display keypoints to check the accuracy of an exported model dlclive.benchmark('/my/exported/model', 'my_video.avi', display=True) Analyze a video (save poses to hdf5) and create a labeled video, similar to :function:`DeepLabCut.benchmark_videos` and :function:`create_labeled_video` dlclive.benchmark('/my/exported/model', 'my_video.avi', save_poses=True, save_video=True) """ ### load video cap = cv2.VideoCapture(video_path) ret, frame = cap.read() n_frames = (n_frames if (n_frames > 0) and (n_frames < cap.get(cv2.CAP_PROP_FRAME_COUNT) - 1) else (cap.get(cv2.CAP_PROP_FRAME_COUNT) - 1)) n_frames = int(n_frames) im_size = (cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) ### get resize factor if pixels is not None: resize = np.sqrt(pixels / (im_size[0] * im_size[1])) if resize is not None: im_size = (int(im_size[0] * resize), int(im_size[1] * resize)) ### create video writer if save_video: colors = None out_dir = (output if output is not None else os.path.dirname( os.path.realpath(video_path))) out_vid_base = os.path.basename(video_path) out_vid_file = os.path.normpath( f"{out_dir}/{os.path.splitext(out_vid_base)[0]}_DLCLIVE_LABELED.avi" ) fourcc = cv2.VideoWriter_fourcc(*"DIVX") fps = cap.get(cv2.CAP_PROP_FPS) vwriter = cv2.VideoWriter(out_vid_file, fourcc, fps, im_size) ### check for pandas installation if using save_poses flag if save_poses: try: import pandas as pd use_pandas = True except: use_pandas = False warnings.warn( "Could not find installation of pandas; saving poses as a numpy array with the dimensions (n_frames, n_keypoints, [x, y, likelihood])." ) ### initialize DLCLive and perform inference inf_times = np.zeros(n_frames) poses = [] live = DLCLive( model_path, tf_config=tf_config, resize=resize, display=display, pcutoff=pcutoff, display_radius=display_radius, display_cmap=cmap, ) poses.append(live.init_inference(frame)) TFGPUinference = True if len(live.outputs) == 1 else False iterator = range(n_frames) if (print_rate) or (display) else tqdm( range(n_frames)) for i in iterator: ret, frame = cap.read() if not ret: warnings.warn( "Did not complete {:d} frames. There probably were not enough frames in the video {}." .format(n_frames, video_path)) break start_pose = time.time() poses.append(live.get_pose(frame)) inf_times[i] = time.time() - start_pose if save_video: if colors is None: all_colors = getattr(cc, cmap) colors = [ ImageColor.getcolor(c, "RGB")[::-1] for c in all_colors[::int(len(all_colors) / poses[-1].shape[0])] ] this_pose = poses[-1] for j in range(this_pose.shape[0]): if this_pose[j, 2] > pcutoff: x = int(this_pose[j, 0]) y = int(this_pose[j, 1]) frame = cv2.circle(frame, (x, y), display_radius, colors[j], thickness=-1) if resize is not None: frame = cv2.resize(frame, im_size) vwriter.write(frame) if print_rate: print("pose rate = {:d}".format(int(1 / inf_times[i]))) if print_rate: print("mean pose rate = {:d}".format(int(np.mean(1 / inf_times)))) ### gather video and test parameterization # dont want to fail here so gracefully failing on exception -- # eg. some packages of cv2 don't have CAP_PROP_CODEC_PIXEL_FORMAT try: fourcc = decode_fourcc(cap.get(cv2.CAP_PROP_FOURCC)) except: fourcc = "" try: fps = round(cap.get(cv2.CAP_PROP_FPS)) except: fps = None try: pix_fmt = decode_fourcc(cap.get(cv2.CAP_PROP_CODEC_PIXEL_FORMAT)) except: pix_fmt = "" try: frame_count = round(cap.get(cv2.CAP_PROP_FRAME_COUNT)) except: frame_count = None try: orig_im_size = ( round(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), round(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), ) except: orig_im_size = None meta = { "video_path": video_path, "video_codec": fourcc, "video_pixel_format": pix_fmt, "video_fps": fps, "video_total_frames": frame_count, "original_frame_size": orig_im_size, "dlclive_params": live.parameterization, } ### close video and tensorflow session cap.release() live.close() if save_video: vwriter.release() if save_poses: cfg_path = os.path.normpath(f"{model_path}/pose_cfg.yaml") ruamel_file = ruamel_yaml.YAML() dlc_cfg = ruamel_file.load(open(cfg_path, "r")) bodyparts = dlc_cfg["all_joints_names"] poses = np.array(poses) if use_pandas: poses = poses.reshape( (poses.shape[0], poses.shape[1] * poses.shape[2])) pdindex = pd.MultiIndex.from_product( [bodyparts, ["x", "y", "likelihood"]], names=["bodyparts", "coords"]) pose_df = pd.DataFrame(poses, columns=pdindex) out_dir = (output if output is not None else os.path.dirname( os.path.realpath(video_path))) out_vid_base = os.path.basename(video_path) out_dlc_file = os.path.normpath( f"{out_dir}/{os.path.splitext(out_vid_base)[0]}_DLCLIVE_POSES.h5" ) pose_df.to_hdf(out_dlc_file, key="df_with_missing", mode="w") else: out_vid_base = os.path.basename(video_path) out_dlc_file = os.path.normpath( f"{out_dir}/{os.path.splitext(out_vid_base)[0]}_DLCLIVE_POSES.npy" ) np.save(out_dlc_file, poses) return inf_times, im_size, TFGPUinference, meta
def load_yaml(fname_input): with open(fname_input, 'r') as myfile: text_input = myfile.read() myfile.close() yaml = ry.YAML() return dict(yaml.load(text_input))
import ruamel_yaml yaml = ruamel_yaml.YAML(typ='safe') configMap = dict() dataMap = dict() configKeys = dict(nirLabReduxRoot="/data/redux", nirLabConfigRoot="/data/pfsx/config") configMap['detector'] = dict(template="{nirLabConfigRoot}/{cam}/detector.yaml", loader=yaml.load) dataMap['spsFile'] = dict( template="pfsx/{pfsDay}/sps/PF{site}A{visit:06d}{spectrograph}{armNum}.fits" ) dataMap['rampFile'] = dict( template="pfsx/{pfsDay}/sps/PF{site}B{visit:06d}{spectrograph}{armNum}.fits" ) dataMap['reduxDir'] = dict( template="{nirLabReduxRoot}/{cam}/{pfsDay}/{experimentName}") dataMap['mask'] = dict( template="{nirLabReduxRoot}/{cam}/calibs/mask-{visit:06d}-{cam}.fits", loaderModule='ics.hxutils.mask') dataMap['dark'] = dict( template="{nirLabReduxRoot}/{cam}/calibs/dark-{visit:06d}-{cam}.fits", loaderModule='ics.hxutils.darkCube') dataMap['isr'] = dict(template="{reduxDir}/isr-{visit:06d}-{cam}.fits")
def load_yaml(fpath): with open(fpath, 'r') as f: yaml = ry.YAML().load(f) return yaml
# ruamel_yaml is does a nice job with formatting but it is slow to load YAML. # pyyaml is faster to load YAML anyway and *much* faster with CLoader if it # is available. # # The biggest reason to still use ruamel_yaml over pyyaml is # https://github.com/yaml/pyyaml/issues/121 # However I do not want to make pyyaml *also* a requirement so it will use it # if it can or fall back try: import ruamel_yaml from ruamel_yaml.scalarstring import LiteralScalarString as PreservedScalarString except ImportError: import ruamel.yaml as ruamel_yaml from ruamel.yaml.scalarstring import LiteralScalarString as PreservedScalarString yaml = ruamel_yaml.YAML() def pss(item): """ Convert strings with '\n' to PreservedScalarString and recurse into dicts and lists (and tuples which are converted to lists). """ if isinstance(item, (list, tuple,),): return [pss(i) for i in item] # Convert tuples to lists for parsing elif isinstance(item, dict): item = item.copy() for key, val in item.items(): item[key] = pss(val) return item elif isinstance(item, str,) and "\n" in item: