tensorflow_tutorials_libs.utils

本文介绍了一系列在使用TensorFlow处理神经网络时的实用工具函数,包括创建权重和偏置变量、图像批量拼接、权重可视化及数据腐蚀等,旨在帮助开发者更高效地进行神经网络的训练与调试。
"""
Some useful utilities when dealing with neural nets w/ tensorflow.
Parag K. Mital, Jan. 2016
"""
import tensorflow as tf
import numpy as np

def montage_batch(images):
    """Draws all filters (n_input * n_output filters) as a
    montage image separated by 1 pixel borders.

    Parameters
    ----------
    batch : numpy.ndarray
        Input array to create montage of.

    Returns
    -------
    m : numpy.ndarray
        Montage image.
    """
    img_h = images.shape[1]
    img_w = images.shape[2]
    n_plots = int(np.ceil(np.sqrt(images.shape[0])))
    m = np.ones(
        (images.shape[1] * n_plots + n_plots + 1,
         images.shape[2] * n_plots + n_plots + 1, 3)) * 0.5

    for i in range(n_plots):
        for j in range(n_plots):
            this_filter = i * n_plots + j
            if this_filter < images.shape[0]:
                this_img = images[this_filter, ...]
                m[1 + i + i * img_h:1 + i + (i + 1) * img_h,
                  1 + j + j * img_w:1 + j + (j + 1) * img_w, :] = this_img
    return m


# %%
def montage(W):
    """Draws all filters (n_input * n_output filters) as a
    montage image separated by 1 pixel borders.

    Parameters
    ----------
    W : numpy.ndarray
        Input array to create montage of.

    Returns
    -------
    m : numpy.ndarray
        Montage image.
    """
    W = np.reshape(W, [W.shape[0], W.shape[1], 1, W.shape[2] * W.shape[3]])
    n_plots = int(np.ceil(np.sqrt(W.shape[-1])))
    # 创建大矩阵,将所有通道的卷积核放进去,其中元素1即分隔像素点
    m = np.ones(
        (W.shape[0] * n_plots + n_plots + 1,
         W.shape[1] * n_plots + n_plots + 1)) * 0.5
    for i in range(n_plots):
        for j in range(n_plots):
            this_filter = i * n_plots + j
            if this_filter < W.shape[-1]:
                m[1 + i + i * W.shape[0]:1 + i + (i + 1) * W.shape[0],
                  1 + j + j * W.shape[1]:1 + j + (j + 1) * W.shape[1]] = (
                  	# 第3维:通道和第4维:通道序号,都会被squeeze消除掉,只剩卷积核的宽和高上的值
                    np.squeeze(W[:, :, :, this_filter]))
    return m




# %%
def corrupt(x):
    """Take an input tensor and add uniform masking.

    Parameters
    ----------
    x : Tensor/Placeholder
        Input to corrupt.

    Returns
    -------
    x_corrupted : Tensor
        50 pct of values corrupted.
    """
    return tf.multiply(x, tf.cast(tf.random_uniform(shape=tf.shape(x),
                                               minval=0,
                                               maxval=2,
                                               dtype=tf.int32), tf.float32))


# %%
def weight_variable(shape):
    '''Helper function to create a weight variable initialized with
    a normal distribution

    Parameters
    ----------
    shape : list
        Size of weight variable
    '''
    initial = tf.random_normal(shape, mean=0.0, stddev=0.01)
    return tf.Variable(initial)


# %%
def bias_variable(shape):
    '''Helper function to create a bias variable initialized with
    a constant value.

    Parameters
    ----------
    shape : list
        Size of weight variable
    '''
    initial = tf.random_normal(shape, mean=0.0, stddev=0.01)
    return tf.Variable(initial)

<think>好的,我现在需要帮助用户解决roslaunch命令执行无响应的问题,具体是执行`roslaunch bingda_tutorials gazebo_world.launch`时没有反应。首先,我得回想一下常见的ROS问题排查步骤,并结合用户提供的引用内容来寻找可能的解决方案。 首先,用户提到的引用[3]中提到,当出现类似错误时,可能是因为找不到对应的package或launch文件,解决方法通常是source工作空间的setup.bash文件。所以这可能是一个潜在的原因。用户需要确认是否正确source了工作空间,并且包名和launch文件名是否正确。 接下来,用户的问题是无反应,而不是直接报错,这可能意味着命令在运行但卡住了,或者根本没有启动。我需要考虑其他可能性,比如roscore是否已经启动,因为roslaunch需要roscore的存在。如果roscore没有运行,roslaunch可能会挂起或没有输出。 另外,环境变量是否正确设置也是一个关键点。用户可能需要检查ROS_PACKAGE_PATH是否包含相关的工作空间路径。此外,launch文件本身可能存在错误,比如语法错误或依赖项未安装,这可能导致roslaunch在解析时卡住。 还有,权限问题也可能导致launch文件无法执行,比如launch文件没有可执行权限,或者某些节点需要sudo权限。不过这种情况通常会提示权限被拒绝,但也不排除某些情况下没有明显提示。 此外,查看roslaunch的详细输出可能会有帮助。用户可以使用`--screen`参数来查看输出信息,例如`roslaunch bingda_tutorials gazebo_world.launch --screen`,这样可以将日志输出到终端,帮助定位问题。 总结一下,可能的排查步骤包括: 1. 检查包和launch文件是否存在。 2. 确认工作空间已正确编译并source。 3. 确保roscore正在运行。 4. 使用`--screen`参数查看详细输出。 5. 检查launch文件语法和依赖项。 6. 确认环境变量和权限设置正确。 需要一步步引导用户进行这些检查,并提供相应的解决建议。同时,结合用户提供的引用内容,特别是关于source工作空间的步骤,可能需要重点强调这一点,因为这是常见的问题来源。</think>### 排查roslaunch命令无响应的步骤 #### 1. **确认包和launch文件是否存在** - 使用`rospack find bingda_tutorials`检查包路径是否存在。若返回`Package 'bingda_tutorials' not found`,说明包未正确安装或未被ROS识别[^3]。 - 检查launch文件路径:进入包目录查看`gazebo_world.launch`是否在`launch`文件夹中,例如: ```bash ls ~/catkin_ws/src/bingda_tutorials/launch/ ``` #### 2. **确保工作空间已正确编译并source** - 编译工作空间: ```bash cd ~/catkin_ws && catkin_make ``` - **Source环境变量**(关键步骤): ```bash source ~/catkin_ws/devel/setup.bash ``` 若未执行此操作,ROS无法识别新编译的包[^3]。 #### 3. **检查roscore是否运行** - 在终端执行`roscore`,确保ROS核心已启动。若未运行,`roslaunch`会因无法连接而卡住。 - 可通过新终端执行`rosnode list`验证,若返回`/rosout`则表示roscore正常。 #### 4. **添加`--screen`参数查看详细输出** - 执行命令时添加`--screen`以显示日志: ```bash roslaunch bingda_tutorials gazebo_world.launch --screen ``` - 观察输出是否包含以下错误: - `RLException: [gazebo_world.launch] is neither a launch file...`: 包路径问题[^3]。 - `[ERROR] [launch]: 节点未找到`: 依赖包未安装(如缺少`gazebo_ros`)[^1]。 #### 5. **验证依赖项和launch文件语法** - **依赖项检查**: - 确保`package.xml`中声明了所有依赖(如`gazebo_ros`),并通过`rosdep install`安装: ```bash rosdep install --from-paths src --ignore-src -y ``` - **launch文件语法**: - 检查XML标签是否闭合,参数定义是否正确。例如: ```xml <launch> <include file="$(find gazebo_ros)/launch/empty_world.launch" /> <!-- 示例引用[^1] --> </launch> ``` #### 6. **环境变量与权限问题** - 检查`ROS_PACKAGE_PATH`是否包含工作空间: ```bash echo $ROS_PACKAGE_PATH ``` 若缺失路径,需重新source。 - 赋予launch文件执行权限: ```bash chmod +x ~/catkin_ws/src/bingda_tutorials/launch/gazebo_world.launch ``` --- ### 常见原因与解决方法 | 现象 | 可能原因 | 解决方法 | |-----------------------|-----------------------------------|--------------------------------------------------------------------------| | 终端无任何输出 | roscore未启动 | 新终端执行`roscore` | | 报错`RLException` | 包未正确source或路径错误 | 执行`source ~/catkin_ws/devel/setup.bash`并检查包名 | | Gazebo黑屏/卡住 | Gazebo模型加载慢或网络问题 | 提前下载模型(`~/.gazebo/models`),或禁用GUI:`<arg name="gui" value="false"/>` | | 节点启动失败 | 依赖包未安装 | 使用`rosdep install`安装缺失依赖 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值