简体   繁体   中英

AttributeError: 'Tensor' object has no attribute 'numpy

I try to using autokeras for image classification model when I fit my data this error happen

DATADIR = r"C:\Users\angesu\Desktop\DOCUMENT_DATA"

CATEGORIES = ["resume","transcript","certificate"]
IMG_SIZE = 250

for category in CATEGORIES :
    path = os.path.join(DATADIR, category)
    for img in os.listdir(path):
        img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)

training_data = []
def create_training_data():
    for category in CATEGORIES :
        path = os.path.join(DATADIR, category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try :
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
                training_data.append([new_array, class_num])
            except Exception as e:
                pass
create_training_data()
random.shuffle(training_data)

X = [] #features
y = [] #labels
for features, label in training_data:
    X.append(features)
    y.append(label)

X = np.asarray(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

clf = ak.ImageClassifier(max_trials=10)
clf.fit(X,y,validation_split=0.1)

def prepare(file):
    IMG_SIZE = 250
    img_array = cv2.imread(file, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

path = r"C:\Users\angesu\Downloads\Garn-Certificate-4.jpg"

predicted_y = clf.predict(prepare(path))

print(clf.evaluate(test_set))

I don't know how to fix this problem I'm trying to use tf.data.Dataset.from_tensor_slices() but this error still show I'm google this error but I don't understand anything The error is in clf.fit(X,y,validation_split=0.1)

Error message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-b327f1f23808> in <module>
      1 clf = ak.ImageClassifier(max_trials=10)
      2 # Feed the tensorflow Dataset to the classifier.
----> 3 clf.fit(X,Y,validation_split=0.1)
      4 # Predict with the best model.
      5 

C:\ProgramData\Anaconda3\lib\site-packages\autokeras\task.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
    114                     validation_split=validation_split,
    115                     validation_data=validation_data,
--> 116                     **kwargs)
    117 
    118 

C:\ProgramData\Anaconda3\lib\site-packages\autokeras\auto_model.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
    156             y=y,
    157             validation_data=validation_data,
--> 158             validation_split=validation_split)
    159 
    160         # Initialize the hyper_graph.

C:\ProgramData\Anaconda3\lib\site-packages\autokeras\auto_model.py in _prepare_data(self, x, y, validation_data, validation_split)
    259         if validation_data is None and validation_split:
    260             self._split_dataset = True
--> 261             dataset, validation_data = utils.split_dataset(dataset, validation_split)
    262         return dataset, validation_data
    263 

C:\ProgramData\Anaconda3\lib\site-packages\autokeras\utils.py in split_dataset(dataset, validation_split)
     64         A tuple of two tf.data.Dataset. The training set and the validation set.
     65     """
---> 66     num_instances = dataset.reduce(np.int64(0), lambda x, _: x + 1).numpy()
     67     if num_instances < 2:
     68         raise ValueError('The dataset should at least contain 2 '

AttributeError: 'Tensor' object has no attribute 'numpy'

autokeras.task.ImageClassifier.fit expects that both training data are either numpy.ndarray or tensorflow.Dataset .

y is currently a list. Convert it to a numpy array

Y = np.asarray(y)

clf.fit(X, Y, validation_split=0.1)

A discrepancy between the example for tf.data.Dataset.reduce documentation in Tensorflow v1.15.0 and v2.0.0 reflects that reduce function returns a tensorflow.python.framework.ops.Tensor instance and not a tensorflow.python.framework.ops.EagerTensor instance for the earlier version.

autokeras.utils.split_dataset expects the reduced Dataset to be an EagerTensor instance as it invokes numpy function on it.

I suggest upgrading your tensorflow version to 2.0.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM