alok-ai-lab / pyDeepInsight

A python implementation of the DeepInsight methodology.
GNU General Public License v3.0
158 stars 47 forks source link

Error in `transform` #44

Closed shibataryohei closed 5 months ago

shibataryohei commented 5 months ago

Hi.

I'm the one who opened issue #39. Thanks for addressing it! Updated Python in Google Colab to 3.11 and it fixed the issue, but now I'm getting errors in code that used to run fine.

reducer = TSNE(n_components = 2,
               metric = 'cosine',
               init = 'random',
               learning_rate = 'auto',
               n_jobs = -1,
               verbose = True,
               random_state = 42)

pixel_size = (227, 227)
it = ImageTransformer(feature_extractor = reducer, # reducer, tSNE
                      pixels = pixel_size)

it.fit(X_train_sm,
       y = y_train_sm,
       plot = True)

X_train_img = it.transform(X_train_sm)

The error is

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py](https://localhost:8080/#) in get_loc(self, key)
   3652         try:
-> 3653             return self._engine.get_loc(casted_key)
   3654         except KeyError as err:

6 frames
/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

/usr/local/lib/python3.10/dist-packages/pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '(slice(None, None, None), array([2955]))' is an invalid key

During handling of the above exception, another exception occurred:

InvalidIndexError                         Traceback (most recent call last)
[<ipython-input-19-46862b2ad71d>](https://localhost:8080/#) in <cell line: 1>()
----> 1 X_train_img = it.transform(X_train_sm)

[/content/pyDeepInsight/pyDeepInsight/image_transformer.py](https://localhost:8080/#) in transform(self, X, img_format, empty_value)
    343             img_matrix[:] = empty_value
    344         for i, c in enumerate(unq):
--> 345             img_matrix[:, c[0], c[1]] = X[:, np.where(idx == i)[0]].mean(axis=1)
    346 
    347         if img_format == 'rgb':

[/usr/local/lib/python3.10/dist-packages/pandas/core/frame.py](https://localhost:8080/#) in __getitem__(self, key)
   3759             if self.columns.nlevels > 1:
   3760                 return self._getitem_multilevel(key)
-> 3761             indexer = self.columns.get_loc(key)
   3762             if is_integer(indexer):
   3763                 indexer = [indexer]

[/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py](https://localhost:8080/#) in get_loc(self, key)
   3658             #  InvalidIndexError. Otherwise we fall through and re-raise
   3659             #  the TypeError.
-> 3660             self._check_indexing_error(key)
   3661             raise
   3662 

[/usr/local/lib/python3.10/dist-packages/pandas/core/indexes/base.py](https://localhost:8080/#) in _check_indexing_error(self, key)
   5735             # if key is not a scalar, directly raise an error (the code below
   5736             # would convert to numpy arrays and raise later any way) - GH29926
-> 5737             raise InvalidIndexError(key)
   5738 
   5739     @cache_readonly

InvalidIndexError: (slice(None, None, None), array([2955]))

X_train_sm is a matrix with (442, 3656) and the data looks good. y_train_sm is a vector with 442.

Could the recent pyDeepInsight update be related to this error? If there's a fix, please let me know.

Best, Ryohei

kaboroevich commented 5 months ago

I believe I know the issue. You are providing a pandas DataFrame to ImageTransformer when it should be a numpy array.

Change the line

it.transform(X_train_sm)

to

it.transform(X_train_sm.to_numpy())

Or convert it beforehand.

shibataryohei commented 5 months ago

Hi Kaboroevich,

That works well! Thank you so much for your kindness.