【发布时间】:2020-02-03 20:02:09
【问题描述】:
我正在使用 Resnet 架构来比较性能与 CNN。我在第一次测试中使用了这个 Resnet。我正在重用我的代码来为网络加载和准备数据,它工作得很好。脚本的其余部分似乎也可以正常工作,除非它到达 fit_generator。在 fit_generator 它暂停了一段时间然后似乎退出了我有一个打印语句说“发生了什么?”我很困惑,因为我希望出现错误消息或程序崩溃或其他东西。我正在使用运行最新版本 anaconda 的 Windows 10。在我的condo环境中,我使用的是python 3.6,最新版本的Keras 2.3,最新版本的TensorFlow。如果有任何见解,我将不胜感激。
def batch_generator(X_train, Y_train):
while True:
for fl, lb in zip(X_train, Y_train):
sam, lam = get_IQsamples(fl, lb)
max_iter = sam.shape[0]
sample = [] # store all the generated data batches
label = [] # store all the generated label batches
i = 0
for d, l in zip(sam, lam):
sample.append(d)
label.append(l)
i += 1
if i == max_iter:
break
sample = np.asarray(sample)
label = np.asarray(label)
yield sample, label
def residual_stack(x, f):
# 1x1 conv linear
x = Conv2D(f, (1, 1), strides=1, padding='same', data_format='channels_last')(x)
x = Activation('linear')(x)
# residual unit 1
x_shortcut = x
x = Conv2D(f, (3, 2), strides=1, padding="same", data_format='channels_last')(x)
x = Activation('relu')(x)
x = Conv2D(f, 3, strides=1, padding="same", data_format='channels_last')(x)
x = Activation('linear')(x)
# add skip connection
if x.shape[1:] == x_shortcut.shape[1:]:
x = Add()([x, x_shortcut])
else:
raise Exception('Skip Connection Failure!')
# residual unit 2
x_shortcut = x
x = Conv2D(f, 3, strides=1, padding="same", data_format='channels_last')(x)
x = Activation('relu')(x)
x = Conv2D(f, 3, strides = 1, padding = "same", data_format='channels_last')(x)
x = Activation('linear')(x)
# add skip connection
if x.shape[1:] == x_shortcut.shape[1:]:
x = Add()([x, x_shortcut])
else:
raise Exception('Skip Connection Failure!')
# max pooling layer
x = MaxPooling2D(pool_size=2, strides=None, padding='valid', data_format='channels_last')(x)
return x
.
定义 ResNet 模型
# define resnet model
def ResNet(input_shape, classes):
# create input tensor
x_input = Input(input_shape)
x = x_input
# residual stack
num_filters = 40
x = residual_stack(x, num_filters)
x = residual_stack(x, num_filters)
x = residual_stack(x, num_filters)
x = residual_stack(x, num_filters)
x = residual_stack(x, num_filters)
# output layer
x = Flatten()(x)
x = Dense(128, activation="selu", kernel_initializer="he_normal")(x)
x = Dropout(.5)(x)
x = Dense(128, activation="selu", kernel_initializer="he_normal")(x)
x = Dropout(.5)(x)
x = Dense(classes , activation='softmax', kernel_initializer = glorot_uniform(seed=0))(x)
# Create model
model = Model(inputs = x_input, outputs = x)
model.summary()
return model
model = ResNet((32,32,2),8)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print('Load complete!')
print('\n')
steps = val_length_train // batchsize
valid_steps = val_length // batchsize
history = model.fit_generator(
generator=train_gen,
epochs=3,
verbose=0,
steps_per_epoch=steps,
validation_data=valid_gen,
validation_steps=valid_steps,
callbacks=[tensorboard])
print("what happened?")
【问题讨论】:
-
所以,您遇到了一个涉及生成器的问题。要是能看到生成器就好了。
-
我不在电脑旁,但生成器是我使用的标准模板,它在 5 个项目中运行良好。不知道为什么会出现问题,但你的正确我应该提供它。
-
这里是生成器。它是正交数据,来自无线电的 IQ 数据。
-
你的
verbose=0,它不会打印任何东西。 -
这里有一个类似的问题。我尝试了作者的方法,但没有帮助。我确实重建了一个新的 conda 环境,但行为仍然相同 stackoverflow.com/questions/57350547/…