Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 属性错误:';张量';对象没有属性'_凯拉斯#u形';_Python_Tensorflow_Keras - Fatal编程技术网

Python 属性错误:';张量';对象没有属性'_凯拉斯#u形';

Python 属性错误:';张量';对象没有属性'_凯拉斯#u形';,python,tensorflow,keras,Python,Tensorflow,Keras,我试图运行下面的代码生成一个JSON文件,并使用它构建一个带有一组图像的t-SNE。然而,我对Keras和机器学习的经验有限,我无法运行下面的代码并得到错误:AttributeError:“Tensor”对象没有属性“\u Keras\u shape” import argparse import sys import numpy as np import json import os from os.path import isfile, join import keras from kera

我试图运行下面的代码生成一个JSON文件,并使用它构建一个带有一组图像的t-SNE。然而,我对Keras和机器学习的经验有限,我无法运行下面的代码并得到错误:AttributeError:“Tensor”对象没有属性“\u Keras\u shape”

import argparse
import sys
import numpy as np
import json
import os
from os.path import isfile, join
import keras
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
from keras.models import Model
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from scipy.spatial import distance

def process_arguments(args):
    parser = argparse.ArgumentParser(description='tSNE on audio')
    parser.add_argument('--images_path', action='store', help='path to directory of images')
    parser.add_argument('--output_path', action='store', help='path to where to put output json file')
    parser.add_argument('--num_dimensions', action='store', default=2, help='dimensionality of t-SNE points (default 2)')
    parser.add_argument('--perplexity', action='store', default=30, help='perplexity of t-SNE (default 30)')
    parser.add_argument('--learning_rate', action='store', default=150, help='learning rate of t-SNE (default 150)')
    params = vars(parser.parse_args(args))
    return params

def get_image(path, input_shape):
    img = image.load_img(path, target_size=input_shape)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x

def find_candidate_images(images_path):
    """
    Finds all candidate images in the given folder and its sub-folders.
    Returns:
        images: a list of absolute paths to the discovered images.
    """
    images = []
    for root, dirs, files in os.walk(images_path):
        for name in files:
            file_path = os.path.abspath(os.path.join(root, name))
            if ((os.path.splitext(name)[1]).lower() in ['.jpg','.png','.jpeg']):
                images.append(file_path)
    return images

def analyze_images(images_path):
    # make feature_extractor
    model = keras.applications.VGG16(weights='imagenet', include_top=True)
    feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output)
    input_shape = model.input_shape[1:3]
    # get images
    candidate_images = find_candidate_images(images_path)
    # analyze images and grab activations
    activations = []
    images = []
    for idx,image_path in enumerate(candidate_images):
        file_path = join(images_path,image_path)
        img = get_image(file_path, input_shape);
        if img is not None:
            print("getting activations for %s %d/%d" % (image_path,idx,len(candidate_images)))
            acts = feat_extractor.predict(img)[0]
            activations.append(acts)
            images.append(image_path)
    # run PCA firt
    print("Running PCA on %d images..." % len(activations))
    features = np.array(activations)
    pca = PCA(n_components=300)
    pca.fit(features)
    pca_features = pca.transform(features)
    return images, pca_features

def run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate):
    images, pca_features = analyze_images(images_path)
    print("Running t-SNE on %d images..." % len(images))
    X = np.array(pca_features)
    tsne = TSNE(n_components=tsne_dimensions, learning_rate=tsne_learning_rate, perplexity=tsne_perplexity, verbose=2).fit_transform(X)
    # save data to json
    data = []
    for i,f in enumerate(images):
        point = [float((tsne[i,k] - np.min(tsne[:,k]))/(np.max(tsne[:,k]) - np.min(tsne[:,k]))) for k in range(tsne_dimensions) ]
        data.append({"path":os.path.abspath(join(images_path,images[i])), "point":point})
    with open(output_path, 'w') as outfile:
        json.dump(data, outfile)


if __name__ == '__main__':
    params = process_arguments(sys.argv[1:])
    images_path = params['images_path']
    output_path = params['output_path']
    tsne_dimensions = int(params['num_dimensions'])
    tsne_perplexity = int(params['perplexity'])
    tsne_learning_rate = int(params['learning_rate'])
    run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate)
    print("finished saving %s" % output_path)
发件人:
https://github.com/ml4a/ml4a-ofx/blob/master/scripts/tSNE-images.py

以下是我得到的:

    Traceback (most recent call last):
  File "tSNE-images.py", line 95, in <module>
    run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate)
  File "tSNE-images.py", line 75, in run_tsne
    images, pca_features = analyze_images(images_path)
  File "tSNE-images.py", line 50, in analyze_images
    feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output)
  File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 91, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 251, in _init_graph_network
    input_shapes=[x._keras_shape for x in self.inputs],
  File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 251, in <listcomp>
    input_shapes=[x._keras_shape for x in self.inputs],
AttributeError: 'Tensor' object has no attribute '_keras_shape'

然而,我似乎不知道如何使用Lambda更新代码。如何解决此错误?

我按照@user2300867的建议,使用以下内容更新了tensorflow:

pip3 install --upgrade tensorflow-gpu
并将keras更新为2.2.4

pip install Keras==2.2.4
我仍然有错误:

TypeError: expected str, bytes or os.PathLike object, not NoneType

但这很容易通过编辑本地路径的代码来解决

如果您可以在出现错误的地方进行修改,而不是发布完整的程序,那将是最好的选择。您能为您看到的异常包括堆栈跟踪吗?@jdehesa我已经用堆栈跟踪进行了更新。Thanks@user2300867升级Keras和Tensorflow,看看错误是否得到解决。
TypeError: expected str, bytes or os.PathLike object, not NoneType