Concatenation makes the training fail keras - python-2.7

I want to implement the U-net CNN architecture. In this architecture, there is several concatenations in the "upsampling" part. I use keras 2.1.3, python 2.7 and tensorflow '1.4.0-rc0'
My inputs are of the shape (6,128,128) (channel first). Here is the code I've came up with
input_shape = (6, 128, 128)
# Create model U-net Model
input_fields = Input(shape=input_shape)
f32 = Conv2D(32, (3,3), padding="same")(input_fields)
f32 = Activation("relu", name="f32")(f32)
s32 = Conv2D(32, (3,3), padding="same")(f32)
s32 = Activation("relu",name="s32")(s32) ## To concatenate 32
pool32_64 = MaxPooling2D((2,2), padding="same")(s32)
f64 = Conv2D(64, (3,3), padding="same")(pool32_64)
f64 = Activation("relu")(f64)
s64 = Conv2D(64, (3,3), padding="same")(f64)
s64 = Activation("relu")(s64) # To concatenate 64
pool64_128 = MaxPooling2D((2,2), padding="same")(s64)
f128 = Conv2D(128, (3,3), padding="same")(pool64_128)
f128 = Activation("relu")(f128)
s128 = Conv2D(128, (3,3), padding="same")(f128)
s128 = Activation("relu")(s128)
print "Last shape before Upsampling "s128.get_shape()
#### vvv Upsampling Part vvv ####
up_128_64 = UpSampling2D((2,2))(s128)
up_128_64 = Conv2D(64, (2,2), padding="same")(up_128_64)
print "Conv2d pu_128_64 ", up_128_64.get_shape()
m64 = Concatenate(axis=0)([s64, up_128_64]) #or concatenate([s64, up_128_64], axis=0)
f64U = Conv2D(64, (3,3), padding="same")(m64)
f64U = Activation("relu")(f64U)
#print "f64U.get_shape()", f64U.get_shape()
s64U = Conv2D(64, (3,3), padding="same")(f64U)
s64U = Activation("relu")(s64U)
up_64_32 = UpSampling2D((2,2))(s64U)
up_64_32 = Conv2D(32, (2,2), padding="same")(up_64_32)
m32 = Concatenate(axis=0)([s32, up_64_32]) # or concatenate([s32, up_64_32], axis=0)
f32U = Conv2D(32, (3,3), padding="same")(m32)
f32U = Activation("relu")(f32U)
print "f32U.get_shape()", f32U.get_shape()
s32U = Conv2D(32, (3,3), padding="same")(f32U)
s32U = Activation("relu")(s32U)
output_field = Conv2D(1, (1,1), padding="same")(s32U)
output_field = Activation("relu")(output_field)
print output_field.get_shape()
U_net = Model(input_fields, output_field)
U_net.summary()
U_net.compile(optimizer="RMSProp", loss="mse")#, metrics=["accuracy"])
U_net.fit(X_train, y_train)
Concerning the U_net.summary() the output is :
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) (None, 6, 128, 128) 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 32, 128, 128) 1760 input_1[0][0]
__________________________________________________________________________________________________
f32 (Activation) (None, 32, 128, 128) 0 conv2d_1[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 128, 128) 9248 f32[0][0]
__________________________________________________________________________________________________
s32 (Activation) (None, 32, 128, 128) 0 conv2d_2[0][0]
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 32, 64, 64) 0 s32[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 64, 64) 18496 max_pooling2d_1[0][0]
__________________________________________________________________________________________________
activation_1 (Activation) (None, 64, 64, 64) 0 conv2d_3[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 64, 64, 64) 36928 activation_1[0][0]
__________________________________________________________________________________________________
activation_2 (Activation) (None, 64, 64, 64) 0 conv2d_4[0][0]
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 64, 32, 32) 0 activation_2[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D) (None, 128, 32, 32) 73856 max_pooling2d_2[0][0]
__________________________________________________________________________________________________
activation_3 (Activation) (None, 128, 32, 32) 0 conv2d_5[0][0]
__________________________________________________________________________________________________
conv2d_6 (Conv2D) (None, 128, 32, 32) 147584 activation_3[0][0]
__________________________________________________________________________________________________
activation_4 (Activation) (None, 128, 32, 32) 0 conv2d_6[0][0]
__________________________________________________________________________________________________
up_sampling2d_1 (UpSampling2D) (None, 128, 64, 64) 0 activation_4[0][0]
__________________________________________________________________________________________________
conv2d_7 (Conv2D) (None, 64, 64, 64) 32832 up_sampling2d_1[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 64, 64, 64) 0 activation_2[0][0]
conv2d_7[0][0]
__________________________________________________________________________________________________
conv2d_8 (Conv2D) (None, 64, 64, 64) 36928 concatenate_1[0][0]
__________________________________________________________________________________________________
activation_5 (Activation) (None, 64, 64, 64) 0 conv2d_8[0][0]
__________________________________________________________________________________________________
conv2d_9 (Conv2D) (None, 64, 64, 64) 36928 activation_5[0][0]
__________________________________________________________________________________________________
activation_6 (Activation) (None, 64, 64, 64) 0 conv2d_9[0][0]
__________________________________________________________________________________________________
up_sampling2d_2 (UpSampling2D) (None, 64, 128, 128) 0 activation_6[0][0]
__________________________________________________________________________________________________
conv2d_10 (Conv2D) (None, 32, 128, 128) 8224 up_sampling2d_2[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate) (None, 32, 128, 128) 0 s32[0][0]
conv2d_10[0][0]
__________________________________________________________________________________________________
conv2d_11 (Conv2D) (None, 32, 128, 128) 9248 concatenate_2[0][0]
__________________________________________________________________________________________________
activation_7 (Activation) (None, 32, 128, 128) 0 conv2d_11[0][0]
__________________________________________________________________________________________________
conv2d_12 (Conv2D) (None, 32, 128, 128) 9248 activation_7[0][0]
__________________________________________________________________________________________________
activation_8 (Activation) (None, 32, 128, 128) 0 conv2d_12[0][0]
__________________________________________________________________________________________________
conv2d_13 (Conv2D) (None, 1, 128, 128) 33 activation_8[0][0]
__________________________________________________________________________________________________
activation_9 (Activation) (None, 1, 128, 128) 0 conv2d_13[0][0]
==================================================================================================
Total params: 421,313
Trainable params: 421,313
Non-trainable params: 0
The network is built and
X_train.shape = (576, 6, 128, 128)
y_train.shape = (576, 1, 128, 128)
But during the training I receive this error
Epoch 1/1
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
........../run_CNN_tau.py in <module>()
174 U_net.compile(optimizer="RMSProp", loss="mae")#, metrics=["accuracy"])
175
--> 176 U_net.fit(X_train, y_train)#, validation_data=(X_test, y_test), epochs=1)
177
178 #model.add(Conv2D(32, kernel_size=(16,16), padding="same", activation='relu', input_shape=input_shape))
/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc 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)
1667 initial_epoch=initial_epoch,
1668 steps_per_epoch=steps_per_epoch,
-> 1669 validation_steps=validation_steps)
1670
1671 def evaluate(self, x=None, y=None,
/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
1204 ins_batch[i] = ins_batch[i].toarray()
1205
-> 1206 outs = f(ins_batch)
1207 if not isinstance(outs, list):
1208 outs = [outs]
/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
2473 session = get_session()
2474 updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2475 **self.session_kwargs)
2476 return updated[:len(self.outputs)]
2477
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
887 try:
888 result = self._run(None, fetches, feed_dict, options_ptr,
--> 889 run_metadata_ptr)
890 if run_metadata:
891 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
1118 if final_fetches or final_targets or (handle and feed_dict_tensor):
1119 results = self._do_run(handle, final_targets, final_fetches,
-> 1120 feed_dict_tensor, options, run_metadata)
1121 else:
1122 results = []
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1315 if handle is None:
1316 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1317 options, run_metadata)
1318 else:
1319 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
1334 except KeyError:
1335 pass
-> 1336 raise type(e)(node_def, op, message)
1337
1338 def _extend_graph(self):
InvalidArgumentError: Incompatible shapes: [96,1,128,128] vs. [32,1,128,128]
[[Node: training/RMSprop/gradients/loss/activation_9_loss/sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _class=["loc:#loss/activation_9_loss/sub"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](training/RMSprop/gradients/loss/activation_9_loss/sub_grad/Shape, training/RMSprop/gradients/loss/activation_9_loss/sub_grad/Shape_1)]]
Caused by op u'training/RMSprop/gradients/loss/activation_9_loss/sub_grad/BroadcastGradientArgs', defined at:
File "/usr/local/bin/ipython", line 11, in <module>
sys.exit(start_ipython())
File "/usr/local/lib/python2.7/dist-packages/IPython/__init__.py", line 119, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 658, in launch_instance
app.start()
File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/ipapp.py", line 355, in start
self.shell.mainloop()
File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/interactiveshell.py", line 495, in mainloop
self.interact()
File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/interactiveshell.py", line 486, in interact
self.run_cell(code, store_history=True)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2714, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2824, in run_ast_nodes
if self.run_code(code, result):
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2878, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-463-b869a174fafa>", line 1, in <module>
get_ipython().magic(u'run run_CNN_tau.py')
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2160, in magic
return self.run_line_magic(magic_name, magic_arg_s)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2081, in run_line_magic
result = fn(*args,**kwargs)
File "<decorator-gen-58>", line 2, in run
File "/usr/local/lib/python2.7/dist-packages/IPython/core/magic.py", line 188, in <lambda>
call = lambda f, *a, **k: f(*a, **k)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.py", line 742, in run
run()
File "/usr/local/lib/python2.7/dist-packages/IPython/core/magics/execution.py", line 728, in run
exit_ignore=exit_ignore)
File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2483, in safe_execfile
self.compile if kw['shell_futures'] else None)
File "/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.py", line 289, in execfile
builtin_mod.execfile(filename, *where)
File "/home/nsaura/Documents/Git_RANNS/ML/turbo/wk/tests/python/run_CNN_tau.py", line 176, in <module>
U_net.fit(X_train, y_train)#, validation_data=(X_test, y_test), epochs=1)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1646, in fit
self._make_train_function()
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 970, in _make_train_function
loss=self.total_loss)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 233, in get_updates
grads = self.get_gradients(loss, params)
File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 78, in get_gradients
grads = K.gradients(loss, params)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 2512, in gradients
return tf.gradients(loss, variables, colocate_gradients_with_ops=True)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 581, in gradients
grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 353, in _MaybeCompile
return grad_fn() # Exit early
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 581, in <lambda>
grad_scope, op, func_call, lambda: grad_fn(op, *out_grads))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_grad.py", line 727, in _SubGrad
rx, ry = gen_array_ops._broadcast_gradient_args(sx, sy)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 532, in _broadcast_gradient_args
"BroadcastGradientArgs", s0=s0, s1=s1, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
...which was originally created as op u'loss/activation_9_loss/sub', defined at:
File "/usr/local/bin/ipython", line 11, in <module>
sys.exit(start_ipython())
[elided 16 identical lines from previous traceback]
File "/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.py", line 289, in execfile
builtin_mod.execfile(filename, *where)
File "/home/nsaura/Documents/Git_RANNS/ML/turbo/wk/tests/python/run_CNN_tau.py", line 174, in <module>
U_net.compile(optimizer="RMSProp", loss="mae")#, metrics=["accuracy"])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 827, in compile
sample_weight, mask)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 426, in weighted
score_array = fn(y_true, y_pred)
File "/usr/local/lib/python2.7/dist-packages/keras/losses.py", line 18, in mean_absolute_error
return K.mean(K.abs(y_pred - y_true), axis=-1)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 894, in binary_op_wrapper
return func(x, y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 4636, in _sub
"Sub", x=x, y=y, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): Incompatible shapes: [96,1,128,128] vs. [32,1,128,128]
[[Node: training/RMSprop/gradients/loss/activation_9_loss/sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _class=["loc:#loss/activation_9_loss/sub"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](training/RMSprop/gradients/loss/activation_9_loss/sub_grad/Shape, training/RMSprop/gradients/loss/activation_9_loss/sub_grad/Shape_1)]]
The point is, this U-net works fine if the concatenation layers are removed. Can someone explain how I can fix this issue ?

According to the Keras documentation, the default batch size for training is 32 samples (https://keras.io/models/model/#fit), and if I look at your architecture, it seems like you are essentially taking the input stream, splitting it, and then merging it twice (once for each Concatenate) yielding 96 samples per batch. This might explain the error message content: "[96,1,128,128] vs. [32,1,128,128]".
Are you sure you want to be doing Concatenate along the batch dimension? Hope this helps.

Related

Fortran read data with different colums in multiple rows

Hi I am almost new for fortran and struggling with it at the moment.
My question is,
I open a text file and wanna read muliple lines which have different colums.
(My goal is to read the element into an 1D array)
The last line in my code will print the start to end location.
I hope someone could help me.
IMPLICIT NONE
CHARACTER(500) :: MODELLINE
INTEGER(4) :: STARTPOINT, POINT, N, CNT, NROW
INTEGER(4) :: I, NNOD, J, K
INTEGER(4), ALLOCATABLE :: STARLIST(:), FINDCOL(:,:)
OPEN(UNIT=400, FILE='inputfile.txt', STATUS ='OLD')
CNT = 0
STARTPOINT =0
DO I = 1, 4000
READ(400,'(A)',END = 10001, ERR= 10001) MODELLINE
CNT = CNT + 1
IF (MODELLINE(1:7) == '*ELSET,') STARTPOINT = STARTPOINT+1
ENDDO
10001 REWIND(400)
ALLOCATE(STARLIST(STARTPOINT))
NROW = CNT
K = 0
POINT =0
DO I = 1, NROW
READ(400,'(A)') MODELLINE
IF (MODELLINE(1:7) == '*ELSET,') K = K+1
STARLIST(K) = I ! Location for *ELSET
IF (MODELLINE(1:19) == '*ELSET, ELSET=PART4') POINT = I
ENDDO
!WRITE(*,*) STARTPOINT, STARLIST, POINT
DO I = 1, STARTPOINT
IF (STARLIST(I) == POINT-1) THEN
K = STARLIST(I)-STARLIST(I+1)
ENDIF
ENDDO
WRITE(*,*) POINT, POINT-(K)-1
REWIND(400)
END
inputfile is like this
*ELSET, ELSET=PART1
203, 204, 205, 206, 207, 208, 209, 210, 211, 212
213, 214, 215, 216, 217, 218, 219, 220, 221, 222
223, 224, 225, 255, 256, 257, 258, 259, 260, 261
262, 263, 264, 265, 266, 267, 268, 269, 270, 271
272, 273, 274, 275, 276, 277, 278, 279, 308, 309
310, 311, 312, 313, 314, 315, 316, 317, 318, 319
320, 321, 322, 323, 324, 325, 326, 327, 328, 329
330, 331, 332, 333, 334, 335, 336, 347, 348, 349
350, 351, 352, 353, 354, 355, 356
2203, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2212
2213, 2214, 2215, 2216, 2217, 2218, 2219, 2220, 2221, 2222
2223, 2224, 2225, 2255, 2256, 2257, 2258, 2259, 2260, 2261
2262, 2263, 2264, 2265, 2266, 2267, 2268, 2269, 2270, 2271
2272, 2273, 2274, 2275, 2276, 2277, 2278, 2279, 2308, 2309
2310, 2311, 2312, 2313, 2314, 2315, 2316, 2317, 2318, 2319
2320, 2321, 2322, 2323, 2324, 2325, 2326, 2327, 2328, 2329
2330, 2331, 2332, 2333, 2334, 2335, 2336, 2347, 2348, 2349
2350, 2351, 2352, 2353, 2354, 2355, 2356
*ELSET, ELSET=PART2
226, 227, 228, 229, 337, 359, 465, 466, 467, 468
469, 470, 471, 472, 473, 474, 475, 476, 477, 478
479, 480, 481, 482, 483, 484, 485, 486, 487, 488
2226, 2227, 2228, 2229, 2337, 2359, 2465, 2466, 2467, 2468
2469, 2470, 2471, 2472, 2473, 2474, 2475, 2476, 2477, 2478
2479, 2480, 2481, 2482, 2483, 2484, 2485, 2486, 2487, 2488
*ELSET, ELSET=PART3
230, 231, 232, 233, 234, 235, 236, 237, 238, 239
240, 241, 242, 243, 244, 245, 246, 247, 248, 249
250, 251, 252, 253, 254
2230, 2231, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2239
2240, 2241, 2242, 2243, 2244, 2245, 2246, 2247, 2248, 2249
2250, 2251, 2252, 2253, 2254
*ELSET, ELSET=PART4
255, 256, 257, 258, 259, 260, 261, 262, 263, 264
265, 266, 267, 268, 269, 270, 271, 272, 273, 274
275, 276, 277, 278, 279
2255, 2256, 2257, 2258, 2259, 2260, 2261, 2262, 2263, 2264
2265, 2266, 2267, 2268, 2269, 2270, 2271, 2272, 2273, 2274
2275, 2276, 2277, 2278, 2279
*ELSET, ELSET=PART5
280, 281, 282, 283, 284, 285, 286, 287, 288, 289
290, 291, 292, 293, 294, 295, 296, 297, 298, 299
300, 301, 302, 303, 304, 305, 306, 307
2280, 2281, 2282, 2283, 2284, 2285, 2286, 2287, 2288, 2289
2290, 2291, 2292, 2293, 2294, 2295, 2296, 2297, 2298, 2299
2300, 2301, 2302, 2303, 2304, 2305, 2306, 2307
*ELSET, ELSET=PART6
338, 339, 340, 341, 342, 343, 344, 345, 346
2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346
*ELSET, ELSET=PART7
357, 358, 360, 361, 362, 363, 364, 365, 366, 367
368, 369, 370, 371, 372, 373, 374, 375, 376, 377
378, 379, 380, 381, 382, 383, 384, 385, 386, 387
388, 389, 390, 393
2357, 2358, 2360, 2361, 2362, 2363, 2364, 2365, 2366, 2367
2368, 2369, 2370, 2371, 2372, 2373, 2374, 2375, 2376, 2377
2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2386, 2387
2388, 2389, 2390, 2393

How to create multiple Conv3DLSTMCell in tensorflow

I'm trying to create my model with several conv3d lstm cell layers:
I run the following code:
conv1, state1 = conv3d('conv1', _X, [8,112,112,1], [3,3,3], 64)
pool1 = max_pool('pool1', conv1, k=1)
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
pool2 = max_pool('pool2', conv2, k=2)
The conv3d functions:
def conv3d(myname, l_input, shape, kernel, outchan):
cell = contrib_rnn_cell.Conv3DLSTMCell(input_shape=shape, output_channels=out$
hidden = cell.zero_state(array_ops.shape(l_input)[0], dtypes.float32)
output, state = cell(l_input, hidden)
print(output.shape)
return output, state
My code runs OK for the conv1 and pool1 but for conv2 layer it shows me an error:
Traceback (most recent call last):
File "conv3dlstm.py", line 272, in <module>
run(16)
File "conv3dlstm.py", line 199, in run
biases)
File "/home/user/projects/model_conv3dlstm.py", line 47, in inference_c3d
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 190, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 696, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1297, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1093, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 431, in get_variable
return custom_getter(**custom_getter_kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 193, in _rnn_get_variable
variable = getter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 408, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 747, in _get_single_variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable conv_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
I saw the code in run_cell.py at line 2200 which is:
kernel = vs.get_variable(
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
Which is getting variable with fixed name "kernel". If I understand it correctly, it is supposed to be unique. But it means I can't create more of Conv3DLSTMCells than one? Is it a bug or am I using it incorrectly?

InvalidArgumentError : You must feed a value for placeholder tensor 'input_1/X' with dtype float

I am new to tensorflow and tflearn and getting this error while training the model.
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1/X' with dtype float
[[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Here is my code for preference.
X = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Y = np.array([i[1] for i in train])
test_x = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
test_y = np.array([i[1] for i in test])
where train and test are numpy arrays having first element as image and second element as label. I am trying to fit my model by this line.
model.fit({'input': X}, {'targets': Y}, n_epoch=5, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
This is the complete error that I am getting:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-34-cf830d06009d> in <module>()
----> 1 model.fit({'input': X}, {'targets': Y}, n_epoch=5, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.pyc in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
213 excl_trainops=excl_trainops,
214 run_id=run_id,
--> 215 callbacks=callbacks)
216
217 def predict(self, X):
/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.pyc in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
331 (bool(self.best_checkpoint_path) | snapshot_epoch),
332 snapshot_step,
--> 333 show_metric)
334
335 # Update training state
/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.pyc in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
772 tflearn.is_training(True, session=self.session)
773 _, train_summ_str = self.session.run([self.train, self.summ_op],
--> 774 feed_batch)
775
776 # Retrieve loss value from summary string
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
776 try:
777 result = self._run(None, fetches, feed_dict, options_ptr,
--> 778 run_metadata_ptr)
779 if run_metadata:
780 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
980 if final_fetches or final_targets:
981 results = self._do_run(handle, final_targets, final_fetches,
--> 982 feed_dict_string, options, run_metadata)
983 else:
984 results = []
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1030 if handle is None:
1031 return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1032 target_list, options, run_metadata)
1033 else:
1034 return self._do_call(_prun_fn, self._session, handle, feed_dict,
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
1050 except KeyError:
1051 pass
-> 1052 raise type(e)(node_def, op, message)
1053
1054 def _extend_graph(self):
InvalidArgumentError: You must feed a value for placeholder tensor 'input_1/X' with dtype float
[[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'input_1/X', defined at:
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/kernelapp.py", line 469, in main
app.start()
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/kernelapp.py", line 459, in start
ioloop.IOLoop.instance().start()
File "/usr/lib/python2.7/dist-packages/zmq/eventloop/ioloop.py", line 162, in start
super(ZMQIOLoop, self).start()
File "/usr/lib/python2.7/dist-packages/tornado/ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "/usr/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.py", line 281, in dispatcher
return self.dispatch_shell(stream, msg)
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.py", line 245, in dispatch_shell
handler(stream, idents, msg)
File "/usr/lib/python2.7/dist-packages/IPython/kernel/zmq/ipkernel.py", line 389, in execute_request
shell.run_cell(code, store_history=store_history, silent=silent)
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2741, in run_cell
interactivity=interactivity, compiler=compiler)
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-14-fe1453e052a7>", line 6, in <module>
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
File "/usr/local/lib/python2.7/dist-packages/tflearn/layers/core.py", line 81, in input_data
placeholder = tf.placeholder(shape=shape, dtype=dtype, name="X")
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'input_1/X' with dtype float
[[Node: input_1/X = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
add dtype=np.float32 (or float64) to declare the type as float.
X = np.array([i[0] for i in train], dtype=np.float32 ).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
Y = np.array([i[1] for i in train], dtype=np.float32 )
test_x = np.array([i[0] for i in test], dtype=np.float32 ).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
test_y = np.array([i[1] for i in test], dtype=np.float32 )

Keras Reshape Error in Flatten Operation at Runtime

I have a problem using keras with either backend (tf or theano).
When I execute my model which is defined like this:
model = Sequential()
model.add(Conv1D(filters=2, kernel_size=15, padding="valid", input_shape=(training_feature_maps.shape[1], training_feature_maps.shape[2]), kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dropout(rate=0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(128, kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))
print model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(training_feature_maps, y_train, batch_size=100, epochs=50, validation_data=(testing_feature_maps, y_test), shuffle=True)
The input feature maps have shapes (~26000, 1050, 4) and (~8000, 1050, 4).
The summary is printed and looks good:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dropout_1 (Dropout) (None, 1050, 4) 0
_________________________________________________________________
conv1d_1 (Conv1D) (None, 1036, 2) 122
_________________________________________________________________
activation_1 (Activation) (None, 1036, 2) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 1036, 2) 0
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 518, 2) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 1036) 0
_________________________________________________________________
dense_1 (Dense) (None, 128) 132736
_________________________________________________________________
activation_2 (Activation) (None, 128) 0
_________________________________________________________________
dense_2 (Dense) (None, 2) 258
_________________________________________________________________
activation_3 (Activation) (None, 2) 0
=================================================================
Total params: 133,116.0
Trainable params: 133,116.0
Non-trainable params: 0.0
_________________________________________________________________
But during run-time, I get the following error using Tensorflow as backend:
Caused by op u'flatten_1/Reshape', defined at:
File "load_data.py", line 598, in <module>
model.add(Flatten())
File "/home/sasse/.local/lib/python2.7/site-packages/keras/models.py", line 455, in add
output_tensor = layer(self.outputs[0])
File "/home/sasse/.local/lib/python2.7/site-packages/keras/engine/topology.py", line 554, in __call__
output = self.call(inputs, **kwargs)
File "/home/sasse/.local/lib/python2.7/site-packages/keras/layers/core.py", line 495, in call
return K.batch_flatten(inputs)
File "/home/sasse/.local/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1744, in batch_flatten
x = tf.reshape(x, tf.stack([-1, prod(shape(x)[1:])]))
File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2630, in reshape
name=name)
File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op
op_def=op_def)
File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/sasse/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): size 1 must be non-negative, not -31193103
[[Node: flatten_1/Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](max_pooling1d_1/Squeeze, flatten_1/stack)]]
[[Node: mul_2/_45 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_870_mul_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
When using theano instead, I get the following error:
RuntimeError: c_extract: Some CudaNdarray has dim 2 on broadcastable dimension 2
Apply node that caused the error: GPU_mrg_uniform{CudaNdarrayType(float32, 3D),inplace}(<CudaNdarrayType(float32, vector)>, MakeVector{dtype='int64'}.0)
Toposort index: 52
Inputs types: [CudaNdarrayType(float32, vector), TensorType(int64, vector)]
Inputs shapes: [(92160,), (3,)]
Inputs strides: [(1,), (8,)]
Inputs values: ['not shown', array([ 100, 1036, 2])]
Outputs clients: [['output'], [GpuElemwise{Composite{Cast{float32}(LT(i0, i1))}}[(0, 0)](GPU_mrg_uniform{CudaNdarrayType(float32, 3D),inplace}.1, CudaNdarrayConstant{[[[ 0.5]]]})]
I'm running on a Titan X GPU with CUDA 8.0.44 and CuDNN 5105.
Strangely enough, all works fine when I use only one filter in the convolution.
Do you have any idea, why this is happening?
Best regards,
Roman

Creating correct for-loop/iteration for a unique list

I have a list of the numbers 1,2,3 and 4.
I wish to print them out in the following manner:
1
2
3
4
11
12
13
14
21
22
23
24
31
..and so on.
How is it possible to do?
Thanks
from itertools import product
maximumDigits = 2
digits = '1234'
for l in range(1, maximumDigits + 1):
for n in product(digits, repeat=l):
print(''.join(n))
Gives you:
1
2
3
4
11
12
13
14
21
22
23
24
31
32
33
34
41
42
43
44
Non-itertools solution:
>>> digits = (1, 2, 3, 4)
>>> nums = newNums = list(digits)
# calculate 2-digit numbers
>>> newNums = [n * 10 + m for n in newNums for m in digits]
>>> nums.extend(newNums)
>>> nums
[1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44]
# calculate 3-digit numbers
>>> newNums = [n * 10 + m for n in newNums for m in digits]
>>> nums.extend(newNums)
>>> nums
[1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44, 111, 112, 113, 114, 121, 122, 123, 124, 131, 132, 133, 134, 141, 142, 143, 144, 211, 212, 213, 214, 221, 222, 223, 224, 231, 232, 233, 234, 241, 242, 243, 244, 311, 312, 313, 314, 321, 322, 323, 324, 331, 332, 333, 334, 341, 342, 343, 344, 411, 412, 413, 414, 421, 422, 423, 424, 431, 432, 433, 434, 441, 442, 443, 444]
# this repeats for each new digit you want