我正在尝试使用 keras和tensorflow 编写代码,我想使用批处理大小。
您可以在https://github.com/aSafarpoor/vgg_vidclassification.git
中找到代码它可以与以下内容一起运行:
python main.py --net resnet34s --batch_size 1 --gpu 2,3 --lr 0.001 --warmup_ratio 0.1 --optimizer adam --epochs 4-多进程1 --loss softmax --hdim 30- -wdim 30 --frame_num 80 --aggregation_mode vlad --vlad_cluster 4
当我设置批次大小== 1时,它可以工作, 但是当我将其更改为另一个数字时,会出现如下错误:
Epoch 1/2
Learning rate for epoch 1 is 0.0001.
Traceback (most recent call last):
File "main.py", line 214, in <module> main()
File "main.py", line 145, in main verbose=1)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py",
line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line
1418, in fit_generator
initial_epoch=initial_epoch)
File "/usr/local/lib/python2.7/dist-
packages/keras/engine/training_generator.py", line 217, in fit_generator
class_weight=class_weight)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line
1217, in train_on_batch
outputs = self.train_function(ins)
File "/usr/local/lib/python2.7/dist-
packages/keras/backend/tensorflow_backend.py", line 2715, in __call__
return self._call(inputs)
File "/usr/local/lib/python2.7/dist-
packages/keras/backend/tensorflow_backend.py", line 2675, in _call
fetched = self._callable_fn(*array_vals)
File "/usr/local/lib/python2.7/dist-
packages/tensorflow/python/client/session.py", line 1458, in __call__
run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible
shapes: [2,5994] vs. [6,5994]
[[{{node
training/Adam/gradients/loss/prediction_loss/mul_grad/BroadcastGradientArgs}}]
]
我用不同的版本运行它,错误是相同的;像形状:[2]与[6]
您所看到的错误的重要部分是:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible
shapes: [2,5994] vs. [6,5994]
[[{{node
training/Adam/gradients/loss/prediction_loss/mul_grad/BroadcastGradientArgs}}]
]
关于代码:
首先,最重要的部分位于git可以访问的src文件夹中
此部分用于培训: network.fit_generator(trn_gen,
steps_per_epoch=int(len(partition['train'])//args.batch_size),
epochs=args.epochs,
max_queue_size=10,
callbacks=callbacks,
use_multiprocessing=False,
workers=1,
verbose=1)
在utils.py中阅读这样的视频:
vidcap = cv2.VideoCapture(path)
success,image = vidcap.read()
success2=success
while(success):
image= cv2.resize(image,(dim_h,dim_w))
sub_arr.append(image)
success,image = vidcap.read()
c+=1
while(c<spec_len and success2):
sub_arr.append(sub_arr[-1])
c+=1
# if(len(sub_arr)>0):
big_arr=sub_arr[:spec_len]
np_arr=np.asarray(big_arr)
# print(np_arr.shape," GgggG")
return np_arr
然后准备好数据:
trnlist, trnlb = toolkits.get_voxceleb2_datalist(args, path='../meta/voxlb2_train.txt')
vallist, vallb = toolkits.get_voxceleb2_datalist(args, path='../meta/voxlb2_val.txt')
partition = {'train': trnlist.flatten(), 'val': vallist.flatten()}
模型函数是model.py中的resnet_3D_v1,看看:
bn_axis = 3
if mode == 'train':
inputs = Input(shape=input_dim, name='input')
else:
# inputs = Input(shape=(input_dim[0], None, input_dim[-1]), name='input')
inputs = Input(shape=input_dim, name='input')
# ===============================================
# Convolution Block 1
# ===============================================
x1 = Conv3D(64, (1,1,1),
kernel_initializer='orthogonal',
use_bias=False, trainable=True,
kernel_regularizer=l2(weight_decay),
padding='same',
name='conv1_1/3x3_s1')(inputs)
x1 = BatchNormalization(axis=bn_axis, name='conv1_1/3x3_s1/bn', trainable=True)(x1)
x1 = Activation('relu')(x1)
x1 = MaxPooling3D((1, 1,1), strides=(1, 1,1))(x1)
# ===============================================
# Convolution Section 2
# ===============================================
x2 = conv_block_3D(x1, 3, [48, 48, 96], stage=2, block='a', strides=(1, 1,1), trainable=True)
x2 = identity_block_3D(x2, 3, [48, 48, 96], stage=2, block='b', trainable=True)
# ===============================================
# Convolution Section 3
# ===============================================
x3 = conv_block_3D(x2, 3, [96, 96, 128], stage=3, block='a', trainable=True)
x3 = identity_block_3D(x3, 3, [96, 96, 128], stage=3, block='b', trainable=True)
x3 = identity_block_3D(x3, 3, [96, 96, 128], stage=3, block='c', trainable=True)
# ===============================================
# Convolution Section 4
# ===============================================
x4 = conv_block_3D(x3, 3, [128, 128, 512], stage=4, block='a', trainable=True)
x4 = identity_block_3D(x4, 3, [128, 128, 512], stage=4, block='b', trainable=True)
x4 = identity_block_3D(x4, 3, [128, 128, 512], stage=4, block='c', trainable=True)
# ===============================================
# Convolution Section 5
# ===============================================
x5 = conv_block_3D(x4, 3, [512,512, 2048], stage=5, block='a', trainable=True)
x5 = identity_block_3D(x5, 3, [512,512, 2048], stage=5, block='b', trainable=True)
x5 = identity_block_3D(x5, 3, [512,512, 2048], stage=5, block='c', trainable=True)
y = MaxPooling3D((1, 1,1), strides=(1, 1,1), name='mpool2')(x5)
return inputs, y
,输入为(frame_counter,w,h,rgb = 3)
它也使用了model.py。