我想了解如何使用卷积神经网络将高分辨率图像生成低分辨率图像。
网络输入图像是否需要较小,而输出图像是其两倍大小?
我构建了以下模型:
w,h,c=x_train[0].shapeinput = Input(shape=(w,h,c),name='LR')x = UpSampling2D(size=(2,2), name='UP')(input)h = Dense(720, activation='relu', name ='hide')(x)h2= Dense(1280, activation='relu', name ='hide2')(h)output= Dense(3, activation='relu', name ='output')(h2)model = Model(inputs=input, outputs=output)model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])model.fit(x_train,y_train, epochs=50, verbose=0)
Y_train 的尺寸是 x_train 的两倍。
但我收到了以下错误信息:
ResourceExhaustedError: OOM when allocating tensor with shape[4608000,720] and type float on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu [[{{node hide/MatMul}}]]Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info
我做错了什么?
回答:
这种内存不足(OOM)错误通常是由于批次大小过大,无法适应内存所致。
我执行了
model.fit(x_train,y_train,batch_size=1024, epochs=50, verbose=0)
,结果占用了超过10%的系统内存。
那么1024听起来太大了。从较小的批次大小开始(例如约64),然后以2的幂逐渐增加(例如128, 256…),直到找到一个足够大但仍能适应内存的批次大小。
关于如何计算最佳批次大小的讨论可能也有帮助,参见如何计算最佳批次大小…