我用tensorflow后端运行Keras。我想测试所需验证数据的比例,因此我编写了一组拆分百分比的循环。每次拆分后,训练进行了2个时期。奇怪的是我说错了
Exception has occurred: tensorflow.python.framework.errors_impl.InvalidArgumentError
You must feed a value for placeholder tensor 'input_1' with dtype float and shape [?,32,32,3]
[[{{node input_1}} = Placeholder[dtype=DT_FLOAT, shape=[?,32,32,3], _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]
Caused by op 'input_1', defined at: File "c:\Users\Dell\.vscode\extensions\ms-python.python-
在第二个拆分选择的第一个时期的末尾。
代码看起来像这样
from keras.models import Model
from keras.layers import Dense, GlobalMaxPool2D, Dropout
from keras.layers import Input
from keras.optimizers import SGD
from keras.models import load_model
from keras.callbacks import ModelCheckpoint, TensorBoard
import numpy as np
import os
import tensorflow as tf
from keras.applications.vgg19 import VGG19
from DatageneratorVGG19 import DataGenerator
from time import time
from DeepMetaVGG19_whole import loadwholedataset
model_types = {'VGG19': VGG19}
net_continue = 0
nb_epoch, batch_size = 2, 4
sampleN = int(64)
imageSize = [32,32,3]
outputSize = [25]
show_samples = False
lr, momentum, decay = 0.01, 0.9, 5e-6
##############################
# set GPU
##############################
os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2,3'
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3333)
def train_once(split):
for model_type, model_name in model_types.items():
print('\n##############################\n Run for model: %s\n##############################' % model_type)
with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
##############################
# get model
##############################
cacheDir = './VGG19/'
if not os.path.exists(cacheDir):
os.makedirs(cacheDir)
preName = '_' + model_type
modelFile = cacheDir + 'model' + preName + '.h5'
if os.path.exists(modelFile) & net_continue:
##############################
# load model
##############################
model = load_model(modelFile)
else:
##############################
# generate model
##############################
base_model = model_name(input_tensor=Input(shape=imageSize), weights='imagenet', include_top=False)
x = base_model.output
x = GlobalMaxPool2D()(x)
x = Dense(1024, activation=None)(x)
x = Dropout(0.5)(x)
predictions = Dense(outputSize[0], activation='sigmoid')(x)
model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=SGD(lr=lr, momentum=momentum, decay=decay), loss='binary_crossentropy',
metrics=['binary_accuracy'])
filewriter=TensorBoard(log_dir="./VGG19/logs/split__{}dataset2".format(str(split)), histogram_freq=2, write_graph=True, write_images=True)
tr_x, tr_y=loadwholedataset()
hist = model.fit(tr_x, tr_y, batch_size=batch_size, epochs=nb_epoch, callbacks=[filewriter], validation_split=split)
del model, base_model
for split in [0.1*i for i in range(3,10) ]:
train_once(split)
任何想法可能是什么问题?谢谢!