とある科学の備忘録

とある科学の備忘録

CやPythonのプログラミング、Arduino等を使った電子工作をメインに書いています。また、木製CNCやドローンの自作製作記も更新中です。たまに機械学習とかもやってます。

torch-points3dをWindowsのPython3.10で使えるようにする

TL;DR

  • torch-open3dをWindowsで使えるようにした
  • ネット上の記事だと情報が古くてpython3.7やpython3.8のあたりの話しかなかったので、3.10でも使用できることを確認したかった
  • 本家がpoetryを使っていたがuvに変更
  • uv add torch-points-3dとかでbuildに失敗するときは、`--no-build-isolation`オプションを付けることが重要
  • とりあえずuv run eval.pyが動作するところまではできた

出来上がったもの:
github.com

バージョン更新したやつは以下のコミットを参照:
github.com


インストール例参考

上の20nishipがフォークしたレポジトリのuv.lockを使って`uv sync`コマンドを使ったら動くと思うが、念のため巣の状態からのインストール手順を簡単に説明すると、

cd <torch-points3dのレポジトリ>
uv add torch numpy
uv add torch-points-kernels  --no-build-isolation
uv add torch-scatter torch-sparse torch-cluster --no-build-isolation

...
# これでほかのパッケージもインストールしたら torch-points3dのレポジトリの中のeval.pyとかが動くようになるはず
uv run eval.py 

こんな感じで、torchだけを最初に入れておく必要がある。

原因はtorch-points-kernelsなどがtorch本体に依存しているため、requirements.txtの内容を一度に全部入れようとすると以下のような「no module named torch」エラーが出る。

PS C:\Users\ユーザー名\Desktop\test\torch-points3d> uv add torch-scatter torch-sparse torch-cluster
Resolved 266 packages in 635ms                                                                                                                                                                                                                                                                                        
      Built torch-points3d @ file:///C:/Users/ユーザー名\/Desktop/test/torch-points3d                                                                                                                                                                                                                                       
  × Failed to build `torch-cluster==1.6.3`                                                                                                                                                                                                                                                                            
  ├─▶ The build backend returned an error                                                                                                                                                                                                                                                                             
  ╰─▶ Call to `setuptools.build_meta:__legacy__.build_wheel` failed (exit code: 1)                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                      
      [stderr]
      Traceback (most recent call last):
        File "<string>", line 14, in <module>
        File "C:\Users\ユーザー名\\AppData\Local\uv\cache\builds-v0\.tmp4ImQcE\lib\site-packages\setuptools\build_meta.py", line 331, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
        File "C:\Users\ユーザー名\\AppData\Local\uv\cache\builds-v0\.tmp4ImQcE\lib\site-packages\setuptools\build_meta.py", line 301, in _get_build_requires
          self.run_setup()
        File "C:\Users\ユーザー名\\AppData\Local\uv\cache\builds-v0\.tmp4ImQcE\lib\site-packages\setuptools\build_meta.py", line 512, in run_setup
          super().run_setup(setup_script=setup_script)
        File "C:\Users\ユーザー名\AppData\Local\uv\cache\builds-v0\.tmp4ImQcE\lib\site-packages\setuptools\build_meta.py", line 317, in run_setup
          exec(code, locals())
        File "<string>", line 8, in <module>
      ModuleNotFoundError: No module named 'torch'

      hint: This error likely indicates that `torch-cluster@1.6.3` depends on `torch`, but doesn't declare it as a build dependency. If `torch-cluster` is a first-party package, consider adding `torch` to its `build-system.requires`. Otherwise, either add it to your `pyproject.toml` under:

      [tool.uv.extra-build-dependencies]
      torch-cluster = ["torch"]

      or `uv pip install torch` into the environment and re-run with `--no-build-isolation`.
  help: If you want to add the package regardless of the failed resolution, provide the `--frozen` flag to skip locking and syncing.

解決策としてはまずtorchを入れて、そのあと--no-build-isolationを付けて追加していく必要がある。

参考記事

  • windows + python 3.8でインストールしている例
  • ↓--no-build-isolationのあたりの話。uvを使ってインストールしている

参考程度にpyproject.toml

[project]
name = "torch_points3d"
version = "0.2.0"
description = "Point Cloud Deep Learning Extension Library for PyTorch"
readme = "README.md"
authors = [
    { name = "Thomas Chaton", email = "thomas.chaton.ai@gmail.com" },
    { name = "Nicolas Chaulet", email = "nicolas.chaulet@gmail.com" },
]
requires-python = ">=3.10"
dependencies = [
    "matplotlib>=3.1",
    "hydra-core~=1.0.0",
    "wandb>=0.8.18",
    "tqdm>=4.40",
    "torchnet>=0.0.4",
    "tensorboard>=2.1",
    "torch-geometric>=1.7.1",
    "pytorch-metric-learning>=0.9.87.dev0",
    "scikit-image>=0.16.2",
    "numba>=0.50.0",
    "plyfile>=0.7.2",
    "gdown>=3.12.0",
    "types-six>=0.1.6",
    "types-requests>=0.1.11",
    "laspy>=2.0.3",
    "torch>=1.1.0",
    "hatchling>=1.27.0",
    "editables>=0.5",
    "torch-points-kernels>=0.6.10",
    "torch-scatter>=2.1.2",
    "torch-sparse>=0.6.18",
    "torch-cluster>=1.6.3",
    "pandas>=2.3.3",
]

[project.optional-dependencies]
dev = [
    "pylint>=2.4",
    "autopep8>=1.4",
    "flake8>=3.7",
    "rope>=0.14.0",
    "pre-commit>=1.21.0",
    "black>=19.10b0",
    "jupyterlab>=1.2.6",
    "pyvista>=0.23.1",
    "panel>=0.8.0",
    "param>=1.9.3",
    "codecov>=2.0.16",
    "gpustat>=0.6.0",
    "snakeviz>=2.0.1",
    "sphinx-rtd-theme>=0.4.3",
    "sphinx>=2.4.4",
    "sphinx-autobuild>=0.7.1",
    "mypy>=0.770",
    "ipywidgets>=7.5.1",
]

[tool.black]
line-length = 120

[build-system]
requires = ["hatchling>=1.0.0"]
build-backend = "hatchling.build"

独自プラットフォーム構築中です

仕事で取り組んでいる内容が、燈株式会社の公式Tech blogで公開されました!

当ブログの管理人は主に独自プラットフォームの構築を行なっております。

技術的に非常に面白い内容となっているため、ぜひご覧ください!!!!

tech.akariinc.co.jp


※:引用元記事の著者、学習の実装を書いた人は当ブログ管理人とは別人です.

独自プラットフォーム上のレンダリング結果

( https://tech.akariinc.co.jp/entry/2025/07/31/180000 より引用)

C# 基礎編 - その1

バイトでC#を勉強しているのでまとめ用に書く

工事中&期間限定公開

プログラム以前

mainメソッド

C++等で最初に実行される関数を表すmain()関数は、C#ではクラス内のメソッドとして定義される

// 最小構成のプログラム
class Test {
    static void Main() {
         Console.WriteLine("Hello, World");
    }
}


コメント

/*コメント~~~*/
/*  
スラッシュとスターに囲まれた部分は、
複数行でも
コメントになります。
*/

int a = 0; // スラッシュを二個重ねると、それ以降の文末までがコメントになる。

標準出力

Console.WriteやConsole.WriteLine関数で行う

Console.Write(""改行なしで標準出力");  
Console.WriteLine("改行ありで標準出力");

// 変数の出力
const string str = "hoge";
const int  myint = 114;
Console.Write(str);
Console.Write("str の中身は" + str + "です");

// フォーマット
Console.WriteLine("str の中身は{0}です", str);
Console.WriteLine("str の中身は{0}で, myintの中身は{1}です", str, myint);
Console.Write($"str の中身は{str}です");




予約語

docs.microsoft.com

abstract      as     base          bool     break     byte     case     catch     char     checked     class
const     continue     decimal     default     delegate     do     double     else     enum     
event     explicit     extern     false     finally     fixed     float     for     foreach     goto
if      implicit     in     int     interface     internal     is     lock     long     namespace
new      null     object     operator     out     override     params     private     protected
public     readonly     ref     return     sbyte     sealed     short     sizeof     stackalloc
static     string     struct     switch     this     throw     true     try
typeof    uint     ulong     unchecked      unsafe
ushort  using  virtual   void   volatile  while




変数の型

ユーザー定義の型(classやenum等)を除くと以下の様なものがある(代表例)

sbyte System.SByte 符号付き8bit整数
byte System.Byte 符号なし8bit整数
short System.Int16 符号付き16bit整数
ushort System.UInt16 符号なし16bit整数
int System.Int32 符号付き32bit整数
uint System.UInt32 符号なし32bit整数
long System.Int64 符号付き64bit整数
ulong System.UInt64 符号なし64bit整数
char System.Char 文字
float System.Single 小数(単精度)
double System.Double 小数(倍精度)
bool System.Boolean ブール値(false、true)
decimal System.Decimal 10進数
string System.String 文字列
//宣言  :   型 変数名;
int seisu;

// 宣言 + 初期化    :   型 変数名 = 初期値
int seisu = 123:

ルール

  • `const` を型の前に着けると、コンパイル時に値が埋め込む定数となる
    • 宣言時に初期化しなければならない
    • またその値はコンパイル時に決定する必要がある。
  • readonlyを付けると、宣言時の初期化以降、読み取り専用の変数となる
    • 宣言時に初期化しなければならない
    • 初期化値は実行時まで分からなくても良い
  • var 変数名 = 初期化値;と書くと、型推論してくれる。

例:

string str = "文字列型";
int    seisuu = 10; // 整数型
uint   unsigned_int = 10; // 非負整数
bool   bool_value = true; // true or falseの真偽値
float  float_value = 3.14f; // 不動点単精度少数
double double_value = 3.14d; // 不動点倍精度少数
var annmoku = 10; // 暗黙的型付け

四則演算

//---------------------------四則演算---------------------------------
int hoge = 0;
hoge = 10   +   20; Console.WriteLine($"10   +   20 = {hoge}");
hoge = 10   -   20; Console.WriteLine($"10   -   20 = {hoge}");
hoge = 10   *   20; Console.WriteLine($"10   *   20 = {hoge}");
hoge = 10   /   20; Console.WriteLine($"10   /   20 = {hoge}");
double hoge2 = 10.0 / 20.0; Console.WriteLine($"10.0 / 20.0 = {hoge2}");;
hoge = 30   %    8; Console.WriteLine($"30   / 8  = {hoge}");// 余り出力

条件分岐

if文

Random r = new Random();
int RandomValue = r.Next(100); // 0 ~ 99 の乱数作成

if ( RandomValue < 30) {
    Console.WriteLine("RandomValue  < 30");
}else if(RandomValue < 50) {
    Console.WriteLine("30 <= RandomValue  < 50");
}else if(RandomValue < 70) {
    Console.WriteLine("50 <= RandomValue  < 70");
}else{
    Console.WriteLine("70 <= RandomValue ");  
}


演算子

a a はbより小さい
a>b aはbより大きい
a<=b a はb以下
a>=b aはb以上
a == b aとbは等しい
a != b aはbではない

for文

for(int i = 0; i< 10; i++){
    Console.Write(i);
}

// リストに対するfor文
var arr = new ArrayList(){"list 1", "list 2", "list 3", "list 4"};
foreach(string t in arr){
     Console.WriteLine(t);
}

// 辞書に対するfor文
var myDic = new Dictionary<string, int>(){
   {"a", 1}, {"b", 2}, {"c", 3}
};

foreach(KeyValuePair<string, int> t in myDic){
  Console.WriteLine($"key = {t.Key} => {t.Value}");
}

while

int iii= 0;
while(iii< 3){Console.Write(iii); iii++;}

switch

Random rd = new Random();
int  i= rd.Next(10);

switch(i){
    case 1: 
         Console.Write("i = 0");
        break;
    case 2: 
        Console.Write("i = 2");
        break;
    default:
         Console.Write("それ以外");
          break;
}

配列/リスト

// 書き方その1(固定長)
// 型名[] 変数名 = new 型名[] { 値1, 値2, ...};
string[] mylist = new string[4]{"hoge", "hjuga", "a", "aa"};
Console.WriteLine(mylist[0]);
// 書き方その2(可変長、要Cast)
// ArrayListはObject型としてデータを代入する ( = どんな型でも代入できる)
// なので、型判定は実行時に行われる。また、値を取り出すときにはCastする必要がある(Visual BasicではOption StrictをOnにした場合)
ArrayList mylist2 = new ArrayList(){
    "list 1", "list 2", "list 3", "list 4"
};
mylist2.RemoveAt(0);  // 0番目を削除
mylist2.Add("aaaaaaaaa"); // リストの最後尾に「aaaaaaaa」を加える
mylist2.Add(5); // 0~4番目の要素は文字列型だが、ArrayList なので数値を入れることもできる
Console.WriteLine(mylist2[0]);

string moji = mylist[0]; // コンパイルエラーになるかも?
string moji = (string)mylist[0]; // string型にキャストする必要がある
// 書き方その3(可変長、固定型)
// ArrayListは特定の型としてリストを作るので、それぞれの要素にはその型しか代入できない
// 取り出すとき等に型変換は必要ない

// string型で、mylist3 という変数名のList
List<string> mylist3 = new List<string>(){
    "a", "b", "c"
};
// mylist3.Add(5);// string型のリストにintを入れようとしているのでビルドエラー
mylist3.Add("aaaa");// これはOK


使い方
mylistという名前のArrayList またはListがあったとすると、

  • mylist[n] でmylistのn+1番目の要素にアクセスできる
    • 代入も可能

プロパティ(クラス内変数):

mylist.Capacity 確保している容量
mylist.Count 配列の長さ

メソッド(関数)の代表的なもの:

mylist.Add(object) objectをmylistの末尾に挿入
Insert(n, object) objectをmylistのn番目(n = int32型)に挿入
mylist.IndexOf(object) mylist中でobjectを検索し、最初に見つかったオブジェクトのインデックス (0 から始まる) を返す
mylist.Contains(object) mylist中にobjectがあればtrueを返し、ない場合はfalseを返す
mylist.Remove(object) mylist中にobjectがあれば、最初に出現したものを削除
mylist.RemoveAt(n) n+1番目の要素を削除
mylist.RemoveAt(a, b) aからbで指定された範囲の要素を削除
mylist.Clear() 全削除。Countは0になるがCapacityは0になるとは限らない
mylist.Sort() ソート

それ以外は↓に載っている
docs.microsoft.com

docs.microsoft.com

List / ArrayListでfor文

var arr = new ArrayList(){"list 1", "list 2", "list 3", "list 4"};
foreach(string t in arr){
    Console.WriteLine(t);
}

辞書

Dictionary<string, int> myDic = new Dictionary<string, int>(){
    {"a", 1}, {"b", 2}, {"c", 3}
};
Console.WriteLine(myDic["a"]);
myDic["a"] = 100;  // 代入
myDic["d"] = 100; // 新しく key-valueを作成して代入
myDic.Remove("b"); // {b : 2} のkey-valueを削除
Console.WriteLine(myDic["a"]);


// 因みに、辞書のなかの値を全て出力するには、

// 例1
foreach(KeyValuePair<string, int> t in myDic){
    Console.WriteLine($"key = {t.Key} => {t.Value}");
}

// 例2
foreach(string key in myDic.keys){
    Console.WriteLine($"{key}に相当する値は{mDic[key]}です");
}

その他:
docs.microsoft.com


関数