简体   繁体   English

Keras CNN 卡在 CIFAR10 数据集的 epoch 1

[英]Keras CNN stuck on epoch 1 for CIFAR10 datase

I want to train a CNN which classifies the images from the CIFAR10 data base.我想训练一个 CNN 来对 CIFAR10 数据库中的图像进行分类。 Based on previous tasks with Keras the code should work like this基于之前 Keras 的任务,代码应该像这样工作

import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from random import sample
import tensorflow as tf
from tensorflow import keras

cat_dict = {0: 'airplane',
            1: 'automobile',
            2: 'bird',
            3: 'cat',
            4: 'deer',
            5: 'dog',
            6: 'frog',
            7: 'horse',
            8: 'ship',
            9: 'truck'}

def assign_class(val):
    isclass = cat_dict[int(val)]
    return isclass

def show_imgs(X,Y):
    plt.figure(1, figsize=(20,20))
    k = 0
    for i in range(0,5):
        for j in range(0,5):
            plt.subplot2grid((5,5),(i,j))
            plt.imshow(X[k], cmap='gray')
            plt.title(assign_class(Y[k]))
            k = k+1
            plt.axis('off')
    # show the plot
    plt.show()
    
# Load data & split data between train and test sets
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()

show_imgs(x_train, y_train)

print('Shape of training data:', x_train.shape)
print('Shape of test data:', x_test.shape)

# Normalization
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255.0
x_test /= 255.0

#One-hot encode
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

print(x_train.shape[0], 'training samples')
print(x_test.shape[0], 'test samples')
print(y_train.shape[0], 'training label samples')
print(y_test.shape[0], 'test label samples')

E = 5   #epochs
B = 128 #batch size
n_classes = 10

cnn = keras.models.Sequential()

cnn.add(keras.layers.Conv2D(filters = 32, kernel_size = (2,2), input_shape = (32,32,3), padding = 'valid', strides = (1,1) ))
cnn.add(keras.layers.Activation('relu'))
cnn.add(keras.layers.MaxPooling2D(pool_size = (2, 2))) 
cnn.add(keras.layers.Flatten())
cnn.add(keras.layers.Dense(n_classes, input_shape = (3072,), activation='softmax'))

cnn.summary()

cnn.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

log = cnn.fit(x_train, y_train, batch_size = B, epochs = E, validation_data = (x_test, y_test), verbose = 1)

I would expect that the .fit() starts to fit the model but instead the only output I get is "Epoch 1/5 " and nothing else (besides my computer starts to sound like an A747).我希望.fit()开始适合 model 但我得到的唯一 output 是“Epoch 1/5”,没有别的(除了我的电脑开始听起来像 A747)。

Reinstalled a whole new environment and it worked with no problem重新安装了一个全新的环境,它没有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM