B - 3D City Model

本文介绍了一种计算基于矩形网格构建的城市模型表面总面积的方法。该模型由一系列堆叠的立方体建筑组成,并通过一个n×m的数字矩阵来描述各建筑物的高度。文章提供了一个C++程序实例,用于读取输入并计算包括顶部、底部和侧面在内的总表面积。

A city is built on the top of a rectangular n × m grid where all the grid cells are equal squares. Each of the n·m grid cells can serve as a foundation of a single building in the city. A building is represented as a number of 1 × 1 × 1cubes stacked on the top of each other. The cube that lays in the foundation of a building entirely occupies a single cell on the grid. It is clear that adjacent buildings can share a wall or a part of it. Typical cities can be seen on the image below.

The King of Berland has a 3D model of the capital city in his office. This model was made on a special 3D-printer out of plastic. It represents a layout of the capital city, but the scale is smaller, so it's very convenient for the King to examine the model without having to visit the city itself. The King is bored though because the model is colorless, so he wants to paint the model. To calculate the exact amount of required paint he should know the total area of the model's surface.

You have to help the King and write a program that will calculate the required surface area of the given model. While calculating the surface area you should count not only the side surfaces, but also the areas of the top and bottom facets.

The model is given to you as n × m matrix of digits. A digit in the j-th position of the i-th row stands for the height of the building with its foundation in cell (i, j) of the model. If the corresponding digit is equal to "0", it means there is no building built on the top of this cell.

Input

The first line of input contains a pair of integers n, m (1 ≤ n, m ≤ 100), wheren — amount of rows in the given grid, m — amount of columns. The following nlines contain the description of the model. These n lines contain m digits each representing heights of the buildings. It's guaranteed that the given matrix contains at least one non-zero digit.

Output

Output the only positive integer — surface area of the model.

Examples
Input
3 3
111
212
111
Output
38
Input
3 4
1000
0010
0000
Output
12
Note

The first sample test corresponds to the leftmost picture from the problem statement.

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
char a[105][105];
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n, m;
    cin >> n >> m;

    int sum = 0;
    int i, j, k;
    for(i = 0; i < n; i++)
    {
            cin >> a[i];
    }
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            if(a[i][j] == '0')continue;
            sum += 2;
            if(i == 0)
            {
                sum += a[i][j]  - '0';
                if(n == 1){sum += a[i][j] - '0';}
                else if(i + 1 < n && a[i + 1][j] < a[i][j])sum += a[i][j] - a[i + 1][j];
            }
            else if(i == n - 1)
            {
                sum += a[i][j] - '0';
                if(n == 1){sum += a[i][j] - '0';}
                else if(i - 1 >= 0 && a[i - 1][j] < a[i][j])sum += a[i][j] - a[i - 1][j];
            }
            else
            {
                if(i + 1 < n && a[i + 1][j] < a[i][j])sum += a[i][j] - a[i + 1][j];
                if(i - 1 >= 0 && a[i - 1][j] < a[i][j])sum += a[i][j] - a[i - 1][j];
            }
            if(j == 0)
            {
                sum += a[i][j] - '0';
                if(m == 1){sum += a[i][j] - '0';}
                else if(j + 1 < m && a[i][j] > a[i][j + 1])sum += a[i][j] - a[i][j + 1];
            }
            else if(j == m - 1)
            {
                sum += a[i][j] - '0';
                if(m == 1){sum += a[i][j] - '0';}
                else if(j - 1 >= 0 && a[i][j] > a[i][j - 1])sum += a[i][j] - a[i][j - 1];
            }
            else
            {
                if(j + 1 < m && a[i][j] > a[i][j + 1])sum += a[i][j] - a[i][j + 1];
                if(j - 1 >= 0 && a[i][j] > a[i][j - 1])sum += a[i][j] - a[i][j - 1];
            }
        }
    }
    cout << sum << endl;
    return 0;
}

Simulation ​Simulation Configuration: All configuration files for simulation scenes are located in: /OpenFly-Platform/configs/ Files are ​named after their corresponding scenario (e.g., env_airsim_16.yaml). ​These YAML files define simulation parameters including IP addresses, ports for scene orchestration, and communication ports for toolchain integration. UE (env_ue_xxx) Download files from Huggingface link and unzip, or For custom UE scenes, configure them through UnrealCV plugin integration by following the doc Move env_ue_xxx/ folders to OpenFly-Platform/envs/ue/ AirSim (env_airsim_xxx) Download files from Huggingface link and unzip Move env_airsim_xxx/ folders to OpenFly-Platform/envs/airsim/ 3DGS (env_gs_xxx) Prepare SIBR_viewers Refer to this project # For Ubuntu 22.04, install dependencies: sudo apt install -y cmake libglew-dev libassimp-dev libboost-all-dev libgtk-3-dev libopencv-dev libglfw3-dev libavdevice-dev libavcodec-dev libeigen3-dev libxxf86vm-dev libembree-dev #​ return to the ./OpenFly-Platform directory​ cd envs/gs/SIBR_viewers cmake . -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_IBR_HIERARCHYVIEWER=ON -DBUILD_IBR_ULR=OFF -DBUILD_IBR_DATASET_TOOLS=OFF -DBUILD_IBR_GAUSSIANVIEWER=OFF cmake --build build -j --target install --config Release Download files from Huggingface link (Coming soon), or you can generate custom env following Hierarchial 3DGS Move env_gs_xxx/ folders to OpenFly-Platform/envs/gs/ GTAV (env_game_xxx) ( ! You should configure it under Windows ) Installation of DeepGTAV following this repo Set the game in windowed mode In the graphics setting MSAA has to be disabled, otherwise no objects are detected. The correct settings should be loaded by replacing the files in Documents/Rockstar Games/GTA V/Profiles/, but keep this in mind if you modify the game settings If you have a 4k screen and want to capture 4k data (very hardware hungry, but runs smooth on an RTX3090): Set the screen resolution to "7680x4320DSR" in NVIDIA GeForce Experience. This increases the buffer sizes for pixel perfect 4k segmentation data. ​Simulation Usage: For UE, AirSim and 3DGS, launch the env_bridge, and wait for around 20 seconds until you see 'ready to be connected'. #​ return to the ./OpenFly-Platform directory​ conda activate openfly python scripts/sim/env_bridge.py --env env_xx_xxx #such as env_airsim_16b # wait for 20s Due to the GPU-intensive nature of rendering in UE projects, we start it in headless mode. If you want to view the UE rendering screen, please comment out the headless-related code in envs/ue/env_ue_xxx/CitySample.sh. Note that this may cause the rendering interface to crash. For GTAV pip install pyautogui Clone the DeepGTAV repo, put the compiled plugin to the root directory of GTAV. Start GTA V and enter story mode. Change camera to First-per view (by pressing V) (preferred) In settings, set the screen resolution to "7680x4320DSR" and place the window on top-left corner of your screen Run eval.py for evaluation. Toolchain ​Toolchain Configuration: All configuration files for Toolchain are located in: /OpenFly-Platform/configs/ Files are ​named after their corresponding scenario (e.g., env_airsim_16.yaml). ​For each scenario, the simulation and toolchain share the same configuration file. For the toolchain, the configuration file includes the occupancy map information, trajectory generation parameters, etc. The specific meaning of each parameter can be found in the configuration file /OpenFly-Platform/configs/env_airsim_16.yaml . Scene data files Download files from Huggingface link and unzip,or generate point cloud data by following the toolchain configuration. Move env_xx_xx.pcd files to OpenFly-Platform/scene_data/pcd_map. Raw point cloud files (PCD format) for scenes are stored in: /OpenFly-Platform/scene_data/pcd_map/. Processed segmentation data (e.g., semantic labels, region partitions) is stored in: /OpenFly-Platform/scene_data/seg_map/. ​Toolchain Usage: point cloud generation: UE: We utilized the dataset provided by this project, using depth maps and pose information, and synthesized point clouds according to this code. AirSim: We traversed the simulated environment through a grid, collected lidar point clouds, and transformed the coordinates to the world coordinate system. Finally, we merged all the point clouds to obtain the point cloud of the entire scene. You can adjust the scanning range MapBound and interval LidarDelta in the corresponding configs/env_airsim_xx.yaml. #​ return to the ./OpenFly-Platform directory​ conda activate openfly bash scripts/toolchain/pcdgen_tool.sh env_xx_xxx #such as env_airsim_16 3DGS: We directly utilized the sparse point cloud synthesized by colmap, with the file path at <3dgs_dataset>/camera_calibration/aligned/sparse/0/points3D.ply. GTAV(Coming soon) segmentation generation: UE, AirSim, and 3DGS: Executing the simulation, run the shell script responsible for generation the segmentation.We provide two selectable modes: ​​BEV generation​​ and ​​manual annotation​​. #​ return to the ./OpenFly-Platform directory​ #BEV generation bash scripts/toolchain/seggen_tool.sh env_xx_xxx bev #such as env_airsim_16 ​#manual annotation​ bash scripts/toolchain/seggen_tool.sh env_xx_xxx manual #such as env_airsim_16 Due to the high GPU requirements for rendering urban scenes in UE, to avoid program crashes, the uploaded binary files are configured to use the lowest rendering settings by default (we found that this does not affect the quality of the generated images). If you wish to render images with higher quality, you can refer to env_ue_xxx/City_UE52/Saved/Config/Linux/GameUserSettings_best.ini to modify GameUserSettings.ini. GTAV(Coming soon) trajectory generation: UE, AirSim, and 3DGS: Executing the simulation, run the shell script responsible for generating the trajectory. #​ return to the ./OpenFly-Platform directory​ bash scripts/toolchain/trajgen_tool.sh env_xx_xxx #such as env_airsim_16 GTAV(Coming soon) instruction generation: Our OpenFly dataset has been converted to parquet format. If you want to use this code, you need to convert parquet back to uncompressed format. So we recommend that you use this code to generate your own trajectory instructions. You need to prepare: 1. OpenAI's API 2. Traj in the same format as the OpenFly dataset (a series of images and a jsonl file that records actions) When using GPT to generate instructions, first configure the "key" and "model" in the /OpenFly-Platform/tool_ws/src/ins_gen/gpt_api_config.json file, modify the data directory in /OpenFly-Platform/tool_ws/src/ins_gen/gpt_generation.py. And use a json file to store all the traj directories you want to generate instructions Then run the shell script responsible for generating the instructions. #​ return to the ./OpenFly-Platform directory​ conda activate openfly python3 tool_ws/src/ins_gen/gpt_generation.py --json Your json PATH --type train/test # Training: Data Preparation We are currently optimizing the datasets. The Hugging Face link is temporarily unavailable. Coming soon! For your custom datasets, make sure that you pre-build all the required TensorFlow Datasets (TFDS) datasets. Firstly, prepare the train.json with following format: [ { 'image_path': '', 'gpt_instruction': 'Proceed towards a medium ...', 'action': [9, ...], 'index_list': ['image_2', ...], 'pos': [[-93.81, 536.11, 74.86], ... ], 'yaw': [1.57, 1.57, 1.57, 1.57] }, ... ] Now you can run train/datasets_builder/vln/vln_dataset_builder.py which is used to transfer the generated data format to rlds format. cd train/datasets/vln tfds build --data_dir <TFDS_DATA_DIR> For custom dataset, you need to change the data mixture after transferring the data format in train/datasets/dataset.py to specify the names of datasets. OXE_NAMED_MIXTURES: Dict[str, List[Tuple[str, float]]] = { "vln_mix" : [ ("vln_scene1", 1.0), ("vln_scene2", 1.0), ], } Download Pretrained Weights We released our pretrained weights of Openfly-Agent which is trained by full fine-tuning OpenVLA model checkpoint. Now you can download the weights and directly finetuning your data. Model Link Openfly-Agent huggingface Train The training script are train/train.sh. And you need to change following parameters: grid_size :refers to the token compress ratio. history_frames :refers to the number of frame as history information, which should be corresponded to your custom dataset setting. model_name_or_path :path to the pretrained VLM weights Other hyperparameters like "batch_size", "save_steps" could be customized according to your computation resources. Evaluation You can refer to our evaluation script train/eval.py to evaluate your openfly-agent. We use the eval_test.json file as a demonstration to configure the environments that need to be evaluated. You can customize an evaluation configuration file based on the json files available at https://huggingface.co/datasets/IPEC-COMMUNITY/OpenFly/tree/main/Annotation Test Make sure your trained checkpoint dir has two files: "data_statistics.json". If not, please copy them from downloaded openfly-agent weights or this link. from typing import Dict, List, Optional, Union from pathlib import Path import numpy as np import torch import cv2 from PIL import Image from transformers import LlamaTokenizerFast from transformers import AutoConfig, AutoImageProcessor, AutoModelForVision2Seq, AutoProcessor import os, json from model.prismatic import PrismaticVLM from model.overwatch import initialize_overwatch from model.action_tokenizer import ActionTokenizer from model.vision_backbone import DinoSigLIPViTBackbone, DinoSigLIPImageTransform from model.llm_backbone import LLaMa2LLMBackbone from extern.hf.configuration_prismatic import OpenFlyConfig from extern.hf.modeling_prismatic import OpenVLAForActionPrediction from extern.hf.processing_prismatic import PrismaticImageProcessor, PrismaticProcessor AutoConfig.register("openvla", OpenFlyConfig) AutoImageProcessor.register(OpenFlyConfig, PrismaticImageProcessor) AutoProcessor.register(OpenFlyConfig, PrismaticProcessor) AutoModelForVision2Seq.register(OpenFlyConfig, OpenVLAForActionPrediction) model_name_or_path="IPEC-COMMUNITY/openfly-agent-7b" processor = AutoProcessor.from_pretrained(model_name_or_path) model = AutoModelForVision2Seq.from_pretrained( model_name_or_path, attn_implementation="flash_attention_2", # [Optional] Requires `flash_attn` torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, trust_remote_code=True, ).to("cuda:0") image = Image.fromarray(cv2.imread("example.png")) prompt = "Take off, go straight pass the river" inputs = processor(prompt, [image, image, image]).to("cuda:0", dtype=torch.bfloat16) action = model.predict_action(**inputs, unnorm_key="vln_norm", do_sample=False) print(action)详细解释一下
最新发布
09-26
这段代码展示了一个使用预训练模型进行视觉-语言-动作(Vision-Language-Action, VLA)推理的示例。它加载了一个预训练的 OpenFly-Agent 模型,处理输入图像和文本提示,并预测对应的动作。以下是详细的解释: --- ### 1. **导入必要的库** ```python from typing import Dict, List, Optional, Union from pathlib import Path import numpy as np import torch import cv2 from PIL import Image from transformers import LlamaTokenizerFast from transformers import AutoConfig, AutoImageProcessor, AutoModelForVision2Seq, AutoProcessor import os, json ``` - **`typing` 模块**:用于类型提示,提高代码可读性和安全性。 - **`Pathlib`**:用于更现代的文件路径操作。 - **`numpy` 和 `torch`**:用于数值计算和深度学习。 - **`cv2` 和 `PIL.Image`**:用于图像处理。 - **`transformers` 库**:用于加载预训练模型和处理器。 --- ### 2. **注册自定义模型配置和处理器** ```python AutoConfig.register("openvla", OpenFlyConfig) AutoImageProcessor.register(OpenFlyConfig, PrismaticImageProcessor) AutoProcessor.register(OpenFlyConfig, PrismaticProcessor) AutoModelForVision2Seq.register(OpenFlyConfig, OpenVLAForActionPrediction) ``` - **`AutoConfig`、`AutoImageProcessor`、`AutoModelForVision2Seq`**:HuggingFace Transformers 提供的自动类,用于加载模型配置、图像处理器和模型。 - 这里注册了自定义的 `OpenFlyConfig`、`PrismaticImageProcessor` 和 `OpenVLAForActionPrediction`,以便 Transformers 能够识别并正确加载这些自定义组件。 --- ### 3. **加载预训练模型和处理器** ```python model_name_or_path="IPEC-COMMUNITY/openfly-agent-7b" processor = AutoProcessor.from_pretrained(model_name_or_path) model = AutoModelForVision2Seq.from_pretrained( model_name_or_path, attn_implementation="flash_attention_2", # [Optional] Requires `flash_attn` torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, trust_remote_code=True, ).to("cuda:0") ``` - **`model_name_or_path`**:指定预训练模型的路径或 HuggingFace ID。 - **`processor`**:自动加载与模型对应的处理器,用于文本和图像的预处理。 - **`model`**:加载预训练的 VLA 模型,并将其移动到 GPU(`cuda:0`)上。 - **`attn_implementation="flash_attention_2"`**:使用 Flash Attention 技术优化注意力机制,提高推理速度。 - **`torch_dtype=torch.bfloat16`**:使用 `bfloat16` 精度减少内存占用并加速计算。 - **`low_cpu_mem_usage=True`**:减少 CPU 内存占用。 - **`trust_remote_code=True`**:允许加载远程代码(如自定义模型类)。 --- ### 4. **加载图像并准备提示** ```python image = Image.fromarray(cv2.imread("example.png")) prompt = "Take off, go straight pass the river" ``` - **`cv2.imread("example.png")`**:使用 OpenCV 读取图像。 - **`Image.fromarray(...)`**:将 OpenCV 图像转换为 PIL 图像格式。 - **`prompt`**:提供一个文本提示,描述需要执行的动作。 --- ### 5. **预处理输入并进行推理** ```python inputs = processor(prompt, [image, image, image]).to("cuda:0", dtype=torch.bfloat16) action = model.predict_action(**inputs, unnorm_key="vln_norm", do_sample=False) print(action) ``` - **`processor(prompt, [image, image, image])`**:将文本提示和图像序列(这里重复了三次图像)输入处理器,生成模型所需的输入张量。 - **`.to("cuda:0", dtype=torch.bfloat16)`**:将输入数据移动到 GPU 并转换为 `bfloat16` 精度。 - **`model.predict_action(...)`**:调用模型的 `predict_action` 方法,传入输入数据和参数: - **`unnorm_key="vln_norm"`**:指定用于反归一化的键。 - **`do_sample=False`**:不进行采样,直接使用贪婪解码。 - **`print(action)`**:输出预测的动作。 --- ### 6. **总结** 这段代码展示了如何使用 OpenFly-Agent 模型进行视觉-语言-动作推理。它加载了预训练模型,处理了输入图像和文本提示,并输出预测的动作。整个过程涉及图像处理、文本处理、模型推理等多个步骤,适用于机器人控制、自动驾驶等任务。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值