@torch.no_grad()<EOL><INDENT>def record_take(self, model, env_instance, device, take_number):<DEDENT>
frames = []<EOL>observation = env_instance.reset()<EOL>if model.is_recurrent:<EOL><INDENT>hidden_state = model.zero_state(<NUM_LIT:1>).to(device)<EOL><DEDENT>frames.append(env_instance.render('<STR_LIT>'))<EOL>print("<STR_LIT>")<EOL>while True:<EOL><INDENT>observation_array = np.expand_dims(np.array(observation), axis=<NUM_LIT:0>)<EOL>observation_tensor = torch.from_numpy(observation_array).to(device)<EOL>if model.is_recurrent:<EOL><INDENT>output = model.step(observation_tensor, hidden_state, **self.sample_args)<EOL>hidden_state = output['<STR_LIT:state>']<EOL>actions = output['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>actions = model.step(observation_tensor, **self.sample_args)['<STR_LIT>']<EOL><DEDENT>actions = actions.detach().cpu().numpy()<EOL>observation, reward, done, epinfo = env_instance.step(actions[<NUM_LIT:0>])<EOL>frames.append(env_instance.render('<STR_LIT>'))<EOL>if '<STR_LIT>' in epinfo:<EOL><INDENT>break<EOL><DEDENT><DEDENT>takename = self.model_config.output_dir('<STR_LIT>', self.model_config.run_name, self.videoname.format(take_number))<EOL>pathlib.Path(os.path.dirname(takename)).mkdir(parents=True, exist_ok=True)<EOL>fourcc = cv2.VideoWriter_fourcc('<STR_LIT:M>', '<STR_LIT>', '<STR_LIT:P>', '<STR_LIT>')<EOL>video = cv2.VideoWriter(takename, fourcc, self.fps, (frames[<NUM_LIT:0>].shape[<NUM_LIT:1>], frames[<NUM_LIT:0>].shape[<NUM_LIT:0>]))<EOL>for i in tqdm.trange(len(frames), file=sys.stdout):<EOL><INDENT>video.write(cv2.cvtColor(frames[i], cv2.COLOR_RGB2BGR))<EOL><DEDENT>video.release()<EOL>print("<STR_LIT>".format(takename))<EOL>
Record a single movie and store it on hard drive