keras model.fit_generator训练完一个epoch之后无法加载训练集怎么处理?

1、在训练神经网络的过程中遇到了训练完一个epoch之后无法继续训练的问题,具体问题截图如下

图片说明

数据生成的代码如下

def GET_DATASET_SHUFFLE(train_x, train_y, batch_size):
    #random.shuffle(X_samples)

    batch_num = int(len(train_x) / batch_size)
    max_len = batch_num * batch_size
    X_samples = np.array(train_x[0:max_len])
    Y_samples = np.array(train_y[0:max_len])

    X_batches = np.split(X_samples, batch_num)
    Y_batches = np.split(Y_samples, batch_num)

    for i in range(batch_num):
        x = np.array(list(map(load_image, X_batches[i])))
        y = np.array(list(map(load_label, Y_batches[i])))
        yield x, y 

想要向各位大神请教一下,刚刚接触这个不是太懂

1个回答

hsuuuu1
hsuuuu1 回复目标工头的搬砖工: 请问如何解决的?
18 天之前 回复
yx331832761
问心思旧 回复目标工头的搬砖工: 请问如何解决的?我是训练十几个epoch后停止,不用生成器就没问题
2 个月之前 回复
eswai
Eswai 回复目标工头的搬砖工: 求问怎么解决的?我也遇到这个问题
3 个月之前 回复
Melon233
目标工头的搬砖工 我昨天解决了
4 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
keras model 训练 train_loss,train_acc再变,但是val_loss,val_test却一直不变,是哪里有问题?

Epoch 1/15 3112/3112 [==============================] - 73s 237ms/step - loss: 8.1257 - acc: 0.4900 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 2/15 3112/3112 [==============================] - 71s 231ms/step - loss: 8.1730 - acc: 0.4929 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 3/15 3112/3112 [==============================] - 72s 232ms/step - loss: 8.1730 - acc: 0.4929 - val_loss: 8.1763 - val_acc: 0.4427 Epoch 4/15 3112/3112 [==============================] - 71s 229ms/step - loss: 7.0495 - acc: 0.5617 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 5/15 3112/3112 [==============================] - 71s 230ms/step - loss: 5.5504 - acc: 0.6549 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 6/15 3112/3112 [==============================] - 71s 230ms/step - loss: 4.9359 - acc: 0.6931 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 7/15 3112/3112 [==============================] - 71s 230ms/step - loss: 4.8969 - acc: 0.6957 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 8/15 3112/3112 [==============================] - 72s 234ms/step - loss: 4.9446 - acc: 0.6925 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 9/15 3112/3112 [==============================] - 71s 231ms/step - loss: 4.5114 - acc: 0.7201 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 10/15 3112/3112 [==============================] - 73s 237ms/step - loss: 4.7944 - acc: 0.7021 - val_loss: 8.1763 - val_acc: 0.4927 Epoch 11/15 3112/3112 [==============================] - 74s 240ms/step - loss: 4.6789 - acc: 0.7095 - val_loss: 8.1763 - val_acc: 0.4927

在学习人脸识别运用keras的过程中出现了问题

本人在尝试学习"http://www.cnblogs.com/neo-T/p/6477378.html" 此博客提供的人脸识别代码 遇到了以下问题,不知该怎么解决 ``` WARNING:tensorflow:Variable *= will be deprecated. Use variable.assign_mul if you want assignment to the variable value or 'x = x * y' if you want a new python Tensor object. Epoch 1/10 Traceback (most recent call last): File "E:/python/python3.64/python代码练习/人脸识别/face_train_use_keras.py", line 189, in <module> model.train(dataset) File "E:/python/python3.64/python代码练习/人脸识别/face_train_use_keras.py", line 179, in train validation_data=(dataset.valid_images, dataset.valid_labels)) File "E:\python\anaconda\anaconda\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "E:\python\anaconda\anaconda\lib\site-packages\keras\models.py", line 1315, in fit_generator initial_epoch=initial_epoch) File "E:\python\anaconda\anaconda\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "E:\python\anaconda\anaconda\lib\site-packages\keras\engine\training.py", line 2268, in fit_generator callbacks.on_epoch_end(epoch, epoch_logs) File "E:\python\anaconda\anaconda\lib\site-packages\keras\callbacks.py", line 77, in on_epoch_end callback.on_epoch_end(epoch, logs) File "E:\python\anaconda\anaconda\lib\site-packages\keras\callbacks.py", line 339, in on_epoch_end self.progbar.update(self.seen, self.log_values) AttributeError: 'ProgbarLogger' object has no attribute 'log_values' ``` 我清楚ProgbarLogger内没有log_values的属性的意思,但是因为刚开始接触,不知道应怎样改动

保存keras模型时出现的问题

求助各路大神,小弟最近用keras跑神经网络模型,在训练和测试时都很好没问题,但是在保存时出现问题 小弟保存模型用的语句: json_string = model.to_json() open('my_model_architecture.json', 'w').write(json_string) #保存网络结构 model.save_weights('my_model_weights.h5',overwrite='true') #保存权重 但是运行后会显示Process finished with exit code -1073741819 (0xC0000005) 然后保存权重的.h5文件没有内容 求助各位大神是怎么回事啊

基于keras,使用imagedatagenerator.flow函数读入数据,训练集ACC极低

在做字符识别的神经网络,数据集是用序号标好名称的图片,标签取图片的文件名。想用Imagedatagenrator 函数和flow函数,增加样本的泛化性,然后生成数据传入网络,可是这样acc=1/类别数,基本为零。请问哪里出了问题 ``` datagen = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1 ) def read_train_image(self, name): myimg = Image.open(name).convert('RGB') return np.array(myimg) def train(self): #训练集 train_img_list = [] train_label_list = [] #测试集 test_img_list = [] test_label_list = [] for file in os.listdir('train'): files_img_in_array = self.read_train_image(name='train/' + file) train_img_list.append(files_img_in_array) # Image list add up train_label_list.append(int(file.split('_')[0])) # lable list addup for file in os.listdir('test'): files_img_in_array = self.read_train_image(name='test/' + file) test_img_list.append(files_img_in_array) # Image list add up test_label_list.append(int(file.split('_')[0])) # lable list addup train_img_list = np.array(train_img_list) train_label_list = np.array(train_label_list) test_img_list = np.array(train_img_list) test_label_list = np.array(train_label_list) train_label_list = np_utils.to_categorical(train_label_list, 5788) test_label_list = np_utils.to_categorical(test_label_list, 5788) train_img_list = train_img_list.astype('float32') test_img_list = test_img_list.astype('float32') test_img_list /= 255.0 train_img_list /= 255.0 ``` 这是图片数据的处理,图片和标签都存到list里。下面是用fit_genrator训练 ``` model.fit_generator( self.datagen.flow(x=train_img_list, y=train_label_list, batch_size=2), samples_per_epoch=len(train_img_list), epochs=10, validation_data=(test_img_list,test_label_list), ) ```

keras实现人脸识别,训练失败……请教大神指点迷津!!!

![图片说明](https://img-ask.csdn.net/upload/201904/26/1556209614_615215.jpg) 各位大神,如图所示,在训练过程中,第二轮开始出现问题,这是什么原因呢? 代码如下: ------------------------------------------------- ``` import random import keras import numpy as np import cv2 from sklearn.model_selection import train_test_split from keras.preprocessing.image import ImageDataGenerator from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Convolution2D, MaxPooling2D from keras.optimizers import SGD from keras.utils import np_utils from keras.models import load_model from keras import backend as K from source_data import load_dataset,resize_img #定义数据集格式 class Dataset: def __init__(self, path_name): #训练数据集 self.train_images = None self.train_labels = None #测试集 self.valid_images = None self.valid_labels = None #样本数据 self.test_images = None self.test_labels = None #load路径 self.path_name = path_name #维度顺序 self.input_shape = None #加载数据集并按照交叉验证的原则划分数据集,完成数据预处理 def load(self,img_rows=64, img_cols=64,img_channels = 3,nb_classes = 2): #加载数据集到内存 images,labels=load_dataset(self.path_name)#函数调用 train_images, valid_images, train_labels, valid_labels= train_test_split(images, labels, test_size = 0.3, random_state = random.randint(0, 100)) _, test_images, _, test_labels = train_test_split(images, labels, test_size = 0.5, random_state = random.randint(0, 100)) #根据backend类型确定输入图片数据时的顺序为:channels,rows,cols,否则:rows,cols,channels #这部分代码就是根据keras库要求的维度顺序重组训练数据集 train_images = train_images.reshape(train_images.shape[0], img_rows, img_cols, img_channels) valid_images = valid_images.reshape(valid_images.shape[0], img_rows, img_cols, img_channels) test_images = test_images.reshape(test_images.shape[0], img_rows, img_cols, img_channels) self.input_shape = (img_rows, img_cols, img_channels) #输出训练集、验证集、测试集的数量 print(train_images.shape[0], 'train samples') print(valid_images.shape[0], 'valid samples') print(test_images.shape[0], 'test samples') #我们的模型使用categorical_crossentropy作为损失函数,因此需要根据类别数量nb_classes将 #类别标签进行one-hot编码使其向量化,在这里我们的类别只有两种,经过转化后标签数据变为二维 train_labels = np_utils.to_categorical(train_labels, nb_classes) valid_labels = np_utils.to_categorical(valid_labels, nb_classes) test_labels = np_utils.to_categorical(test_labels, nb_classes) #像素数据浮点化以便归一化 train_images = train_images.astype('float32') valid_images = valid_images.astype('float32') test_images = test_images.astype('float32') #将其归一化,图像的各像素值归一化到0—1区间 train_images /= 255 valid_images /= 255 test_images /= 255 self.train_images = train_images self.valid_images = valid_images self.test_images = test_images self.train_labels = train_labels self.valid_labels = valid_labels self.test_labels = test_labels class Model: def __init__(self): self.model = None #建立keras模型 def build_model(self, dataset, nb_classes = 2): #构建一个空的网络模型,序贯模型或线性堆叠模型,添加各个layer self.model = Sequential() #以下代码将顺序添加CNN网络需要的各层,一个add就是一个网络层 self.model.add(Convolution2D(32, 3, 3, border_mode='same', input_shape = dataset.input_shape)) #1 2维卷积层 self.model.add(Activation('relu')) #2 激活函数层 self.model.add(Convolution2D(32, 3, 3)) #3 2维卷积层 self.model.add(Activation('relu')) #4 激活函数层 self.model.add(MaxPooling2D(pool_size=(2, 2))) #5 池化层 self.model.add(Dropout(0.25)) #6 Dropout层 self.model.add(Convolution2D(64, 3, 3, border_mode='same')) #7 2维卷积层 self.model.add(Activation('relu')) #8 激活函数层 self.model.add(Convolution2D(64, 3, 3)) #9 2维卷积层 self.model.add(Activation('relu')) #10 激活函数层 self.model.add(MaxPooling2D(pool_size=(2, 2))) #11 池化层 self.model.add(Dropout(0.25)) #12 Dropout层 self.model.add(Flatten()) #13 Flatten层 self.model.add(Dense(512)) #14 Dense层,又被称作全连接层 self.model.add(Activation('relu')) #15 激活函数层 self.model.add(Dropout(0.5)) #16 Dropout层 self.model.add(Dense(nb_classes)) #17 Dense层 self.model.add(Activation('softmax')) #18 分类层,输出最终结果 #Prints a string summary of the network self.model.summary() #训练模型 def train(self, dataset, batch_size = 20, nb_epoch = 10, data_augmentation = True): sgd = SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True) #采用随机梯度下降优化器进行训练,首先生成一个优化器对象 self.model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=['accuracy']) #完成实际的模型配置 #不使用数据提升,所谓的提升就是从我们提供的训练数据中利用旋转、翻转、加噪声等方法提升训练数据规模,增加模型训练量 if not data_augmentation: self.model.fit(dataset.train_images, dataset.train_labels, batch_size = batch_size, epochs = nb_epoch, validation_data = (dataset.valid_images, dataset.valid_labels), shuffle = True) #使用实时数据提升 else: #定义数据生成器用于数据提升,其返回一个生成器对象datagen,datagen每被调用一 #次其生成一组数据(顺序生成),节省内存,其实就是python的数据生成器 datagen = ImageDataGenerator( featurewise_center = False, #是否使输入数据去中心化(均值为0), samplewise_center = False, #是否使输入数据的每个样本均值为0 featurewise_std_normalization = False, #是否数据标准化(输入数据除以数据集的标准差) samplewise_std_normalization = False, #是否将每个样本数据除以自身的标准差 zca_whitening = False, #是否对输入数据施以ZCA白化 rotation_range = 20, #数据提升时图片随机转动的角度(范围为0~180) width_shift_range = 0.2, #数据提升时图片水平偏移的幅度(单位为图片宽度的占比,0~1之间的浮点数) height_shift_range = 0.2, #同上,只不过这里是垂直 horizontal_flip = True, #是否进行随机水平翻转 vertical_flip = False) #是否进行随机垂直翻转 #计算整个训练样本集的数量以用于特征值归一化等处理 datagen.fit(dataset.train_images) #利用生成器开始训练模型—0.7*N self.model.fit_generator(datagen.flow(dataset.train_images, dataset.train_labels, batch_size = batch_size), steps_per_epoch = dataset.train_images.shape[0], epochs = nb_epoch, validation_data = (dataset.valid_images, dataset.valid_labels)) if __name__ == '__main__': dataset = Dataset('e:\saving') dataset.load()#实例操作,完成实际数据加载和预处理 model = Model() model.build_model(dataset) #训练数据 model.train(dataset) ```

Keras 错误'ProgbarLogger' no attribute 'log_values'

用keras(TensorFlow后端)跑LSMT的时候出现了下面的问题。 Traceback (most recent call last): File "/Users/zhaojing/PycharmProjects/CWS_02/lstm01.py", line 94, in <module> model.fit(np.array(list(d['x'])).reshape(-1,maxlen), np.array(list(d['y'])).reshape((-1,maxlen,5)), batch_size=batch_size, epochs=1) File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 1705, in fit validation_steps=validation_steps) File "/usr/local/lib/python3.6/site-packages/keras/engine/training.py", line 1256, in _fit_loop callbacks.on_epoch_end(epoch, epoch_logs) File "/usr/local/lib/python3.6/site-packages/keras/callbacks.py", line 77, in on_epoch_end callback.on_epoch_end(epoch, logs) File "/usr/local/lib/python3.6/site-packages/keras/callbacks.py", line 339, in on_epoch_end self.progbar.update(self.seen, self.log_values) AttributeError: 'ProgbarLogger' object has no attribute 'log_values' 请问这个问题该怎么解决呢。 keras 版本:2.1.6 和TensorFlow 版本1.8.0rc1

Keras测试错误'ProgbarLogger' no attribute 'log_values'

Keras 简单程序测试,出现如下错位,请问如何解决: Apple@Host~/test$ python3 kt.py Using Theano backend. -------------------------------------------------- Iteration 1 Train on 0 samples, validate on 0 samples Epoch 1/2 Traceback (most recent call last): File "ktest2.py", line 189, in <module> model.fit(inputs, labels, batch_size=batch_size, nb_epoch=2, validation_split = 0.1) File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 672, in fit initial_epoch=initial_epoch) File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 1196, in fit initial_epoch=initial_epoch) File "/usr/local/lib/python3.5/dist-packages/keras/engine/training.py", line 911, in _fit_loop callbacks.on_epoch_end(epoch, epoch_logs) File "/usr/local/lib/python3.5/dist-packages/keras/callbacks.py", line 76, in on_epoch_end callback.on_epoch_end(epoch, logs) File "/usr/local/lib/python3.5/dist-packages/keras/callbacks.py", line 265, in on_epoch_end self.progbar.update(self.seen, self.log_values, force=True) AttributeError: 'ProgbarLogger' object has no attribute 'log_values' Apple@Host:~/test$

Keras, Tensorflow, ValueError

把csdn上一个颜值打分程序放到jupyter notebook上跑,程序如下: ``` from keras.applications import ResNet50 from keras import optimizers from keras.layers import Dense, Dropout from keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint from keras.backend.tensorflow_backend import set_session os.environ['CUDA_VISIBLE_DEVICES'] = '1' config = tf.ConfigProto() config.gpu_options.allow_growth = True set_session(tf.Session(config=config)) batch_size = 32 target_size = (224, 224) resnet = ResNet50(include_top=False, pooling='avg') resnet.trainable = False # keras.backend.clear_session() # tf.reset_default_graph() model = Sequential() model.add(resnet) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) print(model.summary()) model.compile(optimizer=optimizers.SGD(lr=0.001), loss='mse') callbacks = [EarlyStopping(monitor='val_loss', patience=5, verbose=1, min_delta=1e-4), ReduceLROnPlateau(monitor='val_loss', patience=3, factor=0.1, epsilon=1e-4), ModelCheckpoint(monitor='val_loss', filepath='weights/resnet50_weights.hdf5', save_best_only=True, save_weights_only=True)] train_file_list, test_file_list = read_data_list() train_steps_per_epoch = math.ceil(len(train_file_list) / batch_size) test_steps_per_epoch = math.ceil(len(test_file_list) / batch_size) train_data = DataGenerator(train_file_list, target_size,batch_size) test_data = DataGenerator(test_file_list, target_size, batch_size) model.fit_generator(train_data, steps_per_epoch=train_steps_per_epoch, epochs=30, verbose=1, callbacks=callbacks, validation_data=test_data, validation_steps=test_steps_per_epoch, use_multiprocessing=True) ``` 结果引发如下错误: ValueError Traceback (most recent call last) <ipython-input-34-ae0a8870fdc1> in <module>() 20 # tf.reset_default_graph() 21 model = Sequential() ---> 22 model.add(resnet) 23 model.add(Dropout(0.5)) 24 model.add(Dense(1, activation='sigmoid')) ...Ignoring many tracing lines... ValueError: Variable bn_conv1/moving_mean/biased already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at: File "xxxx\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__ self._traceback = _extract_stack() File "xxxx\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op original_op=self._default_original_op, op_def=op_def) File "xxxx\anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op op_def=op_def) 我按照网上说法在model语句前加了tf.reset_default_graph() ,结果又产生新的error: ValueError: Tensor("conv1_1/kernel:0", shape=(7, 7, 3, 64), dtype=float32_ref) must be from the same graph as Tensor("resnet50/conv1_pad/Pad:0", shape=(?, ?, ?, 3), dtype=float32). 又按照网上说法加了keras.backend.clear_session(),总共加的两句前前后后在很多地方放了测试,结果都会有新的问题: ValueError: Tensor("conv1/kernel:0", shape=(7, 7, 3, 64), dtype=float32_ref) must be from the same graph as Tensor("resnet50/conv1_pad/Pad:0", shape=(?, ?, ?, 3), dtype=float32). 请教大牛究竟该如何彻底解决问题。

TensorFlow的Keras如何使用Dataset作为数据输入?

当我把dataset作为输入数据是总会报出如下错误,尽管我已经在数据解析那里reshape了图片大小为(512,512,1),请问该如何修改? ``` ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) ``` **图片大小定义** ``` import tensorflow as tf from tensorflow import keras IMG_HEIGHT = 512 IMG_WIDTH = 512 IMG_CHANNELS = 1 IMG_PIXELS = IMG_CHANNELS * IMG_HEIGHT * IMG_WIDTH ``` **解析函数** ``` def parser(record): features = tf.parse_single_example(record, features={ 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([23], tf.int64) }) image = tf.decode_raw(features['image_raw'], tf.uint8) label = tf.cast(features['label'], tf.int32) image.set_shape([IMG_PIXELS]) image = tf.reshape(image, [IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS]) image = tf.cast(image, tf.float32) return image, label ``` **模型构建** ``` dataset = tf.data.TFRecordDataset([TFRECORD_PATH]) dataset.map(parser) dataset = dataset.repeat(10*10).batch(10) model = keras.Sequential([ keras.layers.Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu', input_shape=(512, 512, 1)), keras.layers.MaxPool2D(pool_size=(2, 2)), keras.layers.Dropout(0.25), keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu'), keras.layers.MaxPool2D(pool_size=(2, 2)), keras.layers.Dropout(0.25), keras.layers.Flatten(), keras.layers.Dense(128, activation='relu'), keras.layers.Dropout(0.25), keras.layers.Dense(23, activation='softmax') ]) model.compile(optimizer=keras.optimizers.Adam(), loss=keras.losses.sparse_categorical_crossentropy, metrics=[tf.keras.metrics.categorical_accuracy]) model.fit(dataset.make_one_shot_iterator(), epochs=10, steps_per_epoch=10) ```

运行结果如下:train(generator,discriminator,gan_model,latent_dim) NameError: name 'train' is not defined,请问如何解决

import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from numpy import hstack from numpy import zeros from numpy import ones from numpy.random import rand from numpy.random import randn from keras.models import Sequential from keras.layers import Dense def define_discriminator(n_inputs=2): model=Sequential() model.add(Dense(25, activation='relu',kernel_initializer='he_uniform',input_dim=n_inputs)) model.add(Dense(1,activation='sigmoid')) model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy']) return model def define_generator(latent_dim,n_outputs=2): model=Sequential() model.add(Dense(15, activation='relu',kernel_initializer='he_uniform', input_dim=latent_dim)) model.add(Dense(n_outputs,activation='linear')) return model def define_gan(generator,discriminator): discriminator.trainable=False model=Sequential() model.add(generator) model.add(discriminator) model.compile(loss='binary_crossentropy',optimizer='adam') return model def generate_real_samples(n): x1=rand(n)-0.5 x2=x1*x1 x1=x1.reshape(n,1) x2=x2.reshape(n,1) x=hstack((x1,x2)) y=ones((n,1)) return x,y def generate_latent_points(latent_dim,n): x_input=randn(latent_dim*n) x_input=x_input.reshape(n,latent_dim) return x_input def generate_fake_samples(generator,latent_dim,n): x_input=generate_latent_points(latent_dim,n) x=generator.predict(x_input) y=zeros((n,1)) return x,y def summarize_performance(epoch,generator,discriminator,latent_dim,n=100): x_real,y_real=generate_real_samples(n) _,acc_real=discriminator.evaluate(x_real,y_real,verbose=0) x_fake, y_fake = generate_fake_samples(generator,latent_dim,n) _, acc_fake = discriminator.evaluate(x_fake, y_fake, verbose=0) print(epoch,acc_real,acc_fake) plt.scatter(x_real[:,0],x_real[:,1],color='red') plt.scatter(x_fake[:, 0], x_fake[:, 1], color='blue') plt.show() def train(g_model,d_model,gan_model,latent_dim,n_epochs=10000,n_batch=128,n_eval=2000): half_batch=int(n_batch/2) for i in range(n_epochs): x_real,y_real=generate_real_samples(half_batch) x_fake,y_fake=generate_fake_samples(g_model,latent_dim,half_batch) d_model.train_on_batch(x_real,y_real) d_model.train_on_batch(x_fake, y_fake) x_gan=generate_latent_points(latent_dim,n_batch) y_gan=ones((n_batch,1)) gan_model.train_on_batch(x_gan,y_gan) if(i+1)%n_epochs==0: summarize_performance(i,g_model,d_model,latent_dim) latent_dim=5 discriminator=define_discriminator() generator=define_generator(latent_dim) gan_model=define_gan(generator,discriminator) train(generator,discriminator,gan_model,latent_dim) 问题

CNN模型报错None values not supported,怎么解决?

深度学习小白一枚,最近在kaggle网站上看一个皮肤病数据集的代码→ [皮肤病数据集-CNN模型](https://www.kaggle.com/sid321axn/step-wise-approach-cnn-model-77-0344-accuracy "") 可是按照原网站的代码复制上去后(前面访问文件夹的代码有改动,不过不影响后面代码),用vscode运行时出现了问题 ``` history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size), steps_per_epoch=x_train.shape[0] // batch_size, epochs = epochs, verbose = 1, callbacks=[learning_rate_reduction], validation_data = (x_validate,y_validate)) ``` 报错如下: ![图片说明](https://img-ask.csdn.net/upload/202002/01/1580524933_147759.png) ![图片说明](https://img-ask.csdn.net/upload/202002/01/1580525568_202234.png) ![图片说明](https://img-ask.csdn.net/upload/202002/01/1580525554_366954.png) 报错:ValueError: None values not supported. 求助,这种问题该怎么解决,源代码可以戳上方蓝字进入kaggle网站上看。

tf.keras图像二分类,predict()时卡死是什么原因

根据https://tensorflow.google.cn/tutorials/images/classification 里的例子进行图像二分类 ``` import numpy as np import pandas as pd import matplotlib.pyplot as plt from PIL import Image import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D from tensorflow.keras.preprocessing.image import ImageDataGenerator train_yes_dir = 'E:\\ml\\tr\\logo_yes\\' train_no_dir = 'E:\\ml\\tr\\logo_no\\' validation_yes_dir = 'E:\\ml\\v\\yes_v\\' validation_no_dir = 'E:\\ml\\v\\no_v\\' train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data validation_image_generator = ImageDataGenerator(rescale=1./255) batch_size = 32 epochs = 10 IMG_HEIGHT = 64 IMG_WIDTH = 64 # 训练数据 train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size, directory='E:\\ml\\tr\\', color_mode='rgb', shuffle=True, target_size=(IMG_HEIGHT, IMG_WIDTH), class_mode='binary') # 验证数据 val_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size, directory='E:\\ml\\v\\', color_mode='rgb', shuffle=True, target_size=(IMG_HEIGHT, IMG_WIDTH), class_mode='binary') # 建立模型 model = Sequential([ Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)), MaxPooling2D(), Conv2D(32, 3, padding='same', activation='relu'), MaxPooling2D(), Conv2D(64, 3, padding='same', activation='relu'), MaxPooling2D(), Flatten(), Dense(512, activation='relu'), Dense(1) ]) model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) ``` ``` model.summary() Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_12 (Conv2D) (None, 64, 64, 16) 448 _________________________________________________________________ max_pooling2d_12 (MaxPooling (None, 32, 32, 16) 0 _________________________________________________________________ conv2d_13 (Conv2D) (None, 32, 32, 32) 4640 _________________________________________________________________ max_pooling2d_13 (MaxPooling (None, 16, 16, 32) 0 _________________________________________________________________ conv2d_14 (Conv2D) (None, 16, 16, 64) 18496 _________________________________________________________________ max_pooling2d_14 (MaxPooling (None, 8, 8, 64) 0 _________________________________________________________________ flatten_4 (Flatten) (None, 4096) 0 _________________________________________________________________ dense_8 (Dense) (None, 512) 2097664 _________________________________________________________________ dense_9 (Dense) (None, 1) 513 ================================================================= Total params: 2,121,761 Trainable params: 2,121,761 Non-trainable params: 0 _________________________________________________________________ history = model.fit_generator( train_data_gen, steps_per_epoch=312 // batch_size, epochs=epochs, validation_data=val_data_gen, validation_steps=100 // batch_size ) Epoch 1/10 9/9 [==============================] - 1s 120ms/step - loss: 0.7537 - accuracy: 0.5357 - val_loss: 0.6520 - val_accuracy: 0.5000 Epoch 2/10 9/9 [==============================] - 1s 111ms/step - loss: 0.5691 - accuracy: 0.5590 - val_loss: 0.4826 - val_accuracy: 0.7917 Epoch 3/10 9/9 [==============================] - 1s 112ms/step - loss: 0.4936 - accuracy: 0.7750 - val_loss: 0.2535 - val_accuracy: 1.0000 Epoch 4/10 9/9 [==============================] - 1s 106ms/step - loss: 0.2540 - accuracy: 0.9571 - val_loss: 0.1010 - val_accuracy: 1.0000 Epoch 5/10 9/9 [==============================] - 1s 108ms/step - loss: 0.1271 - accuracy: 0.9714 - val_loss: 0.0308 - val_accuracy: 1.0000 Epoch 6/10 9/9 [==============================] - 1s 112ms/step - loss: 0.1058 - accuracy: 0.9786 - val_loss: 0.0299 - val_accuracy: 1.0000 Epoch 7/10 9/9 [==============================] - 1s 109ms/step - loss: 0.0296 - accuracy: 0.9964 - val_loss: 0.0173 - val_accuracy: 1.0000 Epoch 8/10 9/9 [==============================] - 1s 111ms/step - loss: 0.0463 - accuracy: 0.9929 - val_loss: 0.0119 - val_accuracy: 1.0000 Epoch 9/10 9/9 [==============================] - 1s 110ms/step - loss: 0.0471 - accuracy: 0.9893 - val_loss: 0.0150 - val_accuracy: 1.0000 Epoch 10/10 9/9 [==============================] - 1s 115ms/step - loss: 0.0303 - accuracy: 0.9893 - val_loss: 0.0151 - val_accuracy: 1.0000 ``` ![图片说明](https://img-ask.csdn.net/upload/202004/10/1586513081_848177.png) 结果红框的值无限在跑 不出结果 求大神解答

关于 keras 中用ImageDataGenerator 做 data augmentation 的问题

各位大神好,小白刚接触深度学习和keras. 有两个问题一直困扰着我,用keras中的 ImageDataGenerator做data augmentation时, (1)每个epoch的图片都不同,这样的做,反向传播时修改的参数还准确吗,训练模型严谨吗, (2)我试着输出过训练图像,发现里面没有原始图像,这样做数据扩张感觉很慌,是不是我使用方法的问题啊,请大佬指点迷津 ``` datagen = ImageDataGenerator( rescale=None, shear_range=0.2, zoom_range=[0.95,1.05], rotation_range=10, horizontal_flip=True, vertical_flip=True, fill_mode='reflect', ) training = model.fit_generator(datagen.flow(data_train, label_train_binary, batch_size=n_batch, shuffle=True), callbacks=[checkpoint,tensorboard,csvlog],validation_data=(data_val,label_val_binary),steps_per_epoch=len(data_train)//n_batch, nb_epoch=10000, verbose=1) ```

求问怎么加dropout啊,急急急,在线等

#!/usr/bin/env python # encoding: utf-8 import matplotlib.pylab as plt from keras.optimizers import Adam from keras.preprocessing.image import ImageDataGenerator from keras.layers import Dropout import numpy as np from sklearn import datasets from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold from keras.models import Sequential import sys import tensorflow as tf sys.path.append("../process")#添加其他文件夹 import data_input#导入其他模块 from traffic_network import LeNet import numpy as np from keras.models import Sequential from keras.layers import Dropout from keras.layers import Dense from keras.constraints import max_norm from keras.optimizers import SGD sgd = SGD(lr=0.01, momentum=0.0, decay=0.0, nesterov=False) def train(aug, model,train_x,train_y,test_x,test_y): model.add(Dropout(rate=0.0, input_shape=(4,))) model.compile(loss="categorical_crossentropy", optimizer="sgd",metrics=["accuracy"])#配置 #model.fit(train_x,train_y,batch_size,epochs,validation_data=(test_x,test_y)) _history = model.fit_generator(aug.flow(train_x,train_y,batch_size=batch_size), validation_data=(test_x,test_y),steps_per_epoch=len(train_x)//batch_size, epochs=epochs,verbose=1) #拟合,具体fit_generator请查阅其他文档,steps_per_epoch是每次迭代,需要迭代多少个batch_size,validation_data为test数据,直接做验证,不参与训练 model.save("../predict/traffic_model.h5") print(_history.history.keys()) plt.style.use("ggplot")#matplotlib的美化样式 plt.figure() N = epochs plt.plot(np.arange(0,N),_history.history["loss"],label ="train_loss")#model的history有四个属性,loss,val_loss,acc,val_acc plt.plot(np.arange(0,N),_history.history["val_loss"],label="val_loss") plt.plot(np.arange(0,N),_history.history["acc"],label="train_acc") plt.plot(np.arange(0,N),_history.history["val_acc"],label="val_acc") plt.title("loss and accuracy") plt.xlabel("epoch") plt.ylabel("loss/acc") plt.legend(loc="best") plt.savefig("../result/result.png") plt.show() if __name__ =="__main__": channel = 3 height = 32 width = 32 class_num = 8 norm_size = 32#参数 batch_size = 64 epochs = 1000#40 DROPOUT = 0.3 model = LeNet.neural(channel=channel, height=height, width=width, classes=class_num)#网络 train_x, train_y = data_input.load_data("../data/train", norm_size, class_num) test_x, test_y = data_input.load_data("../data/test", norm_size, class_num)#生成数据 aug = ImageDataGenerator(rotation_range=30,width_shift_range=0.1, height_shift_range=0.1,shear_range=0.2,zoom_range=0.2, horizontal_flip=True,fill_mode="nearest")#数据增强,生成迭代器 train(aug,model,train_x,train_y,test_x,test_y)#训练 我这里怎么加dropout啊,rate太高val_acc就会变成恒定是1.0然后train_acc识别率也也上不去了,求大神指导啊

Mask r-cnn 无法训练的问题

在做 https://github.com/matterport/Mask_RCNN 的复现。 在复现train_shpes时,在heads层训练时,卡在了Epoch 1/1。我观察下gpu和cpu,都没有工作 我在停止代码运行时发现停在了 File "<ipython-input-2-72119e4591c8>", line 1, in <module> runfile('D:/py/Mask_RCNN-master/samples/shapes/train_shapes.py', wdir='D:/py/Mask_RCNN-master/samples/shapes') File "D:\anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace) File "D:\anaconda\envs\tensorflow\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "D:/py/Mask_RCNN-master/samples/shapes/train_shapes.py", line 258, in <module> layers='heads') File "D:\py\Mask_RCNN-master\mrcnn\model.py", line 2352, in train use_multiprocessing=True, File "D:\anaconda\envs\tensorflow\lib\site-packages\keras\legacy\interfaces.py", line 87, in wrapper return func(*args, **kwargs) File "D:\anaconda\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 2011, in fit_generator generator_output = next(output_generator) File "D:\anaconda\envs\tensorflow\lib\site-packages\keras\utils\data_utils.py", line 644, in get time.sleep(self.wait_time) 有大佬知道怎么解决吗,或者有谁出现了相同的问题吗??

keras用2层cnn和2层全连接搭建食物图像识别的神经网络acc一直在50上不去。

现在用10x750+10x250张图片做训练测试 总体有101x750+101x250 不知道这样测试出来的网络能不能直接套用在全部的数据上(因为电脑原因跑的比较慢才这样测试的) 用10类别测试的时候 acc比较低而且把待测图片放入测试文件中的时候会出现100%但指向另外类别的结果 ``` import sys import os from keras.preprocessing.image import ImageDataGenerator from keras import optimizers from keras.models import Sequential from keras.layers import Dropout, Flatten, Dense, Activation from keras.layers.convolutional import Convolution2D, MaxPooling2D from keras import callbacks import time import tensorflow as tf config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) sess = tf.Session(config=config) start = time.time() DEV = False argvs = sys.argv argc = len(argvs) if argc > 1 and (argvs[1] == "--development" or argvs[1] == "-d"): DEV = True if DEV: epochs = 2 else: epochs = 10 train_data_path = 'data/train' validation_data_path = 'data/test' """ Parameters """ img_width, img_height = 100, 100 batch_size = 32 samples_per_epoch = 7500 validation_steps = 490 nb_filters1 = 32 nb_filters2 = 64 conv1_size = 3 conv2_size = 2 pool_size = 2 classes_num = 10 lr = 0.0004 model = Sequential() model.add(Convolution2D( nb_filters1, conv1_size, conv1_size, border_mode ="same", input_shape=(img_width, img_height, 3) )) model.add(Activation("relu")) model.add(MaxPooling2D( pool_size=(pool_size, pool_size) )) model.add(Convolution2D(nb_filters2, conv2_size, conv2_size, border_mode ="same")) model.add(Activation("relu")) model.add(Convolution2D(nb_filters2, conv2_size, conv2_size, border_mode ="same")) model.add(Activation("relu")) model.add(MaxPooling2D(pool_size=(pool_size, pool_size), dim_ordering='th')) model.add(Flatten()) model.add(Dense(256)) model.add(Activation("relu")) model.add(Dropout(0.5)) model.add(Dense(classes_num, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=lr), metrics=['accuracy']) train_datagen = ImageDataGenerator( rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_path, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') validation_generator = test_datagen.flow_from_directory( validation_data_path, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') """ Tensorboard log """ log_dir = './training101tf-log/' tb_cb = callbacks.TensorBoard(log_dir=log_dir, histogram_freq=0) cbks = [tb_cb] model.fit_generator( train_generator, samples_per_epoch=samples_per_epoch, epochs=epochs, validation_data=validation_generator, callbacks=cbks, validation_steps=validation_steps) target_dir = './models_training101/' if not os.path.exists(target_dir): os.mkdir(target_dir) model.save('./models_training101/model.h5') model.save_weights('./models_training101/weights.h5') #Calculate execution time end = time.time() dur = end-start if dur<60: print("Execution Time:",dur,"seconds") elif dur>60 and dur<3600: dur=dur/60 print("Execution Time:",dur,"minutes") else: dur=dur/(60*60) print("Execution Time:",dur,"hours") ```

使用keras进行分类问题时,验证集loss,accuracy 显示0.0000e+00,但是最后画图像时能显示出验证曲线

data_train, data_test, label_train, label_test = train_test_split(data_all, label_all, test_size= 0.2, random_state = 1) data_train, data_val, label_train, label_val = train_test_split(data_train,label_train, test_size = 0.25) data_train = np.asarray(data_train, np.float32) data_test = np.asarray(data_test, np.float32) data_val = np.asarray(data_val, np.float32) label_train = np.asarray(label_train, np.int32) label_test = np.asarray(label_test, np.int32) label_val = np.asarray(label_val, np.int32) training = model.fit_generator(datagen.flow(data_train, label_train_binary, batch_size=200,shuffle=True), validation_data=(data_val,label_val_binary), samples_per_epoch=len(data_train)*8, nb_epoch=30, verbose=1) def plot_history(history): plt.plot(training.history['acc']) plt.plot(training.history['val_acc']) plt.title('model accuracy') plt.xlabel('epoch') plt.ylabel('accuracy') plt.legend(['acc', 'val_acc'], loc='lower right') plt.show() plt.plot(training.history['loss']) plt.plot(training.history['val_loss']) plt.title('model loss') plt.xlabel('epoch') plt.ylabel('loss') plt.legend(['loss', 'val_loss'], loc='lower right') plt.show() plot_history(training) ![图片说明](https://img-ask.csdn.net/upload/201812/10/1544423669_112599.jpg)![图片说明](https://img-ask.csdn.net/upload/201812/10/1544423681_598605.jpg)

keras resnet应用时不论输入什么图片输出是固定的

用下面这样的代码测试的时候result都是固定值 之前都是用model.add这样来写结构的 不知道是不是结构写法的问题,model.add这样就没问题 ``` x = load_img(file, target_size=(img_width,img_height)) x = img_to_array(x) x = np.expand_dims(x, axis=0) array = model.predict(x) result = array[0] ``` training.py: ``` # coding=utf-8 from keras.models import Model from keras.layers import Input, Dense, Dropout, BatchNormalization, Conv2D, MaxPooling2D, AveragePooling2D, concatenate, \ Activation, ZeroPadding2D from keras.layers import add, Flatten from keras.utils import plot_model from keras.metrics import top_k_categorical_accuracy from keras.preprocessing.image import ImageDataGenerator from keras.models import load_model from keras import optimizers import os import sys import tensorflow as tf from keras import callbacks config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) sess = tf.Session(config=config) DEV = False argvs = sys.argv argc = len(argvs) if argc > 1 and (argvs[1] == "--development" or argvs[1] == "-d"): DEV = True if DEV: EPOCH=4 else: EPOCH=1 # Global Constants samples_per_epoch = 3750 validation_steps = 490 NB_CLASS=5 IM_WIDTH=100 IM_HEIGHT=100 train_root='data/train' vaildation_root='data/test' batch_size=16 lr=0.0004 # train data train_datagen = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, horizontal_flip=True, rescale=1./255 ) train_generator = train_datagen.flow_from_directory( train_root, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, shuffle=True ) # vaild data vaild_datagen = ImageDataGenerator( width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, horizontal_flip=True, rescale=1./255 ) vaild_generator = train_datagen.flow_from_directory( vaildation_root, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, ) def Conv2d_BN(x, nb_filter, kernel_size, strides=(1, 1), padding='same', name=None): if name is not None: bn_name = name + '_bn' conv_name = name + '_conv' else: bn_name = None conv_name = None x = Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu', name=conv_name)(x) x = BatchNormalization(axis=3, name=bn_name)(x) return x def identity_Block(inpt, nb_filter, kernel_size, strides=(1, 1), with_conv_shortcut=False): x = Conv2d_BN(inpt, nb_filter=nb_filter, kernel_size=kernel_size, strides=strides, padding='same') x = Conv2d_BN(x, nb_filter=nb_filter, kernel_size=kernel_size, padding='same') if with_conv_shortcut:#shortcut的含义是:将输入层x与最后的输出层y进行连接,如上图所示 shortcut = Conv2d_BN(inpt, nb_filter=nb_filter, strides=strides, kernel_size=kernel_size) x = add([x, shortcut]) return x else: x = add([x, inpt]) return x def resnet_34(width,height,channel,classes): inpt = Input(shape=(width, height, channel)) x = ZeroPadding2D((3, 3))(inpt) #conv1 x = Conv2d_BN(x, nb_filter=64, kernel_size=(7, 7), strides=(2, 2), padding='valid') x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x) #conv2_x x = identity_Block(x, nb_filter=64, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=64, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=64, kernel_size=(3, 3)) #conv3_x x = identity_Block(x, nb_filter=128, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = identity_Block(x, nb_filter=128, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=128, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=128, kernel_size=(3, 3)) #conv4_x x = identity_Block(x, nb_filter=256, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = identity_Block(x, nb_filter=256, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=256, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=256, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=256, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=256, kernel_size=(3, 3)) #conv5_x x = identity_Block(x, nb_filter=512, kernel_size=(3, 3), strides=(2, 2), with_conv_shortcut=True) x = identity_Block(x, nb_filter=512, kernel_size=(3, 3)) x = identity_Block(x, nb_filter=512, kernel_size=(3, 3)) x = AveragePooling2D(pool_size=(4, 4))(x) x = Flatten()(x) x = Dense(classes, activation='softmax')(x) model = Model(inputs=inpt, outputs=x) return model if __name__ == '__main__': if (os.path.exists('modelresnet') and DEV): model = load_model('./modelresnet/resnet_50.h5')######## model.load_weights('./modelresnet/weights.h5') else: model = resnet_34(IM_WIDTH,IM_HEIGHT,3,NB_CLASS) model.compile(loss='categorical_crossentropy', optimizer=optimizers.RMSprop(lr=lr), metrics=['accuracy']) print ('Model Compiled') model.fit_generator( train_generator, samples_per_epoch=samples_per_epoch, epochs=EPOCH, validation_data=vaild_generator, validation_steps=validation_steps) target_dir = './modelresnet/' if not os.path.exists(target_dir): os.mkdir(target_dir) model.save('./modelresnet/resnet_50.h5') model.save_weights('./modelresnet/weights.h5') #loss,acc,top_acc=model.evaluate_generator(test_generator, steps=test_generator.n / batch_size) #print 'Test result:loss:%f,acc:%f,top_acc:%f' % (loss, acc, top_acc) ```

Segnet网络用keras实现的时候报错ValueError,求大神帮忙看看

![图片说明](https://img-ask.csdn.net/upload/201904/05/1554454470_801036.jpg) 报错为:Error when checking target: expected activation_1 to have 3 dimensions, but got array with shape (32, 10) keras+tensorflow后端 代码如下 ``` # coding=utf-8 import matplotlib from PIL import Image matplotlib.use("Agg") import matplotlib.pyplot as plt import argparse import numpy as np from keras.models import Sequential from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, BatchNormalization, Reshape, Permute, Activation, Flatten # from keras.utils.np_utils import to_categorical # from keras.preprocessing.image import img_to_array from keras.models import Model from keras.layers import Input from keras.callbacks import ModelCheckpoint # from sklearn.preprocessing import LabelBinarizer # from sklearn.model_selection import train_test_split # import pickle import matplotlib.pyplot as plt import os from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator( rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) path = '/tmp/2' os.chdir(path) training_set = train_datagen.flow_from_directory( 'trainset', target_size=(64,64), batch_size=32, class_mode='categorical', shuffle=True) test_set = test_datagen.flow_from_directory( 'testset', target_size=(64,64), batch_size=32, class_mode='categorical', shuffle=True) def SegNet(): model = Sequential() # encoder model.add(Conv2D(64, (3, 3), strides=(1, 1), input_shape=(64, 64, 3), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) # (128,128) model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) # (64,64) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) # (32,32) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) # (16,16) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(MaxPooling2D(pool_size=(2, 2))) # (8,8) # decoder model.add(UpSampling2D(size=(2, 2))) # (16,16) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(UpSampling2D(size=(2, 2))) # (32,32) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(512, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(UpSampling2D(size=(2, 2))) # (64,64) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(UpSampling2D(size=(2, 2))) # (128,128) model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(UpSampling2D(size=(2, 2))) # (256,256) model.add(Conv2D(64, (3, 3), strides=(1, 1), input_shape=(64, 64, 3), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same', activation='relu')) model.add(BatchNormalization()) model.add(Conv2D(10, (1, 1), strides=(1, 1), padding='valid', activation='relu')) model.add(BatchNormalization()) model.add(Reshape((64*64, 10))) # axis=1和axis=2互换位置,等同于np.swapaxes(layer,1,2) model.add(Permute((2, 1))) #model.add(Flatten()) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy']) model.summary() return model def main(): model = SegNet() filepath = "/tmp/2/weights.best.hdf5" checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] history = model.fit_generator( training_set, steps_per_epoch=(training_set.samples / 32), epochs=20, callbacks=callbacks_list, validation_data=test_set, validation_steps=(test_set.samples / 32)) # Plotting the Loss and Classification Accuracy model.metrics_names print(history.history.keys()) # "Accuracy" plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('Model Accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() # "Loss" plt.plot(history.history['loss']) plt.plot(history.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['train', 'test'], loc='upper left') plt.show() if __name__ == '__main__': main() ``` 主要是这里,segnet没有全连接层,最后输出的应该是一个和输入图像同等大小的有判别标签的shape吗。。。求教怎么改。 输入图像是64 64的,3通道,总共10类,分别放在testset和trainset两个文件夹里

大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了

大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...

在中国程序员是青春饭吗?

今年,我也32了 ,为了不给大家误导,咨询了猎头、圈内好友,以及年过35岁的几位老程序员……舍了老脸去揭人家伤疤……希望能给大家以帮助,记得帮我点赞哦。 目录: 你以为的人生 一次又一次的伤害 猎头界的真相 如何应对互联网行业的「中年危机」 一、你以为的人生 刚入行时,拿着傲人的工资,想着好好干,以为我们的人生是这样的: 等真到了那一天,你会发现,你的人生很可能是这样的: ...

Java基础知识面试题(2020最新版)

文章目录Java概述何为编程什么是Javajdk1.5之后的三大版本JVM、JRE和JDK的关系什么是跨平台性?原理是什么Java语言有哪些特点什么是字节码?采用字节码的最大好处是什么什么是Java程序的主类?应用程序和小程序的主类有何不同?Java应用程序与小程序之间有那些差别?Java和C++的区别Oracle JDK 和 OpenJDK 的对比基础语法数据类型Java有哪些数据类型switc...

我以为我学懂了数据结构,直到看了这个导图才发现,我错了

数据结构与算法思维导图

String s = new String(" a ") 到底产生几个对象?

老生常谈的一个梗,到2020了还在争论,你们一天天的,哎哎哎,我不是针对你一个,我是说在座的各位都是人才! 上图红色的这3个箭头,对于通过new产生一个字符串(”宜春”)时,会先去常量池中查找是否已经有了”宜春”对象,如果没有则在常量池中创建一个此字符串对象,然后堆中再创建一个常量池中此”宜春”对象的拷贝对象。 也就是说准确答案是产生了一个或两个对象,如果常量池中原来没有 ”宜春” ,就是两个。...

技术大佬:我去,你写的 switch 语句也太老土了吧

昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意。但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:“我擦,小王,你丫写的 switch 语句也太老土了吧!” 来看看小王写的代码吧,看完不要骂我装逼啊。 private static String createPlayer(PlayerTypes p...

Linux面试题(2020最新版)

文章目录Linux 概述什么是LinuxUnix和Linux有什么区别?什么是 Linux 内核?Linux的基本组件是什么?Linux 的体系结构BASH和DOS之间的基本区别是什么?Linux 开机启动过程?Linux系统缺省的运行级别?Linux 使用的进程间通信方式?Linux 有哪些系统日志文件?Linux系统安装多个桌面环境有帮助吗?什么是交换空间?什么是root帐户什么是LILO?什...

Linux命令学习神器!命令看不懂直接给你解释!

大家都知道,Linux 系统有非常多的命令,而且每个命令又有非常多的用法,想要全部记住所有命令的所有用法,恐怕是一件不可能完成的任务。 一般情况下,我们学习一个命令时,要么直接百度去搜索它的用法,要么就直接用 man 命令去查看守冗长的帮助手册。这两个都可以实现我们的目标,但有没有更简便的方式呢? 答案是必须有的!今天给大家推荐一款有趣而实用学习神器 — kmdr,让你解锁 Linux 学习新姿势...

和黑客斗争的 6 天!

互联网公司工作,很难避免不和黑客们打交道,我呆过的两家互联网公司,几乎每月每天每分钟都有黑客在公司网站上扫描。有的是寻找 Sql 注入的缺口,有的是寻找线上服务器可能存在的漏洞,大部分都...

史上最全的 python 基础知识汇总篇,没有比这再全面的了,建议收藏

网友们有福了,小编终于把基础篇的内容全部涉略了一遍,这是一篇关于基础知识的汇总的文章,请朋友们收下,不用客气,不过文章篇幅肯能会有点长,耐心阅读吧爬虫(七十)多进程multiproces...

讲一个程序员如何副业月赚三万的真实故事

loonggg读完需要3分钟速读仅需 1 分钟大家好,我是你们的校长。我之前讲过,这年头,只要肯动脑,肯行动,程序员凭借自己的技术,赚钱的方式还是有很多种的。仅仅靠在公司出卖自己的劳动时...

女程序员,为什么比男程序员少???

昨天看到一档综艺节目,讨论了两个话题:(1)中国学生的数学成绩,平均下来看,会比国外好?为什么?(2)男生的数学成绩,平均下来看,会比女生好?为什么?同时,我又联想到了一个技术圈经常讨...

85后蒋凡:28岁实现财务自由、34岁成为阿里万亿电商帝国双掌门,他的人生底层逻辑是什么?...

蒋凡是何许人也? 2017年12月27日,在入职4年时间里,蒋凡开挂般坐上了淘宝总裁位置。 为此,时任阿里CEO张勇在任命书中力赞: 蒋凡加入阿里,始终保持创业者的冲劲,有敏锐的...

总结了 150 余个神奇网站,你不来瞅瞅吗?

原博客再更新,可能就没了,之后将持续更新本篇博客。

副业收入是我做程序媛的3倍,工作外的B面人生是怎样的?

提到“程序员”,多数人脑海里首先想到的大约是:为人木讷、薪水超高、工作枯燥…… 然而,当离开工作岗位,撕去层层标签,脱下“程序员”这身外套,有的人生动又有趣,马上展现出了完全不同的A/B面人生! 不论是简单的爱好,还是正经的副业,他们都干得同样出色。偶尔,还能和程序员的特质结合,产生奇妙的“化学反应”。 @Charlotte:平日素颜示人,周末美妆博主 大家都以为程序媛也个个不修边幅,但我们也许...

MySQL数据库面试题(2020最新版)

文章目录数据库基础知识为什么要使用数据库什么是SQL?什么是MySQL?数据库三大范式是什么mysql有关权限的表都有哪几个MySQL的binlog有有几种录入格式?分别有什么区别?数据类型mysql有哪些数据类型引擎MySQL存储引擎MyISAM与InnoDB区别MyISAM索引与InnoDB索引的区别?InnoDB引擎的4大特性存储引擎选择索引什么是索引?索引有哪些优缺点?索引使用场景(重点)...

新一代神器STM32CubeMonitor介绍、下载、安装和使用教程

关注、星标公众号,不错过精彩内容作者:黄工公众号:strongerHuang最近ST官网悄悄新上线了一款比较强大的工具:STM32CubeMonitor V1.0.0。经过我研究和使用之...

如果你是老板,你会不会踢了这样的员工?

有个好朋友ZS,是技术总监,昨天问我:“有一个老下属,跟了我很多年,做事勤勤恳恳,主动性也很好。但随着公司的发展,他的进步速度,跟不上团队的步伐了,有点...

我入职阿里后,才知道原来简历这么写

私下里,有不少读者问我:“二哥,如何才能写出一份专业的技术简历呢?我总感觉自己写的简历太烂了,所以投了无数份,都石沉大海了。”说实话,我自己好多年没有写过简历了,但我认识的一个同行,他在阿里,给我说了一些他当年写简历的方法论,我感觉太牛逼了,实在是忍不住,就分享了出来,希望能够帮助到你。 01、简历的本质 作为简历的撰写者,你必须要搞清楚一点,简历的本质是什么,它就是为了来销售你的价值主张的。往深...

大学一路走来,学习互联网全靠这几个网站,最终拿下了一把offer

大佬原来都是这样炼成的

离职半年了,老东家又发 offer,回不回?

有小伙伴问松哥这个问题,他在上海某公司,在离职了几个月后,前公司的领导联系到他,希望他能够返聘回去,他很纠结要不要回去? 俗话说好马不吃回头草,但是这个小伙伴既然感到纠结了,我觉得至少说明了两个问题:1.曾经的公司还不错;2.现在的日子也不是很如意。否则应该就不会纠结了。 老实说,松哥之前也有过类似的经历,今天就来和小伙伴们聊聊回头草到底吃不吃。 首先一个基本观点,就是离职了也没必要和老东家弄的苦...

为什么你不想学习?只想玩?人是如何一步一步废掉的

不知道是不是只有我这样子,还是你们也有过类似的经历。 上学的时候总有很多光辉历史,学年名列前茅,或者单科目大佬,但是虽然慢慢地长大了,你开始懈怠了,开始废掉了。。。 什么?你说不知道具体的情况是怎么样的? 我来告诉你: 你常常潜意识里或者心理觉得,自己真正的生活或者奋斗还没有开始。总是幻想着自己还拥有大把时间,还有无限的可能,自己还能逆风翻盘,只不是自己还没开始罢了,自己以后肯定会变得特别厉害...

什么时候跳槽,为什么离职,你想好了么?

都是出来打工的,多为自己着想

为什么程序员做外包会被瞧不起?

二哥,有个事想询问下您的意见,您觉得应届生值得去外包吗?公司虽然挺大的,中xx,但待遇感觉挺低,马上要报到,挺纠结的。

当HR压你价,说你只值7K,你该怎么回答?

当HR压你价,说你只值7K时,你可以流畅地回答,记住,是流畅,不能犹豫。 礼貌地说:“7K是吗?了解了。嗯~其实我对贵司的面试官印象很好。只不过,现在我的手头上已经有一份11K的offer。来面试,主要也是自己对贵司挺有兴趣的,所以过来看看……”(未完) 这段话主要是陪HR互诈的同时,从公司兴趣,公司职员印象上,都给予对方正面的肯定,既能提升HR的好感度,又能让谈判气氛融洽,为后面的发挥留足空间。...

面试阿里p7,被按在地上摩擦,鬼知道我经历了什么?

面试阿里p7被问到的问题(当时我只知道第一个):@Conditional是做什么的?@Conditional多个条件是什么逻辑关系?条件判断在什么时候执...

你期望月薪4万,出门右拐,不送,这几个点,你也就是个初级的水平

先来看几个问题通过注解的方式注入依赖对象,介绍一下你知道的几种方式@Autowired和@Resource有何区别说一下@Autowired查找候选者的...

面试了一个 31 岁程序员,让我有所触动,30岁以上的程序员该何去何从?

最近面试了一个31岁8年经验的程序猿,让我有点感慨,大龄程序猿该何去何从。

大三实习生,字节跳动面经分享,已拿Offer

说实话,自己的算法,我一个不会,太难了吧

程序员垃圾简历长什么样?

已经连续五年参加大厂校招、社招的技术面试工作,简历看的不下于万份 这篇文章会用实例告诉你,什么是差的程序员简历! 疫情快要结束了,各个公司也都开始春招了,作为即将红遍大江南北的新晋UP主,那当然要为小伙伴们做点事(手动狗头)。 就在公众号里公开征简历,义务帮大家看,并一一点评。《启舰:春招在即,义务帮大家看看简历吧》 一石激起千层浪,三天收到两百多封简历。 花光了两个星期的所有空闲时...

立即提问
相关内容推荐