I found a gym environment on GitHub for robotics, I tried running it on collab without rendering with the following code
import gym
import panda_gym
env = gym.make('PandaReach-v2', render=True)
obs = env.reset()
done = False
while not done:
action = env.action_space.sample() # random action
obs, reward, done, info = env.step(action)
env.close()
I got the following error
import gym
import panda_gym
env = gym.make('PandaReach-v2', render=True)
obs = env.reset()
done = False
while not done:
action = env.action_space.sample() # random action
obs, reward, done, info = env.step(action)
env.close()
here is the library link:https://pypi.org/project/panda-gym
hope that i can get help
Related
I am trying to implement a code for saving videos every 500 episodes while using OpenAI gym and training. I was unable to get videos every 500 episodes.
Code:
from gym import wrappers
import gym
vidsavedir = "./video"
vidsaveeachepi = 500
env = gym.make("MsPacman-v0")
env = wrappers.Monitor(env, vidsavedir, video_callable = lambda episode_id: (episode_id % vidsaveeachepi) == 0, force = True)
env.reset()
for i in range(1,2):
for step in range(1000):
env.render()
action = env.action_space.sample()
observation, reward, done, info = env.step(action)
if done:
env.reset()
print(step)
break;
print(i)
env.close()
When I print steps it shows more than 500 but the video does not save. Only one video can see in the folder.
How can I solve this issue?
I tried to install open gym Mario environment. Almost every tutorial tells me to do so.
from nes_py.wrappers import JoypadSpace
import gym_super_mario_bros
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT
env = gym_super_mario_bros.make('SuperMarioBros-v0')
env = JoypadSpace(env, SIMPLE_MOVEMENT)
done = True
for step in range(5000):
if done:
state = env.reset()
observation, reward, done, info = env.step(env.action_space.sample())
env.render()
env.close()
I am new in Python and I faced with a problem in my code. I try to build my custom environment for a Deep Q-Network program. The name of my environment is "FooEnv".But when I run the main code, I faced with this error in line FooEnv.reset()
type object 'FooEnv' has no attribute 'reset'
This is my main code, That I call "FooEnv" here:
import json
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import sgd
from FooEnv import FooEnv
class ExperienceReplay(object):
def __init__(self, max_memory=100, discount=.9):
self.max_memory = max_memory
self.memory = list()
self.discount = discount
def remember(self, states, game_over):
# memory[i] = [[state_t, action_t, reward_t, state_t+1], game_over?]
self.memory.append([states, game_over])
if len(self.memory) > self.max_memory:
del self.memory[0]
def get_batch(self, model, batch_size=10):
len_memory = len(self.memory)
num_actions = model.output_shape[-1]
# env_dim = self.memory[0][0][0].shape[1]
env_dim = self.memory[0][0][0].shape[1]
inputs = np.zeros((min(len_memory, batch_size), env_dim))
targets = np.zeros((inputs.shape[0], num_actions))
for i, idx in enumerate(np.random.randint(0, len_memory,
size=inputs.shape[0])):
state_t, action_t, reward_t, state_tp1 = self.memory[idx][0]
game_over = self.memory[idx][1]
inputs[i:i+1] = state_t
# There should be no target values for actions not taken.
# Thou shalt not correct actions not taken #deep
targets[i] = model.predict(state_t)[0]
Q_sa = np.max(model.predict(state_tp1)[0])
if game_over: # if game_over is True
targets[i, action_t] = reward_t
else:
# reward_t + gamma * max_a' Q(s', a')
targets[i, action_t] = reward_t + self.discount * Q_sa
return inputs, targets
if __name__ == "__main__":
# parameters
epsilon = .1
num_actions = 2
epoch = 1000
max_memory = 500
hidden_size = 100
batch_size = 50
input_size = 2
f_c=[2.4*10**9]
eta_Los=[1]
eta_NLos=[2]
x_threshold = [5]
model = Sequential()
model.add(Dense(hidden_size, input_shape=(2, ), activation='relu'))
model.add(Dense(hidden_size, activation='relu'))
model.add(Dense(num_actions))
model.compile(sgd(lr=.2), "mse")
# Define environment/game
env = FooEnv(f_c, eta_Los, eta_NLos)
# Initialize experience replay object
exp_replay = ExperienceReplay(max_memory=max_memory)
FooEnv.reset()
And this is my FooEnv code:
import numpy as np
import math
class FooEnv(object):
def __init__(self, f_c, eta_Los, eta_NLos):
self.f_c = f_c
self.eta_Los = eta_Los
self.eta_NLos = eta_NLos
self.num_actions = 2
def reset(self):
state=self.state
E_Consumtion, Average_Delay_UAV, Average_DeLay_FAP = state
E_Consumtion=0
Average_Delay_UAV=0
Average_DeLay_FAP=0
self.state = np.append(E_Consumtion,Average_Delay_UAV,Average_DeLay_FAP)
self.steps_beyond_done = None
return np.array(self.state)
I would greatly appreciated it if you could help me with this.
FooEnv is a class and env is an object of that class. You want to reset the object, not the class.
I wrote a python script to start a server on port 36296 but it doesn't work at all, what went wrong here? When I tried to connect to port 36296, it said connection failed: connection refused. Please help explain why it is malfunctioning in such a peculiar way. Thank you. (I have tried this many times in many ways for a long time, so help would be EXTREMELY appreciated.)
import sys, time, logging, os, argparse
import numpy as np
from PIL import Image, ImageGrab
from socketserver import TCPServer, StreamRequestHandler
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
from train import create_model, is_valid_track_code, INPUT_WIDTH,
INPUT_HEIGHT, INPUT_CHANNELS
def prepare_image(im):
im = im.resize((INPUT_WIDTH, INPUT_HEIGHT))
im_arr = np.frombuffer(im.tobytes(), dtype=np.uint8)
im_arr = im_arr.reshape((INPUT_HEIGHT, INPUT_WIDTH, INPUT_CHANNELS))
im_arr = np.expand_dims(im_arr, axis=0)
return im_arr
class TCPHandler(StreamRequestHandler):
def handle(self):
if args.all:
weights_file = 'weights/all.hdf5'
logger.info("Loading {}...".format(weights_file))
model.load_weights(weights_file)
logger.info("Handling a new connection...")
for line in self.rfile:
message = str(line.strip(),'utf-8')
logger.debug(message)
if message.startswith("COURSE:") and not args.all:
course = message[7:].strip().lower()
weights_file = 'weights/{}.hdf5'.format(course)
logger.info("Loading {}...".format(weights_file))
model.load_weights(weights_file)
if message.startswith("PREDICTFROMCLIPBOARD"):
im = ImageGrab.grabclipboard()
if im != None:
prediction = model.predict(prepare_image(im), batch_size=1)[0]
self.wfile.write((str(prediction[0]) + "\n").encode('utf-8'))
else:
self.wfile.write("PREDICTIONERROR\n".encode('utf-8'))
if message.startswith("PREDICT:"):
im = Image.open(message[8:])
prediction = model.predict(prepare_image(im), batch_size=1)[0]
self.wfile.write((str(prediction[0]) + "\n").encode('utf-8'))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Start a prediction server that other apps will call into.')
parser.add_argument('-a', '--all', action='store_true', help='Use the combined weights for all tracks, rather than selecting the weights file based off of the course code sent by the Play.lua script.', default=False)
parser.add_argument('-p', '--port', type=int, help='Port number', default=36296)
parser.add_argument('-c', '--cpu', action='store_true', help='Force Tensorflow to use the CPU.', default=False)
args = parser.parse_args()
if args.cpu:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
logger.info("Loading model...")
model = create_model(keep_prob=1)
if args.all:
model.load_weights('weights/all.hdf5')
logger.info("Starting server...")
server = TCPServer(('0.0.0.0', args.port), TCPHandler)
print("Listening on Port: {}".format(server.server_address[1]))
sys.stdout.flush()
server.serve_forever()
I have written a small programm that uses a pipe.
The parent takes care of camera connection while the child is processing the images.
The child process calls two functions FunctionA and FunctionB. Both times the image from the parent is processed.
I want to run the two functions as fast as possible since they are time consuming 0.1s, 0.12s. I tried multiprocessing.Processes and multiprocessing.pools. The bottle neck in the first case is the creation of the two processes for each image passed by the parent. Pools only make sense if I would have a list of images which I cant wait for since the speed up from the pipe will be wasted which is already quite good -3.123 seconds. Does somebody has a smart solution for this sort of problem?
Kind regards :)
Example code:
import multiprocessing
import cv2
import glob
import cv2
from multiprocessing import pool
from multiprocessing.dummy import Pool as ThreadPool
import glob
import time
from ProcA import FunctionA
from ProcB import FunctionB
import Lines
import Feature
#===============================================================================
# Test Pipe
#===============================================================================
def cam_loop(pipe_parent):
imagePathes = glob.glob("Images\*.jpg")
for path in imagePathes:
image = cv2.imread(path)
pipe_parent.send(image)
StringFromChild = pipe_parent.recv()
print("StringFromChild:",StringFromChild)
def show_loop(pipe_child):
#cv2.namedWindow('pepe')
proc = Preprocessor.Preprocessor()
line = Lines.Lines()
features = Feature.FeatureDetector()
imgIdx = 0
Q_Barcode = multiprocessing.Queue(10)
Q_CapFeatures = multiprocessing.Queue(100)
while True:
image = pipe_child.recv()
start = time.time()
#Calculating features
#Create processes
p1 = multiprocessing.Process(target = FunctionA, args = (proc, image, imgIdx, None, None, None,))
p1.start()
p2 = multiprocessing.Process(target = FunctionB, args = (line, proc, features, image, imgIdx,))
p2.start()
p1.join()
p2.join()
#send features calculated to parent
pipe_child.send("OK")
end = time.time()
print("TimeMultiProc",end - start)
start = time.time()
#Calculating feature
FunctionA(proc, image, imgIdx, None, None, None)
FunctionB(line, proc, features, image, imgIdx)
#send features calculated to parent
end = time.time()
print("TimeSerial",end - start)
if __name__ == '__main__':
logger = multiprocessing.log_to_stderr()
logger.setLevel(multiprocessing.SUBDEBUG)
pipe_parent, pipe_child = multiprocessing.Pipe()
cam_process = multiprocessing.Process(target=cam_loop,args=(pipe_parent, ))
cam_process.start()
show_process = multiprocessing.Process(target=show_loop,args=(pipe_child, ))
show_process.start()
cam_process.join()
show_loop.join()