I tried to use keras.preprocessing.image.ImageDataGenerator on TPU,but i get this error from the first epoch.the same code work with jupyter notebook but take hours for trainig.
My MODEL:
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3)))
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(220))
model.add(Activation('relu'))
model.add(Dropout(0.4))
model.add(Dense(120))
model.add(Activation('softmax'))
Optimizer
opt = tf.train.AdamOptimizer(learning_rate)
model.compile(
optimizer=opt,
loss='categorical_crossentropy',
metrics=['acc'])
Convert Keras to TPU
try:
device_name = os.environ['COLAB_TPU_ADDR']
TPU_ADDRESS = 'grpc://' + device_name
print('Found TPU at: {}'.format(TPU_ADDRESS))
except KeyError:
print('TPU not found')
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(TPU_ADDRESS)))
ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')#binary ,categorical
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')
Model Fit
model_fit=tpu_model.fit_generator(
train_generator,
epochs=50,
steps_per_epoch=60,
)
I get this error
Epoch 1/50 15/33 [============>.................] - ETA: 8s - loss:
4.7722 - acc: 0.0083INFO:tensorflow:New input shapes; (re-)compiling: mode=train (# of cores 8), [TensorSpec(shape=(0,), dtype=tf.int32,
name='core_id_60'), TensorSpec(shape=(0, 128, 128, 3),
dtype=tf.float32, name='conv2d_3_input_20'), TensorSpec(shape=(0,
120), dtype=tf.float32, name='activation_13_target_30')]
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call
last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
in _create_c_op(graph, node_def, inputs, control_inputs) 1658
try:
-> 1659 c_op = c_api.TF_FinishOperation(op_desc) 1660 except errors.InvalidArgumentError as e:
InvalidArgumentError: slice index 0 of dimension 0 out of bounds. for
'strided_slice_19' (op: 'StridedSlice') with input shapes: [0], [1],
[1], [1] and with computed input tensors: input[1] = <0>, input[2] =
<1>, input[3] = <1>.
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call
last) 17 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py
in _create_c_op(graph, node_def, inputs, control_inputs) 1660
except errors.InvalidArgumentError as e: 1661 # Convert to
ValueError for backwards compatibility.
-> 1662 raise ValueError(str(e)) 1663 1664 return c_op
ValueError: slice index 0 of dimension 0 out of bounds. for
'strided_slice_19' (op: 'StridedSlice') with input shapes: [0], [1],
[1], [1] and with computed input tensors: input[1] = <0>, input[2] =
<1>, input[3] = <1>.
Related
I am trying to use Transfer Learning using ResNet-50 in TensorFlow2 and Keras on CIFAR-10 dataset which has (32, 32, 3) images.
The default ResNet-50's first conv layer uses a filter size of (7, 7) with stride = 2, the resulting CIFAR-10 is reduced too much spatially here which is to be avoided. As a 'hack', the images are attempted to be upscaled from (32, 32) to (224, 224). The code is:
import tensorflow.keras as K
# Define KerasTensor as input-
input_t = K.Input(shape = (32, 32, 3))
res_model = K.applications.ResNet50(
include_top = False,
weights = "imagenet",
input_tensor = input_t
)
# Since CIFAR-10 dataset is small as compared to ImageNet, the images are upscaled to (224, 224)-
to_res = (224, 224)
model = K.models.Sequential()
model.add(K.layers.Lambda(lambda image: tf.image.resize(image, to_res)))
model.add(res_model)
model.add(K.layers.Flatten())
model.add(K.layers.BatchNormalization())
model.add(K.layers.Dense(units = 10, activation = 'softmax'))
# Choose an optimizer and loss function for training-
loss_fn = tf.keras.losses.CategoricalCrossentropy()
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.1, momentum = 0.9)
model.compile(
# loss = 'categorical_crossentropy',
loss = loss_fn,
# optimizer = K.optimizers.RMSprop(lr=2e-5),
optimizer = optimizer,
metrics=['accuracy']
)
history = model.fit(
x = X_train, y = y_train,
batch_size = batch_size, epochs = 10,
validation_data = (X_test, y_test),
# callbacks=[check_point]
)
To which I get the error:
Epoch 1/10 WARNING:tensorflow:Model was constructed with shape (None,
32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 32,
32, 3), dtype=tf.float32, name='input_1'), name='input_1',
description="created by layer 'input_1'"), but it was called on an
input with incompatible shape (None, 224, 224, 3).
ValueError Traceback (most recent call
last)
in ()
2 x = X_train, y = y_train,
3 batch_size = batch_size, epochs = 10,
----> 4 validation_data = (X_test, y_test),
5 # callbacks=[check_point]
6 )
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py
in wrapper(*args, **kwargs)
975 except Exception as e: # pylint:disable=broad-except
976 if hasattr(e, "ag_error_metadata"):
--> 977 raise e.ag_error_metadata.to_exception(e)
978 else:
979 raise
ValueError: in user code:
ValueError: Input 0 is incompatible with layer resnet50: expected
shape=(None, 32, 32, 3), found shape=(None, 224, 224, 3)
The input of the model is still (32, 32, 3)
input_t = K.Input(shape = (32, 32, 3))
I try run this NN but return this error:
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 10. Shapes are [3] and [10]. for '{{node AssignAddVariableOp_2}} = AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_2/resource, Sum_2)' with input shapes: [], [10].
def build_model(hp):
model=Sequential()
dropout = 0.2
hp_units = hp.Int('units', min_value=32, max_value=512, step=32)
hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])
model.add(LSTM(units=hp_units,input_shape=(X_train.shape[1:])
,return_sequences=True))
model.add(Dropout(dropout))
model.add(BatchNormalization())
model.add(LSTM(units=hp_units, return_sequences=False))
model.add(Dropout(dropout))
model.add(BatchNormalization())
model.add(Dense(units=hp_units, activation='sigmoid'))
model.add(Dropout(rate=dropout))
model.add(Dense(10))
model.compile(optimizer=keras.optimizers.Adam(learning_rate=hp_learning_rate),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tfa.metrics.F1Score(num_classes=3)])
return model
tuner = kt.Hyperband(build_model,
objective=kt.Objective('val_tfa.metrics.F1Score', direction='max'),
max_epochs = 10,
factor = 3,
directory = 'model2',
project_name= 'Hyper tuning')
stop_early = EarlyStopping(monitor='val_tfa.metrics.F1Score', patience=3)
tuner.search(X_train, y_train, epochs=50, validation_split=0.2, callbacks=[stop_early])
I have a sample data of page visits of one page for 803 days. I have extracted features from data like mean, median etc and final shape of data is (803, 25). I had taken a train set of 640 and a test set of 160. I am trying to use CNN+LSTM model using Keras. But I am getting an error in model.fit method.
I have tried permute layer and changed input shapes but still not able to fix it.
trainX.shape = (642, 1, 25)
trainY.shape = (642,)
testX.shape = (161, 1, 25)
testY.shape = (161,)
'''python
# Basic layer
model = Sequential()
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), strides=1, padding='SAME', input_shape = (642, 25, 1), activation = 'relu')))
model.add(TimeDistributed(Convolution2D(filters = 32, kernel_size = (3, 3), activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Convolution2D(32, 3, 3, activation = 'relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size = (2, 2))))
model.add(TimeDistributed(Flatten()))
model.add(Permute((2, 3), input_shape=(1, 25)))
model.add(LSTM(units=54, return_sequences=True))
# To avoid overfitting
model.add(Dropout(0.2))
# Adding 6 more layers
model.add(LSTM(units=25, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=50, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=54))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(units = 1, activation='relu', kernel_regularizer=regularizers.l1(0.0001))))
model.add(PReLU(weights=None, alpha_initializer="zero")) # add an advanced activation
model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
model.fit(trainX, trainY, epochs = 50, batch_size = 32)
predictions = model.predict(testX)
'''
#Runtime Error
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-218-86932db86d0b> in <module>()
42
43 model.compile(optimizer = 'adam', loss = customSmapeLoss, metrics=['mae'])
---> 44 model.fit(trainX, trainY, epochs = 50, batch_size = 32)
Error - IndexError: list index out of range
The input_shape requires a tuple of length 4 when it using TimeDistributed with Conv2D
see https://keras.io/layers/wrappers/
input_shape=(10, 299, 299, 3))
Dont you think your data field is too little? usually, CNN+LSTM is meant for a more complicated task with thousands of sequential images/videos.
i am trying to use VGG16 pretrained in Keras, but i keep getting this error:
ValueError: Error when checking target: expected block5_pool to have 4 dimensions, but got array with shape (6, 1)
What is the error mean?
Error when checking target: expected block5_pool to have 4 dimensions,
but got array with shape (6, 1)
This the my actual code:
from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
# dimensions of our images.
img_width, img_height = 224, 224
train_data_dir = 'database/train'
validation_data_dir = 'database/validation'
nb_train_samples = 2000
nb_validation_samples = 26
epochs = 50
batch_size = 20
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
print('VGG-16 Model loaded.')
top_model = Sequential()
top_model.add(ZeroPadding2D((1,1),input_shape=input_shape))
top_model.add(Conv2D(64, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(64, (3, 3), activation='relu'))
top_model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(128, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(128, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(256, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(ZeroPadding2D((1,1)))
top_model.add(Conv2D(512, (3, 3), activation='relu'))
top_model.add(MaxPooling2D((2,2), strides=(2,2)))
top_model.add(Flatten())
top_model.add(Dense(4096, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(12))
top_model.add(Activation('softmax'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
# top_model.load_weights('./vgg16_face_weights.h5')
# add the model on top of the convolutional base
model.add_update(top_model)
# set the first 25 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:25]:
layer.trainable = False
model.compile(loss='sparse_categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='binary')
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size)
model.save_weights('first_try.h5')
The terminal run:
VGG-16 Model loaded.
Found 46 images belonging to 12 classes.
Found 26 images belonging to 12 classes.
Epoch 1/50
Traceback (most recent call last):
File "C:/Users/w024029h/PycharmProjects/keras_pretrained/pretrained.py", line 113, in <module>
validation_steps=nb_validation_samples // batch_size)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 2230, in fit_generator
class_weight=class_weight)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1877, in train_on_batch
class_weight=class_weight)
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 1480, in _standardize_user_data
exception_prefix='target')
File "C:\Users\w024029h\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\training.py", line 113, in _standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected block5_pool to have 4 dimensions, but got array with shape (6, 1)
I had this same error before. I resolved it by changing the class_mode on the data generators from 'binary' to None
According to the keras docs, setting the class_mode to 'binary' will return a 1-D array.
By changing the class_mode=None, no labels are returned and the generator will only yield batches of image data. This is what you are expecting with your model and this works well with the fit_generator() method. However, when using class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.
This page was useful to me
^look at flow_from_directory()
Hope this helps!
I am having an issue when trying to train my model in Keras 2.0.8, Python 3.6.1, and a Tensorflow Backend.
Error Message:
ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1)
X_train = numpy.swapaxes(X_train, 1, 3)
X_test = numpy.swapaxes(X_test, 1, 3)
print("X_train shape: ") --> size = (2592, 1, 1366, 96)
print("-----")
print("X_test shape") --> size = (648, 1, 1366, 96)
print("-----")
print(Y_train.shape) --> size = (2592,)
print("-----")
print("Y_test shape") --> size = (648,)
Relevant Code snippets:
K.set_image_dim_ordering('th')
K.set_image_data_format('channels_first')
def create_model(weights_path=None):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96)))
model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(16, activation='relu'))
model.add(Dense(2, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
model = create_model()
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.SGD(lr=0.01),
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=32,
epochs=100,
verbose=1,
validation_data=(X_test, Y_test))
Line 142, where I call model.fit() is where I am getting this error
Things I have tried to fix this error
Referenced these stack overflow posts:
I tried to reshape the Y_test and Y_train numpy arrays using the following code:
Y_train.reshape(2592, 2)
Y_test.reshape(648, 2)
However, I get the following error:
ValueError: cannot reshape array of size 2592 into shape (2592,2)
It seems to me you need to change the last layer of the NN:
def create_model(weights_path=None):
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96)))
model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
if weights_path:
model.load_weights(weights_path)
return model
As you are using the categorical_crossentropy loss, you have to use one-hot encoded labels. For this you can use the function to_categorical from keras.utils.np_utils
from keras.utils import np_utils
y_train_onehot = np_utils.to_categorical(y_train)
y_test_onehot = np_utils.to_categorical(y_test)
Then use the one-hot encoded labels to train your model.