why Keras 2D regression network has constant output

眉间皱痕 提交于 2020-02-25 07:12:21

问题


I am working on the some kind of the 2D Regression Deep network with keras, but the network has constant output for every datasets, even I test with handmade dataset in this code I feed the network with a constant 2d values and the output is linear valu of the X (2*X/100) but the out put is constant.

import resource
import glob
import gc
rsrc = resource.RLIMIT_DATA
soft, hard = resource.getrlimit(rsrc)
print ('Soft limit starts as  :', soft)

resource.setrlimit(rsrc, (4 * 1024 * 1024 * 1024, hard))  # limit to four giga bytes

soft, hard = resource.getrlimit(rsrc)
print ('Soft limit changed to :', soft)

from keras.models import Sequential
import keras.optimizers
from keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization
from keras.layers import Convolution2D, MaxPooling2D,AveragePooling2D
import numpy as np
import random
from keras.utils import plot_model

sample_size = 1
batch_size = 50
input_shape = (int(720 / 4), int(1280 / 4), sample_size * 5)

# model
model = Sequential()
model.add(BatchNormalization(input_shape=input_shape))
model.add(Convolution2D(128, (3, 3), activation='relu', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(128, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))

model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))
model.add(Convolution2D(256, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(256, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))

model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))

model.add(Convolution2D(512, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(512, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))

model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))

model.add(Flatten())
model.add(Dense(4096, activation='relu',kernel_initializer='random_uniform'))
#model.add(Dropout(0.5))
model.add(Dense(512, activation='sigmoid',kernel_initializer='random_uniform'))
model.add(Dense(1, activation='sigmoid',kernel_initializer='random_uniform'))
model.compile(loss='mean_absolute_error',
              optimizer='adam',
              metrics=['mae','mse'])
model.summary()
plot_model(model,to_file='model.png')

def generate_tr(batch_size, is_training=False):
    x=np.linspace(0, 10, num=5000).reshape(-1, 1)

    counter = 0
    print 'start'
    while 1:
        samples=np.zeros((batch_size, 720/4, 1280/4, 5))
        labels=[]
    for t in range (batch_size):
        i = int(random.randint(0, 4999))
        for b in range(sample_size): 
            samples[t, :,:,b*5:b*5+5] = np.random.rand(720/4,1280/4,5)/10+x[i] 

        labels.append((2*x[i])/100)

        counter += 1
        print counter #, labels
        yield ((samples), np.asarray(labels))


tt = model.fit_generator(generate_tr(batch_size, True), steps_per_epoch=100, epochs=10,
                         use_multiprocessing=False, verbose=2)

score = model.predict_generator(generate_tr(batch_size, True), steps=30)

the output is always average of all of the values (here is .10)

do you know why?

来源:https://stackoverflow.com/questions/45830331/why-keras-2d-regression-network-has-constant-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!