gapml / CV

Apache License 2.0
10 stars 5 forks source link

Problem of dimensions #14

Closed Divyanshupy closed 4 years ago

Divyanshupy commented 5 years ago

@Stancoo Hello, so I was trying to use gapcv in order to split my data into train and validation split so that i can easily apply Image Augmentation. I am using gray parameter in the config list in order to convert the images to grayscale and the shape of the output images obtained is of the shape(batch_size, height, width) but the model expects 4d shape i.e(batch_size, height, width,1). Is there any way to expand_dims for all images.

Divyanshupy commented 5 years ago
import gapcv
from gapcv.vision import Images
from gapcv.utils.img_tools import ImgUtils
images = Images('SignLanguage2', train_dir, config=['resize=(128,128)', 'gray', 'store', 'stream'])

images = Images(config=['stream'])
images.load('SignLanguage2')

images.split = 0.2
X_test,Y_test = images.test
images.minibatch = 128
gap_generator = images.minibatch
total_train_images = images.count-len(X_test)
n_classes = len(images.classes)
images = next(iter(gap_generator))
history=model.fit_generator(generator=gap_generator,
                           steps_per_epoch = total_train_images//128,
                           epochs=25
                           validation_data=(X_test,Y_test),
                           verbose=1)
gangelou commented 5 years ago

Hi all, I had the same problem too. The main issue I think comes from the hd5 library.

Perhaps the issue is manifested in the source code vision.py in

def _init_stream_hdf5(self, name, nelem):

where the shape is set but I'm not 100% sure.

I implemented a quick dirty hack for the time being. First I reshape my X_test data in my code: For Example:

images.split = 0.2
X_test,Y_test = images.test
X_test = X_test.reshape(128,128,1)

Here we need to force a color channel dimension, which doesn't work when we use grayscale.
(This can be set in a more clever and general way)

Next we have to deal with the minibatches In the actual source code for this library vision.py I went to

    @property
    def minibatch(self):

and before I return

yield np.asarray(x_batch), self._one_hot(np.asarray(y_batch), self._nlabels)

I reshape np.asarray(x_batch) so that it too has a dimension for the color channel. here a hacky example would be something like:

aa=np.asarray(x_batch).shape
xb= np.asarray(x_batch).reshape(aa[0],aa[1],aa[2],1)
yield xb, self._one_hot(yb, self._nlabels) 

Yet to be determined whether these changes actually break anything in terms of training my model.

virtualdvid commented 5 years ago

Hi guys, I'm really sorry for my late replay. I've been so busy at work. I worked for a couple of days coming up with an implementation to solve the missed feature.

There is a new config gray-expand_dim. You just have to add it to the list and that's it.

To solve your issue try:

from gapcv.vision import Images

images = Images('SignLanguage2', train_dir, config=['resize=(128,128)', 'gray-expand_dim', 'stream'])

images = Images(config=['stream'])
images.load('SignLanguage2')

images.split = 0.2
X_test,Y_test = images.test
images.minibatch = 128
gap_generator = images.minibatch
total_train_images = images.count-len(X_test)
n_classes = len(images.classes)

history=model.fit_generator(generator=gap_generator,
    steps_per_epoch = total_train_images//128,
    epochs=25
    validation_data=(X_test,Y_test),
    verbose=1)

Here I have a playground where I play on kaggle with gapcv and TF2.0.

Let me know if this works for you.

My apologies again Andy has been busy as well writing his book and the crazy work at Google AI. I'm a lonely wolf in this project while he is free again. I'll be happy if you give me a hand. Thanks for trying gapcv! :)