zrma / 1d1python

1 day 1 coding with python
2 stars 2 forks source link

On the problems of Python package #19

Closed lvjinqiao closed 3 years ago

lvjinqiao commented 3 years ago

I'm sorry, I can't speak English. I hope you can understand what I said. When I quoted your NES package, it seemed that there was an error in the program. I don't know if it was a problem with the NES package. I would like to ask you to take a look

""" @author: Viet Nguyen nhviet1009@gmail.com """

import gym_super_mario_bros from gym.spaces import Box from gym import Wrapper from nes_py.wrappers import JoypadSpace from gym_super_mario_bros.actions import SIMPLE_MOVEMENT, COMPLEX_MOVEMENT, RIGHT_ONLY import cv2 import numpy as np import subprocess as sp import torch.multiprocessing as mp import time

class Monitor: def init(self, width, height, saved_path):

    self.command = ["ffmpeg", "-y", "-f", "rawvideo", "-vcodec", "rawvideo", "-s", "{}X{}".format(width, height),
                    "-pix_fmt", "rgb24", "-r", "60", "-i", "-", "-an", "-vcodec", "mpeg4", saved_path]
    try:
        print(self.command)
        self.pipe = sp.Popen(self.command, stdin=sp.PIPE, stderr=sp.PIPE)
    except FileNotFoundError:
        pass

def record(self, image_array):
    self.pipe.stdin.write(image_array.tostring())

def process_frame(frame):

if frame is not None:
    frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    frame = cv2.resize(frame, (84, 84))[None, :, :] / 255.
    return frame
else:
    return np.zeros((1, 84, 84))

class CustomReward(Wrapper): def init(self, env=None, monitor=None): super(CustomReward, self).init(env) self.observation_space = Box(low=0, high=255, shape=(1, 84, 84)) self.curr_score = 0 if monitor: self.monitor = monitor else: self.monitor = None

def step(self, action):
    state, reward, done, info = self.env.step(action)
    if self.monitor:
        self.monitor.record(state)
    state = process_frame(state)
    reward += (info["score"] - self.curr_score) / 40.
    self.curr_score = info["score"]
    if done:
        if info["flag_get"]:
            reward += 50
        else:
            reward -= 50
    return state, reward / 10., done, info

def reset(self):
    self.curr_score = 0
    return process_frame(self.env.reset())

class CustomSkipFrame(Wrapper): def init(self, env, skip=4): super(CustomSkipFrame, self).init(env) self.observation_space = Box(low=0, high=255, shape=(skip, 84, 84)) self.skip = skip self.states = np.zeros((skip, 84, 84), dtype=np.float32)

def step(self, action):
    total_reward = 0
    last_states = []
    for i in range(self.skip):
        state, reward, done, info = self.env.step(action)
        total_reward += reward
        if i >= self.skip / 2:
            last_states.append(state)
        if done:
            self.reset()
            return self.states[None, :, :, :].astype(np.float32), total_reward, done, info
    max_state = np.max(np.concatenate(last_states, 0), 0)
    self.states[:-1] = self.states[1:]
    self.states[-1] = max_state
    return self.states[None, :, :, :].astype(np.float32), total_reward, done, info

def reset(self):
    state = self.env.reset()
    self.states = np.concatenate([state for _ in range(self.skip)], 0)

    return self.states[None, :, :, :].astype(np.float32)

def create_train_env(world, stage, actions, output_path=None): env = gym_super_mario_bros.make("SuperMarioBros-{}-{}-v0".format(world, stage)) if output_path: monitor = Monitor(256, 240, output_path) else: monitor = None

env = JoypadSpace(env, actions)
env = CustomReward(env, monitor)
env = CustomSkipFrame(env)

return env

def test(): return "11111111111111111"

class MultipleEnvironments: def init(self, world, stage, action_type, num_envs, output_path=None): self.agent_conns, self.envconns = zip(*[mp.Pipe() for in range(num_envs)]) if action_type == "right": actions = RIGHT_ONLY elif action_type == "simple": actions = SIMPLE_MOVEMENT else: actions = COMPLEX_MOVEMENT

    self.envs = [create_train_env(world, stage, actions, output_path=output_path) for _ in range(num_envs)]

    self.num_states = self.envs[0].observation_space.shape[0]

    self.num_actions = len(actions)

    for index in range(num_envs):
        process = mp.Process(target=self.run, args=(index,))
        process.start()
        print(process.pid)

        # self.env_conns[index].close()

def run(self, index):

    # self.agent_conns[index].close()
    while True:

        request, action = self.env_conns[index].recv()
        if request == "step":
            self.env_conns[index].send(self.envs[index].step(action.item()))

        elif request == "reset":

            self.env_conns[index].send(self.envs[index].reset())

        else:
            raise NotImplementedError

if name == 'main': envs = MultipleEnvironments(5, 2, "simple", 2)

envs.envs[0].render()
envs.agent_conns[0].send(("reset", None))

D:\Pycharm\Project\venv\Scripts\python.exe C:/Users/LV/Desktop/Super-mario-bros-PPO-pytorch-master/src/env.py 8408 14212 Process Process-1: Traceback (most recent call last): File "D:\python\lib\multiprocessing\process.py", line 297, in _bootstrap self.run() File "D:\python\lib\multiprocessing\process.py", line 99, in run self._target(*self._args, self._kwargs) File "C:\Users\LV\Desktop\Super-mario-bros-PPO-pytorch-master\src\env.py", line 156, in run self.env_conns[index].send(self.envs[index].reset()) File "C:\Users\LV\Desktop\Super-mario-bros-PPO-pytorch-master\src\env.py", line 95, in reset state = self.env.reset() File "C:\Users\LV\Desktop\Super-mario-bros-PPO-pytorch-master\src\env.py", line 68, in reset return process_frame(self.env.reset()) File "D:\python\lib\site-packages\nes_py\wrappers\joypad_space.py", line 78, in reset return self.env.reset() File "D:\python\lib\site-packages\gym\wrappers\time_limit.py", line 25, in reset return self.env.reset(kwargs) File "D:\python\lib\site-packages\nes_py\nes_env.py", line 258, in reset self._restore() File "D:\python\lib\site-packages\nes_py\nes_env.py", line 220, in _restore _LIB.Restore(self._env) OSError: exception: access violation reading 0x0000016262170760

zrma commented 3 years ago

Dear @lvjinqiao

Sorry for replying too late. I didn't manage this repository for a long time, so I find this issue just now.

I think you've reported this issue to the wrong repository. I found the same issue on https://github.com/Kautenja/nes-py/issues/71#issue-775855012 so I closed it.

Best Regards. zrma