i have developed keras model , trying fit data model. here code model.fit
def train(run_name, start_epoch, stop_epoch, img_w): # input parameters img_h = 64 words_per_epoch = 300 val_split = 0.2 val_words = int(words_per_epoch * (val_split)) # network parameters conv_filters = 16 kernel_size = (3, 3) pool_size = 2 time_dense_size = 32 rnn_size = 256 minibatch_size = 32 if k.image_data_format() == 'channels_first': input_shape = (1, img_w, img_h) else: input_shape = (img_w, img_h, 1) act = 'relu' input_data = input(name='the_input', shape=input_shape, dtype='float32') inner = conv2d(conv_filters, kernel_size, padding='same', activation=act, kernel_initializer='he_normal', name='conv1')(input_data) inner = maxpooling2d(pool_size=(pool_size, pool_size), name='max1')(inner) inner = conv2d(conv_filters, kernel_size, padding='same', activation=act, kernel_initializer='he_normal', name='conv2')(inner) inner = maxpooling2d(pool_size=(pool_size, pool_size), name='max2')(inner) conv_to_rnn_dims = (img_w // (pool_size ** 2), (img_h // (pool_size ** 2)) * conv_filters) inner = reshape(target_shape=conv_to_rnn_dims, name='reshape')(inner) # cuts down input size going rnn: inner = dense(time_dense_size, activation=act, name='dense1')(inner) # 2 layers of bidirectional grus # gru seems work well, if not better lstm: gru_1 = gru(rnn_size, return_sequences=true, kernel_initializer='he_normal', name='gru1')(inner) gru_1b = gru(rnn_size, return_sequences=true, go_backwards=true, kernel_initializer='he_normal', name='gru1_b')(inner) gru1_merged = add([gru_1, gru_1b]) gru_2 = gru(rnn_size, return_sequences=true, kernel_initializer='he_normal', name='gru2')(gru1_merged) gru_2b = gru(rnn_size, return_sequences=true, go_backwards=true, kernel_initializer='he_normal', name='gru2_b')(gru1_merged) # transforms rnn output character activations: # print("output size",img_gen.get_output_size()) inner = dense(47, kernel_initializer='he_normal', name='dense2')(concatenate([gru_2, gru_2b])) y_pred = activation('softmax', name='softmax')(inner) # model(inputs=input_data, outputs=y_pred).summary() labels = input(name='the_labels', shape=[10], dtype='float32') input_length = input(name='input_length', shape=[1], dtype='int64') label_length = input(name='label_length', shape=[1], dtype='int64') # keras doesn't support loss funcs parameters # ctc loss implemented in lambda layer loss_out = lambda(ctc_lambda_func, output_shape=(1,), name='ctc')([y_pred, labels, input_length, label_length]) # clipnorm seems speeds convergence sgd = sgd(lr=0.02, decay=1e-6, momentum=0.9, nesterov=true, clipnorm=5) model = model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out) # loss calc occurs elsewhere, use dummy lambda func loss model.compile(loss={'ctc': lambda y_true, y_pred: y_pred}, optimizer=sgd) test_func = k.function([input_data], [y_pred]) # viz_cb = vizcallback(run_name, test_func, img_gen.next_val()) (x_train, y_train, train_input_length, train_labels_length), (x_test, y_test, test_input_length, test_labels_length) = dataset_load('./ocr_bangladata.pkl.gz') print(y_train[0]) x_train = x_train.reshape(x_train.shape[0], 128,64,1) x_test = x_test.reshape(x_test.shape[0], 128,64,1) x_train = x_train.astype('float32') x_test = x_test.astype('float32') model.fit((np.array(x_train), np.array(y_train),np.array(train_input_length), np.array(train_labels_length)), batch_size=32, epochs=120, verbose=1,validation_data=[np.array(x_test), np.array(y_test),np.array(test_input_length), np.array(test_labels_length)])
after getting error
typeerror: error when checking model input: data should numpy array, or list/dict of numpy arrays.
i tried print data types of each array. result
<type 'numpy.ndarray'>
but still getting error. there specific reason it? using tensorflow model backend of keras.
here have 4 inputs:
model = model(inputs=[input_data, labels, input_length, label_length], outputs=loss_out)
each of 4 inputs must numpy array, , fit method wants them in list (you're using tuple):
model.fit([x_train, y_train,train_input_length, train_labels_length],...)
and you're missing outputs in fit
method. must match defined loss_out
when creating model.
Comments
Post a Comment