代码未执行,但也未显示任何错误

时间:2019-07-17 06:25:26

标签: python machine-learning keras computer-vision

我正在开发一种机器学习算法,我的代码如下:

图像目录

train_dir = '../input/train_images'
train_labels = pd.read_csv('../input/train.csv')
train_labels['diagnosis'] = train_labels['diagnosis'].astype(str)
train_labels["id_code"]=train_labels["id_code"].apply(lambda x:x+".png")


test_dir = '../input/test_images'
test_labels = '../input/test.csv'

预处理

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255,)

train_generator = train_datagen.flow_from_dataframe(
    train_labels[:], 
    directory="../input/train_images", 
    x_col='id_code', y_col='diagnosis', 
    target_size=(150, 150), 
    color_mode='rgb', 
    class_mode='categorical', 
    batch_size=32, 
    shuffle=True,)

模型

def get_model():
    model = models.Sequential()
    model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(150,150,3)))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(64, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))

    model.add(layers.Flatten())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(512, activation='relu'))
    model.add(layers.Dense(5, activation='softmax'))

    #Compile your model
    model.compile(loss='categorical_crossentropy',
                optimizer=optimizers.Adam(),
                metrics=['acc'])

    return model

使用k交叉验证对模型进行训练

k = 4
num_validation_samples = len(train_generator) // k
np.random.shuffle(train_generator)

validation_scores = []
for fold in range(k):
    print('processing fold #:', fold)
    validation_data = train_generator[num_validation_samples * fold: num_validation_samples * (fold + 1)]
    validation_targets = train_labels[num_validation_samples * fold: num_validation_samples * (fold + 1)]

    training_data = np.concatenate([train_generator[:num_validation_samples * fold], train_generator[num_validation_samples * (fold + 1) : ]], axis = 0)  
    training_targets = np.concatenate([train_labels[:num_validation_samples * fold], train_labels[num_validation_samples * (fold + 1) :]], axis = 0)

    model = get_model()

    #Run the model
    model.fit_generator(
        training_data,
        training_targets,
        steps_per_epoch=30, 
        epochs=30, 
        batch_size = 20,
        verbose = 0)

    #Validate the model
    val_loss, val_acc = model.evaluate(validation_data, validation_targets, verbose=0)
    validation_scores.append(val_loss)

除了对模型部分的训练之外,此代码的每个部分都起作用。它显示了正在执行的执行符号,但从未执行。我已经等了几个小时,但是什么也没发生。我不懂为什么。我将感谢您的帮助。

0 个答案:

没有答案