tensorflow `错误:传递给'expanddims'的参数'x'必须是Tensor或类Tensor,但得到了'tensor' `

92vpleto  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(111)

我正在使用tensorflow进行项目扫描图像应用程序。我在TensorFlow上的图像大小有问题。我使用世博会和世博相机也为模型培训我使用可教学的机器。在编码路径中,一切都很好,但这行代码不工作并显示错误:
第一个月
下面是我的代码:

import * as tf from '@tensorflow/tfjs';
import {
  bundleResourceIO,
  cameraWithTensors,
} from '@tensorflow/tfjs-react-native';
import { Camera } from 'expo-camera';
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useRef, useState } from 'react';
import { Dimensions, StyleSheet, Text, View } from 'react-native';

const TensorCamera = cameraWithTensors(Camera);

// The size of camera preview.
//
// These are only for iOS devices.
const CAM_PREVIEW_WIDTH = Dimensions.get('window').width;
const CAM_PREVIEW_HEIGHT = CAM_PREVIEW_WIDTH / (9 / 16);

// The size of the output tensor (image) from TensorCamera.
//
// 9/16.
const OUTPUT_TENSOR_WIDTH = 224;
const OUTPUT_TENSOR_HEIGHT = 480;

export default function App() {
  const [tfReady, setTfReady] = useState(false);
  const [model, setModel] = useState();
  const [isHotdog, setIsHotdog] = useState(null);

  const rafId = useRef(null);

  // Make sure tfjs and tfjs-react-native work, especially the tensor camera.
  useEffect(() => {
    async function prepare() {
      rafId.current = null;

      // Request camera permission.
      await Camera.requestCameraPermissionsAsync();

      // Wait for tfjs to initialize the backend.
      await tf.ready();

      // Load model.
      const modelJson = require('./model/model.json');
      const modelWeights = require('./model/weights.bin');
      
      const model = await tf.loadLayersModel(
        bundleResourceIO(modelJson, modelWeights)
      );
      setModel(model);
      //console.log(model)
      

      // Ready!!
      setTfReady(true);
    }

    prepare();
  }, []);

  // This will be called when the component in unmounted.
  useEffect(() => {
    return () => {
      if (rafId.current != null && rafId.current !== 0) {
        cancelAnimationFrame(rafId.current);
        rafId.current = 0;
      }
    };
  }, []);

  // Handler that will be called when TensorCamera is ready.
  const handleCameraStream = (images, updatePreview, gl) => {
    console.log('camera ready!');
    // Here, we want to get the tensor from each frame (image), and feed the
    // tensor to the model (which we will train separately).
    //
    // We will do this repeatly in a animation loop.
    const loop = () => {
      // This might not be necessary, but add it here just in case.
      if (rafId.current === 0) {
        return;
      }

      // Wrap this inside tf.tidy to release tensor memory automatically.
      tf.tidy(() => {
        console.log('here is fine');
       
        const imageTensor = images.next().value.expandDims(0).div(127.5).sub(1);
        console.log('here');

        
        const f =
          (OUTPUT_TENSOR_HEIGHT - OUTPUT_TENSOR_WIDTH) /
          2 /
          OUTPUT_TENSOR_HEIGHT;
        const cropped = tf.image.cropAndResize(
          // Image tensor.
          imageTensor,
          
          tf.tensor2d([f, 0, 1 - f, 1], [1, 4]),
         
          [0],
         
          [224, 224]
        );

        // Feed the processed tensor to the model and get result tensor(s).
        const result = model.predict(cropped);
        console.log('5');
        // Get the actual data (an array in this case) from the result tensor.
        const logits = result.dataSync();
        console.log(logits);
        // Logits should be the probability of two classes (hot dog, not hot dog).
        if (logits) {
          setIsHotdog(logits[0] > logits[1]);
        } else {
          setIsHotdog(null);
        }
      });

      rafId.current = requestAnimationFrame(loop);
    };

    loop();
  };

  if (!tfReady) {
    return (
      <View style={styles.loadingMsg}>
        <Text>Loading...</Text>
      </View>
    );
  } else {
    return (
      <View style={styles.container}>
        <TensorCamera
          style={styles.camera}
          autorender={true}
          type={Camera.Constants.Type.back}
          // Output tensor related props.
          // These decide the shape of output tensor from the camera.
          resizeWidth={OUTPUT_TENSOR_WIDTH}
          resizeHeight={OUTPUT_TENSOR_HEIGHT}
          resizeDepth={3}
          onReady={handleCameraStream}
        />
        <View
          style={
            isHotdog
              ? styles.resultContainerHotdog
              : styles.resultContainerNotHotdog
          }
        >
          <Text style={styles.resultText}>
            {isHotdog ? 'hotdog' : 'nothotdog'}
          </Text>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    position: 'relative',
    width: CAM_PREVIEW_WIDTH,
    height: CAM_PREVIEW_HEIGHT,
    marginTop: Dimensions.get('window').height / 2 - CAM_PREVIEW_HEIGHT / 2,
  },
  // Tensor camera requires z-index.
  camera: {
    width: '100%',
    height: '100%',
    zIndex: 1,
  },
  loadingMsg: {
    position: 'absolute',
    width: '100%',
    height: '100%',
    alignItems: 'center',
    justifyContent: 'center',
  },
  resultContainerHotdog: {
    position: 'absolute',
    top: 0,
    left: 0,
    zIndex: 100,
    padding: 20,
    borderRadius: 8,
    backgroundColor: '#00aa00',
  },
  resultContainerNotHotdog: {
    position: 'absolute',
    top: 0,
    left: 0,
    zIndex: 100,
    padding: 20,
    borderRadius: 8,
    backgroundColor: '#aa0000',
  },
  resultText: {
    fontSize: 30,
    color: 'white',
  },
});

字符串

baubqpgj

baubqpgj1#

TensorFlow.js(从4.8.0开始)似乎存在直接从Tensor调用方法的问题,至少在React Native中是这样。尝试从tf对象调用这些方法,并将Tensor作为参数传入:

tf.sub(tf.div(tf.expandDims(images.next().value), tf.scalar(127.5)), tf.scalar(1))

字符串

  • 注意:不要忘记将标量传递为tf.Scalar s!*

相关问题