I have a a predicament with the model I am developing based on a popular dataset of skin-cancer images.
I have to points I'd like some guidance on -
A.
The original dataset is over +10K images with which almost 7000 images belong to one of the seven classes. I've created a subset of 4948 random images with which I ran a function to convert the images into a list of lists - first list contains the image and the latter the class as well as dismiss any images that are of class (5 - the class with the +6800K images). Thought process was to normalise the distribution across the classes.
Re-running the original model with an output (Dense layer of 6 neurons instead of 7) - retrieves an error.
Am I missing a step to 'indicate' to the model that there are only six possible classes? The model runs only when the output layer has seven neurons.
error:
Train on 1245 samples, validate on 312 samples
Epoch 1/30
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-138-8a3b40a69e37> in <module>
25 metrics=["accuracy"])
26
---> 27 model.fit(X_train, y_train, batch_size=32, epochs=30, validation_split=0.2)
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
778 validation_steps=validation_steps,
779 validation_freq=validation_freq,
--> 780 steps_name='steps_per_epoch')
781
782 def evaluate(self,
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
361
362 # Get outputs.
--> 363 batch_outs = f(ins_batch)
364 if not isinstance(batch_outs, list):
365 batch_outs = [batch_outs]
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3290
3291 fetched = self._callable_fn(*array_vals,
-> 3292 run_metadata=self.run_metadata)
3293 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3294 output_structure = nest.pack_sequence_as(
/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Received a label value of 6 which is outside the valid range of [0, 6). Label values: 1 1 2 4 2 1 2 1 2 1 2 2 4 2 2 1 3 1 4 6 0 2 4 2 0 4 2 4 4 0 2 4
[[{{node loss_15/activation_63_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
B.
I'm attempting to add Data Augmentation as the dataset is relatively small taking into account the number of classes and the sparsity of the images across the classes. Once I try to run the generator I receive the error message below which suggests that there is something wrong with one of the variables in the validation_data tuple. I cannot understand what the issue is.
Example values of the test set look like:
[[[[0.41568627]
[0.4 ]
[0.43137255]
...
[0.54509804]
[0.54901961]
[0.54509804]]
[[0.42352941]
[0.43137255]
[0.43921569]
...
[0.56078431]
[0.54117647]
[0.55294118]]
[[0.41960784]
[0.41960784]
[0.45490196]
...
[0.51764706]
[0.57254902]
[0.50588235]]
...
[[0.30980392]
[0.36470588]
[0.36470588]
...
[0.47058824]
[0.44705882]
[0.41960784]]
[[0.29803922]
[0.31764706]
[0.34509804]
...
[0.45098039]
[0.43921569]
[0.4 ]]
[[0.25882353]
[0.30196078]
[0.31764706]
...
[0.45490196]
[0.42745098]
[0.36078431]]]
[[[0.60784314]
[0.59215686]
[0.56862745]
...
[0.59607843]
[0.63921569]
[0.63529412]]
[[0.6627451 ]
[0.63137255]
[0.62352941]
...
[0.67843137]
[0.60784314]
[0.63529412]]
[[0.62745098]
[0.65098039]
[0.6 ]
...
[0.61568627]
[0.63921569]
[0.67058824]]
...
[[0.62352941]
[0.6 ]
[0.59607843]
...
[0.6627451 ]
[0.71372549]
[0.6745098 ]]
[[0.61568627]
[0.58431373]
[0.61568627]
...
[0.67058824]
[0.65882353]
[0.68235294]]
[[0.61176471]
[0.60392157]
[0.61960784]
...
[0.65490196]
[0.6627451 ]
[0.66666667]]]]
[2, 1, 4, 4, 2]
Error:
Epoch 1/10
1/155 [..............................] - ETA: 11s - loss: 1.7916 - acc: 0.3000
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-139-8f19a958861f> in <module>
12 history = model.fit_generator(trainAug.flow(X_train, y_train, batch_size=batch_size)
13 ,epochs = 10, validation_data = (X_test, y_test),
---> 14 steps_per_epoch= X_train.shape[0]// batch_size
15 )
16 #epochs = epochs, validation_data = (X_test, y_test),
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1431 shuffle=shuffle,
1432 initial_epoch=initial_epoch,
-> 1433 steps_name='steps_per_epoch')
1434
1435 def evaluate_generator(self,
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_generator.py in model_iteration(model, data, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch, mode, batch_size, steps_name, **kwargs)
262
263 is_deferred = not model._is_compiled
--> 264 batch_outs = batch_function(*batch_data)
265 if not isinstance(batch_outs, list):
266 batch_outs = [batch_outs]
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight, reset_metrics)
1173 self._update_sample_weight_modes(sample_weights=sample_weights)
1174 self._make_train_function()
-> 1175 outputs = self.train_function(ins) # pylint: disable=not-callable
1176
1177 if reset_metrics:
/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3290
3291 fetched = self._callable_fn(*array_vals,
-> 3292 run_metadata=self.run_metadata)
3293 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3294 output_structure = nest.pack_sequence_as(
/anaconda3/lib/python3.7/site-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Received a label value of 6 which is outside the valid range of [0, 6). Label values: 0 1 6 4 2 4 2 0 1 2
[[{{node loss_15/activation_63_loss/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]]
Code:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys
import os
import cv2
DATA_DIR = "/Users/namefolder/PycharmProjects/skin-cancer/HAM10000_images_part_1"
metadata = pd.read_csv(os.path.join(DATA_DIR, 'HAM10000_metadata.csv'))
lesion_type_dict = {'nv': 'Melanocytic nevi',
'mel': 'Melanoma',
'bkl': 'Benign keratosis-like lesions ',
'bcc': 'Basal cell carcinoma',
'akiec': 'Actinic keratoses',
'vasc': 'Vascular lesions',
'df': 'Dermatofibroma'}
metadata['cell_type'] = metadata['dx'].map(lesion_type_dict.get)
metadata['dx_code'] = pd.Categorical(metadata['dx']).codes
# save array of image-id and diagnosis-type (categorical)
metadata = metadata[['image_id', 'dx', 'dx_type', 'dx_code']]
training_data = []
IMG_SIZE=50
# preparing training data
def creating_training_data(path):
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))
for index, row in metadata.iterrows():
if (img == row['image_id']+'.jpg') & (row['dx_code'] != 5):
try:
training_data.append([new_array, row['dx_code']])
except Exception as ee:
pass
except Exception as e:
pass
return training_data
training_data = creating_training_data(DATA_DIR)
import random
random.shuffle(training_data)
# Splitting data into X features and Y label
X_train = []
y_train = []
for features, label in training_data:
X_train.append(features)
y_train.append(label)
# Reshaping of the data - required by Tensorflow and Keras (*necessary step of deep-learning using these repos)
X_train = np.array(X_train).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
# Normalize data - to reduce processing requirements
X_train = X_train/255.0
# model configuration
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape = X_train.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Dense(6))
model.add(Activation("softmax"))
model.compile(loss="mean_squared_error",
optimizer="adam",
metrics=["accuracy"])
# Data Augmentation - Repo enabler
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
# initialize the training training data augmentation object
trainAug = ImageDataGenerator(
rescale=1 / 255.0,
rotation_range=20,
zoom_range=0.05,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
horizontal_flip=True,
fill_mode="nearest")
# initialize the validation (and testing) data augmentation object
valAug = ImageDataGenerator(rescale=1 / 255.0)
#set a leraning rate annealer
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
#Augmented Images model development
)
trainAug.fit(X_train)
#Fit the model
epochs = 10
batch_size= 10
history = model.fit_generator(trainAug.flow(X_train, y_train, batch_size=batch_size),epochs = 10, validation_data = (X_test, y_test), steps_per_epoch= X_train.shape[0]// batch_size)
Initially you had 7 labels: your code was then expecting labels 0, 1, 2, 3, 4, 5, 6
You removed label 5 from the dataset, ok. Now you have 6 labels in total.
Your code is expecting: 0, 1, 2, 3, 4, 5
But what you have in your data is: 0, 1, 2, 3, 4, 6
After removing the label 5, you need to transform the labels 6 into 5.
Something in the lines of:
if (img == row['image_id']+'.jpg') & (row['dx_code'] > 5):
try:
training_data.append([new_array, row['dx_code'] - 1])
except Exception as ee:
pass
elif (img == row['image_id']+'.jpg') & (row['dx_code'] < 5):
try:
training_data.append([new_array, row['dx_code']])
except Exception as ee:
pass
Related
I'm a beginner in Python & CNN.
I did write a simple code for training model between 2 classes
I've a folder has 2 folders for training and 2 folders for validation
import keras,os
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.preprocessing.image import ImageDataGenerator
Classifier = Sequential()
classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy' , metrics = ['raccuracy'])
train_datagen = ImageDataGenerator(rescale = 1./255, shear_range = 0.2,zoom_range = 0.2,horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory(r'C:\Users\user1\Documents\code\Test', target_size = (244, 244), color_mode="rgb", batch_size = 32, class_mode = 'binary', shuffle=True,)
test_set = test_datagen.flow_from_directory(r'C:\Users\user1\Documents\code\Valid', target_size = (244, 244), color_mode="rgb", batch_size = 32, class_mode = 'binary', shuffle=True,)
STEP_SIZE_TRAIN=training_set.n #train_generator.batch_size
STEP_SIZE_VALID=test_set.n #valid_generator.batch_size
classifier.fit_generator(generator = training_set, steps_per_epoch = STEP_SIZE_TRAIN,epochs = 5,validation_data = test_set,validation_steps = STEP_SIZE_VALID)
Everything went well But I have an error. I couldn't know what is the problem. It starts immediately for the first epoch as showing below
Found 518 images belonging to 2 classes.
Found 40 images belonging to 2 classes.
Epoch 1/5
TypeError Traceback (most recent call last) <ipython-input-21-c93c80bb7785> in <module>
20 STEP_SIZE_VALID=test_set.n #valid_generator.batch_size
21
---> 22 classifier.fit_generator(generator = training_set,
23 steps_per_epoch = STEP_SIZE_TRAIN,
24 epochs = 5,
~\anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py in new_func(*args, **kwargs)
322 'in a future version' if date is None else ('after %s' % date),
323 instructions)
--> 324 return func(*args, **kwargs)
325 return tf_decorator.make_decorator(
326 func, new_func, 'deprecated',
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, validation_freq, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch) 1813 """ 1814
_keras_api_gauge.get_cell('fit_generator').set(True)
-> 1815 return self.fit( 1816 generator, 1817 steps_per_epoch=steps_per_epoch,
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing) 1096 batch_size=batch_size): 1097 callbacks.on_train_batch_begin(step)
-> 1098 tmp_logs = train_function(iterator) 1099 if data_handler.should_sync: 1100 context.async_wait()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in __call__(self, *args, **kwds)
778 else:
779 compiler = "nonXla"
--> 780 result = self._call(*args, **kwds)
781
782 new_tracing_count = self._get_tracing_count()
~\anaconda3\lib\site-packages\tensorflow\python\eager\def_function.py in _call(self, *args, **kwds)
805 # In this case we have created variables on the first call, so we run the
806 # defunned version which is guaranteed to never create variables.
--> 807 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable
808 elif self._stateful_fn is not None:
809 # Release the lock early so that multiple threads can perform the call TypeError: 'NoneType' object is not callable.
Can any one please tell me what is the problem ?
I want to build a Neural Network with two inputs: for image data and for numeric data. So I wrote custom data generator for that. The train and validation dataframes contain 11 columns:
image_name — path to the image;
9 numeric features;
target — class for the item (last column).
The code for custom generator (based on this answer):
target_size = (224, 224)
batch_size = 1
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True)
val_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_dataframe(
train,
x_col='image_name',
y_col=train.columns[1:],
target_size=target_size,
batch_size=batch_size,
shuffle=True,
class_mode='raw')
validation_generator = val_datagen.flow_from_dataframe(
validation,
x_col='image_name',
y_col=validation.columns[1:],
target_size=target_size,
shuffle=False,
batch_size=batch_size,
class_mode='raw')
def train_generator_func():
count = 0
while True:
if count == len(train.index):
train_generator.reset()
break
count += 1
data = train_generator.next()
imgs = []
cols = []
targets = []
for k in range(batch_size):
imgs.append(data[0][k])
cols.append(data[1][k][:-1])
targets.append(data[1][k][-1])
yield [imgs, cols], targets
def validation_generator_func():
count = 0
while True:
if count == len(validation.index):
validation_generator.reset()
break
count += 1
data = validation_generator.next()
imgs = []
cols = []
targets = []
for k in range(batch_size):
imgs.append(data[0][k])
cols.append(data[1][k][:-1])
targets.append(data[1][k][-1])
yield [imgs, cols], targets
Model building:
def mlp_model(dim):
model = Sequential()
model.add(Dense(8, input_dim=dim, activation="relu"))
model.add(Dense(4, activation="relu"))
return model
def vgg16_model():
model = VGG16(weights='imagenet', include_top=False, input_shape=target_size+(3,))
x=Flatten()(model.output)
output=Dense(1,activation='sigmoid')(x) # because we have to predict the AUC
model=Model(model.input,output)
return model
def concatenated_model(cnn, mlp):
combinedInput = concatenate([cnn.output, mlp.output])
x = Dense(4, activation="relu")(combinedInput)
x = Dense(1, activation="sigmoid")(x)
model = Model(inputs=[cnn.input, mlp.input], outputs=x)
return model
def focal_loss(alpha=0.25,gamma=2.0):
def focal_crossentropy(y_true, y_pred):
bce = K.binary_crossentropy(y_true, y_pred)
y_pred = K.clip(y_pred, K.epsilon(), 1.- K.epsilon())
p_t = (y_true*y_pred) + ((1-y_true)*(1-y_pred))
alpha_factor = 1
modulating_factor = 1
alpha_factor = y_true*alpha + ((1-alpha)*(1-y_true))
modulating_factor = K.pow((1-p_t), gamma)
# compute the final loss and return
return K.mean(alpha_factor*modulating_factor*bce, axis=-1)
return focal_crossentropy
cnn = vgg16_model()
mlp = mlp_model(9)
model = concatenated_model(cnn, mlp)
opt = Adam(lr=1e-5)
model.compile(loss=focal_loss(), metrics=[tf.keras.metrics.AUC()],optimizer=opt)
nb_epochs = 2
nb_train_steps = train.shape[0]//batch_size
nb_val_steps = validation.shape[0]//batch_size
model.fit(
train_generator_func(),
steps_per_epoch=nb_train_steps,
epochs=nb_epochs,
validation_data=validation_generator_func(),
validation_steps=nb_val_steps)
And fitting doesn't work with error message:
AttributeError Traceback (most recent call last)
<ipython-input-53-253849fd34d6> in <module>
9 epochs=nb_epochs,
10 validation_data=validation_generator_func(),
---> 11 validation_steps=nb_val_steps)
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
106 def _method_wrapper(self, *args, **kwargs):
107 if not self._in_multi_worker_mode(): # pylint: disable=protected-access
--> 108 return method(self, *args, **kwargs)
109
110 # Running inside `run_distribute_coordinator` already.
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1061 use_multiprocessing=use_multiprocessing,
1062 model=self,
-> 1063 steps_per_execution=self._steps_per_execution)
1064
1065 # Container that configures and calls `tf.keras.Callback`s.
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1108 use_multiprocessing=use_multiprocessing,
1109 distribution_strategy=ds_context.get_strategy(),
-> 1110 model=model)
1111
1112 strategy = ds_context.get_strategy()
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
796 return tensor_shape.TensorShape([None for _ in shape.as_list()])
797
--> 798 output_shapes = nest.map_structure(_get_dynamic_shape, peek)
799 output_types = nest.map_structure(lambda t: t.dtype, peek)
800
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
633
634 return pack_sequence_as(
--> 635 structure[0], [func(*x) for x in entries],
636 expand_composites=expand_composites)
637
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
633
634 return pack_sequence_as(
--> 635 structure[0], [func(*x) for x in entries],
636 expand_composites=expand_composites)
637
d:\pyenv\keras-gpu\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in _get_dynamic_shape(t)
792 shape = t.shape
793 # Unknown number of dimensions, `as_list` cannot be called.
--> 794 if shape.rank is None:
795 return shape
796 return tensor_shape.TensorShape([None for _ in shape.as_list()])
AttributeError: 'tuple' object has no attribute 'rank'
So I tried to look at Keras sources but without any success.
If I use modified train_generator and validation_generator (y_col='target' instead of y_col=train.columns[1:]) everything works fine.
You need to convert all the individual objects returned by both the training and validation generators to Numpy arrays:
yield [np.array(imgs), np.array(cols)], np.array(targets)
Alternatively, a simpler and much more efficient solution is to not iterate over the data batch at all; instead, we can take advantage of the fact that these objects are already Numpy arrays when returned by ImageDataGenerator, so we can write:
imgs = data[0]
cols = data[1][:,:-1]
targets = data[1][:,-1:]
yield [imgs, cols], targets
A different solution worked for me, just posting it here.
I ran into the problem working with two very similar dataframes in one notebook, where for one of them the error occurred.
I noticed the dtypes were slightly different int64 vs Int64, where the target column coded as Int64 gave the error.
For me the following worked:
dataframe[target_col] = dataframe[target_col].astype(int)
I am using a simple Keras Sequential Model with CNN, on MNIST Data. I could Build the Model but when I run model.fit, I encounter the error, AttributeError: 'Dimension' object has no attribute 'log10'. Below mentioned is my code. Googled it but couldn't find the solution.
Below mentioned is the complete code. TF Version is 1.15.
# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals
from io import open
# Common imports
import numpy as np
import os
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, Dense, Dropout, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
height = 28
width = 28
channels = 1
n_inputs = height * width
conv1_fmaps = 32
conv1_ksize = 3
conv1_stride = 1
conv1_pad = "SAME"
conv2_fmaps = 64
conv2_ksize = 3
conv2_stride = 2
conv2_pad = "SAME"
pool3_fmaps = conv2_fmaps
n_fc1 = 64
n_outputs = 10
with tf.name_scope("inputs"):
X = tf.placeholder(tf.float32, shape=[None, n_inputs], name="X")
X_reshaped = tf.reshape(X, shape=[-1, height, width, channels])
y = tf.placeholder(tf.int32, shape=[None], name="y")
cnn_model = Sequential()
cnn_model.add(Conv2D(filters=conv1_fmaps, kernel_size=conv1_ksize,
strides=conv1_stride, padding=conv1_pad,
activation=tf.nn.relu, input_shape=(height, width, channels),
data_format='channels_last'))
cnn_model.add(MaxPool2D(pool_size = (2,2), strides= (2,2), padding="VALID"))
cnn_model.add(Dropout(0.25))
cnn_model.add(Flatten())
cnn_model.add(Dense(units = 32, activation = 'relu'))
cnn_model.add(Dense(units = 10, activation = 'sigmoid'))
cnn_model.summary()
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0
X_train_reshaped = tf.reshape(X_train, shape=[-1, height, width, channels])
X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0
X_test_reshaped = tf.reshape(X_test, shape=[-1, height, width, channels])
#y_train = y_train.astype(np.int32)
y_train = tf.cast(y_train, dtype = tf.int32)
#y_test = y_test.astype(np.int32)
y_test = tf.cast(y_test, dtype = tf.int32)
cnn_model.compile(loss ='sparse_categorical_crossentropy', optimizer=Adam(lr=0.001),metrics =['accuracy'])
steps_per_epoch = X_train_reshaped.shape[0]//512
steps_per_epoch
epochs = 50
history = cnn_model.fit(x = X_train_reshaped,
y = y_train,
batch_size = 512,
epochs = 5,
verbose = 1, validation_data = (X_test_reshaped, y_test),
validation_steps = 10, steps_per_epoch=steps_per_epoch)
Stack Trace of Error is shown below:
Train on 117 samples, validate on 10000 samples
Epoch 1/5
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-89dd7568f671> in <module>
6 epochs = 5,
7 verbose = 1, validation_data = (X_test_reshaped, y_test),
----> 8 validation_steps = 10, steps_per_epoch=steps_per_epoch)
~/anaconda3/envs/TF_PY_36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
725 max_queue_size=max_queue_size,
726 workers=workers,
--> 727 use_multiprocessing=use_multiprocessing)
728
729 def evaluate(self,
~/anaconda3/envs/TF_PY_36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
673 validation_steps=validation_steps,
674 validation_freq=validation_freq,
--> 675 steps_name='steps_per_epoch')
676
677 def evaluate(self,
~/anaconda3/envs/TF_PY_36/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
347 batch_logs = cbks.make_logs(model, batch_logs, batch_outs, mode)
348 callbacks._call_batch_hook(mode, 'end', step, batch_logs)
--> 349 progbar.on_batch_end(step, batch_logs)
350 step += 1
351
~/anaconda3/envs/TF_PY_36/lib/python3.6/site-packages/tensorflow_core/python/keras/callbacks.py in on_batch_end(self, batch, logs)
759 # will be handled by on_epoch_end.
760 if self.verbose and (self.target is None or self.seen < self.target):
--> 761 self.progbar.update(self.seen, self.log_values)
762
763 def on_epoch_end(self, epoch, logs=None):
~/anaconda3/envs/TF_PY_36/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/generic_utils.py in update(self, current, values)
393
394 if self.target is not None:
--> 395 numdigits = int(np.log10(self.target)) + 1
396 bar = ('%' + str(numdigits) + 'd/%d [') % (current, self.target)
397 prog = float(current) / self.target
AttributeError: 'Dimension' object has no attribute 'log10'
Thank you in advance for your help.
Error is coming because your steps_per_epoch has datatype Dimension and not integer.
steps_per_epoch = X_train_reshaped.shape[0]//512
type(steps_per_epoch)
# output: tensorflow.python.framework.tensor_shape.Dimension
To change your shape to integer try this:
steps_per_epoch = X_train_reshaped.shape[0].value//512
I am trying to train variational encoder. But I am getting
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]].
I read the images using opencv and append it to the list and then I converted it into numpy array.
Copied code from : http://www.stokastik.in/understanding-variational-autoencoders/
I am using convolutional variational autoencoder.
images = []
files = glob.glob('../dataset/maggi/*.*')
i=0
for file in files:
try:
img = cv2.imread(file)
img = cv2.resize(img, (28,28))
images.append(img)
except:
print('error')
x_train = np.asarray(images)
x_train = x_train.astype('float32') / 255.
print('Input size : ',x_train.shape)
conv_variational_autoencoder(x_train)
Output :
Input size : (1446, 28, 28, 3)
Epoch 1/50
----------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-166-2e8711de7bdc> in <module>()
72 print('Input size : ',x_train.shape)
73
---> 74 conv_variational_autoencoder(x_train)
<ipython-input-166-2e8711de7bdc> in conv_variational_autoencoder(X_train)
50 adam = Adam(lr=0.0005)
51 autoencoder.compile(optimizer=adam, loss=None)
---> 52 autoencoder.fit(X_train, shuffle=True, epochs=50, batch_size=32)
53
54
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: Incompatible shapes: [32,784] vs. [32,2352]
[[{{node custom_variational_layer_21/logistic_loss/mul}}]]
Thanks for the link to the article! It's a really interesting and good writeup.
Now for the problem:
As a rule: ALWAYS check your models' inputs and ouputs by using the model.summaray() function. In your case your model looks like this:
Now watch closely. Your input images are of the shape 28x28x3 like you defined yourself. But the output is 28x28x1 because the article you used trains the model on mnist, which is greyscale and thus only has 1 channel for colors, you have three.
This yields an error in the loss function, because it tries to compare how well a greyscale image looks like a color image, which of course doesn't work.
To fix this, all you have to do is go to the decoder part of the conv_variational_autoencoder(x_train) function and change the output size of the last Conv2DTranspose to be 28x28x3 instead of 28x28x1:
#Decoder
decoder_input = Input(shape=(196,))
p = Reshape((14, 14, 1))(decoder_input)
x = Conv2DTranspose(32, (3, 3), activation='relu', padding='same')(p)
x = UpSampling2D((2, 2))(x)
# dec_out = Conv2DTranspose(1, (3, 3), activation='sigmoid', padding='same')(x)
# Change the above line to:
dec_out = Conv2DTranspose(3, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, dec_out)
And it should train straight away. Good luck!
I am new to Keras and I am trying to build a recurrent neural network to classify Audio files.
During the training, I am receiving an InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000).
I have found various topics talking about this error but, to be honest, I did not understand what I have to change in the parameters I am passing to the network in order to help it managing the negative values I have in the training array.
Below the code:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
max_features = 20000
maxlen = 40
batch_size = 32
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(Bidirectional(LSTM(64)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
print('Train...')
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=4,
validation_data=[X_test, y_test])
Error below:
Train...
Train on 964 samples, validate on 476 samples
Epoch 1/4
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-22-3452d23cb8b5> in <module>()
12 batch_size=batch_size,
13 epochs=4,
---> 14 validation_data=[X_test, y_test])
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
1037 initial_epoch=initial_epoch,
1038 steps_per_epoch=steps_per_epoch,
-> 1039 validation_steps=validation_steps)
1040
1041 def evaluate(self, x=None, y=None,
/usr/local/lib/python3.6/dist-packages/keras/engine/training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
197 ins_batch[i] = ins_batch[i].toarray()
198
--> 199 outs = f(ins_batch)
200 outs = to_list(outs)
201 for l, o in zip(out_labels, outs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
2713 return self._legacy_call(inputs)
2714
-> 2715 return self._call(inputs)
2716 else:
2717 if py_any(is_tensor(x) for x in inputs):
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
2673 fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
2674 else:
-> 2675 fetched = self._callable_fn(*array_vals)
2676 return fetched[:len(self.outputs)]
2677
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
InvalidArgumentError: indices[28,0] = -711 is not in [0, 20000)
[[{{node embedding_3/embedding_lookup}} = GatherV2[Taxis=DT_INT32, Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:#training_1/Adam/Assign_2"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_3/embeddings/read, embedding_3/Cast, training_1/Adam/gradients/embedding_3/embedding_lookup_grad/concat/axis)]]
EDIT1: X_train is the float32 array below
array([[-5.79938449e+02, 6.63875936e+01, -6.75054944e+00, ...,
-2.89458464e+00, -2.30009868e+00, -2.34216322e+00],
[-3.38924973e+02, 1.60668197e+01, -5.39871140e+01, ...,
1.27180395e+00, 4.28090614e+00, 2.01538667e+00],
[-5.53199739e+02, 3.45314936e+01, -1.68711443e+01, ...,
-9.47345310e-02, -1.04780706e-02, 1.69060756e-01],
...,
[-5.91902354e+02, 6.14329122e+01, 1.43761675e+00, ...,
-4.38644438e+00, -3.67977820e+00, -1.89899207e+00],
[-7.04889969e+02, 6.24931510e+01, 1.90338300e+01, ...,
-1.47540089e+00, -1.75498741e+00, -4.55713837e-01],
[-8.24296641e+02, 7.43124586e+01, 1.43319513e+01, ...,
-7.60749297e-01, -1.05324700e+00, -8.54044186e-01]])