2015-04-17 15 views
5

Pracuję nad zadaniami klasyfikacji obrazu i zdecydowałem się użyć Lasagne + Nolearn do prototypu sieci neuronowych. Wszystkie standardowe przykłady, takie jak klasyfikacja numerów MNIST, działają dobrze, ale problemy pojawiają się, gdy próbuję pracować z własnymi obrazami.Wprowadzanie obrazu z sieci neuronowej Theano/Lasagne/Nolearn

Chcę użyć obrazów 3-kanałowych, a nie w skali szarości. A tam gdzie jest kod Próbuję dostać tablic z obrazami:

img = Image.open(item) 
img = ImageOps.fit(img, (256, 256), Image.ANTIALIAS) 
img = np.asarray(img, dtype = 'float64')/255. 
img = img.transpose(2,0,1).reshape(3, 256, 256) 
X.append(img) 

Oto kod z NN i jego zabudowy:

X, y = simple_load("new") 

X = np.array(X) 
y = np.array(y) 


net1 = NeuralNet(
    layers=[ # three layers: one hidden layer 
     ('input', layers.InputLayer), 
     ('hidden', layers.DenseLayer), 
     ('output', layers.DenseLayer), 
     ], 
    # layer parameters: 
    input_shape=(None, 65536), # 96x96 input pixels per batch 
    hidden_num_units=100, # number of units in hidden layer 
    output_nonlinearity=None, # output layer uses identity function 
    output_num_units=len(y), # 30 target values 

    # optimization method: 
    update=nesterov_momentum, 
    update_learning_rate=0.01, 
    update_momentum=0.9, 

    regression=True, # flag to indicate we're dealing with regression problem 


     max_epochs=400, # we want to train this many epochs 
     verbose=1, 
     ) 

    net1.fit(X, y) 

I odbieranie wyjątki takie jak ten:

Traceback (most recent call last): 
    File "las_mnist.py", line 39, in <module> 
    net1.fit(X[i], y[i]) 
    File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 266, in fit 
    self.train_loop(X, y) 
    File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 273, in train_loop 
    X, y, self.eval_size) 
    File "/usr/local/lib/python2.7/dist-packages/nolearn/lasagne.py", line 377, in train_test_split 
    kf = KFold(y.shape[0], round(1./eval_size)) 
IndexError: tuple index out of range 

W jakim formacie "karmisz" swoje sieci danymi obrazu? Dzięki za odpowiedzi lub wskazówki!

Odpowiedz

5

Jeśli robisz klasyfikacji trzeba zmodyfikować kilka rzeczy:

  1. W swoim kodzie ustawiono regression = True. Aby zrobić klasyfikację, usuń ten wiersz.
  2. Upewnij się, że kształt wejścia dopasowuje kształt X, jeśli chcą wejść 3 różnych kanałów
  3. Ponieważ robisz klasyfikacji trzeba wyjście użyć Softmax nieliniowość (w tej chwili masz tożsamość, która nie pomoże Ci z klasyfikacją)

    X, y = simple_load("new") 
    
    X = np.array(X) 
    y = np.array(y) 
    
    net1 = NeuralNet(
        layers=[ # three layers: one hidden layer 
         ('input', layers.InputLayer), 
         ('hidden', layers.DenseLayer), 
         ('output', layers.DenseLayer), 
         ], 
        # layer parameters: 
        input_shape=(None, 3, 256, 256), # TODO: change this 
        hidden_num_units=100, # number of units in hidden layer 
        output_nonlinearity=lasagne.nonlinearities.softmax, # TODO: change this 
        output_num_units=len(y), # 30 target values 
    
        # optimization method: 
        update=nesterov_momentum, 
        update_learning_rate=0.01, 
        update_momentum=0.9, 
    
        max_epochs=400, # we want to train this many epochs 
        verbose=1, 
    

    )

Powiązane problemy