kingfengji / gcForest

This is the official implementation for the paper 'Deep forest: Towards an alternative to deep neural networks'
http://lamda.nju.edu.cn/code_gcForest.ashx
1.31k stars 427 forks source link

can't not run with python3 #40

Open Green-li opened 6 years ago

Green-li commented 6 years ago

Environment : python3.6, Centos 7.

code in lib/gcforest/layers/fg_pool_layer.py:

class FGPoolLayer(BaseLayer):
    def __init__(self, layer_config, data_cache):
        """
        Pooling Layer (MaxPooling, AveragePooling)
        """
        super(FGPoolLayer, self).__init__(layer_config, data_cache)
        self.win_x = self.get_value("win_x", None, int, required=True)
        self.win_y = self.get_value("win_y", None, int, required=True)
        self.pool_method = self.get_value("pool_method", "avg", basestring)
.....

while run using the config examples/demo_mnist-gc.json, it has an error:

Traceback (most recent call last):
  File "examples/demo_mnist.py", line 55, in <module>
    gc = GCForest(config)
  File "lib/gcforest/gcforest.py", line 16, in __init__
    self.fg = FGNet(self.config["net"], self.train_config.data_cache)
  File "lib/gcforest/fgnet.py", line 36, in __init__
    layer = get_layer(layer_config, self.data_cache)
  File "lib/gcforest/layers/__init__.py", line 32, in get_layer
    layer = layer_class(layer_config, data_cache)
  File "lib/gcforest/layers/fg_pool_layer.py", line 27, in __init__
    self.pool_method = self.get_value("pool_method", "avg", basestring)
NameError: name 'basestring' is not defined

the basestring function is no longer used in python3.

kingfengji commented 6 years ago

change basestring to six.string_types (and remember to import six) if you are using py3.5

Green-li commented 6 years ago

@kingfengji thanks!but another error occurs:

Traceback (most recent call last):
  File "examples/demo_mnist.py", line 66, in <module>
    X_train_enc = gc.fit_transform(X_train, y_train)
  File "lib/gcforest/gcforest.py", line 31, in fit_transform
    self.fg.fit_transform(X_train, y_train, X_test, y_test, train_config)
  File "lib/gcforest/fgnet.py", line 54, in fit_transform
    layer.fit_transform(train_config)
  File "lib/gcforest/layers/fg_win_layer.py", line 92, in fit_transform
    X_win = get_windows(X, self.win_x, self.win_y, self.stride_x, self.stride_y, self.pad_x, self.pad_y)
  File "lib/gcforest/utils/win_utils.py", line 48, in get_windows
    X_win = np.empty(( nc, n * nh * nw), dtype=np.float32)
TypeError: 'float' object cannot be interpreted as an integer

I change the code in line42,43 in win_utils.py,it doesn't work.

# from / to //
nh = (h - win_y) // stride_y + 1 
nw = (w - win_x) // stride_x + 1
hierongiacomini commented 6 years ago

I got the same errors so:

  1. Get Python 2.7.

  2. Clone gcforest folder from "\lib\gcforest" to your Python 2.7 folder "\Lib\site-packages\gcforest".

  3. Install all dependences less Keras(Tensorflow). Maybe if you need, download xgboost from https://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost in windows systems, install with pip install your-package_name.whl run from the directory of the file .whl downloaded.

  4. In demo_mnist.py remove import of keras to get mnist.

  5. Get MNIST from elsewhere, need be np.array of shape (samples,28,28) for X_train and X_test.

or

  1. pip install python-mnist Download mnist dataset from http://yann.lecun.com/exdb/mnist/ (4 files in red links .gz) and put in some directory, I recomend in your Python 2.7 folder "\Lib\site-packages\mnist\MNIST" (need create folder MNIST).

    Open your Python 2.7 folder "\Lib\site-packages\mnist\loader.py". Localize:

    class MNIST(object):
        def __init__(self, path=' ', mode='vanilla', return_type='lists', gz=False):"_

Change to, with "YOUR PYTHON 2.7 PATH" or set path to mnist dataset .gz files folder path

    class MNIST(object):
        def __init__(self, path='YOUR PYTHON 2.7 PATH\Lib\site-packages\mnist\MNIST', mode='vanilla', return_type='numpy', gz=True):"_

Create a new function inside class MNIST(Caution with "tabs" and "spaces" behind rows):

    def load_data(self):
        ims, labels = self.load(os.path.join(self.path, self.train_img_fname),
                            os.path.join(self.path, self.train_lbl_fname))
        self.train_images = self.process_images(ims).reshape(60000,28,28)
        self.train_labels = self.process_labels(labels)

        ims, labels = self.load(os.path.join(self.path, self.test_img_fname),
                            os.path.join(self.path, self.test_lbl_fname))

        self.test_images = self.process_images(ims).reshape(10000,28,28)
        self.test_labels = self.process_labels(labels)

    return self.train_images, self.train_labels, self.test_images, self.test_labels
  1. Inside demo_mnist.py remove:

    from keras.datasets import mnist

    Insert:

    from mnist import MNIST

    Remove:

    (X_train, y_train), (X_test, y_test) = mnist.load_data()

    Insert:

    mndata = MNIST()
    X_train, y_train, X_test, y_test = mndata.load_data()
  2. Run the demo from the example folder demo_mnist.py or demo_mnist.py --model demo_mnist-gc.json

madarax64 commented 6 years ago

Hello @Green-li , I'm not sure if you've found a solution yet. I was able to make a few modifications which resolved the issue in win_utils.py - I hope @kingfengji can confirm they don't mess anything up too much?

Specifically in the win_utils.py file, I made four modifications based on the observed error messages. Just below line 18, you may consider adding the following two lines:

k = int(k) di = int(di)

Specifically, we're typecasting the k and di variables to ints.

Next, inside the get_windows() method, locate the following two lines: nh = (h - win_y) / stride_y + 1 nw = (w - win_x) / stride_x + 1

And add the following two lines below them:

nh = int(nh) nw = int(nw)

Which are just more typecasts. This seemed to fix the issue for me, and I was able to use gcForest in both Cascade and Multigrained Scanning mode.

Big ups to @kingfengji . I hope my modifications aren't too troublesome though.

Please update this thread with the results of these modifications on your code. It'd be nice to know if they work for you too.

Regards, M.

marooncn commented 6 years ago

Added @madarax64 , also modify fg_pool_layer.py, add nh = int(nh) nw = int(nw) below nh = (h - 1) / win_y + 1 nw = (w - 1) / win_x + 1

Tessic commented 4 years ago

it worked @marooncn @madarax64. but another error occurs:

Traceback (most recent call last): 
  File "/examples/demo_mnist.py", line 77, in <module>
    y_pred = gc.predict(X_test)
  File "\lib\gcforest\gcforest.py", line 61, in predict
    y_proba = self.predict_proba(X)
  File "\lib\gcforest\gcforest.py", line 56, in predict_proba
    X = self.fg.transform(X)
  File "\lib\gcforest\fgnet.py", line 68, in transform
    layer.transform()
  File "\lib\gcforest\layers\fg_win_layer.py", line 130, in transform
    y_proba = est.predict_proba(X_win)
AttributeError: 'NoneType' object has no attribute 'predict_proba'

it seems like that can't find the self.estimator1d[ti] because I set the keep_model_in_mem false. how can i solve this? @kingfengji

Fanersama commented 2 years ago

it worked @marooncn @madarax64. but another error occurs:

Traceback (most recent call last): 
  File "/examples/demo_mnist.py", line 77, in <module>
    y_pred = gc.predict(X_test)
  File "\lib\gcforest\gcforest.py", line 61, in predict
    y_proba = self.predict_proba(X)
  File "\lib\gcforest\gcforest.py", line 56, in predict_proba
    X = self.fg.transform(X)
  File "\lib\gcforest\fgnet.py", line 68, in transform
    layer.transform()
  File "\lib\gcforest\layers\fg_win_layer.py", line 130, in transform
    y_proba = est.predict_proba(X_win)
AttributeError: 'NoneType' object has no attribute 'predict_proba'

it seems like that can't find the self.estimator1d[ti] because I set the keep_model_in_mem false. how can i solve this? @Tessic @kingfengji 我也遇到了同样的问题,请问您解决了吗? I meet the same question, how did you deal with this ? very thanks