當前位置: 首頁>>技術教程>>正文


python – ImportError:無法導入名稱’_validate_lengths’

問題描述

學習Tensorflow遇到以下問題(我使用Pycharm,環境是Ubuntu 16.04)。我不知道此錯誤的原因。

from numpy.lib.arraypad import _validate_lengths

ImportError: cannot import name ‘_validate_lengths’

出錯的代碼如下,需要提示來解決此錯誤。謝謝。

import tensorflow as tf
from skimage import transform
from skimage import data
import matplotlib.pyplot as plt
import os
import numpy as np
from skimage.color import rgb2gray
import random

#listdir: This method returns a list containing the names of the entries in the directory given by path.
# Return True if path is an existing directory

def load_data(data_dir):
    # Get all subdirectories of data_dir. Each represents a label.
    directories = [d for d in os.listdir(data_dir)
                   if os.path.isdir(os.path.join(data_dir, d))]
    # Loop through the label directories and collect the data in
    # two lists, labels and images.
    labels = []
    images = []
    for d in directories:
        label_dir = os.path.join(data_dir, d)
        file_names = [os.path.join(label_dir, f)
                      for f in os.listdir(label_dir)
                      if f.endswith(".ppm")]
        for f in file_names:
            images.append(data.imread(f))
            labels.append(int(d))
    return images, labels


ROOT_PATH = "/home/tahir/PhD Study/Traffic Signs Using Tensorflow/"
train_data_dir = os.path.join(ROOT_PATH, "TrafficSigns/Training")
test_data_dir = os.path.join(ROOT_PATH, "TrafficSigns/Testing")

images, labels = load_data(train_data_dir)

# Print the `images` dimensions
print(images.ndim)

# Print the number of `images`'s elements
print(images.size)

# Print the first instance of `images`
images[0]

 

最佳思路

我更新了skimage軟件包。

pip install --upgrade scikit-image

問題解決了。這是Skimage版本的問題,已在0.14.2中解決。 另外,此版本非常穩定。

    Installing collected packages: dask, scikit-image
  Found existing installation: dask 0.19.1
    Uninstalling dask-0.19.1:
      Successfully uninstalled dask-0.19.1
  Found existing installation: scikit-image 0.13.0
    Uninstalling scikit-image-0.13.0:
      Successfully uninstalled scikit-image-0.13.0
Successfully installed dask-1.0.0 scikit-image-0.14.2

 

參考資料

 

本文由《純淨天空》出品。文章地址: https://vimsky.com/zh-tw/article/4306.html,未經允許,請勿轉載。