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


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

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

.image.cropAndResize() 函數用於從指定的輸入圖像張量中取出輸出,並通過雙線性采樣或最近鄰采樣將它們重新縮放到正常輸出維度,如裁剪長度所述。

用法:

tf.image.cropAndResize(image, boxes, boxInd, cropSize, 
method?, extrapolationValue?)

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

  • images:所述的 4d 張量,其配置為 [batch, imageHeight, imageWidth, depth]。其中,imageHeight 和 imageWidth 應為正數,定義要從中獲取裁剪的圖像組。它可以是 tf.Tensor4D、TypedArray 或 Array 類型。
  • boxes:所述 2d float32 張量,其配置為 [numBoxes, 4]。並且每次訪問都是[y1, x1, y2, x2],允許(y1, x1)和(y2, x2)是分組中boxInd[i]圖像中框的標準化坐標。它可以是 tf.Tensor2D、TypedArray 或 Array 類型。
  • boxInd:所述 1d int32 張量,其配置為 [numBoxes] 以及範圍 [0, batch) 中的值,它定義了 i-th 框指示的圖像。它是 tf.Tensor1D、TypedArray 或 Array 類型。
  • cropSize:它是規定的 1d int32 張量,它具有兩個元素,其配置為 [cropHeigh,cropWidth],定義了每個裁剪重新縮放到的長度。它的類型為 [number, number]。
  • method:它是一個可選參數,用於定義重新縮放的采樣方法。默認值是雙線性的。它可以是 ‘bilinear’ 或 ‘nearest’ 類型。
  • extrapolationValue:規定的閾值用於根據規定的分數得出何時刪除框的結論。默認值為零。它是可選的,類型為 number。

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



範例1:使用 4d 張量、boxes、boxInd 和cropSize 參數。

Javascript


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

輸出:

Tensor
     [ [ [[0, 0],]]]

範例2:使用浮點數組、方法以及外推值。

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]]
]];
  
// Defining boxes with an array of floats
const boxes = [[11.1, 2.3, 7.3, 6.4], [1, 4]];
  
// Calling image.cropAndResize() method and
// Printing output
tf.image.cropAndResize(arr, boxes, [2, 4], [2, 1], 'nearest', 0.4).print();

輸出:

Tensor
    [[ [[0, 0, 0, 0],],

       [[0, 0, 0, 0],]],


     [ [[0, 0, 0, 0],],

       [[0, 0, 0, 0],]]]

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




相關用法


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