【问题标题】:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).in jupyternotebookValueError:无法将 NumPy 数组转换为张量(不支持的对象类型 numpy.ndarray)。在 jupyternotebook
【发布时间】:2022-03-05 18:58:20
【问题描述】:

我用jupyter写代码

print('---------------Data Gathering------------------')
# put The address of test images
directory_location_test = "G:\\ml\\test"
data_test, labels_test = data_extraction(directory_location_test)
testX = np.array(data_test)
#testX = tf.convert_to_tensor(testX)
#testX = testX.astype('int32')

testY = labels_test

但在我的代码中显示

<ipython-input-5-9ba842610e7d>:60: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
  testX = np.array(data_test)

和错误: 请我解决

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    使用data_extraction() 加载的数据似乎不像弃用错误所说的那样具有统一大小:Creating an ndarray from ragged nested sequences

    输入数据可能沿某些轴具有不同的长度,例如。 data = [[1], [1,2]]

    如果不知道 `data_extraction()' 是什么,我真的无能为力。

    【讨论】:

      【解决方案2】:

      坦克

                  # image_resize
      #######################################################
      def image_resize(folder):
         
          images = []
          num_images = 0
          location = folder
          for filename in os.listdir(folder):
              img = cv2.imread(os.path.join(folder, filename))
              if img is not None:
                  new_img = np.array(Image.fromarray(img).resize((96,96), Image.ANTIALIAS))  # Resize the images to 50 X 50
                  images.append(new_img)
                  num_images += 1
                  cv2.imwrite("{}/{}".format(location, filename), new_img)
              print("_image:{0} complete".format(filename))
          return None
      
      
      print('____________________Start Program__________________________')
      directory_location_train = "G:\\ml\\train"
      for folder in os.listdir(directory_location_train):
          print("image_resize on folder: {0}".format(folder))
          image_resize(directory_location_train+"/"+folder) and the code -- is:
      ########################################## Define Functions ############################################################
      def data_extraction(directory):
          images = []
          label = []
          num_images = 0
          for folder in os.listdir(directory):
              print(folder)
              for filename in os.listdir("{}/{}".format(directory, folder)):
                  img = cv2.imread(os.path.join(directory, folder, filename))                   
                  if img is not None:
                      images.append(img)
                      label.append(folder)
                      num_images += 1
          le = LabelEncoder()
          label = le.fit_transform(label)
          return images, label
      #############################################
      def swish(x, beta = 2):
          return (x * sigmoid(beta * x))
      
      get_custom_objects().update({'swish': Activation(swish)})
      
      
      ######################################### Get train and test input #####################################################
      print('---------------Data Gathering------------------')
      # put The address of test images
      directory_location_test = "G:\\ml\\test"
      data_test, labels_test = data_extraction(directory_location_test)
      testX = np.array(data_test)
      testY = labels_test
      

      【讨论】:

        猜你喜欢
        • 2020-07-05
        • 2020-10-15
        • 1970-01-01
        • 2020-11-22
        • 2021-04-25
        • 2022-01-10
        • 2020-09-09
        • 2021-01-12
        • 2021-10-11
        相关资源
        最近更新 更多