2016-05-07 66 views
6

由於某些原因,當我使用帶keras的ImageDataGenerator時,它會在我開始貼合時凍結。我得到以下輸出。它只是掛就行了Epoch 1/5使用ImageDataGenerator時Keras會凍結

Using Theano backend. 
Using gpu device 0: GeForce GTX TITAN (CNMeM is disabled, cuDNN not available) 
Loading Data 
Compiling Model 
Fitting Data 
Epoch 1/5 

它表明我的CPU內核中的一個運行在100%這樣的東西是在CPU發生偶數但應當用我的GPU來擬合數據。如果我註釋掉fit_generator並使用fit函數,代碼如下工作。

import os 
os.environ["THEANO_FLAGS"] = "mode=FAST_RUN,device=gpu,floatX=float32" 
import minst_loader 
import matplotlib.pyplot as plt 
import numpy as np 
from scipy.misc import imrotate 
import random 
from keras.datasets import cifar10 

np.set_printoptions(suppress = True) 

print('Loading Data') 
x_train, y_train = (minst_loader.load_images('/home/chase/Desktop/MINST/train-images.idx3-ubyte'), \ 
         minst_loader.load_labels('/home/chase/Desktop/MINST/train-labels.idx1-ubyte')) 

x_test, y_test = (minst_loader.load_images('/home/chase/Desktop/MINST/t10k-images.idx3-ubyte'), \ 
         minst_loader.load_labels('/home/chase/Desktop/MINST/t10k-labels.idx1-ubyte')) 

for i in range(len(y_train)): 
    v = np.zeros(10) 
    v[y_train[i]] = 1 
    y_train[i] = v 

#  for j in range(8): 
#   x = imrotate(x_train[i], random.random() * 20) 
#   x_train.append(x) 
#   y_train.append(y_train[i]) 


for i in range(len(y_test)): 
    v = np.zeros(10) 
    v[y_test[i]] = 1 
    y_test[i] = v 

x_train = np.array(x_train) 
y_train = np.array(y_train) 
x_test = np.array(x_test) 
y_test = np.array(y_test) 



from keras.models import Sequential 
from keras.layers.core import Dense, Dropout, Flatten 
from keras.layers.noise import GaussianNoise 
from keras.preprocessing.image import ImageDataGenerator 
from keras.optimizers import SGD 
from keras.callbacks import ProgbarLogger, Callback 

datagen = ImageDataGenerator(rotation_range = 20, dim_ordering = 'tf') 

model = Sequential() 
model.add(Flatten(input_shape = (28, 28))) 
model.add(Dense(200, activation = 'tanh')) 
model.add(Dropout(0.5)) 
model.add(Dense(20, activation = 'tanh')) 
model.add(Dense(10, activation = 'softmax')) 

print('Compiling Model') 
sgd = SGD(lr = 0.01, decay = 0.1, momentum = 0.9, nesterov = True) 
model.compile(loss = 'categorical_crossentropy', optimizer = sgd) 
print('Fitting Data') 

#model.fit(x_train, y_train, batch_size = 128, nb_epoch = 400, validation_data = (x_test, y_test)) 
model.fit_generator(datagen.flow(x_train, y_train), samples_per_epoch = len(x_train), nb_epoch = 5) 
def max_index(lst): 
    mi = 0 
    for i in range(1, len(lst)): 
     mi = i if lst[i] > lst[mi] else mi 
    return mi 

result = model.predict(x_test) 
correct = 0 
for y, yt in zip(result, y_test): 
    correct += max_index(y) == max_index(yt) 
print(correct/len(y_test)) 

而且,這裏是我的MINST裝載機如果有人想嘗試運行它...

import struct 
import numpy as np 
import matplotlib.pyplot as plt 

def load_images(images_file): 
    data = None 
    with open(images_file, 'rb') as f: 
     data = f.read() 

    mn, n, h, w = struct.unpack('>4I', data[0:16]) 
    assert(mn == 2051) 
    data = data[16:] 
    images = [] 
    for i in range(n): 
     img = np.array([float(b) for b in data[w * h * i:w * h * (i + 1)]]) 
     img /= 255.0 
     img = np.reshape(img, (w, h)) 
     images.append(img) 
    return images 

def load_labels(labels_file): 
    data = None 
    with open(labels_file, 'rb') as f: 
     data = f.read() 

    mn, n = struct.unpack('>2I', data[0:8]) 
    assert(mn == 2049) 
    return [int(b) for b in data[8:]] 
+0

嘗試verbose=2作爲參數,你確定你的配置設置爲使用GPU?如果您嘗試使用http://deeplearning.net/software/theano/tutorial/using_gpu.html中的「使用GPU測試Theano」腳本,您會得到什麼? – ginge

回答

0

fit_generator()

相關問題