【问题标题】:TensorFlow get error - the truth value of an array with more than one element is ambiguousTensorFlow 出错 - 具有多个元素的数组的真值不明确
【发布时间】:2019-11-08 01:01:44
【问题描述】:

实验在Windows 10 Pro Intel(R) Core(TM) i5-4590 CPU @ 3.3 GHz上进行,基于Anaconda平台和Spyder Python 3.7.150,通过Python语言和Python编程库函数。

我收到错误消息:

文件“C:/Users/HSIPL/Desktop/DNN.py”,第 244 行,在 if(pred_img[0]

ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

我该如何解决这个问题?

# Importing Libraries
from matplotlib import pyplot as plt
from tensorflow.keras.preprocessing.image import array_to_img, img_to_array, load_img
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.image as mpimg
import numpy as np
import os

# Preparing Dataset
# Setting names of the directies for both sets
base_dir = 'data'
seta ='One'
setb ='Two'

# Each of the sets has three sub directories train, validation and test
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')

def prepare_data(base_dir, seta, setb):
# Take the directory names for the base directory and both the sets 
# Returns the paths for train, validation for each of the sets
    seta_train_dir = os.path.join(train_dir, seta)
    setb_train_dir = os.path.join(train_dir, setb)

    seta_valid_dir = os.path.join(validation_dir, seta)
    setb_valid_dir = os.path.join(validation_dir, setb)

    seta_train_fnames = os.listdir(seta_train_dir)
    setb_train_fnames = os.listdir(setb_train_dir)

    return seta_train_dir, setb_train_dir, seta_valid_dir, setb_valid_dir, seta_train_fnames, setb_train_fnames

seta_train_dir, setb_train_dir, seta_valid_dir, setb_valid_dir, seta_train_fnames, setb_train_fnames = prepare_data(base_dir, seta, setb)

seta_test_dir = os.path.join(test_dir, seta)
setb_test_dir = os.path.join(test_dir, setb)
test_fnames_seta = os.listdir(seta_test_dir)
test_fnames_setb = os.listdir(setb_test_dir)

datagen = ImageDataGenerator( 
          height_shift_range = 0.2,
          width_shift_range = 0.2,
          rotation_range = 40,
          shear_range = 0.2,
          zoom_range = 0.2,
          horizontal_flip = True,
          fill_mode = 'nearest')

img_path = os.path.join(seta_train_dir, seta_train_fnames[3])
img = load_img(img_path, target_size = (150, 150))
x = img_to_array(img)
x = x.reshape((1,) + x.shape)

i = 0
for batch in datagen.flow(x, batch_size = 1):
    plt.figure(i)
    imgplot = plt.imshow(array_to_img(batch[0]))
    i += 1
    if i % 5 == 0:
        break

# Convolutional Neural Network Model
# Import TensorFlow Libraries
from tensorflow.keras import layers
from tensorflow.keras import Model       

img_input = layers.Input(shape = (150, 150, 3))        
x = layers.Flatten()( img_input )
x = layers.Dense(512, activation = 'relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(512, activation = 'relu')(x)
x = layers.Dropout(0.2)(x)
x = layers.Dense(256, activation = 'relu')(x)
x = layers.Dropout(0.2)(x)
output = layers.Dense(15, activation = 'softmax')(x)

model = Model(img_input, output)

model.summary()

import tensorflow as tf
# Using binary_crossentropy as the loss function and
# Adam Optimizer as the optimizing function when training
model.compile(loss = 'sparse_categorical_crossentropy',
              optimizer = tf.optimizers.Adam(learning_rate = 0.0005),
              metrics = ['acc'])
from tensorflow.keras.preprocessing.image import ImageDataGenerator            

# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)

# Flow training images in batches of 20 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
                  train_dir,
                  target_size = (150, 150),
                  batch_size = 20,
                  class_mode = 'binary')

validation_generator = test_datagen.flow_from_directory(
                       validation_dir,
                       target_size = (150, 150),
                       batch_size = 20,
                       class_mode = 'binary')

# 4x4 grid
ncols = 5
nrows = 5

pic_index = 0

# Set up matpotlib fig and size it to fit 5x5 pics
fig = plt.gcf()
fig.set_size_inches(ncols * 5, nrows * 5)

pic_index += 10
next_seta_pix = [os.path.join(seta_train_dir, fname)
                 for fname in seta_train_fnames[pic_index - 10:pic_index]]
next_setb_pix = [os.path.join(setb_train_dir, fname)
                 for fname in setb_train_fnames[pic_index - 10:pic_index]]

for i, img_path in enumerate(next_seta_pix + next_setb_pix):
# Set up subplot; subplot indices start at 1
    sp = plt.subplot(nrows, ncols, i + 1)
    sp.axis('Off')

    img = mpimg.imread(img_path)
    plt.imshow(img)

plt.show()

# Train the model
mymodel = model.fit_generator(
          train_generator,
          steps_per_epoch = 10,
          epochs = 80,
          validation_data = validation_generator,
          validation_steps = 7,
          verbose = 2)

import random
from tensorflow.keras.preprocessing.image import img_to_array, load_img

successive_outputs = [layer.output for layer in model.layers[1:]]
visualization_model = Model(img_input, successive_outputs)

a_img_files = [os.path.join(seta_train_dir, f) for f in seta_train_fnames]
b_img_files = [os.path.join(setb_train_dir, f) for f in setb_train_fnames]
img_path = random.choice(a_img_files + b_img_files)

img = load_img(img_path, target_size = (150, 150))
x = img_to_array(img)
x = x.reshape((1,) + x.shape)

x /= 255

successive_feature_maps = visualization_model.predict(x)

layer_names = [layer.name for layer in model.layers]

for layer_name, feature_map in zip(layer_names, successive_feature_maps):
    if len(feature_map.shape) == 4:
# Just do this for the conv/maxpool layers
        n_features = feature_map.shape[-1]
# The feature map has shape(1, size,size, n_features)
        size = feature_map.shape[1]
# Will tile images in this matrix
        display_grid = np.zeros((size, size * n_features))
        for i in range(n_features):
# Postprocess the feature           
            x = feature_map[0, :, :, i]
            x -= x.mean()

            x *= 64
            x += 128
            x = np.clip(x, 0, 255).astype('float32')
# Will tile each filter into this big horizontal grid
            display_grid[:, i * size : (i + 1) * size] = x 

# Accuracy results for each training and validation epoch
acc = mymodel.history['acc']
val_acc = mymodel.history['val_acc']

# Loss Results for each training and validation epoch
loss = mymodel.history['loss']
val_loss = mymodel.history['val_loss']

epochs = range(len(acc))

# Plot accuracy for each training and validation epoch
plt.plot(epochs, acc)
plt.plot(epochs, val_acc)
plt.title('Training and validation accuracy')

plt.figure()

# Plot loss for each training and validation epoch
plt.plot(epochs, loss)
plt.plot(epochs, val_loss)
plt.title('Training and validation loss')

plt.figure()

# Testing model on a random train image from set a

train_img = random.choice(seta_train_fnames)
train_image_path = os.path.join(seta_train_dir, train_img)
train_img = load_img(train_image_path, target_size = (150, 150))
plt.imshow(train_img)
train_img = (np.expand_dims(train_img, 0))
train_img = tf.cast(train_img, tf.float32)
print(train_img.shape)

model.predict(train_img)

# Testing model on a random train image from set b

train_img = random.choice(setb_train_fnames)
train_image_path = os.path.join(setb_train_dir, train_img)
train_img = load_img(train_image_path, target_size = (150, 150))
plt.imshow(train_img)
train_img = (np.expand_dims(train_img, 0))
train_img = tf.cast(train_img, tf.float32)
print(train_img.shape)

model.predict(train_img)

# Testing a random image from the test set a 

cal_o = 0
cal_t = 0
cal_unconclusive = 0
alist = []
for fname in test_fnames_seta:
    if fname.startswith('.'):
        continue
    file_path = os.path.join(seta_test_dir, fname)
    load_file = load_img(file_path, target_size = (150, 150))
    load_file = (np.expand_dims(load_file, 0))
    load_file = tf.cast(load_file, tf.float32)
    pred_img = model.predict(load_file)
    if(pred_img[0]<0.5):
        cal_o+=1
    elif(pred_img[0]>0.5):
        cal_t+=1
    else:
        print(pred_img[0], "\n")
        cal_unconclusive+=1
        alist.append(file_path)
print(alist)

print("Identified as:")
print("One:", cal_o)
print("Two:", cal_t)
print( "Inconclusive:", cal_unconclusive)
print( "Percentage:", (cal_o/(cal_o + cal_t + cal_unconclusive)) * 100)
a =  (cal_o/(cal_o + cal_t + cal_unconclusive)) * 100

# Testing a random image from the test set b

cal_o = 0
cal_t = 0
cal_unconclusive = 0
alist = []
for fname in test_fnames_setb:
    if fname.startswith('.'):
        continue
    file_path = os.path.join(setb_test_dir, fname)
    load_file = load_img(file_path, target_size = (150, 150))
    load_file = (np.expand_dims(load_file, 0))
    load_file = tf.cast(load_file, tf.float32)
    pred_img = model.predict(load_file)
    if(pred_img[0]<0.5):
        cal_o+=1
    elif(pred_img[0]>0.5):
        cal_t+=1
    else:
        print(pred_img[0], "\n")
        cal_unconclusive+=1
        alist.append(file_path)
print(alist)

print("Identified as:")
print("One:", cal_o)
print("Two:", cal_t)
print( "Inconclusive:", cal_unconclusive)
print( "Percentage:", (cal_t/(cal_o + cal_t + cal_unconclusive)) * 100)
b =  (cal_t/(cal_o + cal_t + cal_unconclusive)) * 100             

avg = (a+b)/2
print("\nAverage Percentage:", avg)

请帮忙,谢谢---

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    错误是因为 pred_img[0] 不是单个值。您正在将其与单个值 0.5 进行比较。

    在第 243 行添加以下行并检查预测图像,然后在下一行使用 if 循环比较相应的值。

    print(type(pred_img))
    print(pred_img)
    

    【讨论】:

      猜你喜欢
      • 2015-01-06
      • 2019-08-21
      • 2019-03-19
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      相关资源
      最近更新 更多