使用ImageDataGenerator时Keras冻结

时间:2016-05-07 23:33:22

标签: python theano keras

由于某种原因,当我使用带有keras的ImageDataGenerator时,它会在我开始拟合时冻结。我得到以下输出。它只挂在Epoch 1/5

行上
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%,所以即使它应该使用我的GPU来适应数据,也会在cpu上发生一些事情。如果我注释掉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:]]

1 个答案:

答案 0 :(得分:0)

尝试将distance作为verbose=2

中的参数