當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.image.transform()用法及代碼示例

Tensorflow.js是Google開發的開放源代碼庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。

.image.transform() 函數用於將指定的變換應用於圖像。

用法:

tf.image.transform(image, transforms, interpolation?, fillMode?, 
fillValue?, outputShape?)

參數:此方法接受以下參數:

  • images:所述的 4d 張量,其配置為 [batch, imageHeight, imageWidth, depth]。它可以是 tf.Tensor4D、TypedArray 或 Array 類型。
  • transforms:所述的投影變換矩陣或矩陣。它是一個範圍為 8 的一維張量,或者維度為 N x 8 的張量。如果單行變換是 [a0, a1, a2, b0 b1, b2, c0, c1],隨後它會繪製輸出點,即(x, y) 變成修改後的輸入點即 (x', y') = ((a0 x + a1 y + a2) /k, (b0 x + b1 y + b2) /k),其中 k = c0 x + c1 y + 1。此外,與將輸入點繪製到輸出點的變換相比,變換是相反的。
  • interpolation:這是規定的插值模式。它可以是 ‘nearest’ 或 ‘bilinear’ 類型。它是可選的,默認值為 ‘nearest’。
  • fillMode:這裏,超出輸入邊界的所述點根據所述模式進行填充。模式是可選的,可以是 ‘constant’, ‘reflect’、‘wrap’ 或 ‘nearest’ 類型。默認值為 ‘constant’。在“反射”模式下,即 (d c b a | a b c d | d c b a ),通過在最大像素的邊周圍進行冥想來延長輸入。在“常量”模式下,即(k k k k | a b c d | k k k k),通過填充超出邊的每個值以及相同的常量值 k 來延長輸入。在'wrap'模式下,即(a b c d | a b c d | a b c d),輸入通過圍繞相對的邊被拉長。在“最近”模式下,即(a a a a | a b c d | d d d d),輸入被最近的像素拉長。
  • fillValue:如果聲明的 fillMode 為“常量”,則它是一個浮點數,用於描述要在邊之外填充的值。它是可選的,類型為 number。
  • outputShape:它是可選的,類型為 [number, number]。

返回值:它返回 tf.Tensor4D 對象。



範例1:使用 4d 張量和 2d 張量,即變換。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Calling image.transform() method and
// Printing output
tf.image.transform(tf.tensor4d([[
  
  [[4, 7], [21, 9]],
  
  [[8, 9], [1, 33]]
  
]]), tf.tensor2d([1, 2, 3, 4, 5, 6, 7, 8], [1, 8])).print();

輸出:

Tensor
    [[[[0, 0 ],
       [1, 33]],

      [[1, 33],
       [8, 9 ]]]]

範例2:使用浮點數、插值、fillMode、fillValue 和 outputShape 數組。

Javascript


// Importing the tensorflow.js library
const tf = require("@tensorflow/tfjs")
  
// Defining an array of floats
const arr = [[
  [[1.1, 1.7, 1.5, 1.1], 
  [1.7, 1.9, 8.1, 6.3]],
  [[3.3, 3.4, 3.7, 4.0], 
  [5.1, 5.2, 5.3, 5.9]]
]];
  
// Calling image.transform() method and
// Printing output
tf.image.transform(arr, tf.tensor2d(
    [1.3, 2.4, 3.5, 4.5, 5.1, 6.0, 7.2, 8.5], 
    [1, 8]),'bilinear', 'reflect', 2, [2, 2])
    .print();

輸出:

Tensor
    [[[[3.3      , 3.4000001, 3.7      , 4        ],
       [4.3536587, 4.4536586, 4.6365857, 5.112195 ]],

      [[4.4178948, 4.5178947, 4.6936841, 5.1800003],
       [3.8970597, 4.0186343, 4.3869019, 4.721858 ]]]]

參考:https://js.tensorflow.org/api/latest/#image.transform




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.image.transform() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。