3
votes

I am trying to build a CNN using tflearn using the following code

train_images, train_labels, test_images, test_labels = load_dataset()
convnet = input_data(shape=[None, 28, 28, 1], name='input')
convnet = conv_2d(convnet, 32, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = conv_2d(convnet, 64, 2, activation='relu')
convnet = max_pool_2d(convnet, 2)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 24, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=0.01, loss='categorical_crossentropy')

model = tflearn.DNN(convnet)
model.fit(train_images, train_labels, n_epoch=30,
          validation_set=(test_images,  test_labels),
          snapshot_step=500, show_metric=True, run_id='characterOCR')
model.save('CNN.model')

The dataset was reshaped to the following shape

def load_mnist_images(filename):
    with gzip.open(filename, 'rb') as f:
        data = np.frombuffer(f.read(), np.uint8, offset=16)
        data = data.reshape(-1, 28, 28, 1)
        return data

It's my dataset but I built it based on the MNIST structure, now I am getting the following error :

Traceback (most recent call last):
  File "/home/hassan/JPG-PNG-to-MNIST-NN-Format/CNN_network.py", line 56, in <module>
    snapshot_step=500, show_metric=True, run_id='characterOCR')
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/models/dnn.py", line 216, in fit
    callbacks=callbacks)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/helpers/trainer.py", line 339, in fit
    show_metric)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tflearn/helpers/trainer.py", line 818, in _train
    feed_batch)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1149, in _run
    str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (64,) for Tensor 'TargetsData/Y:0', which has shape '(?, 24)'

Does anyone have a solution?

1

1 Answers

0
votes

well for tflearn I didn't solve the problem all I know that the error coming from that I feed the fully connected layer shape 24 a data with shape of 64 I don't know where does this 64 come from

but I switched from tflearn to Keras API and now it's working

if anyone want to know the source code please informe me