Project Section - My Project - Upload Keras code question

Does the Keras code only contain the Model definition (sequential API) to be able to upload ? I tried it with a .py file containing a full DL processing program with Keras model but it failed to upload:

Blockquote
from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode=‘nearest’)

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense

model = Sequential()
model.add(Conv2D(32, 3, 3, input_shape=(3, 150, 150)))
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)))

the model so far outputs 3D feature maps (height, width, features)

model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation(‘relu’))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation(‘sigmoid’))

model.compile(loss=‘binary_crossentropy’,
optimizer=‘rmsprop’,
metrics=[‘accuracy’])

batch_size = 16

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)

this is a generator that will read pictures found in

subfolers of ‘data/train’, and indefinitely generate

batches of augmented image data

train_generator = train_datagen.flow_from_directory(
‘/data/34/.kaggle/competitions/dogs-vs-cats/kdata/train’, # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=batch_size,
class_mode=‘binary’) # since we use binary_crossentropy loss, we need binary labels

this is a similar generator, for validation data

validation_generator = test_datagen.flow_from_directory(
‘/data/34/.kaggle/competitions/dogs-vs-cats/kdata/validation’,
target_size=(150, 150),
batch_size=batch_size,
class_mode=‘binary’)

model.fit_generator(
generator=train_generator,
validation_data=validation_generator,
samples_per_epoch=2000,
nb_epoch=40,
nb_val_samples=800,
verbose=1)
model.save_weights(‘first_try.h5’) # always save your weights after training or during training

Blockquote

The error messages were :slight_smile:
Connected to compute server.
Traceback (most recent call last):
File “backend/importKeras.py”, line 493, in importKeras (backend/importKeras.c:16331)
File “”, line 20, in
File “/usr/local/lib/python3.5/dist-packages/keras/models.py”, line 332, in add
output_tensor = layer(self.outputs[0])
File “/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py”, line 572, in call
self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File “/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py”, line 635, in add_inbound_node
Node.create_node(self, inbound_layers, node_indices, tensor_indices)
File “/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py”, line 166, in create_node
output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
File “/usr/local/lib/python3.5/dist-packages/keras/layers/pooling.py”, line 160, in call
dim_ordering=self.dim_ordering)
File “/usr/local/lib/python3.5/dist-packages/keras/layers/pooling.py”, line 210, in _pooling_function
pool_mode=‘max’)
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 33, in func_wrapper
train_ret = func(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 2919, in pool2d
out = _postprocess_convnd_output(KerasSymbol(s), dim_ordering)
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 26, in func_wrapper
train_ret = func(*args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 2701, in _postprocess_convnd_output
if dim_ordering == ‘tf’ and ndim(x) > 3:
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 634, in ndim
s = shape(x)
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 579, in shape
return x.get_shape()
File “/usr/local/lib/python3.5/dist-packages/keras/backend/mxnet_backend.py”, line 258, in get_shape
_, out_shape, _ = self.symbol.infer_shape_partial()
File “/usr/local/lib/python3.5/dist-packages/mxnet/symbol/symbol.py”, line 1035, in infer_shape_partial
return self._infer_shape_impl(True, *args, **kwargs)
File “/usr/local/lib/python3.5/dist-packages/mxnet/symbol/symbol.py”, line 1092, in infer_shape_impl
ctypes.byref(complete)))
File “/usr/local/lib/python3.5/dist-packages/mxnet/base.py”, line 146, in check_call
raise MXNetError(py_str(LIB.MXGetLastError()))
mxnet.base.MXNetError: Error in operator pooling0: [14:14:44] src/operator/./pooling-inl.h:214: Check failed: param
.kernel[0] <= dshape[2] + 2 * param
.pad[0] kernel size (2) exceeds input (1 padded to 1)

Stack trace returned 10 entries:
[bt] (0) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(+0x26ae5c) [0x7f11b7b6de5c]
[bt] (1) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(+0x252bed2) [0x7f11b9e2eed2]
[bt] (2) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(+0x22f80e7) [0x7f11b9bfb0e7]
[bt] (3) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(+0x2114d2e) [0x7f11b9a17d2e]
[bt] (4) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(+0x2117890) [0x7f11b9a1a890]
[bt] (5) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(MXSymbolInferShape+0x1539) [0x7f11b99a08a9]
[bt] (6) /usr/local/lib/python3.5/dist-packages/mxnet/libmxnet.so(MXSymbolInferShapePartial+0x82) [0x7f11b99a2132]
[bt] (7) /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so(ffi_call_unix64+0x4c) [0x7f11d700ce20]
[bt] (8) /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so(ffi_call+0x2eb) [0x7f11d700c88b]
[bt] (9) /usr/lib/python3.5/lib-dynload/_ctypes.cpython-35m-x86_64-linux-gnu.so(_ctypes_callproc+0x49a) [0x7f11d700701a]

Uploading to server
***FAILED.import failed

There is no limitation on Keras code. It can have sequential or functional API and it can be a pull DL processing program.

Does this program work in Keras 1.2.2 environment ? Can you share the .py file so that we can try to reproduce this issue ?

The program worked with keras 1.2.2 AFAIK. It actually worked in the DLS notebook. I try to upload the program but it not possible to load .py file on the forum. The code is in my first posts. Copy/paste would work.