Applying VGG16 for 10 images but get the value error - python-3.x

The proposed method can automatically detect the features of medical images under the condition determined by the algorithms, and achieve the correct and fast recognition results.
I was trying to run the image classification with using CNN method but then I got the error message below
File "<ipython-input-2-4e7ea6cc5087>", line 1, in <module>
runfile('C:/Users/MDIC/Desktop/VGG for 10 Images.py', wdir='C:/Users/MDIC/Desktop')
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/MDIC/Desktop/VGG for 10 Images.py", line 224, in <module>
sp = plt.subplot(nrows, ncols, i + 1)
File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 1084, in subplot
a = fig.add_subplot(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1367, in add_subplot
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_subplots.py", line 60, in __init__
).format(maxn=rows*cols, num=num))
ValueError: num must be 1 <= num <= 25, not 26
This is my Python code
# 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 ten sets
base_dir = 'data'
seta ='Man1'
setb ='Man2'
setc ='Man3'
setd ='Man4'
sete ='Man5'
setf ='Man6'
setg ='Man7'
seth ='Man8'
seti ='Man9'
setj ='Man10'
# 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, setc, setd, sete, setf, setg, seth, seti, setj):
# 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)
setc_train_dir = os.path.join(train_dir, setc)
setd_train_dir = os.path.join(train_dir, setd)
sete_train_dir = os.path.join(train_dir, sete)
setf_train_dir = os.path.join(train_dir, setf)
setg_train_dir = os.path.join(train_dir, setg)
seth_train_dir = os.path.join(train_dir, seth)
seti_train_dir = os.path.join(train_dir, seti)
setj_train_dir = os.path.join(train_dir, setj)
seta_valid_dir = os.path.join(validation_dir, seta)
setb_valid_dir = os.path.join(validation_dir, setb)
setc_valid_dir = os.path.join(validation_dir, setc)
setd_valid_dir = os.path.join(validation_dir, setd)
sete_valid_dir = os.path.join(validation_dir, sete)
setf_valid_dir = os.path.join(validation_dir, setf)
setg_valid_dir = os.path.join(validation_dir, setg)
seth_valid_dir = os.path.join(validation_dir, seth)
seti_valid_dir = os.path.join(validation_dir, seti)
setj_valid_dir = os.path.join(validation_dir, setj)
seta_train_fnames = os.listdir(seta_train_dir)
setb_train_fnames = os.listdir(setb_train_dir)
setc_train_fnames = os.listdir(setc_train_dir)
setd_train_fnames = os.listdir(setd_train_dir)
sete_train_fnames = os.listdir(sete_train_dir)
setf_train_fnames = os.listdir(setf_train_dir)
setg_train_fnames = os.listdir(setg_train_dir)
seth_train_fnames = os.listdir(seth_train_dir)
seti_train_fnames = os.listdir(seti_train_dir)
setj_train_fnames = os.listdir(setj_train_dir)
return seta_train_dir, setb_train_dir, setc_train_dir, setd_train_dir, sete_train_dir, setf_train_dir, setg_train_dir, seth_train_dir, seti_train_dir, setj_train_dir, seta_valid_dir, setb_valid_dir, setc_valid_dir, setd_valid_dir, sete_valid_dir, setf_valid_dir, setg_valid_dir, seth_valid_dir, seti_valid_dir, setj_valid_dir, seta_train_fnames, setb_train_fnames, setc_train_fnames, setd_train_fnames, sete_train_fnames, setf_train_fnames, setg_train_fnames, seth_train_fnames, seti_train_fnames, setj_train_fnames
seta_train_dir, setb_train_dir, setc_train_dir, setd_train_dir, sete_train_dir, setf_train_dir, setg_train_dir, seth_train_dir, seti_train_dir, setj_train_dir, seta_valid_dir, setb_valid_dir, setc_valid_dir, setd_valid_dir, sete_valid_dir, setf_valid_dir, setg_valid_dir, seth_valid_dir, seti_valid_dir, setj_valid_dir, seta_train_fnames, setb_train_fnames, setc_train_fnames, setd_train_fnames, sete_train_fnames, setf_train_fnames, setg_train_fnames, seth_train_fnames, seti_train_fnames, setj_train_fnames = prepare_data(base_dir, seta, setb, setc, setd, sete, setf, setg, seth, seti, setj)
seta_test_dir = os.path.join(test_dir, seta)
setb_test_dir = os.path.join(test_dir, setb)
setc_test_dir = os.path.join(test_dir, setc)
setd_test_dir = os.path.join(test_dir, setd)
sete_test_dir = os.path.join(test_dir, sete)
setf_test_dir = os.path.join(test_dir, setf)
setg_test_dir = os.path.join(test_dir, setg)
seth_test_dir = os.path.join(test_dir, seth)
seti_test_dir = os.path.join(test_dir, seti)
setj_test_dir = os.path.join(test_dir, setj)
test_fnames_seta = os.listdir(seta_test_dir)
test_fnames_setb = os.listdir(setb_test_dir)
test_fnames_setc = os.listdir(setc_test_dir)
test_fnames_setd = os.listdir(setd_test_dir)
test_fnames_sete = os.listdir(sete_test_dir)
test_fnames_setf = os.listdir(setf_test_dir)
test_fnames_setg = os.listdir(setg_test_dir)
test_fnames_seth = os.listdir(seth_test_dir)
test_fnames_seti = os.listdir(seti_test_dir)
test_fnames_setj = os.listdir(setj_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 % 10 == 0:
break
# Convolutional Neural Network model
# Import TensorFlow libraries
from tensorflow.keras import layers
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
img_input = layers.Input(shape = (150, 150, 3))
# 2D Convolution layer with 64 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(64, 3, activation = 'relu')(img_input)
# 2D max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 128 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(128, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 256 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(256, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# Flatten layer
x = layers.Flatten()(x)
# Fully connected layers and ReLU activation algorithm
x = layers.Dense(4096, activation = 'relu')(x)
x = layers.Dense(4096, activation = 'relu')(x)
x = layers.Dense(1000, activation = 'relu')(x)
# Dropout layers for optimisation
x = layers.Dropout(0.5)(x)
# Fully connected layers and sigmoid activation algorithm
model = Sequential()
model.add(Dense(10))
output = layers.Dense(10, activation = 'sigmoid')(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
nrows = 5
ncols = 5
pic_index = 0
# Set up matpotlib fig and size it to fit 5x5 pics
fig = plt.gcf()
fig.set_size_inches(nrows * 5, ncols * 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]]
next_setc_pix = [os.path.join(setc_train_dir, fname)
for fname in setc_train_fnames[pic_index-10:pic_index]]
next_setd_pix = [os.path.join(setd_train_dir, fname)
for fname in setd_train_fnames[pic_index-10:pic_index]]
next_sete_pix = [os.path.join(sete_train_dir, fname)
for fname in sete_train_fnames[pic_index-10:pic_index]]
next_setf_pix = [os.path.join(setf_train_dir, fname)
for fname in setf_train_fnames[pic_index-10:pic_index]]
next_setg_pix = [os.path.join(setg_train_dir, fname)
for fname in setg_train_fnames[pic_index-10:pic_index]]
next_seth_pix = [os.path.join(seth_train_dir, fname)
for fname in seth_train_fnames[pic_index-10:pic_index]]
next_seti_pix = [os.path.join(seti_train_dir, fname)
for fname in seti_train_fnames[pic_index-10:pic_index]]
next_setj_pix = [os.path.join(setj_train_dir, fname)
for fname in setj_train_fnames[pic_index-10:pic_index]]
for i, img_path in enumerate(next_seta_pix + next_setb_pix + next_setc_pix + next_setd_pix + next_sete_pix + next_setf_pix + next_setg_pix + next_seth_pix + next_seti_pix + next_setj_pix):
# Set up subplot; subplot indices start at 1
sp = plt.subplot(nrows, ncols, i + 1)
# Dont show axes
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]
c_img_files = [os.path.join(setc_train_dir, f) for f in setc_train_fnames]
d_img_files = [os.path.join(setd_train_dir, f) for f in setd_train_fnames]
e_img_files = [os.path.join(sete_train_dir, f) for f in sete_train_fnames]
f_img_files = [os.path.join(setf_train_dir, f) for f in setf_train_fnames]
g_img_files = [os.path.join(setg_train_dir, f) for f in setg_train_fnames]
h_img_files = [os.path.join(seth_train_dir, f) for f in seth_train_fnames]
i_img_files = [os.path.join(seti_train_dir, f) for f in seti_train_fnames]
j_img_files = [os.path.join(setj_train_dir, f) for f in setj_train_fnames]
img_path = random.choice(a_img_files + b_img_files + c_img_files + d_img_files + e_img_files + f_img_files + g_img_files + h_img_files + i_img_files + j_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']

what i understood from your code , your doing multi class classification because you have used Dense(10) at the last layer so
you need to change class_mode = 'binary' into
class model ='categorical' &
also change activation function sigmoid into
output = layers.Dense(10, activation = 'softmax')(x)

Related

Applying CNN method for face recognition but get the value error

The proposed method can automatically detect the features of hyperspectral images under the condition determined by the algorithms, and achieve the correct and fast recognition results.
Here I was trying to run the face recognition with using CNN method but then I got the error message as below ---
**
File "<ipython-input-6-fdb29ac830ce>", line 1, in <module>
runfile('C:/Users/MDIC/Desktop/Face Recognition With CNN.py', wdir='C:/Users/MDIC/Desktop')
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/MDIC/Desktop/Face Recognition With CNN.py", line 221, in <module>
plt.plot(epochs, val_acc)
File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 2811, in plot
is not None else {}), **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\__init__.py", line 1810, in inner
return func(ax, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1611, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 393, in _grab_next_args
yield from self._plot_args(this, kwargs)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 370, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 231, in _xy_from_xy
"have shapes {} and {}".format(x.shape, y.shape))
ValueError: x and y must have same first dimension, but have shapes (2,) and (1,)
**
This is my coding ---
# 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 ='Man_One'
setb ='Man_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))
# 2D Convolution layer with 64 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(64, 3, activation = 'relu')(img_input)
# 2D max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 128 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(128, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 256 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(256, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# 2D Max pooling layer
x = layers.MaxPooling2D(2)(x)
# 2D Convolution layer with 512 filters of dimension 3x3 and ReLU activation algorithm
x = layers.Conv2D(512, 3, activation = 'relu')(x)
# Flatten layer
x = layers.Flatten()(x)
# Fully connected layers and ReLU activation algorithm
x = layers.Dense(1024, activation = 'relu')(x)
x = layers.Dense(1024, activation = 'relu')(x)
x = layers.Dense(1000, activation = 'relu')(x)
# Dropout layers for optimisation
x = layers.Dropout(0.5)(x)
# Fully connected layers and sigmoid activation algorithm
output = layers.Dense(1, activation = 'sigmoid')(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 = 'binary_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.legend(['train', 'val'], loc='center')
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.legend(['train', 'val'], loc='center')
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.figure()
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.figure()
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_mo = 0
cal_mt = 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_mo+=1
elif(pred_img[0]>0.5):
cal_mt+=1
else:
print(pred_img[0], "\n")
cal_unconclusive+=1
alist.append(file_path)
print(alist)
print("Identified as: \n")
print("Man_One:", cal_mo)
print("Man_Two:", cal_mt)
print( "Inconclusive:", cal_unconclusive)
print( "Percentage:", (cal_mo/(cal_mo + cal_mt + cal_unconclusive)) * 100)
a = (cal_mo/(cal_mo + cal_mt + cal_unconclusive)) * 100
# Testing a random image from the test set b
cal_mo = 0
cal_mt = 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_mo+=1
elif(pred_img[0]>0.5):
cal_mt+=1
else:
print(pred_img[0], "\n")
cal_unconclusive+=1
alist.append(file_path)
print(alist)
print("Identified as: \n")
print("Man_One:", cal_mo)
print("Man_Two:", cal_mt)
print( "Inconclusive:", cal_unconclusive)
print( "Percentage:", (cal_mt/(cal_mo + cal_mt + cal_unconclusive)) * 100)
b = (cal_mt/(cal_mo + cal_mt + cal_unconclusive)) * 100
avg = (a+b)/2
print("Average Percentage:", avg)
Kindly look carefully at the above programming since it is a little bit long
Please help me a soon as possible
Thank you very much
It could be that your validation generated data terminates before reaching the 80 epochs of training. Check that you have at least 7*80 validation images.
Then check the number of elements in your: mymodel.history['val_acc']. It must be the same for training and validation if you use the epochs = range(len(acc)) as your x values for the graphs. The problem is that your acc and val_acc have different number of elements.

How do I test a complex CNN model on a new image?

I am learning CNN and I have found a script online that classifies building rooftops from satellite images. The script works just fine but I am not able to figure out a way to test the script on a new single image. I am showing the code briefly and then I will show what I have tried:
seq = iaa.Sequential([
iaa.imgcorruptlike.Fog(severity=1),
iaa.imgcorruptlike.Spatter(severity =1),
])
batch_size = 16
size = 512
epochs =50
version = 1 # version 2 for MobilV2unet
data_augmentation = True
model_type = 'UNet%d' % (version)
translearn = True
from tensorflow.keras.applications import MobileNetV2
def m_u_net(input_shape):
inputs = Input(shape=input_shape, name="input_image")
encoder = MobileNetV2(input_tensor=inputs, weights="imagenet", include_top=False, alpha=1.3)
#encoder.trainable=False
skip_connection_names = ["input_image", "block_1_expand_relu", "block_3_expand_relu", "block_6_expand_relu"]
encoder_output = encoder.get_layer("block_13_expand_relu").output
f = [16, 32, 48, 64]
x = encoder_output
for i in range(1, len(skip_connection_names)+1, 1):
x_skip = encoder.get_layer(skip_connection_names[-i]).output
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, x_skip])
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(f[-i], (3, 3), padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Conv2D(1, (1, 1), padding="same")(x)
x = Activation("sigmoid")(x)
model = Model(inputs, x)
return model
def load_rasters_simple(path, pathX, pathY ): # Subset from original raster with extent and upperleft coord
"""Load training data pairs (two high resolution images and two low resolution images)"""
pathXabs = os.path.join(path, pathX)
pathYabs = os.path.join(path, pathY)
le = len(os.listdir(pathXabs) )
stackX = []
stackY = []
for i in range(0, le):
fileX = os.path.join(pathXabs, os.listdir(pathXabs)[i])
fileY = os.path.join(pathYabs, os.listdir(pathXabs)[i])
dataX = gdal_array.LoadFile(fileX) #.astype(np.int),ysize=extent[1],xsize=extent[0]
stackX.append(dataX)
dataY = gdal_array.LoadFile(fileY) #.astype(np.int),ysize=extent[1],xsize=extent[0]
stackY.append(dataY)
stackX = np.array(stackX)
stackY = np.array(stackY)
return stackX, stackY
X, Y= load_rasters_simple('/Users/vaibhavsaxena/Desktop/segmentation/Classification/Satellite dataset ó± (global cities)','image','label')
def slice (arr, size, inputsize,stride):
result = []
if stride is None:
stride = size
for i in range(0, (inputsize-size)+1, stride):
for j in range(0, (inputsize-size)+1, stride):
s = arr[i:(i+size),j:(j+size), ]
result.append(s)
result = np.array(result)
return result
def batchslice (arr, size, inputsize, stride, num_img):
result = []
for i in range(0, num_img):
s= slice(arr[i,], size, inputsize, stride )
result.append(s )
result = np.array(result)
result = result.reshape(result.shape[0]*result.shape[1], result.shape[2], result.shape[3], -1)
return result
Y=batchslice(Y, size, Y.shape[1], size, Y.shape[0]).squeeze()
X_cl =batchslice(X_cl, size, X_cl.shape[1], size, X_cl.shape[0])
X_train = X_cl[:int(X_cl.shape[0]*0.8),]
Y_train = Y[:int(Y.shape[0]*0.8),]
X_test = X_cl[int(X_cl.shape[0]*0.8)+1:,]
Y_test = Y[int(Y.shape[0]*0.8)+1:,]
THEN the big unet model architecture. The whole script can be found here.
This model just works fine with the dataset. I am trying to test it with my own out of dataset image and this is what I have tried:
model = load_model('no_aug_unet_model.h5', custom_objects=dependencies)
model.compile(loss='binary_crossentropy', metrics=[iou],
optimizer=Adam(learning_rate=lr_schedule(0)))
from keras.preprocessing import image
test_image= image.load_img('bangkok_noi_2.jpg', target_size = (2000, 2000))
test_image = image.img_to_array(test_image)
test_image1 = test_image.reshape((1,2000,2000,3))
testpre = model.predict(test_image1)
img = Image.fromarray(test_image, 'RGB')
img.show()
The original shape of my test image is (1852, 3312, 3).
I am getting a weirdly predicted image that makes no sense unlike the expectations. I believe, I am doing the wrong preprocessing with my test image. Any help would be extremely appreciated.
The whole script can be found here.

I run this code and I get the following error. How do I fix this?

This is a code to predict stock price movements using TensorFlow and the ReLu activation function. I run the following code:
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import pandas_datareader as web
dataset = web.DataReader('AAPL', data_source = 'yahoo', start = '1989-01-01', end = '2019-12-25')
import math
close_price = dataset.filter(['Close']).values
data_train_len = math.ceil(len(close_price) * .8)
sc = MinMaxScaler(feature_range = (0, 1))
sc_data = sc.fit_transform(close_price)
data_train = sc_data[0 : data_train_len, : ]
xtrain = []
ytrain = []
for i in range(60, len(data_train)):
xtrain.append(data_train[i - 60 : i, 0])
ytrain.append(data_train[i, 0])
xtrain, ytrain = np.array(xtrain), np.array(ytrain)
xtrain = np.reshape(xtrain, (xtrain.shape[0], xtrain.shape[1], 1))
print(xtrain.shape, ytrain.shape)
data_test = sc_data[data_train_len - 60 : , :]
xtest = []
ytest = close_price[data_train_len :, :]
for i in range(60, len(data_test)):
xtest.append(data_test[i - 60 : i, 0])
xtest = np.array(xtest)
xtest = np.reshape(xtest, (xtest.shape[0], xtest.shape[1], 1))
print(xtest.shape, ytest.shape)
# Number of stock in training data
n_stocks = xtrain.shape[1]
#Model architecture parameters
n_neurons_1 = 1024
n_neurons_2 = 512
n_neurons_3 = 256
n_neurons_4 = 128
# Session
sesh = tf.InteractiveSession()
# Define two variables as placeholders
a = tf.placeholder(dtype = tf.float32, shape = [None, n_stocks])
b = tf.placeholder(dtype = tf.float32, shape = [1, None])
# Initializers
sig = 1
weight_init = tf.variance_scaling_initializer(mode = "fan_avg", distribution = "uniform", scale =
sig)
bias_init = tf.zeros_initializer()
# Hidden weights
w_hid_1 = tf.Variable(weight_init([n_stocks, n_neurons_1]))
bias_hid_1 = tf.Variable(bias_init([n_neurons_1]))
w_hid_2 = tf.Variable(weight_init([n_neurons_1, n_neurons_2]))
bias_hid_2 = tf.Variable(bias_init([n_neurons_2]))
w_hid_3 = tf.Variable(weight_init([n_neurons_2, n_neurons_3]))
bias_hid_3 = tf.Variable(bias_init([n_neurons_3]))
w_hid_4 = tf.Variable(weight_init([n_neurons_3, n_neurons_4]))
bias_hid_4 = tf.Variable(bias_init([n_neurons_4]))
# Output weights
w_out = tf.Variable(weight_init([n_neurons_4, 1]))
bias_out = tf.Variable(bias_init([1]))
# Hidden layers
hid_1 = tf.nn.relu(tf.add(tf.matmul(a, w_hid_1), bias_hid_1))
hid_2 = tf.nn.relu(tf.add(tf.matmul(hid_1, w_hid_2), bias_hid_2))
hid_3 = tf.nn.relu(tf.add(tf.matmul(hid_2, w_hid_3), bias_hid_3))
hid_4 = tf.nn.relu(tf.add(tf.matmul(hid_3, w_hid_4), bias_hid_4))
# Transposed Output layer
out = tf.transpose(tf.add(tf.matmul(hid_4, w_out), bias_out))
# Cost function
mse = tf.reduce_mean(tf.squared_difference(out, b))
rmse = tf.sqrt(tf.reduce_mean(tf.squared_difference(out, b)))
opt1 = tf.train.AdamOptimizer().minimize(mse)
opt2 = tf.train.AdamOptimizer().minimize(rmse)
sesh.run(tf.global_variables_initializer())
# Setup plot
plt.ion()
fig = plt.figure()
ax1 = fig.add_subplot(111)
line1, = ax1.plot(ytest)
line2, = ax1.plot(ytest * 0.5)
plt.show()
# Fitting neural network
batch_size = 256
mse_train = []
rmse_train = []
mse_test = []
rmse_test = []
# Run tensorflow
epochs = 10
for epoch in range(epochs):
# Training data is shuffled
shuffle_ind = np.random.permutation(np.arange(len(ytrain)))
xtrain = xtrain[shuffle_ind]
ytrain = ytrain[shuffle_ind]
# Minibatch training
for i in range(0, len(ytrain) // batch_size):
start = i * batch_size
batch_x = xtrain[start : start + batch_size]
batch_y = ytrain[start : start + batch_size]
# Run optimizer with batch
sesh.run(opt1, feed_dict = {a : batch_x, b : batch_y})
sesh.run(opt2, feed_dict = {a : batch_x, b : batch_y})
I get the following error:
ValueError: Cannot feed value of shape (256, 60, 1) for Tensor 'Placeholder_30:0', which has shape '(?, 60)'
This error appears for both of the last two lines under 'Run Optimizer with Batch'. How do I fix this?
It seems like you trying to feed data that doesn't fit with place holder (I think you placeholder a), simple way to change your place holder to a = tf.placeholder(dtype = tf.float32, shape = [None, n_stocks, 1]) or change your xtest and xtrain dimension (the line that you use reshape) by reduce last dimension using np.squeeze().

K.gradients returning [None]

K.gradients() return “None”. (Keras with tensorflow backend). Is there a problem with my model structure or something?
I have modified WGAN-GP model with keras for text, refer to this code: [https://github.com/OctThe16th/WGAN-GP-with-keras-for-text/blob/master/Exploration/GenerativeAdverserialWGAN-GP.py].
from keras.layers import Dense, Flatten, Input, BatchNormalization, Dropout, GRU, Bidirectional, Reshape, Activation
from keras.layers.noise import GaussianNoise
from keras.models import Model
from keras.layers.merge import _Merge
from keras.layers import Convolution1D, AveragePooling1D, ZeroPadding1D, UpSampling1D, concatenate, regularizers
from keras.layers import Embedding, Reshape, Lambda
from keras.layers import LSTM, multiply
from keras.optimizers import Adam, RMSprop
from random import randint
from keras.initializers import Constant
from keras import backend as K
from keras import layers
import numpy as np
from functools import partial
import pickle
import os
import tensorflow as tf
def wasserstein_loss(y_true, y_pred):
return K.mean(y_true * y_pred)
def gradient_penalty_loss(y_true, y_pred, averaged_samples):
'''Computes gradient penalty based on prediction and weighted real / fake samples'''
gradients = K.gradients(K.sum(y_pred), averaged_samples)
# compute the euclidean norm by squaring ...
gradients_sqr = K.square(gradients)
# ... summing over the rows ...
gradients_sqr_sum = K.sum(gradients_sqr)
# ... and sqrt
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
# compute lambda * (1 - ||grad||)^2 still for each single sample
gradient_penalty = K.square(1 - gradient_l2_norm)
# return the mean as loss over all the batch samples
return K.mean(gradient_penalty)
class RandomWeightedAverage(_Merge):
def _merge_function(self, inputs):
weights = K.random_uniform((BATCH_SIZE, 1), dtype='float32')
return (weights * inputs[0]) + ((1 - weights) * inputs[1])
#K.argmax() is not differentiable, this function is defined to repalce K.argmax(), and it is differentiable.
def argmax(x):
y = tf.reduce_sum(tf.cumsum(tf.ones_like(x), axis=-1) * tf.exp(beta * x) / tf.reduce_sum(tf.exp(beta * x), axis=-1, keep_dims=True), axis=-1) - 1
return y
def generator_mod(softmax_shape):
person1_input = Input(shape=(1,), dtype='float32')
noise_input = Input(shape=(1, person_embedding_dim), dtype='float32')
relation_input = Input(shape=(1,), dtype='float32')
person1_embedded = Embedding(1,person_embedding_dim)(person1_input)
relation_embedded = Embedding(1,relation_embedding_dim)(relation_input)
embedded_layer = concatenate([person1_embedded, relation_embedded, noise_input], axis=1)
drop_1 = BatchNormalization(momentum=0.8)(embedded_layer)
x_1 = Convolution1D(filters=64, kernel_size=3, padding='same', activation='elu')(drop_1)
x_1 = BatchNormalization()(x_1)
x_1 = Convolution1D(filters=32, kernel_size=3, padding='same', activation='elu')(x_1)
x_1 = BatchNormalization()(x_1)
x_1 = Flatten()(x_1)
x_1 = Dense(32, activation='relu')(x_1)
######################################################################
person1_description = Input(shape=(max_sequence_length,), dtype='float32')
embedded_sequences1 = Embedding(len(word_index) + 1, word_embeddings_dim)(person1_description)
lstm_out1 = Bidirectional(LSTM(64))(embedded_sequences1)
attention_1 = Dense(128, activation='softmax', name='attention_vec')(lstm_out1)
attention_mul = multiply([lstm_out1, attention_1], name='attention_mul')
#####globel attention finish#####
x_2 = Dense(32, activation='relu')(attention_mul)
full_connected = multiply([x_1, x_2], name='full_connected')
x = Dense(softmax_shape, activation='softmax')(full_connected)
output = Lambda(argmax)(x)#shape (?,)
output = Lambda(K.expand_dims, arguments={'axis': -1})(output) #shape (?,1)
model = Model(inputs = [person1_input, noise_input, relation_input, person1_description], outputs = output)
return model
def discriminator_mod():
person1_input = Input(shape=(1,), dtype='float32')
person2_input = Input(shape=(1,), dtype='float32')
relation_input = Input(shape=(1,), dtype='float32')
person1_embedded = Embedding(1, person_embedding_dim)(person1_input)
person2_embedded = Embedding(1, person_embedding_dim)(person2_input)
relation_embedded = Embedding(len(word_index) + 1, word_embeddings_dim)(relation_input)
embedded_layer = concatenate([person1_embedded, person2_embedded, relation_embedded], axis=1)
drop_1 = Dropout(0.5)(embedded_layer)
x = Convolution1D(128, 1, activation='relu')(drop_1)
x = BatchNormalization()(x)
x = Convolution1D(filters=64, kernel_size=3, padding='same', activation='elu')(x)
x = BatchNormalization()(x)
x = Convolution1D(filters=32, kernel_size=3, padding='same', activation='elu')(x)
x = BatchNormalization()(x)
x = Flatten()(x)
x = Dense(32, activation='relu')(x)
auxiliary_input1 = Input(shape=(max_sequence_length,), dtype='float32', name='aux_input1')
embedded_sequences1 = Embedding(len(word_index) + 1, word_embeddings_dim)(auxiliary_input1)
lstm_out1 = Bidirectional(LSTM(64))(embedded_sequences1)
lstm_drop1 = Dropout(0.5)(lstm_out1)
auxiliary_input2 = Input(shape=(max_sequence_length,), dtype='float32', name='aux_input2')
embedded_sequences2 = Embedding(len(word_index) + 1, word_embeddings_dim)(auxiliary_input2)
lstm_out2 = Bidirectional(LSTM(64))(embedded_sequences2)
lstm_drop2 = Dropout(0.5)(lstm_out2)
lstm_drop = multiply([lstm_drop1, lstm_drop2])
#####globel attention start#####
attention_1 = Dense(128, activation='softmax', name='attention_vec')(lstm_drop)
attention_mul = multiply([lstm_drop, attention_1], name='attention_mul')
#####globel attention finish#####
# attention_mul = Flatten()(attention_mul)
attention_mul = Dense(32, activation='relu')(attention_mul)
#####globel attention start#####
full_connected = multiply([x, attention_mul], name='full_connected')
attention_2 = Dense(32, activation='softmax')(full_connected)
attention_final = multiply([full_connected, attention_2])
#####globel attention finish#####
dense_layer = Dense(16, activation='relu')(attention_final)
main_output = Dense(1, activation='tanh', name='main_output')(dense_layer)
model = Model(inputs=[person1_input, person2_input, relation_input, auxiliary_input1, auxiliary_input2], outputs= main_output)
return model
def train(from_save_point=False, suffix='rnn'):
X_train = np.random.randn(10,243)
generator = generator_mod(person_total)
discriminator = discriminator_mod()
generator.summary()
discriminator.summary()
for layer in discriminator.layers:
layer.trainable = False
discriminator.trainable = False
for layer in discriminator.layers:
layer.trainable = False
discriminator.trainable = False
person1 = Input(shape=(1,))
relation = Input(shape=(1,))
seed = Input(shape=(1,person_embedding_dim))
person1_description = Input(shape=(max_sequence_length,))
genarated_person2 = generator([person1, seed, relation, person1_description])
person2_description = Input(shape=(max_sequence_length,))
discriminator_layers_for_generator = discriminator([person1, genarated_person2, relation, person1_description, person2_description])
generator_model = Model(inputs=[person1, relation, seed, person1_description, person2_description], outputs=[discriminator_layers_for_generator])
generator_model.compile(optimizer= RMSprop(lr=0.0001, rho=0.9), loss=wasserstein_loss)
for layer in discriminator.layers:
layer.trainable = True
for layer in generator.layers:
layer.trainable = False
discriminator.trainable = True
generator.trainable = False
person2 = Input(shape=(1,))
generated_samples_for_discriminator = generator([person1, seed, relation, person1_description])
discriminator_output_from_generator = discriminator([person1, generated_samples_for_discriminator, relation, person1_description, person2_description])
discriminator_output_from_real_samples = discriminator([person1, person2, relation, person1_description, person2_description])
averaged_samples = RandomWeightedAverage()([person2, generated_samples_for_discriminator])
averaged_samples_out = discriminator([person1, averaged_samples, relation, person1_description, person2_description])
partial_gp_loss = partial(gradient_penalty_loss, averaged_samples= averaged_samples)
partial_gp_loss.__name__ = 'gradient_penalty'
discriminator_model = Model(inputs=[person1, person2, relation, person1_description, person2_description, seed], outputs=[discriminator_output_from_real_samples, discriminator_output_from_generator, averaged_samples_out])
# averaged_samples_out
discriminator_model.compile(optimizer=RMSprop(lr=0.0001, rho=0.9), loss=[wasserstein_loss, wasserstein_loss, partial_gp_loss])
# partial_gp_loss
positive_y = np.ones((BATCH_SIZE, 1), dtype=np.float32)
negative_y = -positive_y
dummy_y = np.zeros((BATCH_SIZE, 1), dtype=np.float32)
if __name__ == "__main__":
# convert_text_to_nptensor(cutoff=50, min_frequency_words=100000, max_lines=20000000)
train(from_save_point=False, suffix='Google')
However, when the code execute to this line:
gradients = K.gradients(K.sum(y_pred), averaged_samples)
The error message is that:
'TypeError: Failed to convert object of type to Tensor. Contents: [None]. Consider casting elements to a supported type'
Can anyone help me? Thank you very much!

Rank mismatch error in Tensorflow

I'm working on creating an image classifier that can differentiate between cats and dogs. I have the follwing code:
import cv2
import os
from tqdm import tqdm
import numpy as np
import tensorflow as tf
img_height = 128
img_width = 128
path = "./train"
# class info
file = os.listdir(path)
index = []
images = []
# image size and channels
channels = 3
n_inputs = img_width * img_height * channels
# First convolutional layer
conv1_fmaps = 96 # Number of feature maps created by this layer
conv1_ksize = 4 # kernel size 3x3
conv1_stride = 2
conv1_pad = "SAME"
# Second convolutional layer
conv2_fmaps = 192
conv2_ksize = 4
conv2_stride = 4
conv2_pad = "SAME"
# Third layer is a pooling layer
pool3_fmaps = conv2_fmaps # Isn't it obvious?
n_fc1 = 192 # Total number of output features
n_outputs = 2
with tf.name_scope("inputs"):
X = tf.placeholder(tf.float32, shape=[None, img_width, img_height, channels], name="X")
X_reshaped = tf.reshape(X, shape=[-1, img_height, img_width, channels])
y = tf.placeholder(tf.int32, shape=[None, 2], name="y")
conv1 = tf.layers.conv2d(X_reshaped, filters=conv1_fmaps, kernel_size=conv1_ksize, strides=conv1_stride, padding=conv1_pad, activation=tf.nn.relu, name="conv1")
conv2 = tf.layers.conv2d(conv1, filters=conv2_fmaps, kernel_size=conv2_ksize, strides=conv2_stride, padding=conv2_pad, activation=tf.nn.relu, name="conv2")
n_epochs = 10
batch_size = 250
with tf.name_scope("pool3"):
pool3 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
pool3_flat = tf.reshape(pool3, shape=[-1, pool3_fmaps * 8 * 8])
with tf.name_scope("fc1"):
fc1 = tf.layers.dense(pool3_flat, n_fc1, activation=tf.nn.relu name="fc1")
with tf.name_scope("output"):
logits = tf.layers.dense(fc1, n_outputs, name="output")
Y_proba = tf.nn.softmax(logits, name="Y_proba")
with tf.name_scope("train"):
xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)
loss = tf.reduce_mean(xentropy)
optimizer = tf.train.AdamOptimizer()
training_op = optimizer.minimize(loss)
with tf.name_scope("eval"):
correct = tf.nn.in_top_k(logits, y, 1)
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
init = tf.global_variables_initializer()
with tf.name_scope("init_and_save"):
saver = tf.train.Saver()
def next_batch(num):
index = []
images = []
# Data set Creation
print("Creating batch dataset "+str(num+1)+"...")
for f in tqdm(range(num * batch_size, (num+1)*batch_size)):
if file[f].find("dog"):
index.append(np.array([0, 1]))
else:
index.append(np.array([1, 0]))
image = cv2.imread(path + "/" + file[f])
image = cv2.resize(image, (img_width, img_height), 0, 0, cv2.INTER_LINEAR)
# image = image.astype(np.float32)
images.append(image)
images = np.array(images, dtype=np.uint8)
images = images.astype('float32')
images = images / 255
print("\nBatch "+str(num+1)+" creation finished.")
# print([images, index])
return [images, index]
with tf.Session() as sess:
init.run()
for epoch in range(n_epochs):
for iteration in range(25000 // batch_size):
X_batch, y_batch = next_batch(iteration)
sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
print(epoch, "Train accuracy:", acc_train)
save_path = saver.save(sess, "./dogvscat_mnist_model.ckpt")
But I'm getting this error:
ValueError: Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).
Can anyone point out the problem and help me to solve it. I'm totally new to this.
For tf.nn.sparse_softmax_corss_entropy_with_logits rank(labels) = rank(logits) - 1, so you need to redefine the labels placeholder as follows
...
y = tf.placeholder(tf.int32, shape=[None], name="y")
...
xentropy=tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=y)
...
X_batch, y_batch = next_batch(iteration)
y_batch = np.argmax(y_batch, axis=1)
OR you can you just use tf.nn.softmax_cross_entropy_with_logits without changing labels placeholder.
xentropy=tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels=y)

Resources