Keras congela ao usar ImageDataGenerator

votos
6

Por alguma razão quando eu uso um ImageDataGenerator com keras ele congela quando eu começar o ajuste. Eu recebo a seguinte saída. Ele simplesmente trava na linhaEpoch 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

Isso mostra que uma das minhas núcleos de CPU está sendo executado em 100% assim que algo está acontecendo na cpu mesmo, embora ele deve estar usando a minha GPU para ajustar os dados. O código abaixo funciona se eu comente a fit_generator e utilizar a função de ajuste.

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))

Também aqui é o meu carregador de minst se alguém quiser tentar executá-lo ...

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:]]
Publicado 07/05/2016 em 22:33
fonte usuário
Em outras línguas...                            


1 respostas

votos
0

Tente verbose=2como um parâmetro emfit_generator()

Respondeu 18/02/2017 em 13:11
fonte usuário

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more