Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 TensorFlow get error - array with more than one element is ambiguous
#1



The experiment is carried out on Windows 10 Pro Intel ® Core ™ i5-4590 CPU @ 3.3 GHz, based on the platform of Anaconda with Spyder Python 3.7.150, it is programming through the Python language and Python library function.

I get the error message:



File "C:/Users/HSIPL/Desktop/DNN.py", line 244, in if(pred_img[0]<0.5):

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()



How can I solve the problem ---




# 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)







Please help and thank you





Quote
#2
pred_img[0] is not a single value as you expect it to be, it´s an array.
So look into how pred_img is created.
Quote
#3


May I know how to modify the coding ---




Quote
#4
insert a
print(pred_img, type(pred_img))
to see what pred_img is.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TensorFlow CNN get error - DataType uint8 not in list vokoyo 3 282 Nov-09-2019, 04:03 AM
Last Post: xuanloc97
  ValueError: The truth value of a Series is ambiguous ? firebird 1 805 Aug-01-2019, 12:33 AM
Last Post: scidam
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 716 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  python odeint keeps giving me size of array error kiyoshi7 1 794 Nov-01-2018, 02:03 AM
Last Post: j.crater
  Newbie at using python and tensorflow getting error when running simple code FeatherineAu 0 1,655 Sep-28-2018, 02:09 PM
Last Post: FeatherineAu
  Python import tensorflow error owenhe 7 2,198 Jun-23-2018, 12:41 PM
Last Post: gontajones
  Convert element of list to integer(Two dimentional array) zorro_phu 3 888 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 10,018 Apr-02-2018, 12:03 AM
Last Post: scidam
  error in script "Expected 2D array, got 1D array instead:" drogontargaryen 0 13,517 Aug-07-2017, 04:32 PM
Last Post: drogontargaryen

Forum Jump:


Users browsing this thread: 1 Guest(s)