def test_custom_templates(): template_names = ["complete", "basic_web", "square"] for template in template_names: path = custom_template(d2_path(gettempdir()), template=template) cfg = m1.load_config(os.path.join(path, 'config.py')) assert (cfg != None) mcfg = m1.load_config(os.path.join(path, 'myconfig.py')) assert (mcfg != None)
def test_drive(): path = default_template(d2_path(gettempdir())) myconfig = open(os.path.join(path, 'myconfig.py'), "wt") myconfig.write("CAMERA_TYPE = 'MOCK'\n") myconfig.write("DRIVE_TRAIN_TYPE = 'None'") myconfig.close() cfg = m1.load_config(os.path.join(path, 'config.py')) cfg.MAX_LOOPS = 10 complete.drive(cfg=cfg)
def run(self, args, parser): ''' Load the images from a tub and create a movie from them. Movie ''' if args.tub is None: print("ERR>> --tub argument missing.") parser.print_help() return if args.type is None and args.model is not None: print( "ERR>> --type argument missing. Required when providing a model." ) parser.print_help() return if args.salient: if args.model is None: print( "ERR>> salient visualization requires a model. Pass with the --model arg." ) parser.print_help() conf = os.path.expanduser(args.config) if not os.path.exists(conf): print("No config file at location: %s. Add --config to specify\ location or run from dir containing config.py." % conf) return self.cfg = m1.load_config(conf) self.tub = Tub(args.tub) self.index = self.tub.get_index(shuffled=False) start = args.start self.end = args.end if args.end != -1 else len(self.index) if self.end >= len(self.index): self.end = len(self.index) - 1 num_frames = self.end - start self.iRec = start self.scale = args.scale self.keras_part = None self.do_salient = False if args.model is not None: self.keras_part = get_model_by_type(args.type, cfg=self.cfg) self.keras_part.load(args.model) self.keras_part.compile() if args.salient: self.do_salient = self.init_salient(self.keras_part.model) print('making movie', args.out, 'from', num_frames, 'images') clip = mpy.VideoClip(self.make_frame, duration=((num_frames - 1) / self.cfg.DRIVE_LOOP_HZ)) clip.write_videofile(args.out, fps=self.cfg.DRIVE_LOOP_HZ)
def main(tub_path, count): cfg = m1.load_config('config.py') records_paths = gather_records(cfg, tub_path) record_name = "user/angle" num_bins = 20 half_bins = num_bins // 2 bins = {} records = [] for i in range(num_bins): bins[i] = [] if len(records_paths) <= count: print("we have fewer records than target count.") return #put the record in a bin based on angle. expecting -1 to 1 for record_path in records_paths: with open(record_path, 'r') as fp: record = json.load(fp) user_angle = float(record["user/angle"]) record["filename"] = record_path iBin = round((user_angle * half_bins) + (half_bins - 1)) % num_bins bins[iBin].append(record) records.append(record) for i in range(num_bins): print("iBin:", len(bins[i])) keep = [] iBin = 0 while len(keep) < count: iBin += 1 if iBin >= num_bins: iBin = 0 num_elem = len(bins[iBin]) if num_elem > 0: iRec = random.randint(0, num_elem - 1) rec = bins[iBin].pop(iRec) keep.append(rec) print("have", count, "records selected. removing the rest...") for record in records: if record in keep: continue img_filename = os.path.join(tub_path, record['cam/image_array']) os.unlink(img_filename) os.unlink(record["filename"]) print('done')
def load_config(config_path): ''' load a config from the given path ''' conf = os.path.expanduser(config_path) if not os.path.exists(conf): print("No config file at location: %s. Add --config to specify\ location or run from dir containing config.py." % conf) return None try: cfg = m1.load_config(conf) except: print("Exception while loading config from", conf) return None return cfg
def profile(model_path, model_type): cfg = m1.load_config('config.py') model_path = os.path.expanduser(model_path) model = m1.utils.get_model_by_type(model_type, cfg) model.load(model_path) count, h, w, ch = 1, cfg.TARGET_H, cfg.TARGET_W, cfg.TARGET_D seq_len = 0 if "rnn" in model_type or "3d" in model_type: seq_len = cfg.SEQUENCE_LENGTH #generate random array in the right shape img = np.random.rand(int(h), int(w), int(ch)).astype(np.uint8) if seq_len: img_arr = [] for i in range(seq_len): img_arr.append(img) img_arr = np.array(img_arr) #make a timer obj timer = FPSTimer() try: while True: ''' run forward pass on model ''' if seq_len: model.run(img_arr) else: model.run(img) ''' keep track of iterations and give feed back on iter/sec ''' timer.on_frame() except KeyboardInterrupt: pass
zero_pulse=cfg.THROTTLE_STOPPED_PWM, min_pulse=cfg.THROTTLE_REVERSE_PWM) V.add(steering, inputs=['steering']) V.add(throttle, inputs=['throttle']) #add tub to save data inputs=['cam/image_array', 'steering', 'throttle'] types=['image_array', 'float', 'float'] th = TubHandler(path=cfg.DATA_PATH) tub = th.new_tub_writer(inputs=inputs, types=types) V.add(tub, inputs=inputs, outputs=["tub/num_records"], run_condition='recording') #run the vehicle V.start(rate_hz=cfg.DRIVE_LOOP_HZ, max_loop_count=cfg.MAX_LOOPS) if __name__ == '__main__': args = docopt(__doc__) cfg = m1.load_config() if args['drive']: drive(cfg)
""" import os import time import math from docopt import docopt import irmark1 as m1 from irmark1.parts.cv import CvImageView, ImgBGR2RGB, ImgRGB2BGR, ImageScale, ImgWriter from irmark1.parts.salient import SalientVis from irmark1.parts.network import ZMQValueSub, UDPValueSub, TCPClientValue from irmark1.parts.transform import Lambda from irmark1.parts.image import JpgToImgArr V = m1.vehicle.Vehicle() args = docopt(__doc__) cfg = m1.load_config(args['--config']) model_path = args['--model'] model_type = args['--type'] ip = args['--ip'] if model_type is None: model_type = "categorical" model = m1.utils.get_model_by_type(model_type, cfg) model.load(model_path) V.add(TCPClientValue(name="camera", host=ip), outputs=["packet"]) V.add(JpgToImgArr(), inputs=["packet"], outputs=["img"]) V.add(ImgBGR2RGB(), inputs=["img"], outputs=["img"]) V.add(SalientVis(model), inputs=["img"], outputs=["img"])
def test_config(): path = default_template(d2_path(gettempdir())) cfg = m1.load_config(os.path.join(path, 'config.py')) assert (cfg != None)