Save videos in every 500 episode in OpenAI Gym - openai-gym

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?

Related

How to fix an error in open gym environment

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()

Panda gym returning errors when running

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

Pytorch Dataset for video

Hi I made a video frames loader Dataset to be fed into a pytorch model. I want to sample frames from a video, but the frames should be uniformly sampled from each video. This is the class I came up with. I was wondering if there was any better method to speed up the sampling process.
Do you have any suggestion especially in the read_video method part??
Thanks
import torch
import torchvision as tv
import cv2
import numpy as np
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
from pathlib import Path
class VideoLoader(torch.utils.data.Dataset):
def __init__(self, data_path, classes, transforms=None, max_frames=None, frames_ratio=None):
super(VideoLoader, self).__init__()
self.data_path = data_path
self.classes = classes
self.frames_ratio = frames_ratio
self.transforms = transforms
self.max_frames = max_frames
def read_video(self, path):
frames = []
vc = cv2.VideoCapture(path)
total_frames = int(vc.get(cv2.CAP_PROP_FRAME_COUNT))
if self.frames_ratio:
if type(self.frames_ratio) is float:
frames_to_pick = int(total_frames * self.frames_ratio)
else:
frames_to_pick = self.frames_ratio
else:
frames_to_pick = total_frames
idxs = np.linspace(0, total_frames, frames_to_pick, endpoint=False)
for i in idxs:
ok, f = vc.read()
if ok:
f = tv.transforms.ToTensor()(f)
f = self.transforms(f) if self.transforms else f
frames.append(f)
vc.set(cv2.CAP_PROP_POS_FRAMES, i)
if self.max_frames and len(frames) == self.max_frames: break
else: break
vc.release()
return torch.stack(frames)
def __getitem__(self, index):
v_path, label = self.data_path[index]
return self.read_video(v_path), self.classes[label]
def __len__(self): return len(self.data_path)
Because you can't really seek through a video in parallel, there's not really any faster sampling process you can run locally. I personally had trouble with this problem which is why I started building a simple API for this called Sieve. You can literally upload data directly to Sieve (either from a cloud bucket or from local storage) and it'll quickly cut up all the frames for you and even mark them with things like motion, people, objects, and more. It parallelizes using serverless functions in the cloud which makes it really fast, even for hours or days of footage.
You can then quickly export from Sieve using the dashboard which gives you a quick curl command you can run to download the exact samples you want.
Here's a helpful repo: https://github.com/Sieve-Data/automatic-video-processing
If you are happy with extracting the frames of each video to disk beforehand, this library is exactly what you're looking for:
Video-Dataset-Loading-PyTorch on Github
https://github.com/RaivoKoot/Video-Dataset-Loading-Pytorch

Python Flask for cnn-text-classification tensorflow POST request format

I am doing cnn-text-classification-tf. My aim is to do prediction from frozen graph.
My question is how can the prediction be done from the frozen graph. I found a great tutorial https://blog.metaflow.fr/tensorflow-how-to-freeze-a-model-and-serve-it-with-a-python-api-d4f3596b3adc. where he is implementing frozen graph using flask.
I am using flask code like below to do prediction.
import json, argparse, time
import tensorflow as tf
from linkedin import load_graph
from flask import Flask, request
from flask_cors import CORS
##################################################
# API part
##################################################
app = Flask(__name__)
cors = CORS(app)
#app.route("/api/predict", methods=['POST'])
def predict():
start = time.time()
data = request.data.decode("utf-8")
if data == "":
params = request.form
x_in = json.loads(params['x'])
else:
params = json.loads(data)
x_in = params['x']
##################################################
# Tensorflow part
##################################################
y_out = persistent_sess.run(y, feed_dict={
x: x_in
# x: [[3, 5, 7, 4, 5, 1, 1, 1, 1, 1]] # < 45
})
##################################################
# END Tensorflow part
##################################################
json_data = json.dumps({'y': y_out.tolist()})
print("Time spent handling the request: %f" % (time.time() - start))
return json_data
##################################################
# END API part
##################################################
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--frozen_model_filename", default="frozen_model2.pb", type=str, help="Frozen model file to import")
parser.add_argument("--gpu_memory", default=.2, type=float, help="GPU memory per process")
args = parser.parse_args()
##################################################
# Tensorflow part
##################################################
print('Loading the model')
graph = load_graph(args.frozen_model_filename)
x = graph.get_tensor_by_name('prefix/input_x:0')
y = graph.get_tensor_by_name('prefix/output/predictions:0')
print('Starting Session, setting the GPU memory usage to %f' % args.gpu_memory)
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory)
sess_config = tf.ConfigProto(gpu_options=gpu_options)
persistent_sess = tf.Session(graph=graph, config=sess_config)
##################################################
# END Tensorflow part
##################################################
print('Starting the API')
app.run()
I am using terminal on MacOS to run this script
After running this I am using postman to POST the request:
How do I correctly frame this request to get proper response. What is the exact input needed in Postman in the body?
In Postman, you are sending a POST request and data in json format, so you would need to do changes in code.
for getting data, first do the validation as follows:
if not 'data' in request.json:
abort(400)
After that you can access the string as follows:
x_in = request.json["data"]
But in case, you want to do changes in the request then you can try sending data as follows:
{
"data" : {
"x" : "good movie it was"
}
}
This should work, in case not, then do let me know.

Does tensorflow-serving supports multi-threading?

I have some problem with using tensorflow serving.
I deployed my tensorflow model as RESTful APIs using tensorflow serving. But I doubt if tf-serving server supports multi-threading. I've done some experiments and it does not seem to be.
I also noticed that there is --tensorflow_session_parallelism option for tensorflow_model_server, but using the option makes my server more slow..
Is there any reference for using tensorflow serving with multi-threading?
Elaborating the content of the link provided by #ReInvent_IO, just in case if the link doesn't work in the future.
Code for the same is shown below:
"""A client that talks to tensorflow_model_server loaded with mnist model.
The client downloads test images of mnist data set, queries the service with
such test images to get predictions, and calculates the inference error rate.
Typical usage example:
mnist_client.py --num_tests=100 --server=localhost:9000
"""
from __future__ import print_function
import sys
import threading
# This is a placeholder for a Google-internal import.
import grpc
import numpy
import tensorflow as tf
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
import mnist_input_data
Setting the value of concurrency to 5 asking the server to run 5 different threads
tf.app.flags.DEFINE_integer('concurrency', 5,
'maximum number of concurrent inference requests')
tf.app.flags.DEFINE_integer('num_tests', 100, 'Number of test images')
tf.app.flags.DEFINE_string('server', '', 'PredictionService host:port')
tf.app.flags.DEFINE_string('work_dir', '/tmp', 'Working directory. ')
FLAGS = tf.app.flags.FLAGS
class _ResultCounter(object):
"""Counter for the prediction results."""
def __init__(self, num_tests, concurrency):
self._num_tests = num_tests
self._concurrency = concurrency
self._error = 0
self._done = 0
self._active = 0
self._condition = threading.Condition()
def inc_error(self):
with self._condition:
self._error += 1
def inc_done(self):
with self._condition:
self._done += 1
self._condition.notify()
def dec_active(self):
with self._condition:
self._active -= 1
self._condition.notify()
def get_error_rate(self):
with self._condition:
while self._done != self._num_tests:
self._condition.wait()
return self._error / float(self._num_tests)
def throttle(self):
with self._condition:
while self._active == self._concurrency:
self._condition.wait()
self._active += 1
def _create_rpc_callback(label, result_counter):
"""Creates RPC callback function.
Args:
label: The correct label for the predicted example.
result_counter: Counter for the prediction result.
Returns:
The callback function.
"""
def _callback(result_future):
"""Callback function.
Calculates the statistics for the prediction result.
Args:
result_future: Result future of the RPC.
"""
exception = result_future.exception()
if exception:
result_counter.inc_error()
print(exception)
else:
sys.stdout.write('.')
sys.stdout.flush()
response = numpy.array(
result_future.result().outputs['scores'].float_val)
prediction = numpy.argmax(response)
if label != prediction:
result_counter.inc_error()
result_counter.inc_done()
result_counter.dec_active()
return _callback
def do_inference(hostport, work_dir, concurrency, num_tests):
"""Tests PredictionService with concurrent requests.
Args:
hostport: Host:port address of the PredictionService.
work_dir: The full path of working directory for test data set.
concurrency: Maximum number of concurrent requests.
num_tests: Number of test images to use.
Returns:
The classification error rate.
Raises:
IOError: An error occurred processing test data set.
"""
test_data_set = mnist_input_data.read_data_sets(work_dir).test
channel = grpc.insecure_channel(hostport)
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
result_counter = _ResultCounter(num_tests, concurrency)
for _ in range(num_tests):
request = predict_pb2.PredictRequest()
request.model_spec.name = 'mnist'
request.model_spec.signature_name = 'predict_images'
image, label = test_data_set.next_batch(1)
request.inputs['images'].CopyFrom(
tf.contrib.util.make_tensor_proto(image[0], shape=[1, image[0].size]))
result_counter.throttle()
result_future = stub.Predict.future(request, 5.0) # 5 seconds
result_future.add_done_callback(
_create_rpc_callback(label[0], result_counter))
return result_counter.get_error_rate()
def main(_):
if FLAGS.num_tests > 10000:
print('num_tests should not be greater than 10k')
return
if not FLAGS.server:
print('please specify server host:port')
return
error_rate = do_inference(FLAGS.server, FLAGS.work_dir,
FLAGS.concurrency, FLAGS.num_tests)
print('\nInference error rate: %s%%' % (error_rate * 100))
if __name__ == '__main__':
tf.app.run()

Resources