python - WARN : Tried to pass invalid video frame, 标记为损坏的 : Your frame has data type int64, 但我们需要 uint8

标签 python python-3.x ffmpeg gcloud

我正在做一些 Udemy AI 类(class),遇到了一个“教”二维猎豹如何走路的类(class)。我在我的电脑上做练习,但是太费时间了。我决定使用 Google Cloud 运行代码并在几个小时后查看结果。然而,当我运行代码时,我收到以下错误“警告:试图通过
无效的视频帧,标记为损坏:您的帧的数据类型为 int64,但我们需要 uint8(即 0-255 的 RGB 值)”。

执行代码后,我看到了文件夹,但看不到任何视频(只是元信息)。

更多信息(如果有帮助):
我有 1 个 CPU (4g),SSD Ubuntu 16.04 LTS

我还没有尝试任何方法来解决它,因为我不知道该尝试什么。我在网上寻找解决方案,但我无法尝试。

这是代码


import os 
import numpy as np
import gym
from gym import wrappers
import pybullet_envs


class Hp():
    def __init__(self):
        self.nb_steps = 1000
        self.episode_lenght =   1000
        self.learning_rate = 0.02
        self.nb_directions = 32
        self.nb_best_directions = 32
        assert self.nb_best_directions <= self.nb_directions
        self.noise = 0.03
        self.seed = 1
        self.env_name = 'HalfCheetahBulletEnv-v0'


class Normalizer():
    def __init__(self, nb_inputs):
        self.n = np.zeros(nb_inputs)
        self.mean = np.zeros(nb_inputs)
        self.mean_diff = np.zeros(nb_inputs)
        self.var = np.zeros(nb_inputs)

    def observe(self, x):
        self.n += 1.
        last_mean = self.mean.copy()
        self.mean += (x - self.mean) / self.n
        #abajo es el online numerator update
        self.mean_diff += (x - last_mean) * (x - self.mean)
        #abajo online computation de la varianza
        self.var = (self.mean_diff / self.n).clip(min = 1e-2)  

    def normalize(self, inputs):
        obs_mean = self.mean
        obs_std = np.sqrt(self.var)
        return (inputs - obs_mean) / obs_std

class Policy():
    def __init__(self, input_size, output_size):
        self.theta = np.zeros((output_size, input_size))

    def evaluate(self, input, delta = None, direction = None):
        if direction is None:
            return self.theta.dot(input)
        elif direction == 'positive':
            return (self.theta + hp.noise * delta).dot(input)
        else:
            return (self.theta - hp.noise * delta).dot(input)

    def sample_deltas(self):
        return [np.random.randn(*self.theta.shape) for _ in range(hp.nb_directions)]

    def update (self, rollouts, sigma_r):
        step = np.zeros(self.theta.shape)
        for r_pos, r_neg, d in rollouts:
            step += (r_pos - r_neg) * d
        self.theta += hp.learning_rate / (hp.nb_best_directions * sigma_r) * step


def explore(env, normalizer, policy, direction = None, delta = None):
    state = env.reset()
    done = False
    num_plays = 0.
    #abajo puede ser promedio de las rewards
    sum_rewards = 0
    while not done and num_plays < hp.episode_lenght:
        normalizer.observe(state)
        state = normalizer.normalize(state)
        action = policy.evaluate(state, delta, direction)
        state, reward, done, _ = env.step(action)
        reward = max(min(reward, 1), -1)
        #abajo sería poner un promedio
        sum_rewards += reward
        num_plays += 1
    return sum_rewards

def train (env, policy, normalizer, hp):
    for step in range(hp.nb_steps):
        #iniciar las perturbaciones deltas y los rewards positivos/negativos
        deltas = policy.sample_deltas()
        positive_rewards = [0] * hp.nb_directions
        negative_rewards = [0] * hp.nb_directions
        #sacar las rewards en la dirección positiva
        for k in range(hp.nb_directions):
            positive_rewards[k] = explore(env, normalizer, policy, direction = 'positive', delta = deltas[k])
        #sacar las rewards en dirección negativo
        for k in range(hp.nb_directions):
            negative_rewards[k] = explore(env, normalizer, policy, direction = 'negative', delta = deltas[k])
        #sacar todas las rewards para sacar la desvest
        all_rewards = np.array(positive_rewards + negative_rewards)
        sigma_r = all_rewards.std()
        #acomodar los rollauts por el max (r_pos, r_neg) y seleccionar la mejor dirección
        scores = {k:max(r_pos, r_neg) for k, (r_pos, r_neg) in enumerate(zip(positive_rewards, negative_rewards))}
        order = sorted(scores.keys(), key = lambda x:scores[x])[:hp.nb_best_directions]
        rollouts = [(positive_rewards[k], negative_rewards[k], deltas[k]) for k in order]
        #actualizar policy
        policy.update (rollouts, sigma_r)
        #poner el final reward del policy luego del update
        reward_evaluation = explore (env, normalizer, policy)
        print('Paso: ', step, 'Lejania: ', reward_evaluation)

def mkdir(base, name):
    path = os.path.join(base, name)
    if not os.path.exists(path):
        os.makedirs(path)
    return path
work_dir = mkdir('exp', 'brs')
monitor_dir = mkdir(work_dir, 'monitor')

hp = Hp()
np.random.seed(hp.seed)
env = gym.make(hp.env_name)
env = wrappers.Monitor(env, monitor_dir, force = True)
nb_inputs = env.observation_space.shape[0]
nb_outputs = env.action_space.shape[0]
policy = Policy(nb_inputs, nb_outputs)
normalizer = Normalizer(nb_inputs)
train(env, policy, normalizer, hp)

最佳答案

最后,我认为它要么是旧版本的 ffmpeg 的问题,要么是另一个兼容性问题(这是我第一次尝试使用 linux,我无法正确更新 ffmpeg)。我将虚拟环境从 Ubunto 16.04 更改为 Debian。它工作得很好。

关于python - WARN : Tried to pass invalid video frame, 标记为损坏的 : Your frame has data type int64, 但我们需要 uint8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57740328/

相关文章:

python - 另一个列表理解问题

python - 从 cmd 导入 Cmd,(不适用于 MyPrompt 类)

python - 如何获取仅匹配和包含部分的正则表达式匹配索引?

c++ - ffmpeg 错误 - 未定义整体

ruby-on-rails-3 - 使用 ffmpeg 转换媒体类型

python - 更改 pyinstaller 可执行文件的 Stanza 模型目录

python - 使用装饰器包装一个接受可变数量参数的函数

python - Scrapy 多个 url 的问题

jquery - 选择一个由带有 Selenium 的 div 元素覆盖的单选按钮

linux - Linux 中的 Ffmpeg 错误